-
Notifications
You must be signed in to change notification settings - Fork 0
/
dblint_datarules.sql
1074 lines (877 loc) · 36.6 KB
/
dblint_datarules.sql
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
-- Data Rules
-- The data rules are rules that pertain to the contents of the database, except rule 00.
-- Set a few variables
set @max_rows = 1000000; -- maximum amount of rows in table to test for duplication
delimiter //
drop procedure if exists dblint_debug //
create procedure dblint_debug (message varchar(255))
comment 'Store Dblint debugging messages'
begin
insert into dblint_debug select 0, message;
end;
-- call dblint_debug (concat('myvar is: ', myvar, ' and myvar2 is: ', myvar2));
-- Rule 00: Drop non-InnoDB tables
drop procedure if exists dblint_rule00 //
create procedure dblint_rule00 (in schema_ varchar(32))
comment 'Drop all non-InnoDB tables (ie temporary tables), so that dblint works on actual data'
begin
declare table_ varchar(32);
declare number_of_tables integer default 0;
declare i integer default 1;
declare T cursor for select table_name from information_schema.tables where table_schema = schema_ and engine <> 'InnoDB';
open T;
select found_rows() into number_of_tables;
select table_name, engine from information_schema.tables where table_schema = schema_ and engine <> 'InnoDB';
tableLoop: loop
if i > number_of_tables then
leave tableLoop;
end if;
fetch T into table_;
set @v_sql = concat('drop table ', schema_, '.', table_, ';');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
set i = i + 1;
end loop tableLoop;
close T;
end;
//
-- Rule 28: Duplicate rows in a table
-- One million rows takes about 10 mins.
drop procedure if exists dblint_rule28 //
create procedure dblint_rule28 (in schema_ varchar(32))
comment 'Duplicate rows in a table'
begin
declare rule_ integer default 28;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows between 1 and @max_rows;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
select table_name, table_rows, 'Check manually' as message from information_schema.tables where table_schema = schema_ and table_rows > @max_rows;
tableLoop: while i < number_of_objects do
fetch cursorT into table_;
-- the contrived concat prevents the prepared statement from having to deal with reserved words
set @columnnames = (select concat('`', group_concat(column_name separator '`, `'), '`') from information_schema.columns where table_schema = schema_ and table_name = table_ and column_key <> 'PRI' order by ordinal_position);
set @v_sql = concat('select "', table_, '" as table_name, ', @columnnames, ' from ', schema_, '.', table_, ' group by ', @columnnames, ' having count(*) > 1');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
set i = i + 1;
end while tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 29: Storing lists in varchar columns
-- List elements are separated by either comma (,) or semicolon (;).
drop procedure if exists dblint_rule29 //
create procedure dblint_rule29 (in schema_ varchar(32))
comment 'Storing lists in varchar columns'
begin
declare rule_ integer default 29;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_name not like 'dblint%' and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- set @v_sql = concat('select "', table_, '" as table_name, ', column_, ', count(*) from ', schema_, '.', table_, ' where ', column_, ' regexp "([a-za-z0-9]+[,; ])+" group by ', column_);
set @v_sql = concat('insert into dblint_results (id, table_name, column_name, message) select 29, "', table_, '", "', column_, '", "Contains list" from ', schema_, '.', table_, ' where ', column_, ' regexp "([a-za-z0-9]+[,;])+" group by ', column_);
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 31: Defined primary key is not a minimal key
drop procedure if exists dblint_rule31 //
create procedure dblint_rule31 (in schema_ varchar(32))
comment 'Defined primary key is not a minimal key'
begin
declare rule_ integer default 31;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.columns where table_schema = schema_ and column_key = 'PRI' group by table_name;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: while i < number_of_objects do
fetch cursorT into table_;
set @totalrows = (select table_rows from information_schema.tables where table_schema = schema_ and table_name = table_);
if @totalrows = 0 then
set i = i + 1;
iterate tableLoop;
end if;
set @joinon = (select group_concat('t1.', column_name, ' = t2.', column_name) from information_schema.columns where table_schema = schema_ and table_name = table_ and column_key <> 'PRI');
set @joinon = replace(@joinon, ',', ' and ');
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' as t1 join ', schema_, '.', table_, ' as t2 on ', @joinon, ' into @matchrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @matchrows > @totalrows then
insert into dblint_results (id, table_name, message) select rule_, table_, 'Defined primary key is not a minimal key';
end if;
set i = i + 1;
end while tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 32: Redundant columns
drop procedure if exists dblint_rule32 //
create procedure dblint_rule32 (in schema_ varchar(32))
comment 'Redundant columns'
begin
declare rule_ integer default 32;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: while i < number_of_objects do
fetch cursorT into table_;
begin
declare column01 varchar(64);
declare numcolumns1 integer default 0;
declare j1 integer default 1;
declare cursorC1 cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ order by ordinal_position;
open cursorC1;
select found_rows() into numcolumns1;
columnLoop1: while j1 < numcolumns1 do
fetch cursorC1 into column01;
begin
declare column02 varchar(64);
declare numcolumns2 integer default 0;
declare j2 integer default 1;
declare cursorC2 cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and column_name > column01 order by ordinal_position;
open cursorC2;
select found_rows() into numcolumns2;
columnLoop2: while j2 < numcolumns2 do
fetch cursorC2 into column02;
begin
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' where ', column01, ' <> ', column02, ' into @equalrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @equalrows = 0 then
select table_ as table_name, column01 as column_name01, column02 as column_name02;
end if;
end;
set j2 = j2 + 1;
end while columnLoop2;
close cursorC2;
end;
set j1 = j1 + 1;
end while columnLoop1;
close cursorC1;
end;
set i = i + 1;
end while tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 33: All values equal the default value
drop procedure if exists dblint_rule33 //
create procedure dblint_rule33 (in schema_ varchar(32))
comment 'All values equal the default value'
begin
declare rule_ integer default 33;
declare table_ varchar(32);
declare number_of_tables integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_tables;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_tables then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare number_of_columns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and column_default is not NULL;
open cursorC;
select found_rows() into number_of_columns;
columnLoop: loop
if j > number_of_columns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- select the columns that have only one value
set @v_sql = concat('select ', column_, ' from ', schema_, '.', table_, ' group by ', column_, ' having count(distinct ', column_, ') = 1 into @value');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
set @defaultvalue = (select column_default from information_schema.columns where table_schema = schema_ and table_name = table_ and column_name = column_);
if @value = @defaultvalue then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, concat('All values equal the default value: ', coalesce(@defaultvalue, 'none'));
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 34: Not-NULL columns containing many empty strings
drop procedure if exists dblint_rule34 //
create procedure dblint_rule34 (in schema_ varchar(32))
comment 'Not-NULL columns containing many empty strings'
begin
declare rule_ integer default 34;
declare factor real default 0.5; -- factor is the minimum fraction of rows that have NULL values only
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
set @totalrows = (select table_rows from information_schema.tables where table_schema = schema_ and table_name = table_);
begin
declare curColumn varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' and is_nullable = 'NO' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into curColumn;
begin
set @emptystringrows = 0;
set @v_sql = concat("select count(*) from ", schema_, ".", table_, " where ", curColumn, " = '' into @emptystringrows");
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @emptystringrows > @totalrows * factor then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, curColumn, 'Not NULL column containing many empty strings';
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 35: Numbers or dates stored in varchar columns
drop procedure if exists dblint_rule35 //
create procedure dblint_rule35 (in schema_ varchar(32))
comment 'Numbers or dates stored in varchar columns'
begin
declare rule_ integer default 35;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
set @totalrows = (select table_rows from information_schema.tables where table_schema = schema_ and table_name = table_);
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- test numbers
set @v_where = concat_ws(" ", " where ", column_, " regexp '^[1-9]?[0-9]*$'");
set @v_sql = concat("select count(*) from ", schema_, ".", table_, @v_where, " into @isnumberrows");
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @isnumberrows = @totalrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Number stored in varchar column';
end if;
if @isnumberrows > 0 then
set j = j + 1;
iterate columnLoop;
end if;
-- test dates
set @v_where = concat_ws(" ", " where cast(", column_, " as date) is not NULL ");
set @v_sql = concat("select count(*) from ", schema_, ".", table_, @v_where, " into @isdaterows");
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @isdaterows = @totalrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Date stored in varchar column';
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 37: Mixture of data types in text columns
drop procedure if exists dblint_rule37 //
create procedure dblint_rule37 (in schema_ varchar(32))
comment 'Mixture of data types in text columns'
begin
declare rule_ integer default 37;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- get the count of non empty rows
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' where ', column_, ' is not NULL and ', column_, ' <> "" into @nonemptyrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
set @v_select = concat('select count(*) from ', schema_, '.', table_);
set @v_extra_cond = concat(" and ", column_, "<> '' and ", column_, " is not NULL into @foundrows");
-- test integer
set @v_where = concat(' where ', column_, ' regexp "^-?[1-9]?[0-9]+$" ');
set @v_sql = concat(@v_select, @v_where, @v_extra_cond);
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @foundrows > 0 and @foundrows <> @nonemptyrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Integers in varchar column';
set j = j + 1;
iterate columnLoop;
end if;
-- test float
set @v_where = concat(' where ', column_, ' regexp "^-?[0-9]*[\.][0-9]+([ee][-+]?[0-9]+)?$" ');
set @v_sql = concat(@v_select, @v_where, @v_extra_cond);
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @foundrows > 0 and @foundrows <> @nonemptyrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Floats in varchar column';
set j = j + 1;
iterate columnLoop;
end if;
-- test date, time, datetime
set @v_where = concat(' where ', column_, ' regexp "^[0-9\:\/\-]+$" ');
set @v_sql = concat(@v_select, @v_where, @v_extra_cond);
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @foundrows > 0 and @foundrows <> @nonemptyrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Dates in varchar column';
set j = j + 1;
iterate columnLoop;
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 38: Columns with only one value
drop procedure if exists dblint_rule38 //
create procedure dblint_rule38 (in schema_ varchar(32))
comment 'Columns with only one value'
begin
declare rule_ integer default 38;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and column_key = '' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- select columns that have only one value
set @v_sql = concat('insert into dblint_results (id, table_name, column_name, message) select 38, "', table_, '", "', column_, '", "Has only one value" from ', schema_, ".", table_, ' having count(distinct ', column_, ') = 1');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 39: All values differ from the default value
drop procedure if exists dblint_rule39 //
create procedure dblint_rule39 (in schema_ varchar(32))
comment 'All values differ from the default value'
begin
declare rule_ integer default 39;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare curColumn varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and column_key = '' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into curColumn;
begin
-- call dblint_debug (concat('table is: ', table_, ' and column is: ', curColumn));
set @defaultvalue = (select column_default from information_schema.columns where table_schema = schema_ and table_name = table_ and column_name = curColumn and column_default is not NULL);
set @v_sql = concat_ws(' ', 'select count(*) from ', schema_, '.', table_, 'where', curColumn, '<=> trim("', @defaultvalue, '") into @notequalrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @notequalrows = 0 then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, curColumn, concat('All values differ from default: ', coalesce(@defaultvalue, 'none'));
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 40: Inconsistent casing of first character in text columns
drop procedure if exists dblint_rule40 //
create procedure dblint_rule40 (in schema_ varchar(32))
comment 'Inconsistent casing of first character in text columns'
begin
declare rule_ integer default 40;
declare table_ varchar(32);
declare number_of_tables integer default 0;
declare i integer default 1;
declare cursorT cursor for
select t.table_name from information_schema.tables t inner join information_schema.columns c on t.table_schema = c.table_schema and t.table_name = c.table_name where t.table_schema = schema_ and (data_type = 'varchar' or data_type like '%text' or data_type like '%blob') and table_rows > 0 group by table_name;
open cursorT;
select found_rows() into number_of_tables;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_tables then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare number_of_columns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and (data_type = 'varchar' or data_type like '%text' or data_type like '%blob');
open cursorC;
select found_rows() into number_of_columns;
columnLoop: loop
if j > number_of_columns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
set @count = (select count(*) from table_ where cast(lower(substring(column_, 1, 1)) as binary) = cast(substring(column_, 1, 1) as binary) and column_ is not NULL);
if @count > 0 then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Column starts with uppercase or lowercase letter';
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 42: Column values from a small domain
drop procedure if exists dblint_rule42 //
create procedure dblint_rule42 (in schema_ varchar(32))
comment 'Column values from a small domain'
begin
declare rule_ integer default 42;
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
-- consider 2 - 9 distinct values constitute a small domain.
set @v_sql = concat('insert into dblint_results (id, table_name, column_name, message) select 42, "', table_, '", "', column_, '", concat("Has only ", count(distinct ', column_, '), " values") from ', schema_, '.', table_, ' having count(distinct ', column_, ') between 2 and 9');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 43: Large unfilled varchar columns
drop procedure if exists dblint_rule43 //
create procedure dblint_rule43 (in schema_ varchar(32))
comment 'Large unfilled varchar columns'
begin
declare rule_ integer default 43;
declare factor real default 0.5; -- factor is the fraction of the column-size in which the largest value still fits
declare table_ varchar(32);
declare number_of_objects integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into number_of_objects;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > number_of_objects then
leave tableLoop;
end if;
fetch cursorT into table_;
set @totalrows = (select table_rows from information_schema.tables where table_name = table_ and table_schema = schema_);
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and data_type = 'varchar' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
set @maxlength = (select character_maximum_length from information_schema.columns where table_schema = schema_ and table_name = table_ and column_name = column_);
begin
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' where length(', column_, ') < ', factor, ' * ', convert(@maxlength using latin1), ' into @numofrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @numofrows = @totalrows then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, concat('Large character_maximum_length: ', @maxlength);
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 44: Missing Not-NULL constraints
drop procedure if exists dblint_rule44 //
create procedure dblint_rule44 (in schema_ varchar(32))
comment 'Missing Not-NULL constraints'
begin
declare rule_ integer default 44;
declare table_ varchar(32);
declare numtables integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select found_rows() into numtables;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > numtables then
leave tableLoop;
end if;
fetch cursorT into table_;
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and is_nullable = 'YES' order by ordinal_position;
open cursorC;
select found_rows() into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' where ', column_, ' is NULL into @nullrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @nullrows = 0 then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Has missing NOT NULL constraint';
end if;
end;
set j = j + 1;
end loop columnLoop;
close cursorC;
end;
set i = i + 1;
end loop tableLoop;
close cursorT;
select remediation from dblint_rules where id = rule_;
end;
//
-- Rule 45: Column containing too many NULLs
drop procedure if exists dblint_rule45 //
create procedure dblint_rule45 (in schema_ varchar(32))
comment 'Column containing too many NULLs'
begin
declare rule_ integer default 45;
declare factor real default 0.8; -- factor is the minimum fraction of rows that have NULL values only
declare table_ varchar(32);
declare numtables integer default 0;
declare i integer default 1;
declare cursorT cursor for
select table_name from information_schema.tables where table_schema = schema_ and table_rows > 0;
open cursorT;
select count(*) into numtables;
select concat(id, ' ', name, ': ', description) as description from dblint_rules where id = rule_;
tableLoop: loop
if i > numtables then
leave tableLoop;
end if;
fetch cursorT into table_;
set @totalrows = (select table_rows from information_schema.tables where table_schema = schema_ and table_name = table_);
begin
declare column_ varchar(64);
declare numcolumns integer default 0;
declare j integer default 1;
declare cursorC cursor for
select column_name from information_schema.columns where table_schema = schema_ and table_name = table_ and is_nullable = 'YES' order by ordinal_position;
open cursorC;
select count(*) into numcolumns;
columnLoop: loop
if j > numcolumns then
leave columnLoop;
end if;
fetch cursorC into column_;
begin
set @v_sql = concat('select count(*) from ', schema_, '.', table_, ' where ', column_, ' is NULL into @nullrows');
prepare statement from @v_sql;
execute statement;
deallocate prepare statement;
if @nullrows > @totalrows * factor then
insert into dblint_results (id, table_name, column_name, message) select rule_, table_, column_, 'Contains too many NULLs';
end if;
end;