|
@@ -5,8 +5,6 @@ import (
|
|
|
"database/sql"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
- "sync"
|
|
|
- "time"
|
|
|
|
|
|
// PostgreSQL
|
|
|
_ "github.com/lib/pq"
|
|
@@ -16,17 +14,6 @@ import (
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
)
|
|
|
|
|
|
-var (
|
|
|
- config Config
|
|
|
- db *sqlx.DB
|
|
|
- //err error
|
|
|
- once sync.Once
|
|
|
-
|
|
|
- defaultDB *DB
|
|
|
-
|
|
|
- errNoneConnect = errors.New(`数据库连接错误`)
|
|
|
-)
|
|
|
-
|
|
|
// DB define
|
|
|
type DB struct {
|
|
|
Driver string
|
|
@@ -39,18 +26,6 @@ type Tx struct {
|
|
|
tx *sqlx.Tx
|
|
|
}
|
|
|
|
|
|
-// SetConfig set
|
|
|
-func SetConfig(cfg Config) {
|
|
|
- config.Driver = cfg.Driver
|
|
|
- config.DNS = cfg.DNS
|
|
|
- config.MaxOpenConns = cfg.MaxOpenConns
|
|
|
- config.MaxIdle = cfg.MaxIdle
|
|
|
- config.MaxIdleTime = cfg.MaxIdleTime * time.Second
|
|
|
- config.MaxLifeTime = cfg.MaxLifeTime * time.Second
|
|
|
-
|
|
|
- defaultDB = &DB{Driver: config.Driver}
|
|
|
-}
|
|
|
-
|
|
|
// New new DB object
|
|
|
func New() *DB {
|
|
|
return &DB{Driver: config.Driver}
|
|
@@ -107,7 +82,7 @@ func connect() (dbx *sqlx.DB, err error) {
|
|
|
// */
|
|
|
})
|
|
|
if db == nil {
|
|
|
- err = errNoneConnect
|
|
|
+ err = ErrNoneConnect
|
|
|
return
|
|
|
}
|
|
|
dbx = db
|
|
@@ -127,7 +102,7 @@ func connectContext(ctx context.Context) (dbx *sqlx.DB, err error) {
|
|
|
db.DB.SetConnMaxLifetime(config.MaxLifeTime)
|
|
|
})
|
|
|
if db == nil {
|
|
|
- err = errNoneConnect
|
|
|
+ err = ErrNoneConnect
|
|
|
return
|
|
|
}
|
|
|
dbx = db
|
|
@@ -243,21 +218,21 @@ func (d *DB) Rollback() (err error) {
|
|
|
}
|
|
|
|
|
|
// TransExec trans execute with named args
|
|
|
-func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) TransExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.tx.NamedExec(query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// TransExec trans execute with named args
|
|
|
-func (d *DB) TransExecContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) TransExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.tx.NamedExecContext(ctx, query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -280,7 +255,7 @@ func (d *DB) TransUpdate(query string, args interface{}) (reply Reply) {
|
|
|
|
|
|
// TransRow trans get row
|
|
|
func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err error) {
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.tx.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -324,7 +299,7 @@ func (d *DB) Query(dest interface{}, query string, args interface{}) (err error)
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -343,7 +318,7 @@ func (d *DB) QueryContext(ctx context.Context, dest interface{}, query string, a
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -362,7 +337,7 @@ func (d *DB) Rows(dest interface{}, query string, args interface{}) (err error)
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -381,7 +356,7 @@ func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, ar
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -448,7 +423,7 @@ func (d *DB) Row(dest interface{}, query string, args interface{}) (err error) {
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -467,7 +442,7 @@ func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, arg
|
|
|
}
|
|
|
defer d.Close()
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = d.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -478,6 +453,21 @@ func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, arg
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+// 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 (d *DB) In(query string, args ...interface{}) (q string, params []interface{}, err error) {
|
|
|
+ err = d.Connect()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer d.Close()
|
|
|
+ var s string
|
|
|
+ s, params, err = sqlx.In(query, args)
|
|
|
+ q = d.conn.Rebind(s)
|
|
|
+ return
|
|
|
+}
|
|
|
+//*/
|
|
|
+
|
|
|
// InsertReply insert and return DbReply
|
|
|
func (d *DB) InsertReply(query string, args interface{}) (reply Reply) {
|
|
|
var (
|
|
@@ -524,7 +514,7 @@ func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
|
|
|
}
|
|
|
|
|
|
// Insert insert into with named args
|
|
|
-func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) Insert(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.Connect()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -533,14 +523,14 @@ func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExec(query, args); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// InsertContext insert into with named args
|
|
|
-func (d *DB) InsertContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) InsertContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.ConnectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -549,14 +539,14 @@ func (d *DB) InsertContext(ctx context.Context, query string, args interface{})
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Update update/delete with named args
|
|
|
-func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err error) {
|
|
|
+func (d *DB) Update(query string, args interface{}) (rowsAffected int64, err error) {
|
|
|
err = d.Connect()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -565,13 +555,13 @@ func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err err
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExec(query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Update update/delete with named args
|
|
|
-func (d *DB) UpdateContext(ctx context.Context, query string, args interface{}) (RowsAffected int64, err error) {
|
|
|
+func (d *DB) UpdateContext(ctx context.Context, query string, args interface{}) (rowsAffected int64, err error) {
|
|
|
err = d.ConnectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -580,13 +570,13 @@ func (d *DB) UpdateContext(ctx context.Context, query string, args interface{})
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Exec exec
|
|
|
-func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.Connect()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -595,14 +585,14 @@ func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.Exec(query, args...); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// ExecContext exec
|
|
|
-func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.ConnectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -611,14 +601,14 @@ func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{})
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.ExecContext(ctx, query, args...); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Exec exec, with named args
|
|
|
-func (d *DB) NamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.Connect()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -627,14 +617,14 @@ func (d *DB) NamedExec(query string, args interface{}) (LastInsertId, RowsAffect
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExec(query, args); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// NamedExecContext exec, with named args
|
|
|
-func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
err = d.ConnectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -643,8 +633,8 @@ func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{
|
|
|
|
|
|
var rs sql.Result
|
|
|
if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -670,48 +660,48 @@ func (t *Tx) Rollback() error {
|
|
|
}
|
|
|
|
|
|
// NamedExec executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
|
|
|
-func (t *Tx) NamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (t *Tx) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = t.tx.NamedExec(query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// NamedExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
|
|
|
-func (t *Tx) NamedExecContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (t *Tx) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = t.tx.NamedExecContext(ctx, query, args); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
|
|
|
-func (t *Tx) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (t *Tx) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = t.tx.Exec(query, args...); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
|
|
|
-func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
var rs sql.Result
|
|
|
if rs, err = t.tx.ExecContext(ctx, query, args...); err == nil {
|
|
|
- RowsAffected, _ = rs.RowsAffected()
|
|
|
- LastInsertId, _ = rs.LastInsertId()
|
|
|
+ rowsAffected, _ = rs.RowsAffected()
|
|
|
+ lastInsertId, _ = rs.LastInsertId()
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Query executes a query that returns rows, typically a SELECT. with named args
|
|
|
func (t *Tx) Query(dest interface{}, query string, args interface{}) (err error) {
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = t.tx.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -724,7 +714,7 @@ func (t *Tx) Query(dest interface{}, query string, args interface{}) (err error)
|
|
|
|
|
|
// QueryContext executes a query that returns rows, typically a SELECT. with named args
|
|
|
func (t *Tx) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = t.tx.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -737,7 +727,7 @@ func (t *Tx) QueryContext(ctx context.Context, dest interface{}, query string, a
|
|
|
|
|
|
// QueryRow executes a query that returns rows, typically a SELECT. with named args
|
|
|
func (t *Tx) QueryRow(dest interface{}, query string, args interface{}) (err error) {
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = t.tx.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -750,7 +740,7 @@ func (t *Tx) QueryRow(dest interface{}, query string, args interface{}) (err err
|
|
|
|
|
|
// QueryRowContext get row with named args
|
|
|
func (t *Tx) QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = t.tx.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -761,6 +751,17 @@ func (t *Tx) QueryRowContext(ctx context.Context, dest interface{}, query string
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// Stats returns database statistics.
|
|
|
+func Stats() (s sql.DBStats, err error) {
|
|
|
+ defaultDB.conn, err = connect()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ s = defaultDB.Stats()
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// Ping ping connect
|
|
|
func Ping() (err error) {
|
|
|
defaultDB.conn, err = connect()
|
|
@@ -801,7 +802,7 @@ func Query(dest interface{}, query string, args interface{}) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -819,7 +820,7 @@ func QueryContext(ctx context.Context, dest interface{}, query string, args inte
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -837,7 +838,7 @@ func Rows(dest interface{}, query string, args interface{}) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -855,7 +856,7 @@ func RowsContext(ctx context.Context, dest interface{}, query string, args inter
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -884,7 +885,7 @@ func QueryRow(dest interface{}, query string, args interface{}) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -902,7 +903,7 @@ func QueryRowContext(ctx context.Context, dest interface{}, query string, args i
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -920,7 +921,7 @@ func Row(dest interface{}, query string, args interface{}) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamed(query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -938,7 +939,7 @@ func RowContext(ctx context.Context, dest interface{}, query string, args interf
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- nstmt := new(sqlx.NamedStmt)
|
|
|
+ nstmt := &sqlx.NamedStmt{}
|
|
|
nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -949,46 +950,59 @@ func RowContext(ctx context.Context, dest interface{}, query string, args interf
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+// 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 In(query string, args ...interface{}) (q string, params []interface{}, err error) {
|
|
|
+ defaultDB.conn, err = connect()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ q, params, err = defaultDB.In(query, args...)
|
|
|
+ return
|
|
|
+}
|
|
|
+// */
|
|
|
+
|
|
|
// Exec exec
|
|
|
-func Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
defaultDB.conn, err = connect()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- LastInsertId, RowsAffected, err = defaultDB.Exec(query, args...)
|
|
|
+ lastInsertId, rowsAffected, err = defaultDB.Exec(query, args...)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// Exec exec
|
|
|
-func ExecContext(ctx context.Context, query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
defaultDB.conn, err = connectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- LastInsertId, RowsAffected, err = defaultDB.ExecContext(ctx, query, args...)
|
|
|
+ lastInsertId, rowsAffected, err = defaultDB.ExecContext(ctx, query, args...)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// NamedExec exec with named args
|
|
|
-func NamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
defaultDB.conn, err = connect()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- LastInsertId, RowsAffected, err = defaultDB.NamedExec(query, args)
|
|
|
+ lastInsertId, rowsAffected, err = defaultDB.NamedExec(query, args)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// NamedExecContext exec with named args
|
|
|
-func NamedExecContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
|
|
|
+func NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
|
|
|
defaultDB.conn, err = connectContext(ctx)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- LastInsertId, RowsAffected, err = defaultDB.NamedExecContext(ctx, query, args)
|
|
|
+ lastInsertId, rowsAffected, err = defaultDB.NamedExecContext(ctx, query, args)
|
|
|
return
|
|
|
}
|