/
mssql.go
467 lines (431 loc) · 12.1 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package mssql
import (
"database/sql"
"fmt"
"regexp"
"strings"
"github.com/k1LoW/tbls/schema"
"github.com/pkg/errors"
)
var defaultSchemaName = "dbo"
var typeFk = schema.FOREIGN_KEY
var typeCheck = "CHECK"
var reSystemNamed = regexp.MustCompile(`_[^_]+$`)
// Mssql struct
type Mssql struct {
db *sql.DB
}
type relationLink struct {
table string
columns []string
parentTable string
parentColumns []string
}
// NewMssql ...
func NewMssql(db *sql.DB) *Mssql {
return &Mssql{
db: db,
}
}
func (m *Mssql) Analyze(s *schema.Schema) error {
// tables
tableRows, err := m.db.Query(`
SELECT schema_name(schema_id) AS table_schema, name, object_id, type
FROM sys.objects
WHERE type IN ('U', 'V') ORDER BY object_id
`)
defer tableRows.Close()
if err != nil {
return errors.WithStack(err)
}
tables := []*schema.Table{}
links := []relationLink{}
for tableRows.Next() {
var (
tableSchema string
tableName string
tableOid string
tableType string
)
err := tableRows.Scan(&tableSchema, &tableName, &tableOid, &tableType)
if err != nil {
return errors.WithStack(err)
}
tableType = convertTableType(tableType)
name := tableName
if tableSchema != defaultSchemaName {
name = fmt.Sprintf("%s.%s", tableSchema, tableName)
}
table := &schema.Table{
Name: name,
Type: tableType,
}
// view definition
if tableType == "VIEW" {
viewDefRows, err := m.db.Query(`
SELECT definition FROM sys.sql_modules WHERE object_id = $1
`, tableOid)
defer viewDefRows.Close()
if err != nil {
return errors.WithStack(err)
}
for viewDefRows.Next() {
var tableDef sql.NullString
err := viewDefRows.Scan(&tableDef)
if err != nil {
return errors.WithStack(err)
}
table.Def = tableDef.String
}
}
// columns
columnRows, err := m.db.Query(`
SELECT
c.name,
t.name AS type,
c.max_length,
c.is_nullable,
c.is_identity,
object_definition(c.default_object_id)
FROM sys.columns AS c
LEFT JOIN sys.types AS t ON c.system_type_id = t.system_type_id
WHERE c.object_id = $1
ORDER BY c.column_id
`, tableOid)
defer columnRows.Close()
if err != nil {
return errors.WithStack(err)
}
columns := []*schema.Column{}
for columnRows.Next() {
var (
columnName string
dataType string
maxLength int
isNullable bool
isIdentity bool
columnDefault sql.NullString
)
err = columnRows.Scan(&columnName, &dataType, &maxLength, &isNullable, &isIdentity, &columnDefault)
if err != nil {
return errors.WithStack(err)
}
column := &schema.Column{
Name: columnName,
Type: convertColmunType(dataType, maxLength),
Nullable: isNullable,
Default: columnDefault,
}
columns = append(columns, column)
}
table.Columns = columns
// constraints
constraints := []*schema.Constraint{}
/// key constraints
keyRows, err := m.db.Query(`
SELECT
c.name,
i.type_desc,
i.is_unique,
i.is_primary_key,
i.is_unique_constraint,
STRING_AGG(COL_NAME(ic.object_id, ic.column_id), ', ') WITHIN GROUP ( ORDER BY ic.key_ordinal ),
c.is_system_named
FROM sys.key_constraints AS c
LEFT JOIN sys.indexes AS i ON i.object_id = c.parent_object_id AND i.index_id = c.unique_index_id
INNER JOIN sys.index_columns AS ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
WHERE i.object_id = object_id($1)
GROUP BY c.name, i.index_id, i.type_desc, i.is_unique, i.is_primary_key, i.is_unique_constraint, c.is_system_named
ORDER BY i.index_id
`, fmt.Sprintf("%s.%s", tableSchema, tableName))
defer keyRows.Close()
if err != nil {
return errors.WithStack(err)
}
for keyRows.Next() {
var (
indexName string
indexClusterType string
indexIsUnique bool
indexIsPrimaryKey bool
indexIsUniqueConstraint bool
indexColumnName sql.NullString
indexIsSystemNamed bool
)
err = keyRows.Scan(&indexName, &indexClusterType, &indexIsUnique, &indexIsPrimaryKey, &indexIsUniqueConstraint, &indexColumnName, &indexIsSystemNamed)
if err != nil {
return errors.WithStack(err)
}
indexType := "-"
indexDef := []string{
indexClusterType,
}
if indexIsUnique {
indexDef = append(indexDef, "unique")
}
if indexIsPrimaryKey {
indexType = "PRIMARY KEY"
indexDef = append(indexDef, "part of a PRIMARY KEY constraint")
}
if indexIsUniqueConstraint {
indexType = "UNIQUE"
indexDef = append(indexDef, "part of a UNIQUE constraint")
}
indexDef = append(indexDef, fmt.Sprintf("[ %s ]", indexColumnName.String))
constraint := &schema.Constraint{
Name: convertSystemNamed(indexName, indexIsSystemNamed),
Type: indexType,
Def: strings.Join(indexDef, ", "),
Table: &table.Name,
Columns: strings.Split(indexColumnName.String, ", "),
}
constraints = append(constraints, constraint)
}
/// foreign_keys
fkRows, err := m.db.Query(`
SELECT
f.name AS f_name,
object_name(f.parent_object_id) AS table_name,
object_name(f.referenced_object_id) AS parent_table_name,
STRING_AGG(COL_NAME(fc.parent_object_id, fc.parent_column_id), ', ') AS column_names,
STRING_AGG(COL_NAME(fc.referenced_object_id, fc.referenced_column_id), ', ') AS parent_column_names,
update_referential_action_desc,
delete_referential_action_desc,
f.is_system_named
FROM sys.foreign_keys AS f
LEFT JOIN sys.foreign_key_columns AS fc ON f.object_id = fc.constraint_object_id
WHERE f.parent_object_id = object_id($1)
GROUP BY f.name, f.parent_object_id, f.referenced_object_id, delete_referential_action_desc, update_referential_action_desc, f.is_system_named
`, fmt.Sprintf("%s.%s", tableSchema, tableName))
defer fkRows.Close()
if err != nil {
return errors.WithStack(err)
}
for fkRows.Next() {
var (
fkName string
fkTableName string
fkParentTableName string
fkColumnNames string
fkParentColumnNames string
fkUpdateAction string
fkDeleteAction string
fkIsSystemNamed bool
)
err = fkRows.Scan(&fkName, &fkTableName, &fkParentTableName, &fkColumnNames, &fkParentColumnNames, &fkUpdateAction, &fkDeleteAction, &fkIsSystemNamed)
if err != nil {
return errors.WithStack(err)
}
fkDef := fmt.Sprintf("FOREIGN KEY(%s) REFERENCES %s(%s) ON UPDATE %s ON DELETE %s", fkColumnNames, fkParentTableName, fkParentColumnNames, fkUpdateAction, fkDeleteAction) // #nosec
constraint := &schema.Constraint{
Name: convertSystemNamed(fkName, fkIsSystemNamed),
Type: typeFk,
Def: fkDef,
Table: &table.Name,
Columns: strings.Split(fkColumnNames, ", "),
ReferenceTable: &fkParentTableName,
ReferenceColumns: strings.Split(fkParentColumnNames, ", "),
}
links = append(links, relationLink{
table: table.Name,
columns: strings.Split(fkColumnNames, ", "),
parentTable: fkParentTableName,
parentColumns: strings.Split(fkParentColumnNames, ", "),
})
constraints = append(constraints, constraint)
}
/// check_constraints
checkRows, err := m.db.Query(`
SELECT name, definition, is_system_named
FROM sys.check_constraints
WHERE parent_object_id = object_id($1)
`, fmt.Sprintf("%s.%s", tableSchema, tableName))
defer checkRows.Close()
if err != nil {
return errors.WithStack(err)
}
for checkRows.Next() {
var (
checkName string
checkDef string
checkIsSystemNamed bool
)
err = checkRows.Scan(&checkName, &checkDef, &checkIsSystemNamed)
if err != nil {
return errors.WithStack(err)
}
constraint := &schema.Constraint{
Name: convertSystemNamed(checkName, checkIsSystemNamed),
Type: typeCheck,
Def: fmt.Sprintf("CHECK%s", checkDef),
Table: &table.Name,
}
constraints = append(constraints, constraint)
}
table.Constraints = constraints
// triggers
triggerRows, err := m.db.Query(`
SELECT name, definition
FROM sys.triggers AS t
INNER JOIN sys.sql_modules AS sm
ON sm.object_id = t.object_id
WHERE type = 'TR'
AND parent_id = object_id($1)
`, fmt.Sprintf("%s.%s", tableSchema, tableName))
defer triggerRows.Close()
if err != nil {
return errors.WithStack(err)
}
triggers := []*schema.Trigger{}
for triggerRows.Next() {
var (
triggerName string
triggerDef string
)
err = triggerRows.Scan(&triggerName, &triggerDef)
if err != nil {
return errors.WithStack(err)
}
trigger := &schema.Trigger{
Name: triggerName,
Def: triggerDef,
}
triggers = append(triggers, trigger)
}
table.Triggers = triggers
// indexes
indexRows, err := m.db.Query(`
SELECT
i.name AS index_name,
i.type_desc,
i.is_unique,
i.is_primary_key,
i.is_unique_constraint,
STRING_AGG(COL_NAME(ic.object_id, ic.column_id), ', ') WITHIN GROUP ( ORDER BY ic.key_ordinal ),
c.is_system_named
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
LEFT JOIN sys.key_constraints AS c
ON i.object_id = c.parent_object_id AND i.index_id = c.unique_index_id
WHERE i.object_id = object_id($1)
GROUP BY i.name, i.index_id, i.type_desc, i.is_unique, i.is_primary_key, i.is_unique_constraint, c.is_system_named
ORDER BY i.index_id
`, fmt.Sprintf("%s.%s", tableSchema, tableName))
defer indexRows.Close()
if err != nil {
return errors.WithStack(err)
}
indexes := []*schema.Index{}
for indexRows.Next() {
var (
indexName string
indexType string
indexIsUnique bool
indexIsPrimaryKey bool
indexIsUniqueConstraint bool
indexColumnName sql.NullString
indexIsSytemNamed sql.NullBool
)
err = indexRows.Scan(&indexName, &indexType, &indexIsUnique, &indexIsPrimaryKey, &indexIsUniqueConstraint, &indexColumnName, &indexIsSytemNamed)
if err != nil {
return errors.WithStack(err)
}
indexDef := []string{
indexType,
}
if indexIsUnique {
indexDef = append(indexDef, "unique")
}
if indexIsPrimaryKey {
indexDef = append(indexDef, "part of a PRIMARY KEY constraint")
}
if indexIsUniqueConstraint {
indexDef = append(indexDef, "part of a UNIQUE constraint")
}
indexDef = append(indexDef, fmt.Sprintf("[ %s ]", indexColumnName.String))
index := &schema.Index{
Name: convertSystemNamed(indexName, indexIsSytemNamed.Bool),
Def: strings.Join(indexDef, ", "),
Table: &table.Name,
Columns: strings.Split(indexColumnName.String, ", "),
}
indexes = append(indexes, index)
}
table.Indexes = indexes
tables = append(tables, table)
}
s.Tables = tables
// relations
relations := []*schema.Relation{}
for _, l := range links {
r := &schema.Relation{}
table, err := s.FindTableByName(l.table)
if err != nil {
return err
}
r.Table = table
for _, c := range l.columns {
column, err := table.FindColumnByName(c)
if err != nil {
return err
}
r.Columns = append(r.Columns, column)
column.ParentRelations = append(column.ParentRelations, r)
}
parentTable, err := s.FindTableByName(l.parentTable)
if err != nil {
return err
}
r.ParentTable = parentTable
for _, c := range l.parentColumns {
column, err := parentTable.FindColumnByName(c)
if err != nil {
return err
}
r.ParentColumns = append(r.ParentColumns, column)
column.ChildRelations = append(column.ChildRelations, r)
}
relations = append(relations, r)
}
s.Relations = relations
return nil
}
func (m *Mssql) Info() (*schema.Driver, error) {
var v string
row := m.db.QueryRow(`SELECT @@VERSION`)
err := row.Scan(&v)
if err != nil {
return nil, err
}
d := &schema.Driver{
Name: "mssql",
DatabaseVersion: v,
}
return d, nil
}
func convertTableType(t string) string {
switch strings.Trim(t, " ") {
case "U":
return "BASIC TABLE"
case "V":
return "VIEW"
default:
return t
}
}
func convertColmunType(t string, maxLength int) string {
switch t {
case "varchar":
return fmt.Sprintf("varchar(%d)", maxLength)
default:
return t
}
}
func convertSystemNamed(name string, isSytemNamed bool) string {
if isSytemNamed {
return reSystemNamed.ReplaceAllString(name, "*")
}
return name
}