-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
tables.go
1246 lines (1159 loc) · 47.5 KB
/
tables.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 2016 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package infoschema
import (
"fmt"
"sort"
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/sqlexec"
)
const (
tableSchemata = "SCHEMATA"
tableTables = "TABLES"
tableColumns = "COLUMNS"
tableStatistics = "STATISTICS"
tableCharacterSets = "CHARACTER_SETS"
tableCollations = "COLLATIONS"
tableFiles = "FILES"
catalogVal = "def"
tableProfiling = "PROFILING"
tablePartitions = "PARTITIONS"
tableKeyColumm = "KEY_COLUMN_USAGE"
tableReferConst = "REFERENTIAL_CONSTRAINTS"
tableSessionVar = "SESSION_VARIABLES"
tablePlugins = "PLUGINS"
tableConstraints = "TABLE_CONSTRAINTS"
tableTriggers = "TRIGGERS"
tableUserPrivileges = "USER_PRIVILEGES"
tableSchemaPrivileges = "SCHEMA_PRIVILEGES"
tableTablePrivileges = "TABLE_PRIVILEGES"
tableColumnPrivileges = "COLUMN_PRIVILEGES"
tableEngines = "ENGINES"
tableViews = "VIEWS"
tableRoutines = "ROUTINES"
tableParameters = "PARAMETERS"
tableEvents = "EVENTS"
tableGlobalStatus = "GLOBAL_STATUS"
tableGlobalVariables = "GLOBAL_VARIABLES"
tableSessionStatus = "SESSION_STATUS"
tableOptimizerTrace = "OPTIMIZER_TRACE"
tableTableSpaces = "TABLESPACES"
tableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY"
)
type columnInfo struct {
name string
tp byte
size int
flag uint
deflt interface{}
elems []string
}
func buildColumnInfo(tableName string, col columnInfo) *model.ColumnInfo {
mCharset := charset.CharsetBin
mCollation := charset.CharsetBin
mFlag := mysql.UnsignedFlag
if col.tp == mysql.TypeVarchar || col.tp == mysql.TypeBlob {
mCharset = mysql.DefaultCharset
mCollation = mysql.DefaultCollationName
mFlag = col.flag
}
fieldType := types.FieldType{
Charset: mCharset,
Collate: mCollation,
Tp: col.tp,
Flen: col.size,
Flag: mFlag,
}
return &model.ColumnInfo{
Name: model.NewCIStr(col.name),
FieldType: fieldType,
State: model.StatePublic,
}
}
func buildTableMeta(tableName string, cs []columnInfo) *model.TableInfo {
cols := make([]*model.ColumnInfo, 0, len(cs))
for _, c := range cs {
cols = append(cols, buildColumnInfo(tableName, c))
}
for i, col := range cols {
col.Offset = i
}
return &model.TableInfo{
Name: model.NewCIStr(tableName),
Columns: cols,
State: model.StatePublic,
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
}
}
var schemataCols = []columnInfo{
{"CATALOG_NAME", mysql.TypeVarchar, 512, 0, nil, nil},
{"SCHEMA_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"DEFAULT_CHARACTER_SET_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"DEFAULT_COLLATION_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"SQL_PATH", mysql.TypeVarchar, 512, 0, nil, nil},
}
var tablesCols = []columnInfo{
{"TABLE_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_TYPE", mysql.TypeVarchar, 64, 0, nil, nil},
{"ENGINE", mysql.TypeVarchar, 64, 0, nil, nil},
{"VERSION", mysql.TypeLonglong, 21, 0, nil, nil},
{"ROW_FORMAT", mysql.TypeVarchar, 10, 0, nil, nil},
{"TABLE_ROWS", mysql.TypeLonglong, 21, 0, nil, nil},
{"AVG_ROW_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATA_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"MAX_DATA_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"INDEX_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATA_FREE", mysql.TypeLonglong, 21, 0, nil, nil},
{"AUTO_INCREMENT", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATE_TIME", mysql.TypeDatetime, 19, 0, nil, nil},
{"UPDATE_TIME", mysql.TypeDatetime, 19, 0, nil, nil},
{"CHECK_TIME", mysql.TypeDatetime, 19, 0, nil, nil},
{"TABLE_COLLATION", mysql.TypeVarchar, 32, mysql.NotNullFlag, "utf8_bin", nil},
{"CHECK_SUM", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATE_OPTIONS", mysql.TypeVarchar, 255, 0, nil, nil},
{"TABLE_COMMENT", mysql.TypeVarchar, 2048, 0, nil, nil},
}
// See: http://dev.mysql.com/doc/refman/5.7/en/columns-table.html
var columnsCols = []columnInfo{
{"TABLE_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"COLUMN_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"ORDINAL_POSITION", mysql.TypeLonglong, 64, 0, nil, nil},
{"COLUMN_DEFAULT", mysql.TypeBlob, 196606, 0, nil, nil},
{"IS_NULLABLE", mysql.TypeVarchar, 3, 0, nil, nil},
{"DATA_TYPE", mysql.TypeVarchar, 64, 0, nil, nil},
{"CHARACTER_MAXIMUM_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"CHARACTER_OCTET_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"NUMERIC_PRECISION", mysql.TypeLonglong, 21, 0, nil, nil},
{"NUMERIC_SCALE", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATETIME_PRECISION", mysql.TypeLonglong, 21, 0, nil, nil},
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"COLLATION_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"COLUMN_TYPE", mysql.TypeBlob, 196606, 0, nil, nil},
{"COLUMN_KEY", mysql.TypeVarchar, 3, 0, nil, nil},
{"EXTRA", mysql.TypeVarchar, 30, 0, nil, nil},
{"PRIVILEGES", mysql.TypeVarchar, 80, 0, nil, nil},
{"COLUMN_COMMENT", mysql.TypeVarchar, 1024, 0, nil, nil},
}
var statisticsCols = []columnInfo{
{"TABLE_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"NON_UNIQUE", mysql.TypeVarchar, 1, 0, nil, nil},
{"INDEX_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"INDEX_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"SEQ_IN_INDEX", mysql.TypeLonglong, 2, 0, nil, nil},
{"COLUMN_NAME", mysql.TypeVarchar, 21, 0, nil, nil},
{"COLLATION", mysql.TypeVarchar, 1, 0, nil, nil},
{"CARDINALITY", mysql.TypeLonglong, 21, 0, nil, nil},
{"SUB_PART", mysql.TypeLonglong, 3, 0, nil, nil},
{"PACKED", mysql.TypeVarchar, 10, 0, nil, nil},
{"NULLABLE", mysql.TypeVarchar, 3, 0, nil, nil},
{"INDEX_TYPE", mysql.TypeVarchar, 16, 0, nil, nil},
{"COMMENT", mysql.TypeVarchar, 16, 0, nil, nil},
{"INDEX_COMMENT", mysql.TypeVarchar, 1024, 0, nil, nil},
}
var profilingCols = []columnInfo{
{"QUERY_ID", mysql.TypeLong, 20, 0, nil, nil},
{"SEQ", mysql.TypeLong, 20, 0, nil, nil},
{"STATE", mysql.TypeVarchar, 30, 0, nil, nil},
{"DURATION", mysql.TypeNewDecimal, 9, 0, nil, nil},
{"CPU_USER", mysql.TypeNewDecimal, 9, 0, nil, nil},
{"CPU_SYSTEM", mysql.TypeNewDecimal, 9, 0, nil, nil},
{"CONTEXT_VOLUNTARY", mysql.TypeLong, 20, 0, nil, nil},
{"CONTEXT_INVOLUNTARY", mysql.TypeLong, 20, 0, nil, nil},
{"BLOCK_OPS_IN", mysql.TypeLong, 20, 0, nil, nil},
{"BLOCK_OPS_OUT", mysql.TypeLong, 20, 0, nil, nil},
{"MESSAGES_SENT", mysql.TypeLong, 20, 0, nil, nil},
{"MESSAGES_RECEIVED", mysql.TypeLong, 20, 0, nil, nil},
{"PAGE_FAULTS_MAJOR", mysql.TypeLong, 20, 0, nil, nil},
{"PAGE_FAULTS_MINOR", mysql.TypeLong, 20, 0, nil, nil},
{"SWAPS", mysql.TypeLong, 20, 0, nil, nil},
{"SOURCE_FUNCTION", mysql.TypeVarchar, 30, 0, nil, nil},
{"SOURCE_FILE", mysql.TypeVarchar, 20, 0, nil, nil},
{"SOURCE_LINE", mysql.TypeLong, 20, 0, nil, nil},
}
var charsetCols = []columnInfo{
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"DEFAULT_COLLATE_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"DESCRIPTION", mysql.TypeVarchar, 60, 0, nil, nil},
{"MAXLEN", mysql.TypeLonglong, 3, 0, nil, nil},
}
var collationsCols = []columnInfo{
{"COLLATION_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 32, 0, nil, nil},
{"ID", mysql.TypeLonglong, 11, 0, nil, nil},
{"IS_DEFAULT", mysql.TypeVarchar, 3, 0, nil, nil},
{"IS_COMPILED", mysql.TypeVarchar, 3, 0, nil, nil},
{"SORTLEN", mysql.TypeLonglong, 3, 0, nil, nil},
}
var keyColumnUsageCols = []columnInfo{
{"CONSTRAINT_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"CONSTRAINT_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"CONSTRAINT_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"COLUMN_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ORDINAL_POSITION", mysql.TypeLonglong, 10, mysql.NotNullFlag, nil, nil},
{"POSITION_IN_UNIQUE_CONSTRAINT", mysql.TypeLonglong, 10, 0, nil, nil},
{"REFERENCED_TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"REFERENCED_TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"REFERENCED_COLUMN_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
}
// See http://dev.mysql.com/doc/refman/5.7/en/referential-constraints-table.html
var referConstCols = []columnInfo{
{"CONSTRAINT_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"CONSTRAINT_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"CONSTRAINT_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"UNIQUE_CONSTRAINT_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"UNIQUE_CONSTRAINT_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"UNIQUE_CONSTRAINT_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"MATCH_OPTION", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"UPDATE_RULE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"DELETE_RULE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"REFERENCED_TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
}
// See http://dev.mysql.com/doc/refman/5.7/en/variables-table.html
var sessionVarCols = []columnInfo{
{"VARIABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"VARIABLE_VALUE", mysql.TypeVarchar, 1024, 0, nil, nil},
}
// See https://dev.mysql.com/doc/refman/5.7/en/plugins-table.html
var pluginsCols = []columnInfo{
{"PLUGIN_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"PLUGIN_VERSION", mysql.TypeVarchar, 20, 0, nil, nil},
{"PLUGIN_STATUS", mysql.TypeVarchar, 10, 0, nil, nil},
{"PLUGIN_TYPE", mysql.TypeVarchar, 80, 0, nil, nil},
{"PLUGIN_TYPE_VERSION", mysql.TypeVarchar, 20, 0, nil, nil},
{"PLUGIN_LIBRARY", mysql.TypeVarchar, 64, 0, nil, nil},
{"PLUGIN_LIBRARY_VERSION", mysql.TypeVarchar, 20, 0, nil, nil},
{"PLUGIN_AUTHOR", mysql.TypeVarchar, 64, 0, nil, nil},
{"PLUGIN_DESCRIPTION", mysql.TypeLongBlob, types.UnspecifiedLength, 0, nil, nil},
{"PLUGIN_LICENSE", mysql.TypeVarchar, 80, 0, nil, nil},
{"LOAD_OPTION", mysql.TypeVarchar, 64, 0, nil, nil},
}
// See https://dev.mysql.com/doc/refman/5.7/en/partitions-table.html
var partitionsCols = []columnInfo{
{"TABLE_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"PARTITION_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"SUBPARTITION_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"PARTITION_ORDINAL_POSITION", mysql.TypeLonglong, 21, 0, nil, nil},
{"SUBPARTITION_ORDINAL_POSITION", mysql.TypeLonglong, 21, 0, nil, nil},
{"PARTITION_METHOD", mysql.TypeVarchar, 18, 0, nil, nil},
{"SUBPARTITION_METHOD", mysql.TypeVarchar, 12, 0, nil, nil},
{"PARTITION_EXPRESSION", mysql.TypeLongBlob, types.UnspecifiedLength, 0, nil, nil},
{"SUBPARTITION_EXPRESSION", mysql.TypeLongBlob, types.UnspecifiedLength, 0, nil, nil},
{"PARTITION_DESCRIPTION", mysql.TypeLongBlob, types.UnspecifiedLength, 0, nil, nil},
{"TABLE_ROWS", mysql.TypeLonglong, 21, 0, nil, nil},
{"AVG_ROW_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATA_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"MAX_DATA_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"INDEX_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATA_FREE", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATE_TIME", mysql.TypeDatetime, 0, 0, nil, nil},
{"UPDATE_TIME", mysql.TypeDatetime, 0, 0, nil, nil},
{"CHECK_TIME", mysql.TypeDatetime, 0, 0, nil, nil},
{"CHECKSUM", mysql.TypeLonglong, 21, 0, nil, nil},
{"PARTITION_COMMENT", mysql.TypeVarchar, 80, 0, nil, nil},
{"NODEGROUP", mysql.TypeVarchar, 12, 0, nil, nil},
{"TABLESPACE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
}
var tableConstraintsCols = []columnInfo{
{"CONSTRAINT_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"CONSTRAINT_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"CONSTRAINT_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"CONSTRAINT_TYPE", mysql.TypeVarchar, 64, 0, nil, nil},
}
var tableTriggersCols = []columnInfo{
{"TRIGGER_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"TRIGGER_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TRIGGER_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"EVENT_MANIPULATION", mysql.TypeVarchar, 6, 0, nil, nil},
{"EVENT_OBJECT_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"EVENT_OBJECT_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"EVENT_OBJECT_TABLE", mysql.TypeVarchar, 64, 0, nil, nil},
{"ACTION_ORDER", mysql.TypeLonglong, 4, 0, nil, nil},
{"ACTION_CONDITION", mysql.TypeBlob, -1, 0, nil, nil},
{"ACTION_STATEMENT", mysql.TypeBlob, -1, 0, nil, nil},
{"ACTION_ORIENTATION", mysql.TypeVarchar, 9, 0, nil, nil},
{"ACTION_TIMING", mysql.TypeVarchar, 6, 0, nil, nil},
{"ACTION_REFERENCE_OLD_TABLE", mysql.TypeVarchar, 64, 0, nil, nil},
{"ACTION_REFERENCE_NEW_TABLE", mysql.TypeVarchar, 64, 0, nil, nil},
{"ACTION_REFERENCE_OLD_ROW", mysql.TypeVarchar, 3, 0, nil, nil},
{"ACTION_REFERENCE_NEW_ROW", mysql.TypeVarchar, 3, 0, nil, nil},
{"CREATED", mysql.TypeDatetime, 2, 0, nil, nil},
{"SQL_MODE", mysql.TypeVarchar, 8192, 0, nil, nil},
{"DEFINER", mysql.TypeVarchar, 77, 0, nil, nil},
{"CHARACTER_SET_CLIENT", mysql.TypeVarchar, 32, 0, nil, nil},
{"COLLATION_CONNECTION", mysql.TypeVarchar, 32, 0, nil, nil},
{"DATABASE_COLLATION", mysql.TypeVarchar, 32, 0, nil, nil},
}
var tableUserPrivilegesCols = []columnInfo{
{"GRANTEE", mysql.TypeVarchar, 81, 0, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 512, 0, nil, nil},
{"PRIVILEGE_TYPE", mysql.TypeVarchar, 64, 0, nil, nil},
{"IS_GRANTABLE", mysql.TypeVarchar, 3, 0, nil, nil},
}
var tableSchemaPrivilegesCols = []columnInfo{
{"GRANTEE", mysql.TypeVarchar, 81, mysql.NotNullFlag, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"PRIVILEGE_TYPE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"IS_GRANTABLE", mysql.TypeVarchar, 3, mysql.NotNullFlag, nil, nil},
}
var tableTablePrivilegesCols = []columnInfo{
{"GRANTEE", mysql.TypeVarchar, 81, mysql.NotNullFlag, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"PRIVILEGE_TYPE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"IS_GRANTABLE", mysql.TypeVarchar, 3, mysql.NotNullFlag, nil, nil},
}
var tableColumnPrivilegesCols = []columnInfo{
{"GRANTEE", mysql.TypeVarchar, 81, mysql.NotNullFlag, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"COLUMN_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"PRIVILEGE_TYPE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"IS_GRANTABLE", mysql.TypeVarchar, 3, mysql.NotNullFlag, nil, nil},
}
var tableEnginesCols = []columnInfo{
{"ENGINE", mysql.TypeVarchar, 64, 0, nil, nil},
{"SUPPORT", mysql.TypeVarchar, 8, 0, nil, nil},
{"COMMENT", mysql.TypeVarchar, 80, 0, nil, nil},
{"TRANSACTIONS", mysql.TypeVarchar, 3, 0, nil, nil},
{"XA", mysql.TypeVarchar, 3, 0, nil, nil},
{"SAVEPOINTS", mysql.TypeVarchar, 3, 0, nil, nil},
}
var tableViewsCols = []columnInfo{
{"TABLE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"VIEW_DEFINITION", mysql.TypeLongBlob, 0, mysql.NotNullFlag, nil, nil},
{"CHECK_OPTION", mysql.TypeVarchar, 8, mysql.NotNullFlag, nil, nil},
{"IS_UPDATABLE", mysql.TypeVarchar, 3, mysql.NotNullFlag, nil, nil},
{"DEFINER", mysql.TypeVarchar, 77, mysql.NotNullFlag, nil, nil},
{"SECURITY_TYPE", mysql.TypeVarchar, 7, mysql.NotNullFlag, nil, nil},
{"CHARACTER_SET_CLIENT", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"COLLATION_CONNECTION", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
}
var tableRoutinesCols = []columnInfo{
{"SPECIFIC_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ROUTINE_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"ROUTINE_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ROUTINE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ROUTINE_TYPE", mysql.TypeVarchar, 9, mysql.NotNullFlag, nil, nil},
{"DATA_TYPE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"CHARACTER_MAXIMUM_LENGTH", mysql.TypeLong, 21, 0, nil, nil},
{"CHARACTER_OCTET_LENGTH", mysql.TypeLong, 21, 0, nil, nil},
{"NUMERIC_PRECISION", mysql.TypeLonglong, 21, 0, nil, nil},
{"NUMERIC_SCALE", mysql.TypeLong, 21, 0, nil, nil},
{"DATETIME_PRECISION", mysql.TypeLonglong, 21, 0, nil, nil},
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"COLLATION_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"DTD_IDENTIFIER", mysql.TypeLongBlob, 0, 0, nil, nil},
{"ROUTINE_BODY", mysql.TypeVarchar, 8, mysql.NotNullFlag, nil, nil},
{"ROUTINE_DEFINITION", mysql.TypeLongBlob, 0, 0, nil, nil},
{"EXTERNAL_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"EXTERNAL_LANGUAGE", mysql.TypeVarchar, 64, 0, nil, nil},
{"PARAMETER_STYLE", mysql.TypeVarchar, 8, mysql.NotNullFlag, nil, nil},
{"IS_DETERMINISTIC", mysql.TypeVarchar, 3, mysql.NotNullFlag, nil, nil},
{"SQL_DATA_ACCESS", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"SQL_PATH", mysql.TypeVarchar, 64, 0, nil, nil},
{"SECURITY_TYPE", mysql.TypeVarchar, 7, mysql.NotNullFlag, nil, nil},
{"CREATED", mysql.TypeDatetime, 0, mysql.NotNullFlag, "0000-00-00 00:00:00", nil},
{"LAST_ALTERED", mysql.TypeDatetime, 0, mysql.NotNullFlag, "0000-00-00 00:00:00", nil},
{"SQL_MODE", mysql.TypeVarchar, 8192, mysql.NotNullFlag, nil, nil},
{"ROUTINE_COMMENT", mysql.TypeLongBlob, 0, 0, nil, nil},
{"DEFINER", mysql.TypeVarchar, 77, mysql.NotNullFlag, nil, nil},
{"CHARACTER_SET_CLIENT", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"COLLATION_CONNECTION", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"DATABASE_COLLATION", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
}
var tableParametersCols = []columnInfo{
{"SPECIFIC_CATALOG", mysql.TypeVarchar, 512, mysql.NotNullFlag, nil, nil},
{"SPECIFIC_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"SPECIFIC_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ORDINAL_POSITION", mysql.TypeVarchar, 21, mysql.NotNullFlag, nil, nil},
{"PARAMETER_MODE", mysql.TypeVarchar, 5, 0, nil, nil},
{"PARAMETER_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"DATA_TYPE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"CHARACTER_MAXIMUM_LENGTH", mysql.TypeVarchar, 21, 0, nil, nil},
{"CHARACTER_OCTET_LENGTH", mysql.TypeVarchar, 21, 0, nil, nil},
{"NUMERIC_PRECISION", mysql.TypeVarchar, 21, 0, nil, nil},
{"NUMERIC_SCALE", mysql.TypeVarchar, 21, 0, nil, nil},
{"DATETIME_PRECISION", mysql.TypeVarchar, 21, 0, nil, nil},
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"COLLATION_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"DTD_IDENTIFIER", mysql.TypeLongBlob, 0, mysql.NotNullFlag, nil, nil},
{"ROUTINE_TYPE", mysql.TypeVarchar, 9, mysql.NotNullFlag, nil, nil},
}
var tableEventsCols = []columnInfo{
{"EVENT_CATALOG", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"EVENT_SCHEMA", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"EVENT_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"DEFINER", mysql.TypeVarchar, 77, mysql.NotNullFlag, nil, nil},
{"TIME_ZONE", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"EVENT_BODY", mysql.TypeVarchar, 8, mysql.NotNullFlag, nil, nil},
{"EVENT_DEFINITION", mysql.TypeLongBlob, 0, 0, nil, nil},
{"EVENT_TYPE", mysql.TypeVarchar, 9, mysql.NotNullFlag, nil, nil},
{"EXECUTE_AT", mysql.TypeDatetime, 0, 0, nil, nil},
{"INTERVAL_VALUE", mysql.TypeVarchar, 256, 0, nil, nil},
{"INTERVAL_FIELD", mysql.TypeVarchar, 18, 0, nil, nil},
{"SQL_MODE", mysql.TypeVarchar, 8192, mysql.NotNullFlag, nil, nil},
{"STARTS", mysql.TypeDatetime, 0, 0, nil, nil},
{"ENDS", mysql.TypeDatetime, 0, 0, nil, nil},
{"STATUS", mysql.TypeVarchar, 18, mysql.NotNullFlag, nil, nil},
{"ON_COMPLETION", mysql.TypeVarchar, 12, mysql.NotNullFlag, nil, nil},
{"CREATED", mysql.TypeDatetime, 0, mysql.NotNullFlag, "0000-00-00 00:00:00", nil},
{"LAST_ALTERED", mysql.TypeDatetime, 0, mysql.NotNullFlag, "0000-00-00 00:00:00", nil},
{"LAST_EXECUTED", mysql.TypeDatetime, 0, 0, nil, nil},
{"EVENT_COMMENT", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"ORIGINATOR", mysql.TypeLong, 10, mysql.NotNullFlag, 0, nil},
{"CHARACTER_SET_CLIENT", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"COLLATION_CONNECTION", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"DATABASE_COLLATION", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
}
var tableGlobalStatusCols = []columnInfo{
{"VARIABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"VARIABLE_VALUE", mysql.TypeVarchar, 1024, 0, nil, nil},
}
var tableGlobalVariablesCols = []columnInfo{
{"VARIABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"VARIABLE_VALUE", mysql.TypeVarchar, 1024, 0, nil, nil},
}
var tableSessionStatusCols = []columnInfo{
{"VARIABLE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, nil, nil},
{"VARIABLE_VALUE", mysql.TypeVarchar, 1024, 0, nil, nil},
}
var tableOptimizerTraceCols = []columnInfo{
{"QUERY", mysql.TypeLongBlob, 0, mysql.NotNullFlag, "", nil},
{"TRACE", mysql.TypeLongBlob, 0, mysql.NotNullFlag, "", nil},
{"MISSING_BYTES_BEYOND_MAX_MEM_SIZE", mysql.TypeShort, 20, mysql.NotNullFlag, 0, nil},
{"INSUFFICIENT_PRIVILEGES", mysql.TypeTiny, 1, mysql.NotNullFlag, 0, nil},
}
var tableTableSpacesCols = []columnInfo{
{"TABLESPACE_NAME", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil},
{"ENGINE", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil},
{"TABLESPACE_TYPE", mysql.TypeVarchar, 64, 0, nil, nil},
{"LOGFILE_GROUP_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"EXTENT_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"AUTOEXTEND_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"MAXIMUM_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"NODEGROUP_ID", mysql.TypeLonglong, 21, 0, nil, nil},
{"TABLESPACE_COMMENT", mysql.TypeVarchar, 2048, 0, nil, nil},
}
var tableCollationCharacterSetApplicabilityCols = []columnInfo{
{"COLLATION_NAME", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
}
func dataForCharacterSets() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("ascii", "ascii_general_ci", "US ASCII", 1),
types.MakeDatums("binary", "binary", "Binary pseudo charset", 1),
types.MakeDatums("latin1", "latin1_swedish_ci", "cp1252 West European", 1),
types.MakeDatums("utf8", "utf8_general_ci", "UTF-8 Unicode", 3),
types.MakeDatums("utf8mb4", "utf8mb4_general_ci", "UTF-8 Unicode", 4),
)
return records
}
func dataForColltions() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("ascii_general_ci", "ascii", 1, "Yes", "Yes", 1),
types.MakeDatums("binary", "binary", 2, "Yes", "Yes", 1),
types.MakeDatums("latin1_swedish_ci", "latin1", 3, "Yes", "Yes", 1),
types.MakeDatums("utf8_general_ci", "utf8", 4, "Yes", "Yes", 1),
types.MakeDatums("utf8mb4_general_ci", "utf8mb4", 5, "Yes", "Yes", 1),
)
return records
}
func dataForCollationCharacterSetApplicability() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("ascii_general_ci", "ascii"),
types.MakeDatums("binary", "binary"),
types.MakeDatums("latin1_swedish_ci", "latin1"),
types.MakeDatums("utf8_general_ci", "utf8"),
types.MakeDatums("utf8_bin", "utf8"),
types.MakeDatums("utf8mb4_general_ci", "utf8mb4"),
)
return records
}
func dataForSessionVar(ctx sessionctx.Context) (records [][]types.Datum, err error) {
sessionVars := ctx.GetSessionVars()
for _, v := range variable.SysVars {
var value string
value, err = variable.GetSessionSystemVar(sessionVars, v.Name)
if err != nil {
return nil, errors.Trace(err)
}
row := types.MakeDatums(v.Name, value)
records = append(records, row)
}
return
}
func dataForUserPrivileges(ctx sessionctx.Context) [][]types.Datum {
pm := privilege.GetPrivilegeManager(ctx)
return pm.UserPrivilegesTable()
}
func dataForEngines() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("InnoDB", "DEFAULT", "Supports transactions, row-level locking, and foreign keys", "YES", "YES", "YES"),
types.MakeDatums("CSV", "YES", "CSV storage engine", "NO", "NO", "NO"),
types.MakeDatums("MRG_MYISAM", "YES", "Collection of identical MyISAM tables", "NO", "NO", "NO"),
types.MakeDatums("BLACKHOLE", "YES", "/dev/null storage engine (anything you write to it disappears)", "NO", "NO", "NO"),
types.MakeDatums("MyISAM", "YES", "MyISAM storage engine", "NO", "NO", "NO"),
types.MakeDatums("MEMORY", "YES", "Hash based, stored in memory, useful for temporary tables", "NO", "NO", "NO"),
types.MakeDatums("ARCHIVE", "YES", "Archive storage engine", "NO", "NO", "NO"),
types.MakeDatums("FEDERATED", "NO", "Federated MySQL storage engine", nil, nil, nil),
types.MakeDatums("PERFORMANCE_SCHEMA", "YES", "Performance Schema", "NO", "NO", "NO"),
)
return records
}
var filesCols = []columnInfo{
{"FILE_ID", mysql.TypeLonglong, 4, 0, nil, nil},
{"FILE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"FILE_TYPE", mysql.TypeVarchar, 20, 0, nil, nil},
{"TABLESPACE_NAME", mysql.TypeVarchar, 20, 0, nil, nil},
{"TABLE_CATALOG", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"LOGFILE_GROUP_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"LOGFILE_GROUP_NUMBER", mysql.TypeLonglong, 32, 0, nil, nil},
{"ENGINE", mysql.TypeVarchar, 64, 0, nil, nil},
{"FULLTEXT_KEYS", mysql.TypeVarchar, 64, 0, nil, nil},
{"DELETED_ROWS", mysql.TypeLonglong, 4, 0, nil, nil},
{"UPDATE_COUNT", mysql.TypeLonglong, 4, 0, nil, nil},
{"FREE_EXTENTS", mysql.TypeLonglong, 4, 0, nil, nil},
{"TOTAL_EXTENTS", mysql.TypeLonglong, 4, 0, nil, nil},
{"EXTENT_SIZE", mysql.TypeLonglong, 4, 0, nil, nil},
{"INITIAL_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"MAXIMUM_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"AUTOEXTEND_SIZE", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATION_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"LAST_UPDATE_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"LAST_ACCESS_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"RECOVER_TIME", mysql.TypeLonglong, 4, 0, nil, nil},
{"TRANSACTION_COUNTER", mysql.TypeLonglong, 4, 0, nil, nil},
{"VERSION", mysql.TypeLonglong, 21, 0, nil, nil},
{"ROW_FORMAT", mysql.TypeVarchar, 21, 0, nil, nil},
{"TABLE_ROWS", mysql.TypeLonglong, 21, 0, nil, nil},
{"AVG_ROW_LENGTH", mysql.TypeLonglong, 21, 0, nil, nil},
{"DATA_FREE", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATE_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"UPDATE_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"CHECK_TIME", mysql.TypeDatetime, -1, 0, nil, nil},
{"CHECKSUM", mysql.TypeLonglong, 21, 0, nil, nil},
{"STATUS", mysql.TypeVarchar, 20, 0, nil, nil},
{"EXTRA", mysql.TypeVarchar, 255, 0, nil, nil},
}
func dataForSchemata(schemas []*model.DBInfo) [][]types.Datum {
var rows [][]types.Datum
for _, schema := range schemas {
record := types.MakeDatums(
catalogVal, // CATALOG_NAME
schema.Name.O, // SCHEMA_NAME
mysql.DefaultCharset, // DEFAULT_CHARACTER_SET_NAME
mysql.DefaultCollationName, // DEFAULT_COLLATION_NAME
nil,
)
rows = append(rows, record)
}
return rows
}
func getRowCountAllTable(ctx sessionctx.Context) (map[int64]uint64, error) {
rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, "select table_id, count from mysql.stats_meta")
if err != nil {
return nil, errors.Trace(err)
}
rowCountMap := make(map[int64]uint64, len(rows))
for _, row := range rows {
tableID := row.GetInt64(0)
rowCnt := row.GetUint64(1)
rowCountMap[tableID] = rowCnt
}
return rowCountMap, nil
}
func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
tableRowsMap, err := getRowCountAllTable(ctx)
if err != nil {
return nil, errors.Trace(err)
}
var rows [][]types.Datum
createTimeTp := tablesCols[15].tp
for _, schema := range schemas {
for _, table := range schema.Tables {
collation := table.Collate
if collation == "" {
collation = charset.CollationUTF8
}
createTime := types.Time{
Time: types.FromGoTime(table.GetUpdateTime()),
Type: createTimeTp,
}
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
"BASE TABLE", // TABLE_TYPE
"InnoDB", // ENGINE
uint64(10), // VERSION
"Compact", // ROW_FORMAT
tableRowsMap[table.ID], // TABLE_ROWS
uint64(0), // AVG_ROW_LENGTH
uint64(16384), // DATA_LENGTH
uint64(0), // MAX_DATA_LENGTH
uint64(0), // INDEX_LENGTH
uint64(0), // DATA_FREE
table.AutoIncID, // AUTO_INCREMENT
createTime, // CREATE_TIME
nil, // UPDATE_TIME
nil, // CHECK_TIME
collation, // TABLE_COLLATION
nil, // CHECKSUM
"", // CREATE_OPTIONS
table.Comment, // TABLE_COMMENT
)
rows = append(rows, record)
}
}
return rows, nil
}
func dataForColumns(schemas []*model.DBInfo) [][]types.Datum {
var rows [][]types.Datum
for _, schema := range schemas {
for _, table := range schema.Tables {
rs := dataForColumnsInTable(schema, table)
rows = append(rows, rs...)
}
}
return rows
}
func dataForColumnsInTable(schema *model.DBInfo, tbl *model.TableInfo) [][]types.Datum {
var rows [][]types.Datum
for i, col := range tbl.Columns {
colLen, decimal := col.Flen, col.Decimal
defaultFlen, defaultDecimal := mysql.GetDefaultFieldLengthAndDecimal(col.Tp)
if colLen == types.UnspecifiedLength {
colLen = defaultFlen
}
if decimal == types.UnspecifiedLength {
decimal = defaultDecimal
}
columnType := col.FieldType.InfoSchemaStr()
columnDesc := table.NewColDesc(table.ToColumn(col))
var columnDefault interface{}
if columnDesc.DefaultValue != nil {
columnDefault = fmt.Sprintf("%v", columnDesc.DefaultValue)
}
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
i+1, // ORIGINAL_POSITION
columnDefault, // COLUMN_DEFAULT
columnDesc.Null, // IS_NULLABLE
types.TypeToStr(col.Tp, col.Charset), // DATA_TYPE
colLen, // CHARACTER_MAXIMUM_LENGTH
colLen, // CHARACTER_OCTET_LENGTH
decimal, // NUMERIC_PRECISION
0, // NUMERIC_SCALE
0, // DATETIME_PRECISION
col.Charset, // CHARACTER_SET_NAME
col.Collate, // COLLATION_NAME
columnType, // COLUMN_TYPE
columnDesc.Key, // COLUMN_KEY
columnDesc.Extra, // EXTRA
"select,insert,update,references", // PRIVILEGES
columnDesc.Comment, // COLUMN_COMMENT
)
// In mysql, 'character_set_name' and 'collation_name' are setted to null when column type is non-varchar or non-blob in information_schema.
if col.Tp != mysql.TypeVarchar && col.Tp != mysql.TypeBlob {
record[13].SetNull()
record[14].SetNull()
}
rows = append(rows, record)
}
return rows
}
func dataForStatistics(schemas []*model.DBInfo) [][]types.Datum {
var rows [][]types.Datum
for _, schema := range schemas {
for _, table := range schema.Tables {
rs := dataForStatisticsInTable(schema, table)
rows = append(rows, rs...)
}
}
return rows
}
func dataForStatisticsInTable(schema *model.DBInfo, table *model.TableInfo) [][]types.Datum {
var rows [][]types.Datum
if table.PKIsHandle {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
"0", // NON_UNIQUE
schema.Name.O, // INDEX_SCHEMA
"PRIMARY", // INDEX_NAME
1, // SEQ_IN_INDEX
col.Name.O, // COLUMN_NAME
"A", // COLLATION
0, // CARDINALITY
nil, // SUB_PART
nil, // PACKED
"", // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
"", // INDEX_COMMENT
)
rows = append(rows, record)
}
}
}
nameToCol := make(map[string]*model.ColumnInfo, len(table.Columns))
for _, c := range table.Columns {
nameToCol[c.Name.L] = c
}
for _, index := range table.Indices {
nonUnique := "1"
if index.Unique {
nonUnique = "0"
}
for i, key := range index.Columns {
col := nameToCol[key.Name.L]
nullable := "YES"
if mysql.HasNotNullFlag(col.Flag) {
nullable = ""
}
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
nonUnique, // NON_UNIQUE
schema.Name.O, // INDEX_SCHEMA
index.Name.O, // INDEX_NAME
i+1, // SEQ_IN_INDEX
key.Name.O, // COLUMN_NAME
"A", // COLLATION
0, // CARDINALITY
nil, // SUB_PART
nil, // PACKED
nullable, // NULLABLE
"BTREE", // INDEX_TYPE
"", // COMMENT
"", // INDEX_COMMENT
)
rows = append(rows, record)
}
}
return rows
}
const (
primaryKeyType = "PRIMARY KEY"
primaryConstraint = "PRIMARY"
uniqueKeyType = "UNIQUE"
)
// dataForTableConstraints constructs data for table information_schema.constraints.See https://dev.mysql.com/doc/refman/5.7/en/table-constraints-table.html
func dataForTableConstraints(schemas []*model.DBInfo) [][]types.Datum {
var rows [][]types.Datum
for _, schema := range schemas {
for _, tbl := range schema.Tables {
if tbl.PKIsHandle {
record := types.MakeDatums(
catalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
mysql.PrimaryKeyName, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
primaryKeyType, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}
for _, idx := range tbl.Indices {
var cname, ctype string
if idx.Primary {
cname = mysql.PrimaryKeyName
ctype = primaryKeyType
} else if idx.Unique {
cname = idx.Name.O
ctype = uniqueKeyType
} else {
// The index has no constriant.
continue
}
record := types.MakeDatums(
catalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
cname, // CONSTRAINT_NAME
schema.Name.O, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
ctype, // CONSTRAINT_TYPE
)
rows = append(rows, record)
}
}
}
return rows
}
// dataForPseudoProfiling returns pseudo data for table profiling when system variable `profiling` is set to `ON`.
func dataForPseudoProfiling() [][]types.Datum {
var rows [][]types.Datum
row := types.MakeDatums(
0, // QUERY_ID
0, // SEQ
"", // STATE
types.NewDecFromInt(0), // DURATION
types.NewDecFromInt(0), // CPU_USER
types.NewDecFromInt(0), // CPU_SYSTEM
0, // CONTEXT_VOLUNTARY
0, // CONTEXT_INVOLUNTARY
0, // BLOCK_OPS_IN
0, // BLOCK_OPS_OUT
0, // MESSAGES_SENT
0, // MESSAGES_RECEIVED
0, // PAGE_FAULTS_MAJOR
0, // PAGE_FAULTS_MINOR
0, // SWAPS
0, // SOURCE_FUNCTION
0, // SOURCE_FILE
0, // SOURCE_LINE
)
rows = append(rows, row)
return rows
}
func dataForKeyColumnUsage(schemas []*model.DBInfo) [][]types.Datum {
rows := make([][]types.Datum, 0, len(schemas)) // The capacity is not accurate, but it is not a big problem.
for _, schema := range schemas {
for _, table := range schema.Tables {
rs := keyColumnUsageInTable(schema, table)
rows = append(rows, rs...)
}
}
return rows
}
func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]types.Datum {
var rows [][]types.Datum
if table.PKIsHandle {
for _, col := range table.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
record := types.MakeDatums(
catalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
primaryConstraint, // CONSTRAINT_NAME
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
1, // ORDINAL_POSITION
1, // POSITION_IN_UNIQUE_CONSTRAINT
nil, // REFERENCED_TABLE_SCHEMA
nil, // REFERENCED_TABLE_NAME
nil, // REFERENCED_COLUMN_NAME
)
rows = append(rows, record)
break
}
}
}
nameToCol := make(map[string]*model.ColumnInfo, len(table.Columns))
for _, c := range table.Columns {
nameToCol[c.Name.L] = c
}
for _, index := range table.Indices {
var idxName string
if index.Primary {
idxName = primaryConstraint
} else if index.Unique {
idxName = index.Name.O
} else {
// Only handle unique/primary key
continue
}
for i, key := range index.Columns {
col := nameToCol[key.Name.L]
record := types.MakeDatums(
catalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
idxName, // CONSTRAINT_NAME
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
i+1, // ORDINAL_POSITION,
nil, // POSITION_IN_UNIQUE_CONSTRAINT
nil, // REFERENCED_TABLE_SCHEMA
nil, // REFERENCED_TABLE_NAME
nil, // REFERENCED_COLUMN_NAME
)
rows = append(rows, record)
}
}
for _, fk := range table.ForeignKeys {
fkRefCol := ""
if len(fk.RefCols) > 0 {
fkRefCol = fk.RefCols[0].O
}
for i, key := range fk.Cols {
col := nameToCol[key.L]
record := types.MakeDatums(
catalogVal, // CONSTRAINT_CATALOG
schema.Name.O, // CONSTRAINT_SCHEMA
fk.Name.O, // CONSTRAINT_NAME
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
col.Name.O, // COLUMN_NAME
i+1, // ORDINAL_POSITION,
1, // POSITION_IN_UNIQUE_CONSTRAINT
schema.Name.O, // REFERENCED_TABLE_SCHEMA