dbquery

package
v0.0.0-...-44d9088 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 6 Imported by: 0

README

db-query

In this package we make generic imlpementation of querying.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAPI = mustNewAPI()

Functions

func NotFound

func NotFound(err error) bool

NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.

func QuestionDelim

func QuestionDelim(builder *strings.Builder, index uint8) error

QuestionDelim delimeter format is used by most MySQL database client drivers

func ScanRow

func ScanRow(dst interface{}, rows Rows) error

ScanRow is a package-level helper function that uses the DefaultAPI object. See API.ScanRow for details.

func SequentialDollarDelim

func SequentialDollarDelim(builder *strings.Builder, index uint8) error

SequentialDollarDelim delimeter format is used by most Postgres database client drivers natively

func SnakeCaseMapper

func SnakeCaseMapper(str string) string

SnakeCaseMapper is a NameMapperFunc that maps struct field to snake case.

Types

type API

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

func NewAPI

func NewAPI(opts ...APIOption) (*API, error)

func (*API) NamedQueryParams

func (api *API) NamedQueryParams(query string, arg interface{}) (string, []interface{}, error)

func (*API) NewRowScanner

func (api *API) NewRowScanner(rows Rows) *RowScanner

NewRowScanner returns a new instance of the RowScanner.

func (*API) PrepareNamed

func (api *API) PrepareNamed(query string, args ...interface{}) (*PreparedQuery, error)

Prepares named queries Prepared queries save a decent amount of computation that need to be done per query. Without preparation each query would approximately take 2000ns.

func (*API) ScanAll

func (api *API) ScanAll(dst interface{}, rows Rows) error

ScanAll iterates all rows to the end. After iterating it closes the rows, and propagates any errors that could pop up. It expects that destination should be a slice. For each row it scans data and appends it to the destination slice. ScanAll supports both types of slices: slice of structs by a pointer and slice of structs by value, for example:

type User struct {
    ID    string
    Name  string
    Email string
    Age   int
}

var usersByPtr []*User
var usersByValue []User

Both usersByPtr and usersByValue are valid destinations for ScanAll function.

Before starting, ScanAll resets the destination slice, so if it's not empty it will overwrite all existing elements.

func (*API) ScanOne

func (api *API) ScanOne(dst interface{}, rows Rows) error

ScanOne iterates all rows to the end and makes sure that there was exactly one row otherwise it returns an error. Use NotFound function to check if there were no rows. After iterating ScanOne closes the rows, and propagates any errors that could pop up. It scans data from that single row into the destination.

func (*API) ScanRow

func (api *API) ScanRow(dst interface{}, rows Rows) error

ScanRow creates a new RowScanner and calls RowScanner.Scan that scans current row data into the destination. It's just a helper function if you don't bother with efficiency and don't want to instantiate a new RowScanner before iterating the rows, so it could cache the reflection work between Scan calls. See RowScanner for details.

type APIOption

type APIOption func(api *API)

func WithLexer

func WithLexer(delim rune, compileDelim DriverDelim) APIOption

WithLexer allows to set a custom

func WithStructTagKey

func WithStructTagKey(structTagKey string) APIOption

WithStructTagKey allows to use a custom struct tag key. The default tag key is `db`.

type DriverDelim

type DriverDelim = func(builder *strings.Builder, index uint8) error

DriverDelim is type of function accepted by the internal lexer for appending named arguments in the format of the native driver

type Lexer

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

func (Lexer) Compile

func (l Lexer) Compile(sql string) (string, []string, error)

Compile is used for parsing and compiling sql queries returns the compiled query as the first parameter and the second parameter consists of all of the

type NameMapperFunc

type NameMapperFunc func(string) string

NameMapperFunc is a function type that maps a struct field name to the database column name.

type PreparedQuery

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

func (*PreparedQuery) GetQuery

func (pq *PreparedQuery) GetQuery(arg interface{}) (string, []interface{}, error)

GetQuery returns the array of the values behind the named params

type RowScanner

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

RowScanner embraces Rows and exposes the Scan method that allows scanning data from the current row into the destination. The first time the Scan method is called it parses the destination type via reflection and caches all required information for further scans. Due to this caching mechanism, it's not allowed to call Scan for destinations of different types, the behavior is unknown in that case. RowScanner doesn't proceed to the next row nor close them, it should be done by the client code.

The main benefit of using this type directly is that you can instantiate a RowScanner and manually iterate over the rows and control how data is scanned from each row. This can be beneficial if the result set is large and you don't want to allocate a slice for all rows at once as it would be done in ScanAll.

ScanOne and ScanAll both use RowScanner type internally.

func NewRowScanner

func NewRowScanner(rows Rows) *RowScanner

NewRowScanner is a package-level helper function that uses the DefaultAPI object. See API.NewRowScanner for details.

func (*RowScanner) Scan

func (rs *RowScanner) Scan(dst interface{}) error

Scan scans data from the current row into the destination. On the first call it caches expensive reflection work and uses it the future calls. See RowScanner for details.

type Rows

type Rows interface {
	Close() error
	Err() error
	Next() bool
	Columns() ([]string, error)
	Scan(dest ...interface{}) error
}

Rows is an abstract database rows that dbscan can iterate over and get the data from. This interface is used to decouple from any particular database library.

Jump to

Keyboard shortcuts

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