-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathsystem.sql.go
706 lines (676 loc) · 20.6 KB
/
system.sql.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: system.sql
package mysql_queries
import (
"context"
"database/sql"
"encoding/json"
"strings"
)
const getCustomFunctionsBySchemas = `-- name: GetCustomFunctionsBySchemas :many
SELECT
ROUTINE_NAME as function_name,
ROUTINE_SCHEMA as schema_name,
DTD_IDENTIFIER as return_data_type,
ROUTINE_DEFINITION as definition,
CASE WHEN IS_DETERMINISTIC = 'YES' THEN 1 ELSE 0 END as is_deterministic
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_TYPE = 'FUNCTION'
AND ROUTINE_SCHEMA in (/*SLICE:schemas*/?)
`
type GetCustomFunctionsBySchemasRow struct {
FunctionName string
SchemaName string
ReturnDataType string
Definition string
IsDeterministic int32
}
func (q *Queries) GetCustomFunctionsBySchemas(ctx context.Context, db DBTX, schemas []string) ([]*GetCustomFunctionsBySchemasRow, error) {
query := getCustomFunctionsBySchemas
var queryParams []interface{}
if len(schemas) > 0 {
for _, v := range schemas {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:schemas*/?", strings.Repeat(",?", len(schemas))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:schemas*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetCustomFunctionsBySchemasRow
for rows.Next() {
var i GetCustomFunctionsBySchemasRow
if err := rows.Scan(
&i.FunctionName,
&i.SchemaName,
&i.ReturnDataType,
&i.Definition,
&i.IsDeterministic,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCustomTriggersBySchemaAndTables = `-- name: GetCustomTriggersBySchemaAndTables :many
SELECT
TRIGGER_NAME AS trigger_name,
TRIGGER_SCHEMA as trigger_schema,
EVENT_OBJECT_SCHEMA AS schema_name,
EVENT_OBJECT_TABLE AS table_name,
ACTION_STATEMENT AS statement,
EVENT_MANIPULATION AS event_type,
ACTION_ORIENTATION AS orientation,
ACTION_TIMING AS timing
FROM
information_schema.TRIGGERS
WHERE
EVENT_OBJECT_SCHEMA = ? AND EVENT_OBJECT_TABLE IN (/*SLICE:tables*/?)
`
type GetCustomTriggersBySchemaAndTablesParams struct {
Schema string
Tables []string
}
type GetCustomTriggersBySchemaAndTablesRow struct {
TriggerName string
TriggerSchema string
SchemaName string
TableName string
Statement string
EventType string
Orientation string
Timing string
}
// sqlc is broken for mysql so can't do CONCAT(EVENT_OBJECT_SCHEMA, '.', EVENT_OBJECT_TABLE) IN (sqlc.slice('schematables'))
func (q *Queries) GetCustomTriggersBySchemaAndTables(ctx context.Context, db DBTX, arg *GetCustomTriggersBySchemaAndTablesParams) ([]*GetCustomTriggersBySchemaAndTablesRow, error) {
query := getCustomTriggersBySchemaAndTables
var queryParams []interface{}
queryParams = append(queryParams, arg.Schema)
if len(arg.Tables) > 0 {
for _, v := range arg.Tables {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:tables*/?", strings.Repeat(",?", len(arg.Tables))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:tables*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetCustomTriggersBySchemaAndTablesRow
for rows.Next() {
var i GetCustomTriggersBySchemaAndTablesRow
if err := rows.Scan(
&i.TriggerName,
&i.TriggerSchema,
&i.SchemaName,
&i.TableName,
&i.Statement,
&i.EventType,
&i.Orientation,
&i.Timing,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDatabaseSchema = `-- name: GetDatabaseSchema :many
SELECT
c.table_schema,
c.table_name,
c.column_name,
c.ordinal_position,
IFNULL(REPLACE(REPLACE(REPLACE(REPLACE(c.COLUMN_DEFAULT, '_utf8mb4\\\'', '_utf8mb4\''), '_utf8mb3\\\'', '_utf8mb3\''), '\\\'', '\''), '\\\'', '\''), '') AS column_default, -- hack to fix this bug https://bugs.mysql.com/bug.php?
c.is_nullable,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.extra
FROM
information_schema.columns AS c
JOIN information_schema.tables AS t ON c.table_schema = t.table_schema
AND c.table_name = t.table_name
WHERE
c.table_schema NOT IN('sys', 'performance_schema', 'mysql')
AND t.table_type = 'BASE TABLE'
`
type GetDatabaseSchemaRow struct {
TableSchema string
TableName string
ColumnName string
OrdinalPosition int64
ColumnDefault interface{}
IsNullable string
DataType string
CharacterMaximumLength sql.NullInt64
NumericPrecision sql.NullInt64
NumericScale sql.NullInt64
Extra sql.NullString
}
func (q *Queries) GetDatabaseSchema(ctx context.Context, db DBTX) ([]*GetDatabaseSchemaRow, error) {
rows, err := db.QueryContext(ctx, getDatabaseSchema)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetDatabaseSchemaRow
for rows.Next() {
var i GetDatabaseSchemaRow
if err := rows.Scan(
&i.TableSchema,
&i.TableName,
&i.ColumnName,
&i.OrdinalPosition,
&i.ColumnDefault,
&i.IsNullable,
&i.DataType,
&i.CharacterMaximumLength,
&i.NumericPrecision,
&i.NumericScale,
&i.Extra,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDatabaseTableSchemasBySchemasAndTables = `-- name: GetDatabaseTableSchemasBySchemasAndTables :many
SELECT
c.TABLE_SCHEMA AS schema_name,
c.TABLE_NAME AS table_name,
c.COLUMN_NAME AS column_name,
c.COLUMN_TYPE AS data_type,
IFNULL(REPLACE(REPLACE(REPLACE(REPLACE(c.COLUMN_DEFAULT, '_utf8mb4\\\'', '_utf8mb4\''), '_utf8mb3\\\'', '_utf8mb3\''), '\\\'', '\''), '\\\'', '\''), '') AS column_default, -- hack to fix this bug https://bugs.mysql.com/bug.php?
CASE WHEN c.IS_NULLABLE = 'YES' THEN 1 ELSE 0 END AS is_nullable,
CAST(IF(c.DATA_TYPE IN ('varchar', 'char'), c.CHARACTER_MAXIMUM_LENGTH, -1) AS SIGNED) AS character_maximum_length,
CAST(IF(c.DATA_TYPE IN ('decimal', 'numeric'), c.NUMERIC_PRECISION,
IF(c.DATA_TYPE = 'smallint', 16,
IF(c.DATA_TYPE = 'int', 32,
IF(c.DATA_TYPE = 'bigint', 64, -1))))AS SIGNED) AS numeric_precision,
CAST(IF(c.DATA_TYPE IN ('decimal', 'numeric'), c.NUMERIC_SCALE, 0)AS SIGNED) AS numeric_scale,
c.ORDINAL_POSITION AS ordinal_position,
c.EXTRA AS identity_generation,
IFNULL(REPLACE(REPLACE(REPLACE(REPLACE(c.GENERATION_EXPRESSION, '_utf8mb4\\\'', '_utf8mb4\''), '_utf8mb3\\\'', '_utf8mb3\''), '\\\'', '\''), '\\\'', '\''), '') AS generation_exp, -- hack to fix this bug https://bugs.mysql.com/
t.AUTO_INCREMENT as auto_increment_start_value
FROM
information_schema.COLUMNS as c
join information_schema.TABLES as t on t.TABLE_SCHEMA = c.TABLE_SCHEMA and t.TABLE_NAME = c.TABLE_NAME
WHERE
-- CONCAT(c.TABLE_SCHEMA, '.', c.TABLE_NAME) IN (sqlc.slice('schematables')) broken
c.TABLE_SCHEMA = ? AND c.TABLE_NAME in (/*SLICE:tables*/?)
ORDER BY
c.ordinal_position
`
type GetDatabaseTableSchemasBySchemasAndTablesParams struct {
Schema string
Tables []string
}
type GetDatabaseTableSchemasBySchemasAndTablesRow struct {
SchemaName string
TableName string
ColumnName string
DataType string
ColumnDefault interface{}
IsNullable int32
CharacterMaximumLength int64
NumericPrecision int64
NumericScale int64
OrdinalPosition int64
IdentityGeneration sql.NullString
GenerationExp interface{}
AutoIncrementStartValue sql.NullInt64
}
func (q *Queries) GetDatabaseTableSchemasBySchemasAndTables(ctx context.Context, db DBTX, arg *GetDatabaseTableSchemasBySchemasAndTablesParams) ([]*GetDatabaseTableSchemasBySchemasAndTablesRow, error) {
query := getDatabaseTableSchemasBySchemasAndTables
var queryParams []interface{}
queryParams = append(queryParams, arg.Schema)
if len(arg.Tables) > 0 {
for _, v := range arg.Tables {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:tables*/?", strings.Repeat(",?", len(arg.Tables))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:tables*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetDatabaseTableSchemasBySchemasAndTablesRow
for rows.Next() {
var i GetDatabaseTableSchemasBySchemasAndTablesRow
if err := rows.Scan(
&i.SchemaName,
&i.TableName,
&i.ColumnName,
&i.DataType,
&i.ColumnDefault,
&i.IsNullable,
&i.CharacterMaximumLength,
&i.NumericPrecision,
&i.NumericScale,
&i.OrdinalPosition,
&i.IdentityGeneration,
&i.GenerationExp,
&i.AutoIncrementStartValue,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getIndicesBySchemasAndTables = `-- name: GetIndicesBySchemasAndTables :many
SELECT
s.TABLE_SCHEMA as schema_name,
s.TABLE_NAME as table_name,
s.COLUMN_NAME as column_name,
s.EXPRESSION as expression,
s.INDEX_NAME as index_name,
s.INDEX_TYPE as index_type,
s.SEQ_IN_INDEX as seq_in_index,
s.NULLABLE as nullable
FROM information_schema.statistics s
LEFT JOIN information_schema.table_constraints tc
ON s.TABLE_SCHEMA = tc.TABLE_SCHEMA
AND s.TABLE_NAME = tc.TABLE_NAME
AND s.INDEX_NAME = tc.CONSTRAINT_NAME
WHERE
s.TABLE_SCHEMA = ?
AND s.TABLE_NAME in (/*SLICE:tables*/?)
AND tc.CONSTRAINT_NAME IS NULL -- filters out other constraints (foreign keys, unique, primary keys, etc)
ORDER BY
s.TABLE_NAME,
s.INDEX_NAME,
s.SEQ_IN_INDEX
`
type GetIndicesBySchemasAndTablesParams struct {
Schema string
Tables []string
}
type GetIndicesBySchemasAndTablesRow struct {
SchemaName string
TableName string
ColumnName sql.NullString
Expression sql.NullString
IndexName string
IndexType string
SeqInIndex sql.NullInt64
Nullable string
}
func (q *Queries) GetIndicesBySchemasAndTables(ctx context.Context, db DBTX, arg *GetIndicesBySchemasAndTablesParams) ([]*GetIndicesBySchemasAndTablesRow, error) {
query := getIndicesBySchemasAndTables
var queryParams []interface{}
queryParams = append(queryParams, arg.Schema)
if len(arg.Tables) > 0 {
for _, v := range arg.Tables {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:tables*/?", strings.Repeat(",?", len(arg.Tables))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:tables*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetIndicesBySchemasAndTablesRow
for rows.Next() {
var i GetIndicesBySchemasAndTablesRow
if err := rows.Scan(
&i.SchemaName,
&i.TableName,
&i.ColumnName,
&i.Expression,
&i.IndexName,
&i.IndexType,
&i.SeqInIndex,
&i.Nullable,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getMysqlRolePermissions = `-- name: GetMysqlRolePermissions :many
WITH admin_privileges AS (
SELECT
privilege_type
FROM
INFORMATION_SCHEMA.USER_PRIVILEGES
WHERE
USER_PRIVILEGES.GRANTEE = CONCAT("'",
SUBSTRING_INDEX(CURRENT_USER(),
'@',
1),
"'@'%'")
),
db_privileges AS (
SELECT
TABLE_SCHEMA AS table_schema,
PRIVILEGE_TYPE AS privilege_type
FROM
INFORMATION_SCHEMA.SCHEMA_PRIVILEGES
WHERE
SCHEMA_PRIVILEGES.GRANTEE = CONCAT("'",
SUBSTRING_INDEX(CURRENT_USER(),
'@',
1),
"'@'%'")
)
SELECT
t.TABLE_SCHEMA AS table_schema, t.TABLE_NAME AS table_name, ap.privilege_type AS privilege_type
FROM
INFORMATION_SCHEMA.TABLES AS t
JOIN admin_privileges AS ap
WHERE
t.TABLE_SCHEMA NOT IN('mysql', 'sys', 'performance_schema', 'information_schema')
UNION
SELECT
t.TABLE_SCHEMA AS table_schema,
t.TABLE_NAME AS table_name,
dp.privilege_type AS privilege_type
FROM
INFORMATION_SCHEMA.TABLES AS t
JOIN db_privileges AS dp ON dp.table_schema = t.table_schema
WHERE
t.TABLE_SCHEMA IN(
SELECT
table_schema FROM db_privileges)
UNION
SELECT
TABLE_PRIVILEGES.TABLE_SCHEMA AS table_schema,
TABLE_PRIVILEGES.TABLE_NAME AS table_name,
TABLE_PRIVILEGES.PRIVILEGE_TYPE AS privilege_type
FROM
INFORMATION_SCHEMA.TABLE_PRIVILEGES
WHERE
TABLE_PRIVILEGES.GRANTEE = CONCAT("'", SUBSTRING_INDEX(CURRENT_USER(), '@', 1), "'@'%'")
`
type GetMysqlRolePermissionsRow struct {
TableSchema string
TableName string
PrivilegeType string
}
func (q *Queries) GetMysqlRolePermissions(ctx context.Context, db DBTX) ([]*GetMysqlRolePermissionsRow, error) {
rows, err := db.QueryContext(ctx, getMysqlRolePermissions)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetMysqlRolePermissionsRow
for rows.Next() {
var i GetMysqlRolePermissionsRow
if err := rows.Scan(&i.TableSchema, &i.TableName, &i.PrivilegeType); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getTableConstraints = `-- name: GetTableConstraints :many
SELECT
tc.table_schema AS schema_name,
tc.table_name AS table_name,
JSON_ARRAYAGG(kcu.column_name) AS constraint_columns,
JSON_ARRAYAGG(CASE WHEN c.is_nullable = 'YES' THEN 0 ELSE 1 END) AS not_nullable,
tc.constraint_name AS constraint_name,
tc.constraint_type AS constraint_type,
COALESCE(kcu.referenced_table_schema, 'NULL') AS referenced_schema_name,
COALESCE(kcu.referenced_table_name, 'NULL') AS referenced_table_name,
JSON_ARRAYAGG(kcu.referenced_column_name) AS referenced_column_names,
rc.update_rule as update_rule,
rc.delete_rule as delete_rule,
IFNULL(REPLACE(REPLACE(REPLACE(REPLACE(cc.check_clause, '_utf8mb4\\\'', '_utf8mb4\''), '_utf8mb3\\\'', '_utf8mb3\''), '\\\'', '\''), '\\\'', '\''), '') AS check_clause -- hack to fix this bug https://bugs.mysql.com/
FROM
information_schema.table_constraints AS tc
LEFT JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
AND tc.table_name = kcu.table_name
LEFT JOIN information_schema.columns as c
ON c.table_schema = kcu.table_schema
AND c.table_name = kcu.table_name
AND c.column_name = kcu.column_name
LEFT JOIN information_schema.referential_constraints as rc
ON rc.constraint_schema = tc.table_schema
AND rc.table_name = tc.table_name
AND rc.constraint_name = tc.constraint_name
LEFT JOIN information_schema.check_constraints as cc
ON tc.constraint_schema = cc.constraint_schema
AND tc.constraint_name = cc.constraint_name
WHERE
tc.table_schema = ?
AND tc.table_name IN (/*SLICE:tables*/?)
GROUP BY
tc.table_schema,
tc.table_name,
tc.constraint_name,
tc.constraint_type,
kcu.referenced_table_schema,
kcu.referenced_table_name,
rc.update_rule,
rc.delete_rule,
cc.check_clause
`
type GetTableConstraintsParams struct {
Schema string
Tables []string
}
type GetTableConstraintsRow struct {
SchemaName string
TableName string
ConstraintColumns json.RawMessage
NotNullable json.RawMessage
ConstraintName string
ConstraintType string
ReferencedSchemaName string
ReferencedTableName string
ReferencedColumnNames json.RawMessage
UpdateRule sql.NullString
DeleteRule sql.NullString
CheckClause interface{}
}
func (q *Queries) GetTableConstraints(ctx context.Context, db DBTX, arg *GetTableConstraintsParams) ([]*GetTableConstraintsRow, error) {
query := getTableConstraints
var queryParams []interface{}
queryParams = append(queryParams, arg.Schema)
if len(arg.Tables) > 0 {
for _, v := range arg.Tables {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:tables*/?", strings.Repeat(",?", len(arg.Tables))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:tables*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetTableConstraintsRow
for rows.Next() {
var i GetTableConstraintsRow
if err := rows.Scan(
&i.SchemaName,
&i.TableName,
&i.ConstraintColumns,
&i.NotNullable,
&i.ConstraintName,
&i.ConstraintType,
&i.ReferencedSchemaName,
&i.ReferencedTableName,
&i.ReferencedColumnNames,
&i.UpdateRule,
&i.DeleteRule,
&i.CheckClause,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getTableConstraintsBySchemas = `-- name: GetTableConstraintsBySchemas :many
SELECT
tc.table_schema AS schema_name,
tc.table_name AS table_name,
JSON_ARRAYAGG(kcu.column_name) AS constraint_columns,
JSON_ARRAYAGG(CASE WHEN c.is_nullable = 'YES' THEN 0 ELSE 1 END) AS not_nullable,
tc.constraint_name AS constraint_name,
tc.constraint_type AS constraint_type,
COALESCE(kcu.referenced_table_schema, 'NULL') AS referenced_schema_name,
COALESCE(kcu.referenced_table_name, 'NULL') AS referenced_table_name,
JSON_ARRAYAGG(kcu.referenced_column_name) AS referenced_column_names,
rc.update_rule as update_rule,
rc.delete_rule as delete_rule,
IFNULL(REPLACE(REPLACE(REPLACE(REPLACE(cc.check_clause, '_utf8mb4\\\'', '_utf8mb4\''), '_utf8mb3\\\'', '_utf8mb3\''), '\\\'', '\''), '\\\'', '\''), '') AS check_clause -- hack to fix this bug https://bugs.mysql.com/
FROM
information_schema.table_constraints AS tc
LEFT JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
AND tc.table_name = kcu.table_name
LEFT JOIN information_schema.columns as c
ON c.table_schema = kcu.table_schema
AND c.table_name = kcu.table_name
AND c.column_name = kcu.column_name
LEFT JOIN information_schema.referential_constraints as rc
ON rc.constraint_schema = tc.table_schema
AND rc.table_name = tc.table_name
AND rc.constraint_name = tc.constraint_name
LEFT JOIN information_schema.check_constraints as cc
ON tc.constraint_schema = cc.constraint_schema
AND tc.constraint_name = cc.constraint_name
WHERE
tc.table_schema IN (/*SLICE:schemas*/?)
GROUP BY
tc.table_schema,
tc.table_name,
tc.constraint_name,
tc.constraint_type,
kcu.referenced_table_schema,
kcu.referenced_table_name,
rc.update_rule,
rc.delete_rule,
cc.check_clause
`
type GetTableConstraintsBySchemasRow struct {
SchemaName string
TableName string
ConstraintColumns json.RawMessage
NotNullable json.RawMessage
ConstraintName string
ConstraintType string
ReferencedSchemaName string
ReferencedTableName string
ReferencedColumnNames json.RawMessage
UpdateRule sql.NullString
DeleteRule sql.NullString
CheckClause interface{}
}
func (q *Queries) GetTableConstraintsBySchemas(ctx context.Context, db DBTX, schemas []string) ([]*GetTableConstraintsBySchemasRow, error) {
query := getTableConstraintsBySchemas
var queryParams []interface{}
if len(schemas) > 0 {
for _, v := range schemas {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:schemas*/?", strings.Repeat(",?", len(schemas))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:schemas*/?", "NULL", 1)
}
rows, err := db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetTableConstraintsBySchemasRow
for rows.Next() {
var i GetTableConstraintsBySchemasRow
if err := rows.Scan(
&i.SchemaName,
&i.TableName,
&i.ConstraintColumns,
&i.NotNullable,
&i.ConstraintName,
&i.ConstraintType,
&i.ReferencedSchemaName,
&i.ReferencedTableName,
&i.ReferencedColumnNames,
&i.UpdateRule,
&i.DeleteRule,
&i.CheckClause,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}