1 Star 1 Fork 1

unsafe-rust / sqlx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
9_sql_context.go 14.24 KB
一键复制 编辑 原始数据 按行查看 历史
unsafe-rust 提交于 2021-03-20 13:23 . create
// +build go1.8
package sqlx
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
)
// LoadFileContext exec's every statement in a file (as a single call to Exec).
// LoadFileContext may return a nil *sql.Result if errors are encountered
// locating or reading the file at path. LoadFile reads the entire file into
// memory, so it is not suitable for loading large data dumps, but can be useful
// for initializing schemas or loading indexes.
//
// FIXME: this does not really work with multi-statement files for mattn/go-sqlite3
// or the go-mysql-driver/mysql drivers; pq seems to be an exception here. Detecting
// this by requiring something with DriverName() and then attempting to split the
// queries will be difficult to get right, and its current driver-specific behavior
// is deemed at least not complex in its incorrectness.
func LoadFileContext(ctx context.Context, e IExecuteContext, path string) (*sql.Result, error) {
realpath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
contents, err := ioutil.ReadFile(realpath)
if err != nil {
return nil, err
}
res, err := e.ExecContext(ctx, string(contents))
return &res, err
}
// ConnectCtx (原ConnectContext) 传入 context.Context,连接到数据库并通过ping进行验证。
func ConnectCtx(ctx context.Context, driverName, dataSourceName string) (*DB, error) {
db, err := Open(driverName, dataSourceName)
if err != nil {
return db, err
}
err = db.PingContext(ctx)
return db, err
}
// TakeCtx (原GetContext) 使用提供的查询器执行QueryRow,然后将结果行扫描到<pointer>。
// 1、如果<pointer>是可扫描的,则结果必须只有一列,否则,将使用StructScan。
// 2、如果结果集为空 将像row.Scan一样返回sql.ErrNoRows。
// 3、<querySql>是查询类的SQL语句,<args>是该SQL语句需要的参数。
func TakeCtx(ctx context.Context, q IQueryContext, pointer interface{}, query string, args ...interface{}) error {
r := q.QueryRowXCtx(ctx, query, args...)
return r.scanAny(pointer, false)
}
// SelectCtx (原SelectContext) 使用提供的查询器查询多条数据,并将结果扫描进<pointer>中。参数<pointer>必须是切片(slice)类型。
// 1、如果<pointer>中的元素是可扫描的,则结果集必须只有一列。否则,将使用StructScan。
// 2、<querySql>是查询类的SQL语句,<args>是该SQL语句需要的参数。
// 3、默认sql.Rows自动关闭。任何占位符参数都将替换为提供的args。
func SelectCtx(ctx context.Context, q IQueryContext, pointer interface{}, querySql string, args ...interface{}) error {
rows, err := q.QueryXCtx(ctx, querySql, args...)
if err != nil {
return err
}
// if something happens here, we want to make sure the rows are Closed
defer rows.Close()
return scanAll(rows, pointer, false)
}
// ExecPanicCtx (原MustExecContext)execs the query using e and panics if there was an error.
// Any placeholder parameters are replaced with supplied args.
func ExecPanicCtx(ctx context.Context, e IExecuteContext, query string, args ...interface{}) sql.Result {
res, err := e.ExecContext(ctx, query, args...)
if err != nil {
panic(err)
}
return res
}
// PrepareXCtx (原PrepareXContext)prepares a statement.
// The provided context is used for the preparation of the statement, not for
// the execution of the statement.
func PrepareXCtx(ctx context.Context, p IPrepareContext, query string) (*Stmt, error) {
s, err := p.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &Stmt{Stmt: s, unsafe: isUnsafe(p), Mapper: mapperFor(p)}, err
}
// SelectCtx (原SelectContext)using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
func (s *Stmt) SelectCtx(ctx context.Context, dest interface{}, args ...interface{}) error {
return SelectCtx(ctx, &qStmt{s}, dest, "", args...)
}
// TakeCtx (原GetContext)using the prepared statement.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (s *Stmt) TakeCtx(ctx context.Context, dest interface{}, args ...interface{}) error {
return TakeCtx(ctx, &qStmt{s}, dest, "", args...)
}
// ExecPanicCtx (原MustExecContext)(panic) using this statement. Note that the query portion of
// the error output will be blank, as Stmt does not expose its query.
// Any placeholder parameters are replaced with supplied args.
func (s *Stmt) ExecPanicCtx(ctx context.Context, args ...interface{}) sql.Result {
return ExecPanicCtx(ctx, &qStmt{s}, "", args...)
}
// QueryRowXCtx(原QueryRowXContext) using this statement.
// Any placeholder parameters are replaced with supplied args.
func (s *Stmt) QueryRowXCtx(ctx context.Context, args ...interface{}) *Row {
qs := &qStmt{s}
return qs.QueryRowXCtx(ctx, "", args...)
}
func (s *Stmt) QueryXCtx(ctx context.Context, args ...interface{}) (*Rows, error) {
qs := &qStmt{s}
return qs.QueryXCtx(ctx, "", args...)
}
func (q *qStmt) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return q.Stmt.QueryContext(ctx, args...)
}
func (q *qStmt) QueryXCtx(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
r, err := q.Stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}, err
}
func (q *qStmt) QueryRowXCtx(ctx context.Context, query string, args ...interface{}) *Row {
rows, err := q.Stmt.QueryContext(ctx, args...)
return &Row{rows: rows, err: err, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}
}
func (q *qStmt) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return q.Stmt.ExecContext(ctx, args...)
}
// BeginTxX begins a transaction and returns an *sqlx.Tx instead of an
// *sql.Tx.
//
// The provided context is used until the transaction is committed or rolled
// back. If the context is canceled, the sql package will roll back the
// transaction. Tx.Commit will return an error if the context provided to
// BeginxContext is canceled.
func (c *Conn) BeginTxX(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
tx, err := c.Conn.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &Tx{Tx: tx, driverName: c.driverName, unsafe: c.unsafe, Mapper: c.Mapper}, err
}
// Rebind a query within a Conn's bindvar type.
func (c *Conn) Rebind(query string) string {
return Rebind(BindType(c.driverName), query)
}
// QueryxContext queries the database and returns an *sqlx.Rows.
// Any placeholder parameters are replaced with supplied args.
func (c *Conn) QueryXCtx(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
r, err := c.Conn.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: c.unsafe, Mapper: c.Mapper}, err
}
// QueryRowxContext queries the database and returns an *sqlx.Row.
// Any placeholder parameters are replaced with supplied args.
func (c *Conn) QueryRowXCtx(ctx context.Context, query string, args ...interface{}) *Row {
rows, err := c.Conn.QueryContext(ctx, query, args...)
return &Row{rows: rows, err: err, unsafe: c.unsafe, Mapper: c.Mapper}
}
// SelectCtx (原SelectContext) using this Conn.
// Any placeholder parameters are replaced with supplied args.
func (c *Conn) SelectCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return SelectCtx(ctx, c, dest, query, args...)
}
// TakeCtx (原GetContext) using this Conn.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (c *Conn) TakeCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return TakeCtx(ctx, c, dest, query, args...)
}
// PrepareXCtx (原PrepareXContext) returns an sqlx.Stmt instead of a sql.Stmt.
// The provided context is used for the preparation of the statement, not for
// the execution of the statement.
func (c *Conn) PrepareXCtx(ctx context.Context, query string) (*Stmt, error) {
return PrepareXCtx(ctx, c, query)
}
// PrepareNamedContext returns an sqlx.NamedStmt
func (db *DB) PrepareNCtx(ctx context.Context, query string) (*StmtN, error) {
return prepareNCtx(ctx, db, query)
}
func (db *DB) PrepareXCtx(ctx context.Context, query string) (*Stmt, error) {
return PrepareXCtx(ctx, db, query)
}
// QueryxContext queries the database and returns an *sqlx.Rows.
// Any placeholder parameters are replaced with supplied args.
func (db *DB) QueryXCtx(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
r, err := db.DB.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: db.unsafe, Mapper: db.Mapper}, err
}
// QueryRowxContext queries the database and returns an *sqlx.Row.
// Any placeholder parameters are replaced with supplied args.
func (db *DB) QueryRowXCtx(ctx context.Context, query string, args ...interface{}) *Row {
rows, err := db.DB.QueryContext(ctx, query, args...)
return &Row{rows: rows, err: err, unsafe: db.unsafe, Mapper: db.Mapper}
}
// NamedQueryContext using this DB.
// Any named placeholder parameters are replaced with fields from arg.
func (db *DB) QueryNCtx(ctx context.Context, query string, arg interface{}) (*Rows, error) {
return QueryNContext(ctx, db, query, arg)
}
// NamedExecContext using this DB.
// Any named placeholder parameters are replaced with fields from arg.
func (db *DB) ExecNCtx(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return ExecNContext(ctx, db, query, arg)
}
// SelectContext using this DB.
// Any placeholder parameters are replaced with supplied args.
func (db *DB) SelectCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return SelectCtx(ctx, db, dest, query, args...)
}
// GetContext using this DB.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (db *DB) TakeCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return TakeCtx(ctx, db, dest, query, args...)
}
// MustExecContext (panic) runs MustExec using this database.
// Any placeholder parameters are replaced with supplied args.
func (db *DB) ExecPanicCtx(ctx context.Context, query string, args ...interface{}) sql.Result {
return ExecPanicCtx(ctx, db, query, args...)
}
// BeginTxx begins a transaction and returns an *sqlx.Tx instead of an
// *sql.Tx.
//
// The provided context is used until the transaction is committed or rolled
// back. If the context is canceled, the sql package will roll back the
// transaction. Tx.Commit will return an error if the context provided to
// BeginxContext is canceled.
func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
tx, err := db.DB.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &Tx{Tx: tx, driverName: db.driverName, unsafe: db.unsafe, Mapper: db.Mapper}, err
}
func (db *DB) BeginPanicTx(ctx context.Context, opts *sql.TxOptions) *Tx {
tx, err := db.BeginTxx(ctx, opts)
if err != nil {
panic(err)
}
return tx
}
// ConnX returns an *sqlx.Conn instead of an *sql.Conn.
func (db *DB) ConnX(ctx context.Context) (*Conn, error) {
conn, err := db.DB.Conn(ctx)
if err != nil {
return nil, err
}
return &Conn{Conn: conn, driverName: db.driverName, unsafe: db.unsafe, Mapper: db.Mapper}, nil
}
// StmtxContext returns a version of the prepared statement which runs within a
// transaction. Provided stmt can be either *sql.Stmt or *sqlx.Stmt.
func (tx *Tx) StmtXCtx(ctx context.Context, stmt interface{}) *Stmt {
var s *sql.Stmt
switch v := stmt.(type) {
case Stmt:
s = v.Stmt
case *Stmt:
s = v.Stmt
case *sql.Stmt:
s = v
default:
panic(fmt.Sprintf("non-statement type %v passed to Stmtx", reflect.ValueOf(stmt).Type()))
}
return &Stmt{Stmt: tx.StmtContext(ctx, s), Mapper: tx.Mapper}
}
// NamedStmtContext returns a version of the prepared statement which runs
// within a transaction.
func (tx *Tx) StmtNCtx(ctx context.Context, stmt *StmtN) *StmtN {
return &StmtN{
QueryString: stmt.QueryString,
Params: stmt.Params,
Stmt: tx.StmtXCtx(ctx, stmt.Stmt),
}
}
// PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
//
// The provided context is used for the preparation of the statement, not for
// the execution of the statement.
func (tx *Tx) PrepareXCtx(ctx context.Context, query string) (*Stmt, error) {
return PrepareXCtx(ctx, tx, query)
}
// PrepareNamedContext returns an sqlx.NamedStmt
func (tx *Tx) PrepareNCtx(ctx context.Context, query string) (*StmtN, error) {
return prepareNCtx(ctx, tx, query)
}
// NamedExecContext using this Tx.
// Any named placeholder parameters are replaced with fields from arg.
func (tx *Tx) ExecNCtx(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return ExecNContext(ctx, tx, query, arg)
}
// MustExecContext runs MustExecContext within a transaction.
// Any placeholder parameters are replaced with supplied args.
func (tx *Tx) ExecPanicCtx(ctx context.Context, query string, args ...interface{}) sql.Result {
return ExecPanicCtx(ctx, tx, query, args...)
}
// QueryxContext within a transaction and context.
// Any placeholder parameters are replaced with supplied args.
func (tx *Tx) QueryXCtx(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
r, err := tx.Tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: r, unsafe: tx.unsafe, Mapper: tx.Mapper}, err
}
// QueryRowxContext within a transaction and context.
// Any placeholder parameters are replaced with supplied args.
func (tx *Tx) QueryRowXCtx(ctx context.Context, query string, args ...interface{}) *Row {
rows, err := tx.Tx.QueryContext(ctx, query, args...)
return &Row{rows: rows, err: err, unsafe: tx.unsafe, Mapper: tx.Mapper}
}
// SelectContext within a transaction and context.
// Any placeholder parameters are replaced with supplied args.
func (tx *Tx) SelectCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return SelectCtx(ctx, tx, dest, query, args...)
}
// GetContext within a transaction and context.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (tx *Tx) TakeCtx(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return TakeCtx(ctx, tx, dest, query, args...)
}
Go
1
https://gitee.com/unsafe-rust/sqlx.git
git@gitee.com:unsafe-rust/sqlx.git
unsafe-rust
sqlx
sqlx
v1.3.3

搜索帮助