Ai
1 Star 0 Fork 0

Erdian718/sqlx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db.go 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
Erdian718 提交于 2025-12-05 14:06 +08:00 . Change query single APIs
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...)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/erdian718/sqlx.git
git@gitee.com:erdian718/sqlx.git
erdian718
sqlx
sqlx
main

搜索帮助