forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_mysql.go
210 lines (178 loc) · 5.09 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
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
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp
import (
"fmt"
"reflect"
"strings"
"time"
)
// Implementation of Dialect for MySQL databases.
type MySQLDialect struct {
// Engine is the storage engine to use "InnoDB" vs "MyISAM" for example
Engine string
// Encoding is the character encoding to use for created tables
Encoding string
}
// Type returns the dialect type
func (d MySQLDialect) Type() DialectType {
return MySQL
}
// QuerySuffix adds a Suffix to any query, usually ";"
func (d MySQLDialect) QuerySuffix() string { return ";" }
// ToSqlType returns the SQL column type to use when creating a
// table of the given Go Type. maxsize can be used to switch based on
// size. For example, in MySQL []byte could map to BLOB, MEDIUMBLOB,
// or LONGBLOB depending on the maxsize
func (d MySQLDialect) ToSqlType(val reflect.Type, opts ColumnOptions) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), opts)
case reflect.Bool:
return "boolean"
case reflect.Int8:
return "tinyint"
case reflect.Uint8:
return "tinyint unsigned"
case reflect.Int16:
return "smallint"
case reflect.Uint16:
return "smallint unsigned"
case reflect.Int, reflect.Int32:
return "int"
case reflect.Uint, reflect.Uint32:
return "int unsigned"
case reflect.Int64:
return "bigint"
case reflect.Uint64:
return "bigint unsigned"
case reflect.Float64, reflect.Float32:
return "double"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "mediumblob"
}
}
switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "double"
case "NullBool":
return "tinyint"
case "Time":
return "datetime"
}
if opts.MaxSize < 1 {
opts.MaxSize = 255
}
/* == About varchar(N) ==
* N is number of characters.
* A varchar column can store up to 65535 bytes.
* Remember that 1 character is 3 bytes in utf-8 charset.
* Also remember that each row can store up to 65535 bytes,
* and you have some overheads, so it's not possible for a
* varchar column to have 65535/3 characters really.
* So it would be better to use 'text' type in stead of
* large varchar type.
*/
if opts.MaxSize < 256 {
return fmt.Sprintf("varchar(%d)", opts.MaxSize)
} else {
return "text"
}
}
// Returns auto_increment
func (d MySQLDialect) AutoIncrStr() string {
return "auto_increment"
}
// Returns NULL
func (d MySQLDialect) AutoIncrBindValue() string {
return "null"
}
// Returns ""
func (d MySQLDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return ""
}
// Returns engine=%s charset=%s based on values stored on struct
func (d MySQLDialect) CreateTableSuffix() string {
if d.Engine == "" || d.Encoding == "" {
msg := "gorp - undefined"
if d.Engine == "" {
msg += " MySQLDialect.Engine"
}
if d.Engine == "" && d.Encoding == "" {
msg += ","
}
if d.Encoding == "" {
msg += " MySQLDialect.Encoding"
}
msg += ". Check that your MySQLDialect was correctly initialized when declared."
panic(msg)
}
return fmt.Sprintf(" engine=%s charset=%s", d.Engine, d.Encoding)
}
// Returns using
func (d MySQLDialect) CreateIndexSuffix() string {
return "using"
}
// Returns on
func (d MySQLDialect) DropIndexSuffix() string {
return "on"
}
// Returns truncate
func (d MySQLDialect) TruncateClause() string {
return "truncate"
}
// Returns sleep(s)
func (d MySQLDialect) SleepClause(s time.Duration) string {
return fmt.Sprintf("sleep(%f)", s.Seconds())
}
// Returns "?"
func (d MySQLDialect) BindVar(i int) string {
return "?"
}
// Returns "insert into %s %s values %s"
func (d MySQLDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
}
// Returns "`%s`"
func (d MySQLDialect) QuoteField(f string) string {
return "`" + f + "`"
}
// Returns "`%s`.`%s`"
func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}
return schema + "." + d.QuoteField(table)
}
// Returns "if not exists"
func (d MySQLDialect) IfSchemaNotExists(command, schema string) string {
return fmt.Sprintf("%s IF NOT EXISTS", command)
}
// Returns "if exists"
func (d MySQLDialect) IfTableExists(command, schema, table string) string {
return fmt.Sprintf("%s IF EXISTS", command)
}
// Returns "if not exists"
func (d MySQLDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s IF NOT EXISTS", command)
}
// Placeholder returns the placeholder for a column value
func (d MySQLDialect) Placeholder(i int) string {
return "?"
}
// SupportsCascade returns whether MySQL supports CASCADE in DROP TABLE
func (d MySQLDialect) SupportsCascade() bool {
return true
}
// SupportsMultipleSchema returns whether MySQL supports multiple schemas
func (d MySQLDialect) SupportsMultipleSchema() bool {
return true
}
// SupportsLastInsertId returns whether MySQL supports LastInsertId
func (d MySQLDialect) SupportsLastInsertId() bool {
return true
}