forked from Oskang09/goloquent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
83 lines (71 loc) · 1.37 KB
/
statement.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package goloquent
import (
"bytes"
"database/sql"
"strings"
"time"
)
type stmt struct {
statement *bytes.Buffer
arguments []interface{}
}
func (s stmt) string() string {
return s.statement.String()
}
func (s stmt) isZero() bool {
return !(s.statement.Len() > 0)
}
type replacer interface {
Bind(uint) string
Value(interface{}) string
}
// Stmt :
type Stmt struct {
stmt
crud string
replacer replacer
startTime time.Time
endTime time.Time
Result sql.Result
}
func (s *Stmt) startTrace() {
s.startTime = time.Now().UTC()
}
func (s *Stmt) stopTrace() {
s.endTime = time.Now().UTC()
}
// TimeElapse :
func (s Stmt) TimeElapse() time.Duration {
return s.endTime.Sub(s.startTime)
}
// Raw :
func (s *Stmt) Raw() string {
buf := new(bytes.Buffer)
if len(s.arguments) <= 0 {
return s.string()
}
arr := strings.Split(s.string(), variable)
for i := 0; i < len(arr); i++ {
str := arr[i] + s.replacer.Bind(uint(i+1))
if i >= len(arr)-1 {
str = arr[i]
}
buf.WriteString(str)
}
return buf.String()
}
// String :
func (s *Stmt) String() string {
buf := new(bytes.Buffer)
arr := strings.Split(s.string(), variable)
for i, aa := range s.arguments {
str := arr[i] + s.replacer.Value(aa)
buf.WriteString(str)
}
buf.WriteString(arr[len(arr)-1])
return buf.String()
}
// Arguments :
func (s Stmt) Arguments() []interface{} {
return s.arguments
}