execsql2 1.130.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,630 @@
1
+ -- md_compare.sql
2
+ --
3
+ -- PURPOSE
4
+ -- Generate MariaDB/MySQL SQL to compare the attributes (non-PK columns) in
5
+ -- a staging table to the equivalently-named base table.
6
+ --
7
+ -- NOTES
8
+ -- 1. The scripts contained in this file that are intended to be called
9
+ -- directly by the user are:
10
+ -- COMPARE_COMMON : Produces SQL that represents comparison
11
+ -- results as categories of "Same", "Different",
12
+ -- "Old", and "New".
13
+ -- COMPARE_COMMON_VALS : Produces SQL that shows both base and
14
+ -- staging values in adjacent columns.
15
+ -- COMPARE_CHANGES : Produces SQL that shows all primary keys in
16
+ -- the staging table plus a column named "changes"
17
+ -- that identifiers whether that row in the
18
+ -- staging table is new, is a changed version of
19
+ -- an existing row, or is identical to an
20
+ -- existing row.
21
+ -- COMPARE_CHANGED : Produces SQL that shows all primary keys in
22
+ -- the base table plus a column named "changed"
23
+ -- that identifiers whether that row exists in
24
+ -- the staging table, has different data in the
25
+ -- staging table, or has identical data in the
26
+ -- staging table.
27
+ -- 2. These scripts query the information schema to obtain
28
+ -- the information needed to generate SQL.
29
+ -- 3. Temporary tables and views created by these scripts all
30
+ -- begin with "cmp_".
31
+ --
32
+ -- AUTHOR
33
+ -- Dreas Nielsen (RDN)
34
+ --
35
+ -- COPYRIGHT AND LICENSE
36
+ -- Copyright (c) 2020 R.Dreas Nielsen
37
+ -- This program is free software: you can redistribute it and/or modify
38
+ -- it under the terms of the GNU General Public License as published by
39
+ -- the Free Software Foundation, either version 3 of the License, or
40
+ -- (at your option) any later version.
41
+ -- This program is distributed in the hope that it will be useful,
42
+ -- but WITHOUT ANY WARRANTY; without even the implied warranty of
43
+ -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44
+ -- GNU General Public License for more details.
45
+ -- The GNU General Public License is available at <http://www.gnu.org/licenses/>
46
+ --
47
+ -- VERSION
48
+ -- 1.1.1
49
+ --
50
+ -- HISTORY
51
+ -- Date Remarks
52
+ -- ---------- -----------------------------------------------------
53
+ -- 2020-02-27 Created from v. 1.1.0 of pg_compare.sql RDN.
54
+ -- 2020-02-29 Excluded PK columns from output of COMPARE_COMMON_VALS.
55
+ -- Completed conversion to md. v. 1.1.1. RDN.
56
+ -- ==================================================================
57
+
58
+
59
+ -- ################################################################
60
+ -- Script COMPARE_COMMON
61
+ -- ===============================================================
62
+ --
63
+ -- Generate SQL to compare the attributes of base and staging
64
+ -- tables for only those primary keys in common. This uses an
65
+ -- inner join between the tables, and does not include any rows
66
+ -- with a primary key that is in one table and not the other.
67
+ --
68
+ -- The output includes a column for every column in the base
69
+ -- table (except those explicitly excluded). Primary key
70
+ -- columns are populated with primary key values. Attribute
71
+ -- columns are populated with one of the following tags:
72
+ -- Same : The value in base and staging tables
73
+ -- are the same.
74
+ -- Different : The values in base and staging tables
75
+ -- are different.
76
+ -- Old : There is a value in the base table but
77
+ -- no value (i.e., null) in the staging table.
78
+ -- New : There is a value in the staging table
79
+ -- but no value (i.e., null) in the base table.
80
+ --
81
+ -- Required input arguments:
82
+ -- stage_pfx : The prefix for staging tables.
83
+ -- table : The name of the table to compare.
84
+ --
85
+ -- Optional input arguments:
86
+ -- include_cols : Contains a comma-separated list of single-quoted
87
+ -- column names in the base table that are the
88
+ -- *only* attribute columns to be compared.
89
+ -- exclude_cols : Contains a comma-separated list of single-quoted
90
+ -- column names in the base table that are not to
91
+ -- be compared. If 'include_cols' is provided,
92
+ -- 'exclude_cols' is ignored.
93
+ --
94
+ -- Required output arguments:
95
+ -- sql_var : The name of the variable into which
96
+ -- the generated SQL will be stored
97
+ --
98
+ -- Notes
99
+ -- 1. The generated SQL is not terminated with a semicolon.
100
+ -- ===============================================================
101
+ -- !x! BEGIN SCRIPT COMPARE_COMMON with parameters (stage_pfx, table, sql_var)
102
+
103
+ -- Create a table of primary key columns for the table
104
+ drop table if exists cmp_primary_key_columns cascade;
105
+ create table cmp_primary_key_columns as
106
+ select k.column_name, k.ordinal_position
107
+ from information_schema.table_constraints as tc
108
+ inner join information_schema.key_column_usage as k
109
+ on tc.constraint_type = 'PRIMARY KEY'
110
+ and tc.constraint_name = k.constraint_name
111
+ and tc.constraint_catalog = k.constraint_catalog
112
+ and tc.constraint_schema = k.constraint_schema
113
+ and tc.table_schema = k.table_schema
114
+ and tc.table_name = k.table_name
115
+ and tc.constraint_name = k.constraint_name
116
+ where
117
+ k.table_name = '!!#table!!'
118
+ and tc.constraint_schema = '!!$db_name!!'
119
+ order by k.ordinal_position
120
+ ;
121
+
122
+ -- !x! if(not hasrows(cmp_primary_key_columns)) { halt message "Table !!#table!! has no primary key columns." }
123
+
124
+ -- Populate a table with the names of the attribute columns
125
+ -- that are to be compared.
126
+ -- Include only those columns from the staging table that are also in the base table.
127
+ -- !x! sub_empty ~col_sel
128
+ -- !x! if(sub_defined(#include_cols))
129
+ -- !x! sub ~col_sel and s.column_name in (!!#include_cols!!)
130
+ -- !x! elseif(sub_defined(#exclude_cols))
131
+ -- !x! sub ~col_sel and s.column_name not in (!!#exclude_cols!!)
132
+ -- !x! endif
133
+ drop table if exists cmp_cols cascade;
134
+ create table cmp_cols as
135
+ select s.column_name
136
+ from information_schema.columns as s
137
+ inner join information_schema.columns as b on s.column_name=b.column_name
138
+ left join cmp_primary_key_columns as pk on pk.column_name = s.column_name
139
+ where
140
+ s.table_name = '!!#stage_pfx!!!!#table!!'
141
+ and b.table_name = '!!#table!!'
142
+ and pk.column_name is null
143
+ !!~col_sel!!
144
+ order by s.ordinal_position;
145
+
146
+ -- Create the SQL to select primary key columns from the base table.
147
+ drop view if exists cmp_pkexpr cascade;
148
+ create view cmp_pkexpr as
149
+ select
150
+ group_concat(concat('b.', column_name) separator ', ')
151
+ from
152
+ cmp_primary_key_columns;
153
+ -- !x! subdata ~pkcolexpr cmp_pkexpr
154
+
155
+
156
+ -- Create the SQL for each column to compare the base (b) and staging (s) tables.
157
+ alter table cmp_cols add column sqlcmd character varying(500);
158
+ update cmp_cols
159
+ set sqlcmd =
160
+ concat('case when b.', column_name, ' is null then case when s.', column_name, ' is null then ''Same'' else ''New'' end else case when s.', column_name, ' is null then ''Old'' else case when b.', column_name, ' = s.', column_name, ' then ''Same'' else ''Different'' end end end as ', column_name)
161
+ ;
162
+
163
+ -- Create the comparison expression for all columns.
164
+ drop view if exists cmp_compexpr cascade;
165
+ create view cmp_compexpr as
166
+ select
167
+ group_concat(sqlcmd separator ', ')
168
+ from
169
+ cmp_cols;
170
+ -- !x! subdata ~compexpr cmp_compexpr
171
+
172
+ -- Create a join expression for primary key columns of the base (b) and
173
+ -- staging (s) tables.
174
+ drop view if exists cmp_joinexpr cascade;
175
+ create view cmp_joinexpr as
176
+ select
177
+ group_concat(concat('b.', column_name, ' = s.', column_name) separator ' and ')
178
+ from
179
+ cmp_primary_key_columns;
180
+ -- !x! subdata ~joinexpr cmp_joinexpr
181
+
182
+
183
+ -- Create and assign the entire SQL.
184
+ -- !x! sub !!#sql_var!! select !!~pkcolexpr!!, !!~compexpr!! from !!#table!! as b inner join !!#stage_pfx!!!!#table!! as s on !!~joinexpr!!
185
+
186
+ -- Clean up.
187
+ drop view if exists cmp_joinexpr;
188
+ drop view if exists cmp_compexpr;
189
+ drop table if exists cmp_cols cascade;
190
+ drop table if exists cmp_primary_key_columns cascade;
191
+
192
+
193
+ -- !x! END SCRIPT COMPARE_COMMON
194
+
195
+ -- #################### End of COMPARE_COMMON ###################
196
+ -- ################################################################
197
+
198
+
199
+
200
+ -- ################################################################
201
+ -- Script COMPARE_COMMON_VALS
202
+ -- ===============================================================
203
+ --
204
+ -- Generate SQL to compare the attributes of base and staging
205
+ -- tables for only those primary keys in common. This uses an
206
+ -- inner join between the tables, and does not include any rows
207
+ -- with a primary key that is in one table and not the other.
208
+ --
209
+ -- The output includes two columns for every attribute column in
210
+ -- the base table (except those explicitly excluded), plus a column
211
+ -- for every primary key column. Primary key
212
+ -- columns are populated with primary key values. The two output
213
+ -- columns for each attribute column in the base table have names that
214
+ -- match the base table column name plus a suffix of either "_old",
215
+ -- representing the base table, or "_new", representing the staging
216
+ -- table.
217
+ --
218
+ -- Required input arguments:
219
+ -- stage_pfx : The prefix for staging tables.
220
+ -- table : The name of the table to compare.
221
+ --
222
+ -- Optional input arguments:
223
+ -- include_cols : Contains a comma-separated list of single-quoted
224
+ -- column names in the base table that are the
225
+ -- *only* attribute columns to be compared.
226
+ -- exclude_cols : Contains a comma-separated list of single-quoted
227
+ -- column names in the base table that are not to
228
+ -- be compared. If 'include_cols' is provided,
229
+ -- 'exclude_cols' is ignored.
230
+ --
231
+ -- Required output arguments:
232
+ -- sql_var : The name of the variable into which
233
+ -- the generated SQL will be stored
234
+ --
235
+ -- Notes
236
+ -- 1. The generated SQL is not terminated with a semicolon.
237
+ -- ===============================================================
238
+ -- !x! BEGIN SCRIPT COMPARE_COMMON_VALS with parameters (stage_pfx, table, sql_var)
239
+
240
+ -- Create a table of primary key columns for the table
241
+ drop table if exists cmp_primary_key_columns cascade;
242
+ create table cmp_primary_key_columns as
243
+ select k.column_name, k.ordinal_position
244
+ from information_schema.table_constraints as tc
245
+ inner join information_schema.key_column_usage as k
246
+ on tc.constraint_type = 'PRIMARY KEY'
247
+ and tc.constraint_name = k.constraint_name
248
+ and tc.constraint_catalog = k.constraint_catalog
249
+ and tc.constraint_schema = k.constraint_schema
250
+ and tc.table_schema = k.table_schema
251
+ and tc.table_name = k.table_name
252
+ and tc.constraint_name = k.constraint_name
253
+ where
254
+ k.table_name = '!!#table!!'
255
+ and tc.constraint_schema = '!!$db_name!!'
256
+ order by k.ordinal_position
257
+ ;
258
+
259
+ -- !x! if(not hasrows(cmp_primary_key_columns)) { halt message "Table !!#table!! has no primary key columns." }
260
+
261
+ -- Populate a table with the names of the attribute columns
262
+ -- that are to be compared.
263
+ -- Include only those columns from the staging table that are also in the base table.
264
+ -- !x! sub_empty ~col_sel
265
+ -- !x! if(sub_defined(#include_cols))
266
+ -- !x! sub ~col_sel and s.column_name in (!!#include_cols!!)
267
+ -- !x! elseif(sub_defined(#exclude_cols))
268
+ -- !x! sub ~col_sel and s.column_name not in (!!#exclude_cols!!)
269
+ -- !x! endif
270
+ drop table if exists cmp_cols cascade;
271
+ create table cmp_cols as
272
+ select s.column_name
273
+ from information_schema.columns as s
274
+ inner join information_schema.columns as b on s.column_name=b.column_name
275
+ left join cmp_primary_key_columns as pk on pk.column_name = s.column_name
276
+ where
277
+ s.table_name = '!!#stage_pfx!!!!#table!!'
278
+ and b.table_name = '!!#table!!'
279
+ and pk.column_name is null
280
+ !!~col_sel!!
281
+ order by s.ordinal_position;
282
+
283
+ -- Create the SQL to select primary key columns from the base table.
284
+ drop view if exists cmp_pkexpr cascade;
285
+ create view cmp_pkexpr as
286
+ select
287
+ group_concat(concat('b.', column_name) separator ', ')
288
+ from
289
+ cmp_primary_key_columns;
290
+ -- !x! subdata ~pkcolexpr cmp_pkexpr
291
+
292
+
293
+ -- Create the SQL for each column to compare the base (b) and staging (s) tables.
294
+ alter table cmp_cols add column sqlcmd character varying(500);
295
+ update cmp_cols
296
+ set sqlcmd =
297
+ concat('b.', column_name, ' as ', column_name, '_old, s.', column_name, ' as ', column_name, '_new')
298
+ ;
299
+
300
+ -- Create the comparison expression for all columns.
301
+ drop view if exists cmp_compexpr cascade;
302
+ create view cmp_compexpr as
303
+ select
304
+ group_concat(sqlcmd separator ', ')
305
+ from
306
+ cmp_cols;
307
+ -- !x! subdata ~compexpr cmp_compexpr
308
+
309
+ -- Create a join expression for primary key columns of the base (b) and
310
+ -- staging (s) tables.
311
+ drop view if exists cmp_joinexpr cascade;
312
+ create view cmp_joinexpr as
313
+ select
314
+ group_concat(concat('b.', column_name, ' = s.', column_name) separator ' and ')
315
+ from
316
+ cmp_primary_key_columns;
317
+ -- !x! subdata ~joinexpr cmp_joinexpr
318
+
319
+
320
+ -- Create and assign the entire SQL.
321
+ -- !x! sub !!#sql_var!! select !!~pkcolexpr!!, !!~compexpr!! from !!#table!! as b inner join !!#stage_pfx!!!!#table!! as s on !!~joinexpr!!
322
+
323
+
324
+ -- Clean up.
325
+ drop view if exists cmp_joinexpr;
326
+ drop view if exists cmp_compexpr;
327
+ drop view if exists cmp_pkexpr;
328
+ drop table if exists cmp_cols cascade;
329
+ drop table if exists cmp_primary_key_columns cascade;
330
+
331
+
332
+ -- !x! END SCRIPT COMPARE_COMMON_VALS
333
+
334
+ -- ############### End of COMPARE_COMMON_VALS ###################
335
+ -- ################################################################
336
+
337
+
338
+
339
+
340
+
341
+ -- ################################################################
342
+ -- Script COMPARE_CHANGES
343
+ -- ===============================================================
344
+ --
345
+ -- Generate SQL to characterize every row of the staging table
346
+ -- data as either a new row, a row with changed data, or a row
347
+ -- with data that are unchanged relative to the base table.
348
+ --
349
+ -- The output includes a column for every primary key plus a
350
+ -- column named "changes" with one of the following tags:
351
+ -- NewRow : The primary key is in the staging table
352
+ -- but not in the base table.
353
+ -- Changed : At least one of the attribute values is
354
+ -- different in the staging table.
355
+ -- Unchanged : All attribute values are the same in the
356
+ -- staging and base tables.
357
+ --
358
+ -- Required input arguments:
359
+ -- stage_pfx : The prefix for staging tables.
360
+ -- table : The name of the table to compare.
361
+ --
362
+ -- Optional input arguments:
363
+ -- include_cols : Contains a comma-separated list of single-quoted
364
+ -- column names in the base table that are the
365
+ -- *only* attribute columns to be compared.
366
+ -- exclude_cols : Contains a comma-separated list of single-quoted
367
+ -- column names in the base table that are not to
368
+ -- be compared. If 'include_cols' is provided,
369
+ -- 'exclude_cols' is ignored.
370
+ --
371
+ -- Required output arguments:
372
+ -- sql_var : The name of the variable into which
373
+ -- the generated SQL will be stored
374
+ --
375
+ -- Notes
376
+ -- 1. The generated SQL is not terminated with a semicolon.
377
+ -- ===============================================================
378
+ -- !x! BEGIN SCRIPT COMPARE_CHANGES with parameters (stage_pfx, table, sql_var)
379
+
380
+ -- Create a table of primary key columns for the table
381
+ drop table if exists cmp_primary_key_columns cascade;
382
+ create table cmp_primary_key_columns as
383
+ select k.column_name, k.ordinal_position
384
+ from information_schema.table_constraints as tc
385
+ inner join information_schema.key_column_usage as k
386
+ on tc.constraint_type = 'PRIMARY KEY'
387
+ and tc.constraint_name = k.constraint_name
388
+ and tc.constraint_catalog = k.constraint_catalog
389
+ and tc.constraint_schema = k.constraint_schema
390
+ and tc.table_schema = k.table_schema
391
+ and tc.table_name = k.table_name
392
+ and tc.constraint_name = k.constraint_name
393
+ where
394
+ k.table_name = '!!#table!!'
395
+ and tc.constraint_schema = '!!$db_name!!'
396
+ order by k.ordinal_position
397
+ ;
398
+
399
+
400
+ -- !x! if(not hasrows(cmp_primary_key_columns)) { halt message "Table !!#table!! has no primary key columns." }
401
+
402
+ -- Populate a table with the names of the attribute columns
403
+ -- that are to be compared.
404
+ -- Include only those columns from the staging table that are also in the base table.
405
+ -- !x! sub_empty ~col_sel
406
+ -- !x! if(sub_defined(#include_cols))
407
+ -- !x! sub ~col_sel and s.column_name in (!!#include_cols!!)
408
+ -- !x! elseif(sub_defined(#exclude_cols))
409
+ -- !x! sub ~col_sel and s.column_name not in (!!#exclude_cols!!)
410
+ -- !x! endif
411
+ drop table if exists cmp_cols cascade;
412
+ create table cmp_cols as
413
+ select s.column_name
414
+ from information_schema.columns as s
415
+ inner join information_schema.columns as b on s.column_name=b.column_name
416
+ left join cmp_primary_key_columns as pk on pk.column_name = s.column_name
417
+ where
418
+ s.table_name = '!!#stage_pfx!!!!#table!!'
419
+ and b.table_name = '!!#table!!'
420
+ and pk.column_name is null
421
+ !!~col_sel!!
422
+ order by s.ordinal_position;
423
+
424
+ -- Create the SQL to select primary key columns from the staging table.
425
+ drop view if exists cmp_pkexpr cascade;
426
+ create view cmp_pkexpr as
427
+ select
428
+ group_concat(concat('s.', column_name) separator ', ')
429
+ from
430
+ cmp_primary_key_columns;
431
+ -- !x! subdata ~pkcolexpr cmp_pkexpr
432
+
433
+
434
+ -- Create the SQL for each column to compare the base (b) and staging (s) tables.
435
+ -- This generates a value of 1 when there are differences and 0 when there are not.
436
+ alter table cmp_cols add column sqlcmd character varying(500);
437
+ update cmp_cols
438
+ set sqlcmd =
439
+ concat('case when b.', column_name, ' is null then case when s.', column_name, ' is null then 0 else 1 end else case when s.', column_name, ' is null then 1 else case when b.', column_name, ' = s.', column_name, ' then 0 else 1 end end end')
440
+ ;
441
+
442
+ -- Create the expression to determine whether there is a difference for any column.
443
+ drop view if exists cmp_compexpr cascade;
444
+ create view cmp_compexpr as
445
+ select
446
+ group_concat(sqlcmd separator ' + ')
447
+ from
448
+ cmp_cols;
449
+ -- !x! subdata ~compexpr cmp_compexpr
450
+ -- !x! sub ~diffexpr case when !!~compexpr!! = 0 then 'Unchanged' else 'Changed' end
451
+
452
+ -- Create a join expression for primary key columns of the base (b) and
453
+ -- staging (s) tables.
454
+ drop view if exists cmp_joinexpr cascade;
455
+ create view cmp_joinexpr as
456
+ select
457
+ group_concat(concat('b.', column_name, ' = s.', column_name) separator ' and ')
458
+ from
459
+ cmp_primary_key_columns;
460
+ -- !x! subdata ~joinexpr cmp_joinexpr
461
+
462
+ -- Get the name of a single primary key column, aliased to the base table,
463
+ -- to test for row existence in the base table.
464
+ -- !x! subdata pkcol1 cmp_primary_key_columns
465
+
466
+ -- Create the SQL to display either the row tag or the comparison tag.
467
+ -- !x! sub ~changeexpr case when b.!!pkcol1!! is null then 'NewRow' else !!~diffexpr!! end as changes
468
+
469
+ -- Create and assign the entire SQL.
470
+ -- !x! sub !!#sql_var!! select !!~pkcolexpr!!, !!~changeexpr!! from !!#stage_pfx!!!!#table!! as s left join !!#table!! as b on !!~joinexpr!!
471
+
472
+
473
+ -- Clean up.
474
+ drop view if exists cmp_joinexpr;
475
+ drop view if exists cmp_compexpr;
476
+ drop view if exists cmp_pkexpr;
477
+ drop table if exists cmp_cols cascade;
478
+ drop table if exists cmp_primary_key_columns cascade;
479
+
480
+
481
+ -- !x! END SCRIPT COMPARE_CHANGES
482
+
483
+ -- ################### End of COMPARE_CHANGES ###################
484
+ -- ################################################################
485
+
486
+
487
+
488
+
489
+
490
+
491
+ -- ################################################################
492
+ -- Script COMPARE_CHANGED
493
+ -- ===============================================================
494
+ --
495
+ -- Generate SQL to characterize every row of the base table
496
+ -- depending on whether there is a matching row in the staging
497
+ -- table, whether a matching row has new data, or whether a
498
+ -- matching row has identical data.
499
+ --
500
+ -- The output includes a column for every primary key plus a
501
+ -- column named "changed" with one of the following tags:
502
+ -- NoNewRow : The primary key is in the base table
503
+ -- but not in the staging table.
504
+ -- Changed : At least one of the attribute values is
505
+ -- different in the staging table.
506
+ -- Unchanged : All attribute values are the same in the
507
+ -- staging and base tables.
508
+ --
509
+ -- Required input arguments:
510
+ -- stage_pfx : The prefix for staging tables.
511
+ -- table : The name of the table to compare.
512
+ --
513
+ -- Optional input arguments:
514
+ -- include_cols : Contains a comma-separated list of single-quoted
515
+ -- column names in the base table that are the
516
+ -- *only* attribute columns to be compared.
517
+ -- exclude_cols : Contains a comma-separated list of single-quoted
518
+ -- column names in the base table that are not to
519
+ -- be compared. If 'include_cols' is provided,
520
+ -- 'exclude_cols' is ignored.
521
+ --
522
+ -- Required output arguments:
523
+ -- sql_var : The name of the variable into which
524
+ -- the generated SQL will be stored
525
+ --
526
+ -- Notes
527
+ -- 1. The generated SQL is not terminated with a semicolon.
528
+ -- ===============================================================
529
+ -- !x! BEGIN SCRIPT COMPARE_CHANGED with parameters (stage_pfx, table, sql_var)
530
+
531
+ -- Create a table of primary key columns for the table
532
+ drop table if exists cmp_primary_key_columns cascade;
533
+ create table cmp_primary_key_columns as
534
+ select k.column_name, k.ordinal_position
535
+ from information_schema.table_constraints as tc
536
+ inner join information_schema.key_column_usage as k
537
+ on tc.constraint_type = 'PRIMARY KEY'
538
+ and tc.constraint_name = k.constraint_name
539
+ and tc.constraint_catalog = k.constraint_catalog
540
+ and tc.constraint_schema = k.constraint_schema
541
+ and tc.table_schema = k.table_schema
542
+ and tc.table_name = k.table_name
543
+ and tc.constraint_name = k.constraint_name
544
+ where
545
+ k.table_name = '!!#table!!'
546
+ and tc.constraint_schema = '!!$db_name!!'
547
+ order by k.ordinal_position
548
+ ;
549
+
550
+ -- !x! if(not hasrows(cmp_primary_key_columns)) { halt message "Table !!#table!! has no primary key columns." }
551
+
552
+ -- Populate a table with the names of the attribute columns
553
+ -- that are to be compared.
554
+ -- Include only those columns from the staging table that are also in the base table.
555
+ -- !x! sub_empty ~col_sel
556
+ -- !x! if(sub_defined(#include_cols))
557
+ -- !x! sub ~col_sel and s.column_name in (!!#include_cols!!)
558
+ -- !x! elseif(sub_defined(#exclude_cols))
559
+ -- !x! sub ~col_sel and s.column_name not in (!!#exclude_cols!!)
560
+ -- !x! endif
561
+ drop table if exists cmp_cols cascade;
562
+ create table cmp_cols as
563
+ select s.column_name
564
+ from information_schema.columns as s
565
+ inner join information_schema.columns as b on s.column_name=b.column_name
566
+ left join cmp_primary_key_columns as pk on pk.column_name = s.column_name
567
+ where
568
+ s.table_name = '!!#stage_pfx!!!!#table!!'
569
+ and b.table_name = '!!#table!!'
570
+ and pk.column_name is null
571
+ !!~col_sel!!
572
+ order by s.ordinal_position;
573
+
574
+ -- Create the SQL to select primary key columns from the base table.
575
+ drop view if exists cmp_pkexpr cascade;
576
+ create view cmp_pkexpr as
577
+ select
578
+ group_concat(concat('b.', column_name) separator ', ')
579
+ from
580
+ cmp_primary_key_columns;
581
+ -- !x! subdata ~pkcolexpr cmp_pkexpr
582
+
583
+
584
+ -- Create the SQL for each column to compare the base (b) and staging (s) tables.
585
+ -- This generates a value of 1 when there are differences and 0 when there are not.
586
+ alter table cmp_cols add column sqlcmd character varying(500);
587
+ update cmp_cols
588
+ set sqlcmd =
589
+ concat('case when b.', column_name, ' is null then case when s.', column_name, ' is null then 0 else 1 end else case when s.', column_name, ' is null then 1 else case when b.', column_name, ' = s.', column_name, ' then 0 else 1 end end end')
590
+ ;
591
+
592
+ -- Create the expression to determine whether there is a difference for any column.
593
+ drop view if exists cmp_compexpr cascade;
594
+ create view cmp_compexpr as
595
+ select
596
+ group_concat(sqlcmd separator ' + ')
597
+ from
598
+ cmp_cols;
599
+ -- !x! subdata ~compexpr cmp_compexpr
600
+ -- !x! sub ~diffexpr case when !!~compexpr!! = 0 then 'Unchanged' else 'Changed' end
601
+
602
+ -- Create a join expression for primary key columns of the base (b) and
603
+ -- staging (s) tables.
604
+ drop view if exists cmp_joinexpr cascade;
605
+ create view cmp_joinexpr as
606
+ select
607
+ group_concat(concat('b.', column_name, ' = s.', column_name) separator ' and ')
608
+ from
609
+ cmp_primary_key_columns;
610
+ -- !x! subdata ~joinexpr cmp_joinexpr
611
+
612
+ -- Get the name of a single primary key column to test for row existence in the
613
+ -- staging table.
614
+ -- !x! subdata pkcol1 cmp_primary_key_columns
615
+
616
+ -- Create the SQL to display either the row tag or the comparison tag.
617
+ -- !x! sub ~changeexpr case when s.!!pkcol1!! is null then 'NoNewRow' else !!~diffexpr!! end as changed
618
+
619
+ -- Create and assign the entire SQL.
620
+ -- !x! sub !!#sql_var!! select !!~pkcolexpr!!, !!~changeexpr!! from !!#table!! as b left join !!#stage_pfx!!!!#table!! as s on !!~joinexpr!!
621
+
622
+
623
+
624
+ -- !x! END SCRIPT COMPARE_CHANGED
625
+
626
+ -- ################### End of COMPARE_CHANGED ###################
627
+ -- ################################################################
628
+
629
+
630
+