-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstmt.go
More file actions
119 lines (104 loc) · 3.23 KB
/
stmt.go
File metadata and controls
119 lines (104 loc) · 3.23 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package sqlz
import (
"bytes"
"context"
"database/sql"
"fmt"
)
// Stmt represents a SQL statement.
type Stmt struct {
sqlBuffer bytes.Buffer
lastChar byte
values []interface{}
args []interface{}
}
// NewStmt returns a Stmt with the given SQL fragment.
func NewStmt(sqlFrag string) *Stmt {
var s Stmt
s.sqlBuffer.WriteString(sqlFrag)
s.lastChar = sqlFrag[len(sqlFrag)-1]
return &s
}
// Append adds the given SQL fragment to the end of the Stmt.
func (s *Stmt) Append(sqlFrag string) *Stmt {
if s.lastChar != ' ' {
s.sqlBuffer.WriteByte(' ')
}
s.sqlBuffer.WriteString(sqlFrag)
s.lastChar = sqlFrag[len(sqlFrag)-1]
return s
}
// Trim removes the given SQL fragment from the end of the Stmt.
func (s *Stmt) Trim(sqlFrag string) *Stmt {
if bytes.HasSuffix(s.sqlBuffer.Bytes(), []byte(sqlFrag)) {
s.sqlBuffer.Truncate(s.sqlBuffer.Len() - len(sqlFrag))
}
return s
}
// Scan adds values as the output of the Stmt.
func (s *Stmt) Scan(values ...interface{}) *Stmt {
s.values = append(s.values, values...)
return s
}
// Format adds arguments as the input of the Stmt.
func (s *Stmt) Format(args ...interface{}) *Stmt {
s.args = append(s.args, args...)
return s
}
// Execer is an interface implemented by `sql.DB` and `sql.Tx`.
type Execer interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error)
}
var _, _ Execer = (*sql.DB)(nil), (*sql.Tx)(nil)
// Exec executes the Stmt.
func (s *Stmt) Exec(ctx context.Context, execer Execer) (sql.Result, error) {
sql := s.SQL()
result, err := execer.ExecContext(ctx, sql, s.args...)
if err != nil {
return nil, fmt.Errorf("execute statement; sql=%q: %w", sql, err)
}
return result, err
}
// Queryer is an interface implemented by `sql.DB` and `sql.Tx`.
type Queryer interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) (row *sql.Row)
QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)
}
var _, _ Queryer = (*sql.DB)(nil), (*sql.Tx)(nil)
// QueryRow executes the Stmt as a query to retrieve a single row.
func (s *Stmt) QueryRow(ctx context.Context, queryer Queryer) error {
sql := s.SQL()
row := queryer.QueryRowContext(ctx, sql, s.args...)
if err := row.Scan(s.values...); err != nil {
return fmt.Errorf("scan row; sql=%q: %w", sql, err)
}
return nil
}
// Query executes the Stmt as a query to retrieve rows.
// The given callback will be called for each row retrieved. If the callback returns false,
// the iteration will be stopped.
func (s *Stmt) Query(ctx context.Context, queryer Queryer, callback func() bool) error {
sql := s.SQL()
rows, err := queryer.QueryContext(ctx, sql, s.args...)
if err != nil {
return fmt.Errorf("execute query; sql=%q: %w", sql, err)
}
for rows.Next() {
if err := rows.Scan(s.values...); err != nil {
rows.Close()
return fmt.Errorf("scan row; sql=%q: %w", sql, err)
}
if !callback() {
if err := rows.Close(); err != nil {
return fmt.Errorf("close rows; sql=%q: %w", sql, err)
}
break
}
}
if err := rows.Err(); err != nil {
return fmt.Errorf("iterate rows; sql=%q: %w", sql, err)
}
return nil
}
// SQL returns the underlying SQL to be executed.
func (s *Stmt) SQL() string { return s.sqlBuffer.String() }