forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_sqlserver.go
178 lines (155 loc) · 4.61 KB
/
dialect_sqlserver.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
// 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 Microsoft SQL Server databases.
// Use gorp.SqlServerDialect{"2005"} for legacy datatypes.
// Tested with driver: github.com/denisenkom/go-mssqldb
type SqlServerDialect struct {
// If set to "2005" legacy datatypes will be used
Version string
}
// Type returns the dialect type
func (d SqlServerDialect) Type() DialectType {
return SQLServer
}
// QuerySuffix adds a Suffix to any query, usually ";"
func (d SqlServerDialect) 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 SQL Server []byte maps to varbinary
func (d SqlServerDialect) ToSqlType(val reflect.Type, opts ColumnOptions) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), opts)
case reflect.Bool:
return "bit"
case reflect.Int8:
return "tinyint"
case reflect.Uint8:
return "smallint"
case reflect.Int16:
return "smallint"
case reflect.Uint16:
return "int"
case reflect.Int, reflect.Int32:
return "int"
case reflect.Uint, reflect.Uint32:
return "bigint"
case reflect.Int64:
return "bigint"
case reflect.Uint64:
return "numeric(20,0)"
case reflect.Float32:
return "float(24)"
case reflect.Float64:
return "float(53)"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "varbinary"
}
}
switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "float(53)"
case "NullBool":
return "bit"
case "NullTime", "Time":
if d.Version == "2005" {
return "datetime"
}
return "datetime2"
}
if opts.MaxSize < 1 {
if d.Version == "2005" {
return fmt.Sprintf("nvarchar(%d)", 255)
}
return "nvarchar(max)"
}
return fmt.Sprintf("nvarchar(%d)", opts.MaxSize)
}
// Returns "identity(0,1)"
func (d SqlServerDialect) AutoIncrStr() string {
return "identity(0,1)"
}
// Returns empty string since SQL Server removes autoincrement columns from INSERT statements
func (d SqlServerDialect) AutoIncrBindValue() string {
return ""
}
// Returns empty string
func (d SqlServerDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return ""
}
// Returns ";"
func (d SqlServerDialect) CreateTableSuffix() string {
return ";"
}
// Returns empty string
func (d SqlServerDialect) CreateIndexSuffix() string {
return ""
}
// Returns empty string
func (d SqlServerDialect) DropIndexSuffix() string {
return ""
}
// Returns "truncate table"
func (d SqlServerDialect) TruncateClause() string {
return "truncate table"
}
// Returns waitfor delay 'time'
func (d SqlServerDialect) SleepClause(s time.Duration) string {
return fmt.Sprintf("waitfor delay '%d:%02d:%02d.%03d'",
int(s.Hours()),
int(s.Minutes())%60,
int(s.Seconds())%60,
int(s.Milliseconds())%1000)
}
// Returns "?"
func (d SqlServerDialect) BindVar(i int) string {
return "?"
}
// Handles auto-increment values for SQL Server
func (d SqlServerDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
}
// Returns quoted field name with SQL Server-style escaping
func (d SqlServerDialect) QuoteField(f string) string {
return "[" + strings.Replace(f, "]", "]]", -1) + "]"
}
// Returns quoted table name with schema support
func (d SqlServerDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}
return d.QuoteField(schema) + "." + d.QuoteField(table)
}
// Returns SQL Server-specific schema existence check
func (d SqlServerDialect) IfSchemaNotExists(command, schema string) string {
return fmt.Sprintf("if schema_id(N'%s') is null %s", schema, command)
}
// Returns SQL Server-specific table existence check
func (d SqlServerDialect) IfTableExists(command, schema, table string) string {
var schema_clause string
if strings.TrimSpace(schema) != "" {
schema_clause = fmt.Sprintf("%s.", d.QuoteField(schema))
}
return fmt.Sprintf("if object_id('%s%s') is not null %s",
schema_clause, d.QuoteField(table), command)
}
// Returns SQL Server-specific table non-existence check
func (d SqlServerDialect) IfTableNotExists(command, schema, table string) string {
var schema_clause string
if strings.TrimSpace(schema) != "" {
schema_clause = fmt.Sprintf("%s.", schema)
}
return fmt.Sprintf("if object_id('%s%s') is null %s",
schema_clause, table, command)
}