Browse Source

Move PrepareSQL func to engine

Volodymyr Tkach 2 years ago
parent
commit
813f6d730c
3 changed files with 13 additions and 4 deletions
  1. 5 4
      gosql/common/common.go
  2. 4 0
      gosql/common/dbmethods.go
  3. 4 0
      gosql/common/tx.go

+ 5 - 4
gosql/common/common.go

@@ -32,6 +32,7 @@ type Engine interface {
 	InsertRow(ctx context.Context, row any) error
 	Ping(context.Context) error
 	Prepare(ctx context.Context, query string) (*sql.Stmt, error)
+	PrepareSQL(query string, args ...any) *Prepared
 	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
@@ -156,6 +157,10 @@ func log(w io.Writer, fname string, start time.Time, err error, tx bool, query s
 	return res
 }
 
+func prepareSQL(query string, args ...any) *Prepared {
+	return &Prepared{query, args}
+}
+
 func queryRowByIDString(row any) string {
 	v := reflect.ValueOf(row).Elem()
 	t := v.Type()
@@ -247,7 +252,3 @@ func OpenDB(databaseURL *url.URL, migrationsDir string, skipMigration bool, debu
 
 	return db, nil
 }
-
-func PrepareSQL(query string, args ...any) *Prepared {
-	return &Prepared{query, args}
-}

+ 4 - 0
gosql/common/dbmethods.go

@@ -112,6 +112,10 @@ func (d *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, error
 	return stm, err
 }
 
+func (d *DBMethods) PrepareSQL(query string, args ...any) *Prepared {
+	return prepareSQL(query, args...)
+}
+
 func (d *DBMethods) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
 	start := time.Now()
 	rows, err := d.DB.QueryContext(ctx, d.fixQuery(query), args...)

+ 4 - 0
gosql/common/tx.go

@@ -92,6 +92,10 @@ func (t *Tx) InsertRow(ctx context.Context, row any) error {
 	return err
 }
 
+func (t *Tx) PrepareSQL(query string, args ...any) *Prepared {
+	return prepareSQL(query, 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...)