queryrepo

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package queryrepo enables the use of centralized storage for all SQL queries used in an application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadQueryFromFs added in v0.1.2

func LoadQueryFromFs(f fs.FS, rootPath, collectionName, queryName string) (string, error)

LoadQueryFromFs retrieves a query from a filesystem. It needs the root path to start the search from, as well as a collection name and a query name. The collection name equals to a direct directory name in the root path. The query name is the file name (without extension) to load the contents from. It returns and empty string and an error if the file cannot be found.

func Prepare

func Prepare[T Preparer](t T, r *Repository, collectionName, queryName string) (*sql.Stmt, error)

Prepare creates a prepared statement for the supplied Preparer by looking up a query in the supplied repository. It returns an nil pointer and an error if either the query cannot be found in the supplied repository, or the statement preparation fails.

func PrepareContext added in v0.1.7

func PrepareContext[T Preparer](ctx context.Context, t T, r *Repository, collectionName, queryName string) (*sql.Stmt, error)

Prepare creates a prepared statement for the supplied Preparer by looking up a query in the supplied repository using a context. It returns an nil pointer and an error if either the query cannot be found in the supplied repository, or the statement preparation fails.

func PrepareFromFs added in v0.1.2

func PrepareFromFs[T Preparer](t T, f fs.FS, rootPath, collectionName, queryName string) (*sql.Stmt, error)

PrepareFromFs creates a prepared statement for the supplied Preparer by looking up a query in the supplied filesystem. It returns an nil pointer and an error if either the query cannot be found in the supplied filesystem, or the statement preparation fails.

func PrepareFromFsContext added in v0.1.7

func PrepareFromFsContext[T Preparer](ctx context.Context, t T, f fs.FS, rootPath, collectionName, queryName string) (*sql.Stmt, error)

PrepareFromFs creates a prepared statement for the supplied Preparer by looking up a query in the supplied filesystem using a context. It returns an nil pointer and an error if either the query cannot be found in the supplied filesystem, or the statement preparation fails.

Types

type Preparer added in v0.1.2

type Preparer interface {
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Preparer defines the interface to create a prepared statement.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

A Repository stores multiple collections of queries in a map for later use. Queries can either be retrieved by their name, or be used to create a prepared statement.

func NewFromFs

func NewFromFs(f fs.FS, rootPath string) (*Repository, error)

NewFromFs creates a new repository using a filesystem. It takes a filesystem and a root path to start loading files from and returns an error if files cannot be loaded.

func (*Repository) DbPrepare

func (r *Repository) DbPrepare(db *sql.DB, collectionName, queryName string) (*sql.Stmt, error)

DbPrepare creates a prepared statement for the supplied database handle. It takes a collection name and query name to look up the query to create the prepared statement.

func (*Repository) DbPrepareContext added in v0.1.7

func (r *Repository) DbPrepareContext(ctx context.Context, db *sql.DB, collectionName, queryName string) (*sql.Stmt, error)

DbPrepareContext creates a prepared statement for the supplied database handle using a context. It takes a collection name and query name to look up the query to create the prepared statement.

func (*Repository) Get

func (r *Repository) Get(collectionName, queryName string) (string, error)

Get retrieves the supplied query from the repository. It takes a collection name and a query name to perform the lookup and returns an empty string and an error if the query cannot be found in the collection.

func (*Repository) TxPrepare

func (r *Repository) TxPrepare(tx *sql.Tx, collectionName, queryName string) (*sql.Stmt, error)

TxPrepare creates a prepared statement for the supplied in-progress database transaction. It takes a collection name and query name to look up the query to create the prepared statement.

func (*Repository) TxPrepareContext added in v0.1.7

func (r *Repository) TxPrepareContext(ctx context.Context, tx *sql.Tx, collectionName, queryName string) (*sql.Stmt, error)

TxPrepare creates a prepared statement for the supplied in-progress database transaction using a context. It takes a collection name and query name to look up the query to create the prepared statement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL