-
Notifications
You must be signed in to change notification settings - Fork 1
/
information_schema.go
1224 lines (1112 loc) · 56.8 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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package information_schema
import (
"bytes"
"fmt"
"io"
"strings"
"time"
"github.com/Ciyfly/FakerVitess/go/sqltypes"
"github.com/Ciyfly/FakerVitess/go/vt/sqlparser"
. "github.com/Ciyfly/FakerMysql/sql"
"github.com/Ciyfly/FakerMysql/sql/analyzer"
"github.com/Ciyfly/FakerMysql/sql/parse"
"github.com/Ciyfly/FakerMysql/sql/plan"
)
const (
// InformationSchemaDatabaseName is the name of the information schema database.
InformationSchemaDatabaseName = "information_schema"
// FilesTableName is the name of the files table.
FilesTableName = "files"
// ColumnStatisticsTableName is the name of the column statistics table.
ColumnStatisticsTableName = "column_statistics"
// TablesTableName is the name of tables table.
TablesTableName = "tables"
// ColumnsTableName is the name of columns table.
ColumnsTableName = "columns"
// SchemataTableName is the name of the schemata table.
SchemataTableName = "schemata"
// CollationsTableName is the name of the collations table.
CollationsTableName = "collations"
// StatisticsTableName is the name of the statistics table.
StatisticsTableName = "statistics"
// TableConstraintsTableName is the name of the table_constraints table.
TableConstraintsTableName = "table_constraints"
// ReferentialConstraintsTableName is the name of the table_constraints table.
ReferentialConstraintsTableName = "referential_constraints"
// KeyColumnUsageTableName is the name of the key_column_usage table.
KeyColumnUsageTableName = "key_column_usage"
// TriggersTableName is the name of the triggers table.
TriggersTableName = "triggers"
// EventsTableName is the name of the events table.
EventsTableName = "events"
// RoutinesTableName is the name of the routines table.
RoutinesTableName = "routines"
// ViewsTableName is the name of the views table.
ViewsTableName = "views"
// UserPrivilegesTableName is the name of the user_privileges table
UserPrivilegesTableName = "user_privileges"
// CharacterSetsTableName is the name of the character_sets table
CharacterSetsTableName = "character_sets"
// EnginesTableName is the name of the engines table
EnginesTableName = "engines"
// CheckConstraintsTableName is the name of check_constraints table
CheckConstraintsTableName = "check_constraints"
// PartitionsTableName is the name of the partitions table
PartitionsTableName = "partitions"
// InnoDBTempTableName is the name of the INNODB_TEMP_TABLE_INFO table
InnoDBTempTableName = "innodb_temp_table_info"
)
var _ Database = (*informationSchemaDatabase)(nil)
type informationSchemaDatabase struct {
name string
tables map[string]Table
}
type informationSchemaTable struct {
name string
schema Schema
catalog Catalog
rowIter func(*Context, Catalog) (RowIter, error)
}
type informationSchemaPartition struct {
key []byte
}
type informationSchemaPartitionIter struct {
informationSchemaPartition
pos int
}
var (
_ Database = (*informationSchemaDatabase)(nil)
_ Table = (*informationSchemaTable)(nil)
_ Partition = (*informationSchemaPartition)(nil)
_ PartitionIter = (*informationSchemaPartitionIter)(nil)
)
var filesSchema = Schema{
{Name: "file_id", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "file_name", Type: LongText, Source: FilesTableName, Nullable: true},
{Name: "file_type", Type: LongText, Source: FilesTableName, Nullable: true},
{Name: "tablespace_name", Type: LongText, Source: FilesTableName},
{Name: "table_catalog", Type: LongText, Source: FilesTableName},
{Name: "table_schema", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "table_name", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "logfile_group_name", Type: LongText, Source: FilesTableName, Nullable: true},
{Name: "logfile_group_number", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "engine", Type: LongText, Source: FilesTableName},
{Name: "fulltext_keys", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "deleted_rows", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "update_count", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "free_extents", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "total_extents", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "extent_size", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "initial_size", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "maximum_size", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "autoextend_size", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "creation_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "last_update_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "last_access_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "recover_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "transaction_counter", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "version", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "row_format", Type: LongText, Source: FilesTableName, Nullable: true},
{Name: "table_rows", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "avg_row_length", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "data_length", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "max_data_length", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "index_length", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "data_free", Type: Int64, Source: FilesTableName, Nullable: true},
{Name: "create_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "update_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "check_time", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "checksum", Type: LongBlob, Source: FilesTableName, Nullable: true},
{Name: "status", Type: LongText, Source: FilesTableName, Nullable: true},
{Name: "extra", Type: LongBlob, Source: FilesTableName, Nullable: true},
}
var columnStatisticsSchema = Schema{
{Name: "schema_name", Type: LongText, Source: ColumnStatisticsTableName},
{Name: "table_name", Type: LongText, Source: ColumnStatisticsTableName},
{Name: "column_name", Type: LongText, Source: ColumnStatisticsTableName},
{Name: "histogram", Type: JSON, Source: ColumnStatisticsTableName},
}
var tablesSchema = Schema{
{Name: "table_catalog", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: TablesTableName},
{Name: "table_schema", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: TablesTableName},
{Name: "table_name", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: TablesTableName},
{Name: "table_type", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: TablesTableName},
{Name: "engine", Type: LongText, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "version", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "row_format", Type: LongText, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "table_rows", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "avg_row_length", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "data_length", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "max_data_length", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "index_length", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "data_free", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "auto_increment", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "create_time", Type: Timestamp, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "update_time", Type: Timestamp, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "check_time", Type: Timestamp, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "table_collation", Type: LongText, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "checksum", Type: Uint64, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "create_options", Type: LongText, Default: nil, Nullable: true, Source: TablesTableName},
{Name: "table_comment", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: TablesTableName},
}
var columnsSchema = Schema{
{Name: "table_catalog", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "table_schema", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "table_name", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "column_name", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "ordinal_position", Type: Uint64, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), "0", Uint64, false), Nullable: false, Source: ColumnsTableName},
{Name: "column_default", Type: LongText, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "is_nullable", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "data_type", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "character_maximum_length", Type: Uint64, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "character_octet_length", Type: Uint64, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "numeric_precision", Type: Uint64, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "numeric_scale", Type: Uint64, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "datetime_precision", Type: Uint64, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "character_set_name", Type: LongText, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "collation_name", Type: LongText, Default: nil, Nullable: true, Source: ColumnsTableName},
{Name: "column_type", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "column_key", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "extra", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "privileges", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "column_comment", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
{Name: "generation_expression", Type: LongText, Default: parse.MustStringToColumnDefaultValue(NewEmptyContext(), `""`, LongText, false), Nullable: false, Source: ColumnsTableName},
}
var schemataSchema = Schema{
{Name: "catalog_name", Type: LongText, Default: nil, Nullable: false, Source: SchemataTableName},
{Name: "schema_name", Type: LongText, Default: nil, Nullable: false, Source: SchemataTableName},
{Name: "default_character_set_name", Type: LongText, Default: nil, Nullable: false, Source: SchemataTableName},
{Name: "default_collation_name", Type: LongText, Default: nil, Nullable: false, Source: SchemataTableName},
{Name: "sql_path", Type: LongText, Default: nil, Nullable: true, Source: SchemataTableName},
}
var collationsSchema = Schema{
{Name: "collation_name", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
{Name: "character_set_name", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
{Name: "id", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
{Name: "is_default", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
{Name: "is_compiled", Type: LongText, Default: nil, Nullable: true, Source: CollationsTableName},
{Name: "sortlen", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
{Name: "pad_attribute", Type: LongText, Default: nil, Nullable: false, Source: CollationsTableName},
}
var statisticsSchema = Schema{
{Name: "table_catalog", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "table_schema", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "table_name", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "non_unique", Type: Int64, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "index_schema", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "index_name", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "seq_in_index", Type: Int64, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "column_name", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "collation", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "cardinality", Type: Int64, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "sub_part", Type: Int64, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "packed", Type: Int64, Default: nil, Nullable: true, Source: StatisticsTableName},
{Name: "nullable", Type: LongText, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "index_type", Type: LongText, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "comment", Type: LongText, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "index_comment", Type: LongText, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "is_visible", Type: LongText, Default: nil, Nullable: false, Source: StatisticsTableName},
{Name: "expression", Type: LongText, Default: nil, Nullable: true, Source: StatisticsTableName},
}
var tableConstraintsSchema = Schema{
{Name: "constraint_catalog", Type: LongText, Default: nil, Nullable: true, Source: TableConstraintsTableName},
{Name: "constraint_schema", Type: LongText, Default: nil, Nullable: true, Source: TableConstraintsTableName},
{Name: "constraint_name", Type: LongText, Default: nil, Nullable: true, Source: TableConstraintsTableName},
{Name: "table_schema", Type: LongText, Default: nil, Nullable: true, Source: TableConstraintsTableName},
{Name: "table_name", Type: LongText, Default: nil, Nullable: true, Source: TableConstraintsTableName},
{Name: "constraint_type", Type: LongText, Default: nil, Nullable: false, Source: TableConstraintsTableName},
{Name: "enforced", Type: LongText, Default: nil, Nullable: false, Source: TableConstraintsTableName},
}
var referentialConstraintsSchema = Schema{
{Name: "constraint_catalog", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "constraint_schema", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "constraint_name", Type: LongText, Default: nil, Nullable: true, Source: ReferentialConstraintsTableName},
{Name: "unique_constraint_catalog", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "unique_constraint_schema", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "unique_constraint_name", Type: LongText, Default: nil, Nullable: true, Source: ReferentialConstraintsTableName},
{Name: "match_option", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "update_rule", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "delete_rule", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "table_name", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
{Name: "referenced_table_name", Type: LongText, Default: nil, Nullable: false, Source: ReferentialConstraintsTableName},
}
var keyColumnUsageSchema = Schema{
{Name: "constraint_catalog", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "constraint_schema", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "constraint_name", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "table_catalog", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "table_schema", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "table_name", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "column_name", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "ordinal_position", Type: LongText, Default: nil, Nullable: false, Source: KeyColumnUsageTableName},
{Name: "position_in_unique_constraint", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "referenced_table_schema", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "referenced_table_name", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
{Name: "referenced_column_name", Type: LongText, Default: nil, Nullable: true, Source: KeyColumnUsageTableName},
}
var triggersSchema = Schema{
{Name: "trigger_catalog", Type: LongText, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "trigger_schema", Type: LongText, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "trigger_name", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "event_manipulation", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "event_object_catalog", Type: LongText, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "event_object_schema", Type: LongText, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "event_object_table", Type: LongText, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "action_order", Type: Int64, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "action_condition", Type: Int64, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "action_statement", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "action_orientation", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "action_timing", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "action_reference_old_table", Type: Int64, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "action_reference_new_table", Type: Int64, Default: nil, Nullable: true, Source: TriggersTableName},
{Name: "action_reference_old_row", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "action_reference_new_row", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "created", Type: Timestamp, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "sql_mode", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "definer", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "character_set_client", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "collation_connection", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
{Name: "database_collation", Type: LongText, Default: nil, Nullable: false, Source: TriggersTableName},
}
var eventsSchema = Schema{
{Name: "event_catalog", Type: LongText, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "event_schema", Type: LongText, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "event_name", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "definer", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "time_zone", Type: LongText, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "event_body", Type: LongText, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "event_definition", Type: LongText, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "event_type", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "execute_at", Type: Datetime, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "interval_value", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "interval_field", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "sql_mode", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "starts", Type: Datetime, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "ends", Type: Datetime, Default: nil, Nullable: true, Source: EventsTableName},
{Name: "status", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "on_completion", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "created", Type: Timestamp, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "last_altered", Type: Timestamp, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "last_executed", Type: Datetime, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "event_comment", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "originator", Type: Int64, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "character_set_client", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "collation_connection", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
{Name: "database_collation", Type: LongText, Default: nil, Nullable: false, Source: EventsTableName},
}
var routinesSchema = Schema{
{Name: "specific_name", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "routine_catalog", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "routine_schema", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "routine_name", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "routine_type", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "data_type", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "character_maximum_length", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "character_octet_length", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "numeric_precision", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "numeric_scale", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "datetime_precision", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "character_set_name", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "collation_name", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "dtd_identifier", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "routine_body", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "routine_definition", Type: LongText, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "external_name", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "external_language", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "parameter_style", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "is_deterministic", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "sql_data_access", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "sql_path", Type: Int64, Default: nil, Nullable: true, Source: RoutinesTableName},
{Name: "security_type", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "created", Type: Timestamp, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "last_altered", Type: Timestamp, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "sql_mode", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "routine_comment", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "definer", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "character_set_client", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "collation_connection", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
{Name: "database_collation", Type: LongText, Default: nil, Nullable: false, Source: RoutinesTableName},
}
var viewsSchema = Schema{
{Name: "table_catalog", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "table_schema", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "table_name", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "view_definition", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "check_option", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "is_updatable", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "definer", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "security_type", Type: LongText, Default: nil, Nullable: true, Source: ViewsTableName},
{Name: "character_set_client", Type: LongText, Default: nil, Nullable: false, Source: ViewsTableName},
{Name: "collation_connection", Type: LongText, Default: nil, Nullable: false, Source: ViewsTableName},
}
var userPrivilegesSchema = Schema{
{Name: "grantee", Type: LongText, Default: nil, Nullable: false, Source: UserPrivilegesTableName},
{Name: "table_catalog", Type: LongText, Default: nil, Nullable: false, Source: UserPrivilegesTableName},
{Name: "privilege_type", Type: LongText, Default: nil, Nullable: false, Source: UserPrivilegesTableName},
{Name: "is_grantable", Type: LongText, Default: nil, Nullable: false, Source: UserPrivilegesTableName},
}
var characterSetSchema = Schema{
{Name: "character_set_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: CharacterSetsTableName},
{Name: "default_collate_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: CharacterSetsTableName},
{Name: "description", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 2048), Default: nil, Nullable: false, Source: CharacterSetsTableName},
{Name: "maxlen", Type: Uint8, Default: nil, Nullable: false, Source: CharacterSetsTableName},
}
var enginesSchema = Schema{
{Name: "engine", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: EnginesTableName},
{Name: "support", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 8), Default: nil, Nullable: false, Source: EnginesTableName},
{Name: "comment", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 80), Default: nil, Nullable: false, Source: EnginesTableName},
{Name: "transactions", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 3), Default: nil, Nullable: false, Source: EnginesTableName},
{Name: "xa", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 3), Default: nil, Nullable: false, Source: EnginesTableName},
{Name: "savepoints", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 3), Default: nil, Nullable: false, Source: EnginesTableName},
}
var checkConstraintsSchema = Schema{
{Name: "constraint_catalog", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: CheckConstraintsTableName},
{Name: "constraint_schema", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: CheckConstraintsTableName},
{Name: "constraint_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: CheckConstraintsTableName},
{Name: "check_clause", Type: LongText, Default: nil, Nullable: false, Source: CheckConstraintsTableName},
}
var partitionSchema = Schema{
{Name: "table_catalog", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "table_schema", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "table_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false, Source: PartitionsTableName},
{Name: "partition_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "subpartition_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "partition_ordinal_position", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "subpartition_ordinal_position", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "partition_method", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 13), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "subpartition_method", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 13), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "partition_expression", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 2048), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "subpartition_expression", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 2048), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "partition_description", Type: LongText, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "table_rows", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "avg_row_length", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "data_length", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "max_data_length", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "index_length", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "data_free", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "create_time", Type: Timestamp, Default: nil, Nullable: false, Source: PartitionsTableName},
{Name: "update_time", Type: Datetime, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "check_time", Type: Datetime, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "checksum", Type: Uint64, Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "partition_comment", Type: LongText, Default: nil, Nullable: false, Source: PartitionsTableName},
{Name: "nodegroup", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 256), Default: nil, Nullable: true, Source: PartitionsTableName},
{Name: "tablespace_name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 258), Default: nil, Nullable: true, Source: PartitionsTableName},
}
var innoDBTempTableSchema = Schema{
{Name: "table_id", Type: Int64, Default: nil, Nullable: false, Source: InnoDBTempTableName},
{Name: "name", Type: MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: true, Source: InnoDBTempTableName},
{Name: "n_cols", Type: Uint64, Default: nil, Nullable: false, Source: InnoDBTempTableName},
{Name: "space", Type: Uint64, Default: nil, Nullable: false, Source: InnoDBTempTableName},
}
func tablesRowIter(ctx *Context, cat Catalog) (RowIter, error) {
var rows []Row
for _, db := range cat.AllDatabases() {
tableType := "BASE TABLE"
engine := "INNODB"
rowFormat := "Dynamic"
if db.Name() == InformationSchemaDatabaseName {
tableType = "SYSTEM VIEW"
engine = "MEMORY"
rowFormat = "Fixed"
}
y2k, _ := Timestamp.Convert("2000-01-01 00:00:00")
err := DBTableIter(ctx, db, func(t Table) (cont bool, err error) {
autoVal := getAutoIncrementValue(ctx, t)
rows = append(rows, Row{
"def", // table_catalog
db.Name(), // table_schema
t.Name(), // table_name
tableType, // table_type
engine, // engine
10, // version (protocol, always 10)
rowFormat, // row_format
nil, // table_rows
nil, // avg_row_length
nil, // data_length
nil, // max_data_length
nil, // max_data_length
nil, // data_free
autoVal, // auto_increment
y2k, // create_time
y2k, // update_time
nil, // check_time
Collation_Default.String(), // table_collation
nil, // checksum
nil, // create_options
"", // table_comment
})
return true, nil
})
if err != nil {
return nil, err
}
views, err := viewsInDatabase(ctx, db)
if err != nil {
return nil, err
}
for _, view := range views {
rows = append(rows, Row{
"def", // table_catalog
db.Name(), // table_schema
view.Name, // table_name
"VIEW", // table_type
engine, // engine
10, // version (protocol, always 10)
rowFormat, // row_format
nil, // table_rows
nil, // avg_row_length
nil, // data_length
nil, // max_data_length
nil, // max_data_length
nil, // data_free
nil, // auto_increment
nil, // create_time
nil, // update_time
nil, // check_time
Collation_Default.String(), // table_collation
nil, // checksum
nil, // create_options
"", // table_comment
})
}
}
return RowsToRowIter(rows...), nil
}
func columnsRowIter(ctx *Context, cat Catalog) (RowIter, error) {
var rows []Row
for _, db := range cat.AllDatabases() {
err := DBTableIter(ctx, db, func(t Table) (cont bool, err error) {
for i, c := range t.Schema() {
var (
nullable string
charName interface{}
collName interface{}
)
if c.Nullable {
nullable = "YES"
} else {
nullable = "NO"
}
if IsText(c.Type) {
charName = Collation_Default.CharacterSet().String()
collName = Collation_Default.String()
}
rows = append(rows, Row{
"def", // table_catalog
db.Name(), // table_schema
t.Name(), // table_name
c.Name, // column_name
uint64(i), // ordinal_position
c.Default.String(), // column_default
nullable, // is_nullable
strings.ToLower(c.Type.String()), // data_type
nil, // character_maximum_length
nil, // character_octet_length
nil, // numeric_precision
nil, // numeric_scale
nil, // datetime_precision
charName, // character_set_name
collName, // collation_name
strings.ToLower(c.Type.String()), // column_type
"", // column_key
c.Extra, // extra
"select", // privileges
c.Comment, // column_comment
"", // generation_expression
})
}
return true, nil
})
if err != nil {
return nil, err
}
}
return RowsToRowIter(rows...), nil
}
func schemataRowIter(ctx *Context, c Catalog) (RowIter, error) {
dbs := c.AllDatabases()
var rows []Row
for _, db := range dbs {
rows = append(rows, Row{
"def",
db.Name(),
Collation_Default.CharacterSet().String(),
Collation_Default.String(),
nil,
})
}
return RowsToRowIter(rows...), nil
}
func collationsRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for cName := range CollationToMySQLVals {
c := Collations[cName]
rows = append(rows, Row{
c.String(),
c.CharacterSet().String(),
c.ID(),
c.IsDefault(),
c.IsCompiled(),
c.SortLen(),
c.PadSpace(),
})
}
return RowsToRowIter(rows...), nil
}
func charsetRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, c := range SupportedCharsets {
rows = append(rows, Row{
c.String(),
c.DefaultCollation().String(),
c.Description(),
c.MaxLength(),
})
}
return RowsToRowIter(rows...), nil
}
func engineRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, c := range SupportedEngines {
rows = append(rows, Row{
c.String(),
c.Support(),
c.Comment(),
c.Transactions(),
c.XA(),
c.Savepoints(),
})
}
return RowsToRowIter(rows...), nil
}
func triggersRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, db := range c.AllDatabases() {
triggerDb, ok := db.(TriggerDatabase)
if ok {
triggers, err := triggerDb.GetTriggers(ctx)
if err != nil {
return nil, err
}
var triggerPlans []*plan.CreateTrigger
for _, trigger := range triggers {
parsedTrigger, err := parse.Parse(ctx, trigger.CreateStatement)
if err != nil {
return nil, err
}
triggerPlan, ok := parsedTrigger.(*plan.CreateTrigger)
if !ok {
return nil, ErrTriggerCreateStatementInvalid.New(trigger.CreateStatement)
}
triggerPlans = append(triggerPlans, triggerPlan)
}
beforeTriggers, afterTriggers := analyzer.OrderTriggers(triggerPlans)
var beforeDelete []*plan.CreateTrigger
var beforeInsert []*plan.CreateTrigger
var beforeUpdate []*plan.CreateTrigger
var afterDelete []*plan.CreateTrigger
var afterInsert []*plan.CreateTrigger
var afterUpdate []*plan.CreateTrigger
for _, triggerPlan := range beforeTriggers {
switch triggerPlan.TriggerEvent {
case sqlparser.DeleteStr:
beforeDelete = append(beforeDelete, triggerPlan)
case sqlparser.InsertStr:
beforeInsert = append(beforeInsert, triggerPlan)
case sqlparser.UpdateStr:
beforeUpdate = append(beforeUpdate, triggerPlan)
}
}
for _, triggerPlan := range afterTriggers {
switch triggerPlan.TriggerEvent {
case sqlparser.DeleteStr:
afterDelete = append(afterDelete, triggerPlan)
case sqlparser.InsertStr:
afterInsert = append(afterInsert, triggerPlan)
case sqlparser.UpdateStr:
afterUpdate = append(afterUpdate, triggerPlan)
}
}
// These are grouped as such just to use the index as the action order. No special importance on the arrangement,
// or the fact that these are slices in a larger slice rather than separate counts.
for _, planGroup := range [][]*plan.CreateTrigger{beforeDelete, beforeInsert, beforeUpdate, afterDelete, afterInsert, afterUpdate} {
for order, triggerPlan := range planGroup {
triggerEvent := strings.ToUpper(triggerPlan.TriggerEvent)
triggerTime := strings.ToUpper(triggerPlan.TriggerTime)
tableName := triggerPlan.Table.(*plan.UnresolvedTable).Name()
characterSetClient, err := ctx.GetSessionVariable(ctx, "character_set_client")
if err != nil {
return nil, err
}
collationConnection, err := ctx.GetSessionVariable(ctx, "collation_connection")
if err != nil {
return nil, err
}
collationServer, err := ctx.GetSessionVariable(ctx, "collation_server")
if err != nil {
return nil, err
}
rows = append(rows, Row{
"def", // trigger_catalog
triggerDb.Name(), // trigger_schema
triggerPlan.TriggerName, // trigger_name
triggerEvent, // event_manipulation
"def", // event_object_catalog
triggerDb.Name(), // event_object_schema //TODO: table may be in a different db
tableName, // event_object_table
int64(order + 1), // action_order
nil, // action_condition
triggerPlan.BodyString, // action_statement
"ROW", // action_orientation
triggerTime, // action_timing
nil, // action_reference_old_table
nil, // action_reference_new_table
"OLD", // action_reference_old_row
"NEW", // action_reference_new_row
time.Unix(0, 0).UTC(), // created
"", // sql_mode
"", // definer
characterSetClient, // character_set_client
collationConnection, // collation_connection
collationServer, // database_collation
})
}
}
}
}
return RowsToRowIter(rows...), nil
}
func checkConstraintsRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, db := range c.AllDatabases() {
tableNames, err := db.GetTableNames(ctx)
if err != nil {
return nil, err
}
for _, tableName := range tableNames {
tbl, _, err := c.Table(ctx, db.Name(), tableName)
if err != nil {
return nil, err
}
checkTbl, ok := tbl.(CheckTable)
if ok {
checkDefinitions, err := checkTbl.GetChecks(ctx)
if err != nil {
return nil, err
}
for _, checkDefinition := range checkDefinitions {
rows = append(rows, Row{"def", db.Name(), checkDefinition.Name, checkDefinition.CheckExpression})
}
}
}
}
return RowsToRowIter(rows...), nil
}
func tableConstraintRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, db := range c.AllDatabases() {
tableNames, err := db.GetTableNames(ctx)
if err != nil {
return nil, err
}
for _, tableName := range tableNames {
tbl, _, err := c.Table(ctx, db.Name(), tableName)
if err != nil {
return nil, err
}
// Get all the CHECKs
checkTbl, ok := tbl.(CheckTable)
if ok {
checkDefinitions, err := checkTbl.GetChecks(ctx)
if err != nil {
return nil, err
}
for _, checkDefinition := range checkDefinitions {
enforced := "YES"
if !checkDefinition.Enforced {
enforced = "NO"
}
rows = append(rows, Row{"def", db.Name(), checkDefinition.Name, db.Name(), tbl.Name(), "CHECK", enforced})
}
}
// Get UNIQUEs, PRIMARY KEYs
// TODO: Doesn't correctly consider primary keys from table implementations that don't implement sql.IndexedTable
indexTable, ok := tbl.(IndexedTable)
if ok {
indexes, err := indexTable.GetIndexes(ctx)
if err != nil {
return nil, err
}
for _, index := range indexes {
outputType := "PRIMARY KEY"
if index.ID() != "PRIMARY" {
if index.IsUnique() {
outputType = "UNIQUE"
} else {
// In this case we have a multi-index which is not represented in this table
continue
}
}
rows = append(rows, Row{"def", db.Name(), index.ID(), db.Name(), tbl.Name(), outputType, "YES"})
}
}
// Get FKs
fkTable, ok := tbl.(ForeignKeyTable)
if ok {
fks, err := fkTable.GetForeignKeys(ctx)
if err != nil {
return nil, err
}
for _, fk := range fks {
rows = append(rows, Row{"def", db.Name(), fk.Name, db.Name(), tbl.Name(), "FOREIGN KEY", "YES"})
}
}
}
}
return RowsToRowIter(rows...), nil
}
func getColumnNamesFromIndex(idx Index, table Table) []string {
var indexCols []string
for _, expr := range idx.Expressions() {
col := plan.GetColumnFromIndexExpr(expr, table)
if col != nil {
indexCols = append(indexCols, fmt.Sprintf("`%s`", col.Name))
}
}
return indexCols
}
func keyColumnConstraintRowIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, db := range c.AllDatabases() {
tableNames, err := db.GetTableNames(ctx)
if err != nil {
return nil, err
}
for _, tableName := range tableNames {
tbl, _, err := c.Table(ctx, db.Name(), tableName)
if err != nil {
return nil, err
}
// Get UNIQUEs, PRIMARY KEYs
// TODO: Doesn't correctly consider primary keys from table implementations that don't implement sql.IndexedTable
indexTable, ok := tbl.(IndexedTable)
if ok {
indexes, err := indexTable.GetIndexes(ctx)
if err != nil {
return nil, err
}
for _, index := range indexes {
// In this case we have a multi-index which is not represented in this table
if index.ID() != "PRIMARY" && !index.IsUnique() {
continue
}
colNames := getColumnNamesFromIndex(index, tbl)
// Create a Row for each column this index refers too.
for i, colName := range colNames {
colName = strings.Replace(colName, "`", "", -1) // get rid of backticks
ordinalPosition := i + 1 // Ordinal Positions starts at one
rows = append(rows, Row{"def", db.Name(), index.ID(), "def", db.Name(), tbl.Name(), colName, ordinalPosition, nil, nil, nil, nil})
}
}
}
// Get FKs
fkTable, ok := tbl.(ForeignKeyTable)
if ok {
fks, err := fkTable.GetForeignKeys(ctx)
if err != nil {
return nil, err
}
for _, fk := range fks {
for j, colName := range fk.Columns {
ordinalPosition := j + 1
referencedSchema := db.Name()
referencedTableName := fk.ReferencedTable
referencedColumnName := strings.Replace(fk.ReferencedColumns[j], "`", "", -1) // get rid of backticks
rows = append(rows, Row{"def", db.Name(), fk.Name, "def", db.Name(), tbl.Name(), colName, ordinalPosition, ordinalPosition, referencedSchema, referencedTableName, referencedColumnName})
}
}
}
}
}
return RowsToRowIter(rows...), nil
}
// innoDBTempTableIter returns info on the temporary tables stored in the session.
// TODO: Since Table ids and Space are not yet supported this table is not completely accurate yet.
func innoDBTempTableIter(ctx *Context, c Catalog) (RowIter, error) {
var rows []Row
for _, db := range c.AllDatabases() {
tb, ok := db.(TemporaryTableDatabase)
if !ok {
continue
}
tables, err := tb.GetAllTemporaryTables(ctx)
if err != nil {
return nil, err
}
for i, table := range tables {
rows = append(rows, Row{i, table.String(), len(table.Schema()), 0})
}
}
return RowsToRowIter(rows...), nil
}
func emptyRowIter(ctx *Context, c Catalog) (RowIter, error) {
return RowsToRowIter(), nil
}
// NewInformationSchemaDatabase creates a new INFORMATION_SCHEMA Database.
func NewInformationSchemaDatabase() Database {
return &informationSchemaDatabase{
name: InformationSchemaDatabaseName,
tables: map[string]Table{
FilesTableName: &informationSchemaTable{
name: FilesTableName,
schema: filesSchema,
},
ColumnStatisticsTableName: &informationSchemaTable{
name: ColumnStatisticsTableName,
schema: columnStatisticsSchema,
},
TablesTableName: &informationSchemaTable{
name: TablesTableName,
schema: tablesSchema,
rowIter: tablesRowIter,
},
ColumnsTableName: &informationSchemaTable{
name: ColumnsTableName,
schema: columnsSchema,
rowIter: columnsRowIter,
},
SchemataTableName: &informationSchemaTable{
name: SchemataTableName,
schema: schemataSchema,
rowIter: schemataRowIter,
},
CollationsTableName: &informationSchemaTable{
name: CollationsTableName,
schema: collationsSchema,
rowIter: collationsRowIter,
},
CharacterSetsTableName: &informationSchemaTable{
name: CharacterSetsTableName,
schema: characterSetSchema,
rowIter: charsetRowIter,
},
StatisticsTableName: &informationSchemaTable{
name: StatisticsTableName,
schema: statisticsSchema,
rowIter: emptyRowIter,
},
TableConstraintsTableName: &informationSchemaTable{
name: TableConstraintsTableName,
schema: tableConstraintsSchema,
rowIter: tableConstraintRowIter,
},