Documentation
¶
Index ¶
- Constants
- Variables
- func BindDriver(driverName string, bindType int)
- func BindType(driverName string) int
- func Get(q Queryer, dest any, query string, args ...any) error
- func GetContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
- func GetG[T any](ctx context.Context, q QueryerContext, query string, args ...any) (res T, err error)
- func In(query string, args ...any) (string, []any, error)
- func LoadFile(e Execer, path string) (*sql.Result, error)
- func LoadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error)
- func MapScan(r ColScanner, dest map[string]any) error
- func MustExec(e Execer, query string, args ...any) sql.Result
- func MustExecContext(ctx context.Context, e ExecerContext, query string, args ...any) sql.Result
- func Named(query string, arg any) (string, []any, error)
- func NamedExec(e Ext, query string, arg any) (sql.Result, error)
- func NamedExecContext(ctx context.Context, e ExtContext, query string, arg any) (sql.Result, error)
- func Rebind(bindType int, query string) string
- func Select(q Queryer, dest any, query string, args ...any) error
- func SelectContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
- func SelectG[T any](ctx context.Context, q QueryerContext, query string, args ...any) (res []T, err error)
- func SelectIter[T any](ctx context.Context, q QueryerContext, query string, args ...any) iter.Seq2[T, error]
- func SliceScan(r ColScanner) ([]any, error)
- func StructScan(r ColScanner, dest any) error
- type ColScanner
- type Conn
- func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (c *Conn) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (c *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error)
- func (c *Conn) QueryRowxContext(ctx context.Context, query string, args ...any) *Row
- func (c *Conn) QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)
- func (c *Conn) Rebind(query string) string
- func (c *Conn) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- type DB
- func Connect(driverName, dataSourceName string) (*DB, error)
- func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error)
- func MustConnect(driverName, dataSourceName string) *DB
- func MustOpen(driverName, dataSourceName string) *DB
- func NewDb(db *sql.DB, driverName string) *DB
- func Open(driverName, dataSourceName string) (*DB, error)
- func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Beginx() (*Tx, error)
- func (db *DB) BindNamed(query string, arg any) (string, []any, error)
- func (db *DB) Connx(ctx context.Context) (*Conn, error)
- func (db *DB) Get(dest any, query string, args ...any) error
- func (db *DB) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (db *DB) MapperFunc(mf func(string) string)
- func (db *DB) MustBegin() *Tx
- func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx
- func (db *DB) MustExec(query string, args ...any) sql.Result
- func (db *DB) MustExecContext(ctx context.Context, query string, args ...any) sql.Result
- func (db *DB) NamedExec(query string, arg any) (sql.Result, error)
- func (db *DB) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)
- func (db *DB) NamedQuery(query string, arg any) (*Rows, error)
- func (db *DB) NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)
- func (db *DB) PrepareNamed(query string) (*NamedStmt, error)
- func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)
- func (db *DB) Preparex(query string) (*Stmt, error)
- func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error)
- func (db *DB) QueryRowx(query string, args ...any) *Row
- func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...any) *Row
- func (db *DB) Queryx(query string, args ...any) (*Rows, error)
- func (db *DB) QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)
- func (db *DB) Rebind(query string) string
- func (db *DB) Select(dest any, query string, args ...any) error
- func (db *DB) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- type Execer
- type ExecerContext
- type Ext
- type ExtContext
- type NamedStmt
- func (n *NamedStmt) Close() error
- func (n *NamedStmt) Exec(arg any) (sql.Result, error)
- func (n *NamedStmt) ExecContext(ctx context.Context, arg any) (sql.Result, error)
- func (n *NamedStmt) Get(dest any, arg any) error
- func (n *NamedStmt) GetContext(ctx context.Context, dest any, arg any) error
- func (n *NamedStmt) MustExec(arg any) sql.Result
- func (n *NamedStmt) MustExecContext(ctx context.Context, arg any) sql.Result
- func (n *NamedStmt) Query(arg any) (*sql.Rows, error)
- func (n *NamedStmt) QueryContext(ctx context.Context, arg any) (*sql.Rows, error)
- func (n *NamedStmt) QueryRow(arg any) *Row
- func (n *NamedStmt) QueryRowContext(ctx context.Context, arg any) *Row
- func (n *NamedStmt) QueryRowx(arg any) *Row
- func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg any) *Row
- func (n *NamedStmt) Queryx(arg any) (*Rows, error)
- func (n *NamedStmt) QueryxContext(ctx context.Context, arg any) (*Rows, error)
- func (n *NamedStmt) Select(dest any, arg any) error
- func (n *NamedStmt) SelectContext(ctx context.Context, dest any, arg any) error
- type Preparer
- type PreparerContext
- type Queryer
- type QueryerContext
- type Row
- type Rows
- type Stmt
- func (s *Stmt) Get(dest any, args ...any) error
- func (s *Stmt) GetContext(ctx context.Context, dest any, args ...any) error
- func (s *Stmt) MustExec(args ...any) sql.Result
- func (s *Stmt) MustExecContext(ctx context.Context, args ...any) sql.Result
- func (s *Stmt) QueryRowx(args ...any) *Row
- func (s *Stmt) QueryRowxContext(ctx context.Context, args ...any) *Row
- func (s *Stmt) Queryx(args ...any) (*Rows, error)
- func (s *Stmt) QueryxContext(ctx context.Context, args ...any) (*Rows, error)
- func (s *Stmt) Select(dest any, args ...any) error
- func (s *Stmt) SelectContext(ctx context.Context, dest any, args ...any) error
- type Tx
- func (tx *Tx) BindNamed(query string, arg any) (string, []any, error)
- func (tx *Tx) Get(dest any, query string, args ...any) error
- func (tx *Tx) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (tx *Tx) MustExec(query string, args ...any) sql.Result
- func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...any) sql.Result
- func (tx *Tx) NamedExec(query string, arg any) (sql.Result, error)
- func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)
- func (tx *Tx) NamedQuery(query string, arg any) (*Rows, error)
- func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)
- func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt
- func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt
- func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error)
- func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)
- func (tx *Tx) Preparex(query string) (*Stmt, error)
- func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error)
- func (tx *Tx) QueryRowx(query string, args ...any) *Row
- func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...any) *Row
- func (tx *Tx) Queryx(query string, args ...any) (*Rows, error)
- func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)
- func (tx *Tx) Rebind(query string) string
- func (tx *Tx) Select(dest any, query string, args ...any) error
- func (tx *Tx) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- func (tx *Tx) Stmtx(stmt any) *Stmt
- func (tx *Tx) StmtxContext(ctx context.Context, stmt any) *Stmt
Constants ¶
const ( UNKNOWN = bind.UNKNOWN QUESTION = bind.QUESTION DOLLAR = bind.DOLLAR NAMED = bind.NAMED AT = bind.AT )
Bindvar type constants. These are re-exported from the internal bind package to maintain API compatibility with jmoiron/sqlx.
Variables ¶
var ( // ErrBindMismatch is returned when there is a mismatch between query variables and arguments. ErrBindMismatch = bind.ErrBindMismatch // ErrNamedPropertyNotFound is returned when a named parameter cannot be found in the provided struct or map. ErrNamedPropertyNotFound = bind.ErrNamedPropertyNotFound // ErrUnsupportedType is returned when an unsupported type is provided for binding. ErrUnsupportedType = bind.ErrUnsupportedType // ErrSyntax is returned when there is a syntax error in the query parsing. ErrSyntax = bind.ErrSyntax // ErrColumnNotFound is returned when a column from the database cannot be mapped to a destination. ErrColumnNotFound = bind.ErrColumnNotFound )
var ErrStopIter = fmt.Errorf("stop iteration")
var NameMapper = strings.ToLower
NameMapper maps column names to struct field names. By default, it uses strings.ToLower to lowercase struct field names. It can be set to whatever you want, but it is encouraged to be set before sqlx is used as name-to-field mappings are cached after first use on a type.
Functions ¶
func BindDriver ¶
BindDriver sets the BindType for driverName to bindType.
func Get ¶
Get does a QueryRow using the provided Queryer, and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used.
func GetContext ¶
GetContext does a QueryRowContext using the provided QueryerContext, and scans the resulting row to dest.
func GetG ¶
func GetG[T any](ctx context.Context, q QueryerContext, query string, args ...any) (res T, err error)
GetG executes a query that is expected to return at most one row, scanning it into a value of type T. It uses pre-calculated struct field offsets for efficient scanning.
user, err := sqlx.GetG[User](ctx, db, "SELECT * FROM users WHERE id = ?", 1)
func In ¶
In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The query should use the ? bindVar. The return value uses the ? bindVar.
func LoadFileContext ¶
LoadFileContext exec's every statement in a file with context.
func MapScan ¶
func MapScan(r ColScanner, dest map[string]any) error
MapScan scans a single row into a map[string]any.
func MustExecContext ¶
MustExecContext execs the query using e and panics if there was an error.
func Named ¶
Named takes a query using named parameters and an argument and returns a new query with the named parameters replaced with positional parameters and the corresponding argument list.
func NamedExecContext ¶
NamedExecContext uses BindNamed to bind a query and then runs Exec on the result.
func Select ¶
Select executes a query using the provided Queryer, and StructScans each row into dest, which must be a slice.
func SelectContext ¶
func SelectContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
SelectContext executes a query using the provided QueryerContext, and StructScans each row into dest, which must be a slice.
func SelectG ¶
func SelectG[T any](ctx context.Context, q QueryerContext, query string, args ...any) (res []T, err error)
SelectG executes a query and returns a slice of T, using generics for type safety. It uses pre-calculated struct field offsets for efficient scanning.
users, err := sqlx.SelectG[User](ctx, db, "SELECT * FROM users WHERE active = ?", true)
func SelectIter ¶
func SelectIter[T any](ctx context.Context, q QueryerContext, query string, args ...any) iter.Seq2[T, error]
SelectIter returns an iterator (iter.Seq2) that streams rows one at a time, scanning each into a value of type T using pre-calculated struct field offsets. This avoids loading all results into memory at once.
for user, err := range sqlx.SelectIter[User](ctx, db, "SELECT * FROM users") {
if err != nil {
return err
}
process(user)
}
func StructScan ¶
func StructScan(r ColScanner, dest any) error
StructScan scans a ColScanner (Row or Rows) into a struct. It respects the unsafe flag from Row or Rows; if not in unsafe mode, unmapped columns will cause an error.
Types ¶
type ColScanner ¶
ColScanner defines the interface for MapScan and SliceScan.
type Conn ¶
type Conn struct {
*sql.Conn
DriverName string
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
}
Conn wraps sql.Conn with extra functionality.
func (*Conn) GetContext ¶
GetContext using this Conn.
func (*Conn) PreparexContext ¶
PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
func (*Conn) QueryRowxContext ¶
QueryRowxContext queries the database and returns an *sqlx.Row.
func (*Conn) QueryxContext ¶
QueryxContext queries the database and returns an *sqlx.Rows.
type DB ¶
type DB struct {
*sql.DB
DriverName string
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
}
DB wraps sql.DB with extra functionality.
func ConnectContext ¶
ConnectContext to a database and verify with a ping.
func MustConnect ¶
MustConnect connects to a database and panics on error.
func MustOpen ¶
MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error.
func NewDb ¶
NewDb returns a new sqlx DB wrapper for a pre-existing *sql.DB. The driverName of the original database is required for named query support.
func (*DB) Get ¶
Get using this DB. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.
func (*DB) GetContext ¶
GetContext using this DB.
func (*DB) MapperFunc ¶
MapperFunc sets a new mapper for this db using the default sqlx struct tag and the provided mapper function.
func (*DB) MustBeginTx ¶
MustBeginTx starts a transaction with context, and panics on error.
func (*DB) MustExecContext ¶
MustExecContext (panic) runs MustExecContext using this database.
func (*DB) NamedExec ¶
NamedExec using this DB. Any named placeholder parameters are replaced with fields from arg.
func (*DB) NamedExecContext ¶
NamedExecContext using this DB.
func (*DB) NamedQuery ¶
NamedQuery using this DB. Any named placeholder parameters are replaced with fields from arg.
func (*DB) NamedQueryContext ¶
NamedQueryContext using this DB.
func (*DB) PrepareNamed ¶
PrepareNamed returns an sqlx.NamedStmt.
func (*DB) PrepareNamedContext ¶
PrepareNamedContext returns an sqlx.NamedStmt.
func (*DB) PreparexContext ¶
PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
func (*DB) QueryRowxContext ¶
QueryRowxContext queries the database and returns an *sqlx.Row.
func (*DB) QueryxContext ¶
QueryxContext queries the database and returns an *sqlx.Rows.
type ExecerContext ¶
type ExecerContext interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
ExecerContext defines the interface for MustExecContext and LoadFileContext.
type ExtContext ¶
type ExtContext interface {
QueryerContext
ExecerContext
}
ExtContext defines a union interface which can Bind, Query, and Exec with context.
type NamedStmt ¶
type NamedStmt struct {
Stmt *sql.Stmt
QueryString string
Params []string
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
}
NamedStmt is a prepared statement that executes named queries. Prepare it how you would execute a NamedQuery, but pass in a struct or map when executing.
func (*NamedStmt) Exec ¶
Exec executes a named statement using the struct passed. If the struct is a slice or array, it iterates and executes for each element.
func (*NamedStmt) ExecContext ¶
ExecContext executes a named statement using the struct passed. If the struct is a slice or array, it iterates and executes for each element.
func (*NamedStmt) GetContext ¶
GetContext using this NamedStmt.
func (*NamedStmt) MustExecContext ¶
MustExecContext (panic) runs MustExecContext using this NamedStmt.
func (*NamedStmt) Query ¶
Query executes a named statement using the struct passed, returns *sql.Rows.
func (*NamedStmt) QueryContext ¶
QueryContext executes a named statement using the struct passed, returns *sql.Rows.
func (*NamedStmt) QueryRow ¶
QueryRow executes a named statement using the struct passed, returns *sqlx.Row.
func (*NamedStmt) QueryRowContext ¶
QueryRowContext executes a named statement returning an *sqlx.Row.
func (*NamedStmt) QueryRowx ¶
QueryRowx executes a named statement using the struct passed, returns *sqlx.Row.
func (*NamedStmt) QueryRowxContext ¶
QueryRowxContext executes a named statement returning an *sqlx.Row.
func (*NamedStmt) QueryxContext ¶
QueryxContext executes a named statement returning an *sqlx.Rows.
type PreparerContext ¶
type PreparerContext interface {
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}
PreparerContext defines the interface for PreparexContext.
type Queryer ¶
type Queryer interface {
Query(query string, args ...any) (*sql.Rows, error)
Queryx(query string, args ...any) (*Rows, error)
QueryRowx(query string, args ...any) *Row
}
Queryer defines the interface for Get and Select.
type QueryerContext ¶
type QueryerContext interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)
QueryRowxContext(ctx context.Context, query string, args ...any) *Row
}
QueryerContext defines the interface for GetContext and SelectContext.
type Row ¶
type Row struct {
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
// contains filtered or unexported fields
}
Row reimplements sql.Row to provide access to the underlying sql.Rows object.
func (*Row) ColumnTypes ¶
func (r *Row) ColumnTypes() ([]*sql.ColumnType, error)
ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error.
func (*Row) StructScan ¶
StructScan a single Row into dest.
type Rows ¶
type Rows struct {
*sql.Rows
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
// contains filtered or unexported fields
}
Rows wraps sql.Rows to cache costly reflection operations.
func NamedQuery ¶
NamedQuery binds a named query and then runs Query on the result using the provided Ext (which is typically a *DB or *Tx).
func NamedQueryContext ¶
NamedQueryContext binds a named query and then runs Query on the result using the provided ExtContext.
func (*Rows) StructScan ¶
type Stmt ¶
Stmt wraps sql.Stmt with extra functionality.
func PreparexContext ¶
PreparexContext prepares a statement with context and returns an *sqlx.Stmt.
func (*Stmt) GetContext ¶
GetContext using this prepared statement.
func (*Stmt) MustExecContext ¶
MustExecContext (panic) runs MustExecContext using this statement.
func (*Stmt) QueryRowxContext ¶
QueryRowxContext using this prepared statement.
func (*Stmt) QueryxContext ¶
QueryxContext using this prepared statement.
type Tx ¶
type Tx struct {
*sql.Tx
DriverName string
Mapper *reflectx.Mapper
Unsafe bool
StrictTagParsing bool
}
Tx wraps sql.Tx with extra functionality.
func (*Tx) GetContext ¶
GetContext within this transaction.
func (*Tx) MustExecContext ¶
MustExecContext runs MustExecContext within this transaction.
func (*Tx) NamedExecContext ¶
NamedExecContext a named query within this transaction.
func (*Tx) NamedQuery ¶
NamedQuery within this transaction.
func (*Tx) NamedQueryContext ¶
NamedQueryContext within this transaction.
func (*Tx) NamedStmt ¶
NamedStmt returns a version of the prepared statement which runs within a transaction.
func (*Tx) NamedStmtContext ¶
NamedStmtContext returns a version of the prepared statement which runs within a transaction.
func (*Tx) PrepareNamed ¶
PrepareNamed returns an sqlx.NamedStmt.
func (*Tx) PrepareNamedContext ¶
PrepareNamedContext returns an sqlx.NamedStmt.
func (*Tx) PreparexContext ¶
PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
func (*Tx) QueryRowxContext ¶
QueryRowxContext within this transaction.
func (*Tx) QueryxContext ¶
QueryxContext within this transaction.
func (*Tx) SelectContext ¶
SelectContext within this transaction.