Documentation
¶
Overview ¶
Package db 提供了 dbx 的基础设施层实现。 它包含 DB、Tx、Stmt、Conn 等具体数据库封装类型, 以及连接管理、预编译语句等基础设施功能。
Index ¶
- func BindArray(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error)
- func BindMap(bindType int, query string, args map[string]any) (string, []any, error)
- func BindNamed(bindType int, query string, arg any) (string, []any, error)
- func BindNamedMapper(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error)
- func BindStruct(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error)
- func CompileNamedQuery(qs []byte, bindType int) (query string, names []string, err error)
- func FixBound(bound string, loop int) string
- func GetContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
- func IsUnsafe(i any) bool
- func LoadFile(e core.Execer, path string) (*sql.Result, error)
- func LoadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error)
- func MapperFor(i any) *reflectx.Mapper
- 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 core.Ext, query string, arg any) (sql.Result, error)
- func NamedExecContext(ctx context.Context, e ExtContext, query string, arg any) (sql.Result, error)
- func NamedQuery(e core.Ext, query string, arg any) (*core.Rows, error)
- func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg any) (*core.Rows, error)
- func SelectContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
- func WithSetUnsafe(u bool) func(*dbOptions)
- func WithUnsafe() func(*dbOptions)
- type Conn
- func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (c *Conn) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, 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) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (c *Conn) QueryRowxContext(ctx context.Context, query string, args ...any) *core.Row
- func (c *Conn) QueryxContext(ctx context.Context, query string, args ...any) (*core.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, args ...func(*dbOptions)) (*DB, error)
- func ConnectContext(ctx context.Context, driverName, dataSourceName string, ...) (*DB, error)
- func MustConnect(driverName, dataSourceName string, args ...func(*dbOptions)) *DB
- func MustOpen(driverName, dataSourceName string, args ...func(*dbOptions)) *DB
- func NewDb(d *sql.DB, driverName string, args ...func(*dbOptions)) *DB
- func Open(driverName, dataSourceName string, args ...func(*dbOptions)) (*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) DriverName() string
- func (db *DB) Exec(query string, args ...any) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, 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) (*core.Rows, error)
- func (db *DB) NamedQueryContext(ctx context.Context, query string, arg any) (*core.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) Query(query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...any) *sql.Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (db *DB) QueryRowx(query string, args ...any) *core.Row
- func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...any) *core.Row
- func (db *DB) Queryx(query string, args ...any) (*core.Rows, error)
- func (db *DB) QueryxContext(ctx context.Context, query string, args ...any) (*core.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
- func (db *DB) SetUnsafe(u bool)
- func (db *DB) Unsafe() *DB
- type ExecerContext
- 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) *core.Row
- func (n *NamedStmt) QueryRowContext(ctx context.Context, arg any) *core.Row
- func (n *NamedStmt) QueryRowx(arg any) *core.Row
- func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg any) *core.Row
- func (n *NamedStmt) Queryx(arg any) (*core.Rows, error)
- func (n *NamedStmt) QueryxContext(ctx context.Context, arg any) (*core.Rows, error)
- func (n *NamedStmt) Select(dest any, arg any) error
- func (n *NamedStmt) SelectContext(ctx context.Context, dest any, arg any) error
- func (n *NamedStmt) Unsafe() *NamedStmt
- type PreparerContext
- type QStmt
- func (q *QStmt) Exec(query string, args ...any) (sql.Result, error)
- func (q *QStmt) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (q *QStmt) Query(query string, args ...any) (*sql.Rows, error)
- func (q *QStmt) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (q *QStmt) QueryRowx(query string, args ...any) *core.Row
- func (q *QStmt) QueryRowxContext(ctx context.Context, query string, args ...any) *core.Row
- func (q *QStmt) Queryx(query string, args ...any) (*core.Rows, error)
- func (q *QStmt) QueryxContext(ctx context.Context, query string, args ...any) (*core.Rows, error)
- type Queryable
- type QueryerContext
- 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) *core.Row
- func (s *Stmt) QueryRowxContext(ctx context.Context, args ...any) *core.Row
- func (s *Stmt) Queryx(args ...any) (*core.Rows, error)
- func (s *Stmt) QueryxContext(ctx context.Context, args ...any) (*core.Rows, error)
- func (s *Stmt) Select(dest any, args ...any) error
- func (s *Stmt) SelectContext(ctx context.Context, dest any, args ...any) error
- func (s *Stmt) Unsafe() *Stmt
- type Tx
- func (tx *Tx) BindNamed(query string, arg any) (string, []any, error)
- func (tx *Tx) DriverName() string
- func (tx *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, 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) (*core.Rows, error)
- func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg any) (*core.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) Query(query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryRow(query string, args ...any) *sql.Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (tx *Tx) QueryRowx(query string, args ...any) *core.Row
- func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...any) *core.Row
- func (tx *Tx) Queryx(query string, args ...any) (*core.Rows, error)
- func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...any) (*core.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
- func (tx *Tx) Unsafe() *Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindNamedMapper ¶
func BindNamedMapper(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error)
BindNamedMapper 使用指定的 Mapper 进行命名绑定。
func BindStruct ¶
BindStruct 将命名参数查询与结构体参数的字段绑定。
func CompileNamedQuery ¶
CompileNamedQuery 将命名查询编译为未绑定的查询和名称列表。
func GetContext ¶
GetContext 使用提供的 Queryer 执行 QueryRow,并将结果行扫描到 dest 中。
func LoadFileContext ¶
LoadFileContext 执行文件中的每条语句。
func MustExecContext ¶
MustExecContext 使用 e 执行查询,如果发生错误则 panic。
func NamedExecContext ¶
NamedExecContext 使用 BindStruct 获取可执行的查询并运行 Exec。
func NamedQuery ¶
NamedQuery 绑定命名查询并运行 Query。
func NamedQueryContext ¶
func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg any) (*core.Rows, error)
NamedQueryContext 绑定命名查询并运行 Query。
func SelectContext ¶
func SelectContext(ctx context.Context, q QueryerContext, dest any, query string, args ...any) error
SelectContext 使用提供的 Queryer 执行查询,并将每行通过 StructScan 扫描到 dest 中。
func WithSetUnsafe ¶
func WithSetUnsafe(u bool) func(*dbOptions)
WithSetUnsafe 返回一个选项,设置构建出的类型的不安全模式。
Types ¶
type Conn ¶
Conn 是 sql.Conn 的封装,提供额外的功能。
func (*Conn) ExecContext ¶
ExecContext 在 Conn 和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Conn) GetContext ¶
GetContext 使用此 Conn 获取单条记录。
func (*Conn) PreparexContext ¶
PreparexContext 返回 Stmt 而不是 sql.Stmt。
func (*Conn) QueryContext ¶
QueryContext 在 Conn 和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Conn) QueryRowContext ¶
QueryRowContext 在 Conn 和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Conn) QueryRowxContext ¶
QueryRowxContext 查询数据库并返回 *Row。
func (*Conn) QueryxContext ¶
QueryxContext 查询数据库并返回 *Rows。
type DB ¶
DB 是 sql.DB 的封装,在 Open 时记录 driverName, 主要用于使用正确的绑定变量自动绑定命名查询。
func ConnectContext ¶
func ConnectContext(ctx context.Context, driverName, dataSourceName string, args ...func(*dbOptions)) (*DB, error)
ConnectContext 连接到数据库并通过 ping 验证连接。
func MustConnect ¶
MustConnect 连接到数据库并在出错时 panic。
func (*DB) DriverName ¶
DriverName 返回传递给此 DB 的 Open 函数的 driverName。
func (*DB) ExecContext ¶
ExecContext 在 context 下执行查询并记录 SQL 日志(仅 debug 模式)。
func (*DB) GetContext ¶
GetContext 使用此 DB 获取单条记录。
func (*DB) MapperFunc ¶
MapperFunc 为此 DB 设置一个新的 Mapper。
func (*DB) MustBeginTx ¶
MustBeginTx 开始一个事务,出错时 panic。
func (*DB) MustExecContext ¶
MustExecContext 使用此数据库运行 MustExec,出错时 panic。
func (*DB) NamedExecContext ¶
NamedExecContext 使用此 DB 执行命名查询。
func (*DB) NamedQuery ¶
NamedQuery 使用此 DB 进行命名查询。
func (*DB) NamedQueryContext ¶
NamedQueryContext 使用此 DB 进行命名查询。
func (*DB) PrepareNamed ¶
PrepareNamed 返回 NamedStmt。
func (*DB) PrepareNamedContext ¶
PrepareNamedContext 返回 NamedStmt。
func (*DB) PreparexContext ¶
PreparexContext 返回 Stmt 而不是 sql.Stmt。
func (*DB) QueryContext ¶
QueryContext 在 context 下执行查询并记录 SQL 日志(仅 debug 模式)。
func (*DB) QueryRowContext ¶
QueryRowContext 在 context 下执行查询并记录 SQL 日志(仅 debug 模式)。
func (*DB) QueryRowxContext ¶
QueryRowxContext 查询数据库并返回 *Row。
func (*DB) QueryxContext ¶
QueryxContext 查询数据库并返回 *Rows。
func (*DB) SelectContext ¶
SelectContext 使用此 DB 进行查询。
type ExecerContext ¶
type ExecerContext interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
ExecerContext 是带 Context 的执行接口。
type ExtContext ¶
type ExtContext interface {
core.Binder
QueryerContext
ExecerContext
}
ExtContext 是带 Context 的联合接口,可以绑定、查询和执行。
type NamedStmt ¶
NamedStmt 是一个执行命名查询的预编译语句。
func (*NamedStmt) ExecContext ¶
ExecContext 使用传入的结构体执行命名语句。
func (*NamedStmt) GetContext ¶
GetContext 使用此 NamedStmt 获取单条记录。
func (*NamedStmt) MustExecContext ¶
MustExecContext 执行 NamedStmt,出错时 panic。
func (*NamedStmt) QueryContext ¶
QueryContext 使用结构体参数执行命名语句,返回行。
func (*NamedStmt) QueryRowContext ¶
QueryRowContext 对数据库执行命名语句。
func (*NamedStmt) QueryRowxContext ¶
QueryRowxContext 使用此 NamedStmt。
func (*NamedStmt) QueryxContext ¶
QueryxContext 使用此 NamedStmt 查询。
func (*NamedStmt) SelectContext ¶
SelectContext 使用此 NamedStmt 查询。
type PreparerContext ¶
type PreparerContext interface {
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}
PreparerContext 是带 Context 的预编译接口。
type QStmt ¶
type QStmt struct{ *Stmt }
QStmt 是一个封装,通过实现 Queryer 和 Execer 接口使 Stmt 可复用。
func (*QStmt) ExecContext ¶
func (*QStmt) QueryContext ¶
func (*QStmt) QueryRowxContext ¶
type Queryable ¶
type Queryable interface {
core.Ext
ExecerContext
PreparerContext
QueryerContext
core.Preparer
GetContext(context.Context, any, string, ...any) error
SelectContext(context.Context, any, string, ...any) error
Get(any, string, ...any) error
MustExecContext(context.Context, string, ...any) sql.Result
PreparexContext(context.Context, string) (*Stmt, error)
QueryRowContext(context.Context, string, ...any) *sql.Row
Select(any, string, ...any) error
QueryRow(string, ...any) *sql.Row
PrepareNamedContext(context.Context, string) (*NamedStmt, error)
PrepareNamed(string) (*NamedStmt, error)
Preparex(string) (*Stmt, error)
NamedExec(string, any) (sql.Result, error)
NamedExecContext(context.Context, string, any) (sql.Result, error)
MustExec(string, ...any) sql.Result
NamedQuery(string, any) (*core.Rows, error)
NamedQueryContext(context.Context, string, any) (*core.Rows, error)
}
Queryable 是统一 DB 与 Tx 的联合接口,二者均可作为查询执行器互换使用。
type QueryerContext ¶
type QueryerContext interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryxContext(ctx context.Context, query string, args ...any) (*core.Rows, error)
QueryRowxContext(ctx context.Context, query string, args ...any) *core.Row
}
QueryerContext 是带 Context 的查询接口。
type Stmt ¶
Stmt 是 sql.Stmt 的 dbx 封装,提供额外的功能。
func PreparexContext ¶
PreparexContext 预编译一条语句。
func (*Stmt) GetContext ¶
GetContext 使用预编译语句获取单条记录。
func (*Stmt) MustExecContext ¶
MustExecContext 使用此语句执行,出错时 panic。
func (*Stmt) QueryRowxContext ¶
QueryRowxContext 使用此语句查询单行。
func (*Stmt) QueryxContext ¶
QueryxContext 使用此语句查询。
func (*Stmt) SelectContext ¶
SelectContext 使用预编译语句进行查询。
type Tx ¶
Tx 是 sql.Tx 的 dbx 封装,提供额外的功能。
func (*Tx) ExecContext ¶
ExecContext 在事务和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Tx) GetContext ¶
GetContext 在事务和 context 中获取单条记录。
func (*Tx) MustExecContext ¶
MustExecContext 在事务中运行 MustExecContext。
func (*Tx) NamedExecContext ¶
NamedExecContext 在事务和 context 中执行命名查询。
func (*Tx) NamedQuery ¶
NamedQuery 在事务中执行命名查询。
func (*Tx) NamedQueryContext ¶
NamedQueryContext 在事务和 context 中执行命名查询。
func (*Tx) NamedStmtContext ¶
NamedStmtContext 返回在事务中运行的命名预编译语句版本。
func (*Tx) PrepareNamed ¶
PrepareNamed 返回 NamedStmt。
func (*Tx) PrepareNamedContext ¶
PrepareNamedContext 返回 NamedStmt。
func (*Tx) PreparexContext ¶
PreparexContext 返回 Stmt 而不是 sql.Stmt。
func (*Tx) QueryContext ¶
QueryContext 在事务和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Tx) QueryRowContext ¶
QueryRowContext 在事务和 context 中执行查询并记录 SQL 日志(仅 debug 模式)。
func (*Tx) QueryRowxContext ¶
QueryRowxContext 在事务和 context 中查询单行。
func (*Tx) QueryxContext ¶
QueryxContext 在事务和 context 中查询。
func (*Tx) SelectContext ¶
SelectContext 在事务和 context 中查询。
func (*Tx) StmtxContext ¶
StmtxContext 返回在事务中运行的预编译语句版本。