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,291 @@
1
+ -- pg_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 temporary table named "gls_glossary"
23
+ -- in the 'initial' database; b) creates a temporary 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
+ --
40
+ -- VERSION
41
+ -- 1.0.1
42
+ --
43
+ -- HISTORY
44
+ -- Date Remarks
45
+ -- ---------- ---------------------------------------------------------------
46
+ -- 2019-04-17 Created with INIT_GLOSSARY and ADD_GLOSSARY_LIST scripts. RDN.
47
+ -- 2019-04-19 Added ADD_GLOSSARY_ITEM and ADD_GLOSSARY_TABLE scripts.
48
+ -- Debugged--functional. Version 1.0.0. RDN.
49
+ -- 2019-04-20 Modified table copying to use different names in case the
50
+ -- glossary is in the same database that is used for data
51
+ -- summarization. Modified item addition to do nothing on
52
+ -- conflicts. Modified to use subquery instead of CTE when
53
+ -- appending a new item. Version 1.0.1. RDN.
54
+ -- ============================================================================
55
+
56
+
57
+ -- ################################################################
58
+ -- Script INIT_GLOSSARY
59
+ -- ===============================================================
60
+ --
61
+ -- Initialize the custom glossary and describe the master glossary
62
+ -- that will provide descriptions to include in the custom glossary.
63
+ --
64
+ -- Required input arguments:
65
+ -- glossary_db_alias : The execsql alias of the database containing
66
+ -- the master glossary table.
67
+ -- glossary_table : The name of the master glossary table.
68
+ -- name_col : The name of the column in the glossary
69
+ -- table containing the column (or other
70
+ -- object) name.
71
+ -- def_col : The name of the column in the glossary
72
+ -- table containing the definition of the
73
+ -- column (or other object).
74
+ -- Optional input argument:
75
+ -- def_url : The URL of a page that provides additional
76
+ -- information about the column (or other object).
77
+ -- ===============================================================
78
+ -- !x! BEGIN SCRIPT INIT_GLOSSARY with parameters (glossary_db_alias, glossary_table, name_col, def_col)
79
+
80
+ -- !x! sub gls_alias !!#glossary_db_alias!!
81
+ -- !x! sub gls_table !!#glossary_table!!
82
+ -- !x! sub gls_name !!#name_col!!
83
+ -- !x! sub gls_def !!#def_col!!
84
+ -- !x! if(sub_defined(#def_url))
85
+ -- !x! sub gls_has_url Yes
86
+ -- !x! sub gls_url !!#def_url!!
87
+ -- !x! sub ~url_colspec !!gls_url!! text,
88
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!, !!gls_url!!
89
+ -- !x! else
90
+ -- !x! sub gls_has_url No
91
+ -- !x! sub_empty ~url_colspec
92
+ -- !x! sub gls_collist !!gls_name!!, !!gls_def!!
93
+ -- !x! endif
94
+
95
+ -- Create a temporary table for the selected glossary entries in the 'initial' database.
96
+ -- !x! sub ~entry_alias !!$current_alias!!
97
+ -- !x! use initial
98
+ drop table if exists gls_glossary cascade;
99
+ create temporary table gls_glossary (
100
+ !!gls_name!! text,
101
+ !!gls_def!! text,
102
+ !!~url_colspec!!
103
+ constraint pk_gls_glossary primary key (!!gls_name!!)
104
+ );
105
+
106
+ drop view if exists glossary cascade;
107
+ create temporary view glossary as
108
+ select !!gls_collist!!
109
+ from gls_glossary order by !!gls_name!!;
110
+
111
+ -- !x! use !!~entry_alias!!
112
+
113
+ -- !x! END SCRIPT INIT_GLOSSARY
114
+ -- #################### End of INIT_GLOSSARY ####################
115
+ -- ################################################################
116
+
117
+
118
+
119
+
120
+
121
+ -- ################################################################
122
+ -- Script ADD_GLOSSARY_LIST
123
+ -- ===============================================================
124
+ --
125
+ -- Add a specific list of columns to the glossary.
126
+ --
127
+ -- Required input arguments:
128
+ -- column_list : A string of comma-separated column names.
129
+ -- ===============================================================
130
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_LIST with parameters (column_list)
131
+
132
+ -- !x! sub ~entry_alias !!$current_alias!!
133
+ -- !x! use initial
134
+
135
+ drop table if exists gls_column_list cascade;
136
+ select
137
+ trim(regexp_split_to_table('!!#column_list!!', E'\\s*,\\s*')) as !!gls_name!!
138
+ into
139
+ gls_column_list;
140
+
141
+ -- Add any of the column names that are not already in the glossary.
142
+ -- 1. Get list of column names not already in the glossary into a temp table.
143
+ -- 2. Copy that temp table to the glossary database.
144
+ -- 3. In the glossary db, get the glossary information from the master glossary
145
+ -- into another temp table.
146
+ -- 4. Copy that second temp table to the 'initial' database.
147
+ -- 5. In the 'initial' database, append those rows to the gls_glossary table.
148
+
149
+ -- 1.
150
+ drop table if exists gls_newentries;
151
+ select gls_column_list.!!gls_name!!
152
+ into temporary table gls_newentries
153
+ from
154
+ gls_column_list
155
+ left join gls_glossary on gls_glossary.!!gls_name!! = gls_column_list.!!gls_name!!
156
+ where
157
+ gls_glossary.!!gls_name!! is null;
158
+
159
+ -- 2.
160
+ -- Change the name in case it's in the same database.
161
+ -- !x! copy gls_newentries from initial to replacement gls_newentries2 in !!gls_alias!!
162
+
163
+ -- 3.
164
+ -- !x! use !!gls_alias!!
165
+ drop table if exists gls_newglossary2;
166
+ select
167
+ !!gls_collist!!
168
+ into temporary table gls_newglossary2
169
+ from
170
+ !!gls_table!!
171
+ where
172
+ !!gls_name!! in (select !!gls_name!! from gls_newentries2);
173
+
174
+ drop table gls_newentries2 cascade;
175
+
176
+ -- 4.
177
+ -- !x! copy gls_newglossary2 from !!gls_alias!! to replacement gls_newglossary in initial
178
+
179
+ -- 5.
180
+ -- !x! use initial
181
+ insert into gls_glossary (!!gls_collist!!)
182
+ select !!gls_collist!! from gls_newglossary;
183
+
184
+ drop table gls_newglossary cascade;
185
+
186
+ -- !x! use !!~entry_alias!!
187
+
188
+ -- !x! END SCRIPT ADD_GLOSSARY_LIST
189
+ -- ################## End of ADD_GLOSSARY_LIST ##################
190
+ -- ################################################################
191
+
192
+
193
+
194
+
195
+
196
+ -- ################################################################
197
+ -- Script ADD_GLOSSARY_ITEM
198
+ -- ===============================================================
199
+ --
200
+ -- Add an object name and definition to the custom glossary.
201
+ -- This does not use the master glossary.
202
+ --
203
+ -- Required input arguments:
204
+ -- item : The name of a column or other item to be
205
+ -- defined in the custom glossary.
206
+ -- definition : The definition of the glossary entry.
207
+ --
208
+ -- Optional input argument:
209
+ -- def_url : The URL of a page that provides additional
210
+ -- information about the item.
211
+ -- ===============================================================
212
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_ITEM with parameters (item, definition)
213
+
214
+ -- !x! sub ~entry_alias !!$current_alias!!
215
+ -- !x! use initial
216
+
217
+ -- !x! if(is_true(gls_has_url))
218
+ -- !x! andif(sub_defined(#def_url))
219
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!, !!gls_url!!)
220
+ select
221
+ inp.item, inp.definition, inp.url
222
+ from
223
+ (select '!!#item!!'::text as item, '!!#definition!!'::text as definition, '!!#def_url!!'::text as url) as inp
224
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
225
+ where
226
+ g.!!gls_name!! is null;
227
+ -- !x! else
228
+ insert into gls_glossary (!!gls_name!!, !!gls_def!!)
229
+ select
230
+ inp.item, inp.definition
231
+ from
232
+ (select '!!#item!!'::text as item, '!!#definition!!'::text as definition) as inp
233
+ left join gls_glossary as g on g.!!gls_name!! = inp.item
234
+ where
235
+ g.!!gls_name!! is null;
236
+ -- !x! endif
237
+
238
+ -- !x! use !!~entry_alias!!
239
+
240
+ -- !x! END SCRIPT ADD_GLOSSARY_ITEM
241
+ -- ################## End of ADD_GLOSSARY_ITEM ##################
242
+ -- ################################################################
243
+
244
+
245
+
246
+
247
+
248
+ -- ################################################################
249
+ -- Script ADD_GLOSSARY_TABLE
250
+ -- ===============================================================
251
+ --
252
+ -- Add definitions of all columns in a specified table to the
253
+ -- custom glossary.
254
+ --
255
+ -- Required input arguments:
256
+ -- schema : The schema of the table.
257
+ -- table : The table name.
258
+ --
259
+ -- ===============================================================
260
+ -- !x! BEGIN SCRIPT ADD_GLOSSARY_TABLE with parameters (schema, table)
261
+
262
+ -- !x! sub ~entry_alias !!$current_alias!!
263
+
264
+ -- Get columns of the specified table into a string.
265
+ -- !x! if(is_null("!!#schema!!"))
266
+ -- !x! sub_empty ~schema_sel
267
+ -- !x! else
268
+ -- !x! sub ~schema_sel and table_schema = '!!#schema!!'
269
+ -- !x! endif
270
+ drop view if exists gls_collist cascade;
271
+ create temporary view gls_collist as
272
+ select
273
+ string_agg(column_name, ', ') as collist
274
+ from
275
+ information_schema.columns
276
+ where
277
+ table_name = '!!#table!!'
278
+ !!~schema_sel!!
279
+ ;
280
+ -- !x! subdata ~collist gls_collist
281
+
282
+ -- Add all column names in the string.
283
+ -- !x! execute script ADD_GLOSSARY_LIST with (column_list="!!~collist!!")
284
+
285
+ -- !x! use !!~entry_alias!!
286
+
287
+ -- !x! END SCRIPT ADD_GLOSSARY_TABLE
288
+ -- ################# End of ADD_GLOSSARY_TABLE ##################
289
+ -- ################################################################
290
+
291
+