-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStmt.go
43 lines (39 loc) · 1.21 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
package db
import (
"database/sql"
"errors"
"github.com/ssgo/log"
"time"
)
type Stmt struct {
conn *sql.Stmt
lastSql *string
lastArgs []interface{}
Error error
logger *dbLogger
}
func (stmt *Stmt) Exec(args ...interface{}) *ExecResult {
stmt.lastArgs = args
if stmt.conn == nil {
return &ExecResult{Sql: stmt.lastSql, Args: stmt.lastArgs, usedTime: -1, logger: stmt.logger, Error: errors.New("operate on a bad connection")}
}
startTime := time.Now()
r, err := stmt.conn.Exec(args...)
endTime := time.Now()
if err != nil {
//logError(err, stmt.lastSql, stmt.lastArgs)
stmt.logger.LogQueryError(err.Error(), *stmt.lastSql, stmt.lastArgs, log.MakeUesdTime(startTime, endTime))
return &ExecResult{Sql: stmt.lastSql, Args: stmt.lastArgs, usedTime: log.MakeUesdTime(startTime, endTime), logger: stmt.logger, Error: err}
}
return &ExecResult{Sql: stmt.lastSql, Args: stmt.lastArgs, usedTime: log.MakeUesdTime(startTime, endTime), logger: stmt.logger, result: r}
}
func (stmt *Stmt) Close() error {
if stmt.conn == nil {
return errors.New("operate on a bad connection")
}
err := stmt.conn.Close()
if err != nil {
stmt.logger.LogQueryError(err.Error(), *stmt.lastSql, stmt.lastArgs, -1)
}
return err
}