Browse Source

Add methods for prepared SQLs

Volodymyr Tkach 2 years ago
parent
commit
a94f9d7e9d
4 changed files with 42 additions and 0 deletions
  1. 8 0
      gosql/common/common.go
  2. 16 0
      gosql/common/dbmethods.go
  3. 6 0
      gosql/common/prepared.go
  4. 12 0
      gosql/common/tx.go

+ 8 - 0
gosql/common/common.go

@@ -25,12 +25,16 @@ type Engine interface {
 	CurrentUnixTimestamp() int64
 	DeleteRowByID(ctx context.Context, id int64, row any) error
 	Each(ctx context.Context, query string, logic func(ctx context.Context, rows *Rows) error, args ...any) error
+	EachPrepared(ctx context.Context, prep *Prepared, logic func(ctx context.Context, rows *Rows) error) error
 	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
+	ExecPrepared(ctx context.Context, prep *Prepared) (sql.Result, error)
 	Ping(context.Context) error
 	Prepare(ctx context.Context, query string) (*sql.Stmt, error)
 	Query(ctx context.Context, query string, args ...any) (*Rows, error)
+	QueryPrepared(ctx context.Context, prep *Prepared) (*Rows, error)
 	QueryRow(ctx context.Context, query string, args ...any) *Row
 	QueryRowByID(ctx context.Context, id int64, row any) error
+	QueryRowPrepared(ctx context.Context, prep *Prepared) *Row
 	RowExists(ctx context.Context, id int64, row any) bool
 	SetConnMaxLifetime(d time.Duration)
 	SetMaxIdleConns(n int)
@@ -205,3 +209,7 @@ func OpenDB(databaseURL *url.URL, migrationsDir string, skipMigration bool, debu
 
 	return db, nil
 }
+
+func PrepareSQL(query string, args ...any) *Prepared {
+	return &Prepared{query, args}
+}

+ 16 - 0
gosql/common/dbmethods.go

@@ -77,6 +77,10 @@ func (d *DBMethods) Each(ctx context.Context, query string, callback func(ctx co
 	return nil
 }
 
+func (d *DBMethods) EachPrepared(ctx context.Context, prep *Prepared, callback func(ctx context.Context, rows *Rows) error) error {
+	return d.Each(ctx, prep.Query, callback, prep.Args...)
+}
+
 func (d *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
 	start := time.Now()
 	res, err := d.DB.ExecContext(ctx, d.fixQuery(query), args...)
@@ -84,6 +88,10 @@ func (d *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Re
 	return res, err
 }
 
+func (d *DBMethods) ExecPrepared(ctx context.Context, prep *Prepared) (sql.Result, error) {
+	return d.Exec(ctx, prep.Query, prep.Query)
+}
+
 func (d *DBMethods) Ping(ctx context.Context) error {
 	start := time.Now()
 	err := d.DB.PingContext(ctx)
@@ -105,6 +113,10 @@ func (d *DBMethods) Query(ctx context.Context, query string, args ...any) (*Rows
 	return &Rows{Rows: rows}, err
 }
 
+func (d *DBMethods) QueryPrepared(ctx context.Context, prep *Prepared) (*Rows, error) {
+	return d.Query(ctx, prep.Query, prep.Args...)
+}
+
 func (d *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *Row {
 	start := time.Now()
 	row := d.DB.QueryRowContext(ctx, d.fixQuery(query), args...)
@@ -117,6 +129,10 @@ func (d *DBMethods) QueryRowByID(ctx context.Context, id int64, row any) error {
 	return d.QueryRow(ctx, query, id).Scans(row)
 }
 
+func (d *DBMethods) QueryRowPrepared(ctx context.Context, prep *Prepared) *Row {
+	return d.QueryRow(ctx, prep.Query, prep.Args...)
+}
+
 func (d *DBMethods) RowExists(ctx context.Context, id int64, row any) bool {
 	var exists int
 	query := rowExistsString(row)

+ 6 - 0
gosql/common/prepared.go

@@ -0,0 +1,6 @@
+package common
+
+type Prepared struct {
+	Query string
+	Args  []any
+}

+ 12 - 0
gosql/common/tx.go

@@ -52,6 +52,10 @@ func (t *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result, e
 	return res, err
 }
 
+func (t *Tx) ExecPrepared(ctx context.Context, prep *Prepared) (sql.Result, error) {
+	return t.Exec(ctx, prep.Query, prep.Args...)
+}
+
 func (t *Tx) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
 	start := time.Now()
 	rows, err := t.tx.QueryContext(ctx, t.fixQuery(query), args...)
@@ -59,6 +63,10 @@ func (t *Tx) Query(ctx context.Context, query string, args ...any) (*Rows, error
 	return &Rows{Rows: rows}, err
 }
 
+func (t *Tx) QueryPrepared(ctx context.Context, prep *Prepared) (*Rows, error) {
+	return t.Query(ctx, prep.Query, prep.Args...)
+}
+
 func (t *Tx) QueryRow(ctx context.Context, query string, args ...any) *Row {
 	start := time.Now()
 	row := t.tx.QueryRowContext(ctx, t.fixQuery(query), args...)
@@ -71,6 +79,10 @@ func (t *Tx) QueryRowByID(ctx context.Context, id int64, row any) error {
 	return t.QueryRow(ctx, query, id).Scans(row)
 }
 
+func (t *Tx) QueryRowPrepared(ctx context.Context, prep *Prepared) *Row {
+	return t.QueryRow(ctx, prep.Query, prep.Args...)
+}
+
 func (t *Tx) RowExists(ctx context.Context, id int64, row any) bool {
 	var exists int
 	query := rowExistsString(row)