代码拉取完成,页面将自动刷新
package sqlx
import (
"context"
"database/sql"
)
// DB is a database handle representing a pool of zero or more underlying connections.
// It's safe for concurrent use by multiple goroutines.
type DB sql.DB
// NewDB creates a DB from sql.DB.
func NewDB(raw *sql.DB) *DB {
return (*DB)(raw)
}
// Open opens a database specified by its database driver name and a driver-specific data source name.
func Open(driverName string, dataSourceName string) (*DB, error) {
raw, err := sql.Open(driverName, dataSourceName)
return NewDB(raw), err
}
// Raw returns the sql.DB.
func (db *DB) Raw() *sql.DB {
return (*sql.DB)(db)
}
// Close closes the database and prevents new queries from starting.
func (db *DB) Close() error {
return db.Raw().Close()
}
// Begin starts a transaction. The default isolation level is dependent on the driver.
func (db *DB) Begin(options *TxOptions) (*Tx, error) {
return db.BeginContext(context.Background(), options)
}
// Begin starts a transaction. The default isolation level is dependent on the driver.
func (db *DB) BeginContext(ctx context.Context, options *TxOptions) (*Tx, error) {
if options == nil {
options = NewTxOptions()
}
raw, err := db.Raw().BeginTx(ctx, &sql.TxOptions{
Isolation: options.isolation,
ReadOnly: options.readOnly,
})
return NewTx(raw), err
}
// Prepare creates a prepared statement for later queries or executions.
func (db *DB) Prepare(cmd string) (*Stmt, error) {
return db.PrepareContext(context.Background(), cmd)
}
// PrepareContext creates a prepared statement for later queries or executions.
func (db *DB) PrepareContext(ctx context.Context, cmd string) (*Stmt, error) {
raw, err := db.Raw().PrepareContext(ctx, cmd)
return NewStmt(raw), err
}
// Exec executes a query without returning any rows.
func (db *DB) Exec(cmd string, args ...any) (sql.Result, error) {
return db.ExecContext(context.Background(), cmd, args...)
}
// ExecContext executes a query without returning any rows.
func (db *DB) ExecContext(ctx context.Context, cmd string, args ...any) (sql.Result, error) {
return db.Raw().ExecContext(ctx, cmd, args...)
}
// QuerySingle executes a query and fills the non-slice result.
func (db *DB) QuerySingle(cmd string, result any, args ...any) error {
return db.QuerySingleContext(context.Background(), cmd, result, args...)
}
// QuerySingleContext executes a query and fills the non-slice result.
func (db *DB) QuerySingleContext(ctx context.Context, cmd string, result any, args ...any) error {
rows, err := db.QueryRowsContext(ctx, cmd, args...)
if err != nil {
return err
}
defer rows.Close()
return fillSingleResult(rows, result)
}
// GetRow executes a query that returns a single row.
func (db *DB) GetRow(cmd string, args ...any) *sql.Row {
return db.GetRowContext(context.Background(), cmd, args...)
}
// GetRowContext executes a query that returns a single row.
func (db *DB) GetRowContext(ctx context.Context, cmd string, args ...any) *sql.Row {
return db.Raw().QueryRowContext(ctx, cmd, args...)
}
// Query executes a query and fills the slice result.
func (db *DB) Query(cmd string, result any, args ...any) error {
return db.QueryContext(context.Background(), cmd, result, args...)
}
// QueryContext executes a query and fills the slice result.
func (db *DB) QueryContext(ctx context.Context, cmd string, result any, args ...any) error {
rows, err := db.QueryRowsContext(ctx, cmd, args...)
if err != nil {
return err
}
defer rows.Close()
return fillSliceResult(rows, result)
}
// QueryRows executes a query that returns rows.
func (db *DB) QueryRows(cmd string, args ...any) (*sql.Rows, error) {
return db.QueryRowsContext(context.Background(), cmd, args...)
}
// QueryRowsContext executes a query that returns rows.
func (db *DB) QueryRowsContext(ctx context.Context, cmd string, args ...any) (*sql.Rows, error) {
return db.Raw().QueryContext(ctx, cmd, args...)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。