Repository navigation
Expand file tree
/
Copy pathstmt.go
More file actions
59 lines (51 loc) · 2.08 KB
/
Copy pathstmt.go
File metadata and controls
59 lines (51 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package sql
import (
"database/sql"
"github.com/acronis/perfkit/db"
)
// sqlStatement defines the interface for prepared statement operations
type sqlStatement interface {
// Exec executes a prepared statement with the given arguments.
// Returns a Result interface and error.
// Usage example:
// stmt.Exec(2, 2, "test") // for most dialects
// stmt.Exec(sql.Named("Origin", 2), sql.Named("Type", 2), sql.Named("Name", "test")) // for MSSQL
Exec(args ...any) (db.Result, error)
// Close releases resources associated with the prepared statement.
// Should be called when the statement is no longer needed.
// Typically used with defer:
// defer stmt.Close()
Close() error
}
// sqlStmt implements the sqlStatement interface by wrapping a *sql.Stmt
type sqlStmt struct {
stmt *sql.Stmt // underlying SQL prepared statement
}
// Exec executes the prepared statement with the given arguments.
// Implements the sqlStatement interface.
// Args can be either positional parameters (?, $1, etc.) or named parameters (@Name)
// depending on the SQL dialect being used.
// Returns the sql.Result from the underlying statement execution.
func (s *sqlStmt) Exec(args ...any) (db.Result, error) {
return s.stmt.Exec(args...)
}
// Close releases the database resources associated with the prepared statement.
// Should be called when done with the statement to prevent resource leaks.
// Implements the sqlStatement interface.
func (s *sqlStmt) Close() error {
return s.stmt.Close()
}
// Prepare creates a prepared statement for the given query.
// Part of the db.DatabaseAccessor interface.
// The query format depends on the dialect:
// - MSSQL: Uses @param named parameters
// - PostgreSQL: Uses $1, $2, etc. positional parameters
// - Others: Uses ? positional parameters
//
// Example queries:
// - MSSQL: "INSERT INTO table (col1, col2) VALUES (@Param1, @Param2)"
// - PostgreSQL: "INSERT INTO table (col1, col2) VALUES ($1, $2)"
// - Others: "INSERT INTO table (col1, col2) VALUES (?, ?)"
func (g *sqlGateway) Prepare(query string) (db.Stmt, error) {
return g.rw.prepareContext(g.ctx, query)
}