-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_mysql.go
57 lines (48 loc) · 1.35 KB
/
dialect_mysql.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
package zinc
import (
"database/sql"
"errors"
"reflect"
)
type mysqlDialect struct{}
func (d mysqlDialect) DriverName() string {
return "mysql"
}
func (d mysqlDialect) QuoteString(s string) string {
return quoteString(s, quoteBack)
}
func (d mysqlDialect) NamingString(s string) string {
return namingString(s, bindNamed)
}
func (d mysqlDialect) CompileNamedQuery(q string, _ *Options) (string, []string, error) {
return compileNamedQuery([]byte(q), bindQuestion)
}
func (d mysqlDialect) NewDest(ci *sql.ColumnType, _ *Options) any {
return reflect.New(ci.ScanType()).Interface()
}
func (d mysqlDialect) CoerceDest(ci *sql.ColumnType, scannedVal reflect.Value, toType reflect.Type, opts *Options) (reflect.Value, error) {
if toType == typAny {
if sliceContains(mysqlTextTypes, ci.DatabaseTypeName()) {
switch a := scannedVal.Interface().(type) {
case []byte:
s, ok := b2s(a, opts.TextCharset)
if !ok {
return reflect.Value{}, errors.New("failed to convert []byte to string")
}
return reflect.ValueOf(s), nil
case sql.RawBytes:
s, ok := b2s(a, opts.TextCharset)
if !ok {
return reflect.Value{}, errors.New("failed to convert sql.RawBytes to string")
}
return reflect.ValueOf(s), nil
}
}
}
return coerceDest(ci, scannedVal, toType, opts)
}
var mysqlTextTypes = []string{
"CHAR",
"VARCHAR",
"TEXT",
}