2020-02-23 07:39:26 +03:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
2020-02-23 08:22:08 +03:00
|
|
|
"reflect"
|
2020-02-23 07:39:26 +03:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
func isPrintable(s []byte) bool {
|
|
|
|
for _, r := range s {
|
|
|
|
if !unicode.IsPrint(rune(r)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:22:08 +03:00
|
|
|
var convertableTypes = []reflect.Type{reflect.TypeOf(time.Time{}), reflect.TypeOf(false), reflect.TypeOf([]byte{})}
|
|
|
|
|
2020-02-23 07:39:26 +03:00
|
|
|
func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, vars ...interface{}) string {
|
2020-02-23 08:22:08 +03:00
|
|
|
var convertParams func(interface{}, int)
|
2020-02-23 07:39:26 +03:00
|
|
|
|
2020-02-23 08:22:08 +03:00
|
|
|
convertParams = func(v interface{}, idx int) {
|
2020-02-23 07:39:26 +03:00
|
|
|
switch v := v.(type) {
|
|
|
|
case bool:
|
|
|
|
vars[idx] = fmt.Sprint(v)
|
|
|
|
case time.Time:
|
2020-02-23 14:41:29 +03:00
|
|
|
if v.IsZero() {
|
|
|
|
vars[idx] = escaper + "0000-00-00 00:00:00" + escaper
|
|
|
|
} else {
|
2020-03-12 03:39:42 +03:00
|
|
|
vars[idx] = escaper + v.Format("2006-01-02 15:04:05.999") + escaper
|
2020-02-23 14:41:29 +03:00
|
|
|
}
|
2020-02-23 07:39:26 +03:00
|
|
|
case []byte:
|
|
|
|
if isPrintable(v) {
|
|
|
|
vars[idx] = escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper
|
|
|
|
} else {
|
|
|
|
vars[idx] = escaper + "<binary>" + escaper
|
|
|
|
}
|
|
|
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
|
|
|
vars[idx] = fmt.Sprintf("%d", v)
|
|
|
|
case float64, float32:
|
|
|
|
vars[idx] = fmt.Sprintf("%.6f", v)
|
|
|
|
case string:
|
|
|
|
vars[idx] = escaper + strings.Replace(v, escaper, "\\"+escaper, -1) + escaper
|
|
|
|
default:
|
|
|
|
if v == nil {
|
|
|
|
vars[idx] = "NULL"
|
|
|
|
} else {
|
2020-04-15 18:58:26 +03:00
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
|
2020-02-23 14:41:29 +03:00
|
|
|
if !rv.IsValid() {
|
|
|
|
vars[idx] = "NULL"
|
2020-04-15 18:58:26 +03:00
|
|
|
} else if rv.Kind() == reflect.Ptr && !rv.IsZero() {
|
|
|
|
convertParams(reflect.Indirect(rv).Interface(), idx)
|
|
|
|
} else {
|
|
|
|
for _, t := range convertableTypes {
|
|
|
|
if rv.Type().ConvertibleTo(t) {
|
|
|
|
convertParams(rv.Convert(t).Interface(), idx)
|
|
|
|
return
|
|
|
|
}
|
2020-02-23 08:22:08 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 18:58:26 +03:00
|
|
|
vars[idx] = escaper + strings.Replace(fmt.Sprint(v), escaper, "\\"+escaper, -1) + escaper
|
|
|
|
}
|
2020-02-23 07:39:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:22:08 +03:00
|
|
|
for idx, v := range vars {
|
|
|
|
if valuer, ok := v.(driver.Valuer); ok {
|
|
|
|
v, _ = valuer.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
convertParams(v, idx)
|
|
|
|
}
|
|
|
|
|
2020-02-23 07:39:26 +03:00
|
|
|
if numericPlaceholder == nil {
|
|
|
|
for _, v := range vars {
|
|
|
|
sql = strings.Replace(sql, "?", v.(string), 1)
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-23 08:22:08 +03:00
|
|
|
sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$")
|
2020-02-23 07:39:26 +03:00
|
|
|
for idx, v := range vars {
|
2020-02-24 03:51:35 +03:00
|
|
|
sql = strings.Replace(sql, "$"+strconv.Itoa(idx+1)+"$", v.(string), 1)
|
2020-02-23 07:39:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql
|
|
|
|
}
|