Documentation ¶
Index ¶
- func Load(in string) map[string]string
- type NamedDB
- func (db NamedDB) NamedGet(dest any, query string, arg any) error
- func (db NamedDB) NamedGetContext(ctx context.Context, dest any, query string, arg any) error
- func (db NamedDB) NamedSelect(dest any, query string, arg any) error
- func (db NamedDB) NamedSelectContext(ctx context.Context, dest any, query string, arg any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load parses the input string and extracts SQL queries into a map. It expects the input to be a string containing SQL queries separated by a prefix. The queries are identified by lines prefixed with "---- sqlfName: $name". Comment lines starting with "--" are ignored.
Parameters: - in: the input string containing the SQL queries
Returns: - a map containing the extracted SQL queries, where the key is the name and the value is the query string The intended use is to call Load in an init script, and assign the output to a package level variable that then can be used then in the queries. //go:embed mySqlFile.sql var mySqlString string var mySqlQueries map[string]string
func init() { mySqlQueries = sqlf.Load(mySqlString) } func retrieve(....) .... { err = db.Get(&id, mySqlQueries["queryName"], arg) }
The idea behind it is to have sql queries separated from code to take advantage of ide tools, and also to make navigation in the project easier.
Types ¶
type NamedDB ¶
NamedDB is a type that represents database with additional named parameter support. It is a wrapper around *sqlx.DB, which is an extension of the standard sql.DB type.
func (NamedDB) NamedGet ¶
NamedGet performs a named query and retrieves a single row data into the specified destination. It takes the destination pointer, the query string, and the argument as parameters. The destination must be a pointer to a struct, and the argument must be a struct or a map. If any error occurs during the preparation or retrieval, it is returned.
func (NamedDB) NamedGetContext ¶
NamedGetContext performs a named query and retrieves a single row data into the specified destination using a context. It takes the context, the destination pointer, the query string, and the argument as parameters. The destination must be a pointer to a struct, and the argument must be a struct or a map. If any error occurs during the preparation or retrieval, it is returned.
func (NamedDB) NamedSelect ¶
NamedSelect executes a named query and retrieves multiple rows of data into the specified destination. It takes the destination pointer, the query string, and the argument as parameters. The destination must be a pointer to a slice of structs, and the argument must be a struct or a map. If any error occurs during the preparation or retrieval, it is returned.
func (NamedDB) NamedSelectContext ¶
NamedSelectContext performs a named query and retrieves multiple rows of data into the specified destination using a context. It takes the context, the destination pointer, the query string, and the argument as parameters. The destination must be a pointer to a slice of structs, and the argument must be a struct or a map. If any error occurs during the preparation or retrieval, it is returned.