|
@@ -33,6 +33,24 @@ func Open(driverName, dataSourceName string) (*DB, error) {
|
|
return &DB{db: db}, err
|
|
return &DB{db: db}, err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (this *DB) Begin(ctx context.Context) (*Tx, error) {
|
|
|
|
+ tx, err := this.db.BeginTx(ctx, &sql.TxOptions{
|
|
|
|
+ Isolation: sql.LevelDefault,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ if consts.ParamDebug {
|
|
|
|
+ log("[TX] TRANSACTION START", time.Now(), err, true)
|
|
|
|
+ }
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ if consts.ParamDebug {
|
|
|
|
+ s := time.Now()
|
|
|
|
+ log("[TX] TRANSACTION START", s, err, true)
|
|
|
|
+ return &Tx{tx, s}, err
|
|
|
|
+ }
|
|
|
|
+ return &Tx{tx, time.Now()}, err
|
|
|
|
+}
|
|
|
|
+
|
|
func (this *DB) Close() error {
|
|
func (this *DB) Close() error {
|
|
if consts.ParamDebug {
|
|
if consts.ParamDebug {
|
|
err := this.db.Close()
|
|
err := this.db.Close()
|
|
@@ -42,6 +60,16 @@ func (this *DB) Close() error {
|
|
return this.db.Close()
|
|
return this.db.Close()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (this *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
|
|
|
+ if consts.ParamDebug {
|
|
|
|
+ s := time.Now()
|
|
|
|
+ r, e := this.db.ExecContext(ctx, query, args...)
|
|
|
|
+ log(query, s, e, false)
|
|
|
|
+ return r, e
|
|
|
|
+ }
|
|
|
|
+ return this.db.ExecContext(ctx, query, args...)
|
|
|
|
+}
|
|
|
|
+
|
|
func (this *DB) Ping(ctx context.Context) error {
|
|
func (this *DB) Ping(ctx context.Context) error {
|
|
if consts.ParamDebug {
|
|
if consts.ParamDebug {
|
|
err := this.db.PingContext(ctx)
|
|
err := this.db.PingContext(ctx)
|
|
@@ -51,16 +79,14 @@ func (this *DB) Ping(ctx context.Context) error {
|
|
return this.db.PingContext(ctx)
|
|
return this.db.PingContext(ctx)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *DB) SetConnMaxLifetime(d time.Duration) {
|
|
|
|
- this.db.SetConnMaxLifetime(d)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (this *DB) SetMaxIdleConns(n int) {
|
|
|
|
- this.db.SetMaxIdleConns(n)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (this *DB) SetMaxOpenConns(n int) {
|
|
|
|
- this.db.SetMaxOpenConns(n)
|
|
|
|
|
|
+func (this *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
|
|
|
+ if consts.ParamDebug {
|
|
|
|
+ s := time.Now()
|
|
|
|
+ r, e := this.db.QueryContext(ctx, query, args...)
|
|
|
|
+ log(query, s, e, false)
|
|
|
|
+ return r, e
|
|
|
|
+ }
|
|
|
|
+ return this.db.Query(query, args...)
|
|
}
|
|
}
|
|
|
|
|
|
func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
|
func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
|
@@ -73,42 +99,16 @@ func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{})
|
|
return this.db.QueryRow(query, args...)
|
|
return this.db.QueryRow(query, args...)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *DB) Begin(ctx context.Context) (*Tx, error) {
|
|
|
|
- tx, err := this.db.BeginTx(ctx, &sql.TxOptions{
|
|
|
|
- Isolation: sql.LevelDefault,
|
|
|
|
- })
|
|
|
|
- if err != nil {
|
|
|
|
- if consts.ParamDebug {
|
|
|
|
- log("[TX] TRANSACTION START", time.Now(), err, true)
|
|
|
|
- }
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if consts.ParamDebug {
|
|
|
|
- s := time.Now()
|
|
|
|
- log("[TX] TRANSACTION START", s, err, true)
|
|
|
|
- return &Tx{tx, s}, err
|
|
|
|
- }
|
|
|
|
- return &Tx{tx, time.Now()}, err
|
|
|
|
|
|
+func (this *DB) SetConnMaxLifetime(d time.Duration) {
|
|
|
|
+ this.db.SetConnMaxLifetime(d)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
|
|
|
- if consts.ParamDebug {
|
|
|
|
- s := time.Now()
|
|
|
|
- r, e := this.db.QueryContext(ctx, query, args...)
|
|
|
|
- log(query, s, e, false)
|
|
|
|
- return r, e
|
|
|
|
- }
|
|
|
|
- return this.db.Query(query, args...)
|
|
|
|
|
|
+func (this *DB) SetMaxIdleConns(n int) {
|
|
|
|
+ this.db.SetMaxIdleConns(n)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
|
|
|
- if consts.ParamDebug {
|
|
|
|
- s := time.Now()
|
|
|
|
- r, e := this.db.ExecContext(ctx, query, args...)
|
|
|
|
- log(query, s, e, false)
|
|
|
|
- return r, e
|
|
|
|
- }
|
|
|
|
- return this.db.ExecContext(ctx, query, args...)
|
|
|
|
|
|
+func (this *DB) SetMaxOpenConns(n int) {
|
|
|
|
+ this.db.SetMaxOpenConns(n)
|
|
}
|
|
}
|
|
|
|
|
|
func (this *DB) Transaction(ctx context.Context, queries func(ctx context.Context, tx *Tx) error) error {
|
|
func (this *DB) Transaction(ctx context.Context, queries func(ctx context.Context, tx *Tx) error) error {
|