ls 8 miesięcy temu
rodzic
commit
4db368d29e
1 zmienionych plików z 135 dodań i 15 usunięć
  1. 135 15
      db/db.go

+ 135 - 15
db/db.go

@@ -315,6 +315,44 @@ func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string,
 	return err
 }
 
+// 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()
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	nstmt, err := d.conn.PrepareNamed(query)
+	if err != nil {
+		return err
+	}
+	defer nstmt.Close()
+
+	err = nstmt.Select(dest, args)
+
+	return err
+}
+
+// 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)
+	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
+}
+
 // Rows get rows with named args
 func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
 	err := d.Connect()
@@ -353,17 +391,37 @@ func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, ar
 	return err
 }
 
-// Get get
-func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
+// 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()
 	if err != nil {
 		return err
 	}
 	defer d.Close()
 
-	err = d.conn.Get(dest, query, args...)
+	return d.conn.Get(dest, query, args...)
+}
 
-	return err
+// 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)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	return d.conn.GetContext(ctx, dest, query, args...)
+}
+
+// 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()
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	return d.conn.Get(dest, query, args...)
 }
 
 // GetContext get
@@ -374,9 +432,7 @@ func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, arg
 	}
 	defer d.Close()
 
-	err = d.conn.GetContext(ctx, dest, query, args...)
-
-	return err
+	return d.conn.GetContext(ctx, dest, query, args...)
 }
 
 // Row get row with named args
@@ -393,9 +449,7 @@ func (d *DB) Row(dest interface{}, query string, args interface{}) error {
 	}
 	defer nstmt.Close()
 
-	err = nstmt.Get(dest, args)
-
-	return err
+	return nstmt.Get(dest, args)
 }
 
 // RowContext get row with named args
@@ -412,9 +466,7 @@ func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, arg
 	}
 	defer nstmt.Close()
 
-	err = nstmt.GetContext(ctx, dest, args)
-
-	return err
+	return nstmt.GetContext(ctx, dest, args)
 }
 
 // InsertReply insert and return DbReply
@@ -572,8 +624,8 @@ 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) {
+// 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.NamedExec(query, args); err == nil {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
@@ -614,6 +666,40 @@ func Select(dest interface{}, query string, args ...interface{}) (err error) {
 	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 err
+	}
+
+	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 err
+	}
+
+	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()
@@ -659,6 +745,40 @@ func Get(dest interface{}, query string, args ...interface{}) (err error) {
 	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, err := defaultDB.conn.PrepareNamed(query)
+	if err != nil {
+		return err
+	}
+	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, err := defaultDB.conn.PrepareNamedContext(ctx, query)
+	if err != nil {
+		return err
+	}
+	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()