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,327 @@
1
+ -- md_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 Postgres
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 that is not removed when these
24
+ -- scripts complete or the connection is closed; b) creates a
25
+ -- view named "glossary" in the 'initial' database that is also
26
+ -- not automatically removed.
27
+ --
28
+ -- COPYRIGHT AND LICENSE
29
+ -- Copyright (c) 2019, R. Dreas Nielsen
30
+ -- This program is free software: you can redistribute it and/or modify it
31
+ -- under the terms of the GNU General Public License as published by the
32
+ -- Free Software Foundation, either version 3 of the License, or (at your
33
+ -- option) any later version. This program is distributed in the hope that
34
+ -- it will be useful, but WITHOUT ANY WARRANTY; without even the implied
35
+ -- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36
+ -- GNU General Public License for more details. The GNU General Public
37
+ -- License is available at http://www.gnu.org/licenses/.
38
+ --
39
+ -- AUTHORS
40
+ -- Dreas Nielsen (RDN)
41
+ --
42
+ -- VERSION
43
+ -- 1.0.0
44
+ --
45
+ -- HISTORY
46
+ -- Date Remarks
47
+ -- ---------- ---------------------------------------------------------------
48
+ -- 2019-04-23 Created from 2019-04-20 version of pg_glossary.sql. RDN.
49
+ -- 2019-04-24 Wrapped all 'drop table' statements in execsql tests for table
50
+ -- existence, to eliminate spurious messages from the DBMS.
51
+ -- Made the 'gls_glossary' table and 'glossary' view non-temporary
52
+ -- because the 'glossary' view can't reference a temporary table. RDN.
53
+ -- ============================================================================
54
+
55
+
56
+ -- ################################################################
57
+ -- Script INIT_GLOSSARY
58
+ -- ===============================================================
59
+ --
60
+ -- Initialize the custom glossary and describe the master glossary
61
+ -- that will provide descriptions to include in the custom glossary.
62
+ --
63
+ -- Required input arguments:
64
+ -- glossary_db_alias : The execsql alias of the database containing
65
+ -- the master glossary table.
66
+ -- glossary_table : The name of the master glossary table.
67
+ -- name_col : The name of the column in the glossary
68
+ -- table containing the column (or other
69
+ -- object) name.
70
+ -- def_col : The name of the column in the glossary
71
+ -- table containing the definition of the
72
+ -- column (or other object).
73
+ -- Optional input argument:
74
+ -- def_url : The URL of a page that provides additional
75
+ -- information about the column (or other object).
76
+ -- ===============================================================
77
+ -- !x! BEGIN SCRIPT INIT_GLOSSARY with parameters (glossary_db_alias, glossary_table, name_col, def_col)
78
+
79
+ -- !x! sub gls_alias !!#glossary_db_alias!!
80
+ -- !x! sub gls_table !!#glossary_table!!
81
+ -- !x! sub gls_name !!#name_col!!
82
+ -- !x! sub gls_def !!#def_col!!
83
+ -- !x! if(sub_defined(#def_url))
84
+ -- !x! sub gls_has_url Yes
85
+ -- !x! sub gls_url !!#def_url!!
86
+ -- !x! sub ~url_colspec !!gls_url!! text,
87
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!, !!gls_url!!
88
+ -- !x! else
89
+ -- !x! sub gls_has_url No
90
+ -- !x! sub_empty ~url_colspec
91
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!
92
+ -- !x! endif
93
+
94
+ -- Create a temporary table for the selected glossary entries in the 'initial' database.
95
+ -- !x! sub ~entry_alias !!$current_alias!!
96
+ -- !x! use initial
97
+ -- !x! if(table_exists(gls_glossary))
98
+ drop table if exists gls_glossary cascade;
99
+ -- !x! endif
100
+ create table gls_glossary (
101
+ !!gls_name!! varchar(255),
102
+ !!gls_def!! varchar(255),
103
+ !!~url_colspec!!
104
+ constraint pk_gls_glossary primary key (!!gls_name!!)
105
+ );
106
+
107
+ drop view if exists glossary cascade;
108
+ create view glossary as
109
+ select !!gls_collist!!
110
+ from gls_glossary order by !!gls_name!!;
111
+
112
+ -- !x! use !!~entry_alias!!
113
+
114
+ -- !x! END SCRIPT INIT_GLOSSARY
115
+ -- #################### End of INIT_GLOSSARY ####################
116
+ -- ################################################################
117
+
118
+
119
+
120
+
121
+
122
+ -- ################################################################
123
+ -- Script ADD_GLOSSARY_LIST
124
+ -- ===============================================================
125
+ --
126
+ -- Add a specific list of columns to the glossary.
127
+ --
128
+ -- Required input arguments:
129
+ -- column_list : A string of comma-separated column names.
130
+ -- ===============================================================
131
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_LIST with parameters (column_list)
132
+
133
+ -- !x! sub ~entry_alias !!$current_alias!!
134
+ -- !x! use initial
135
+
136
+ -- !x! if(table_exists(gls_column_list))
137
+ drop table if exists gls_column_list cascade;
138
+ -- !x! endif
139
+ create table gls_column_list (
140
+ !!gls_name!! varchar(150) not null unique
141
+ );
142
+
143
+ insert into gls_column_list
144
+ (!!gls_name!!)
145
+ with recursive itemtable as (
146
+ select
147
+ trim(substring_index(data, ',', 1)) as column_name,
148
+ right(data, length(data) - locate(',', data, 1)) as data
149
+ from (select '!!#column_list!!' as data) as input
150
+ union
151
+ select
152
+ trim(substring_index(data, ',', 1)) as column_name,
153
+ right(data, length(data) - locate(',', data, 1)) as data
154
+ from itemtable
155
+ )
156
+ select column_name as item
157
+ from itemtable;
158
+
159
+ -- Add any of the column names that are not already in the glossary.
160
+ -- 1. Get list of column names not already in the glossary into a temp table.
161
+ -- 2. Copy that temp table to the glossary database.
162
+ -- 3. In the glossary db, get the glossary information from the master glossary
163
+ -- into another temp table.
164
+ -- 4. Copy that second temp table to the 'initial' database.
165
+ -- 5. In the 'initial' database, append those rows to the gls_glossary table.
166
+
167
+ -- 1.
168
+ -- !x! if(table_exists(gls_newentries))
169
+ drop table if exists gls_newentries cascade;
170
+ -- !x! endif
171
+ create table gls_newentries
172
+ select gls_column_list.!!gls_name!!
173
+ from
174
+ gls_column_list
175
+ left join gls_glossary on gls_glossary.!!gls_name!! = gls_column_list.!!gls_name!!
176
+ where
177
+ gls_glossary.!!gls_name!! is null;
178
+
179
+ -- 2.
180
+ -- Change the name in case it's in the same database.
181
+ -- !x! copy gls_newentries from initial to replacement gls_newentries2 in !!gls_alias!!
182
+
183
+ -- 3.
184
+ -- !x! use !!gls_alias!!
185
+ -- !x! if(table_exists(gls_newglossary2))
186
+ drop table if exists gls_newglossary2 cascade;
187
+ -- !x! endif
188
+ create table gls_newglossary2
189
+ select
190
+ !!gls_collist!!
191
+ from
192
+ !!gls_table!!
193
+ where
194
+ !!gls_name!! in (select !!gls_name!! from gls_newentries2);
195
+
196
+ -- !x! if(table_exists(gls_newentries2))
197
+ drop table gls_newentries2 cascade;
198
+ -- !x! endif
199
+
200
+ -- 4.
201
+ -- !x! copy gls_newglossary2 from !!gls_alias!! to replacement gls_newglossary in initial
202
+
203
+ -- 5.
204
+ -- !x! use initial
205
+ insert into gls_glossary (!!gls_collist!!)
206
+ select !!gls_collist!! from gls_newglossary;
207
+
208
+ -- Clean up
209
+ -- !x! if(table_exists(gls_newglossary))
210
+ drop table if exists gls_newglossary cascade;
211
+ -- !x! endif
212
+ -- !x! if(table_exists(gls_newentries))
213
+ drop table if exists gls_newentries cascade;
214
+ -- !x! endif
215
+ -- !x! if(table_exists(gls_newglossary2))
216
+ drop table if exists gls_newglossary2 cascade;
217
+ -- !x! endif
218
+
219
+
220
+ -- !x! use !!~entry_alias!!
221
+
222
+ -- !x! END SCRIPT ADD_GLOSSARY_LIST
223
+ -- ################## End of ADD_GLOSSARY_LIST ##################
224
+ -- ################################################################
225
+
226
+
227
+
228
+
229
+
230
+ -- ################################################################
231
+ -- Script ADD_GLOSSARY_ITEM
232
+ -- ===============================================================
233
+ --
234
+ -- Add an object name and definition to the custom glossary.
235
+ -- This does not use the master glossary.
236
+ --
237
+ -- Required input arguments:
238
+ -- item : The name of a column or other item to be
239
+ -- defined in the custom glossary.
240
+ -- definition : The definition of the glossary entry.
241
+ --
242
+ -- Optional input argument:
243
+ -- def_url : The URL of a page that provides additional
244
+ -- information about the item.
245
+ -- ===============================================================
246
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_ITEM with parameters (item, definition)
247
+
248
+ -- !x! sub ~entry_alias !!$current_alias!!
249
+ -- !x! use initial
250
+
251
+ -- !x! if(is_true(gls_has_url))
252
+ -- !x! andif(sub_defined(#def_url))
253
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!, !!gls_url!!)
254
+ select
255
+ inp.item, inp.definition, inp.url
256
+ from
257
+ (select '!!#item!!'::text as item, '!!#definition!!'::text as definition, '!!#def_url!!'::text as url) as inp
258
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
259
+ where
260
+ g.!!gls_name!! is null;
261
+ -- !x! else
262
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!)
263
+ select
264
+ inp.item, inp.definition
265
+ from
266
+ (select
267
+ cast('!!#item!!' as varchar(255)) as item,
268
+ cast('!!#definition!!' as varchar(255)) as definition
269
+ ) as inp
270
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
271
+ where
272
+ g.!!gls_name!! is null;
273
+ -- !x! endif
274
+
275
+ -- !x! use !!~entry_alias!!
276
+
277
+ -- !x! END SCRIPT ADD_GLOSSARY_ITEM
278
+ -- ################## End of ADD_GLOSSARY_ITEM ##################
279
+ -- ################################################################
280
+
281
+
282
+
283
+
284
+
285
+ -- ################################################################
286
+ -- Script ADD_GLOSSARY_TABLE
287
+ -- ===============================================================
288
+ --
289
+ -- Add definitions of all columns in a specified table to the
290
+ -- custom glossary.
291
+ --
292
+ -- Required input arguments:
293
+ -- table : The table name.
294
+ --
295
+ -- ===============================================================
296
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_TABLE with parameters (table)
297
+
298
+ -- !x! sub ~entry_alias !!$current_alias!!
299
+
300
+ -- Get columns of the specified table into a string.
301
+ -- !x! if(view_exists(gls_collist))
302
+ drop view if exists gls_collist cascade;
303
+ -- !x! endif
304
+ create view gls_collist as
305
+ select
306
+ group_concat(column_name separator ', ') as collist
307
+ from
308
+ information_schema.columns
309
+ where
310
+ table_name = '!!#table!!'
311
+ ;
312
+ -- !x! subdata ~collist gls_collist
313
+
314
+ -- Add all column names in the string.
315
+ -- !x! execute script ADD_GLOSSARY_LIST with (column_list="!!~collist!!")
316
+
317
+ -- !x! if(view_exists(gls_collist))
318
+ drop view if exists gls_collist cascade;
319
+ -- !x! endif
320
+
321
+ -- !x! use !!~entry_alias!!
322
+
323
+ -- !x! END SCRIPT ADD_GLOSSARY_TABLE
324
+ -- ################# End of ADD_GLOSSARY_TABLE ##################
325
+ -- ################################################################
326
+
327
+