| 1 |
package logger |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql/driver" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"regexp" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
"unicode" |
| 12 |
|
| 13 |
"gorm.io/gorm/utils" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
tmFmtWithMS = "2006-01-02 15:04:05.999" |
| 18 |
tmFmtZero = "0000-00-00 00:00:00" |
| 19 |
nullStr = "NULL" |
| 20 |
) |
| 21 |
|
| 22 |
func isPrintable(s string) bool { |
| 23 |
for _, r := range s { |
| 24 |
if !unicode.IsPrint(r) { |
| 25 |
return false |
| 26 |
} |
| 27 |
} |
| 28 |
return true |
| 29 |
} |
| 30 |
|
| 31 |
// A list of Go types that should be converted to SQL primitives |
| 32 |
var convertibleTypes = []reflect.Type{reflect.TypeOf(time.Time{}), reflect.TypeOf(false), reflect.TypeOf([]byte{})} |
| 33 |
|
| 34 |
// RegEx matches only numeric values |
| 35 |
var numericPlaceholderRe = regexp.MustCompile(`\$\d+\$`) |
| 36 |
|
| 37 |
func isNumeric(k reflect.Kind) bool { |
| 38 |
switch k { |
| 39 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 40 |
return true |
| 41 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 42 |
return true |
| 43 |
case reflect.Float32, reflect.Float64: |
| 44 |
return true |
| 45 |
default: |
| 46 |
return false |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// ExplainSQL generate SQL string with given parameters, the generated SQL is expected to be used in logger, execute it might introduce a SQL injection vulnerability |
| 51 |
func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, avars ...interface{}) string { |
| 52 |
var ( |
| 53 |
convertParams func(interface{}, int) |
| 54 |
vars = make([]string, len(avars)) |
| 55 |
) |
| 56 |
|
| 57 |
convertParams = func(v interface{}, idx int) { |
| 58 |
switch v := v.(type) { |
| 59 |
case bool: |
| 60 |
vars[idx] = strconv.FormatBool(v) |
| 61 |
case time.Time: |
| 62 |
if v.IsZero() { |
| 63 |
vars[idx] = escaper + tmFmtZero + escaper |
| 64 |
} else { |
| 65 |
vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper |
| 66 |
} |
| 67 |
case *time.Time: |
| 68 |
if v != nil { |
| 69 |
if v.IsZero() { |
| 70 |
vars[idx] = escaper + tmFmtZero + escaper |
| 71 |
} else { |
| 72 |
vars[idx] = escaper + v.Format(tmFmtWithMS) + escaper |
| 73 |
} |
| 74 |
} else { |
| 75 |
vars[idx] = nullStr |
| 76 |
} |
| 77 |
case driver.Valuer: |
| 78 |
reflectValue := reflect.ValueOf(v) |
| 79 |
if v != nil && reflectValue.IsValid() && ((reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) || reflectValue.Kind() != reflect.Ptr) { |
| 80 |
r, _ := v.Value() |
| 81 |
convertParams(r, idx) |
| 82 |
} else { |
| 83 |
vars[idx] = nullStr |
| 84 |
} |
| 85 |
case fmt.Stringer: |
| 86 |
reflectValue := reflect.ValueOf(v) |
| 87 |
switch reflectValue.Kind() { |
| 88 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 89 |
vars[idx] = fmt.Sprintf("%d", reflectValue.Interface()) |
| 90 |
case reflect.Float32, reflect.Float64: |
| 91 |
vars[idx] = fmt.Sprintf("%.6f", reflectValue.Interface()) |
| 92 |
case reflect.Bool: |
| 93 |
vars[idx] = fmt.Sprintf("%t", reflectValue.Interface()) |
| 94 |
case reflect.String: |
| 95 |
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprintf("%v", v), escaper, escaper+escaper) + escaper |
| 96 |
default: |
| 97 |
if v != nil && reflectValue.IsValid() && ((reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil()) || reflectValue.Kind() != reflect.Ptr) { |
| 98 |
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprintf("%v", v), escaper, escaper+escaper) + escaper |
| 99 |
} else { |
| 100 |
vars[idx] = nullStr |
| 101 |
} |
| 102 |
} |
| 103 |
case []byte: |
| 104 |
if s := string(v); isPrintable(s) { |
| 105 |
vars[idx] = escaper + strings.ReplaceAll(s, escaper, escaper+escaper) + escaper |
| 106 |
} else { |
| 107 |
vars[idx] = escaper + "<binary>" + escaper |
| 108 |
} |
| 109 |
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: |
| 110 |
vars[idx] = utils.ToString(v) |
| 111 |
case float32: |
| 112 |
vars[idx] = strconv.FormatFloat(float64(v), 'f', -1, 32) |
| 113 |
case float64: |
| 114 |
vars[idx] = strconv.FormatFloat(v, 'f', -1, 64) |
| 115 |
case string: |
| 116 |
vars[idx] = escaper + strings.ReplaceAll(v, escaper, escaper+escaper) + escaper |
| 117 |
default: |
| 118 |
rv := reflect.ValueOf(v) |
| 119 |
if v == nil || !rv.IsValid() || rv.Kind() == reflect.Ptr && rv.IsNil() { |
| 120 |
vars[idx] = nullStr |
| 121 |
} else if valuer, ok := v.(driver.Valuer); ok { |
| 122 |
v, _ = valuer.Value() |
| 123 |
convertParams(v, idx) |
| 124 |
} else if rv.Kind() == reflect.Ptr && !rv.IsZero() { |
| 125 |
convertParams(reflect.Indirect(rv).Interface(), idx) |
| 126 |
} else if isNumeric(rv.Kind()) { |
| 127 |
if rv.CanInt() || rv.CanUint() { |
| 128 |
vars[idx] = fmt.Sprintf("%d", rv.Interface()) |
| 129 |
} else { |
| 130 |
vars[idx] = fmt.Sprintf("%.6f", rv.Interface()) |
| 131 |
} |
| 132 |
} else { |
| 133 |
for _, t := range convertibleTypes { |
| 134 |
if rv.Type().ConvertibleTo(t) { |
| 135 |
convertParams(rv.Convert(t).Interface(), idx) |
| 136 |
return |
| 137 |
} |
| 138 |
} |
| 139 |
vars[idx] = escaper + strings.ReplaceAll(fmt.Sprint(v), escaper, escaper+escaper) + escaper |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
for idx, v := range avars { |
| 145 |
convertParams(v, idx) |
| 146 |
} |
| 147 |
|
| 148 |
if numericPlaceholder == nil { |
| 149 |
var idx int |
| 150 |
var newSQL strings.Builder |
| 151 |
|
| 152 |
for _, v := range []byte(sql) { |
| 153 |
if v == '?' { |
| 154 |
if len(vars) > idx { |
| 155 |
newSQL.WriteString(vars[idx]) |
| 156 |
idx++ |
| 157 |
continue |
| 158 |
} |
| 159 |
} |
| 160 |
newSQL.WriteByte(v) |
| 161 |
} |
| 162 |
|
| 163 |
sql = newSQL.String() |
| 164 |
} else { |
| 165 |
sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$") |
| 166 |
|
| 167 |
sql = numericPlaceholderRe.ReplaceAllStringFunc(sql, func(v string) string { |
| 168 |
num := v[1 : len(v)-1] |
| 169 |
n, _ := strconv.Atoi(num) |
| 170 |
|
| 171 |
// position var start from 1 ($1, $2) |
| 172 |
n -= 1 |
| 173 |
if n >= 0 && n <= len(vars)-1 { |
| 174 |
return vars[n] |
| 175 |
} |
| 176 |
return v |
| 177 |
}) |
| 178 |
} |
| 179 |
|
| 180 |
return sql |
| 181 |
} |