1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- package db
- import (
- "context"
- "database/sql"
- "errors"
- "fmt"
- // PostgreSQL
- _ "github.com/lib/pq"
- // MySQL
- _ "github.com/go-sql-driver/mysql"
- "github.com/jmoiron/sqlx"
- _ "github.com/mattn/go-sqlite3"
- )
- // DB define
- type DB struct {
- Driver string
- conn *sqlx.DB
- tx *sqlx.Tx
- }
- // Tx is an in-progress database transaction.
- type Tx struct {
- tx *sqlx.Tx
- }
- // New new DB object
- func New() *DB {
- return &DB{Driver: config.Driver}
- }
- // Release free db connect
- func Release() {
- if db != nil {
- db.Close()
- }
- }
- // NewConfig new DB dynamic object
- func NewConfig(config Config) (dbx *DB, err error) {
- dbx = &DB{}
- dbx.Driver = config.Driver
- dbx.conn, err = sqlx.Connect(config.Driver, config.DNS)
- if err != nil {
- return
- }
- dbx.conn.SetMaxOpenConns(config.MaxOpenConns)
- dbx.conn.SetMaxIdleConns(config.MaxIdle)
- dbx.conn.SetConnMaxIdleTime(config.MaxIdleTime)
- dbx.conn.SetConnMaxLifetime(config.MaxLifeTime)
- //err = dbx.conn.Ping()
- return
- }
- // ReleaseConfig free db connect
- func ReleaseConfig(dbx *DB) {
- if dbx.conn != nil {
- dbx.conn.Close()
- }
- }
- func connect() (dbx *sqlx.DB, err error) {
- once.Do(func() {
- db, err = sqlx.Open(config.Driver, config.DNS) // sqlx.Connect(config.Driver, config.DNS)
- if err != nil {
- return
- }
- db.DB.SetMaxOpenConns(config.MaxOpenConns)
- db.DB.SetMaxIdleConns(config.MaxIdle)
- db.DB.SetConnMaxIdleTime(config.MaxIdleTime)
- db.DB.SetConnMaxLifetime(config.MaxLifeTime)
- /*
- err = db.Ping()
- if err != nil {
- return
- }
- // */
- })
- if db == nil {
- err = ErrNoneConnect
- return
- }
- dbx = db
- return
- }
- func connectContext(ctx context.Context) (dbx *sqlx.DB, err error) {
- once.Do(func() {
- db, err = sqlx.ConnectContext(ctx, config.Driver, config.DNS)
- if err != nil {
- return
- }
- db.DB.SetMaxOpenConns(config.MaxOpenConns)
- db.DB.SetMaxIdleConns(config.MaxIdle)
- db.DB.SetConnMaxIdleTime(config.MaxIdleTime)
- db.DB.SetConnMaxLifetime(config.MaxLifeTime)
- })
- if db == nil {
- err = ErrNoneConnect
- return
- }
- dbx = db
- return
- }
- // Stats Stats returns database statistics.
- func (d *DB) Stats() (s sql.DBStats) {
- s = d.conn.DB.Stats()
- return
- }
- // Connect connect to database
- func (d *DB) Connect() (err error) {
- if d.conn != nil {
- return
- }
- d.conn, err = connect()
- return
- }
- // ConnectContext connect to database
- func (d *DB) ConnectContext(ctx context.Context) (err error) {
- if d.conn != nil {
- return
- }
- d.conn, err = connectContext(ctx)
- return
- }
- // Close close database connect
- func (d *DB) Close() {
- // use pool
- //d.conn.Close()
- }
- // Ping Ping connect
- func (d *DB) Ping() (err error) {
- d.conn, err = connect()
- if err != nil {
- return
- }
- return d.conn.Ping()
- }
- // PingContext Ping connect
- func (d *DB) PingContext(ctx context.Context) (err error) {
- d.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- return d.conn.PingContext(ctx)
- }
- // BeginTrans begin trans
- func (d *DB) BeginTrans() (err error) {
- d.conn, err = connect()
- if err != nil {
- return
- }
- d.tx, err = d.conn.Beginx()
- return
- }
- // Begin starts a transaction. The default isolation level is dependent on the driver.
- func (d *DB) Begin() (tx *Tx, err error) {
- d.conn, err = connect()
- if err != nil {
- return
- }
- tx = &Tx{}
- tx.tx, err = d.conn.Beginx()
- return
- }
- // BeginTransx begin trans
- func (d *DB) BeginTransx(ctx context.Context, opts *sql.TxOptions) (err error) {
- d.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- d.tx, err = d.conn.BeginTxx(ctx, opts)
- return
- }
- // BeginTx starts a transaction.
- func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *Tx, err error) {
- d.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- tx = &Tx{}
- tx.tx, err = d.conn.BeginTxx(ctx, opts)
- return
- }
- // Commit commit
- func (d *DB) Commit() (err error) {
- return d.tx.Commit()
- }
- // Rollback rollback
- func (d *DB) Rollback() (err error) {
- return d.tx.Rollback()
- }
- // TransExec trans execute with named args
- 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()
- }
- return
- }
- // TransExec trans execute with named args
- 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()
- }
- return
- }
- // TransUpdate trans update
- func (d *DB) TransUpdate(query string, args interface{}) (reply Reply) {
- var (
- err error
- rs sql.Result
- )
- if rs, err = d.tx.NamedExec(query, args); err == nil {
- a, _ := rs.RowsAffected()
- reply = ReplyOk(a, 0)
- } else {
- reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
- }
- return
- }
- // TransRow trans get row
- func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err error) {
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.tx.PrepareNamed(query)
- if err != nil {
- return err
- }
- defer nstmt.Close()
- err = nstmt.Get(dest, args)
- //err = d.tx.Get(dest, query, args)
- return err
- }
- // Preparex a statement within a transaction.
- func (d *DB) Preparex(query string) (stmt *Stmt, err error) {
- stmt, err = d.conn.Preparex(query)
- return
- }
- // PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
- func (d *DB) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error) {
- stmt, err = d.conn.PreparexContext(ctx, query)
- return
- }
- // PrepareNamed returns an sqlx.NamedStmt
- func (d *DB) PrepareNamed(query string) (stmt *NamedStmt, err error) {
- stmt, err = d.conn.PrepareNamed(query)
- return
- }
- // PrepareNamedContext returns an sqlx.NamedStmt
- func (d *DB) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error) {
- stmt, err = d.conn.PrepareNamedContext(ctx, query)
- return
- }
- // Select select
- func (d *DB) Select(dest interface{}, query string, args ...interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return err
- }
- defer d.Close()
- err = d.conn.Select(dest, query, args...)
- return
- }
- // SelectContext select
- func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- err = d.conn.SelectContext(ctx, dest, query, args...)
- return
- }
- // Query get rows with named args, Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
- func (d *DB) Query(dest interface{}, query string, args interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return err
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Select(dest, args)
- return
- }
- // QueryContext get rows with named args, QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
- func (d *DB) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.SelectContext(ctx, dest, args)
- return
- }
- // Rows get rows with named args
- func (d *DB) Rows(dest interface{}, query string, args interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Select(dest, args)
- return
- }
- // RowsContext get rows with named args
- func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return err
- }
- defer nstmt.Close()
- err = nstmt.SelectContext(ctx, dest, args)
- return
- }
- // QueryRow get row, QueryRow executes a query that is expected to return at most one row.
- func (d *DB) QueryRow(dest interface{}, query string, args ...interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return err
- }
- defer d.Close()
- err = d.conn.Get(dest, query, args...)
- return
- }
- // QueryRowContext get row, QueryRowContext executes a query that is expected to return at most one row.
- func (d *DB) QueryRowContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- err = d.conn.GetContext(ctx, dest, query, args...)
- return
- }
- // Get get row, QueryRow executes a query that is expected to return at most one row.
- func (d *DB) Get(dest interface{}, query string, args ...interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- err = d.conn.Get(dest, query, args...)
- return
- }
- // GetContext get
- func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- err = d.conn.GetContext(ctx, dest, query, args...)
- return
- }
- // Row get row with named args
- func (d *DB) Row(dest interface{}, query string, args interface{}) (err error) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Get(dest, args)
- return
- }
- // RowContext get row with named args
- func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = d.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.GetContext(ctx, dest, args)
- 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 (
- err error
- rs sql.Result
- )
- err = d.Connect()
- if err != nil {
- reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
- return
- }
- defer d.Close()
- if rs, err = d.conn.NamedExec(query, args); err == nil {
- a, _ := rs.RowsAffected()
- n, _ := rs.LastInsertId()
- reply = ReplyOk(a, n)
- } else {
- reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
- }
- return
- }
- // UpdateReply update/delete and return DbReply
- func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
- var (
- err error
- rs sql.Result
- )
- err = d.Connect()
- if err != nil {
- reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
- return
- }
- defer d.Close()
- if rs, err = d.conn.NamedExec(query, args); err == nil {
- a, _ := rs.RowsAffected()
- reply = ReplyOk(a, 0)
- } else {
- reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
- }
- return
- }
- // Insert insert into with named args
- func (d *DB) Insert(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExec(query, args); err == nil {
- 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) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
- 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) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExec(query, args); err == nil {
- 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) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
- rowsAffected, _ = rs.RowsAffected()
- }
- return
- }
- // Exec exec
- func (d *DB) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.Exec(query, args...); err == nil {
- 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) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.ExecContext(ctx, query, args...); err == nil {
- 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) {
- err = d.Connect()
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExec(query, args); err == nil {
- 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) {
- err = d.ConnectContext(ctx)
- if err != nil {
- return
- }
- defer d.Close()
- var rs sql.Result
- if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
- lastInsertId, _ = rs.LastInsertId()
- rowsAffected, _ = rs.RowsAffected()
- }
- return
- }
- // Limit MySQL limit
- func (d *DB) Limit(page, pagesize int) string {
- // MySQL limit 0, size
- if d.Driver == `mysql` {
- return fmt.Sprintf(" LIMIT %d, %d", (page-1)*pagesize, pagesize)
- }
- // // PostgreSQL limit size offset 0
- return fmt.Sprintf(" LIMIT %d OFFSET %d", pagesize, (page-1)*pagesize)
- }
- // Commit commits the transaction.
- func (t *Tx) Commit() error {
- return t.tx.Commit()
- }
- // Rollback aborts the transaction.
- func (t *Tx) Rollback() error {
- return t.tx.Rollback()
- }
- // 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) {
- var rs sql.Result
- if rs, err = t.tx.NamedExec(query, args); err == nil {
- 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) {
- var rs sql.Result
- if rs, err = t.tx.NamedExecContext(ctx, query, args); err == nil {
- 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) {
- var rs sql.Result
- if rs, err = t.tx.Exec(query, args...); err == nil {
- 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) {
- var rs sql.Result
- if rs, err = t.tx.ExecContext(ctx, query, args...); err == nil {
- rowsAffected, _ = rs.RowsAffected()
- lastInsertId, _ = rs.LastInsertId()
- }
- return
- }
- // Preparex a statement within a transaction.
- func (t *Tx) Preparex(query string) (stmt *Stmt, err error) {
- stmt, err = t.tx.Preparex(query)
- return
- }
- // PreparexContext a statement within a transaction.
- func (t *Tx) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error) {
- stmt, err = t.tx.PreparexContext(ctx, query)
- return
- }
- // PrepareNamed returns an sqlx.NamedStmt
- func (t *Tx) PrepareNamed(query string) (stmt *NamedStmt, err error) {
- stmt, err = t.tx.PrepareNamed(query)
- return
- }
- // PrepareNamedContext returns an sqlx.NamedStmt
- func (t *Tx) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error) {
- stmt, err = t.tx.PrepareNamedContext(ctx, query)
- 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 := &sqlx.NamedStmt{}
- nstmt, err = t.tx.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Select(dest, args)
- return
- }
- // 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 := &sqlx.NamedStmt{}
- nstmt, err = t.tx.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.SelectContext(ctx, dest, args)
- return
- }
- // 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 := &sqlx.NamedStmt{}
- nstmt, err = t.tx.PrepareNamed(query)
- if err != nil {
- return err
- }
- defer nstmt.Close()
- err = nstmt.Get(dest, args)
- return
- }
- // QueryRowContext get row with named args
- func (t *Tx) QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = t.tx.PrepareNamedContext(ctx, query)
- if err != nil {
- return err
- }
- defer nstmt.Close()
- err = nstmt.GetContext(ctx, dest, args)
- 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()
- if err != nil {
- return
- }
- err = defaultDB.Ping()
- return
- }
- // PingContext ping connect
- func PingContext(ctx context.Context) (err error) {
- defaultDB.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- err = defaultDB.PingContext(ctx)
- return
- }
- // Select select
- func Select(dest interface{}, query string, args ...interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return err
- }
- err = defaultDB.conn.Select(dest, query, args...)
- return
- }
- // Query get rows with named args
- func Query(dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Select(dest, args)
- return
- }
- // QueryContext get rows with named args
- func QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.SelectContext(ctx, dest, args)
- return
- }
- // Rows get rows with named args
- func Rows(dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Select(dest, args)
- return
- }
- // RowsContext get rows with named args
- func RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.SelectContext(ctx, dest, args)
- return
- }
- // Get get
- func Get(dest interface{}, query string, args ...interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- err = defaultDB.conn.Get(dest, query, args...)
- return
- }
- // QueryRow get row with named args
- func QueryRow(dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Get(dest, args)
- return
- }
- // QueryRowContext get row with named args
- func QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.GetContext(ctx, dest, args)
- return
- }
- // Row get row with named args
- func Row(dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamed(query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.Get(dest, args)
- return
- }
- // RowContext get row with named args
- func RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
- defaultDB.conn, err = connectContext(ctx)
- if err != nil {
- return
- }
- nstmt := &sqlx.NamedStmt{}
- nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
- if err != nil {
- return
- }
- defer nstmt.Close()
- err = nstmt.GetContext(ctx, dest, args)
- 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) {
- defaultDB.conn, err = connect()
- if err != nil {
- return
- }
- lastInsertId, rowsAffected, err = defaultDB.Exec(query, args...)
- return
- }
- // Exec exec
- 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...)
- return
- }
- // NamedExec exec with named args
- 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)
- return
- }
- // NamedExecContext exec with named args
- 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)
- return
- }
|