-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
37 lines (30 loc) · 797 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
package cluster
import (
"database/sql"
"github.com/pkg/errors"
)
// RegisterStmt register a SQL statement.
//
// Registered statements will be prepared upfront and re-used, to speed up
// execution.
//
// Return a unique registration code.
func RegisterStmt(sql string) int {
code := len(stmts)
stmts[code] = sql
return code
}
// PrepareStmts prepares all registered statements and returns an index from
// statement code to prepared statement object.
func PrepareStmts(db *sql.DB) (map[int]*sql.Stmt, error) {
index := map[int]*sql.Stmt{}
for code, sql := range stmts {
stmt, err := db.Prepare(sql)
if err != nil {
return nil, errors.Wrapf(err, "%q", sql)
}
index[code] = stmt
}
return index, nil
}
var stmts = map[int]string{} // Statement code to statement SQL text