-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
218 lines (198 loc) · 6.95 KB
/
gorm.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package logger
import (
"database/sql/driver"
"fmt"
"reflect"
"regexp"
"strconv"
"time"
"unicode"
)
type GORMLogger struct {
Level string
LogTag string
}
func (g *GORMLogger) Print(value ...interface{}) {
if value[0] == "sql" {
newErr := GenericError(nil)
newErr.Labels = make(map[string]string)
data := GetSQLString(newErr.Labels, value...)
newErr.LogTag = g.LogTag
newErr.LogLevel = g.Level
newErr.Query = data[1].(string)
newErr.QueryTimingString = data[0].(string)
newErr.Message = data[2].(string)
newErr.Hint = value[3].(string)
newErr.Log()
}
}
func isPrintable(s string) bool {
for _, r := range s {
if !unicode.IsPrint(r) {
return false
}
}
return true
}
func GetSQLString(labels map[string]string, values ...interface{}) (messages []interface{}) {
if len(values) > 1 {
var (
sql string
// formattedValues []string
level = values[0]
)
messages = []interface{}{}
if level == "sql" {
// duration
messages = append(messages, fmt.Sprintf("%.2fms", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
// sql
for index, value := range values[4].([]interface{}) {
indirectValue := reflect.Indirect(reflect.ValueOf(value))
if indirectValue.IsValid() {
value = indirectValue.Interface()
if t, ok := value.(time.Time); ok {
if t.IsZero() {
labels[strconv.Itoa(index)] = fmt.Sprintf("'%v'", "0000-00-00 00:00:00")
// formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00"))
} else {
labels[strconv.Itoa(index)] = fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))
// formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
}
} else if b, ok := value.([]byte); ok {
if str := string(b); isPrintable(str) {
labels[strconv.Itoa(index)] = str
// formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
} else {
labels[strconv.Itoa(index)] = "<binary>"
// formattedValues = append(formattedValues, "'<binary>'")
}
} else if r, ok := value.(driver.Valuer); ok {
if value, err := r.Value(); err == nil && value != nil {
labels[strconv.Itoa(index)] = fmt.Sprintf("'%v'", value)
// formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
} else {
labels[strconv.Itoa(index)] = "NULL"
// formattedValues = append(formattedValues, "NULL")
}
} else {
switch value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
// formattedValues = append(formattedValues, fmt.Sprintf("%v", value))
labels[strconv.Itoa(index)] = fmt.Sprintf("'%v'", value)
default:
labels[strconv.Itoa(index)] = fmt.Sprintf("'%v'", value)
// formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
}
}
} else {
labels[strconv.Itoa(index)] = "NULL"
// formattedValues = append(formattedValues, "NULL")
}
}
// // differentiate between $n placeholders or else treat like ?
// if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
// sql = values[3].(string)
// for index, value := range labels {
// placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
// sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
// }
// } else {
// formattedValuesLength := len(labels)
// for index, value := range sqlRegexp.Split(values[3].(string), -1) {
// sql += value
// if index < formattedValuesLength {
// sql += formattedValues[index]
// }
// }
// }
messages = append(messages, sql)
messages = append(messages, fmt.Sprintf("%v ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned"))
}
}
return
}
func ParseGORM(values ...interface{}) (messages []interface{}) {
if len(values) > 1 {
var (
sql string
formattedValues []string
level = values[0]
currentTime = "\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m"
source = fmt.Sprintf("\033[35m(%v)\033[0m", values[1])
)
messages = []interface{}{source, currentTime}
if len(values) == 2 {
//remove the line break
currentTime = currentTime[1:]
//remove the brackets
source = fmt.Sprintf("\033[35m%v\033[0m", values[1])
messages = []interface{}{currentTime, source}
}
if level == "sql" {
// duration
messages = append(messages, fmt.Sprintf(" \033[36;1m[%.2fms]\033[0m ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
// sql
for _, value := range values[4].([]interface{}) {
indirectValue := reflect.Indirect(reflect.ValueOf(value))
if indirectValue.IsValid() {
value = indirectValue.Interface()
if t, ok := value.(time.Time); ok {
if t.IsZero() {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00"))
} else {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
}
} else if b, ok := value.([]byte); ok {
if str := string(b); isPrintable(str) {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
} else {
formattedValues = append(formattedValues, "'<binary>'")
}
} else if r, ok := value.(driver.Valuer); ok {
if value, err := r.Value(); err == nil && value != nil {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
} else {
formattedValues = append(formattedValues, "NULL")
}
} else {
switch value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
formattedValues = append(formattedValues, fmt.Sprintf("%v", value))
default:
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
}
}
} else {
formattedValues = append(formattedValues, "NULL")
}
}
// differentiate between $n placeholders or else treat like ?
if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
sql = values[3].(string)
for index, value := range formattedValues {
placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
}
} else {
formattedValuesLength := len(formattedValues)
for index, value := range sqlRegexp.Split(values[3].(string), -1) {
sql += value
if index < formattedValuesLength {
sql += formattedValues[index]
}
}
}
messages = append(messages, sql)
messages = append(messages, fmt.Sprintf(" \n\033[36;31m[%v]\033[0m ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned "))
} else {
messages = append(messages, "\033[31;1m")
messages = append(messages, values[2:]...)
messages = append(messages, "\033[0m")
}
}
return
}
var (
sqlRegexp = regexp.MustCompile(`\?`)
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
)