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,405 @@
1
+ -- ss_glossary.sql
2
+ --
3
+ -- PURPOSE
4
+ -- ExecSQL scripts to create a custom glossary of column names (or
5
+ -- other objects) to accompany a data summary.
6
+ --
7
+ -- HOW TO USE THESE SCRIPTS
8
+ -- 1. Use the execsql CONNECT metacommand to connect to the SQL Server
9
+ -- database containing the master glossary table if the master
10
+ -- glossary is not in the current database.
11
+ -- 2. Call the INIT_GLOSSARY script to describe the master glossary
12
+ -- and initialize the custom subset of the master glossary that
13
+ -- is to be created.
14
+ -- 3. Call any of the ADD_GLOSSARY_LIST, ADD_GLOSSARY_ITEM, and
15
+ -- ADD_GLOSSARY_TABLE scripts to add entries to the custom
16
+ -- glossary.
17
+ -- 4. Use the "glossary" view to display or export the custom glossary.
18
+ --
19
+ -- NOTES
20
+ -- 1. All substitution variables, tables, and views created by these
21
+ -- scripts have the prefix "gls_"
22
+ -- 2. Side effects: a) Creates a table named "gls_glossary"
23
+ -- in the 'initial' database; b) creates a view named
24
+ -- "glossary" in the 'initial' database.
25
+ --
26
+ -- COPYRIGHT AND LICENSE
27
+ -- Copyright (c) 2019, R. Dreas Nielsen
28
+ -- This program is free software: you can redistribute it and/or modify it
29
+ -- under the terms of the GNU General Public License as published by the
30
+ -- Free Software Foundation, either version 3 of the License, or (at your
31
+ -- option) any later version. This program is distributed in the hope that
32
+ -- it will be useful, but WITHOUT ANY WARRANTY; without even the implied
33
+ -- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34
+ -- GNU General Public License for more details. The GNU General Public
35
+ -- License is available at http://www.gnu.org/licenses/.
36
+ --
37
+ -- AUTHORS
38
+ -- Dreas Nielsen (RDN)
39
+ -- Elizabeth Shea (ES)
40
+ --
41
+ -- VERSION
42
+ -- 1.0.0
43
+ --
44
+ -- HISTORY
45
+ -- Date Remarks
46
+ -- ---------- ---------------------------------------------------------------
47
+ -- 2019-05-01 Created SQL Server version from 2019-04-20 version for
48
+ -- Postgres. RDN.
49
+ -- 2019-05-11 Completed conversion to SQL Server. RDN.
50
+ -- ============================================================================
51
+
52
+
53
+
54
+ -- ################################################################
55
+ -- Script STRING_AGG
56
+ -- Code by Elizabeth Shea
57
+ -- ===============================================================
58
+ --
59
+ -- Utility script to perform string aggregation
60
+ -- Beginning with 2017 edition, SQL Server *does* include the string_agg()
61
+ -- function, but earlier versions do not.
62
+ --
63
+ -- Input parameters:
64
+ -- table_name : The name of the table containing the column to be aggregated.
65
+ -- This may be a temp table containing pre-processed strings.
66
+ -- string_col : The name of the column to be aggregated.
67
+ -- order_col : The name of the column to order by
68
+ -- delimiter : Character to be used as a delimiter
69
+ -- string_var : Name of variable to which aggregated string will be assigned
70
+ --
71
+ -- Output parameters:
72
+ -- string_var : The name of the variable to receive the aggregated string.
73
+ --
74
+ --
75
+ -- Example:
76
+ -- -- !x! execute script string_agg with (table_name=#nonnulcols, string_col=null_errors, order_col=null_errors, delimiter=", ", string_var=+nullcols)
77
+ -- ===============================================================
78
+ -- !x! BEGIN SCRIPT STRING_AGG with parameters (table_name, string_col, order_col, delimiter, string_var)
79
+
80
+ if object_id('tempdb..#agg_string') is not null drop table #agg_string;
81
+ with enum as
82
+ (
83
+ select
84
+ cast(!!#string_col!! as varchar(max)) as agg_string,
85
+ row_number() over (order by !!#order_col!!) as row_num
86
+ from
87
+ !!#table_name!!
88
+ ),
89
+ agg as
90
+ (
91
+ select
92
+ one.agg_string,
93
+ one.row_num
94
+ from
95
+ enum as one
96
+ where
97
+ one.row_num=1
98
+ UNION ALL
99
+ select
100
+ agg.agg_string + '!!#delimiter!!' + enum.agg_string as agg_string,
101
+ enum.row_num
102
+ from
103
+ agg, enum
104
+ where
105
+ enum.row_num=agg.row_num+1
106
+ )
107
+ select
108
+ agg_string
109
+ into #agg_string
110
+ from agg
111
+ where row_num=(select max(row_num) from agg);
112
+ -- !x! if(hasrows(#agg_string))
113
+ -- !x! subdata !!#string_var!! #agg_string
114
+ -- !x! endif
115
+
116
+ -- !x! END SCRIPT
117
+ -- ################## End of STRING_AGG #####################
118
+ -- ################################################################
119
+
120
+
121
+
122
+ -- ################################################################
123
+ -- Script INIT_GLOSSARY
124
+ -- ===============================================================
125
+ --
126
+ -- Initialize the custom glossary and describe the master glossary
127
+ -- that will provide descriptions to include in the custom glossary.
128
+ --
129
+ -- Required input arguments:
130
+ -- glossary_db_alias : The execsql alias of the database containing
131
+ -- the master glossary table.
132
+ -- glossary_table : The name of the master glossary table.
133
+ -- name_col : The name of the column in the glossary
134
+ -- table containing the column (or other
135
+ -- object) name.
136
+ -- def_col : The name of the column in the glossary
137
+ -- table containing the definition of the
138
+ -- column (or other object).
139
+ -- Optional input argument:
140
+ -- def_url : The URL of a page that provides additional
141
+ -- information about the column (or other object).
142
+ -- ===============================================================
143
+ -- !x! BEGIN SCRIPT INIT_GLOSSARY with parameters (glossary_db_alias, glossary_table, name_col, def_col)
144
+
145
+ -- !x! sub gls_alias !!#glossary_db_alias!!
146
+ -- !x! sub gls_table !!#glossary_table!!
147
+ -- !x! sub gls_name !!#name_col!!
148
+ -- !x! sub gls_def !!#def_col!!
149
+ -- !x! if(sub_defined(#def_url))
150
+ -- !x! sub gls_has_url Yes
151
+ -- !x! sub gls_url !!#def_url!!
152
+ -- !x! sub ~url_colspec !!gls_url!! text,
153
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!, !!gls_url!!
154
+ -- !x! else
155
+ -- !x! sub gls_has_url No
156
+ -- !x! sub_empty ~url_colspec
157
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!
158
+ -- !x! endif
159
+
160
+ -- Create a table for the selected glossary entries in the 'initial' database.
161
+ -- !x! sub ~entry_alias !!$current_alias!!
162
+ -- !x! use initial
163
+ drop table if exists gls_glossary;
164
+ create table gls_glossary (
165
+ !!gls_name!! varchar(150),
166
+ !!gls_def!! text,
167
+ !!~url_colspec!!
168
+ constraint pk_gls_glossary primary key (!!gls_name!!)
169
+ );
170
+
171
+ drop view if exists glossary;
172
+ create view glossary as
173
+ select !!gls_collist!!
174
+ from gls_glossary;
175
+ -- No ORDER BY clause on views in SQL Server, so pot luck.
176
+
177
+ -- !x! use !!~entry_alias!!
178
+
179
+ -- !x! END SCRIPT INIT_GLOSSARY
180
+ -- #################### End of INIT_GLOSSARY ####################
181
+ -- ################################################################
182
+
183
+
184
+
185
+
186
+
187
+ -- ################################################################
188
+ -- Script ADD_GLOSSARY_LIST
189
+ -- ===============================================================
190
+ --
191
+ -- Add a specific list of columns to the glossary.
192
+ --
193
+ -- Required input arguments:
194
+ -- column_list : A string of comma-separated column names.
195
+ -- ===============================================================
196
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_LIST with parameters (column_list)
197
+
198
+ -- !x! sub ~entry_alias !!$current_alias!!
199
+ -- !x! use initial
200
+
201
+ drop table if exists gls_column_list;
202
+
203
+ -- Recursive CTE to parse column list argument
204
+ -- NOTE: SQL Server 2017 includes the trim() function, but SQL Server 2016 does not,
205
+ -- so a combination of ltrim and rtrim is used here instead.
206
+ -- CTE code by Elizabeth Shea.
207
+ with itemtable as (
208
+ select
209
+ case when charindex(',', column_string) = 0
210
+ then rtrim(ltrim(column_string))
211
+ else rtrim(ltrim(substring(column_string, 1,charindex(',', column_string)-1)))
212
+ end as column_name,
213
+ case when charindex(',', column_string) = 0
214
+ then NULL
215
+ else rtrim(ltrim(substring(column_string, charindex(',', column_string)+1, len(column_string))))
216
+ end as remaining_list
217
+ from
218
+ (select '!!#column_list!!' as column_string) as ts
219
+ UNION ALL
220
+ select
221
+ case when charindex(',', remaining_list) = 0
222
+ then rtrim(ltrim(remaining_list))
223
+ else rtrim(ltrim(substring(remaining_list, 1,charindex(',', remaining_list)-1)))
224
+ end as column_name,
225
+ case when charindex(',', remaining_list) = 0
226
+ then NULL
227
+ else rtrim(ltrim(substring(remaining_list, charindex(',', remaining_list)+1, len(remaining_list))))
228
+ end as remaining_list
229
+ from
230
+ itemtable
231
+ where
232
+ remaining_list is not null
233
+ --Guards against entries with trailing commas:
234
+ -- e.g, 'column1, column2,'
235
+ and rtrim(ltrim(remaining_list))<>''
236
+ )
237
+ select
238
+ column_name as !!gls_name!!
239
+ into
240
+ gls_column_list
241
+ from
242
+ itemtable;
243
+
244
+
245
+ -- Add any of the column names that are not already in the glossary.
246
+ -- 1. Get list of column names not already in the glossary into a temp table.
247
+ -- 2. Copy that temp table to the glossary database.
248
+ -- 3. In the glossary db, get the glossary information from the master glossary
249
+ -- into another temp table.
250
+ -- 4. Copy that second temp table to the 'initial' database.
251
+ -- 5. In the 'initial' database, append those rows to the gls_glossary table.
252
+
253
+ -- 1.
254
+ drop table if exists gls_newentries;
255
+ select gls_column_list.!!gls_name!!
256
+ into gls_newentries
257
+ from
258
+ gls_column_list
259
+ left join gls_glossary on gls_glossary.!!gls_name!! = gls_column_list.!!gls_name!!
260
+ where
261
+ gls_glossary.!!gls_name!! is null;
262
+
263
+ -- 2.
264
+ -- Change the name in case it's in the same database.
265
+ -- !x! copy gls_newentries from initial to replacement gls_newentries2 in !!gls_alias!!
266
+
267
+ -- 3.
268
+ -- !x! use !!gls_alias!!
269
+ drop table if exists gls_newglossary2;
270
+ select
271
+ !!gls_collist!!
272
+ into gls_newglossary2
273
+ from
274
+ !!gls_table!!
275
+ where
276
+ !!gls_name!! in (select !!gls_name!! from gls_newentries2);
277
+
278
+ drop table gls_newentries2;
279
+
280
+ -- 4.
281
+ -- !x! copy gls_newglossary2 from !!gls_alias!! to replacement gls_newglossary in initial
282
+
283
+ -- 5.
284
+ -- !x! use initial
285
+ insert into gls_glossary (!!gls_collist!!)
286
+ select !!gls_collist!! from gls_newglossary;
287
+
288
+ drop table gls_newglossary;
289
+ drop table gls_newentries;
290
+
291
+ -- !x! use !!~entry_alias!!
292
+
293
+ -- !x! END SCRIPT ADD_GLOSSARY_LIST
294
+ -- ################## End of ADD_GLOSSARY_LIST ##################
295
+ -- ################################################################
296
+
297
+
298
+
299
+
300
+
301
+ -- ################################################################
302
+ -- Script ADD_GLOSSARY_ITEM
303
+ -- ===============================================================
304
+ --
305
+ -- Add an object name and definition to the custom glossary.
306
+ -- This does not use the master glossary.
307
+ --
308
+ -- Required input arguments:
309
+ -- item : The name of a column or other item to be
310
+ -- defined in the custom glossary.
311
+ -- definition : The definition of the glossary entry.
312
+ --
313
+ -- Optional input argument:
314
+ -- def_url : The URL of a page that provides additional
315
+ -- information about the item.
316
+ -- ===============================================================
317
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_ITEM with parameters (item, definition)
318
+
319
+ -- !x! sub ~entry_alias !!$current_alias!!
320
+ -- !x! use initial
321
+
322
+ -- !x! if(is_true(gls_has_url))
323
+ -- !x! andif(sub_defined(#def_url))
324
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!, !!gls_url!!)
325
+ select
326
+ inp.item, inp.definition, inp.url
327
+ from
328
+ (select cast('!!#item!!' as varchar(max)) as item, cast('!!#definition!!' as varchar(max)) as definition, cast('!!#def_url!!' as varchar(max)) as url) as inp
329
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
330
+ where
331
+ g.!!gls_name!! is null;
332
+ -- !x! else
333
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!)
334
+ select
335
+ inp.item, inp.definition
336
+ from
337
+ (select cast('!!#item!!' as varchar(max)) as item, cast('!!#definition!!' as varchar(max)) as definition) as inp
338
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
339
+ where
340
+ g.!!gls_name!! is null;
341
+ -- !x! endif
342
+
343
+ -- !x! use !!~entry_alias!!
344
+
345
+ -- !x! END SCRIPT ADD_GLOSSARY_ITEM
346
+ -- ################## End of ADD_GLOSSARY_ITEM ##################
347
+ -- ################################################################
348
+
349
+
350
+
351
+
352
+
353
+ -- ################################################################
354
+ -- Script ADD_GLOSSARY_TABLE
355
+ -- ===============================================================
356
+ --
357
+ -- Add definitions of all columns in a specified table to the
358
+ -- custom glossary.
359
+ --
360
+ -- Required input arguments:
361
+ -- schema : The schema of the table.
362
+ -- table : The table name.
363
+ --
364
+ -- ===============================================================
365
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_TABLE with parameters (schema, table)
366
+
367
+ -- !x! sub ~entry_alias !!$current_alias!!
368
+
369
+ -- Get columns of the specified table into a string.
370
+ drop view if exists gls_collist;
371
+ -- !x! if(is_null("!!#schema!!"))
372
+ create view gls_collist as
373
+ select
374
+ name as column_name
375
+ from
376
+ tempdb.sys.columns
377
+ where
378
+ object_id = object_id('tempdb..!!#table!!')
379
+ ;
380
+ -- !x! else
381
+ create view gls_collist as
382
+ select
383
+ column_name
384
+ from
385
+ information_schema.columns
386
+ where
387
+ table_name = '!!#table!!'
388
+ and table_schema = '!!#schema!!'
389
+ ;
390
+ -- !x! endif
391
+ -- !x! sub_empty ~collist
392
+ -- !x! execute script string_agg with (table_name=gls_collist, string_col=column_name, order_col=column_name, delimiter=", ", string_var=+collist)
393
+
394
+ -- Add all column names in the string.
395
+ -- !x! execute script ADD_GLOSSARY_LIST with (column_list="!!~collist!!")
396
+
397
+ drop view gls_collist;
398
+
399
+ -- !x! use !!~entry_alias!!
400
+
401
+ -- !x! END SCRIPT ADD_GLOSSARY_TABLE
402
+ -- ################# End of ADD_GLOSSARY_TABLE ##################
403
+ -- ################################################################
404
+
405
+