ls 8 ماه پیش
والد
کامیت
d80b162426
1فایلهای تغییر یافته به همراه131 افزوده شده و 4 حذف شده
  1. 131 4
      db/db.go

+ 131 - 4
db/db.go

@@ -604,14 +604,44 @@ func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{})
 	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)
+		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)
+	return fmt.Sprintf(" LIMIT %d OFFSET %d", pagesize, (page-1)*pagesize)
 }
 
 // Commit commits the transaction.
@@ -624,8 +654,8 @@ func (t *Tx) Rollback() error {
 	return t.tx.Rollback()
 }
 
-// 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) {
+// 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 {
 		RowsAffected, _ = rs.RowsAffected()
 		LastInsertId, _ = rs.LastInsertId()
@@ -633,6 +663,81 @@ func (t *Tx) Exec(query string, args interface{}) (LastInsertId, RowsAffected in
 	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) {
+	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) {
+	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) {
+	if rs, err := t.tx.ExecContext(ctx, query, args...); err == nil {
+		RowsAffected, _ = rs.RowsAffected()
+		LastInsertId, _ = rs.LastInsertId()
+	}
+	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, 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, 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, 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, err := t.tx.PrepareNamedContext(ctx, query)
+	if err != nil {
+		return err
+	}
+	defer nstmt.Close()
+
+	err = nstmt.GetContext(ctx, dest, args)
+	return
+}
+
 // Ping ping connect
 func Ping() (err error) {
 	defaultDB.conn, err = connect()
@@ -834,3 +939,25 @@ func ExecContext(ctx context.Context, query string, args ...interface{}) (LastIn
 	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
+}