sqlf

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: Unlicense Imports: 3 Imported by: 0

README

go-sqlf

go-sqlf is a collection of helpers that work hand in hand with sqlx to make your life easier.

sqlf stands for SQL File, this library takes inspiration on https://github.com/sqlc-dev/sqlc way of parsing sql files to generate go files, but only cares about the parsing. SQLf only has one method called load, it loads several queries from a string which should have been previously embedded into your go binary.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(in string) map[string]string

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

type NamedDB struct {
	*sqlx.DB
}

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

func (db NamedDB) NamedGet(dest any, query string, arg any) error

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

func (db NamedDB) NamedGetContext(ctx context.Context, dest any, query string, arg any) error

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

func (db NamedDB) NamedSelect(dest any, query string, arg any) error

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

func (db NamedDB) NamedSelectContext(ctx context.Context, dest any, query string, arg any) error

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.

Jump to

Keyboard shortcuts

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