Documentation ¶
Index ¶
- Variables
- func NotFound(err error) bool
- func QuestionDelim(builder *strings.Builder, index uint8) error
- func ScanRow(dst interface{}, rows Rows) error
- func SequentialDollarDelim(builder *strings.Builder, index uint8) error
- func SnakeCaseMapper(str string) string
- type API
- func (api *API) NamedQueryParams(query string, arg interface{}) (string, []interface{}, error)
- func (api *API) NewRowScanner(rows Rows) *RowScanner
- func (api *API) PrepareNamed(query string, args ...interface{}) (*PreparedQuery, error)
- func (api *API) ScanAll(dst interface{}, rows Rows) error
- func (api *API) ScanOne(dst interface{}, rows Rows) error
- func (api *API) ScanRow(dst interface{}, rows Rows) error
- type APIOption
- type DriverDelim
- type Lexer
- type NameMapperFunc
- type PreparedQuery
- type RowScanner
- type Rows
Constants ¶
This section is empty.
Variables ¶
var DefaultAPI = mustNewAPI()
Functions ¶
func NotFound ¶
NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.
func QuestionDelim ¶
QuestionDelim delimeter format is used by most MySQL database client drivers
func ScanRow ¶
ScanRow is a package-level helper function that uses the DefaultAPI object. See API.ScanRow for details.
func SequentialDollarDelim ¶
SequentialDollarDelim delimeter format is used by most Postgres database client drivers natively
func SnakeCaseMapper ¶
SnakeCaseMapper is a NameMapperFunc that maps struct field to snake case.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
func (*API) NamedQueryParams ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithStructTagKey allows to use a custom struct tag key. The default tag key is `db`.
type DriverDelim ¶
DriverDelim is type of function accepted by the internal lexer for appending named arguments in the format of the native driver
type NameMapperFunc ¶
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.