-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
65 lines (54 loc) · 1.39 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
package sqliteserver
import (
"database/sql"
"strings"
"github.com/siddontang/go-mysql/mysql"
"go.uber.org/zap"
)
func (z *zHandler) HandleStmtPrepare(query string) (params int, columns int, context interface{}, err error) {
logger.Debug("HandleStmtPrepare", zap.String("query", query))
params = strings.Count(query, "?")
qt := getQueryType(query)
sh := newStatementHandler(qt)
query, params, columns, err = sh.Prepare(query)
var stmt *sql.Stmt
if z.tx != nil {
stmt, err = z.tx.Prepare(query)
} else {
stmt, err = z.db.Prepare(query)
}
if err != nil {
logger.Error("error while prepare statement",
zap.String("query", query),
zap.Error(err),
)
return
}
sh.SetStatement(stmt)
z.mu.Lock()
z.stmtCounter ++
z.stmts[z.stmtCounter] = sh
z.mu.Unlock()
context = z.stmtCounter
return
}
func (z *zHandler) HandleStmtExecute(context interface{}, query string, args []interface{}) (*mysql.Result, error) {
logger.Debug("HandleStmtExecute",
zap.Any("context", context),
zap.String("query", query),
zap.Any("args", args),
)
z.mu.Lock()
stmt := z.stmts[context.(int)]
z.mu.Unlock()
return stmt.Execute(query, args)
}
func (z *zHandler) HandleStmtClose(context interface{}) error {
logger.Debug("HandleStmtClose", zap.Any("context", context))
z.mu.Lock()
stmt := z.stmts[context.(int)]
delete(z.stmts, context.(int))
err := stmt.Close()
z.mu.Unlock()
return err
}