ls vor 8 Monaten
Ursprung
Commit
a0cb5238dc
3 geänderte Dateien mit 308 neuen und 14 gelöschten Zeilen
  1. 289 11
      db/db.go
  2. 7 3
      go.mod
  3. 12 0
      go.sum

+ 289 - 11
db/db.go

@@ -1,6 +1,7 @@
 package db
 
 import (
+	"context"
 	"database/sql"
 	"errors"
 	"fmt"
@@ -10,8 +11,9 @@ import (
 	// PostgreSQL
 	_ "github.com/lib/pq"
 	// MySQL
-	//_ "github.com/go-sql-driver/mysql"
+	_ "github.com/go-sql-driver/mysql"
 	"github.com/jmoiron/sqlx"
+	_ "github.com/mattn/go-sqlite3"
 )
 
 var (
@@ -32,6 +34,11 @@ type DB struct {
 	tx     *sqlx.Tx
 }
 
+// Tx is an in-progress database transaction.
+type Tx struct {
+	tx *sqlx.Tx
+}
+
 // SetConfig set
 func SetConfig(cfg Config) {
 	config.Driver = cfg.Driver
@@ -69,7 +76,7 @@ func NewConfig(config Config) (dbx *DB, err error) {
 	dbx.conn.SetMaxIdleConns(config.MaxIdle)
 	dbx.conn.SetConnMaxIdleTime(config.MaxIdleTime)
 	dbx.conn.SetConnMaxLifetime(config.MaxLifeTime)
-	err = dbx.conn.Ping()
+	//err = dbx.conn.Ping()
 	return
 }
 
@@ -91,11 +98,33 @@ func connect() (dbx *sqlx.DB, err error) {
 		db.DB.SetMaxIdleConns(config.MaxIdle)
 		db.DB.SetConnMaxIdleTime(config.MaxIdleTime)
 		db.DB.SetConnMaxLifetime(config.MaxLifeTime)
-		err = db.Ping()
+		/*
+			err = db.Ping()
+			if err != nil {
+				fmt.Println("Connect Ping", err)
+				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 {
-			fmt.Println("Connect Ping", err)
+			fmt.Println("Connect ERR", err)
 			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
@@ -121,6 +150,16 @@ func (d *DB) Connect() (err error) {
 	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
@@ -137,6 +176,16 @@ func (d *DB) Ping() (err error) {
 	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()
@@ -148,6 +197,41 @@ func (d *DB) BeginTrans() (err error) {
 	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()
@@ -158,7 +242,7 @@ func (d *DB) Rollback() (err error) {
 	return d.tx.Rollback()
 }
 
-// TransExec trans execute
+// TransExec trans execute with named args
 func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
 	if rs, err := d.tx.NamedExec(query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
@@ -167,6 +251,15 @@ func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffect
 	return
 }
 
+// TransExec trans execute with named args
+func (d *DB) TransExecContext(ctx context.Context, query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
+	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 (
@@ -209,7 +302,20 @@ func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
 	return err
 }
 
-// Rows get rows
+// SelectContext select
+func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
+	err := d.ConnectContext(ctx)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	err = d.conn.SelectContext(ctx, dest, query, args...)
+
+	return err
+}
+
+// Rows get rows with named args
 func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
 	err := d.Connect()
 	if err != nil {
@@ -228,6 +334,25 @@ func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
 	return err
 }
 
+// RowsContext get rows with named args
+func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) error {
+	err := d.ConnectContext(ctx)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	nstmt, err := d.conn.PrepareNamedContext(ctx, query)
+	if err != nil {
+		return err
+	}
+	defer nstmt.Close()
+
+	err = nstmt.SelectContext(ctx, dest, args)
+
+	return err
+}
+
 // Get get
 func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
 	err := d.Connect()
@@ -241,7 +366,20 @@ func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
 	return err
 }
 
-// Row get row
+// GetContext get
+func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
+	err := d.ConnectContext(ctx)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	err = d.conn.GetContext(ctx, dest, query, args...)
+
+	return err
+}
+
+// Row get row with named args
 func (d *DB) Row(dest interface{}, query string, args interface{}) error {
 	err := d.Connect()
 	if err != nil {
@@ -260,6 +398,25 @@ func (d *DB) Row(dest interface{}, query string, args interface{}) error {
 	return err
 }
 
+// RowContext get row with named args
+func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, args interface{}) error {
+	err := d.ConnectContext(ctx)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	nstmt, err := d.conn.PrepareNamedContext(ctx, query)
+	if err != nil {
+		return err
+	}
+	defer nstmt.Close()
+
+	err = nstmt.GetContext(ctx, dest, args)
+
+	return err
+}
+
 // InsertReply insert and return DbReply
 func (d *DB) InsertReply(query string, args interface{}) (reply Reply) {
 	var (
@@ -305,7 +462,7 @@ func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
 	return
 }
 
-// Insert insert into
+// 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 {
@@ -320,7 +477,22 @@ func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected
 	return
 }
 
-// Update update/delete
+// 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 {
@@ -335,6 +507,21 @@ func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err err
 	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()
@@ -350,6 +537,21 @@ func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, 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
+}
+
 // Limit MySQL limit
 func (d *DB) Limit(page, pagesize int) string {
 	// MySQL limit 0, size
@@ -360,6 +562,25 @@ func (d *DB) Limit(page, pagesize int) string {
 	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()
+}
+
+// TransExec trans execute with named args
+func (t *Tx) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
+	if rs, err := t.tx.NamedExec(query, args); err == nil {
+		RowsAffected, _ = rs.RowsAffected()
+		LastInsertId, _ = rs.LastInsertId()
+	}
+	return
+}
+
 // Ping ping connect
 func Ping() (err error) {
 	defaultDB.conn, err = connect()
@@ -371,6 +592,17 @@ func Ping() (err error) {
 	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()
@@ -382,7 +614,7 @@ func Select(dest interface{}, query string, args ...interface{}) (err error) {
 	return
 }
 
-// Rows get rows
+// Rows get rows with named args
 func Rows(dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connect()
 	if err != nil {
@@ -399,6 +631,23 @@ func Rows(dest interface{}, query string, args interface{}) (err error) {
 	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 err
+	}
+
+	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()
@@ -410,7 +659,7 @@ func Get(dest interface{}, query string, args ...interface{}) (err error) {
 	return
 }
 
-// Row get row
+// Row get row with named args
 func Row(dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connect()
 	if err != nil {
@@ -427,6 +676,24 @@ func Row(dest interface{}, query string, args interface{}) (err error) {
 	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, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	if err != nil {
+		return err
+	}
+	defer nstmt.Close()
+
+	err = nstmt.GetContext(ctx, dest, args)
+	return
+}
+
+// Exec exec
 func Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
 	defaultDB.conn, err = connect()
 	if err != nil {
@@ -436,3 +703,14 @@ func Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64,
 	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
+}

+ 7 - 3
go.mod

@@ -3,10 +3,14 @@ module git.chuangxin1.com/cx/myth
 go 1.17
 
 require (
-	github.com/go-redis/redis/v7 v7.4.0
-	github.com/golang/protobuf v1.3.2
+	github.com/go-redis/redis/v7 v7.4.1
+	github.com/go-sql-driver/mysql v1.8.0
+	github.com/golang/protobuf v1.5.4
 	github.com/jmoiron/sqlx v1.3.5
 	github.com/lib/pq v1.10.9
+	github.com/mattn/go-sqlite3 v1.14.22
 	github.com/ugorji/go/codec v1.2.12
-	gopkg.in/yaml.v2 v2.2.4
+	gopkg.in/yaml.v2 v2.4.0
 )
+
+require filippo.io/edwards25519 v1.1.0 // indirect

+ 12 - 0
go.sum

@@ -1,11 +1,19 @@
+filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
+filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4=
 github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
+github.com/go-redis/redis/v7 v7.4.1 h1:PASvf36gyUpr2zdOUS/9Zqc80GbM+9BDyiJSJDDOrTI=
+github.com/go-redis/redis/v7 v7.4.1/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
 github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
 github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
+github.com/go-sql-driver/mysql v1.8.0 h1:UtktXaU2Nb64z/pLiGIxY4431SJ4/dR5cjMmlVHgnT4=
+github.com/go-sql-driver/mysql v1.8.0/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
 github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
+github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
@@ -20,6 +28,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
 github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
 github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
+github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
+github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
 github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -50,3 +60,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
 gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=