forked from turnkey-commerce/gendal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql.go
253 lines (218 loc) · 5.46 KB
/
mssql.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package loaders
import (
"strings"
_ "github.com/denisenkom/go-mssqldb"
"github.com/knq/snaker"
"github.com/turnkey-commerce/gendal/internal"
"github.com/turnkey-commerce/gendal/models"
)
func init() {
internal.SchemaLoaders["mssql"] = internal.TypeLoader{
MaskFunc: func() string { return "$%d" },
ProcessRelkind: MsRelkind,
Schema: MsSchema,
ParseType: MsParseType,
//EnumList: models.MsEnums,
//EnumValueList: models.MsEnumValues,
//ProcList: models.MsProcs,
//ProcParamList: models.MsProcParams,
TableList: MsTables,
ColumnList: models.MsTableColumns,
ForeignKeyList: models.MsTableForeignKeys,
IndexList: models.MsTableIndexes,
IndexColumnList: models.MsIndexColumns,
QueryColumnList: MsQueryColumns,
}
}
// MsSchema retrieves the name of the current schema.
func MsSchema(args *internal.ArgType) (string, error) {
var err error
// sql query
const sqlstr = `SELECT SCHEMA_NAME()`
var schema string
// run query
models.XOLog(sqlstr)
err = args.DB.QueryRow(sqlstr).Scan(&schema)
if err != nil {
return "", err
}
return schema, nil
}
// MsRelkind returns the postgres string representation for RelType.
func MsRelkind(relType internal.RelType) string {
var s string
switch relType {
case internal.Table:
s = "U"
case internal.View:
s = "V"
default:
panic("unsupported RelType")
}
return s
}
// MsParseType parse a mssql type into a Go type based on the column
// definition.
func MsParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) {
precision := 0
nilVal := "nil"
// extract precision
dt, precision, _ = args.ParsePrecision(dt)
var typ string
switch dt {
case "tinyint", "bit":
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
case "char", "varchar", "text", "nchar", "nvarchar", "ntext", "smallmoney", "money":
nilVal = `""`
typ = "string"
if nullable {
nilVal = "sql.NullString{}"
typ = "sql.NullString"
}
case "smallint":
nilVal = "0"
typ = "int16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "int":
nilVal = "0"
typ = args.Int32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bigint":
nilVal = "0"
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "smallserial":
nilVal = "0"
typ = "uint16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "serial":
nilVal = "0"
typ = args.Uint32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bigserial":
nilVal = "0"
typ = "uint64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "real":
nilVal = "0.0"
typ = "float32"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "numeric", "decimal":
nilVal = "0.0"
typ = "float64"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "binary", "varbinary":
typ = "[]byte"
case "datetime", "datetime2", "timestamp":
nilVal = "time.Time{}"
typ = "time.Time"
case "time with time zone", "time without time zone", "timestamp without time zone":
nilVal = "0"
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "interval":
typ = "*time.Duration"
default:
if strings.HasPrefix(dt, args.Schema+".") {
// in the same schema, so chop off
typ = snaker.SnakeToCamelIdentifier(dt[len(args.Schema)+1:])
nilVal = typ + "(0)"
} else {
typ = snaker.SnakeToCamelIdentifier(dt)
nilVal = typ + "{}"
}
}
return precision, nilVal, typ
}
// MsQueryColumns parses the query and generates a type for it.
func MsQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
var err error
// process inspect -- cannot have 'order by' in a CREATE VIEW
ins := []string{}
for _, l := range inspect {
if !strings.HasPrefix(strings.ToUpper(l), "ORDER BY ") {
ins = append(ins, l)
}
}
// create temporary view xoid
xoid := "_xo_" + internal.GenRandomID()
viewq := `CREATE VIEW ` + xoid + ` AS ` + strings.Join(ins, "\n")
models.XOLog(viewq)
_, err = args.DB.Exec(viewq)
if err != nil {
return nil, err
}
// load columns
cols, err := models.MsTableColumns(args.DB, args.Schema, xoid)
// drop inspect view
dropq := `DROP VIEW ` + xoid
models.XOLog(dropq)
_, _ = args.DB.Exec(dropq)
// load column information
return cols, err
}
// MsTables returns the MsSQL tables with the manual PK information added.
// ManualPk is true when the table's primary key is not an identity.
func MsTables(db models.XODB, schema string, relkind string) ([]*models.Table, error) {
var err error
// get the tables
rows, err := models.MsTables(db, schema, relkind)
if err != nil {
return nil, err
}
// get the tables that have Identity included
identities, err := models.MsIdentities(db, schema)
if err != nil {
// Set it to an empty set on error.
identities = []*models.MsIdentity{}
}
// Add information about manual FK.
var tables []*models.Table
for _, row := range rows {
manualPk := true
// Look for a match in the table name where it contains the identity
for _, identity := range identities {
if identity.TableName == row.TableName {
manualPk = false
}
}
tables = append(tables, &models.Table{
TableName: row.TableName,
Type: row.Type,
ManualPk: manualPk,
})
}
return tables, nil
}