-
Notifications
You must be signed in to change notification settings - Fork 329
/
information_schema.go
268 lines (230 loc) · 6.76 KB
/
information_schema.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
package mssql
import (
"context"
"database/sql"
"fmt"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/errors"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/ddl"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/jmoiron/sqlx"
"github.com/modern-go/reflect2"
"github.com/spf13/cast"
)
type (
informationSchema struct {
conn *sqlx.DB
}
)
func InformationSchema(conn *sqlx.DB) *informationSchema {
return &informationSchema{
conn: conn,
}
}
func (i *informationSchema) TableLookup(ctx context.Context, table, dbname string) (*ddl.Table, error) {
var (
oneTable = i.columnSelect(dbname).Where(
// We don't need to check for DB since the values are already scoped there
exp.ParseIdentifier("TABLE_NAME").Eq(table),
)
)
if out, err := i.scanColumns(ctx, oneTable); err != nil {
return nil, err
} else if len(out) > 0 {
return out[0], nil
} else {
return nil, errors.NotFound("table does not exist")
}
}
func (i *informationSchema) columnSelect(dbname string) *goqu.SelectDataset {
return dialect.GOQU().Select(
"TABLE_NAME",
"COLUMN_NAME",
"IS_NULLABLE",
"DATA_TYPE",
"CHARACTER_MAXIMUM_LENGTH",
"NUMERIC_SCALE",
"DATETIME_PRECISION",
).
// @note this goqu.I is a cheat; try to figure out if we have something nicer available (same applies to lower code)
From(goqu.I(fmt.Sprintf("%s.information_schema.columns", dbname))).
Order(
exp.NewOrderedExpression(exp.ParseIdentifier("TABLE_SCHEMA"), exp.AscDir, exp.NoNullsSortType),
exp.NewOrderedExpression(exp.ParseIdentifier("ORDINAL_POSITION"), exp.AscDir, exp.NoNullsSortType),
)
}
func (i *informationSchema) scanColumns(ctx context.Context, sd *goqu.SelectDataset) (out []*ddl.Table, err error) {
var (
at int
has bool
n2p = make(map[string]int)
// https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.7/en/information-schema-statistics-table.html
aux = make([]struct {
Table string `db:"TABLE_NAME"`
Column string `db:"COLUMN_NAME"`
IsNullable string `db:"IS_NULLABLE"`
Type string `db:"DATA_TYPE"`
// We'll use these two to get numeric's precision and scale
Precision any `db:"DATETIME_PRECISION"`
Scale any `db:"NUMERIC_SCALE"`
// We'll use this one to get varchar length
MaxLength any `db:"CHARACTER_MAXIMUM_LENGTH"`
}, 0)
)
if err = ddl.Structs(ctx, i.conn, sd, &aux); err != nil {
return
}
out = make([]*ddl.Table, 0, 10)
for _, v := range aux {
if at, has = n2p[v.Table]; !has {
at = len(out)
n2p[v.Table] = at
out = append(out, &ddl.Table{Ident: v.Table})
}
if v.Type == "decimal" {
var (
p, s int
)
if !reflect2.IsNil(v.Precision) {
p = cast.ToInt(v.Precision)
}
if !reflect2.IsNil(v.Scale) {
s = cast.ToInt(v.Scale)
}
if p+s > 0 {
v.Type = fmt.Sprintf("%s(%d,%d)", v.Type, p, s)
}
}
if v.Type == "varchar" {
var (
l int
)
if !reflect2.IsNil(v.MaxLength) {
l = cast.ToInt(v.MaxLength)
}
v.Type = fmt.Sprintf("%s(%d)", v.Type, l)
}
out[at].Columns = append(out[at].Columns, &ddl.Column{
Ident: v.Column,
Type: &ddl.ColumnType{
Name: v.Type,
Null: v.IsNullable == "YES",
},
})
}
return
}
func (i *informationSchema) IndexLookup(ctx context.Context, index, table, dbname string) (*ddl.Index, error) {
var (
oneIndexOnly = i.indexSelect(dbname).Where(
// We don't need to check TABLE_SCHEMA because it's all scoped under there already
exp.ParseIdentifier("si.name").Eq(index),
exp.ParseIdentifier("t.name").Eq(table),
)
)
if out, err := i.scanIndexes(ctx, oneIndexOnly); err != nil {
return nil, err
} else if len(out) > 0 {
return out[0], nil
} else {
return nil, nil
}
}
func (i *informationSchema) indexSelect(dbname string) *goqu.SelectDataset {
x := dialect.GOQU().Select(
goqu.I("si.name").As("INDEX_NAME"),
goqu.I("t.name").As("TABLE_NAME"),
goqu.I("col.name").As("COLUMN_NAME"),
goqu.I("si.type_desc").As("INDEX_TYPE"),
// @todo
exp.NewAliasExpression(exp.NewLiteralExpression("''"), "EXPRESSION"),
exp.NewAliasExpression(exp.NewLiteralExpression("''"), "INDEX_COMMENT"),
).
From(
goqu.From(goqu.I(fmt.Sprintf("%s.sys.indexes", dbname))).As("si"),
).
Join(
goqu.I(fmt.Sprintf("%s.sys.index_columns", dbname)).As("ic"),
goqu.On(exp.NewLiteralExpression("si.object_id = ic.object_id and si.index_id = ic.index_id")),
).
Join(
goqu.I(fmt.Sprintf("%s.sys.columns", dbname)).As("col"),
goqu.On(exp.NewLiteralExpression("ic.object_id = col.object_id and ic.column_id = col.column_id")),
).
Join(
goqu.I(fmt.Sprintf("%s.sys.tables", dbname)).As("t"),
goqu.On(exp.NewLiteralExpression("si.object_id = t.object_id")),
).
Join(
goqu.I(fmt.Sprintf("%s.sys.objects", dbname)).As("so"),
goqu.On(exp.NewLiteralExpression("si.object_id=so.object_id")),
).
Join(
goqu.I(fmt.Sprintf("%s.sys.schemas", dbname)).As("sc"),
goqu.On(exp.NewLiteralExpression("so.schema_id=sc.schema_id")),
)
return x
}
func (i *informationSchema) scanIndexes(ctx context.Context, sd *goqu.SelectDataset) (out []*ddl.Index, err error) {
var (
at int
has bool
n2p = make(map[string]int)
// https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.7/en/information-schema-statistics-table.html
aux = make([]struct {
Name string `db:"INDEX_NAME"`
Table string `db:"TABLE_NAME"`
NonUnique bool `db:"NON_UNIQUE"`
Type string `db:"INDEX_TYPE"`
// @todo there's also a "COMMENT" column?
Comment string `db:"INDEX_COMMENT"`
Expression sql.NullString `db:"EXPRESSION"`
ColumnName sql.NullString `db:"COLUMN_NAME"`
ColumnSubPart sql.NullInt32 `db:"SUB_PART"`
ColumnCollation sql.NullString `db:"COLLATION"`
// stats
ColumnStatsCardinality int64 `db:"CARDINALITY"`
}, 0)
)
if err = ddl.Structs(ctx, i.conn, sd, &aux); err != nil {
return
}
out = make([]*ddl.Index, 0, 10)
// iterate over results (aux) and populate ddl.Index struct
for p, a := range aux {
if at, has = n2p[a.Name]; !has {
out = append(out, &ddl.Index{
Ident: a.Name,
TableIdent: a.Table,
Type: a.Type,
Comment: a.Comment,
Fields: make([]*ddl.IndexField, 0),
Unique: !a.NonUnique,
Predicate: "",
Meta: nil,
})
n2p[a.Name] = p
at = p
}
col := &ddl.IndexField{
Length: int(a.ColumnSubPart.Int32),
Statistics: &ddl.IndexFieldStatistics{
Cardinality: a.ColumnStatsCardinality,
},
}
switch a.ColumnCollation.String {
case "A":
col.Sort = dal.IndexFieldSortAsc
case "D":
col.Sort = dal.IndexFieldSortDesc
}
if a.Expression.Valid {
col.Expression = a.Expression.String
} else {
col.Column = a.ColumnName.String
}
out[at].Fields = append(out[at].Fields, col)
}
return
}