Browse Source

fix db overwrite err

ls 1 month ago
parent
commit
ad07672e8e
1 changed files with 120 additions and 89 deletions
  1. 120 89
      db/db.go

+ 120 - 89
db/db.go

@@ -244,7 +244,8 @@ 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) {
-	if rs, err := d.tx.NamedExec(query, args); err == nil {
+	var rs sql.Result
+	if rs, err = d.tx.NamedExec(query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -253,7 +254,8 @@ func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffect
 
 // 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 {
+	var rs sql.Result
+	if rs, err = d.tx.NamedExecContext(ctx, query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -278,7 +280,8 @@ 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, err := d.tx.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.tx.PrepareNamed(query)
 	if err != nil {
 		return err
 	}
@@ -290,183 +293,189 @@ func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err err
 }
 
 // Select select
-func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
-	err := d.Connect()
+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 err
+	return
 }
 
 // SelectContext select
-func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
 	err = d.conn.SelectContext(ctx, dest, query, args...)
-
-	return err
+	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{}) error {
-	err := d.Connect()
+func (d *DB) Query(dest interface{}, query string, args interface{}) (err error) {
+	err = d.Connect()
 	if err != nil {
 		return err
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamed(query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
 	err = nstmt.Select(dest, args)
-
-	return err
+	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{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
 	err = nstmt.SelectContext(ctx, dest, args)
-
-	return err
+	return
 }
 
 // Rows get rows with named args
-func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
-	err := d.Connect()
+func (d *DB) Rows(dest interface{}, query string, args interface{}) (err error) {
+	err = d.Connect()
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamed(query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
 	err = nstmt.Select(dest, args)
-
-	return err
+	return
 }
 
 // RowsContext get rows with named args
-func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
 		return err
 	}
 	defer nstmt.Close()
 
 	err = nstmt.SelectContext(ctx, dest, args)
-
-	return err
+	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{}) error {
-	err := d.Connect()
+func (d *DB) QueryRow(dest interface{}, query string, args ...interface{}) (err error) {
+	err = d.Connect()
 	if err != nil {
 		return err
 	}
 	defer d.Close()
 
-	return d.conn.Get(dest, query, args...)
+	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{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) QueryRowContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	return d.conn.GetContext(ctx, dest, query, args...)
+	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{}) error {
-	err := d.Connect()
+func (d *DB) Get(dest interface{}, query string, args ...interface{}) (err error) {
+	err = d.Connect()
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	return d.conn.Get(dest, query, args...)
+	err = d.conn.Get(dest, query, args...)
+	return
 }
 
 // GetContext get
-func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	return d.conn.GetContext(ctx, dest, query, args...)
+	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{}) error {
-	err := d.Connect()
+func (d *DB) Row(dest interface{}, query string, args interface{}) (err error) {
+	err = d.Connect()
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamed(query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
-	return nstmt.Get(dest, args)
+	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{}) error {
-	err := d.ConnectContext(ctx)
+func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
+	err = d.ConnectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 	defer d.Close()
 
-	nstmt, err := d.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = d.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
-	return nstmt.GetContext(ctx, dest, args)
+	err = nstmt.GetContext(ctx, dest, args)
+	return
 }
 
 // InsertReply insert and return DbReply
@@ -521,6 +530,7 @@ func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.NamedExec(query, args); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -536,6 +546,7 @@ func (d *DB) InsertContext(ctx context.Context, query string, args interface{})
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -581,6 +592,7 @@ func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.Exec(query, args...); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -596,6 +608,7 @@ func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{})
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.ExecContext(ctx, query, args...); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -611,6 +624,7 @@ func (d *DB) NamedExec(query string, args interface{}) (LastInsertId, RowsAffect
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.NamedExec(query, args); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -626,6 +640,7 @@ func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{
 		return
 	}
 	defer d.Close()
+
 	var rs sql.Result
 	if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
 		LastInsertId, _ = rs.LastInsertId()
@@ -656,7 +671,8 @@ 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) {
-	if rs, err := t.tx.NamedExec(query, args); err == nil {
+	var rs sql.Result
+	if rs, err = t.tx.NamedExec(query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -665,7 +681,8 @@ func (t *Tx) NamedExec(query string, args interface{}) (LastInsertId, RowsAffect
 
 // 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) {
-	if rs, err := t.tx.NamedExecContext(ctx, query, args); err == nil {
+	var rs sql.Result
+	if rs, err = t.tx.NamedExecContext(ctx, query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -674,7 +691,8 @@ func (t *Tx) NamedExecContext(ctx context.Context, query string, args interface{
 
 // 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) {
-	if rs, err := t.tx.Exec(query, args...); err == nil {
+	var rs sql.Result
+	if rs, err = t.tx.Exec(query, args...); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -683,7 +701,8 @@ func (t *Tx) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected
 
 // 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) {
-	if rs, err := t.tx.ExecContext(ctx, query, args...); err == nil {
+	var rs sql.Result
+	if rs, err = t.tx.ExecContext(ctx, query, args...); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
 	}
@@ -692,7 +711,8 @@ func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{})
 
 // 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, err := t.tx.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = t.tx.PrepareNamed(query)
 	if err != nil {
 		return
 	}
@@ -704,7 +724,8 @@ 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, err := t.tx.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = t.tx.PrepareNamedContext(ctx, query)
 	if err != nil {
 		return
 	}
@@ -716,7 +737,8 @@ 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, err := t.tx.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = t.tx.PrepareNamed(query)
 	if err != nil {
 		return err
 	}
@@ -728,7 +750,8 @@ 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, err := t.tx.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = t.tx.PrepareNamedContext(ctx, query)
 	if err != nil {
 		return err
 	}
@@ -775,10 +798,11 @@ func Select(dest interface{}, query string, args ...interface{}) (err error) {
 func Query(dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connect()
 	if err != nil {
-		return err
+		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamed(query)
 	if err != nil {
 		return
 	}
@@ -792,10 +816,11 @@ func Query(dest interface{}, query string, args interface{}) (err error) {
 func QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
 		return
 	}
@@ -809,10 +834,11 @@ func QueryContext(ctx context.Context, dest interface{}, query string, args inte
 func Rows(dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connect()
 	if err != nil {
-		return err
+		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamed(query)
 	if err != nil {
 		return
 	}
@@ -826,10 +852,11 @@ func Rows(dest interface{}, query string, args interface{}) (err error) {
 func RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
 	defaultDB.conn, err = connectContext(ctx)
 	if err != nil {
-		return err
+		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
 		return
 	}
@@ -857,9 +884,10 @@ func QueryRow(dest interface{}, query string, args interface{}) (err error) {
 		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamed(query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
@@ -874,9 +902,10 @@ func QueryRowContext(ctx context.Context, dest interface{}, query string, args i
 		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
@@ -891,9 +920,10 @@ func Row(dest interface{}, query string, args interface{}) (err error) {
 		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamed(query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamed(query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()
 
@@ -908,9 +938,10 @@ func RowContext(ctx context.Context, dest interface{}, query string, args interf
 		return
 	}
 
-	nstmt, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	nstmt := new(sqlx.NamedStmt)
+	nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
 	if err != nil {
-		return err
+		return
 	}
 	defer nstmt.Close()