-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
52 lines (43 loc) · 983 Bytes
/
stmt.go
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
package toki
import "database/sql"
// Stmt represents a prepared SQL statement
type Stmt struct {
query string
args []interface{}
db *sql.DB
tx *sql.Tx
}
// Prepare creates a prepared statement
func (b *Builder) Prepare(db *sql.DB) (*Stmt, error) {
query := b.String()
stmt := &Stmt{
query: query,
args: b.args,
db: db,
}
if b.tx != nil {
stmt.tx = b.tx.tx
}
return stmt, nil
}
// Query executes the query and returns rows
func (s *Stmt) Query() (*sql.Rows, error) {
if s.tx != nil {
return s.tx.Query(s.query, s.args...)
}
return s.db.Query(s.query, s.args...)
}
// QueryRow executes the query and returns a single row
func (s *Stmt) QueryRow() *sql.Row {
if s.tx != nil {
return s.tx.QueryRow(s.query, s.args...)
}
return s.db.QueryRow(s.query, s.args...)
}
// Exec executes the statement
func (s *Stmt) Exec() (sql.Result, error) {
if s.tx != nil {
return s.tx.Exec(s.query, s.args...)
}
return s.db.Exec(s.query, s.args...)
}