Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * SQL upgrade code
15 : * N. Nes, M.L. Kersten, S. Mullender
16 : */
17 : #include "monetdb_config.h"
18 : #include "mal_backend.h"
19 : #include "sql_execute.h"
20 : #include "sql_mvc.h"
21 : #include "gdk_time.h"
22 : #include <unistd.h>
23 : #include "sql_upgrades.h"
24 : #include "rel_rel.h"
25 : #include "rel_semantic.h"
26 :
27 : #include "mal_authorize.h"
28 :
29 : /* this function can be used to recreate the system tables (types,
30 : * functions, args) when internal types and/or functions have changed
31 : * (i.e. the ones in sql_types.c) */
32 : static str
33 16 : sql_fix_system_tables(Client c, mvc *sql)
34 : {
35 16 : size_t bufsize = 1000000, pos = 0;
36 16 : char *buf = GDKmalloc(bufsize), *err = NULL;
37 16 : node *n;
38 16 : sql_schema *s;
39 16 : static const char *boolnames[2] = {"false", "true"};
40 :
41 16 : if (buf == NULL)
42 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
43 16 : s = mvc_bind_schema(sql, "sys");
44 :
45 16 : pos += snprintf(buf + pos, bufsize - pos,
46 : "delete from sys.dependencies where id < 2000;\n");
47 :
48 : /* recreate internal types */
49 16 : pos += snprintf(buf + pos, bufsize - pos,
50 : "delete from sys.types where id < 2000;\n");
51 496 : for (n = types->h; n; n = n->next) {
52 480 : sql_type *t = n->data;
53 :
54 480 : if (t->base.id >= FUNC_OIDS)
55 0 : continue;
56 :
57 480 : pos += snprintf(buf + pos, bufsize - pos,
58 : "insert into sys.types values"
59 : " (%d, '%s', '%s', %u, %u, %d, %d, %d);\n",
60 : t->base.id, t->impl, t->base.name, t->digits,
61 480 : t->scale, t->radix, (int) t->eclass,
62 480 : t->s ? t->s->base.id : s->base.id);
63 : }
64 :
65 : /* recreate internal functions */
66 16 : pos += snprintf(buf + pos, bufsize - pos,
67 : "delete from sys.functions where id < 2000;\n"
68 : "delete from sys.args where func_id not in"
69 : " (select id from sys.functions);\n");
70 16 : sqlstore *store = sql->session->tr->store;
71 15120 : for (n = funcs->h; n; n = n->next) {
72 15104 : sql_func *func = n->data;
73 15104 : int number = 0;
74 15104 : sql_arg *arg;
75 15104 : node *m;
76 :
77 15104 : if (func->private || func->base.id >= FUNC_OIDS)
78 3664 : continue;
79 :
80 34320 : pos += snprintf(buf + pos, bufsize - pos,
81 : "insert into sys.functions values"
82 : " (%d, '%s', '%s', '%s',"
83 : " %d, %d, %s, %s, %s, %d, %s, %s, %d);\n",
84 : func->base.id, func->base.name,
85 : sql_func_imp(func), sql_func_mod(func), (int) FUNC_LANG_INT,
86 11440 : (int) func->type,
87 11440 : boolnames[func->side_effect],
88 11440 : boolnames[func->varres],
89 11440 : boolnames[func->vararg],
90 11440 : func->s ? func->s->base.id : s->base.id,
91 11440 : boolnames[func->system],
92 11440 : boolnames[func->semantics],
93 11440 : func->order_required ? 2 : func->opt_order ? 1 : 0);
94 11440 : if (func->res) {
95 22816 : for (m = func->res->h; m; m = m->next, number++) {
96 11408 : arg = m->data;
97 11408 : pos += snprintf(buf + pos, bufsize - pos,
98 : "insert into sys.args"
99 : " values"
100 : " (%d, %d, 'res_%d',"
101 : " '%s', %u, %u, %d,"
102 : " %d);\n",
103 : store_next_oid(store),
104 : func->base.id,
105 : number,
106 11408 : arg->type.type->base.name,
107 : arg->type.digits,
108 : arg->type.scale,
109 11408 : arg->inout, number);
110 : }
111 : }
112 30496 : for (m = func->ops->h; m; m = m->next, number++) {
113 19056 : arg = m->data;
114 19056 : if (arg->name)
115 0 : pos += snprintf(buf + pos, bufsize - pos,
116 : "insert into sys.args"
117 : " values"
118 : " (%d, %d, '%s', '%s',"
119 : " %u, %u, %d, %d);\n",
120 : store_next_oid(store),
121 : func->base.id,
122 : arg->name,
123 0 : arg->type.type->base.name,
124 : arg->type.digits,
125 : arg->type.scale,
126 0 : arg->inout, number);
127 : else
128 19056 : pos += snprintf(buf + pos, bufsize - pos,
129 : "insert into sys.args"
130 : " values"
131 : " (%d, %d, 'arg_%d',"
132 : " '%s', %u, %u, %d,"
133 : " %d);\n",
134 : store_next_oid(store),
135 : func->base.id,
136 : number,
137 19056 : arg->type.type->base.name,
138 : arg->type.digits,
139 : arg->type.scale,
140 19056 : arg->inout, number);
141 : }
142 : }
143 :
144 16 : assert(pos < bufsize);
145 16 : printf("Running database upgrade commands to update system tables.\n\n");
146 16 : fflush(stdout);
147 16 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
148 16 : GDKfree(buf);
149 16 : return err; /* usually MAL_SUCCEED */
150 : }
151 :
152 : static str
153 115 : check_sys_tables(Client c, mvc *m, sql_schema *s)
154 : {
155 115 : struct {
156 : const char *name;
157 : const char *func;
158 : const char *type;
159 : sql_ftype ftype;
160 115 : } tests[] = {
161 : /* tests a few internal functions: the last one created, the
162 : * first one created, and one of the first ones created after
163 : * the geom module */
164 : { "quarter", "quarter", "date", F_FUNC, },
165 : { "sys_update_tables", "update_tables", NULL, F_PROC, },
166 : { "length", "nitems", "blob", F_FUNC, },
167 : { "isnull", "isnil", "void", F_FUNC, },
168 : {0},
169 : };
170 115 : char *err;
171 :
172 : /* if any of the tested function's internal ID does not match the ID
173 : * in the sys.functions table, we recreate the internal part of the
174 : * system tables */
175 511 : for (int i = 0; tests[i].name; i++) {
176 412 : bool needsystabfix = true;
177 412 : sql_subtype tp, *tpp;
178 412 : if (tests[i].type) {
179 313 : sql_find_subtype(&tp, tests[i].type, 0, 0);
180 313 : tpp = &tp;
181 : } else {
182 : tpp = NULL;
183 : }
184 412 : sql_subfunc *f = sql_bind_func(m, s->base.name, tests[i].name, tpp, NULL, tests[i].ftype, true, true);
185 412 : if (f == NULL)
186 16 : throw(SQL, __func__, "cannot find procedure sys.%s(%s)", tests[i].name, tests[i].type ? tests[i].type : "");
187 412 : sqlid id = f->func->base.id;
188 412 : char buf[256];
189 412 : snprintf(buf, sizeof(buf),
190 : "select id from sys.functions where name = '%s' and func = '%s' and schema_id = 2000;\n",
191 : tests[i].name, tests[i].func);
192 412 : res_table *output = NULL;
193 412 : err = SQLstatementIntern(c, buf, "update", true, false, &output);
194 412 : if (err)
195 0 : return err;
196 412 : BAT *b;
197 412 : b = BATdescriptor(output->cols[0].b);
198 412 : res_table_destroy(output);
199 412 : if (b == NULL)
200 0 : throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
201 412 : if (BATcount(b) > 0) {
202 412 : BATiter bi = bat_iterator(b);
203 412 : needsystabfix = * (int *) BUNtloc(bi, 0) != id;
204 412 : bat_iterator_end(&bi);
205 : }
206 412 : BBPunfix(b->batCacheid);
207 412 : if (i == 0 && !needsystabfix) {
208 99 : snprintf(buf, sizeof(buf),
209 : "select a.type from sys.functions f join sys.args a on f.id = a.func_id where f.name = 'quarter' and f.schema_id = 2000 and a.inout = 0 and a.type = 'int';\n");
210 99 : err = SQLstatementIntern(c, buf, "update", true, false, &output);
211 99 : if (err)
212 0 : return err;
213 99 : b = BATdescriptor(output->cols[0].b);
214 99 : res_table_destroy(output);
215 99 : if (b == NULL)
216 0 : throw(SQL, "sql.catalog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
217 99 : needsystabfix = BATcount(b) > 0;
218 99 : BBPunfix(b->batCacheid);
219 : }
220 412 : if (needsystabfix)
221 16 : return sql_fix_system_tables(c, m);
222 : }
223 : return NULL;
224 : }
225 :
226 : #ifdef HAVE_HGE
227 : static str
228 8 : sql_update_hugeint(Client c, mvc *sql)
229 : {
230 8 : size_t bufsize = 8192, pos = 0;
231 8 : char *buf, *err;
232 :
233 8 : (void) sql;
234 8 : if ((buf = GDKmalloc(bufsize)) == NULL)
235 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
236 :
237 : /* 90_generator_hge.sql */
238 8 : pos += snprintf(buf + pos, bufsize - pos,
239 : "create function sys.generate_series(first hugeint, \"limit\" hugeint)\n"
240 : "returns table (value hugeint)\n"
241 : "external name generator.series;\n"
242 : "create function sys.generate_series(first hugeint, \"limit\" hugeint, stepsize hugeint)\n"
243 : "returns table (value hugeint)\n"
244 : "external name generator.series;\n"
245 : "grant execute on function sys.generate_series(hugeint, hugeint) to public;\n"
246 : "grant execute on function sys.generate_series(hugeint, hugeint, hugeint) to public;\n");
247 :
248 : /* 39_analytics_hge.sql */
249 8 : pos += snprintf(buf + pos, bufsize - pos,
250 : "create aggregate stddev_samp(val HUGEINT) returns DOUBLE\n"
251 : " external name \"aggr\".\"stdev\";\n"
252 : "GRANT EXECUTE ON AGGREGATE stddev_samp(HUGEINT) TO PUBLIC;\n"
253 : "create window stddev_samp(val HUGEINT) returns DOUBLE\n"
254 : " external name \"sql\".\"stdev\";\n"
255 : "GRANT EXECUTE ON WINDOW stddev_samp(HUGEINT) TO PUBLIC;\n"
256 : "create aggregate stddev_pop(val HUGEINT) returns DOUBLE\n"
257 : " external name \"aggr\".\"stdevp\";\n"
258 : "GRANT EXECUTE ON AGGREGATE stddev_pop(HUGEINT) TO PUBLIC;\n"
259 : "create window stddev_pop(val HUGEINT) returns DOUBLE\n"
260 : " external name \"sql\".\"stdevp\";\n"
261 : "GRANT EXECUTE ON WINDOW stddev_pop(HUGEINT) TO PUBLIC;\n"
262 : "create aggregate var_samp(val HUGEINT) returns DOUBLE\n"
263 : " external name \"aggr\".\"variance\";\n"
264 : "GRANT EXECUTE ON AGGREGATE var_samp(HUGEINT) TO PUBLIC;\n"
265 : "create window var_samp(val HUGEINT) returns DOUBLE\n"
266 : " external name \"sql\".\"variance\";\n"
267 : "GRANT EXECUTE ON WINDOW var_samp(HUGEINT) TO PUBLIC;\n"
268 : "create aggregate covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
269 : " external name \"aggr\".\"covariance\";\n"
270 : "GRANT EXECUTE ON AGGREGATE covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
271 : "create window covar_samp(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
272 : " external name \"sql\".\"covariance\";\n"
273 : "GRANT EXECUTE ON WINDOW covar_samp(HUGEINT, HUGEINT) TO PUBLIC;\n"
274 : "create aggregate var_pop(val HUGEINT) returns DOUBLE\n"
275 : " external name \"aggr\".\"variancep\";\n"
276 : "GRANT EXECUTE ON AGGREGATE var_pop(HUGEINT) TO PUBLIC;\n"
277 : "create window var_pop(val HUGEINT) returns DOUBLE\n"
278 : " external name \"sql\".\"variancep\";\n"
279 : "GRANT EXECUTE ON WINDOW var_pop(HUGEINT) TO PUBLIC;\n"
280 : "create aggregate covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
281 : " external name \"aggr\".\"covariancep\";\n"
282 : "GRANT EXECUTE ON AGGREGATE covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
283 : "create window covar_pop(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
284 : " external name \"sql\".\"covariancep\";\n"
285 : "GRANT EXECUTE ON WINDOW covar_pop(HUGEINT, HUGEINT) TO PUBLIC;\n"
286 : "create aggregate median(val HUGEINT) returns HUGEINT\n"
287 : " external name \"aggr\".\"median\";\n"
288 : "GRANT EXECUTE ON AGGREGATE median(HUGEINT) TO PUBLIC;\n"
289 : "create aggregate quantile(val HUGEINT, q DOUBLE) returns HUGEINT\n"
290 : " external name \"aggr\".\"quantile\";\n"
291 : "GRANT EXECUTE ON AGGREGATE quantile(HUGEINT, DOUBLE) TO PUBLIC;\n"
292 : "create aggregate median_avg(val HUGEINT) returns DOUBLE\n"
293 : " external name \"aggr\".\"median_avg\";\n"
294 : "GRANT EXECUTE ON AGGREGATE median_avg(HUGEINT) TO PUBLIC;\n"
295 : "create aggregate quantile_avg(val HUGEINT, q DOUBLE) returns DOUBLE\n"
296 : " external name \"aggr\".\"quantile_avg\";\n"
297 : "GRANT EXECUTE ON AGGREGATE quantile_avg(HUGEINT, DOUBLE) TO PUBLIC;\n"
298 : "create aggregate corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
299 : " external name \"aggr\".\"corr\";\n"
300 : "GRANT EXECUTE ON AGGREGATE corr(HUGEINT, HUGEINT) TO PUBLIC;\n"
301 : "create window corr(e1 HUGEINT, e2 HUGEINT) returns DOUBLE\n"
302 : " external name \"sql\".\"corr\";\n"
303 : "GRANT EXECUTE ON WINDOW corr(HUGEINT, HUGEINT) TO PUBLIC;\n"
304 : "create aggregate median(val DECIMAL(38)) returns DECIMAL(38)\n"
305 : " external name \"aggr\".\"median\";\n"
306 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(38)) TO PUBLIC;\n"
307 : "create aggregate median_avg(val DECIMAL(38)) returns DOUBLE\n"
308 : " external name \"aggr\".\"median_avg\";\n"
309 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(38)) TO PUBLIC;\n"
310 : "create aggregate quantile(val DECIMAL(38), q DOUBLE) returns DECIMAL(38)\n"
311 : " external name \"aggr\".\"quantile\";\n"
312 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(38), DOUBLE) TO PUBLIC;\n"
313 : "create aggregate quantile_avg(val DECIMAL(38), q DOUBLE) returns DOUBLE\n"
314 : " external name \"aggr\".\"quantile_avg\";\n"
315 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(38), DOUBLE) TO PUBLIC;\n");
316 :
317 : /* 40_json_hge.sql */
318 8 : pos += snprintf(buf + pos, bufsize - pos,
319 : "create function json.filter(js json, name hugeint)\n"
320 : "returns json external name json.filter;\n"
321 : "GRANT EXECUTE ON FUNCTION json.filter(json, hugeint) TO PUBLIC;\n");
322 :
323 8 : pos += snprintf(buf + pos, bufsize - pos,
324 : "update sys.functions set system = true where system <> true and name in ('generate_series') and schema_id = 2000 and type = %d;\n"
325 : "update sys.functions set system = true where system <> true and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'covar_samp', 'var_pop', 'covar_pop', 'median', 'median_avg', 'quantile', 'quantile_avg', 'corr') and schema_id = 2000 and type = %d;\n"
326 : "update sys.functions set system = true where system <> true and name in ('stddev_samp', 'stddev_pop', 'var_samp', 'covar_samp', 'var_pop', 'covar_pop', 'corr') and schema_id = 2000 and type = %d;\n"
327 : "update sys.functions set system = true where system <> true and name = 'filter' and schema_id = (select id from sys.schemas where name = 'json') and type = %d;\n",
328 : (int) F_UNION, (int) F_AGGR, (int) F_ANALYTIC, (int) F_FUNC);
329 :
330 8 : assert(pos < bufsize);
331 :
332 8 : printf("Running database upgrade commands:\n%s\n", buf);
333 8 : fflush(stdout);
334 8 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
335 8 : GDKfree(buf);
336 8 : return err; /* usually MAL_SUCCEED */
337 : }
338 : #endif
339 :
340 : #ifdef HAVE_SHP
341 : static str
342 0 : sql_create_shp(Client c)
343 : {
344 : //Create the new SHPload procedures
345 0 : const char query[] = "create procedure SHPLoad(fname string, schemaname string, tablename string) external name shp.load;\n"
346 : "create procedure SHPLoad(fname string, tablename string) external name shp.load;\n"
347 : "update sys.functions set system = true where schema_id = 2000 and name in ('shpload');\n";
348 0 : printf("Running database upgrade commands:\n%s\n", query);
349 0 : fflush(stdout);
350 0 : return SQLstatementIntern(c, query, "update", true, false, NULL);
351 : }
352 : #endif
353 :
354 : static str
355 0 : sql_drop_shp(Client c)
356 : {
357 : //Drop the old SHP procedures (upgrade from version before shpload upgrade)
358 0 : const char query[] = "drop procedure if exists SHPattach(string) cascade;\n"
359 : "drop procedure if exists SHPload(integer) cascade;\n"
360 : "drop procedure if exists SHPload(integer, geometry) cascade;\n";
361 0 : printf("Running database upgrade commands:\n%s\n", query);
362 0 : fflush(stdout);
363 0 : return SQLstatementIntern(c, query, "update", true, false, NULL);
364 : }
365 :
366 : static str
367 115 : sql_update_jan2022(Client c, mvc *sql)
368 : {
369 115 : sql_subtype tp;
370 115 : size_t bufsize = 65536, pos = 0;
371 115 : char *buf = NULL, *err = NULL;
372 115 : sql_schema *s = mvc_bind_schema(sql, "sys");
373 115 : sql_table *t;
374 :
375 115 : sql_find_subtype(&tp, "bigint", 0, 0);
376 115 : if (!sql_bind_func(sql, s->base.name, "epoch", &tp, NULL, F_FUNC, true, true)) {
377 115 : sql->session->status = 0; /* if the function was not found clean the error */
378 115 : sql->errstr[0] = '\0';
379 : /* nothing to do */
380 115 : return NULL;
381 : }
382 :
383 0 : if ((buf = GDKmalloc(bufsize)) == NULL)
384 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
385 :
386 : /* sys.epoch_ms now returns a decimal(18,3) */
387 0 : pos += snprintf(buf + pos, bufsize - pos,
388 : "update sys.args set type = 'decimal', type_digits = 18, type_scale = 3 where func_id in (select id from sys.functions where name = 'epoch_ms' and schema_id = 2000) and number = 0 and type = 'bigint';\n");
389 :
390 : /* 16_tracelog */
391 0 : t = mvc_bind_table(sql, s, "tracelog");
392 0 : t->system = 0; /* make it non-system else the drop view will fail */
393 0 : pos += snprintf(buf + pos, bufsize - pos,
394 : "drop view sys.tracelog cascade;\n"
395 : "drop function sys.tracelog() cascade;\n"
396 : "create function sys.tracelog()\n"
397 : " returns table (\n"
398 : " ticks bigint, -- time in microseconds\n"
399 : " stmt string, -- actual statement executed\n"
400 : " event string -- profiler event executed\n"
401 : " )\n"
402 : " external name sql.dump_trace;\n"
403 : "create view sys.tracelog as select * from sys.tracelog();\n"
404 : "update sys._tables set system = true where system <> true and schema_id = 2000"
405 : " and name = 'tracelog';\n"
406 : "update sys.functions set system = true where system <> true and schema_id = 2000"
407 : " and name = 'tracelog' and type = %d;\n", (int) F_UNION);
408 :
409 : /* 17_temporal.sql */
410 0 : pos += snprintf(buf + pos, bufsize - pos,
411 : "drop function sys.epoch(bigint) cascade;\n");
412 0 : pos += snprintf(buf + pos, bufsize - pos,
413 : "create function sys.epoch(sec DECIMAL(18,3)) "
414 : "returns TIMESTAMP WITH TIME ZONE\n"
415 : "external name mtime.epoch;\n"
416 : "grant execute on function sys.epoch (DECIMAL(18,3)) to public;\n"
417 : "update sys.functions set system = true where system <> true and name in ('epoch') and schema_id = 2000 and type = %d;\n", F_FUNC);
418 :
419 : /* 25_debug.sql */
420 0 : pos += snprintf(buf + pos, bufsize - pos,
421 : "drop function sys.malfunctions() cascade;\n"
422 : "create function sys.malfunctions()\n"
423 : " returns table(\"module\" string, \"function\" string, \"signature\" string, \"address\" string, \"comment\" string)\n"
424 : " external name \"manual\".\"functions\";\n"
425 : "create view sys.malfunctions as select * from sys.malfunctions();\n"
426 : "update sys._tables set system = true where system <> true and schema_id = 2000"
427 : " and name = 'malfunctions';\n"
428 : "update sys.functions set system = true where system <> true and schema_id = 2000"
429 : " and name = 'malfunctions';\n");
430 :
431 : /* 21_dependency_views.sql */
432 0 : t = mvc_bind_table(sql, s, "ids");
433 0 : t->system = 0; /* make it non-system else the drop view will fail */
434 0 : t = mvc_bind_table(sql, s, "dependencies_vw");
435 0 : t->system = 0; /* make it non-system else the drop view will fail */
436 0 : pos += snprintf(buf + pos, bufsize - pos,
437 : "drop view sys.dependencies_vw cascade;\n" /* depends on sys.ids */
438 : "drop view sys.ids cascade;\n"
439 : "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table, system) AS\n"
440 : "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'author' AS obj_type, 'sys.auths' AS sys_table, (name in ('public','sysadmin','monetdb','.snapshot')) AS system FROM sys.auths UNION ALL\n"
441 : "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, ifthenelse(system, 'system schema', 'schema'), 'sys.schemas', system FROM sys.schemas UNION ALL\n"
442 : "SELECT t.id, name, t.schema_id, t.id as table_id, t.name as table_name, cast(lower(tt.table_type_name) as varchar(40)), 'sys.tables', t.system FROM sys.tables t left outer join sys.table_types tt on t.type = tt.table_type_id UNION ALL\n"
443 : "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, ifthenelse(t.system, 'system column', 'column'), 'sys._columns', t.system FROM sys._columns c JOIN sys._tables t ON c.table_id = t.id UNION ALL\n"
444 : "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, 'column', 'tmp._columns', t.system FROM tmp._columns c JOIN tmp._tables t ON c.table_id = t.id UNION ALL\n"
445 : "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, ifthenelse(t.system, 'system key', 'key'), 'sys.keys', t.system FROM sys.keys k JOIN sys._tables t ON k.table_id = t.id UNION ALL\n"
446 : "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, 'key', 'tmp.keys', t.system FROM tmp.keys k JOIN tmp._tables t ON k.table_id = t.id UNION ALL\n"
447 : "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, ifthenelse(t.system, 'system index', 'index'), 'sys.idxs', t.system FROM sys.idxs i JOIN sys._tables t ON i.table_id = t.id UNION ALL\n"
448 : "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, 'index' , 'tmp.idxs', t.system FROM tmp.idxs i JOIN tmp._tables t ON i.table_id = t.id UNION ALL\n"
449 : "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, ifthenelse(t.system, 'system trigger', 'trigger'), 'sys.triggers', t.system FROM sys.triggers g JOIN sys._tables t ON g.table_id = t.id UNION ALL\n"
450 : "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, 'trigger', 'tmp.triggers', t.system FROM tmp.triggers g JOIN tmp._tables t ON g.table_id = t.id UNION ALL\n"
451 : "SELECT f.id, f.name, f.schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) as varchar(40)), 'sys.functions', f.system FROM sys.functions f left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
452 : "SELECT a.id, a.name, f.schema_id, a.func_id as table_id, f.name as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) || ' arg' as varchar(44)), 'sys.args', f.system FROM sys.args a JOIN sys.functions f ON a.func_id = f.id left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
453 : "SELECT id, name, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'sequence', 'sys.sequences', false FROM sys.sequences UNION ALL\n"
454 : "SELECT o.id, o.name, pt.schema_id, pt.id, pt.name, 'partition of merge table', 'sys.objects', false FROM sys.objects o JOIN sys._tables pt ON o.sub = pt.id JOIN sys._tables mt ON o.nr = mt.id WHERE mt.type = 3 UNION ALL\n"
455 : "SELECT id, sqlname, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'type', 'sys.types', (sqlname in ('inet','json','url','uuid')) FROM sys.types WHERE id > 2000\n"
456 : " ORDER BY id;\n"
457 : "GRANT SELECT ON sys.ids TO PUBLIC;\n");
458 0 : pos += snprintf(buf + pos, bufsize - pos,
459 : "CREATE VIEW sys.dependencies_vw AS\n"
460 : "SELECT d.id, i1.obj_type, i1.name,\n"
461 : " d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
462 : " d.depend_type, dt.dependency_type_name\n"
463 : " FROM sys.dependencies d\n"
464 : " JOIN sys.ids i1 ON d.id = i1.id\n"
465 : " JOIN sys.ids i2 ON d.depend_id = i2.id\n"
466 : " JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
467 : " ORDER BY id, depend_id;\n"
468 : "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n");
469 0 : pos += snprintf(buf + pos, bufsize - pos,
470 : "UPDATE sys._tables SET system = true WHERE name in ('ids', 'dependencies_vw') AND schema_id = 2000;\n");
471 :
472 : /* 52_describe.sql; but we need to drop most everything from
473 : * 76_dump.sql first */
474 0 : t = mvc_bind_table(sql, s, "describe_comments");
475 0 : t->system = 0;
476 0 : t = mvc_bind_table(sql, s, "describe_constraints");
477 0 : t->system = 0;
478 0 : t = mvc_bind_table(sql, s, "describe_functions");
479 0 : t->system = 0;
480 0 : t = mvc_bind_table(sql, s, "describe_partition_tables");
481 0 : t->system = 0;
482 0 : t = mvc_bind_table(sql, s, "describe_privileges");
483 0 : t->system = 0;
484 0 : t = mvc_bind_table(sql, s, "describe_sequences");
485 0 : t->system = 0;
486 0 : t = mvc_bind_table(sql, s, "describe_tables");
487 0 : t->system = 0;
488 0 : t = mvc_bind_table(sql, s, "dump_add_schemas_to_users");
489 0 : t->system = 0;
490 0 : t = mvc_bind_table(sql, s, "dump_column_defaults");
491 0 : t->system = 0;
492 0 : t = mvc_bind_table(sql, s, "dump_comments");
493 0 : t->system = 0;
494 0 : t = mvc_bind_table(sql, s, "dump_create_roles");
495 0 : t->system = 0;
496 0 : t = mvc_bind_table(sql, s, "dump_create_schemas");
497 0 : t->system = 0;
498 0 : t = mvc_bind_table(sql, s, "dump_create_users");
499 0 : t->system = 0;
500 0 : t = mvc_bind_table(sql, s, "dump_foreign_keys");
501 0 : t->system = 0;
502 0 : t = mvc_bind_table(sql, s, "dump_functions");
503 0 : t->system = 0;
504 0 : t = mvc_bind_table(sql, s, "dump_grant_user_privileges");
505 0 : t->system = 0;
506 0 : t = mvc_bind_table(sql, s, "dump_indices");
507 0 : t->system = 0;
508 0 : t = mvc_bind_table(sql, s, "dump_partition_tables");
509 0 : t->system = 0;
510 0 : t = mvc_bind_table(sql, s, "dump_privileges");
511 0 : t->system = 0;
512 0 : t = mvc_bind_table(sql, s, "dump_sequences");
513 0 : t->system = 0;
514 0 : t = mvc_bind_table(sql, s, "dump_start_sequences");
515 0 : t->system = 0;
516 0 : t = mvc_bind_table(sql, s, "dump_table_constraint_type");
517 0 : t->system = 0;
518 0 : t = mvc_bind_table(sql, s, "dump_tables");
519 0 : t->system = 0;
520 0 : t = mvc_bind_table(sql, s, "dump_triggers");
521 0 : t->system = 0;
522 0 : t = mvc_bind_table(sql, s, "dump_user_defined_types");
523 0 : t->system = 0;
524 0 : t = mvc_bind_table(sql, s, "fully_qualified_functions");
525 0 : t->system = 0;
526 0 : pos += snprintf(buf + pos, bufsize - pos,
527 : /* drop dependent stuff from 76_dump.sql */
528 : "drop function sys.dump_database(boolean) cascade;\n"
529 : "drop procedure sys.dump_table_data() cascade;\n"
530 : "drop procedure sys._dump_table_data(string, string) cascade;\n"
531 : "drop function sys.prepare_esc(string, string) cascade;\n"
532 : "drop function sys.esc(string) cascade;\n"
533 : "drop view sys.dump_privileges cascade;\n"
534 : "drop view sys.dump_user_defined_types cascade;\n"
535 : "drop view sys.dump_comments cascade;\n"
536 : "drop view sys.dump_triggers cascade;\n"
537 : "drop view sys.dump_tables cascade;\n"
538 : "drop view sys.dump_functions cascade;\n"
539 : "drop view sys.dump_start_sequences cascade;\n"
540 : "drop view sys.dump_sequences cascade;\n"
541 : "drop view sys.dump_partition_tables cascade;\n"
542 : "drop view sys.dump_foreign_keys cascade;\n"
543 : "drop view sys.dump_column_defaults cascade;\n"
544 : "drop view sys.dump_indices cascade;\n"
545 : "drop view sys.dump_table_constraint_type cascade;\n"
546 : "drop view sys.dump_grant_user_privileges cascade;\n"
547 : "drop view sys.dump_add_schemas_to_users cascade;\n"
548 : "drop view sys.dump_create_schemas cascade;\n"
549 : "drop view sys.dump_create_users cascade;\n"
550 : "drop view sys.dump_create_roles cascade;\n"
551 :
552 : "drop view sys.describe_functions cascade;\n"
553 : "drop view sys.describe_partition_tables cascade;\n"
554 : "drop view sys.describe_privileges cascade;\n"
555 : "drop view sys.fully_qualified_functions cascade;\n"
556 : "drop view sys.describe_comments cascade;\n"
557 : "drop view sys.describe_tables cascade;\n"
558 : "drop view sys.describe_sequences cascade;\n"
559 : "drop function sys.schema_guard(string, string, string) cascade;\n"
560 : "drop function sys.get_remote_table_expressions(string, string) cascade;\n"
561 : "drop function sys.get_merge_table_partition_expressions(int) cascade;\n"
562 : "drop view sys.describe_constraints cascade;\n"
563 : "drop function sys.alter_table(string, string) cascade;\n"
564 : "drop function sys.FQN(string, string) cascade;\n"
565 : "drop function sys.sq(string) cascade;\n");
566 0 : pos += snprintf(buf + pos, bufsize - pos,
567 : "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
568 : "CREATE FUNCTION sys.FQN(s STRING, t STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(s,'\"','\"\"') || '\".\"' || sys.replace(t,'\"','\"\"') || '\"'; END;\n"
569 : "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
570 : "RETURN\n"
571 : " SELECT sys.replace_first(stmt, '(\\\\s*\"?' || sch || '\"?\\\\s*\\\\.|)\\\\s*\"?' || nme || '\"?\\\\s*', ' ' || sys.FQN(sch, nme) || ' ', 'imsx');\n"
572 : "END;\n"
573 : "CREATE VIEW sys.describe_constraints AS\n"
574 : " SELECT\n"
575 : " s.name sch,\n"
576 : " t.name tbl,\n"
577 : " kc.name col,\n"
578 : " k.name con,\n"
579 : " CASE k.type WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' END tpe\n"
580 : " FROM sys.schemas s, sys._tables t, sys.objects kc, sys.keys k\n"
581 : " WHERE kc.id = k.id\n"
582 : " AND k.table_id = t.id\n"
583 : " AND s.id = t.schema_id\n"
584 : " AND t.system = FALSE\n"
585 : " AND k.type in (0, 1);\n"
586 : "CREATE FUNCTION sys.get_merge_table_partition_expressions(tid INT) RETURNS STRING\n"
587 : "BEGIN\n"
588 : " RETURN\n"
589 : " SELECT\n"
590 : " CASE WHEN tp.table_id IS NOT NULL THEN\n"
591 : " ' PARTITION BY ' ||\n"
592 : " ifthenelse(bit_and(tp.type, 2) = 2, 'VALUES ', 'RANGE ') ||\n"
593 : " CASE\n"
594 : " WHEN bit_and(tp.type, 4) = 4\n"
595 : " THEN 'ON ' || '(' || (SELECT sys.DQ(c.name) || ')' FROM sys.columns c WHERE c.id = tp.column_id)\n"
596 : " ELSE 'USING ' || '(' || tp.expression || ')'\n"
597 : " END\n"
598 : " ELSE\n"
599 : " ''\n"
600 : " END\n"
601 : " FROM (VALUES (tid)) t(id) LEFT JOIN sys.table_partitions tp ON t.id = tp.table_id;\n"
602 : "END;\n"
603 : "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
604 : " RETURN SELECT ' ON ' || sys.SQ(uri) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(\"hash\") FROM sys.remote_table_credentials(s ||'.' || t);\n"
605 : "END;\n"
606 : "CREATE VIEW sys.describe_tables AS\n"
607 : " SELECT\n"
608 : " t.id o,\n"
609 : " s.name sch,\n"
610 : " t.name tab,\n"
611 : " ts.table_type_name typ,\n"
612 : " (SELECT\n"
613 : " ' (' ||\n"
614 : " GROUP_CONCAT(\n"
615 : " sys.DQ(c.name) || ' ' ||\n"
616 : " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
617 : " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
618 : " , ', ') || ')'\n"
619 : " FROM sys._columns c\n"
620 : " WHERE c.table_id = t.id) col,\n"
621 : " CASE ts.table_type_name\n"
622 : " WHEN 'REMOTE TABLE' THEN\n"
623 : " sys.get_remote_table_expressions(s.name, t.name)\n"
624 : " WHEN 'MERGE TABLE' THEN\n"
625 : " sys.get_merge_table_partition_expressions(t.id)\n"
626 : " WHEN 'VIEW' THEN\n"
627 : " sys.schema_guard(s.name, t.name, t.query)\n"
628 : " ELSE\n"
629 : " ''\n"
630 : " END opt\n"
631 : " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
632 : " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE')\n"
633 : " AND t.system = FALSE\n"
634 : " AND s.id = t.schema_id\n"
635 : " AND ts.table_type_id = t.type\n"
636 : " AND s.name <> 'tmp';\n"
637 : "CREATE VIEW sys.fully_qualified_functions AS\n"
638 : " WITH fqn(id, tpe, sig, num) AS\n"
639 : " (\n"
640 : " SELECT\n"
641 : " f.id,\n"
642 : " ft.function_type_keyword,\n"
643 : " CASE WHEN a.type IS NULL THEN\n"
644 : " sys.fqn(s.name, f.name) || '()'\n"
645 : " ELSE\n"
646 : " sys.fqn(s.name, f.name) || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',') OVER (PARTITION BY f.id ORDER BY a.number) || ')'\n"
647 : " END,\n"
648 : " a.number\n"
649 : " FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
650 : " WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
651 : " )\n"
652 : " SELECT\n"
653 : " fqn1.id id,\n"
654 : " fqn1.tpe tpe,\n"
655 : " fqn1.sig nme\n"
656 : " FROM\n"
657 : " fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id) fqn2(id, num)\n"
658 : " ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
659 : "CREATE VIEW sys.describe_comments AS\n"
660 : " SELECT\n"
661 : " o.id id,\n"
662 : " o.tpe tpe,\n"
663 : " o.nme fqn,\n"
664 : " c.remark rem\n"
665 : " FROM (\n"
666 : " SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas\n"
667 : " UNION ALL\n"
668 : " SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
669 : " FROM sys.schemas s JOIN sys.tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
670 : " WHERE s.name <> 'tmp'\n"
671 : " UNION ALL\n"
672 : " SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys.tables t, sys.schemas s WHERE c.table_id = t.id AND t.schema_id = s.id\n"
673 : " UNION ALL\n"
674 : " SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE idx.table_id = t.id AND t.schema_id = s.id\n"
675 : " UNION ALL\n"
676 : " SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
677 : " UNION ALL\n"
678 : " SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf WHERE f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
679 : " ) AS o(id, tpe, nme)\n"
680 : " JOIN sys.comments c ON c.id = o.id;\n"
681 : "CREATE VIEW sys.describe_privileges AS\n"
682 : " SELECT\n"
683 : " CASE\n"
684 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
685 : " 'COPY FROM'\n"
686 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
687 : " 'COPY INTO'\n"
688 : " ELSE\n"
689 : " o.nme\n"
690 : " END o_nme,\n"
691 : " coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
692 : " pc.privilege_code_name p_nme,\n"
693 : " a.name a_nme,\n"
694 : " g.name g_nme,\n"
695 : " p.grantable grantable\n"
696 : " FROM\n"
697 : " sys.privileges p LEFT JOIN\n"
698 : " (\n"
699 : " SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
700 : " from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
701 : " UNION ALL\n"
702 : " SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
703 : " FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
704 : " UNION ALL\n"
705 : " SELECT f.id, f.nme, f.tpe\n"
706 : " FROM sys.fully_qualified_functions f\n"
707 : " ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
708 : " sys.privilege_codes pc,\n"
709 : " auths a, auths g\n"
710 : " WHERE\n"
711 : " p.privileges = pc.privilege_code_id AND\n"
712 : " p.auth_id = a.id AND\n"
713 : " p.grantor = g.id;\n"
714 : "CREATE VIEW sys.describe_partition_tables AS\n"
715 : " SELECT \n"
716 : " m_sch,\n"
717 : " m_tbl,\n"
718 : " p_sch,\n"
719 : " p_tbl,\n"
720 : " CASE\n"
721 : " WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
722 : " WHEN (p_raw_type = 'VALUES' AND pvalues IS NULL) OR (p_raw_type = 'RANGE' AND minimum IS NULL AND maximum IS NULL AND with_nulls) THEN 'FOR NULLS'\n"
723 : " ELSE p_raw_type\n"
724 : " END AS tpe,\n"
725 : " pvalues,\n"
726 : " minimum,\n"
727 : " maximum,\n"
728 : " with_nulls\n"
729 : " FROM \n"
730 : " (WITH\n"
731 : " tp(\"type\", table_id) AS\n"
732 : " (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
733 : " subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
734 : " (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
735 : " FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
736 : " WHERE m_t.\"type\" IN (3, 6)\n"
737 : " AND m_t.schema_id = m_s.id\n"
738 : " AND m_s.name <> 'tmp'\n"
739 : " AND m_t.system = FALSE\n"
740 : " AND m_t.id = d.depend_id\n"
741 : " AND d.id = p_m.id\n"
742 : " AND p_m.schema_id = p_s.id\n"
743 : " ORDER BY m_t.id, p_m.id)\n"
744 : " SELECT\n"
745 : " subq.m_sch,\n"
746 : " subq.m_tbl,\n"
747 : " subq.p_sch,\n"
748 : " subq.p_tbl,\n"
749 : " tp.\"type\" AS p_raw_type,\n"
750 : " CASE WHEN tp.\"type\" = 'VALUES'\n"
751 : " THEN (SELECT GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid)\n"
752 : " ELSE NULL\n"
753 : " END AS pvalues,\n"
754 : " CASE WHEN tp.\"type\" = 'RANGE'\n"
755 : " THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
756 : " ELSE NULL\n"
757 : " END AS minimum,\n"
758 : " CASE WHEN tp.\"type\" = 'RANGE'\n"
759 : " THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
760 : " ELSE NULL\n"
761 : " END AS maximum,\n"
762 : " CASE WHEN tp.\"type\" = 'VALUES'\n"
763 : " THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
764 : " ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
765 : " END AS with_nulls\n"
766 : " FROM \n"
767 : " subq LEFT OUTER JOIN tp\n"
768 : " ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
769 : "CREATE VIEW sys.describe_functions AS\n"
770 : " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
771 : " (\n"
772 : " SELECT\n"
773 : " func_id,\n"
774 : " number,\n"
775 : " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
776 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
777 : " FROM sys.args\n"
778 : " WHERE inout = 1\n"
779 : " ),\n"
780 : " func_args(func_id, func_arg) AS\n"
781 : " (\n"
782 : " SELECT func_id, func_arg\n"
783 : " FROM func_args_all\n"
784 : " WHERE number = max_number\n"
785 : " ),\n"
786 : " func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
787 : " (\n"
788 : " SELECT\n"
789 : " func_id,\n"
790 : " number,\n"
791 : " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
792 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
793 : " group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
794 : " FROM sys.args\n"
795 : " WHERE inout = 0\n"
796 : " ),\n"
797 : " func_rets(func_id, func_ret, func_ret_type) AS\n"
798 : " (\n"
799 : " SELECT\n"
800 : " func_id,\n"
801 : " func_ret,\n"
802 : " func_ret_type\n"
803 : " FROM func_rets_all\n"
804 : " WHERE number = max_number\n"
805 : " )\n"
806 : " SELECT\n"
807 : " f.id o,\n"
808 : " s.name sch,\n"
809 : " f.name fun,\n"
810 : " CASE WHEN f.language IN (1, 2) THEN f.func ELSE 'CREATE ' || ft.function_type_keyword || ' ' || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ')' || CASE WHEN f.type = 5 THEN ' RETURNS TABLE (' || coalesce(fr.func_ret, '') || ')' WHEN f.type IN (1,3) THEN ' RETURNS ' || fr.func_ret_type ELSE '' END || CASE WHEN fl.language_keyword IS NULL THEN '' ELSE ' LANGUAGE ' || fl.language_keyword END || ' ' || f.func END def\n"
811 : " FROM sys.functions f\n"
812 : " LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
813 : " LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
814 : " JOIN sys.schemas s ON f.schema_id = s.id\n"
815 : " JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
816 : " LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
817 : " WHERE s.name <> 'tmp' AND NOT f.system;\n"
818 : "CREATE VIEW sys.describe_sequences AS\n"
819 : " SELECT\n"
820 : " s.name sch,\n"
821 : " seq.name seq,\n"
822 : " seq.\"start\" s,\n"
823 : " get_value_for(s.name, seq.name) rs,\n"
824 : " seq.\"minvalue\" mi,\n"
825 : " seq.\"maxvalue\" ma,\n"
826 : " seq.\"increment\" inc,\n"
827 : " seq.\"cacheinc\" cache,\n"
828 : " seq.\"cycle\" cycle,\n"
829 : " CASE WHEN seq.\"minvalue\" = -9223372036854775807 AND seq.\"increment\" > 0 AND seq.\"start\" = 1 THEN TRUE ELSE FALSE END nomin,\n"
830 : " CASE WHEN seq.\"maxvalue\" = 9223372036854775807 AND seq.\"increment\" < 0 AND seq.\"start\" = -1 THEN TRUE ELSE FALSE END nomax,\n"
831 : " CASE\n"
832 : " WHEN seq.\"minvalue\" = 0 AND seq.\"increment\" > 0 THEN NULL\n"
833 : " WHEN seq.\"minvalue\" <> -9223372036854775807 THEN seq.\"minvalue\"\n"
834 : " ELSE\n"
835 : " CASE\n"
836 : " WHEN seq.\"increment\" < 0 THEN NULL\n"
837 : " ELSE CASE WHEN seq.\"start\" = 1 THEN NULL ELSE seq.\"maxvalue\" END\n"
838 : " END\n"
839 : " END rmi,\n"
840 : " CASE\n"
841 : " WHEN seq.\"maxvalue\" = 0 AND seq.\"increment\" < 0 THEN NULL\n"
842 : " WHEN seq.\"maxvalue\" <> 9223372036854775807 THEN seq.\"maxvalue\"\n"
843 : " ELSE\n"
844 : " CASE\n"
845 : " WHEN seq.\"increment\" > 0 THEN NULL\n"
846 : " ELSE CASE WHEN seq.\"start\" = -1 THEN NULL ELSE seq.\"maxvalue\" END\n"
847 : " END\n"
848 : " END rma\n"
849 : " FROM sys.sequences seq, sys.schemas s\n"
850 : " WHERE s.id = seq.schema_id\n"
851 : " AND s.name <> 'tmp'\n"
852 : " ORDER BY s.name, seq.name;\n"
853 : "GRANT SELECT ON sys.describe_constraints TO PUBLIC;\n"
854 : "GRANT SELECT ON sys.describe_indices TO PUBLIC;\n"
855 : "GRANT SELECT ON sys.describe_column_defaults TO PUBLIC;\n"
856 : "GRANT SELECT ON sys.describe_foreign_keys TO PUBLIC;\n"
857 : "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
858 : "GRANT SELECT ON sys.describe_triggers TO PUBLIC;\n"
859 : "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
860 : "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
861 : "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
862 : "GRANT SELECT ON sys.describe_user_defined_types TO PUBLIC;\n"
863 : "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
864 : "GRANT SELECT ON sys.describe_sequences TO PUBLIC;\n"
865 : "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n");
866 0 : pos += snprintf(buf + pos, bufsize - pos,
867 : "update sys.functions set system = true where system <> true and name in ('sq', 'fqn', 'get_merge_table_partition_expressions', 'get_remote_table_expressions', 'schema_guard') and schema_id = 2000 and type = %d;\n", F_FUNC);
868 0 : pos += snprintf(buf + pos, bufsize - pos,
869 : "update sys._tables set system = true where name in ('describe_constraints', 'describe_tables', 'fully_qualified_functions', 'describe_comments', 'describe_privileges', 'describe_partition_tables', 'describe_sequences', 'describe_functions') AND schema_id = 2000;\n");
870 :
871 : /* 76_dump.sql (most everything already dropped) */
872 0 : pos += snprintf(buf + pos, bufsize - pos,
873 : "CREATE VIEW sys.dump_create_roles AS\n"
874 : " SELECT\n"
875 : " 'CREATE ROLE ' || sys.dq(name) || ';' stmt,\n"
876 : " name user_name\n"
877 : " FROM sys.auths\n"
878 : " WHERE name NOT IN (SELECT name FROM sys.db_user_info)\n"
879 : " AND grantor <> 0;\n"
880 : "CREATE VIEW sys.dump_create_users AS\n"
881 : " SELECT\n"
882 : " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
883 : " sys.sq(sys.password_hash(ui.name)) ||\n"
884 : " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
885 : " ui.name user_name\n"
886 : " FROM sys.db_user_info ui, sys.schemas s\n"
887 : " WHERE ui.default_schema = s.id\n"
888 : " AND ui.name <> 'monetdb'\n"
889 : " AND ui.name <> '.snapshot';\n"
890 : "CREATE VIEW sys.dump_create_schemas AS\n"
891 : " SELECT\n"
892 : " 'CREATE SCHEMA ' || sys.dq(s.name) || ifthenelse(a.name <> 'sysadmin', ' AUTHORIZATION ' || sys.dq(a.name), ' ') || ';' stmt,\n"
893 : " s.name schema_name\n"
894 : " FROM sys.schemas s, sys.auths a\n"
895 : " WHERE s.authorization = a.id AND s.system = FALSE;\n"
896 : "CREATE VIEW sys.dump_add_schemas_to_users AS\n"
897 : " SELECT\n"
898 : " 'ALTER USER ' || sys.dq(ui.name) || ' SET SCHEMA ' || sys.dq(s.name) || ';' stmt,\n"
899 : " s.name schema_name,\n"
900 : " ui.name user_name\n"
901 : " FROM sys.db_user_info ui, sys.schemas s\n"
902 : " WHERE ui.default_schema = s.id\n"
903 : " AND ui.name <> 'monetdb'\n"
904 : " AND ui.name <> '.snapshot'\n"
905 : " AND s.name <> 'sys';\n"
906 : "CREATE VIEW sys.dump_grant_user_privileges AS\n"
907 : " SELECT\n"
908 : " 'GRANT ' || sys.dq(a2.name) || ' ' || ifthenelse(a1.name = 'public', 'PUBLIC', sys.dq(a1.name)) || ';' stmt,\n"
909 : " a2.name grantee,\n"
910 : " a1.name grantor\n"
911 : " FROM sys.auths a1, sys.auths a2, sys.user_role ur\n"
912 : " WHERE a1.id = ur.login_id AND a2.id = ur.role_id;\n"
913 : "CREATE VIEW sys.dump_table_constraint_type AS\n"
914 : " SELECT\n"
915 : " 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ADD CONSTRAINT ' || sys.DQ(con) || ' '||\n"
916 : " tpe || ' (' || GROUP_CONCAT(sys.DQ(col), ', ') || ');' stmt,\n"
917 : " sch schema_name,\n"
918 : " tbl table_name,\n"
919 : " con constraint_name\n"
920 : " FROM sys.describe_constraints GROUP BY sch, tbl, con, tpe;\n"
921 : "CREATE VIEW sys.dump_table_grants AS\n"
922 : " WITH table_grants (sname, tname, grantee, grants, grantor, grantable)\n"
923 : " AS (SELECT s.name, t.name, a.name, sum(p.privileges), g.name, p.grantable\n"
924 : " FROM sys.schemas s, sys.tables t, sys.auths a, sys.privileges p, sys.auths g\n"
925 : " WHERE p.obj_id = t.id AND p.auth_id = a.id AND t.schema_id = s.id AND t.system = FALSE AND p.grantor = g.id\n"
926 : " GROUP BY s.name, t.name, a.name, g.name, p.grantable\n"
927 : " ORDER BY s.name, t.name, a.name, g.name, p.grantable)\n"
928 : " SELECT\n"
929 : " 'GRANT ' || pc.privilege_code_name || ' ON TABLE ' || sys.FQN(sname, tname)\n"
930 : " || ' TO ' || ifthenelse(grantee = 'public', 'PUBLIC', sys.dq(grantee))\n"
931 : " || CASE WHEN grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
932 : " sname schema_name,\n"
933 : " tname table_name,\n"
934 : " grantee\n"
935 : " FROM table_grants LEFT OUTER JOIN sys.privilege_codes pc ON grants = pc.privilege_code_id;\n"
936 : "CREATE VIEW sys.dump_column_grants AS\n"
937 : " SELECT\n"
938 : " 'GRANT ' || pc.privilege_code_name || '(' || sys.dq(c.name) || ') ON ' || sys.FQN(s.name, t.name)\n"
939 : " || ' TO ' || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
940 : " || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
941 : " s.name schema_name,\n"
942 : " t.name table_name,\n"
943 : " c.name column_name,\n"
944 : " a.name grantee\n"
945 : " FROM sys.schemas s,\n"
946 : " sys.tables t,\n"
947 : " sys.columns c,\n"
948 : " sys.auths a,\n"
949 : " sys.privileges p,\n"
950 : " sys.auths g,\n"
951 : " sys.privilege_codes pc\n"
952 : " WHERE p.obj_id = c.id\n"
953 : " AND c.table_id = t.id\n"
954 : " AND p.auth_id = a.id\n"
955 : " AND t.schema_id = s.id\n"
956 : " AND NOT t.system\n"
957 : " AND p.grantor = g.id\n"
958 : " AND p.privileges = pc.privilege_code_id\n"
959 : " ORDER BY s.name, t.name, c.name, a.name, g.name, p.grantable;\n"
960 : "CREATE VIEW sys.dump_function_grants AS\n"
961 : " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
962 : " (SELECT a.func_id,\n"
963 : " a.number,\n"
964 : " max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
965 : " group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
966 : " FROM sys.args a\n"
967 : " WHERE a.inout = 1),\n"
968 : " func_args(func_id, func_arg) AS\n"
969 : " (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
970 : " SELECT\n"
971 : " 'GRANT ' || pc.privilege_code_name || ' ON ' || ft.function_type_keyword || ' '\n"
972 : " || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
973 : " || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
974 : " || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
975 : " s.name schema_name,\n"
976 : " f.name function_name,\n"
977 : " a.name grantee\n"
978 : " FROM sys.schemas s,\n"
979 : " sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
980 : " sys.auths a,\n"
981 : " sys.privileges p,\n"
982 : " sys.auths g,\n"
983 : " sys.function_types ft,\n"
984 : " sys.privilege_codes pc\n"
985 : " WHERE s.id = f.schema_id\n"
986 : " AND f.id = p.obj_id\n"
987 : " AND p.auth_id = a.id\n"
988 : " AND p.grantor = g.id\n"
989 : " AND p.privileges = pc.privilege_code_id\n"
990 : " AND f.type = ft.function_type_id\n"
991 : " AND NOT f.system\n"
992 : " ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
993 : "CREATE VIEW sys.dump_indices AS\n"
994 : " SELECT\n"
995 : " 'CREATE ' || tpe || ' ' || sys.DQ(ind) || ' ON ' || sys.FQN(sch, tbl) || '(' || GROUP_CONCAT(col) || ');' stmt,\n"
996 : " sch schema_name,\n"
997 : " tbl table_name,\n"
998 : " ind index_name\n"
999 : " FROM sys.describe_indices GROUP BY ind, tpe, sch, tbl;\n"
1000 : "CREATE VIEW sys.dump_column_defaults AS\n"
1001 : " SELECT 'ALTER TABLE ' || sys.FQN(sch, tbl) || ' ALTER COLUMN ' || sys.DQ(col) || ' SET DEFAULT ' || def || ';' stmt,\n"
1002 : " sch schema_name,\n"
1003 : " tbl table_name,\n"
1004 : " col column_name\n"
1005 : " FROM sys.describe_column_defaults;\n"
1006 : "CREATE VIEW sys.dump_foreign_keys AS\n"
1007 : " SELECT\n"
1008 : " 'ALTER TABLE ' || sys.FQN(fk_s, fk_t) || ' ADD CONSTRAINT ' || sys.DQ(fk) || ' ' ||\n"
1009 : " 'FOREIGN KEY(' || GROUP_CONCAT(sys.DQ(fk_c), ',') ||') ' ||\n"
1010 : " 'REFERENCES ' || sys.FQN(pk_s, pk_t) || '(' || GROUP_CONCAT(sys.DQ(pk_c), ',') || ') ' ||\n"
1011 : " 'ON DELETE ' || on_delete || ' ON UPDATE ' || on_update ||\n"
1012 : " ';' stmt,\n"
1013 : " fk_s foreign_schema_name,\n"
1014 : " fk_t foreign_table_name,\n"
1015 : " pk_s primary_schema_name,\n"
1016 : " pk_t primary_table_name,\n"
1017 : " fk key_name\n"
1018 : " FROM sys.describe_foreign_keys GROUP BY fk_s, fk_t, pk_s, pk_t, fk, on_delete, on_update;\n"
1019 : "CREATE VIEW sys.dump_partition_tables AS\n"
1020 : " SELECT\n"
1021 : " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
1022 : " CASE \n"
1023 : " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
1024 : " WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
1025 : " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
1026 : " ELSE ''\n"
1027 : " END ||\n"
1028 : " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
1029 : " ';' stmt,\n"
1030 : " m_sch merge_schema_name,\n"
1031 : " m_tbl merge_table_name,\n"
1032 : " p_sch partition_schema_name,\n"
1033 : " p_tbl partition_table_name\n"
1034 : " FROM sys.describe_partition_tables;\n"
1035 : "CREATE VIEW sys.dump_sequences AS\n"
1036 : " SELECT\n"
1037 : " 'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT ' ||\n"
1038 : " CASE WHEN \"s\" <> 0 THEN 'START WITH ' || \"rs\" ELSE '' END ||\n"
1039 : " CASE WHEN \"inc\" <> 1 THEN ' INCREMENT BY ' || \"inc\" ELSE '' END ||\n"
1040 : " CASE\n"
1041 : " WHEN nomin THEN ' NO MINVALUE'\n"
1042 : " WHEN rmi IS NOT NULL THEN ' MINVALUE ' || rmi\n"
1043 : " ELSE ''\n"
1044 : " END ||\n"
1045 : " CASE\n"
1046 : " WHEN nomax THEN ' NO MAXVALUE'\n"
1047 : " WHEN rma IS NOT NULL THEN ' MAXVALUE ' || rma\n"
1048 : " ELSE ''\n"
1049 : " END ||\n"
1050 : " CASE WHEN \"cache\" <> 1 THEN ' CACHE ' || \"cache\" ELSE '' END ||\n"
1051 : " CASE WHEN \"cycle\" THEN ' CYCLE' ELSE '' END ||\n"
1052 : " ';' stmt,\n"
1053 : " sch schema_name,\n"
1054 : " seq seqname\n"
1055 : " FROM sys.describe_sequences;\n"
1056 : "CREATE VIEW sys.dump_start_sequences AS\n"
1057 : " SELECT\n"
1058 : " 'UPDATE sys.sequences seq SET start = ' || s ||\n"
1059 : " ' WHERE name = ' || sys.SQ(seq) ||\n"
1060 : " ' AND schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = ' || sys.SQ(sch) || ');' stmt,\n"
1061 : " sch schema_name,\n"
1062 : " seq sequence_name\n"
1063 : " FROM sys.describe_sequences;\n"
1064 : "CREATE VIEW sys.dump_functions AS\n"
1065 : " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
1066 : " f.sch schema_name,\n"
1067 : " f.fun function_name\n"
1068 : " FROM sys.describe_functions f;\n"
1069 : "CREATE VIEW sys.dump_tables AS\n"
1070 : " SELECT\n"
1071 : " t.o o,\n"
1072 : " CASE\n"
1073 : " WHEN t.typ <> 'VIEW' THEN\n"
1074 : " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
1075 : " ELSE\n"
1076 : " t.opt\n"
1077 : " END stmt,\n"
1078 : " t.sch schema_name,\n"
1079 : " t.tab table_name\n"
1080 : " FROM sys.describe_tables t;\n"
1081 : "CREATE VIEW sys.dump_triggers AS\n"
1082 : " SELECT sys.schema_guard(sch, tab, def) stmt,\n"
1083 : " sch schema_name,\n"
1084 : " tab table_name,\n"
1085 : " tri trigger_name\n"
1086 : " FROM sys.describe_triggers;\n"
1087 : "CREATE VIEW sys.dump_comments AS\n"
1088 : " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
1089 : "CREATE VIEW sys.dump_user_defined_types AS\n"
1090 : " SELECT 'CREATE TYPE ' || sys.FQN(sch, sql_tpe) || ' EXTERNAL NAME ' || sys.DQ(ext_tpe) || ';' stmt,\n"
1091 : " sch schema_name,\n"
1092 : " sql_tpe type_name\n"
1093 : " FROM sys.describe_user_defined_types;\n"
1094 : "CREATE FUNCTION sys.esc(s STRING) RETURNS STRING BEGIN RETURN '\"' || sys.replace(sys.replace(sys.replace(s,E'\\\\', E'\\\\\\\\'), E'\\n', E'\\\\n'), '\"', E'\\\\\"') || '\"'; END;\n"
1095 : "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
1096 : "BEGIN\n"
1097 : " RETURN\n"
1098 : " CASE\n"
1099 : " WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
1100 : " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
1101 : " ELSE\n"
1102 : " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
1103 : " END;\n"
1104 : "END;\n"
1105 : "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
1106 : "BEGIN\n"
1107 : " DECLARE k INT;\n"
1108 : " SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t, sys.schemas s WHERE c.table_id = t.id AND t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
1109 : " IF k IS NOT NULL THEN\n"
1110 : " DECLARE cname STRING;\n"
1111 : " DECLARE ctype STRING;\n"
1112 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
1113 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
1114 : " DECLARE COPY_INTO_STMT STRING;\n"
1115 : " DECLARE _cnt INT;\n"
1116 : " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
1117 : " IF _cnt > 0 THEN\n"
1118 : " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
1119 : " DECLARE SELECT_DATA_STMT STRING;\n"
1120 : " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
1121 : " DECLARE M INT;\n"
1122 : " SET M = (SELECT MAX(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl);\n"
1123 : " WHILE (k < M) DO\n"
1124 : " SET k = (SELECT MIN(c.id) FROM sys.columns c, sys.tables t WHERE c.table_id = t.id AND t.name = tbl AND c.id > k);\n"
1125 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
1126 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
1127 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
1128 : " SET SELECT_DATA_STMT = SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype);\n"
1129 : " END WHILE;\n"
1130 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
1131 : " SET SELECT_DATA_STMT = SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl);\n"
1132 : " insert into sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
1133 : " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
1134 : " END IF;\n"
1135 : " END IF;\n"
1136 : "END;\n"
1137 : "CREATE PROCEDURE sys.dump_table_data()\n"
1138 : "BEGIN\n"
1139 : " DECLARE i INT;\n"
1140 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
1141 : " IF i IS NOT NULL THEN\n"
1142 : " DECLARE M INT;\n"
1143 : " SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
1144 : " DECLARE sch STRING;\n"
1145 : " DECLARE tbl STRING;\n"
1146 : " WHILE i < M DO\n"
1147 : " set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
1148 : " set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
1149 : " CALL sys.dump_table_data(sch, tbl);\n"
1150 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
1151 : " END WHILE;\n"
1152 : " set sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
1153 : " set tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
1154 : " CALL sys.dump_table_data(sch, tbl);\n"
1155 : " END IF;\n"
1156 : "END;\n"
1157 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
1158 : "BEGIN\n"
1159 : " SET SCHEMA sys;\n"
1160 : " TRUNCATE sys.dump_statements;\n"
1161 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
1162 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'SET SCHEMA \"sys\";');\n"
1163 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
1164 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
1165 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
1166 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
1167 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
1168 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
1169 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
1170 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
1171 : " FROM (\n"
1172 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
1173 : " UNION\n"
1174 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
1175 : " ) AS stmts(o, s);\n"
1176 : " IF NOT DESCRIBE THEN\n"
1177 : " CALL sys.dump_table_data();\n"
1178 : " END IF;\n"
1179 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
1180 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
1181 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
1182 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
1183 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
1184 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
1185 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
1186 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
1187 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
1188 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
1189 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
1190 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
1191 : " RETURN sys.dump_statements;\n"
1192 : "END;\n");
1193 0 : pos += snprintf(buf + pos, bufsize - pos,
1194 : "update sys.functions set system = true where system <> true and name in ('esc', 'prepare_esc') and schema_id = 2000 and type = %d;\n", F_FUNC);
1195 0 : pos += snprintf(buf + pos, bufsize - pos,
1196 : "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
1197 0 : pos += snprintf(buf + pos, bufsize - pos,
1198 : "update sys.functions set system = true where system <> true and name in ('dump_table_data') and schema_id = 2000 and type = %d;\n", F_PROC);
1199 0 : pos += snprintf(buf + pos, bufsize - pos,
1200 : "update sys._tables set system = true where name in ('dump_create_roles', 'dump_create_users', 'dump_create_schemas', 'dump_add_schemas_to_users', 'dump_grant_user_privileges', 'dump_table_constraint_type', 'dump_table_grants', 'dump_column_grants', 'dump_function_grants', 'dump_indices', 'dump_column_defaults', 'dump_foreign_keys', 'dump_partition_tables', 'dump_sequences', 'dump_start_sequences', 'dump_functions', 'dump_tables', 'dump_triggers', 'dump_comments', 'dump_user_defined_types') AND schema_id = 2000;\n");
1201 :
1202 : /* 80_udf.sql (removed) */
1203 0 : pos += snprintf(buf + pos, bufsize - pos,
1204 : "drop function sys.reverse(string) cascade;\n"
1205 : "drop all function sys.fuse cascade;\n");
1206 :
1207 : /* 26_sysmon.sql */
1208 0 : pos += snprintf(buf + pos, bufsize - pos,
1209 : "create procedure sys.vacuum(sname string, tname string, cname string)\n"
1210 : " external name sql.vacuum;\n"
1211 : "create procedure sys.vacuum(sname string, tname string, cname string, interval int)\n"
1212 : " external name sql.vacuum;\n"
1213 : "create procedure sys.stop_vacuum(sname string, tname string, cname string)\n"
1214 : " external name sql.stop_vacuum;\n");
1215 0 : pos += snprintf(buf + pos, bufsize - pos,
1216 : "update sys.functions set system = true where system <> true and name in ('vacuum', 'stop_vacuum') and schema_id = 2000 and type = %d;\n", F_PROC);
1217 :
1218 : /* 10_sys_schema_extension.sql */
1219 0 : pos += snprintf(buf + pos, bufsize - pos,
1220 : "CREATE TABLE sys.fkey_actions (\n"
1221 : " action_id SMALLINT NOT NULL PRIMARY KEY,\n"
1222 : " action_name VARCHAR(15) NOT NULL);\n"
1223 : "INSERT INTO sys.fkey_actions (action_id, action_name) VALUES\n"
1224 : " (0, 'NO ACTION'),\n"
1225 : " (1, 'CASCADE'),\n"
1226 : " (2, 'RESTRICT'),\n"
1227 : " (3, 'SET NULL'),\n"
1228 : " (4, 'SET DEFAULT');\n"
1229 : "ALTER TABLE sys.fkey_actions SET READ ONLY;\n"
1230 : "GRANT SELECT ON sys.fkey_actions TO PUBLIC;\n"
1231 : "CREATE VIEW sys.fkeys AS\n"
1232 : "SELECT id, table_id, type, name, rkey, update_action_id, upd.action_name as update_action, delete_action_id, del.action_name as delete_action FROM (\n"
1233 : " SELECT id, table_id, type, name, rkey, cast(((\"action\" >> 8) & 255) as smallint) as update_action_id, cast((\"action\" & 255) as smallint) AS delete_action_id FROM sys.keys WHERE type = 2\n"
1234 : " UNION ALL\n"
1235 : " SELECT id, table_id, type, name, rkey, cast(((\"action\" >> 8) & 255) as smallint) as update_action_id, cast((\"action\" & 255) as smallint) AS delete_action_id FROM tmp.keys WHERE type = 2\n"
1236 : ") AS fks\n"
1237 : "JOIN sys.fkey_actions upd ON fks.update_action_id = upd.action_id\n"
1238 : "JOIN sys.fkey_actions del ON fks.delete_action_id = del.action_id;\n"
1239 : "GRANT SELECT ON sys.fkeys TO PUBLIC;\n"
1240 : );
1241 0 : pos += snprintf(buf + pos, bufsize - pos,
1242 : "update sys._tables set system = true where name in ('fkey_actions', 'fkeys') AND schema_id = 2000;\n");
1243 :
1244 : /* recreate SQL functions that just need to be recompiled since the
1245 : * MAL functions's "unsafe" property was changed */
1246 0 : sql_schema *lg = mvc_bind_schema(sql, "logging");
1247 0 : t = mvc_bind_table(sql, lg, "compinfo");
1248 0 : t->system = 0;
1249 0 : t = mvc_bind_table(sql, s, "schemastorage");
1250 0 : t->system = 0;
1251 0 : t = mvc_bind_table(sql, s, "tablestorage");
1252 0 : t->system = 0;
1253 0 : t = mvc_bind_table(sql, s, "storage");
1254 0 : t->system = 0;
1255 0 : t = mvc_bind_table(sql, s, "rejects");
1256 0 : t->system = 0;
1257 0 : t = mvc_bind_table(sql, s, "queue");
1258 0 : t->system = 0;
1259 0 : t = mvc_bind_table(sql, s, "optimizers");
1260 0 : t->system = 0;
1261 0 : t = mvc_bind_table(sql, s, "prepared_statements_args");
1262 0 : t->system = 0;
1263 0 : t = mvc_bind_table(sql, s, "prepared_statements");
1264 0 : t->system = 0;
1265 0 : t = mvc_bind_table(sql, s, "sessions");
1266 0 : t->system = 0;
1267 0 : t = mvc_bind_table(sql, s, "querylog_calls");
1268 0 : t->system = 0;
1269 0 : t = mvc_bind_table(sql, s, "querylog_history");
1270 0 : t->system = 0;
1271 0 : t = mvc_bind_table(sql, s, "querylog_catalog");
1272 0 : t->system = 0;
1273 0 : pos += snprintf(buf + pos, bufsize - pos,
1274 : "drop view logging.compinfo cascade;\n"
1275 : "drop function logging.compinfo cascade;\n"
1276 : "drop procedure sys.storagemodelinit() cascade;\n"
1277 : "drop view sys.schemastorage cascade;\n"
1278 : "drop view sys.tablestorage cascade;\n"
1279 : "drop view sys.storage cascade;\n"
1280 : "drop function sys.storage() cascade;\n"
1281 : "drop function if exists wlr.tick cascade;\n"
1282 : "drop function if exists wlr.clock cascade;\n"
1283 : "drop function if exists wlc.tick cascade;\n"
1284 : "drop function if exists wlc.clock cascade;\n"
1285 : "drop function profiler.getlimit cascade;\n"
1286 : "drop view sys.rejects cascade;\n"
1287 : "drop function sys.rejects cascade;\n"
1288 : "drop function sys.user_statistics cascade;\n"
1289 : "drop view sys.queue cascade;\n"
1290 : "drop function sys.queue cascade;\n"
1291 : "drop function sys.debugflags cascade;\n"
1292 : "drop function sys.bbp cascade;\n"
1293 : "drop view sys.optimizers cascade;\n"
1294 : "drop function sys.optimizers cascade;\n"
1295 : "drop function sys.querycache cascade;\n"
1296 : "drop function sys.optimizer_stats cascade;\n"
1297 : "drop function sys.current_sessionid cascade;\n"
1298 : "drop view sys.prepared_statements_args cascade;\n"
1299 : "drop function sys.prepared_statements_args cascade;\n"
1300 : "drop view sys.prepared_statements cascade;\n"
1301 : "drop function sys.prepared_statements cascade;\n"
1302 : "drop view sys.sessions cascade;\n"
1303 : "drop function sys.sessions cascade;\n"
1304 : "drop view sys.querylog_history cascade;\n"
1305 : "drop view sys.querylog_calls cascade;\n"
1306 : "drop function sys.querylog_calls cascade;\n"
1307 : "drop view sys.querylog_catalog cascade;\n"
1308 : "drop function sys.querylog_catalog cascade;\n"
1309 : "create function sys.querylog_catalog()\n"
1310 : "returns table(\n"
1311 : " id oid,\n"
1312 : " owner string,\n"
1313 : " defined timestamp,\n"
1314 : " query string,\n"
1315 : " pipe string,\n"
1316 : " \"plan\" string,\n"
1317 : " mal int,\n"
1318 : " optimize bigint\n"
1319 : ")\n"
1320 : "external name sql.querylog_catalog;\n"
1321 : "create view sys.querylog_catalog as select * from sys.querylog_catalog();\n"
1322 : "create function sys.querylog_calls()\n"
1323 : "returns table(\n"
1324 : " id oid,\n"
1325 : " \"start\" timestamp,\n"
1326 : " \"stop\" timestamp,\n"
1327 : " arguments string,\n"
1328 : " tuples bigint,\n"
1329 : " run bigint,\n"
1330 : " ship bigint,\n"
1331 : " cpu int,\n"
1332 : " io int\n"
1333 : ")\n"
1334 : "external name sql.querylog_calls;\n"
1335 : "create view sys.querylog_calls as select * from sys.querylog_calls();\n"
1336 : "create view sys.querylog_history as\n"
1337 : "select qd.*, ql.\"start\",ql.\"stop\", ql.arguments, ql.tuples, ql.run, ql.ship, ql.cpu, ql.io\n"
1338 : "from sys.querylog_catalog() qd, sys.querylog_calls() ql\n"
1339 : "where qd.id = ql.id and qd.owner = user;\n"
1340 : "create function sys.sessions()\n"
1341 : " returns table(\n"
1342 : " \"sessionid\" int,\n"
1343 : " \"username\" string,\n"
1344 : " \"login\" timestamp,\n"
1345 : " \"idle\" timestamp,\n"
1346 : " \"optimizer\" string,\n"
1347 : " \"sessiontimeout\" int,\n"
1348 : " \"querytimeout\" int,\n"
1349 : " \"workerlimit\" int,\n"
1350 : " \"memorylimit\" int,\n"
1351 : " \"language\" string,\n"
1352 : " \"peer\" string,\n"
1353 : " \"hostname\" string,\n"
1354 : " \"application\" string,\n"
1355 : " \"client\" string,\n"
1356 : " \"clientpid\" bigint,\n"
1357 : " \"remark\" string\n"
1358 : " )\n"
1359 : " external name sql.sessions;\n"
1360 : "create view sys.sessions as select * from sys.sessions();\n"
1361 : "create function sys.prepared_statements()\n"
1362 : "returns table(\n"
1363 : " \"sessionid\" int,\n"
1364 : " \"username\" string,\n"
1365 : " \"statementid\" int,\n"
1366 : " \"statement\" string,\n"
1367 : " \"created\" timestamp\n"
1368 : ")\n"
1369 : "external name sql.prepared_statements;\n"
1370 : "grant execute on function sys.prepared_statements to public;\n"
1371 : "create view sys.prepared_statements as select * from sys.prepared_statements();\n"
1372 : "grant select on sys.prepared_statements to public;\n"
1373 : "create function sys.prepared_statements_args()\n"
1374 : "returns table(\n"
1375 : " \"statementid\" int,\n"
1376 : " \"type\" string,\n"
1377 : " \"type_digits\" int,\n"
1378 : " \"type_scale\" int,\n"
1379 : " \"inout\" tinyint,\n"
1380 : " \"number\" int,\n"
1381 : " \"schema\" string,\n"
1382 : " \"table\" string,\n"
1383 : " \"column\" string\n"
1384 : ")\n"
1385 : "external name sql.prepared_statements_args;\n"
1386 : "grant execute on function sys.prepared_statements_args to public;\n"
1387 : "create view sys.prepared_statements_args as select * from sys.prepared_statements_args();\n"
1388 : "grant select on sys.prepared_statements_args to public;\n"
1389 : "create function sys.current_sessionid() returns int\n"
1390 : "external name clients.current_sessionid;\n"
1391 : "grant execute on function sys.current_sessionid to public;\n"
1392 : "create function sys.optimizer_stats()\n"
1393 : " returns table (optname string, count int, timing bigint)\n"
1394 : " external name inspect.optimizer_stats;\n"
1395 : "create function sys.querycache()\n"
1396 : " returns table (query string, count int)\n"
1397 : " external name sql.dump_cache;\n"
1398 : "create function sys.optimizers ()\n"
1399 : " returns table (name string, def string, status string)\n"
1400 : " external name sql.optimizers;\n"
1401 : "create view sys.optimizers as select * from sys.optimizers();\n"
1402 : "create function sys.bbp ()\n"
1403 : " returns table (id int, name string,\n"
1404 : " ttype string, count bigint, refcnt int, lrefcnt int,\n"
1405 : " location string, heat int, dirty string,\n"
1406 : " status string, kind string)\n"
1407 : " external name bbp.get;\n"
1408 : "create function sys.debugflags()\n"
1409 : " returns table(flag string, val bool)\n"
1410 : " external name mdb.\"getDebugFlags\";\n"
1411 : "create function sys.queue()\n"
1412 : "returns table(\n"
1413 : " \"tag\" bigint,\n"
1414 : " \"sessionid\" int,\n"
1415 : " \"username\" string,\n"
1416 : " \"started\" timestamp,\n"
1417 : " \"status\" string,\n"
1418 : " \"query\" string,\n"
1419 : " \"finished\" timestamp,\n"
1420 : " \"maxworkers\" int,\n"
1421 : " \"footprint\" int\n"
1422 : ")\n"
1423 : "external name sysmon.queue;\n"
1424 : "grant execute on function sys.queue to public;\n"
1425 : "create view sys.queue as select * from sys.queue();\n"
1426 : "grant select on sys.queue to public;\n"
1427 : "create function sys.user_statistics()\n"
1428 : "returns table(\n"
1429 : " username string,\n"
1430 : " querycount bigint,\n"
1431 : " totalticks bigint,\n"
1432 : " started timestamp,\n"
1433 : " finished timestamp,\n"
1434 : " maxticks bigint,\n"
1435 : " maxquery string\n"
1436 : ")\n"
1437 : "external name sysmon.user_statistics;\n"
1438 : "create function sys.rejects()\n"
1439 : "returns table(\n"
1440 : " rowid bigint,\n"
1441 : " fldid int,\n"
1442 : " \"message\" string,\n"
1443 : " \"input\" string\n"
1444 : ")\n"
1445 : "external name sql.copy_rejects;\n"
1446 : "grant execute on function rejects to public;\n"
1447 : "create view sys.rejects as select * from sys.rejects();\n"
1448 : "create function profiler.getlimit() returns integer external name profiler.getlimit;\n"
1449 : "create function sys.\"storage\"()\n"
1450 : "returns table (\n"
1451 : " \"schema\" varchar(1024),\n"
1452 : " \"table\" varchar(1024),\n"
1453 : " \"column\" varchar(1024),\n"
1454 : " \"type\" varchar(1024),\n"
1455 : " \"mode\" varchar(15),\n"
1456 : " location varchar(1024),\n"
1457 : " \"count\" bigint,\n"
1458 : " typewidth int,\n"
1459 : " columnsize bigint,\n"
1460 : " heapsize bigint,\n"
1461 : " hashes bigint,\n"
1462 : " phash boolean,\n"
1463 : " \"imprints\" bigint,\n"
1464 : " sorted boolean,\n"
1465 : " revsorted boolean,\n"
1466 : " \"unique\" boolean,\n"
1467 : " orderidx bigint\n"
1468 : ")\n"
1469 : "external name sql.\"storage\";\n"
1470 : "create view sys.\"storage\" as\n"
1471 : "select * from sys.\"storage\"()\n"
1472 : " where (\"schema\", \"table\") in (\n"
1473 : " select sch.\"name\", tbl.\"name\"\n"
1474 : " from sys.\"tables\" as tbl join sys.\"schemas\" as sch on tbl.schema_id = sch.id\n"
1475 : " where tbl.\"system\" = false)\n"
1476 : "order by \"schema\", \"table\", \"column\";\n"
1477 : "create view sys.\"tablestorage\" as\n"
1478 : "select \"schema\", \"table\",\n"
1479 : " max(\"count\") as \"rowcount\",\n"
1480 : " count(*) as \"storages\",\n"
1481 : " sum(columnsize) as columnsize,\n"
1482 : " sum(heapsize) as heapsize,\n"
1483 : " sum(hashes) as hashsize,\n"
1484 : " sum(\"imprints\") as imprintsize,\n"
1485 : " sum(orderidx) as orderidxsize\n"
1486 : " from sys.\"storage\"\n"
1487 : "group by \"schema\", \"table\"\n"
1488 : "order by \"schema\", \"table\";\n"
1489 : "create view sys.\"schemastorage\" as\n"
1490 : "select \"schema\",\n"
1491 : " count(*) as \"storages\",\n"
1492 : " sum(columnsize) as columnsize,\n"
1493 : " sum(heapsize) as heapsize,\n"
1494 : " sum(hashes) as hashsize,\n"
1495 : " sum(\"imprints\") as imprintsize,\n"
1496 : " sum(orderidx) as orderidxsize\n"
1497 : " from sys.\"storage\"\n"
1498 : "group by \"schema\"\n"
1499 : "order by \"schema\";\n"
1500 : "create procedure sys.storagemodelinit()\n"
1501 : "begin\n"
1502 : " delete from sys.storagemodelinput;\n"
1503 : " insert into sys.storagemodelinput\n"
1504 : " select \"schema\", \"table\", \"column\", \"type\", typewidth, \"count\",\n"
1505 : " case when (\"unique\" or \"type\" in ('varchar', 'char', 'clob', 'json', 'url', 'blob', 'geometry', 'geometrya'))\n"
1506 : " then \"count\" else 0 end,\n"
1507 : " case when \"count\" > 0 and heapsize >= 8192 and \"type\" in ('varchar', 'char', 'clob', 'json', 'url')\n"
1508 : " then cast((heapsize - 8192) / \"count\" as bigint)\n"
1509 : " when \"count\" > 0 and heapsize >= 32 and \"type\" in ('blob', 'geometry', 'geometrya')\n"
1510 : " then cast((heapsize - 32) / \"count\" as bigint)\n"
1511 : " else typewidth end,\n"
1512 : " false, case sorted when true then true else false end, \"unique\", true\n"
1513 : " from sys.\"storage\";\n"
1514 : " update sys.storagemodelinput\n"
1515 : " set reference = true\n"
1516 : " where (\"schema\", \"table\", \"column\") in (\n"
1517 : " select fkschema.\"name\", fktable.\"name\", fkkeycol.\"name\"\n"
1518 : " from sys.\"keys\" as fkkey,\n"
1519 : " sys.\"objects\" as fkkeycol,\n"
1520 : " sys.\"tables\" as fktable,\n"
1521 : " sys.\"schemas\" as fkschema\n"
1522 : " where fktable.\"id\" = fkkey.\"table_id\"\n"
1523 : " and fkkey.\"id\" = fkkeycol.\"id\"\n"
1524 : " and fkschema.\"id\" = fktable.\"schema_id\"\n"
1525 : " and fkkey.\"rkey\" > -1 );\n"
1526 : " update sys.storagemodelinput\n"
1527 : " set isacolumn = false\n"
1528 : " where (\"schema\", \"table\", \"column\") not in (\n"
1529 : " select sch.\"name\", tbl.\"name\", col.\"name\"\n"
1530 : " from sys.\"schemas\" as sch,\n"
1531 : " sys.\"tables\" as tbl,\n"
1532 : " sys.\"columns\" as col\n"
1533 : " where sch.\"id\" = tbl.\"schema_id\"\n"
1534 : " and tbl.\"id\" = col.\"table_id\");\n"
1535 : "end;\n"
1536 : "create function logging.compinfo()\n"
1537 : "returns table(\n"
1538 : " \"id\" int,\n"
1539 : " \"component\" string,\n"
1540 : " \"log_level\" string\n"
1541 : ")\n"
1542 : "external name logging.compinfo;\n"
1543 : "grant execute on function logging.compinfo to public;\n"
1544 : "create view logging.compinfo as select * from logging.compinfo();\n"
1545 : "grant select on logging.compinfo to public;\n"
1546 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name in ('schemastorage', 'tablestorage', 'storage', 'rejects', 'queue', 'optimizers', 'prepared_statements_args', 'prepared_statements', 'sessions', 'querylog_history', 'querylog_calls', 'querylog_catalog');\n"
1547 : "update sys._tables set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
1548 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('storagemodelinit', 'storage', 'rejects', 'user_statistics', 'queue', 'debugflags', 'bbp', 'optimizers', 'querycache', 'optimizer_stats', 'current_sessionid', 'prepared_statements_args', 'prepared_statements', 'sessions', 'querylog_calls', 'querylog_catalog');\n"
1549 : "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'logging') and name = 'compinfo';\n"
1550 : "update sys.functions set system = true where system <> true and schema_id = (select id from sys.schemas where name = 'profiler') and name = 'getlimit';\n"
1551 : );
1552 :
1553 : /* 99_system.sql */
1554 0 : t = mvc_bind_table(sql, s, "systemfunctions");
1555 0 : t->system = 0;
1556 0 : pos += snprintf(buf + pos, bufsize - pos,
1557 : "drop view sys.systemfunctions cascade;\n");
1558 :
1559 : /* 80_statistics.sql */
1560 0 : t = mvc_bind_table(sql, s, "statistics");
1561 0 : t->system = 0;
1562 0 : pos += snprintf(buf + pos, bufsize - pos,
1563 : "drop table sys.statistics cascade;\n"
1564 : "drop procedure sys.analyze(int,bigint) cascade;\n"
1565 : "drop procedure sys.analyze(int,bigint,string) cascade;\n"
1566 : "drop procedure sys.analyze(int,bigint,string,string) cascade;\n"
1567 : "drop procedure sys.analyze(int,bigint,string,string,string) cascade;\n"
1568 : "create procedure sys.\"analyze\"()\n"
1569 : "external name sql.\"analyze\";\n"
1570 : "grant execute on procedure sys.\"analyze\"() to public;\n"
1571 : "create procedure sys.\"analyze\"(\"sname\" varchar(1024))\n"
1572 : "external name sql.\"analyze\";\n"
1573 : "grant execute on procedure sys.\"analyze\"(varchar(1024)) to public;\n"
1574 : "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
1575 : "external name sql.\"analyze\";\n"
1576 : "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024)) to public;\n"
1577 : "create procedure sys.\"analyze\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
1578 : "external name sql.\"analyze\";\n"
1579 : "grant execute on procedure sys.\"analyze\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
1580 : "create function sys.\"statistics\"()\n"
1581 : "returns table (\n"
1582 : " \"column_id\" integer,\n"
1583 : " \"schema\" varchar(1024),\n"
1584 : " \"table\" varchar(1024),\n"
1585 : " \"column\" varchar(1024),\n"
1586 : " \"type\" varchar(1024),\n"
1587 : " \"width\" integer,\n"
1588 : " \"count\" bigint,\n"
1589 : " \"unique\" boolean,\n"
1590 : " \"nils\" boolean,\n"
1591 : " \"minval\" string,\n"
1592 : " \"maxval\" string,\n"
1593 : " \"sorted\" boolean,\n"
1594 : " \"revsorted\" boolean\n"
1595 : ")\n"
1596 : "external name sql.\"statistics\";\n"
1597 : "grant execute on function sys.\"statistics\"() to public;\n"
1598 : "create view sys.\"statistics\" as\n"
1599 : "select * from sys.\"statistics\"()\n"
1600 : "-- exclude system tables\n"
1601 : "where (\"schema\", \"table\") in (\n"
1602 : " SELECT sch.\"name\", tbl.\"name\"\n"
1603 : " FROM sys.\"tables\" AS tbl JOIN sys.\"schemas\" AS sch ON tbl.schema_id = sch.id\n"
1604 : " WHERE tbl.\"system\" = FALSE)\n"
1605 : "order by \"schema\", \"table\", \"column\";\n"
1606 : "grant select on sys.\"statistics\" to public;\n"
1607 : "create function sys.\"statistics\"(\"sname\" varchar(1024))\n"
1608 : "returns table (\n"
1609 : " \"column_id\" integer,\n"
1610 : " \"schema\" varchar(1024),\n"
1611 : " \"table\" varchar(1024),\n"
1612 : " \"column\" varchar(1024),\n"
1613 : " \"type\" varchar(1024),\n"
1614 : " \"width\" integer,\n"
1615 : " \"count\" bigint,\n"
1616 : " \"unique\" boolean,\n"
1617 : " \"nils\" boolean,\n"
1618 : " \"minval\" string,\n"
1619 : " \"maxval\" string,\n"
1620 : " \"sorted\" boolean,\n"
1621 : " \"revsorted\" boolean\n"
1622 : ")\n"
1623 : "external name sql.\"statistics\";\n"
1624 : "grant execute on function sys.\"statistics\"(varchar(1024)) to public;\n"
1625 : "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024))\n"
1626 : "returns table (\n"
1627 : " \"column_id\" integer,\n"
1628 : " \"schema\" varchar(1024),\n"
1629 : " \"table\" varchar(1024),\n"
1630 : " \"column\" varchar(1024),\n"
1631 : " \"type\" varchar(1024),\n"
1632 : " \"width\" integer,\n"
1633 : " \"count\" bigint,\n"
1634 : " \"unique\" boolean,\n"
1635 : " \"nils\" boolean,\n"
1636 : " \"minval\" string,\n"
1637 : " \"maxval\" string,\n"
1638 : " \"sorted\" boolean,\n"
1639 : " \"revsorted\" boolean\n"
1640 : ")\n"
1641 : "external name sql.\"statistics\";\n"
1642 : "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024)) to public;\n"
1643 : "create function sys.\"statistics\"(\"sname\" varchar(1024), \"tname\" varchar(1024), \"cname\" varchar(1024))\n"
1644 : "returns table (\n"
1645 : " \"column_id\" integer,\n"
1646 : " \"schema\" varchar(1024),\n"
1647 : " \"table\" varchar(1024),\n"
1648 : " \"column\" varchar(1024),\n"
1649 : " \"type\" varchar(1024),\n"
1650 : " \"width\" integer,\n"
1651 : " \"count\" bigint,\n"
1652 : " \"unique\" boolean,\n"
1653 : " \"nils\" boolean,\n"
1654 : " \"minval\" string,\n"
1655 : " \"maxval\" string,\n"
1656 : " \"sorted\" boolean,\n"
1657 : " \"revsorted\" boolean\n"
1658 : ")\n"
1659 : "external name sql.\"statistics\";\n"
1660 : "grant execute on function sys.\"statistics\"(varchar(1024),varchar(1024),varchar(1024)) to public;\n"
1661 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'statistics';\n"
1662 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('analyze','statistics');\n");
1663 :
1664 0 : assert(pos < bufsize);
1665 0 : printf("Running database upgrade commands:\n%s\n", buf);
1666 0 : fflush(stdout);
1667 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
1668 :
1669 0 : GDKfree(buf);
1670 0 : return err; /* usually MAL_SUCCEED */
1671 : }
1672 :
1673 : static str
1674 115 : sql_update_sep2022(Client c, mvc *sql, sql_schema *s)
1675 : {
1676 115 : size_t bufsize = 65536, pos = 0;
1677 115 : char *err = NULL, *buf = GDKmalloc(bufsize);
1678 115 : res_table *output;
1679 115 : BAT *b;
1680 :
1681 115 : if (buf == NULL)
1682 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1683 :
1684 : /* if sys.db_user_info does not have a column password, we need to
1685 : * add a bunch of columns */
1686 115 : sql_table *db_user_info = find_sql_table(sql->session->tr, s, "db_user_info");
1687 115 : if (find_sql_column(db_user_info, "password") == NULL) {
1688 0 : pos += snprintf(buf + pos, bufsize - pos,
1689 : "alter table sys.db_user_info add column max_memory bigint;\n"
1690 : "alter table sys.db_user_info add column max_workers int;\n"
1691 : "alter table sys.db_user_info add column optimizer varchar(1024);\n"
1692 : "alter table sys.db_user_info add column default_role int;\n"
1693 : "alter table sys.db_user_info add column password varchar(256);\n");
1694 0 : pos += snprintf(buf + pos, bufsize - pos,
1695 : "update sys.db_user_info u set max_memory = 0, max_workers = 0, optimizer = 'default_pipe', default_role = (select id from sys.auths a where a.name = u.name);\n");
1696 0 : int endprint = (int) pos;
1697 0 : bat bid;
1698 0 : BAT *u = NULL, *p = NULL, *d = NULL;
1699 0 : if ((bid = BBPindex("M5system_auth_user")) == 0 ||
1700 0 : (u = BATdescriptor(bid)) == NULL ||
1701 0 : (bid = BBPindex("M5system_auth_passwd_v2")) == 0 ||
1702 0 : (p = BATdescriptor(bid)) == NULL ||
1703 0 : (bid = BBPindex("M5system_auth_deleted")) == 0 ||
1704 0 : (d = BATdescriptor(bid)) == NULL) {
1705 0 : BBPreclaim(u);
1706 0 : BBPreclaim(p);
1707 0 : BBPreclaim(d);
1708 0 : throw(SQL, __func__, INTERNAL_BAT_ACCESS);
1709 : }
1710 0 : BATiter ui = bat_iterator(u);
1711 0 : BATiter pi = bat_iterator(p);
1712 0 : for (oid i = 0; i < ui.count; i++) {
1713 0 : if (BUNfnd(d, &i) == BUN_NONE) {
1714 0 : const char *user = BUNtvar(ui, i);
1715 0 : const char *pass = BUNtvar(pi, i);
1716 0 : if (pos + 4 * (strlen(user) + strlen(pass)) + 64 >= bufsize) {
1717 0 : char *nbuf = GDKrealloc(buf, bufsize + 65536);
1718 0 : if (nbuf == NULL) {
1719 0 : err = createException(SQL, __func__, MAL_MALLOC_FAIL);
1720 0 : break;
1721 : }
1722 : buf = nbuf;
1723 : bufsize += 65536;
1724 : }
1725 0 : pos += snprintf(buf + pos, bufsize - pos,
1726 : "update sys.db_user_info set password = e'");
1727 0 : for (const char *p = pass; *p; p++) {
1728 0 : if (*p < '\040' || *p >= '\177') {
1729 : /* control character or high bit set */
1730 0 : pos += snprintf(buf + pos, bufsize - pos,
1731 : "\\%03o", (unsigned char) *p);
1732 : } else {
1733 0 : if (*p == '\\' || *p == '\'')
1734 0 : buf[pos++] = *p;
1735 0 : buf[pos++] = *p;
1736 : }
1737 : }
1738 0 : pos += snprintf(buf + pos, bufsize - pos,
1739 : "' where name = e'");
1740 0 : for (const char *p = user; *p; p++) {
1741 0 : if (*p < '\040' || *p >= '\177') {
1742 : /* control character or high bit set */
1743 0 : pos += snprintf(buf + pos, bufsize - pos,
1744 : "\\%03o", (unsigned char) *p);
1745 : } else {
1746 0 : if (*p == '\\' || *p == '\'')
1747 0 : buf[pos++] = *p;
1748 0 : buf[pos++] = *p;
1749 : }
1750 : }
1751 0 : pos += snprintf(buf + pos, bufsize - pos,
1752 : "';\n");
1753 : }
1754 : }
1755 0 : if (err == MAL_SUCCEED) {
1756 0 : assert(pos < bufsize);
1757 0 : printf("Running database upgrade commands:\n%.*s-- and copying passwords\n\n", endprint, buf);
1758 0 : fflush(stdout);
1759 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
1760 : }
1761 0 : bat_iterator_end(&ui);
1762 0 : bat_iterator_end(&pi);
1763 0 : bat authbats[4];
1764 0 : authbats[0] = 0;
1765 0 : authbats[1] = u->batCacheid;
1766 0 : authbats[2] = p->batCacheid;
1767 0 : authbats[3] = d->batCacheid;
1768 0 : if (err == MAL_SUCCEED &&
1769 0 : (BATmode(u, true) != GDK_SUCCEED ||
1770 0 : BATmode(p, true) != GDK_SUCCEED ||
1771 0 : BATmode(d, true) != GDK_SUCCEED ||
1772 0 : BBPrename(u, NULL) != 0 ||
1773 0 : BBPrename(p, NULL) != 0 ||
1774 0 : BBPrename(d, NULL) != 0 ||
1775 0 : TMsubcommit_list(authbats, NULL, 4, -1) != GDK_SUCCEED)) {
1776 0 : fprintf(stderr, "Committing removal of old user/password BATs failed\n");
1777 : }
1778 0 : BBPunfix(u->batCacheid);
1779 0 : BBPunfix(p->batCacheid);
1780 0 : BBPunfix(d->batCacheid);
1781 :
1782 0 : if (err == MAL_SUCCEED) {
1783 0 : sql_schema *s = mvc_bind_schema(sql, "sys");
1784 0 : sql_table *t = mvc_bind_table(sql, s, "roles");
1785 0 : t->system = 0;
1786 0 : t = mvc_bind_table(sql, s, "users");
1787 0 : t->system = 0;
1788 0 : t = mvc_bind_table(sql, s, "dependency_schemas_on_users");
1789 0 : t->system = 0;
1790 0 : pos = 0;
1791 0 : pos += snprintf(buf + pos, bufsize - pos,
1792 : "drop view sys.dependency_schemas_on_users cascade;\n"
1793 : "drop view sys.roles cascade;\n"
1794 : "drop view sys.users cascade;\n"
1795 : "drop function sys.db_users() cascade;\n"
1796 : "CREATE VIEW sys.roles AS SELECT id, name, grantor FROM sys.auths a WHERE a.name NOT IN (SELECT u.name FROM sys.db_user_info u);\n"
1797 : "GRANT SELECT ON sys.roles TO PUBLIC;\n"
1798 : "CREATE VIEW sys.users AS SELECT name, fullname, default_schema, schema_path, max_memory, max_workers, optimizer, default_role FROM sys.db_user_info;\n"
1799 : "GRANT SELECT ON sys.users TO PUBLIC;\n"
1800 : "CREATE FUNCTION sys.db_users() RETURNS TABLE(name varchar(2048)) RETURN SELECT name FROM sys.db_user_info;\n"
1801 : "CREATE VIEW sys.dependency_schemas_on_users AS\n"
1802 : "SELECT s.id AS schema_id, s.name AS schema_name, u.name AS user_name, CAST(6 AS smallint) AS depend_type\n"
1803 : " FROM sys.db_user_info AS u, sys.schemas AS s\n"
1804 : " WHERE u.default_schema = s.id\n"
1805 : " ORDER BY s.name, u.name;\n"
1806 : "GRANT SELECT ON sys.dependency_schemas_on_users TO PUBLIC;\n"
1807 : "update sys._tables set system = true where name in ('users', 'roles', 'dependency_schemas_on_users') AND schema_id = 2000;\n"
1808 : "update sys.functions set system = true where system <> true and name in ('db_users') and schema_id = 2000 and type = %d;\n", F_UNION);
1809 0 : assert(pos < bufsize);
1810 0 : printf("Running database upgrade commands:\n%s\n", buf);
1811 0 : fflush(stdout);
1812 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
1813 : }
1814 : }
1815 115 : if (err != MAL_SUCCEED) {
1816 0 : GDKfree(buf);
1817 0 : return err;
1818 : }
1819 :
1820 : /* if 'describe_partition_tables' system view doesn't use 'vals'
1821 : * CTE, re-create it; while we're at it, also update the sequence
1822 : * dumping code */
1823 115 : pos = snprintf(buf, bufsize,
1824 : "select 1 from sys.tables where schema_id = (select \"id\" from sys.schemas where \"name\" = 'sys') and \"name\" = 'describe_partition_tables' and \"query\" not like '%%vals%%';\n");
1825 115 : if ((err = SQLstatementIntern(c, buf, "update", true, false, &output))) {
1826 0 : GDKfree(buf);
1827 0 : return err;
1828 : }
1829 :
1830 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
1831 : /* 52_describe.sql; but we need to drop dependencies from 76_dump.sql first */
1832 0 : sql_schema *s = mvc_bind_schema(sql, "sys");
1833 0 : sql_table *t = mvc_bind_table(sql, s, "describe_partition_tables");
1834 0 : t->system = 0;
1835 0 : t = mvc_bind_table(sql, s, "dump_partition_tables");
1836 0 : t->system = 0;
1837 0 : t = mvc_bind_table(sql, s, "dump_sequences");
1838 0 : t->system = 0;
1839 0 : t = mvc_bind_table(sql, s, "dump_start_sequences");
1840 0 : t->system = 0;
1841 0 : t = mvc_bind_table(sql, s, "describe_tables");
1842 0 : t->system = 0;
1843 0 : t = mvc_bind_table(sql, s, "dump_tables");
1844 0 : t->system = 0;
1845 0 : t = mvc_bind_table(sql, s, "dump_create_users");
1846 0 : t->system = 0;
1847 0 : t = mvc_bind_table(sql, s, "dump_functions");
1848 0 : t->system = 0;
1849 0 : t = mvc_bind_table(sql, s, "dump_triggers");
1850 0 : t->system = 0;
1851 :
1852 0 : pos = 0;
1853 0 : pos += snprintf(buf + pos, bufsize - pos,
1854 : /* drop dependent stuff from 76_dump.sql */
1855 : "drop function sys.dump_database(boolean) cascade;\n"
1856 : "drop procedure sys.dump_table_data() cascade;\n"
1857 : "drop procedure sys.dump_table_data(string, string) cascade;\n"
1858 : "drop view sys.dump_partition_tables cascade;\n"
1859 : "drop view sys.describe_partition_tables cascade;\n"
1860 : "drop view sys.dump_sequences cascade;\n"
1861 : "drop view sys.dump_start_sequences cascade;\n"
1862 : "drop view sys.dump_tables cascade;\n"
1863 : "drop view sys.describe_tables cascade;\n"
1864 : "drop view sys.dump_create_users cascade;\n"
1865 : "drop view sys.dump_functions cascade;\n"
1866 : "drop view sys.dump_triggers cascade;\n"
1867 : "drop function sys.schema_guard cascade;\n"
1868 : "drop function sys.replace_first(string, string, string, string) cascade;\n");
1869 :
1870 0 : pos += snprintf(buf + pos, bufsize - pos,
1871 : "CREATE FUNCTION sys.schema_guard(sch STRING, nme STRING, stmt STRING) RETURNS STRING BEGIN\n"
1872 : "RETURN\n"
1873 : " SELECT 'SET SCHEMA ' || sys.dq(sch) || '; ' || stmt;\n"
1874 : "END;\n"
1875 : "CREATE VIEW sys.dump_functions AS\n"
1876 : " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
1877 : " f.sch schema_name,\n"
1878 : " f.fun function_name\n"
1879 : " FROM sys.describe_functions f;\n"
1880 : "CREATE VIEW sys.dump_triggers AS\n"
1881 : " SELECT sys.schema_guard(sch, tab, def) stmt,\n"
1882 : " sch schema_name,\n"
1883 : " tab table_name,\n"
1884 : " tri trigger_name\n"
1885 : " FROM sys.describe_triggers;\n"
1886 : "CREATE VIEW sys.describe_partition_tables AS\n"
1887 : " SELECT\n"
1888 : " m_sch,\n"
1889 : " m_tbl,\n"
1890 : " p_sch,\n"
1891 : " p_tbl,\n"
1892 : " CASE\n"
1893 : " WHEN p_raw_type IS NULL THEN 'READ ONLY'\n"
1894 : " WHEN (p_raw_type = 'VALUES' AND pvalues IS NULL) OR (p_raw_type = 'RANGE' AND minimum IS NULL AND maximum IS NULL AND with_nulls) THEN 'FOR NULLS'\n"
1895 : " ELSE p_raw_type\n"
1896 : " END AS tpe,\n"
1897 : " pvalues,\n"
1898 : " minimum,\n"
1899 : " maximum,\n"
1900 : " with_nulls\n"
1901 : " FROM\n"
1902 : " (WITH\n"
1903 : " tp(\"type\", table_id) AS\n"
1904 : " (SELECT ifthenelse((table_partitions.\"type\" & 2) = 2, 'VALUES', 'RANGE'), table_partitions.table_id FROM sys.table_partitions),\n"
1905 : " subq(m_tid, p_mid, \"type\", m_sch, m_tbl, p_sch, p_tbl) AS\n"
1906 : " (SELECT m_t.id, p_m.id, m_t.\"type\", m_s.name, m_t.name, p_s.name, p_m.name\n"
1907 : " FROM sys.schemas m_s, sys._tables m_t, sys.dependencies d, sys.schemas p_s, sys._tables p_m\n"
1908 : " WHERE m_t.\"type\" IN (3, 6)\n"
1909 : " AND m_t.schema_id = m_s.id\n"
1910 : " AND m_s.name <> 'tmp'\n"
1911 : " AND m_t.system = FALSE\n"
1912 : " AND m_t.id = d.depend_id\n"
1913 : " AND d.id = p_m.id\n"
1914 : " AND p_m.schema_id = p_s.id\n"
1915 : " ORDER BY m_t.id, p_m.id),\n"
1916 : " vals(id,vals) as\n"
1917 : " (SELECT vp.table_id, GROUP_CONCAT(vp.value, ',') FROM sys.value_partitions vp GROUP BY vp.table_id)\n"
1918 : " SELECT\n"
1919 : " subq.m_sch,\n"
1920 : " subq.m_tbl,\n"
1921 : " subq.p_sch,\n"
1922 : " subq.p_tbl,\n"
1923 : " tp.\"type\" AS p_raw_type,\n"
1924 : " CASE WHEN tp.\"type\" = 'VALUES'\n"
1925 : " THEN (SELECT vals.vals FROM vals WHERE vals.id = subq.p_mid)\n"
1926 : " ELSE NULL\n"
1927 : " END AS pvalues,\n"
1928 : " CASE WHEN tp.\"type\" = 'RANGE'\n"
1929 : " THEN (SELECT minimum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
1930 : " ELSE NULL\n"
1931 : " END AS minimum,\n"
1932 : " CASE WHEN tp.\"type\" = 'RANGE'\n"
1933 : " THEN (SELECT maximum FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
1934 : " ELSE NULL\n"
1935 : " END AS maximum,\n"
1936 : " CASE WHEN tp.\"type\" = 'VALUES'\n"
1937 : " THEN EXISTS(SELECT vp.value FROM sys.value_partitions vp WHERE vp.table_id = subq.p_mid AND vp.value IS NULL)\n"
1938 : " ELSE (SELECT rp.with_nulls FROM sys.range_partitions rp WHERE rp.table_id = subq.p_mid)\n"
1939 : " END AS with_nulls\n"
1940 : " FROM\n"
1941 : " subq LEFT OUTER JOIN tp\n"
1942 : " ON subq.m_tid = tp.table_id) AS tmp_pi;\n"
1943 : "GRANT SELECT ON sys.describe_partition_tables TO PUBLIC;\n"
1944 : "CREATE VIEW sys.dump_partition_tables AS\n"
1945 : "SELECT\n"
1946 : " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
1947 : " CASE\n"
1948 : " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
1949 : " WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
1950 : " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
1951 : " ELSE '' --'READ ONLY'\n"
1952 : " END ||\n"
1953 : " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
1954 : " ';' stmt,\n"
1955 : " m_sch merge_schema_name,\n"
1956 : " m_tbl merge_table_name,\n"
1957 : " p_sch partition_schema_name,\n"
1958 : " p_tbl partition_table_name\n"
1959 : " FROM sys.describe_partition_tables;\n"
1960 : "CREATE VIEW sys.dump_sequences AS\n"
1961 : " SELECT\n"
1962 : " 'CREATE SEQUENCE ' || sys.FQN(sch, seq) || ' AS BIGINT;' stmt,\n"
1963 : " sch schema_name,\n"
1964 : " seq seqname\n"
1965 : " FROM sys.describe_sequences;\n"
1966 : "CREATE VIEW sys.dump_start_sequences AS\n"
1967 : " SELECT 'ALTER SEQUENCE ' || sys.FQN(sch, seq) ||\n"
1968 : " CASE WHEN s = 0 THEN '' ELSE ' RESTART WITH ' || rs END ||\n"
1969 : " CASE WHEN inc = 1 THEN '' ELSE ' INCREMENT BY ' || inc END ||\n"
1970 : " CASE WHEN nomin THEN ' NO MINVALUE' WHEN rmi IS NULL THEN '' ELSE ' MINVALUE ' || rmi END ||\n"
1971 : " CASE WHEN nomax THEN ' NO MAXVALUE' WHEN rma IS NULL THEN '' ELSE ' MAXVALUE ' || rma END ||\n"
1972 : " CASE WHEN \"cache\" = 1 THEN '' ELSE ' CACHE ' || \"cache\" END ||\n"
1973 : " CASE WHEN \"cycle\" THEN '' ELSE ' NO' END || ' CYCLE;' stmt,\n"
1974 : " sch schema_name,\n"
1975 : " seq sequence_name\n"
1976 : " FROM sys.describe_sequences;\n"
1977 : "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
1978 : "BEGIN\n"
1979 : " DECLARE tid INT;\n"
1980 : " SET tid = (SELECT MIN(t.id) FROM sys.tables t, sys.schemas s WHERE t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
1981 : " IF tid IS NOT NULL THEN\n"
1982 : " DECLARE k INT;\n"
1983 : " DECLARE m INT;\n"
1984 : " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
1985 : " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
1986 : " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
1987 : " DECLARE cname STRING;\n"
1988 : " DECLARE ctype STRING;\n"
1989 : " DECLARE _cnt INT;\n"
1990 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
1991 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
1992 : " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
1993 : " IF _cnt > 0 THEN\n"
1994 : " DECLARE COPY_INTO_STMT STRING;\n"
1995 : " DECLARE SELECT_DATA_STMT STRING;\n"
1996 : " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
1997 : " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
1998 : " WHILE (k < m) DO\n"
1999 : " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
2000 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
2001 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
2002 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
2003 : " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
2004 : " END WHILE;\n"
2005 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || ') FROM STDIN USING DELIMITERS ''|'',E''\\\\n'',''\"'';');\n"
2006 : " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
2007 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
2008 : " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
2009 : " END IF;\n"
2010 : " END IF;\n"
2011 : " END IF;\n"
2012 : "END;\n"
2013 : "CREATE PROCEDURE sys.dump_table_data()\n"
2014 : "BEGIN\n"
2015 : " DECLARE i INT;\n"
2016 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
2017 : " IF i IS NOT NULL THEN\n"
2018 : " DECLARE M INT;\n"
2019 : " SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
2020 : " DECLARE sch STRING;\n"
2021 : " DECLARE tbl STRING;\n"
2022 : " WHILE i IS NOT NULL AND i <= M DO\n"
2023 : " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
2024 : " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
2025 : " CALL sys.dump_table_data(sch, tbl);\n"
2026 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
2027 : " END WHILE;\n"
2028 : " END IF;\n"
2029 : "END;\n"
2030 : "CREATE VIEW sys.dump_create_users AS\n"
2031 : " SELECT\n"
2032 : " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
2033 : " sys.sq(sys.password_hash(ui.name)) ||\n"
2034 : " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
2035 : " ui.name user_name\n"
2036 : " FROM sys.db_user_info ui, sys.schemas s\n"
2037 : " WHERE ui.default_schema = s.id\n"
2038 : " AND ui.name <> 'monetdb'\n"
2039 : " AND ui.name <> '.snapshot';\n");
2040 :
2041 0 : pos += snprintf(buf + pos, bufsize - pos,
2042 : "CREATE VIEW sys.describe_tables AS\n"
2043 : " SELECT\n"
2044 : " t.id o,\n"
2045 : " s.name sch,\n"
2046 : " t.name tab,\n"
2047 : " ts.table_type_name typ,\n"
2048 : " (SELECT\n"
2049 : " ' (' ||\n"
2050 : " GROUP_CONCAT(\n"
2051 : " sys.DQ(c.name) || ' ' ||\n"
2052 : " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
2053 : " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
2054 : " , ', ') || ')'\n"
2055 : " FROM sys._columns c\n"
2056 : " WHERE c.table_id = t.id) col,\n"
2057 : " CASE ts.table_type_name\n"
2058 : " WHEN 'REMOTE TABLE' THEN\n"
2059 : " sys.get_remote_table_expressions(s.name, t.name)\n"
2060 : " WHEN 'MERGE TABLE' THEN\n"
2061 : " sys.get_merge_table_partition_expressions(t.id)\n"
2062 : " WHEN 'VIEW' THEN\n"
2063 : " sys.schema_guard(s.name, t.name, t.query)\n"
2064 : " ELSE\n"
2065 : " ''\n"
2066 : " END opt\n"
2067 : " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
2068 : " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
2069 : " AND t.system = FALSE\n"
2070 : " AND s.id = t.schema_id\n"
2071 : " AND ts.table_type_id = t.type\n"
2072 : " AND s.name <> 'tmp';\n"
2073 : "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
2074 : "CREATE VIEW sys.dump_tables AS\n"
2075 : " SELECT\n"
2076 : " t.o o,\n"
2077 : " CASE\n"
2078 : " WHEN t.typ <> 'VIEW' THEN\n"
2079 : " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
2080 : " ELSE\n"
2081 : " t.opt\n"
2082 : " END stmt,\n"
2083 : " t.sch schema_name,\n"
2084 : " t.tab table_name\n"
2085 : " FROM sys.describe_tables t;\n"
2086 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
2087 : "BEGIN\n"
2088 : " SET SCHEMA sys;\n"
2089 : " TRUNCATE sys.dump_statements;\n"
2090 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
2091 : " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
2092 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
2093 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
2094 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
2095 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
2096 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
2097 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
2098 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
2099 : " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
2100 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
2101 : " FROM (\n"
2102 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
2103 : " UNION ALL\n"
2104 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
2105 : " ) AS stmts(o, s);\n"
2106 : " -- dump table data before adding constraints and fixing sequences\n"
2107 : " IF NOT DESCRIBE THEN\n"
2108 : " CALL sys.dump_table_data();\n"
2109 : " END IF;\n"
2110 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
2111 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
2112 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
2113 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
2114 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
2115 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
2116 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
2117 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
2118 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
2119 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
2120 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
2121 : " --TODO Improve performance of dump_table_data.\n"
2122 : " --TODO loaders ,procedures, window and filter sys.functions.\n"
2123 : " --TODO look into order dependent group_concat\n"
2124 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
2125 : " RETURN sys.dump_statements;\n"
2126 : "END;\n");
2127 0 : pos += snprintf(buf + pos, bufsize - pos,
2128 : "update sys._tables set system = true where name in ('describe_partition_tables', 'dump_partition_tables', 'dump_sequences', 'dump_start_sequences', 'describe_tables', 'dump_tables', 'dump_create_users', 'dump_functions', 'dump_triggers') AND schema_id = 2000;\n");
2129 0 : pos += snprintf(buf + pos, bufsize - pos,
2130 : "update sys.functions set system = true where system <> true and name in ('dump_table_data') and schema_id = 2000 and type = %d;\n", F_PROC);
2131 0 : pos += snprintf(buf + pos, bufsize - pos,
2132 : "update sys.functions set system = true where system <> true and name in ('dump_database') and schema_id = 2000 and type = %d;\n", F_UNION);
2133 0 : pos += snprintf(buf + pos, bufsize - pos,
2134 : "update sys.functions set system = true where system <> true and name in ('schema_guard') and schema_id = 2000 and type = %d;\n", F_FUNC);
2135 :
2136 : /* 12_url.sql */
2137 0 : pos += snprintf(buf + pos, bufsize - pos,
2138 : "CREATE function sys.url_extract_host(url string, no_www bool) RETURNS STRING\n"
2139 : "EXTERNAL NAME url.\"extractURLHost\";\n"
2140 : "GRANT EXECUTE ON FUNCTION url_extract_host(string, bool) TO PUBLIC;\n"
2141 : "update sys.functions set system = true where system <> true and name = 'url_extract_host' and schema_id = 2000 and type = %d;\n", F_FUNC);
2142 :
2143 0 : assert(pos < bufsize);
2144 0 : printf("Running database upgrade commands:\n%s\n", buf);
2145 0 : fflush(stdout);
2146 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2147 : }
2148 115 : res_table_destroy(output);
2149 115 : output = NULL;
2150 115 : if (err != MAL_SUCCEED) {
2151 0 : GDKfree(buf);
2152 0 : return err;
2153 : }
2154 :
2155 : /* 10_sys_schema_extensions */
2156 : /* if the keyword LOCKED is in the list of keywords, upgrade */
2157 115 : pos = snprintf(buf, bufsize, "select keyword from sys.keywords where keyword = 'LOCKED';\n");
2158 115 : assert(pos < bufsize);
2159 115 : if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
2160 0 : goto bailout;
2161 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
2162 0 : pos = snprintf(buf, bufsize,
2163 : "ALTER TABLE sys.keywords SET READ WRITE;\n"
2164 : "DELETE FROM sys.keywords WHERE keyword IN ('LOCKED');\n");
2165 0 : assert(pos < bufsize);
2166 0 : printf("Running database upgrade commands:\n%s\n", buf);
2167 0 : fflush(stdout);
2168 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2169 0 : if (err == MAL_SUCCEED) {
2170 0 : pos = snprintf(buf, bufsize, "ALTER TABLE sys.keywords SET READ ONLY;\n");
2171 0 : assert(pos < bufsize);
2172 0 : printf("Running database upgrade commands:\n%s\n", buf);
2173 0 : fflush(stdout);
2174 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2175 : }
2176 : }
2177 115 : res_table_destroy(output);
2178 115 : output = NULL;
2179 115 : if (err != MAL_SUCCEED) {
2180 0 : GDKfree(buf);
2181 0 : return err;
2182 : }
2183 :
2184 : /* if the table type UNLOGGED TABLE is not in the list of table
2185 : * types, upgrade */
2186 115 : pos = snprintf(buf, bufsize, "select table_type_name from sys.table_types where table_type_name = 'UNLOGGED TABLE';\n");
2187 115 : assert(pos < bufsize);
2188 115 : if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
2189 0 : goto bailout;
2190 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
2191 0 : pos = snprintf(buf, bufsize,
2192 : "ALTER TABLE sys.table_types SET READ WRITE;\n"
2193 : "INSERT INTO sys.table_types VALUES (7, 'UNLOGGED TABLE');\n");
2194 0 : assert(pos < bufsize);
2195 0 : printf("Running database upgrade commands:\n%s\n", buf);
2196 0 : fflush(stdout);
2197 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2198 0 : if (err == MAL_SUCCEED) {
2199 0 : pos = snprintf(buf, bufsize, "ALTER TABLE sys.table_types SET READ ONLY;\n");
2200 0 : assert(pos < bufsize);
2201 0 : printf("Running database upgrade commands:\n%s\n", buf);
2202 0 : fflush(stdout);
2203 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2204 : }
2205 : }
2206 115 : res_table_destroy(output);
2207 115 : output = NULL;
2208 :
2209 : /* 16_tracelog */
2210 115 : pos = snprintf(buf, bufsize,
2211 : "select f.id "
2212 : "from sys.schemas s, "
2213 : "sys.functions f, "
2214 : "sys.auths a, "
2215 : "sys.privileges p, "
2216 : "sys.auths g, "
2217 : "sys.function_types ft, "
2218 : "sys.privilege_codes pc "
2219 : "where s.id = f.schema_id "
2220 : "and f.id = p.obj_id "
2221 : "and p.auth_id = a.id "
2222 : "and p.grantor = g.id "
2223 : "and p.privileges = pc.privilege_code_id "
2224 : "and f.type = ft.function_type_id "
2225 : "and s.name = 'sys' "
2226 : "and f.name = 'tracelog' "
2227 : "and ft.function_type_keyword = 'FUNCTION';\n");
2228 115 : assert(pos < bufsize);
2229 115 : if ((err = SQLstatementIntern(c, buf, "update", true, false, &output)))
2230 0 : goto bailout;
2231 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
2232 0 : pos = snprintf(buf, bufsize,
2233 : "grant execute on function sys.tracelog to public;\n"
2234 : "grant select on sys.tracelog to public;\n");
2235 0 : assert(pos < bufsize);
2236 0 : printf("Running database upgrade commands:\n%s\n", buf);
2237 0 : fflush(stdout);
2238 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2239 : }
2240 115 : res_table_destroy(output);
2241 115 : output = NULL;
2242 115 : if (err != MAL_SUCCEED) {
2243 0 : GDKfree(buf);
2244 0 : return err;
2245 : }
2246 :
2247 115 : bailout:
2248 115 : if (output)
2249 0 : res_table_destroy(output);
2250 115 : GDKfree(buf);
2251 115 : return err; /* usually MAL_SUCCEED */
2252 : }
2253 :
2254 : static str
2255 115 : sql_update_jun2023(Client c, mvc *sql, sql_schema *s)
2256 : {
2257 115 : size_t bufsize = 65536, pos = 0;
2258 115 : char *err = NULL, *buf = GDKmalloc(bufsize);
2259 115 : res_table *output;
2260 115 : BAT *b;
2261 115 : sql_subtype t1, t2;
2262 :
2263 115 : (void) sql;
2264 115 : if (buf == NULL)
2265 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
2266 :
2267 : /* wlc/wlr support was removed */
2268 : {
2269 115 : sql_schema *wl = mvc_bind_schema(sql, "wlc");
2270 115 : sql_schema *wr = mvc_bind_schema(sql, "wlr");
2271 115 : if (wl != NULL || wr != NULL) {
2272 0 : if (wl)
2273 0 : wl->system = 0;
2274 0 : if (wr)
2275 0 : wr->system = 0;
2276 :
2277 0 : const char query[] =
2278 : "drop procedure if exists wlc.master() cascade;\n"
2279 : "drop procedure if exists wlc.master(string) cascade;\n"
2280 : "drop procedure if exists wlc.stop() cascade;\n"
2281 : "drop procedure if exists wlc.flush() cascade;\n"
2282 : "drop procedure if exists wlc.beat(int) cascade;\n"
2283 : "drop function if exists wlc.clock() cascade;\n"
2284 : "drop function if exists wlc.tick() cascade;\n"
2285 : "drop procedure if exists wlr.master(string) cascade;\n"
2286 : "drop procedure if exists wlr.stop() cascade;\n"
2287 : "drop procedure if exists wlr.accept() cascade;\n"
2288 : "drop procedure if exists wlr.replicate() cascade;\n"
2289 : "drop procedure if exists wlr.replicate(timestamp) cascade;\n"
2290 : "drop procedure if exists wlr.replicate(tinyint) cascade;\n"
2291 : "drop procedure if exists wlr.replicate(smallint) cascade;\n"
2292 : "drop procedure if exists wlr.replicate(integer) cascade;\n"
2293 : "drop procedure if exists wlr.replicate(bigint) cascade;\n"
2294 : "drop procedure if exists wlr.beat(integer) cascade;\n"
2295 : "drop function if exists wlr.clock() cascade;\n"
2296 : "drop function if exists wlr.tick() cascade;\n"
2297 : "drop schema if exists wlc cascade;\n"
2298 : "drop schema if exists wlr cascade;\n";
2299 0 : printf("Running database upgrade commands:\n%s\n", query);
2300 0 : fflush(stdout);
2301 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2302 : }
2303 : }
2304 :
2305 : /* new function sys.regexp_replace */
2306 115 : allocator *old_sa = sql->sa;
2307 115 : if ((sql->sa = sa_create(sql->pa)) != NULL) {
2308 115 : list *l;
2309 115 : if ((l = sa_list(sql->sa)) != NULL) {
2310 115 : sql_subtype tp;
2311 115 : sql_find_subtype(&tp, "varchar", 0, 0);
2312 115 : list_append(l, &tp);
2313 115 : list_append(l, &tp);
2314 115 : list_append(l, &tp);
2315 115 : list_append(l, &tp);
2316 115 : if (!sql_bind_func_(sql, s->base.name, "regexp_replace", l, F_FUNC, true, true)) {
2317 0 : pos = snprintf(buf, bufsize,
2318 : "create function sys.regexp_replace(ori string, pat string, rep string, flg string)\n"
2319 : "returns string external name pcre.replace;\n"
2320 : "grant execute on function regexp_replace(string, string, string, string) to public;\n"
2321 : "create function sys.regexp_replace(ori string, pat string, rep string)\n"
2322 : "returns string\n"
2323 : "begin\n"
2324 : " return sys.regexp_replace(ori, pat, rep, '');\n"
2325 : "end;\n"
2326 : "grant execute on function regexp_replace(string, string, string) to public;\n"
2327 : "update sys.functions set system = true where system <> true and name = 'regexp_replace' and schema_id = 2000 and type = %d;\n",
2328 : F_FUNC);
2329 0 : assert(pos < bufsize);
2330 0 : sql->session->status = 0;
2331 0 : sql->errstr[0] = '\0';
2332 0 : printf("Running database upgrade commands:\n%s\n", buf);
2333 0 : fflush(stdout);
2334 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2335 : }
2336 115 : sa_destroy(sql->sa);
2337 : }
2338 : }
2339 115 : sql->sa = old_sa;
2340 :
2341 : /* fixes for handling single quotes in strings so that we can run
2342 : * with raw_strings after having created a database without (and
2343 : * v.v.) */
2344 115 : if ((err = SQLstatementIntern(c, "select id from sys.functions where name = 'dump_table_data' and schema_id = 2000 and func like '% R'')%';\n", "update", true, false, &output)) == NULL) {
2345 115 : if (((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) || find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
2346 0 : sql_table *t;
2347 0 : if ((t = mvc_bind_table(sql, s, "describe_tables")) != NULL)
2348 0 : t->system = 0;
2349 0 : if ((t = mvc_bind_table(sql, s, "dump_create_users")) != NULL)
2350 0 : t->system = 0;
2351 0 : if ((t = mvc_bind_table(sql, s, "dump_partition_tables")) != NULL)
2352 0 : t->system = 0;
2353 0 : if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
2354 0 : t->system = 0;
2355 0 : if ((t = mvc_bind_table(sql, s, "dump_tables")) != NULL)
2356 0 : t->system = 0;
2357 0 : pos = 0;
2358 0 : pos += snprintf(buf + pos, bufsize - pos,
2359 : "drop function if exists sys.dump_database(boolean) cascade;\n"
2360 : "drop procedure if exists sys.dump_table_data() cascade;\n"
2361 : "drop procedure if exists sys.dump_table_data(string, string) cascade;\n"
2362 : "drop view if exists sys.dump_tables cascade;\n"
2363 : "drop view if exists sys.dump_comments cascade;\n"
2364 : "drop function if exists sys.prepare_esc(string, string) cascade;\n"
2365 : "drop view if exists sys.dump_partition_tables cascade;\n"
2366 : "drop view if exists sys.dump_create_users cascade;\n"
2367 : "drop view if exists sys.describe_tables cascade;\n"
2368 : "drop function if exists sys.get_remote_table_expressions(string, string) cascade;\n"
2369 : "drop function if exists sys.remote_table_credentials(string) cascade;\n"
2370 : "drop function if exists sys.sq(string) cascade;\n");
2371 0 : if (find_sql_table(sql->session->tr, s, "remote_user_info") == NULL) {
2372 0 : pos += snprintf(buf + pos, bufsize - pos,
2373 : "create table sys.remote_user_info (table_id int, username varchar(1024), password varchar(256));\n"
2374 : "create function sys.decypher (cypher string) returns string external name sql.decypher;\n"
2375 : "update sys.functions set system = true where system <> true and name = 'decypher' and schema_id = 2000 and type = %d;\n"
2376 : "update sys._tables set system = true where system <> true and name = 'remote_user_info' and schema_id = 2000;\n",
2377 : F_FUNC);
2378 : }
2379 0 : pos += snprintf(buf + pos, bufsize - pos,
2380 : "CREATE FUNCTION sys.SQ (s STRING) RETURNS STRING BEGIN RETURN '''' || sys.replace(s,'''','''''') || ''''; END;\n"
2381 : "CREATE FUNCTION sys.get_remote_table_expressions(s STRING, t STRING) RETURNS STRING BEGIN\n"
2382 : " RETURN SELECT ' ON ' || sys.SQ(tt.query) || ' WITH USER ' || sys.SQ(username) || ' ENCRYPTED PASSWORD ' || sys.SQ(sys.decypher(\"password\")) FROM sys.remote_user_info r, sys._tables tt, sys.schemas ss where tt.name = t and ss.name = s and tt.schema_id = ss.id and r.table_id = tt.id;\n"
2383 : "END;\n"
2384 : "CREATE VIEW sys.describe_tables AS\n"
2385 : " SELECT\n"
2386 : " t.id o,\n"
2387 : " s.name sch,\n"
2388 : " t.name tab,\n"
2389 : " ts.table_type_name typ,\n"
2390 : " (SELECT\n"
2391 : " ' (' ||\n"
2392 : " GROUP_CONCAT(\n"
2393 : " sys.DQ(c.name) || ' ' ||\n"
2394 : " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
2395 : " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
2396 : " , ', ') || ')'\n"
2397 : " FROM sys._columns c\n"
2398 : " WHERE c.table_id = t.id) col,\n"
2399 : " CASE ts.table_type_name\n"
2400 : " WHEN 'REMOTE TABLE' THEN\n"
2401 : " sys.get_remote_table_expressions(s.name, t.name)\n"
2402 : " WHEN 'MERGE TABLE' THEN\n"
2403 : " sys.get_merge_table_partition_expressions(t.id)\n"
2404 : " WHEN 'VIEW' THEN\n"
2405 : " sys.schema_guard(s.name, t.name, t.query)\n"
2406 : " ELSE\n"
2407 : " ''\n"
2408 : " END opt\n"
2409 : " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
2410 : " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
2411 : " AND t.system = FALSE\n"
2412 : " AND s.id = t.schema_id\n"
2413 : " AND ts.table_type_id = t.type\n"
2414 : " AND s.name <> 'tmp';\n"
2415 : "CREATE VIEW sys.dump_create_users AS\n"
2416 : " SELECT\n"
2417 : " 'CREATE USER ' || sys.dq(ui.name) || ' WITH ENCRYPTED PASSWORD ' ||\n"
2418 : " sys.sq(sys.password_hash(ui.name)) ||\n"
2419 : " ' NAME ' || sys.sq(ui.fullname) || ' SCHEMA sys' || ifthenelse(ui.schema_path = '\"sys\"', '', ' SCHEMA PATH ' || sys.sq(ui.schema_path)) || ';' stmt,\n"
2420 : " ui.name user_name\n"
2421 : " FROM sys.db_user_info ui, sys.schemas s\n"
2422 : " WHERE ui.default_schema = s.id\n"
2423 : " AND ui.name <> 'monetdb'\n"
2424 : " AND ui.name <> '.snapshot';\n"
2425 : "CREATE VIEW sys.dump_partition_tables AS\n"
2426 : " SELECT\n"
2427 : " 'ALTER TABLE ' || sys.FQN(m_sch, m_tbl) || ' ADD TABLE ' || sys.FQN(p_sch, p_tbl) ||\n"
2428 : " CASE\n"
2429 : " WHEN tpe = 'VALUES' THEN ' AS PARTITION IN (' || pvalues || ')'\n"
2430 : " WHEN tpe = 'RANGE' THEN ' AS PARTITION FROM ' || ifthenelse(minimum IS NOT NULL, sys.SQ(minimum), 'RANGE MINVALUE') || ' TO ' || ifthenelse(maximum IS NOT NULL, sys.SQ(maximum), 'RANGE MAXVALUE')\n"
2431 : " WHEN tpe = 'FOR NULLS' THEN ' AS PARTITION FOR NULL VALUES'\n"
2432 : " ELSE '' --'READ ONLY'\n"
2433 : " END ||\n"
2434 : " CASE WHEN tpe in ('VALUES', 'RANGE') AND with_nulls THEN ' WITH NULL VALUES' ELSE '' END ||\n"
2435 : " ';' stmt,\n"
2436 : " m_sch merge_schema_name,\n"
2437 : " m_tbl merge_table_name,\n"
2438 : " p_sch partition_schema_name,\n"
2439 : " p_tbl partition_table_name\n"
2440 : " FROM sys.describe_partition_tables;\n"
2441 : "CREATE VIEW sys.dump_tables AS\n"
2442 : " SELECT\n"
2443 : " t.o o,\n"
2444 : " CASE\n"
2445 : " WHEN t.typ <> 'VIEW' THEN\n"
2446 : " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
2447 : " ELSE\n"
2448 : " t.opt\n"
2449 : " END stmt,\n"
2450 : " t.sch schema_name,\n"
2451 : " t.tab table_name\n"
2452 : " FROM sys.describe_tables t;\n"
2453 : "CREATE VIEW sys.dump_comments AS\n"
2454 : " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
2455 : "CREATE FUNCTION sys.prepare_esc(s STRING, t STRING) RETURNS STRING\n"
2456 : "BEGIN\n"
2457 : " RETURN\n"
2458 : " CASE\n"
2459 : " WHEN (t = 'varchar' OR t ='char' OR t = 'clob' OR t = 'json' OR t = 'geometry' OR t = 'url') THEN\n"
2460 : " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE ' || 'sys.esc(' || sys.DQ(s) || ')' || ' END'\n"
2461 : " ELSE\n"
2462 : " 'CASE WHEN ' || sys.DQ(s) || ' IS NULL THEN ''null'' ELSE CAST(' || sys.DQ(s) || ' AS STRING) END'\n"
2463 : " END;\n"
2464 : "END;\n"
2465 : "CREATE PROCEDURE sys.dump_table_data(sch STRING, tbl STRING)\n"
2466 : "BEGIN\n"
2467 : " DECLARE tid INT;\n"
2468 : " SET tid = (SELECT MIN(t.id) FROM sys.tables t, sys.schemas s WHERE t.name = tbl AND t.schema_id = s.id AND s.name = sch);\n"
2469 : " IF tid IS NOT NULL THEN\n"
2470 : " DECLARE k INT;\n"
2471 : " DECLARE m INT;\n"
2472 : " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
2473 : " SET m = (SELECT MAX(c.id) FROM sys.columns c WHERE c.table_id = tid);\n"
2474 : " IF k IS NOT NULL AND m IS NOT NULL THEN\n"
2475 : " DECLARE cname STRING;\n"
2476 : " DECLARE ctype STRING;\n"
2477 : " DECLARE _cnt INT;\n"
2478 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
2479 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
2480 : " SET _cnt = (SELECT count FROM sys.storage(sch, tbl, cname));\n"
2481 : " IF _cnt > 0 THEN\n"
2482 : " DECLARE COPY_INTO_STMT STRING;\n"
2483 : " DECLARE SELECT_DATA_STMT STRING;\n"
2484 : " SET COPY_INTO_STMT = 'COPY ' || _cnt || ' RECORDS INTO ' || sys.FQN(sch, tbl) || '(' || sys.DQ(cname);\n"
2485 : " SET SELECT_DATA_STMT = 'SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), ' || sys.prepare_esc(cname, ctype);\n"
2486 : " WHILE (k < m) DO\n"
2487 : " SET k = (SELECT MIN(c.id) FROM sys.columns c WHERE c.table_id = tid AND c.id > k);\n"
2488 : " SET cname = (SELECT c.name FROM sys.columns c WHERE c.id = k);\n"
2489 : " SET ctype = (SELECT c.type FROM sys.columns c WHERE c.id = k);\n"
2490 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || ', ' || sys.DQ(cname));\n"
2491 : " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || '|| ''|'' || ' || sys.prepare_esc(cname, ctype));\n"
2492 : " END WHILE;\n"
2493 : " SET COPY_INTO_STMT = (COPY_INTO_STMT || R') FROM STDIN USING DELIMITERS ''|'',E''\\n'',''\"'';');\n"
2494 : " SET SELECT_DATA_STMT = (SELECT_DATA_STMT || ' FROM ' || sys.FQN(sch, tbl));\n"
2495 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, COPY_INTO_STMT);\n"
2496 : " CALL sys.EVAL('INSERT INTO sys.dump_statements ' || SELECT_DATA_STMT || ';');\n"
2497 : " END IF;\n"
2498 : " END IF;\n"
2499 : " END IF;\n"
2500 : " END;\n"
2501 : "CREATE PROCEDURE sys.dump_table_data()\n"
2502 : "BEGIN\n"
2503 : " DECLARE i INT;\n"
2504 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
2505 : " IF i IS NOT NULL THEN\n"
2506 : " DECLARE M INT;\n"
2507 : " SET M = (SELECT MAX(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system);\n"
2508 : " DECLARE sch STRING;\n"
2509 : " DECLARE tbl STRING;\n"
2510 : " WHILE i IS NOT NULL AND i <= M DO\n"
2511 : " SET sch = (SELECT s.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
2512 : " SET tbl = (SELECT t.name FROM sys.tables t, sys.schemas s WHERE s.id = t.schema_id AND t.id = i);\n"
2513 : " CALL sys.dump_table_data(sch, tbl);\n"
2514 : " SET i = (SELECT MIN(t.id) FROM sys.tables t, sys.table_types ts WHERE t.type = ts.table_type_id AND ts.table_type_name = 'TABLE' AND NOT t.system AND t.id > i);\n"
2515 : " END WHILE;\n"
2516 : " END IF;\n"
2517 : "END;\n"
2518 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
2519 : "BEGIN\n"
2520 : " SET SCHEMA sys;\n"
2521 : " TRUNCATE sys.dump_statements;\n"
2522 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
2523 : " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
2524 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
2525 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
2526 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
2527 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
2528 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
2529 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
2530 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
2531 : " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
2532 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
2533 : " FROM (\n"
2534 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
2535 : " UNION ALL\n"
2536 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
2537 : " ) AS stmts(o, s);\n"
2538 : " -- dump table data before adding constraints and fixing sequences\n"
2539 : " IF NOT DESCRIBE THEN\n"
2540 : " CALL sys.dump_table_data();\n"
2541 : " END IF;\n"
2542 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
2543 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
2544 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
2545 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
2546 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
2547 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
2548 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
2549 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
2550 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
2551 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
2552 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
2553 : " --TODO Improve performance of dump_table_data.\n"
2554 : " --TODO loaders ,procedures, window and filter sys.functions.\n"
2555 : " --TODO look into order dependent group_concat\n"
2556 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
2557 : " RETURN sys.dump_statements;\n"
2558 : "END;\n"
2559 : "update sys.functions set system = true where system <> true and name in ('sq', 'get_remote_table_expressions', 'prepare_esc') and schema_id = 2000 and type = %d;\n"
2560 : "update sys._tables set system = true where system <> true and name in ('describe_tables', 'dump_create_users', 'dump_partition_tables', 'dump_comments', 'dump_tables') and schema_id = 2000;\n"
2561 : "update sys.functions set system = true where system <> true and name = 'dump_table_data' and schema_id = 2000 and type = %d;\n"
2562 : "update sys.functions set system = true where system <> true and name = 'dump_database' and schema_id = 2000 and type = %d;\n"
2563 : "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n",
2564 : F_FUNC, F_PROC, F_UNION);
2565 0 : assert(pos < bufsize);
2566 0 : printf("Running database upgrade commands:\n%s\n", buf);
2567 0 : fflush(stdout);
2568 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2569 : }
2570 115 : res_table_destroy(output);
2571 115 : output = NULL;
2572 : }
2573 :
2574 : /* Add new column 'function_id' to views
2575 : * sys.dependency_tables_on_functions and dependency_views_on_functions */
2576 : {
2577 115 : sql_table *t = find_sql_table(sql->session->tr, s, "dependency_tables_on_functions");
2578 115 : if (t != NULL && find_sql_column(t, "function_id") == NULL) {
2579 0 : t->system = 0; /* sys.dependency_tables_on_functions */
2580 0 : if ((t = mvc_bind_table(sql, s, "dependency_views_on_functions")) != NULL)
2581 0 : t->system = 0;
2582 0 : pos = 0;
2583 0 : pos += snprintf(buf + pos, bufsize - pos,
2584 : "drop view if exists sys.dependency_tables_on_functions cascade;\n"
2585 : "drop view if exists sys.dependency_views_on_functions cascade;\n"
2586 : "CREATE VIEW sys.dependency_tables_on_functions AS\n"
2587 : "SELECT t.schema_id AS table_schema_id, t.id AS table_id, t.name AS table_name,"
2588 : " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
2589 : " FROM sys.functions AS f, sys.tables AS t, sys.dependencies AS dep\n"
2590 : " WHERE t.id = dep.id AND f.id = dep.depend_id\n"
2591 : " AND dep.depend_type = 7 AND f.type <> 2 AND t.type NOT IN (1, 11)\n"
2592 : " ORDER BY t.name, t.schema_id, f.name, f.id;\n"
2593 : "GRANT SELECT ON sys.dependency_tables_on_functions TO PUBLIC;\n"
2594 : "CREATE VIEW sys.dependency_views_on_functions AS\n"
2595 : "SELECT v.schema_id AS view_schema_id, v.id AS view_id, v.name AS view_name,"
2596 : " f.id AS function_id, f.name AS function_name, f.type AS function_type, dep.depend_type AS depend_type\n"
2597 : " FROM sys.functions AS f, sys.tables AS v, sys.dependencies AS dep\n"
2598 : " WHERE v.id = dep.id AND f.id = dep.depend_id\n"
2599 : " AND dep.depend_type = 7 AND f.type <> 2 AND v.type IN (1, 11)\n"
2600 : " ORDER BY v.name, v.schema_id, f.name, f.id;\n"
2601 : "GRANT SELECT ON sys.dependency_views_on_functions TO PUBLIC;\n"
2602 : "update sys._tables set system = true where system <> true and name in "
2603 : "('dependency_tables_on_functions','dependency_views_on_functions') and schema_id = 2000;\n");
2604 0 : assert(pos < bufsize);
2605 0 : printf("Running database upgrade commands:\n%s\n", buf);
2606 0 : fflush(stdout);
2607 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2608 : }
2609 : }
2610 :
2611 115 : if (!sql_bind_func(sql, "sys", "database", NULL, NULL, F_FUNC, true, true)) {
2612 0 : sql->session->status = 0; /* if the function was not found clean the error */
2613 0 : sql->errstr[0] = '\0';
2614 0 : pos = snprintf(buf, bufsize,
2615 : "create function sys.database ()\n"
2616 : "returns string\n"
2617 : "external name inspect.\"getDatabaseName\";\n"
2618 : "grant execute on function sys.database() to public;\n"
2619 : "update sys.functions set system = true where system <> true and name = 'database' and schema_id = 2000 and type = %d;\n",
2620 : (int) F_FUNC);
2621 0 : printf("Running database upgrade commands:\n%s\n", buf);
2622 0 : fflush(stdout);
2623 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2624 : }
2625 :
2626 : /* Add new sysadmin procedure calls: stop, pause and resume with two
2627 : arguments, first arg is query OID and second the user username that
2628 : the query in bound to. */
2629 115 : sql_find_subtype(&t1, "bigint", 64, 0);
2630 115 : sql_find_subtype(&t2, "varchar", 0, 0);
2631 115 : if (!sql_bind_func(sql, "sys", "pause", &t1, &t2, F_PROC, true, true)) {
2632 0 : sql->session->status = 0; /* if the function was not found clean the error */
2633 0 : sql->errstr[0] = '\0';
2634 0 : const char query[] =
2635 : "create function sys.queue(username string) returns table(\"tag\" bigint, \"sessionid\" int, \"username\" string, \"started\" timestamp, \"status\" string, \"query\" string, \"finished\" timestamp, \"maxworkers\" int, \"footprint\" int) external name sysmon.queue;\n"
2636 : "create procedure sys.pause(tag bigint, username string) external name sysmon.pause;\n"
2637 : "create procedure sys.resume(tag bigint, username string) external name sysmon.resume;\n"
2638 : "create procedure sys.stop(tag bigint, username string) external name sysmon.stop;\n"
2639 : "update sys.functions set system = true where system <> true and mod = 'sysmon' and name in ('stop', 'pause', 'resume', 'queue');\n";
2640 0 : printf("Running database upgrade commands:\n%s\n", query);
2641 0 : fflush(stdout);
2642 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2643 : }
2644 :
2645 : /* sys.settimeout and sys.setsession where removed */
2646 115 : if (sql_bind_func(sql, "sys", "settimeout", &t1, NULL, F_PROC, true, true)) {
2647 0 : const char query[] =
2648 : "drop procedure sys.settimeout(bigint) cascade;\n"
2649 : "drop procedure sys.settimeout(bigint, bigint) cascade;\n"
2650 : "drop procedure sys.setsession(bigint) cascade;\n";
2651 0 : printf("Running database upgrade commands:\n%s\n", query);
2652 0 : fflush(stdout);
2653 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2654 : } else {
2655 115 : sql->session->status = 0; /* if the function was not found clean the error */
2656 115 : sql->errstr[0] = '\0';
2657 : }
2658 :
2659 115 : if (!sql_bind_func(sql, "sys", "jarowinkler", &t2, &t2, F_FUNC, true, true)) {
2660 0 : sql->session->status = 0; /* if the function was not found clean the error */
2661 0 : sql->errstr[0] = '\0';
2662 0 : pos = snprintf(buf, bufsize,
2663 : "create function sys.levenshtein(x string, y string)\n"
2664 : "returns int external name txtsim.levenshtein;\n"
2665 : "grant execute on function levenshtein(string, string) to public;\n"
2666 : "create function sys.levenshtein(x string, y string, insdel int, rep int)\n"
2667 : "returns int external name txtsim.levenshtein;\n"
2668 : "grant execute on function levenshtein(string, string, int, int) to public;\n"
2669 : "create function sys.levenshtein(x string, y string, insdel int, rep int, trans int)\n"
2670 : "returns int external name txtsim.levenshtein;\n"
2671 : "grant execute on function levenshtein(string, string, int, int, int) to public;\n"
2672 : "create filter function sys.maxlevenshtein(x string, y string, k int)\n"
2673 : "external name txtsim.maxlevenshtein;\n"
2674 : "grant execute on filter function maxlevenshtein(string, string, int) to public;\n"
2675 : "create filter function sys.maxlevenshtein(x string, y string, k int, insdel int, rep int)\n"
2676 : "external name txtsim.maxlevenshtein;\n"
2677 : "grant execute on filter function maxlevenshtein(string, string, int, int, int) to public;\n"
2678 : "create function sys.jarowinkler(x string, y string)\n"
2679 : "returns double external name txtsim.jarowinkler;\n"
2680 : "grant execute on function jarowinkler(string, string) to public;\n"
2681 : "create filter function minjarowinkler(x string, y string, threshold double)\n"
2682 : "external name txtsim.minjarowinkler;\n"
2683 : "grant execute on filter function minjarowinkler(string, string, double) to public;\n"
2684 : "create function sys.dameraulevenshtein(x string, y string)\n"
2685 : "returns int external name txtsim.dameraulevenshtein;\n"
2686 : "grant execute on function dameraulevenshtein(string, string) to public;\n"
2687 : "create function sys.dameraulevenshtein(x string, y string, insdel int, rep int, trans int)\n"
2688 : "returns int external name txtsim.dameraulevenshtein;\n"
2689 : "grant execute on function dameraulevenshtein(string, string, int, int, int) to public;\n"
2690 :
2691 : "create function sys.editdistance(x string, y string)\n"
2692 : "returns int external name txtsim.editdistance;\n"
2693 : "grant execute on function editdistance(string, string) to public;\n"
2694 : "create function sys.editdistance2(x string, y string)\n"
2695 : "returns int external name txtsim.editdistance2;\n"
2696 : "grant execute on function editdistance2(string, string) to public;\n"
2697 : "create function sys.soundex(x string)\n"
2698 : "returns string external name txtsim.soundex;\n"
2699 : "grant execute on function soundex(string) to public;\n"
2700 : "create function sys.difference(x string, y string)\n"
2701 : "returns int external name txtsim.stringdiff;\n"
2702 : "grant execute on function difference(string, string) to public;\n"
2703 : "create function sys.qgramnormalize(x string)\n"
2704 : "returns string external name txtsim.qgramnormalize;\n"
2705 : "grant execute on function qgramnormalize(string) to public;\n"
2706 :
2707 : "create function asciify(x string)\n"
2708 : "returns string external name str.asciify;\n"
2709 : "grant execute on function asciify(string) to public;\n"
2710 : "create function sys.startswith(x string, y string)\n"
2711 : "returns boolean external name str.startswith;\n"
2712 : "grant execute on function startswith(string, string) to public;\n"
2713 : "create function sys.startswith(x string, y string, icase boolean)\n"
2714 : "returns boolean external name str.startswith;\n"
2715 : "grant execute on function startswith(string, string, boolean) to public;\n"
2716 : "create filter function sys.startswith(x string, y string)\n"
2717 : "external name str.startswith;\n"
2718 : "grant execute on filter function startswith(string, string) to public;\n"
2719 : "create filter function sys.startswith(x string, y string, icase boolean)\n"
2720 : "external name str.startswith;\n"
2721 : "grant execute on filter function startswith(string, string, boolean) to public;\n"
2722 : "create function sys.endswith(x string, y string)\n"
2723 : "returns boolean external name str.endswith;\n"
2724 : "grant execute on function endswith(string, string) to public;\n"
2725 : "create function sys.endswith(x string, y string, icase boolean)\n"
2726 : "returns boolean external name str.endswith;\n"
2727 : "grant execute on function endswith(string, string, boolean) to public;\n"
2728 : "create filter function sys.endswith(x string, y string)\n"
2729 : "external name str.endswith;\n"
2730 : "grant execute on filter function endswith(string, string) to public;\n"
2731 : "create filter function sys.endswith(x string, y string, icase boolean)\n"
2732 : "external name str.endswith;\n"
2733 : "grant execute on filter function endswith(string, string, boolean) to public;\n"
2734 : "create function sys.contains(x string, y string)\n"
2735 : "returns boolean external name str.contains;\n"
2736 : "grant execute on function contains(string, string) to public;\n"
2737 : "create function sys.contains(x string, y string, icase boolean)\n"
2738 : "returns boolean external name str.contains;\n"
2739 : "grant execute on function contains(string, string, boolean) to public;\n"
2740 : "create filter function sys.contains(x string, y string)\n"
2741 : "external name str.contains;\n"
2742 : "grant execute on filter function contains(string, string) to public;\n"
2743 : "create filter function sys.contains(x string, y string, icase boolean)\n"
2744 : "external name str.contains;\n"
2745 : "grant execute on filter function contains(string, string, boolean) to public;\n"
2746 :
2747 : "update sys.functions set system = true where system <> true and name in ('levenshtein', 'dameraulevenshtein', 'jarowinkler', 'editdistance', 'editdistance2', 'soundex', 'difference', 'qgramnormalize') and schema_id = 2000 and type = %d;\n"
2748 : "update sys.functions set system = true where system <> true and name in ('maxlevenshtein', 'minjarowinkler') and schema_id = 2000 and type = %d;\n"
2749 : "update sys.functions set system = true where system <> true and name in ('asciify', 'startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
2750 : "update sys.functions set system = true where system <> true and name in ('startswith', 'endswith', 'contains') and schema_id = 2000 and type = %d;\n"
2751 :
2752 : "delete from sys.triggers where name = 'system_update_tables' and table_id = 2067;\n",
2753 : F_FUNC, F_FILT, F_FUNC, F_FILT);
2754 0 : assert(pos < bufsize);
2755 0 : printf("Running database upgrade commands:\n%s\n", buf);
2756 0 : fflush(stdout);
2757 0 : err = SQLstatementIntern(c, buf, "update", true, false, NULL);
2758 : }
2759 :
2760 : /* remote credentials where moved */
2761 115 : sql_trans *tr = sql->session->tr;
2762 115 : sqlstore *store = tr->store;
2763 115 : sql_table *remote_user_info = find_sql_table(tr, s, "remote_user_info");
2764 115 : sql_column *remote_user_info_id = find_sql_column(remote_user_info, "table_id");
2765 115 : BAT *rt_key = NULL, *rt_username = NULL, *rt_pwhash = NULL, *rt_uri = NULL, *rt_deleted = NULL;
2766 115 : if (!err && store->storage_api.count_col(tr, remote_user_info_id, 0) == 0 && BBPindex("M5system_auth_rt_key")) {
2767 :
2768 0 : rt_key = BATdescriptor(BBPindex("M5system_auth_rt_key"));
2769 0 : rt_uri = BATdescriptor(BBPindex("M5system_auth_rt_uri"));
2770 0 : rt_username = BATdescriptor(BBPindex("M5system_auth_rt_remoteuser"));
2771 0 : rt_pwhash = BATdescriptor(BBPindex("M5system_auth_rt_hashedpwd"));
2772 0 : rt_deleted = BATdescriptor(BBPindex("M5system_auth_rt_deleted"));
2773 0 : if (rt_key == NULL || rt_username == NULL || rt_pwhash == NULL || rt_uri == NULL || rt_deleted == NULL) {
2774 : /* cleanup remainders and continue or full stop ? */
2775 0 : BBPreclaim(rt_key);
2776 0 : BBPreclaim(rt_uri);
2777 0 : BBPreclaim(rt_username);
2778 0 : BBPreclaim(rt_pwhash);
2779 0 : BBPreclaim(rt_deleted);
2780 0 : throw(SQL, __func__, "cannot find M5system_auth bats");
2781 : }
2782 :
2783 0 : BATiter ik = bat_iterator(rt_key);
2784 0 : BATiter iu = bat_iterator(rt_username);
2785 0 : BATiter ip = bat_iterator(rt_pwhash);
2786 0 : for (oid p = 0; p < ik.count; p++) {
2787 0 : if (BUNfnd(rt_deleted, &p) == BUN_NONE) {
2788 0 : char *key = GDKstrdup(BUNtvar(ik, p));
2789 0 : char *username = BUNtvar(iu, p);
2790 0 : char *pwhash = BUNtvar(ip, p);
2791 :
2792 0 : if (!key) {
2793 0 : bat_iterator_end(&ik);
2794 0 : bat_iterator_end(&iu);
2795 0 : bat_iterator_end(&ip);
2796 0 : BBPunfix(rt_key->batCacheid);
2797 0 : BBPunfix(rt_username->batCacheid);
2798 0 : BBPunfix(rt_pwhash->batCacheid);
2799 0 : BBPunfix(rt_deleted->batCacheid);
2800 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
2801 : }
2802 0 : char *d = strchr(key, '.');
2803 : /* . not found simply skip */
2804 0 : if (d) {
2805 0 : *d++ = '\0';
2806 0 : sql_schema *s = find_sql_schema(tr, key);
2807 0 : if (s) {
2808 0 : sql_table *t = find_sql_table(tr, s, d);
2809 0 : if (t && store->table_api.table_insert(tr, remote_user_info, &t->base.id, &username, &pwhash) != LOG_OK) {
2810 0 : bat_iterator_end(&ik);
2811 0 : bat_iterator_end(&iu);
2812 0 : bat_iterator_end(&ip);
2813 0 : BBPunfix(rt_key->batCacheid);
2814 0 : BBPunfix(rt_username->batCacheid);
2815 0 : BBPunfix(rt_pwhash->batCacheid);
2816 0 : BBPunfix(rt_deleted->batCacheid);
2817 0 : GDKfree(key);
2818 0 : throw(SQL, __func__, "Failed to insert remote credentials during upgrade");
2819 : }
2820 : }
2821 : }
2822 0 : GDKfree(key);
2823 : }
2824 : }
2825 0 : bat_iterator_end(&ik);
2826 0 : bat_iterator_end(&iu);
2827 0 : bat_iterator_end(&ip);
2828 : }
2829 115 : if (!err && rt_key) {
2830 0 : bat rtauthbats[6];
2831 :
2832 0 : rtauthbats[0] = 0;
2833 0 : rtauthbats[1] = rt_key->batCacheid;
2834 0 : rtauthbats[2] = rt_uri->batCacheid;
2835 0 : rtauthbats[3] = rt_username->batCacheid;
2836 0 : rtauthbats[4] = rt_pwhash->batCacheid;
2837 0 : rtauthbats[5] = rt_deleted->batCacheid;
2838 :
2839 0 : if (BATmode(rt_key, true) != GDK_SUCCEED ||
2840 0 : BBPrename(rt_key, NULL) != 0 ||
2841 0 : BATmode(rt_username, true) != GDK_SUCCEED ||
2842 0 : BBPrename(rt_username, NULL) != 0 ||
2843 0 : BATmode(rt_pwhash, true) != GDK_SUCCEED ||
2844 0 : BBPrename(rt_pwhash, NULL) != 0 ||
2845 0 : BATmode(rt_uri, true) != GDK_SUCCEED ||
2846 0 : BBPrename(rt_uri, NULL) != 0 ||
2847 0 : BATmode(rt_deleted, true) != GDK_SUCCEED ||
2848 0 : BBPrename(rt_deleted, NULL) != 0 ||
2849 0 : TMsubcommit_list(rtauthbats, NULL, 6, -1) != GDK_SUCCEED) {
2850 0 : fprintf(stderr, "Committing removal of old remote user/password BATs failed\n");
2851 : }
2852 0 : BBPunfix(rt_key->batCacheid);
2853 0 : BBPunfix(rt_username->batCacheid);
2854 0 : BBPunfix(rt_pwhash->batCacheid);
2855 0 : BBPunfix(rt_uri->batCacheid);
2856 0 : BBPunfix(rt_deleted->batCacheid);
2857 : }
2858 :
2859 115 : GDKfree(buf);
2860 115 : return err; /* usually MAL_SUCCEED */
2861 : }
2862 :
2863 : static str
2864 115 : sql_update_jun2023_sp3(Client c, mvc *sql, sql_schema *s)
2865 : {
2866 115 : (void)s;
2867 115 : char *err = NULL;
2868 115 : sql_subtype t1, t2;
2869 :
2870 115 : sql_find_subtype(&t1, "timestamp", 0, 0);
2871 115 : sql_find_subtype(&t2, "varchar", 0, 0);
2872 :
2873 115 : if (!sql_bind_func(sql, "sys", "timestamp_to_str", &t1, &t2, F_FUNC, true, true)) {
2874 0 : sql->session->status = 0;
2875 0 : sql->errstr[0] = '\0';
2876 :
2877 0 : char *query = GDKmalloc(512);
2878 0 : if (query == NULL)
2879 0 : throw(SQL, __func__, SQLSTATE(HY013) MAL_MALLOC_FAIL);
2880 :
2881 0 : snprintf(query, 512, "CREATE FUNCTION timestamp_to_str(d TIMESTAMP, format STRING) RETURNS STRING "
2882 : "EXTERNAL NAME mtime.\"timestamp_to_str\";\n"
2883 : "GRANT EXECUTE ON FUNCTION timestamp_to_str(TIMESTAMP, STRING) TO PUBLIC;\n"
2884 : "UPDATE sys.functions SET system = true WHERE system <> true AND name = 'timestamp_to_str' "
2885 : "AND schema_id = 2000 and type = %d;\n", F_FUNC);
2886 :
2887 0 : printf("Running database upgrade commands:\n%s\n", query);
2888 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2889 0 : GDKfree(query);
2890 : }
2891 :
2892 : return err; /* usually MAL_SUCCEED */
2893 : }
2894 :
2895 : static str
2896 115 : sql_update_dec2023_geom(Client c, mvc *sql, sql_schema *s)
2897 : {
2898 115 : sql_subtype tp;
2899 115 : char *err = NULL;
2900 :
2901 : /* the shp module was changed: drop the old stuff if it exists, only
2902 : * add the new stuff if the appropriate module is available */
2903 115 : sql_find_subtype(&tp, "varchar", 0, 0);
2904 : /* Drop old SHP procedures */
2905 115 : if (sql_bind_func(sql, s->base.name, "shpattach", &tp, NULL, F_PROC, true, true)) {
2906 0 : if ((err = sql_drop_shp(c)) != NULL)
2907 : return err;
2908 : } else {
2909 115 : sql->session->status = 0; /* if the shpattach function was not found clean the error */
2910 115 : sql->errstr[0] = '\0';
2911 : }
2912 : #ifdef HAVE_GEOM
2913 115 : if (backend_has_module(&(int){0}, "geom")) {
2914 : #ifdef HAVE_SHP
2915 115 : if (backend_has_module(&(int){0}, "shp")) {
2916 : /* if shpload with two varchar args does not exist, add the
2917 : * procedures */
2918 115 : if (!sql_bind_func(sql, s->base.name, "shpload", &tp, &tp, F_PROC, true, true)) {
2919 0 : sql->session->status = 0;
2920 0 : sql->errstr[0] = '\0';
2921 0 : if ((err = sql_create_shp(c)) != NULL)
2922 0 : return err;
2923 : }
2924 : }
2925 : #endif
2926 115 : sql_find_subtype(&tp, "geometry", 0, 0);
2927 115 : if (!sql_bind_func(sql, s->base.name, "st_intersects_noindex", &tp, &tp, F_FILT, true, true)) {
2928 0 : sql->session->status = 0;
2929 0 : sql->errstr[0] = '\0';
2930 0 : sql_table *t;
2931 0 : if ((t = mvc_bind_table(sql, s, "geometry_columns")) != NULL)
2932 0 : t->system = 0;
2933 0 : const char query[] =
2934 : "drop function if exists sys.st_intersects(geometry, geometry) cascade;\n"
2935 : "drop function if exists sys.st_dwithin(geometry, geometry, double) cascade;\n"
2936 : "drop view if exists sys.geometry_columns cascade;\n"
2937 : "drop function if exists sys.st_collect(geometry, geometry) cascade;\n"
2938 : "drop aggregate if exists sys.st_collect(geometry) cascade;\n"
2939 : "drop aggregate if exists sys.st_makeline(geometry) cascade;\n"
2940 : "create view sys.geometry_columns as\n"
2941 : " select cast(null as varchar(1)) as f_table_catalog,\n"
2942 : " s.name as f_table_schema,\n"
2943 : " t.name as f_table_name,\n"
2944 : " c.name as f_geometry_column,\n"
2945 : " cast(has_z(c.type_digits) + has_m(c.type_digits) +2 as integer) as coord_dimension,\n"
2946 : " c.type_scale as srid,\n"
2947 : " get_type(c.type_digits, 0) as geometry_type\n"
2948 : " from sys.columns c, sys.tables t, sys.schemas s\n"
2949 : " where c.table_id = t.id and t.schema_id = s.id\n"
2950 : " and c.type in (select sqlname from sys.types where systemname in ('wkb', 'wkba'));\n"
2951 : "GRANT SELECT ON sys.geometry_columns TO PUBLIC;\n"
2952 : "CREATE FUNCTION ST_Collect(geom1 Geometry, geom2 Geometry) RETURNS Geometry EXTERNAL NAME geom.\"Collect\";\n"
2953 : "GRANT EXECUTE ON FUNCTION ST_Collect(Geometry, Geometry) TO PUBLIC;\n"
2954 : "CREATE AGGREGATE ST_Collect(geom Geometry) RETURNS Geometry external name aggr.\"Collect\";\n"
2955 : "GRANT EXECUTE ON AGGREGATE ST_Collect(Geometry) TO PUBLIC;\n"
2956 : "CREATE FUNCTION ST_DistanceGeographic(geom1 Geometry, geom2 Geometry) RETURNS double EXTERNAL NAME geom.\"DistanceGeographic\";\n"
2957 : "GRANT EXECUTE ON FUNCTION ST_DistanceGeographic(Geometry, Geometry) TO PUBLIC;\n"
2958 : "CREATE FILTER FUNCTION ST_DWithinGeographic(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithinGeographic\";\n"
2959 : "GRANT EXECUTE ON FILTER ST_DWithinGeographic(Geometry, Geometry, double) TO PUBLIC;\n"
2960 : "CREATE FILTER FUNCTION ST_DWithin(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME rtree.\"DWithin\";\n"
2961 : "GRANT EXECUTE ON FILTER ST_DWithin(Geometry, Geometry, double) TO PUBLIC;\n"
2962 : "CREATE FILTER FUNCTION ST_DWithin_NoIndex(geom1 Geometry, geom2 Geometry, distance double) EXTERNAL NAME geom.\"DWithin_noindex\";\n"
2963 : "GRANT EXECUTE ON FILTER ST_DWithin_NoIndex(Geometry, Geometry, double) TO PUBLIC;\n"
2964 : "CREATE FUNCTION ST_DWithin2(geom1 Geometry, geom2 Geometry, bbox1 mbr, bbox2 mbr, dst double) RETURNS boolean EXTERNAL NAME geom.\"DWithin2\";\n"
2965 : "GRANT EXECUTE ON FUNCTION ST_DWithin2(Geometry, Geometry, mbr, mbr, double) TO PUBLIC;\n"
2966 : "CREATE FILTER FUNCTION ST_IntersectsGeographic(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"IntersectsGeographic\";\n"
2967 : "GRANT EXECUTE ON FILTER ST_IntersectsGeographic(Geometry, Geometry) TO PUBLIC;\n"
2968 : "CREATE FILTER FUNCTION ST_Intersects(geom1 Geometry, geom2 Geometry) EXTERNAL NAME rtree.\"Intersects\";\n"
2969 : "GRANT EXECUTE ON FILTER ST_Intersects(Geometry, Geometry) TO PUBLIC;\n"
2970 : "CREATE FILTER FUNCTION ST_Intersects_NoIndex(geom1 Geometry, geom2 Geometry) EXTERNAL NAME geom.\"Intersects_noindex\";\n"
2971 : "GRANT EXECUTE ON FILTER ST_Intersects_NoIndex(Geometry, Geometry) TO PUBLIC;\n"
2972 : "CREATE AGGREGATE ST_MakeLine(geom Geometry) RETURNS Geometry external name aggr.\"MakeLine\";\n"
2973 : "GRANT EXECUTE ON AGGREGATE ST_MakeLine(Geometry) TO PUBLIC;\n"
2974 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('st_collect', 'st_distancegeographic', 'st_dwithingeographic', 'st_dwithin', 'st_dwithin_noindex', 'st_dwithin2', 'st_intersectsgeographic', 'st_intersects', 'st_intersects_noindex', 'st_makeline');\n"
2975 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'geometry_columns';\n";
2976 0 : printf("Running database upgrade commands:\n%s\n", query);
2977 0 : fflush(stdout);
2978 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2979 : }
2980 : }
2981 : #endif
2982 115 : return err;
2983 : }
2984 :
2985 : static str
2986 115 : sql_update_dec2023(Client c, mvc *sql, sql_schema *s)
2987 : {
2988 115 : sql_subtype tp;
2989 115 : sql_schema *info;
2990 115 : char *err = NULL;
2991 115 : res_table *output = NULL;
2992 :
2993 115 : sql_find_subtype(&tp, "varchar", 0, 0);
2994 115 : if (sql_bind_func(sql, s->base.name, "similarity", &tp, &tp, F_FUNC, true, true)) {
2995 0 : const char query[] = "drop function sys.similarity(string, string) cascade;\n";
2996 0 : printf("Running database upgrade commands:\n%s\n", query);
2997 0 : fflush(stdout);
2998 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
2999 : } else {
3000 115 : sql->session->status = 0; /* if the function was not found clean the error */
3001 115 : sql->errstr[0] = '\0';
3002 : }
3003 :
3004 115 : if (mvc_bind_table(sql, s, "describe_accessible_tables") == NULL) {
3005 0 : sql->session->status = 0; /* if the view was not found clean the error */
3006 0 : sql->errstr[0] = '\0';
3007 0 : const char query[] =
3008 : "CREATE VIEW sys.describe_accessible_tables AS\n"
3009 : " SELECT\n"
3010 : " schemas.name AS schema,\n"
3011 : " tables.name AS table,\n"
3012 : " tt.table_type_name AS table_type,\n"
3013 : " pc.privilege_code_name AS privs,\n"
3014 : " p.privileges AS privs_code\n"
3015 : " FROM privileges p\n"
3016 : " JOIN sys.roles ON p.auth_id = roles.id\n"
3017 : " JOIN sys.tables ON p.obj_id = tables.id\n"
3018 : " JOIN sys.table_types tt ON tables.type = tt.table_type_id\n"
3019 : " JOIN sys.schemas ON tables.schema_id = schemas.id\n"
3020 : " JOIN sys.privilege_codes pc ON p.privileges = pc.privilege_code_id\n"
3021 : " WHERE roles.name = current_role;\n"
3022 : "GRANT SELECT ON sys.describe_accessible_tables TO PUBLIC;\n"
3023 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'describe_accessible_tables';\n"
3024 :
3025 : /* PYTHON_MAP and PYTHON3_MAP have been removed */
3026 : "alter table sys.function_languages set read write;\n"
3027 : "delete from sys.function_languages where language_keyword like 'PYTHON%_MAP';\n"
3028 : /* for these two, also see load_func() */
3029 : "update sys.functions set language = language - 1 where language in (7, 11);\n"
3030 : "update sys.functions set mod = 'pyapi3' where mod in ('pyapi', 'pyapi3map');\n"
3031 : "commit;\n";
3032 0 : printf("Running database upgrade commands:\n%s\n", query);
3033 0 : fflush(stdout);
3034 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
3035 0 : if (err == MAL_SUCCEED) {
3036 0 : const char query2[] = "alter table sys.function_languages set read only;\n";
3037 0 : printf("Running database upgrade commands:\n%s\n", query2);
3038 0 : fflush(stdout);
3039 0 : err = SQLstatementIntern(c, query2, "update", true, false, NULL);
3040 : }
3041 : }
3042 :
3043 : /* 52_describe.sql changes to update sys.describe_comments view */
3044 115 : if ((err = SQLstatementIntern(c, "select id from sys.tables where name = 'describe_comments' and schema_id = 2000 and query like '% not t.system%';", "update", true, false, &output)) == NULL) {
3045 115 : BAT *b;
3046 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
3047 0 : sql_table *t;
3048 : /* set views internally to non-system to allow drop commands to succeed without error */
3049 0 : if ((t = mvc_bind_table(sql, s, "describe_comments")) != NULL)
3050 0 : t->system = 0;
3051 0 : if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
3052 0 : t->system = 0;
3053 :
3054 0 : const char cmds[] =
3055 : "DROP FUNCTION IF EXISTS sys.dump_database(BOOLEAN) CASCADE;\n"
3056 : "DROP VIEW IF EXISTS sys.dump_comments CASCADE;\n"
3057 : "DROP VIEW IF EXISTS sys.describe_comments CASCADE;\n"
3058 : "CREATE VIEW sys.describe_comments AS\n"
3059 : " SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
3060 : " FROM (\n"
3061 : " SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
3062 : " UNION ALL\n"
3063 : " SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
3064 : " FROM sys.schemas s JOIN sys._tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
3065 : " WHERE NOT t.system\n"
3066 : " UNION ALL\n"
3067 : " SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys._tables t, sys.schemas s WHERE NOT t.system AND c.table_id = t.id AND t.schema_id = s.id\n"
3068 : " UNION ALL\n"
3069 : " SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE NOT t.system AND idx.table_id = t.id AND t.schema_id = s.id\n"
3070 : " UNION ALL\n"
3071 : " SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
3072 : " UNION ALL\n"
3073 : " SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf\n"
3074 : " WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
3075 : " ) AS o(id, tpe, nme)\n"
3076 : " JOIN sys.comments cm ON cm.id = o.id;\n"
3077 : "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
3078 : "CREATE VIEW sys.dump_comments AS\n"
3079 : " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
3080 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
3081 : "BEGIN\n"
3082 : " SET SCHEMA sys;\n"
3083 : " TRUNCATE sys.dump_statements;\n"
3084 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
3085 : " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
3086 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
3087 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
3088 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
3089 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
3090 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
3091 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
3092 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
3093 : " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
3094 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
3095 : " FROM (\n"
3096 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
3097 : " UNION ALL\n"
3098 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
3099 : " ) AS stmts(o, s);\n"
3100 : " IF NOT DESCRIBE THEN\n"
3101 : " CALL sys.dump_table_data();\n"
3102 : " END IF;\n"
3103 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
3104 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
3105 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
3106 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
3107 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
3108 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
3109 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
3110 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
3111 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
3112 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
3113 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
3114 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
3115 : " RETURN sys.dump_statements;\n"
3116 : "END;\n"
3117 : "update sys._tables set system = true where schema_id = 2000 and name in ('describe_comments','dump_comments');\n"
3118 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'dump_database' and type = 5;\n";
3119 :
3120 0 : printf("Running database upgrade commands:\n%s\n", cmds);
3121 0 : fflush(stdout);
3122 0 : err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
3123 : }
3124 115 : res_table_destroy(output);
3125 115 : output = NULL;
3126 : }
3127 :
3128 : /* 52_describe.sql New function sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean) */
3129 115 : allocator *old_sa = sql->sa;
3130 115 : if ((sql->sa = sa_create(sql->pa)) != NULL) {
3131 115 : list *l;
3132 115 : if ((l = sa_list(sql->sa)) != NULL) {
3133 115 : sql_subtype t1, t2;
3134 115 : sql_find_subtype(&t1, "int", 0, 0);
3135 115 : sql_find_subtype(&t2, "boolean", 0, 0);
3136 115 : list_append(l, &tp);
3137 115 : list_append(l, &t1);
3138 115 : list_append(l, &t1);
3139 115 : list_append(l, &t2);
3140 115 : list_append(l, &t2);
3141 115 : if (!sql_bind_func_(sql, s->base.name, "sql_datatype", l, F_FUNC, true, true)) {
3142 0 : const char cmds[] =
3143 : "CREATE FUNCTION sys.sql_datatype(mtype varchar(999), digits integer, tscale integer, nameonly boolean, shortname boolean)\n"
3144 : " RETURNS varchar(1024)\n"
3145 : "BEGIN\n"
3146 : " RETURN\n"
3147 : " CASE mtype\n"
3148 : " WHEN 'char' THEN sys.ifthenelse(nameonly OR digits <= 1, sys.ifthenelse(shortname, 'CHAR', 'CHARACTER'), sys.ifthenelse(shortname, 'CHAR(', 'CHARACTER(') || digits || ')')\n"
3149 : " WHEN 'varchar' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'VARCHAR', 'CHARACTER VARYING'), sys.ifthenelse(shortname, 'VARCHAR(', 'CHARACTER VARYING(') || digits || ')')\n"
3150 : " WHEN 'clob' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'CLOB', 'CHARACTER LARGE OBJECT'), sys.ifthenelse(shortname, 'CLOB(', 'CHARACTER LARGE OBJECT(') || digits || ')')\n"
3151 : " WHEN 'blob' THEN sys.ifthenelse(nameonly OR digits = 0, sys.ifthenelse(shortname, 'BLOB', 'BINARY LARGE OBJECT'), sys.ifthenelse(shortname, 'BLOB(', 'BINARY LARGE OBJECT(') || digits || ')')\n"
3152 : " WHEN 'int' THEN 'INTEGER'\n"
3153 : " WHEN 'bigint' THEN 'BIGINT'\n"
3154 : " WHEN 'smallint' THEN 'SMALLINT'\n"
3155 : " WHEN 'tinyint' THEN 'TINYINT'\n"
3156 : " WHEN 'hugeint' THEN 'HUGEINT'\n"
3157 : " WHEN 'boolean' THEN 'BOOLEAN'\n"
3158 : " WHEN 'date' THEN 'DATE'\n"
3159 : " WHEN 'time' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME', 'TIME(' || (digits -1) || ')')\n"
3160 : " WHEN 'timestamp' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP', 'TIMESTAMP(' || (digits -1) || ')')\n"
3161 : " WHEN 'timestamptz' THEN sys.ifthenelse(nameonly OR digits = 7, 'TIMESTAMP WITH TIME ZONE', 'TIMESTAMP(' || (digits -1) || ') WITH TIME ZONE')\n"
3162 : " WHEN 'timetz' THEN sys.ifthenelse(nameonly OR digits = 1, 'TIME WITH TIME ZONE', 'TIME(' || (digits -1) || ') WITH TIME ZONE')\n"
3163 : " WHEN 'decimal' THEN sys.ifthenelse(nameonly OR digits = 0, 'DECIMAL', 'DECIMAL(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
3164 : " WHEN 'double' THEN sys.ifthenelse(nameonly OR (digits = 53 AND tscale = 0), sys.ifthenelse(shortname, 'DOUBLE', 'DOUBLE PRECISION'), 'FLOAT(' || digits || ')')\n"
3165 : " WHEN 'real' THEN sys.ifthenelse(nameonly OR (digits = 24 AND tscale = 0), 'REAL', 'FLOAT(' || digits || ')')\n"
3166 : " WHEN 'day_interval' THEN 'INTERVAL DAY'\n"
3167 : " WHEN 'month_interval' THEN CASE digits WHEN 1 THEN 'INTERVAL YEAR' WHEN 2 THEN 'INTERVAL YEAR TO MONTH' WHEN 3 THEN 'INTERVAL MONTH' END\n"
3168 : " WHEN 'sec_interval' THEN\n"
3169 : " CASE digits\n"
3170 : " WHEN 4 THEN 'INTERVAL DAY'\n"
3171 : " WHEN 5 THEN 'INTERVAL DAY TO HOUR'\n"
3172 : " WHEN 6 THEN 'INTERVAL DAY TO MINUTE'\n"
3173 : " WHEN 7 THEN 'INTERVAL DAY TO SECOND'\n"
3174 : " WHEN 8 THEN 'INTERVAL HOUR'\n"
3175 : " WHEN 9 THEN 'INTERVAL HOUR TO MINUTE'\n"
3176 : " WHEN 10 THEN 'INTERVAL HOUR TO SECOND'\n"
3177 : " WHEN 11 THEN 'INTERVAL MINUTE'\n"
3178 : " WHEN 12 THEN 'INTERVAL MINUTE TO SECOND'\n"
3179 : " WHEN 13 THEN 'INTERVAL SECOND'\n"
3180 : " END\n"
3181 : " WHEN 'oid' THEN 'OID'\n"
3182 : " WHEN 'json' THEN sys.ifthenelse(nameonly OR digits = 0, 'JSON', 'JSON(' || digits || ')')\n"
3183 : " WHEN 'url' THEN sys.ifthenelse(nameonly OR digits = 0, 'URL', 'URL(' || digits || ')')\n"
3184 : " WHEN 'xml' THEN sys.ifthenelse(nameonly OR digits = 0, 'XML', 'XML(' || digits || ')')\n"
3185 : " WHEN 'geometry' THEN\n"
3186 : " sys.ifthenelse(nameonly, 'GEOMETRY',\n"
3187 : " CASE digits\n"
3188 : " WHEN 4 THEN 'GEOMETRY(POINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3189 : " WHEN 8 THEN 'GEOMETRY(LINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3190 : " WHEN 16 THEN 'GEOMETRY(POLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3191 : " WHEN 20 THEN 'GEOMETRY(MULTIPOINT' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3192 : " WHEN 24 THEN 'GEOMETRY(MULTILINESTRING' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3193 : " WHEN 28 THEN 'GEOMETRY(MULTIPOLYGON' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3194 : " WHEN 32 THEN 'GEOMETRY(GEOMETRYCOLLECTION' || sys.ifthenelse(tscale = 0, ')', ',' || tscale || ')')\n"
3195 : " ELSE 'GEOMETRY'\n"
3196 : " END)\n"
3197 : " ELSE sys.ifthenelse(mtype = lower(mtype), upper(mtype), '\"' || mtype || '\"') || sys.ifthenelse(nameonly OR digits = 0, '', '(' || digits || sys.ifthenelse(tscale = 0, '', ',' || tscale) || ')')\n"
3198 : " END;\n"
3199 : "END;\n"
3200 : "GRANT EXECUTE ON FUNCTION sys.sql_datatype(varchar(999), integer, integer, boolean, boolean) TO PUBLIC;\n"
3201 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name = 'sql_datatype' and type = 1 and language = 2;\n";
3202 :
3203 0 : sql->session->status = 0;
3204 0 : sql->errstr[0] = '\0';
3205 0 : printf("Running database upgrade commands:\n%s\n", cmds);
3206 0 : fflush(stdout);
3207 0 : err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
3208 : }
3209 : }
3210 115 : sa_destroy(sql->sa);
3211 : }
3212 115 : sql->sa = old_sa;
3213 :
3214 :
3215 : /* 91_information_schema.sql */
3216 115 : info = mvc_bind_schema(sql, "information_schema");
3217 115 : if (info == NULL) {
3218 0 : sql->session->status = 0; /* if the schema was not found clean the error */
3219 0 : sql->errstr[0] = '\0';
3220 0 : const char cmds[] =
3221 : "CREATE SCHEMA INFORMATION_SCHEMA;\n"
3222 : "COMMENT ON SCHEMA INFORMATION_SCHEMA IS 'ISO/IEC 9075-11 SQL/Schemata';\n"
3223 : "update sys.schemas set system = true where name = 'information_schema';\n"
3224 :
3225 : "CREATE VIEW INFORMATION_SCHEMA.CHARACTER_SETS AS SELECT\n"
3226 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
3227 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
3228 : " cast('UTF-8' AS varchar(16)) AS CHARACTER_SET_NAME,\n"
3229 : " cast('ISO/IEC 10646:2021' AS varchar(20)) AS CHARACTER_REPERTOIRE,\n"
3230 : " cast('UTF-8' AS varchar(16)) AS FORM_OF_USE,\n"
3231 : " cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_CATALOG,\n"
3232 : " cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_SCHEMA,\n"
3233 : " cast(NULL AS varchar(1)) AS DEFAULT_COLLATE_NAME;\n"
3234 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHARACTER_SETS TO PUBLIC WITH GRANT OPTION;\n"
3235 :
3236 : "CREATE VIEW INFORMATION_SCHEMA.SCHEMATA AS SELECT\n"
3237 : " cast(NULL AS varchar(1)) AS CATALOG_NAME,\n"
3238 : " s.\"name\" AS SCHEMA_NAME,\n"
3239 : " a.\"name\" AS SCHEMA_OWNER,\n"
3240 : " cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_CATALOG,\n"
3241 : " cast(NULL AS varchar(1)) AS DEFAULT_CHARACTER_SET_SCHEMA,\n"
3242 : " cast('UTF-8' AS varchar(16)) AS DEFAULT_CHARACTER_SET_NAME,\n"
3243 : " cast(NULL AS varchar(1)) AS SQL_PATH,\n"
3244 : " s.\"id\" AS schema_id,\n"
3245 : " s.\"system\" AS is_system,\n"
3246 : " cm.\"remark\" AS comments\n"
3247 : " FROM sys.\"schemas\" s\n"
3248 : " INNER JOIN sys.\"auths\" a ON s.\"owner\" = a.\"id\"\n"
3249 : " LEFT OUTER JOIN sys.\"comments\" cm ON s.\"id\" = cm.\"id\"\n"
3250 : " ORDER BY s.\"name\";\n"
3251 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SCHEMATA TO PUBLIC WITH GRANT OPTION;\n"
3252 :
3253 : "CREATE VIEW INFORMATION_SCHEMA.TABLES AS SELECT\n"
3254 : " cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
3255 : " s.\"name\" AS TABLE_SCHEMA,\n"
3256 : " t.\"name\" AS TABLE_NAME,\n"
3257 : " tt.\"table_type_name\" AS TABLE_TYPE,\n"
3258 : " cast(NULL AS varchar(1)) AS SELF_REFERENCING_COLUMN_NAME,\n"
3259 : " cast(NULL AS varchar(1)) AS REFERENCE_GENERATION,\n"
3260 : " cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_CATALOG,\n"
3261 : " cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_SCHEMA,\n"
3262 : " cast(NULL AS varchar(1)) AS USER_DEFINED_TYPE_NAME,\n"
3263 : " cast(sys.ifthenelse((t.\"type\" IN (0, 3, 7, 20, 30) AND t.\"access\" IN (0, 2)), 'YES', 'NO') AS varchar(3)) AS IS_INSERTABLE_INTO,\n"
3264 : " cast('NO' AS varchar(3)) AS IS_TYPED,\n"
3265 : " cast((CASE t.\"commit_action\" WHEN 1 THEN 'DELETE' WHEN 2 THEN 'PRESERVE' WHEN 3 THEN 'DROP' ELSE NULL END) AS varchar(10)) AS COMMIT_ACTION,\n"
3266 : " t.\"schema_id\" AS schema_id,\n"
3267 : " t.\"id\" AS table_id,\n"
3268 : " t.\"type\" AS table_type_id,\n"
3269 : " st.\"count\" AS row_count,\n"
3270 : " t.\"system\" AS is_system,\n"
3271 : " sys.ifthenelse(t.\"type\" IN (1, 11), TRUE, FALSE) AS is_view,\n"
3272 : " t.\"query\" AS query_def,\n"
3273 : " cm.\"remark\" AS comments\n"
3274 : " FROM sys.\"tables\" t\n"
3275 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
3276 : " INNER JOIN sys.\"table_types\" tt ON t.\"type\" = tt.\"table_type_id\"\n"
3277 : " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
3278 : " LEFT OUTER JOIN (SELECT DISTINCT \"schema\", \"table\", \"count\" FROM sys.\"statistics\"()) st ON (s.\"name\" = st.\"schema\" AND t.\"name\" = st.\"table\")\n"
3279 : " ORDER BY s.\"name\", t.\"name\";\n"
3280 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLES TO PUBLIC WITH GRANT OPTION;\n"
3281 :
3282 : "CREATE VIEW INFORMATION_SCHEMA.VIEWS AS SELECT\n"
3283 : " cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
3284 : " s.\"name\" AS TABLE_SCHEMA,\n"
3285 : " t.\"name\" AS TABLE_NAME,\n"
3286 : " t.\"query\" AS VIEW_DEFINITION,\n"
3287 : " cast('NONE' AS varchar(10)) AS CHECK_OPTION,\n"
3288 : " cast('NO' AS varchar(3)) AS IS_UPDATABLE,\n"
3289 : " cast('NO' AS varchar(3)) AS INSERTABLE_INTO,\n"
3290 : " cast('NO' AS varchar(3)) AS IS_TRIGGER_UPDATABLE,\n"
3291 : " cast('NO' AS varchar(3)) AS IS_TRIGGER_DELETABLE,\n"
3292 : " cast('NO' AS varchar(3)) AS IS_TRIGGER_INSERTABLE_INTO,\n"
3293 : " t.\"schema_id\" AS schema_id,\n"
3294 : " t.\"id\" AS table_id,\n"
3295 : " cast(sys.ifthenelse(t.\"system\", t.\"type\" + 10 , t.\"type\") AS smallint) AS table_type_id,\n"
3296 : " t.\"system\" AS is_system,\n"
3297 : " cm.\"remark\" AS comments\n"
3298 : " FROM sys.\"_tables\" t\n"
3299 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
3300 : " LEFT OUTER JOIN sys.\"comments\" cm ON t.\"id\" = cm.\"id\"\n"
3301 : " WHERE t.\"type\" = 1\n"
3302 : " ORDER BY s.\"name\", t.\"name\";\n"
3303 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.VIEWS TO PUBLIC WITH GRANT OPTION;\n"
3304 :
3305 : "CREATE VIEW INFORMATION_SCHEMA.COLUMNS AS SELECT\n"
3306 : " cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
3307 : " s.\"name\" AS TABLE_SCHEMA,\n"
3308 : " t.\"name\" AS TABLE_NAME,\n"
3309 : " c.\"name\" AS COLUMN_NAME,\n"
3310 : " cast(1 + c.\"number\" AS int) AS ORDINAL_POSITION,\n"
3311 : " c.\"default\" AS COLUMN_DEFAULT,\n"
3312 : " cast(sys.ifthenelse(c.\"null\", 'YES', 'NO') AS varchar(3)) AS IS_NULLABLE,\n"
3313 : " cast(sys.\"sql_datatype\"(c.\"type\", c.\"type_digits\", c.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
3314 : " cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml') AND c.\"type_digits\" > 0, c.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
3315 : " cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml') AND c.\"type_digits\" > 0, 4 * cast(c.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
3316 : " cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), c.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
3317 : " cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(c.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
3318 : " cast(sys.ifthenelse(c.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), c.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
3319 : " cast(sys.ifthenelse(c.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), sys.ifthenelse(c.\"type_scale\" > 0, c.\"type_scale\" -1, 0), NULL) AS int) AS DATETIME_PRECISION,\n"
3320 : " cast(sys.ifthenelse(c.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(c.\"type\", c.\"type_digits\", c.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
3321 : " cast(CASE c.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(c.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(c.\"type_scale\" > 0, c.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
3322 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
3323 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
3324 : " cast(sys.ifthenelse(c.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
3325 : " cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
3326 : " cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
3327 : " cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
3328 : " cast(NULL AS varchar(1)) AS DOMAIN_CATALOG,\n"
3329 : " cast(NULL AS varchar(1)) AS DOMAIN_SCHEMA,\n"
3330 : " cast(NULL AS varchar(1)) AS DOMAIN_NAME,\n"
3331 : " cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
3332 : " cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
3333 : " cast(NULL AS varchar(1)) AS UDT_NAME,\n"
3334 : " cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
3335 : " cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
3336 : " cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
3337 : " cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
3338 : " cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
3339 : " cast('NO' AS varchar(3)) AS IS_SELF_REFERENCING,\n"
3340 : " cast(sys.ifthenelse(seq.\"name\" IS NULL OR c.\"null\", 'NO', 'YES') AS varchar(3)) AS IS_IDENTITY,\n"
3341 : " seq.\"name\" AS IDENTITY_GENERATION,\n"
3342 : " seq.\"start\" AS IDENTITY_START,\n"
3343 : " seq.\"increment\" AS IDENTITY_INCREMENT,\n"
3344 : " seq.\"maxvalue\" AS IDENTITY_MAXIMUM,\n"
3345 : " seq.\"minvalue\" AS IDENTITY_MINIMUM,\n"
3346 : " cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, sys.ifthenelse(seq.\"cycle\", 'YES', 'NO')) AS varchar(3)) AS IDENTITY_CYCLE,\n"
3347 : " cast(sys.ifthenelse(seq.\"name\" IS NULL, 'NO', 'YES') AS varchar(3)) AS IS_GENERATED,\n"
3348 : " cast(sys.ifthenelse(seq.\"name\" IS NULL, NULL, c.\"default\") AS varchar(1024)) AS GENERATION_EXPRESSION,\n"
3349 : " cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_START,\n"
3350 : " cast('NO' AS varchar(3)) AS IS_SYSTEM_TIME_PERIOD_END,\n"
3351 : " cast('NO' AS varchar(3)) AS SYSTEM_TIME_PERIOD_TIMESTAMP_GENERATION,\n"
3352 : " cast(sys.ifthenelse(t.\"type\" IN (0,3,7,20,30), 'YES', 'NO') AS varchar(3)) AS IS_UPDATABLE,\n"
3353 : " cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
3354 : " cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
3355 : " cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
3356 : " t.\"schema_id\" AS schema_id,\n"
3357 : " c.\"table_id\" AS table_id,\n"
3358 : " c.\"id\" AS column_id,\n"
3359 : " seq.\"id\" AS sequence_id,\n"
3360 : " t.\"system\" AS is_system,\n"
3361 : " cm.\"remark\" AS comments\n"
3362 : " FROM sys.\"columns\" c\n"
3363 : " INNER JOIN sys.\"tables\" t ON c.\"table_id\" = t.\"id\"\n"
3364 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
3365 : " LEFT OUTER JOIN sys.\"comments\" cm ON c.\"id\" = cm.\"id\"\n"
3366 : " LEFT OUTER JOIN sys.\"sequences\" seq ON ((seq.\"name\"||'\"') = substring(c.\"default\", 3 + sys.\"locate\"('\".\"seq_',c.\"default\",14)))\n"
3367 : " ORDER BY s.\"name\", t.\"name\", c.\"number\";\n"
3368 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.COLUMNS TO PUBLIC WITH GRANT OPTION;\n"
3369 :
3370 : "CREATE VIEW INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS SELECT\n"
3371 : " cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
3372 : " cast(NULL AS varchar(1024)) AS CONSTRAINT_SCHEMA,\n"
3373 : " cast(NULL AS varchar(1024)) AS CONSTRAINT_NAME,\n"
3374 : " cast(NULL AS varchar(1024)) AS CHECK_CLAUSE\n"
3375 : " WHERE 1=0;\n"
3376 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHECK_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
3377 :
3378 : "CREATE VIEW INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS SELECT\n"
3379 : " cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
3380 : " s.\"name\" AS CONSTRAINT_SCHEMA,\n"
3381 : " k.\"name\" AS CONSTRAINT_NAME,\n"
3382 : " cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
3383 : " s.\"name\" AS TABLE_SCHEMA,\n"
3384 : " t.\"name\" AS TABLE_NAME,\n"
3385 : " cast(CASE k.\"type\" WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' WHEN 2 THEN 'FOREIGN KEY' ELSE NULL END AS varchar(16)) AS CONSTRAINT_TYPE,\n"
3386 : " cast('NO' AS varchar(3)) AS IS_DEFERRABLE,\n"
3387 : " cast('NO' AS varchar(3)) AS INITIALLY_DEFERRED,\n"
3388 : " cast('YES' AS varchar(3)) AS ENFORCED,\n"
3389 : " t.\"schema_id\" AS schema_id,\n"
3390 : " t.\"id\" AS table_id,\n"
3391 : " k.\"id\" AS key_id,\n"
3392 : " k.\"type\" AS key_type,\n"
3393 : " t.\"system\" AS is_system\n"
3394 : " FROM (SELECT sk.\"id\", sk.\"table_id\", sk.\"name\", sk.\"type\" FROM sys.\"keys\" sk UNION ALL SELECT tk.\"id\", tk.\"table_id\", tk.\"name\", tk.\"type\" FROM tmp.\"keys\" tk) k\n"
3395 : " INNER JOIN (SELECT st.\"id\", st.\"schema_id\", st.\"name\", st.\"system\" FROM sys.\"_tables\" st UNION ALL"
3396 : " SELECT tt.\"id\", tt.\"schema_id\", tt.\"name\", tt.\"system\" FROM tmp.\"_tables\" tt) t ON k.\"table_id\" = t.\"id\"\n"
3397 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
3398 : " ORDER BY s.\"name\", t.\"name\", k.\"name\";\n"
3399 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLE_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
3400 :
3401 : "CREATE VIEW INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS SELECT\n"
3402 : " cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
3403 : " s.\"name\" AS CONSTRAINT_SCHEMA,\n"
3404 : " fk.\"name\" AS CONSTRAINT_NAME,\n"
3405 : " cast(NULL AS varchar(1)) AS UNIQUE_CONSTRAINT_CATALOG,\n"
3406 : " uks.\"name\" AS UNIQUE_CONSTRAINT_SCHEMA,\n"
3407 : " uk.\"name\" AS UNIQUE_CONSTRAINT_NAME,\n"
3408 : " cast('FULL' AS varchar(7)) AS MATCH_OPTION,\n"
3409 : " fk.\"update_action\" AS UPDATE_RULE,\n"
3410 : " fk.\"delete_action\" AS DELETE_RULE,\n"
3411 : " t.\"schema_id\" AS fk_schema_id,\n"
3412 : " t.\"id\" AS fk_table_id,\n"
3413 : " t.\"name\" AS fk_table_name,\n"
3414 : " fk.\"id\" AS fk_key_id,\n"
3415 : " ukt.\"schema_id\" AS uc_schema_id,\n"
3416 : " uk.\"table_id\" AS uc_table_id,\n"
3417 : " ukt.\"name\" AS uc_table_name,\n"
3418 : " uk.\"id\" AS uc_key_id\n"
3419 : " FROM sys.\"fkeys\" fk\n"
3420 : " INNER JOIN sys.\"tables\" t ON t.\"id\" = fk.\"table_id\"\n"
3421 : " INNER JOIN sys.\"schemas\" s ON s.\"id\" = t.\"schema_id\"\n"
3422 : " LEFT OUTER JOIN sys.\"keys\" uk ON uk.\"id\" = fk.\"rkey\"\n"
3423 : " LEFT OUTER JOIN sys.\"tables\" ukt ON ukt.\"id\" = uk.\"table_id\"\n"
3424 : " LEFT OUTER JOIN sys.\"schemas\" uks ON uks.\"id\" = ukt.\"schema_id\"\n"
3425 : " ORDER BY s.\"name\", t.\"name\", fk.\"name\";\n"
3426 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
3427 :
3428 : "CREATE VIEW INFORMATION_SCHEMA.ROUTINES AS SELECT\n"
3429 : " cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
3430 : " s.\"name\" AS SPECIFIC_SCHEMA,\n"
3431 : " cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
3432 : " cast(NULL AS varchar(1)) AS ROUTINE_CATALOG,\n"
3433 : " s.\"name\" AS ROUTINE_SCHEMA,\n"
3434 : " f.\"name\" AS ROUTINE_NAME,\n"
3435 : " ft.\"function_type_keyword\" AS ROUTINE_TYPE,\n"
3436 : " cast(NULL AS varchar(1)) AS MODULE_CATALOG,\n"
3437 : " cast(NULL AS varchar(1)) AS MODULE_SCHEMA,\n"
3438 : " cast(f.\"mod\" AS varchar(128)) AS MODULE_NAME,\n"
3439 : " cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
3440 : " cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
3441 : " cast(NULL AS varchar(1)) AS UDT_NAME,\n"
3442 : " cast(CASE f.\"type\" WHEN 1 THEN sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) WHEN 2 THEN NULL WHEN 5 THEN 'TABLE' WHEN 7 THEN 'TABLE' ELSE NULL END AS varchar(1024)) AS DATA_TYPE,\n"
3443 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, a.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
3444 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, 4 * cast(a.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
3445 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
3446 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
3447 : " 'UTF-8' AS CHARACTER_SET_NAME,\n"
3448 : " cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
3449 : " cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
3450 : " cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
3451 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
3452 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(a.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
3453 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
3454 : " cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), a.\"type_scale\" -1, NULL) AS int) AS DATETIME_PRECISION,\n"
3455 : " cast(sys.ifthenelse(a.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
3456 : " cast(CASE a.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(a.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
3457 : " cast(NULL AS varchar(1)) AS TYPE_UDT_CATALOG,\n"
3458 : " cast(NULL AS varchar(1)) AS TYPE_UDT_SCHEMA,\n"
3459 : " cast(NULL AS varchar(1)) AS TYPE_UDT_NAME,\n"
3460 : " cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
3461 : " cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
3462 : " cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
3463 : " cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
3464 : " cast(NULL AS int) AS DTD_IDENTIFIER,\n"
3465 : " cast(sys.\"ifthenelse\"(sys.\"locate\"('begin',f.\"func\") > 0, sys.\"ifthenelse\"(sys.\"endswith\"(f.\"func\",';'), sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\"), sys.\"length\"(sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\")))-1), sys.\"substring\"(f.\"func\", sys.\"locate\"('begin',f.\"func\"))), NULL) AS varchar(8196)) AS ROUTINE_BODY,\n"
3466 : " f.\"func\" AS ROUTINE_DEFINITION,\n"
3467 : " cast(sys.\"ifthenelse\"(sys.\"locate\"('external name',f.\"func\") > 0, sys.\"ifthenelse\"(sys.\"endswith\"(f.\"func\",';'), sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\"), sys.\"length\"(sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\")))-1), sys.\"substring\"(f.\"func\", 14 + sys.\"locate\"('external name',f.\"func\"))), NULL) AS varchar(1024)) AS EXTERNAL_NAME,\n"
3468 : " fl.\"language_keyword\" AS EXTERNAL_LANGUAGE,\n"
3469 : " 'GENERAL' AS PARAMETER_STYLE,\n"
3470 : " 'YES' AS IS_DETERMINISTIC,\n"
3471 : " cast(sys.ifthenelse(f.\"side_effect\", 'MODIFIES', 'READ') AS varchar(10)) AS SQL_DATA_ACCESS,\n"
3472 : " cast(CASE f.\"type\" WHEN 2 THEN NULL ELSE 'NO' END AS varchar(3)) AS IS_NULL_CALL,\n"
3473 : " cast(NULL AS varchar(1)) AS SQL_PATH,\n"
3474 : " cast(NULL AS varchar(1)) AS SCHEMA_LEVEL_ROUTINE,\n"
3475 : " cast(NULL AS int) AS MAX_DYNAMIC_RESULT_SETS,\n"
3476 : " cast(NULL AS varchar(1)) AS IS_USER_DEFINED_CAST,\n"
3477 : " cast(NULL AS varchar(1)) AS IS_IMPLICITLY_INVOCABLE,\n"
3478 : " cast(NULL AS varchar(1)) AS SECURITY_TYPE,\n"
3479 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
3480 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
3481 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
3482 : " cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
3483 : " cast(NULL AS timestamp) AS CREATED,\n"
3484 : " cast(NULL AS timestamp) AS LAST_ALTERED,\n"
3485 : " cast(NULL AS varchar(1)) AS NEW_SAVEPOINT_LEVEL,\n"
3486 : " cast(NULL AS varchar(1)) AS IS_UDT_DEPENDENT,\n"
3487 : " cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DATA_TYPE,\n"
3488 : " cast(NULL AS varchar(1)) AS RESULT_CAST_AS_LOCATOR,\n"
3489 : " cast(NULL AS int) AS RESULT_CAST_CHAR_MAX_LENGTH,\n"
3490 : " cast(NULL AS int) AS RESULT_CAST_CHAR_OCTET_LENGTH,\n"
3491 : " cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_CATALOG,\n"
3492 : " cast(NULL AS varchar(1)) AS RESULT_CAST_CHAR_SET_SCHEMA,\n"
3493 : " cast(NULL AS varchar(1)) AS RESULT_CAST_CHARACTER_SET_NAME,\n"
3494 : " cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_CATALOG,\n"
3495 : " cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_SCHEMA,\n"
3496 : " cast(NULL AS varchar(1)) AS RESULT_CAST_COLLATION_NAME,\n"
3497 : " cast(NULL AS int) AS RESULT_CAST_NUMERIC_PRECISION,\n"
3498 : " cast(NULL AS int) AS RESULT_CAST_NUMERIC_RADIX,\n"
3499 : " cast(NULL AS int) AS RESULT_CAST_NUMERIC_SCALE,\n"
3500 : " cast(NULL AS int) AS RESULT_CAST_DATETIME_PRECISION,\n"
3501 : " cast(NULL AS varchar(1)) AS RESULT_CAST_INTERVAL_TYPE,\n"
3502 : " cast(NULL AS int) AS RESULT_CAST_INTERVAL_PRECISION,\n"
3503 : " cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_CATALOG,\n"
3504 : " cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_SCHEMA,\n"
3505 : " cast(NULL AS varchar(1)) AS RESULT_CAST_TYPE_UDT_NAME,\n"
3506 : " cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_CATALOG,\n"
3507 : " cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_SCHEMA,\n"
3508 : " cast(NULL AS varchar(1)) AS RESULT_CAST_SCOPE_NAME,\n"
3509 : " cast(NULL AS int) AS RESULT_CAST_MAX_CARDINALITY,\n"
3510 : " cast(NULL AS varchar(1)) AS RESULT_CAST_DTD_IDENTIFIER,\n"
3511 : " cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
3512 : " cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
3513 : " cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
3514 : " cast(NULL AS varchar(1)) AS RESULT_CAST_FROM_DECLARED_DATA_TYPE,\n"
3515 : " cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_PRECISION,\n"
3516 : " cast(NULL AS int) AS RESULT_CAST_DECLARED_NUMERIC_SCALE,\n"
3517 : " f.\"schema_id\" AS schema_id,\n"
3518 : " f.\"id\" AS function_id,\n"
3519 : " f.\"type\" AS function_type,\n"
3520 : " f.\"language\" AS function_language,\n"
3521 : " f.\"system\" AS is_system,\n"
3522 : " cm.\"remark\" AS comments\n"
3523 : " FROM sys.\"functions\" f\n"
3524 : " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
3525 : " INNER JOIN sys.\"function_types\" ft ON ft.\"function_type_id\" = f.\"type\"\n"
3526 : " INNER JOIN sys.\"function_languages\" fl ON fl.\"language_id\" = f.\"language\"\n"
3527 : " LEFT OUTER JOIN sys.\"args\" a ON a.\"func_id\" = f.\"id\" and a.\"inout\" = 0 and a.\"number\" = 0\n"
3528 : " LEFT OUTER JOIN sys.\"comments\" cm ON cm.\"id\" = f.\"id\"\n"
3529 : " WHERE f.\"type\" in (1, 2, 5, 7)\n"
3530 : " ORDER BY s.\"name\", f.\"name\";\n"
3531 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.ROUTINES TO PUBLIC WITH GRANT OPTION;\n"
3532 :
3533 : "CREATE VIEW INFORMATION_SCHEMA.PARAMETERS AS SELECT\n"
3534 : " cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
3535 : " s.\"name\" AS SPECIFIC_SCHEMA,\n"
3536 : " cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME,\n"
3537 : " cast(sys.ifthenelse((a.\"inout\" = 0 OR f.\"type\" = 2), 1 + a.\"number\", sys.ifthenelse(f.\"type\" = 1, a.\"number\", (1 + a.\"number\" - f.count_out_cols))) AS int) AS ORDINAL_POSITION,\n"
3538 : " cast(sys.ifthenelse(a.\"inout\" = 0, 'OUT', sys.ifthenelse(a.\"inout\" = 1, 'IN', 'INOUT')) as varchar(5)) AS PARAMETER_MODE,\n"
3539 : " cast(sys.ifthenelse(a.\"inout\" = 0, 'YES', 'NO') as varchar(3)) AS IS_RESULT,\n"
3540 : " cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
3541 : " a.\"name\" AS PARAMETER_NAME,\n"
3542 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_CATALOG,\n"
3543 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_SCHEMA,\n"
3544 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_NAME,\n"
3545 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
3546 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
3547 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
3548 : " cast(sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
3549 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, a.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
3550 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, 4 * cast(a.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
3551 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
3552 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
3553 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
3554 : " cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
3555 : " cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
3556 : " cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
3557 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
3558 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(a.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
3559 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
3560 : " cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\" -1, 0), NULL) AS int) AS DATETIME_PRECISION,\n"
3561 : " cast(sys.ifthenelse(a.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
3562 : " cast(CASE a.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(a.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
3563 : " cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
3564 : " cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
3565 : " cast(NULL AS varchar(1)) AS UDT_NAME,\n"
3566 : " cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
3567 : " cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
3568 : " cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
3569 : " cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
3570 : " cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
3571 : " cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
3572 : " cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
3573 : " cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
3574 : " cast(NULL AS varchar(1)) AS PARAMETER_DEFAULT,\n"
3575 : " f.\"schema_id\" AS schema_id,\n"
3576 : " f.\"id\" AS function_id,\n"
3577 : " a.\"id\" AS arg_id,\n"
3578 : " f.\"name\" AS function_name,\n"
3579 : " f.\"type\" AS function_type,\n"
3580 : " f.\"system\" AS is_system\n"
3581 : " FROM sys.\"args\" a\n"
3582 : " INNER JOIN (SELECT fun.*, (select count(*) from sys.args a0 where a0.inout = 0 and a0.func_id = fun.id) as count_out_cols FROM sys.\"functions\" fun WHERE fun.\"type\" in (1, 2, 5, 7)) f ON f.\"id\" = a.\"func_id\"\n"
3583 : " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
3584 : " ORDER BY s.\"name\", f.\"name\", f.\"id\", a.\"inout\" DESC, a.\"number\";\n"
3585 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.PARAMETERS TO PUBLIC WITH GRANT OPTION;\n"
3586 :
3587 : "CREATE VIEW INFORMATION_SCHEMA.SEQUENCES AS SELECT\n"
3588 : " cast(NULL AS varchar(1)) AS SEQUENCE_CATALOG,\n"
3589 : " s.\"name\" AS SEQUENCE_SCHEMA,\n"
3590 : " sq.\"name\" AS SEQUENCE_NAME,\n"
3591 : " cast('BIGINT' AS varchar(16)) AS DATA_TYPE,\n"
3592 : " cast(64 AS SMALLINT) AS NUMERIC_PRECISION,\n"
3593 : " cast(2 AS SMALLINT) AS NUMERIC_PRECISION_RADIX,\n"
3594 : " cast(0 AS SMALLINT) AS NUMERIC_SCALE,\n"
3595 : " sq.\"start\" AS START_VALUE,\n"
3596 : " sq.\"minvalue\" AS MINIMUM_VALUE,\n"
3597 : " sq.\"maxvalue\" AS MAXIMUM_VALUE,\n"
3598 : " sq.\"increment\" AS INCREMENT,\n"
3599 : " cast(sys.ifthenelse(sq.\"cycle\", 'YES', 'NO') AS varchar(3)) AS CYCLE_OPTION,\n"
3600 : " cast(NULL AS varchar(16)) AS DECLARED_DATA_TYPE,\n"
3601 : " cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_PRECISION,\n"
3602 : " cast(NULL AS SMALLINT) AS DECLARED_NUMERIC_SCALE,\n"
3603 : " sq.\"schema_id\" AS schema_id,\n"
3604 : " sq.\"id\" AS sequence_id,\n"
3605 : " get_value_for(s.\"name\", sq.\"name\") AS current_value,\n"
3606 : " sq.\"cacheinc\" AS cacheinc,\n"
3607 : " cm.\"remark\" AS comments\n"
3608 : " FROM sys.\"sequences\" sq\n"
3609 : " INNER JOIN sys.\"schemas\" s ON sq.\"schema_id\" = s.\"id\"\n"
3610 : " LEFT OUTER JOIN sys.\"comments\" cm ON sq.\"id\" = cm.\"id\"\n"
3611 : " ORDER BY s.\"name\", sq.\"name\";\n"
3612 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.SEQUENCES TO PUBLIC WITH GRANT OPTION;\n"
3613 : "\n"
3614 : "update sys._tables set system = true where system <> true\n"
3615 : " and schema_id = (select s.id from sys.schemas s where s.name = 'information_schema')\n"
3616 : " and name in ('character_sets','check_constraints','columns','parameters','routines','schemata','sequences','referential_constraints','table_constraints','tables','views');\n";
3617 0 : printf("Running database upgrade commands:\n%s\n", cmds);
3618 0 : fflush(stdout);
3619 0 : err = SQLstatementIntern(c, cmds, "update", true, false, NULL);
3620 : }
3621 :
3622 : /* 77_storage.sql */
3623 115 : sql_find_subtype(&tp, "varchar", 0, 0);
3624 :
3625 115 : if (!sql_bind_func(sql, s->base.name, "persist_unlogged", &tp, &tp, F_UNION, true, true)) {
3626 0 : sql->session->status = 0;
3627 0 : sql->errstr[0] = '\0';
3628 0 : const char query[] =
3629 : "CREATE FUNCTION sys.persist_unlogged(sname STRING, tname STRING)\n"
3630 : "RETURNS TABLE(\"table\" STRING, \"table_id\" INT, \"rowcount\" BIGINT)\n"
3631 : "EXTERNAL NAME sql.persist_unlogged;\n"
3632 : "GRANT EXECUTE ON FUNCTION sys.persist_unlogged(string, string) TO PUBLIC;\n"
3633 : "UPDATE sys.functions SET system = true WHERE system <> true AND\n"
3634 : "name = 'persist_unlogged' AND schema_id = 2000 AND type = 5 AND language = 1;\n";
3635 0 : printf("Running database upgrade commands:\n%s\n", query);
3636 0 : fflush(stdout);
3637 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
3638 : }
3639 :
3640 115 : return err;
3641 : }
3642 :
3643 : static str
3644 115 : sql_update_dec2023_sp1(Client c, mvc *sql, sql_schema *s)
3645 : {
3646 115 : char *err;
3647 115 : res_table *output;
3648 115 : BAT *b;
3649 :
3650 115 : (void) sql;
3651 115 : (void) s;
3652 :
3653 : /* json.isvalid(json) has been fixed to return NULL on NULL input */
3654 115 : err = SQLstatementIntern(c, "SELECT f.id FROM sys.functions f WHERE f.name = 'isvalid' AND f.schema_id = (SELECT s.id FROM sys.schemas s WHERE s.name = 'json') AND EXISTS (SELECT * FROM sys.args a WHERE a.func_id = f.id AND a.number = 1 AND a.type = 'json') AND f.func LIKE '%begin return true%';\n", "update", true, false, &output);
3655 115 : if (err)
3656 : return err;
3657 115 : b = BATdescriptor(output->cols[0].b);
3658 115 : if (b) {
3659 115 : if (BATcount(b) > 0) {
3660 0 : const char query[] = "drop function json.isvalid(json);\n"
3661 : "create function json.isvalid(js json)\n"
3662 : "returns bool begin return case when js is NULL then NULL else true end; end;\n"
3663 : "GRANT EXECUTE ON FUNCTION json.isvalid(json) TO PUBLIC;\n"
3664 : "update sys.functions set system = true where system <> true and name = 'isvalid' and schema_id = (select id from sys.schemas where name = 'json');\n";
3665 0 : assert(BATcount(b) == 1);
3666 0 : printf("Running database upgrade commands:\n%s\n", query);
3667 0 : fflush(stdout);
3668 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
3669 : }
3670 115 : BBPunfix(b->batCacheid);
3671 : }
3672 115 : res_table_destroy(output);
3673 115 : return err;
3674 : }
3675 :
3676 : static str
3677 115 : sql_update_dec2023_sp4(Client c, mvc *sql, sql_schema *s)
3678 : {
3679 115 : char *err;
3680 115 : res_table *output;
3681 115 : BAT *b;
3682 :
3683 115 : (void) sql;
3684 115 : (void) s;
3685 :
3686 : /* SQL optimizer fixes make that some "dependencies" are no longer
3687 : * dependencies (functions that depend on all columns of a table
3688 : * when it only actually uses a subset of the columns); while we're
3689 : * at it, also fix some ancient dependency changes where view did
3690 : * the same thing (i.e. the second delete will normally not delete
3691 : * anything) */
3692 115 : err = SQLstatementIntern(c, "select * from sys.dependencies where (id, depend_id) in (select c.id, f.id from sys.functions f, sys._tables t, sys._columns c, sys.dependencies d where c.table_id = t.id and f.id = d.depend_id and c.id = d.id and f.schema_id = 2000 and t.schema_id = 2000 and (f.name, t.name, c.name) in (values ('describe_columns', '_columns', 'storage')));\n", "update", true, false, &output);
3693 115 : if (err)
3694 : return err;
3695 115 : b = BATdescriptor(output->cols[0].b);
3696 115 : if (b) {
3697 115 : if (BATcount(b) > 0) {
3698 0 : const char query[] =
3699 : "delete from sys.dependencies where (id, depend_id) in (select c.id, f.id from sys.functions f, sys._tables t, sys._columns c, sys.dependencies d where c.table_id = t.id and f.id = d.depend_id and c.id = d.id and f.schema_id = 2000 and t.schema_id = 2000 and (f.name, t.name, c.name) in (values ('describe_columns', '_columns', 'storage'), ('describe_function', 'function_languages', 'language_name'), ('describe_function', 'function_types', 'function_type_name'), ('describe_function', 'functions', 'func'), ('describe_function', 'functions', 'mod'), ('describe_function', 'functions', 'semantics'), ('describe_function', 'functions', 'side_effect'), ('describe_function', 'functions', 'system'), ('describe_function', 'functions', 'vararg'), ('describe_function', 'functions', 'varres'), ('describe_function', 'schemas', 'authorization'), ('describe_function', 'schemas', 'owner'), ('describe_function', 'schemas', 'system'), ('describe_table', '_tables', 'access'), ('describe_table', '_tables', 'commit_action'), ('describe_table', '_tables', 'system')));\n"
3700 : "delete from sys.dependencies where (id, depend_id) in (select c.id, v.id from sys._tables v, sys._tables t, sys._columns c, sys.dependencies d where c.table_id = t.id and v.id = d.depend_id and c.id = d.id and v.schema_id = 2000 and t.schema_id = 2000 and (v.name, t.name, c.name) in (values ('dependency_columns_on_indexes', '_columns', 'name'), ('dependency_columns_on_indexes', '_columns', 'number'), ('dependency_columns_on_indexes', '_columns', 'storage'), ('dependency_columns_on_indexes', '_columns', 'table_id'), ('dependency_columns_on_indexes', '_columns', 'type_digits'), ('dependency_columns_on_indexes', 'keys', 'id'), ('dependency_columns_on_indexes', 'triggers', 'name'), ('dependency_columns_on_indexes', 'triggers', 'orientation'), ('dependency_columns_on_indexes', 'triggers', 'table_id'), ('dependency_columns_on_indexes', 'triggers', 'time'), ('dependency_columns_on_keys', '_columns', 'name'), ('dependency_columns_on_keys', '_columns', 'table_id'), ('dependency_columns_on_keys', '_columns', 'type'), ('dependency_columns_on_keys', '_columns', 'type_digits'), ('dependency_columns_on_keys', '_columns', 'type_scale'), ('dependency_columns_on_keys', 'triggers', 'name'), ('dependency_columns_on_keys', 'triggers', 'orientation'), ('dependency_columns_on_keys', 'triggers', 'table_id'), ('dependency_columns_on_keys', 'triggers', 'time'), ('dependency_columns_on_triggers', 'keys', 'name'), ('dependency_columns_on_triggers', 'keys', 'rkey'), ('dependency_columns_on_triggers', 'keys', 'type'), ('dependency_functions_on_triggers', 'keys', 'action'), ('dependency_functions_on_triggers', 'keys', 'name'), ('dependency_functions_on_triggers', 'keys', 'rkey'), ('dependency_functions_on_triggers', 'keys', 'type'), ('dependency_keys_on_foreignkeys', '_columns', 'default'), ('dependency_keys_on_foreignkeys', '_columns', 'name'), ('dependency_keys_on_foreignkeys', '_columns', 'table_id'), ('dependency_keys_on_foreignkeys', '_columns', 'type'), ('dependency_keys_on_foreignkeys', '_columns', 'type_digits'), ('dependency_keys_on_foreignkeys', '_columns', 'type_scale'), ('dependency_tables_on_foreignkeys', '_columns', 'default'), ('dependency_tables_on_foreignkeys', '_columns', 'name'), ('dependency_tables_on_foreignkeys', '_columns', 'table_id'), ('dependency_tables_on_foreignkeys', '_columns', 'type'), ('dependency_tables_on_foreignkeys', '_columns', 'type_digits'), ('dependency_tables_on_foreignkeys', '_columns', 'type_scale'), ('dependency_tables_on_indexes', '_columns', 'name'), ('dependency_tables_on_indexes', '_columns', 'number'), ('dependency_tables_on_indexes', '_columns', 'storage'), ('dependency_tables_on_indexes', '_columns', 'table_id'), ('dependency_tables_on_indexes', '_columns', 'type_digits'), ('dependency_tables_on_indexes', 'keys', 'id'), ('dependency_tables_on_triggers', 'keys', 'action'), ('dependency_tables_on_triggers', 'keys', 'name'), ('dependency_tables_on_triggers', 'keys', 'rkey'), ('dependency_tables_on_triggers', 'keys', 'type')));\n"
3701 : "delete from sys.triggers where table_id not in (select id from sys._tables);\n"
3702 : "commit;\n";
3703 0 : assert(BATcount(b) == 1);
3704 0 : printf("Running database upgrade commands:\n%s\n", query);
3705 0 : fflush(stdout);
3706 0 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
3707 0 : if (err == MAL_SUCCEED) {
3708 0 : const char query2[] =
3709 : "create temporary table d as (select distinct * from sys.dependencies);\n"
3710 : "delete from sys.dependencies;\n"
3711 : "insert into sys.dependencies (select * from d);\n";
3712 0 : printf("Running database upgrade commands:\n%s\n", query2);
3713 0 : fflush(stdout);
3714 0 : err = SQLstatementIntern(c, query2, "update", true, false, NULL);
3715 : }
3716 : }
3717 115 : BBPunfix(b->batCacheid);
3718 : }
3719 115 : res_table_destroy(output);
3720 115 : return err;
3721 : }
3722 :
3723 : static str
3724 115 : sql_update_aug2024(Client c, mvc *sql, sql_schema *s)
3725 : {
3726 115 : char *err;
3727 115 : res_table *output;
3728 115 : BAT *b;
3729 :
3730 115 : err = SQLstatementIntern(c, "SELECT id FROM sys.functions WHERE schema_id = 2000 AND name = 'describe_type' AND func LIKE '%sql_datatype%';\n", "update", true, false, &output);
3731 115 : if (err)
3732 : return err;
3733 115 : b = BATdescriptor(output->cols[0].b);
3734 115 : if (b) {
3735 115 : if (BATcount(b) == 0) {
3736 : /* do update */
3737 8 : sql_table *t;
3738 8 : const char query1[] =
3739 : "update sys._columns set type_digits = 7 where type = 'tinyint' and type_digits <> 7;\n"
3740 : "update sys._columns set type_digits = 15 where type = 'smallint' and type_digits <> 15;\n"
3741 : "update sys._columns set type_digits = 31 where type = 'int' and type_digits <> 31;\n"
3742 : "update sys._columns set type_digits = 63 where type = 'bigint' and type_digits <> 63;\n"
3743 : "update sys._columns set type_digits = 127 where type = 'hugeint' and type_digits <> 127;\n"
3744 : "update sys._columns set type = 'varchar' where type in ('clob', 'char') and table_id in (select id from sys._tables where system and name <> 'netcdf_files');\n"
3745 : "update sys.args set type_digits = 7 where type = 'tinyint' and type_digits <> 7;\n"
3746 : "update sys.args set type_digits = 15 where type = 'smallint' and type_digits <> 15;\n"
3747 : "update sys.args set type_digits = 31 where type = 'int' and type_digits <> 31;\n"
3748 : "update sys.args set type_digits = 63 where type = 'bigint' and type_digits <> 63;\n"
3749 : "update sys.args set type_digits = 127 where type = 'hugeint' and type_digits <> 127;\n"
3750 : "update sys.args set type = 'varchar' where type in ('clob', 'char');\n"
3751 : "drop aggregate median(decimal(18,3));\n"
3752 : "drop aggregate median_avg(decimal(18,3));\n"
3753 : "drop aggregate quantile(decimal(18,3), double);\n"
3754 : "drop aggregate quantile_avg(decimal(18,3), double);\n"
3755 : "create aggregate median(val DECIMAL(2)) returns DECIMAL(2)\n"
3756 : " external name \"aggr\".\"median\";\n"
3757 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(2)) TO PUBLIC;\n"
3758 : "create aggregate median(val DECIMAL(4)) returns DECIMAL(4)\n"
3759 : " external name \"aggr\".\"median\";\n"
3760 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(4)) TO PUBLIC;\n"
3761 : "create aggregate median(val DECIMAL(9)) returns DECIMAL(9)\n"
3762 : " external name \"aggr\".\"median\";\n"
3763 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(9)) TO PUBLIC;\n"
3764 : "create aggregate median(val DECIMAL(18)) returns DECIMAL(18)\n"
3765 : " external name \"aggr\".\"median\";\n"
3766 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(18)) TO PUBLIC;\n"
3767 : "create aggregate median_avg(val DECIMAL(2)) returns DOUBLE\n"
3768 : " external name \"aggr\".\"median_avg\";\n"
3769 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(2)) TO PUBLIC;\n"
3770 : "create aggregate median_avg(val DECIMAL(4)) returns DOUBLE\n"
3771 : " external name \"aggr\".\"median_avg\";\n"
3772 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(4)) TO PUBLIC;\n"
3773 : "create aggregate median_avg(val DECIMAL(9)) returns DOUBLE\n"
3774 : " external name \"aggr\".\"median_avg\";\n"
3775 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(9)) TO PUBLIC;\n"
3776 : "create aggregate median_avg(val DECIMAL(18)) returns DOUBLE\n"
3777 : " external name \"aggr\".\"median_avg\";\n"
3778 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(18)) TO PUBLIC;\n"
3779 : "create aggregate quantile(val DECIMAL(2), q DOUBLE) returns DECIMAL(2)\n"
3780 : " external name \"aggr\".\"quantile\";\n"
3781 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(2), DOUBLE) TO PUBLIC;\n"
3782 : "create aggregate quantile(val DECIMAL(4), q DOUBLE) returns DECIMAL(4)\n"
3783 : " external name \"aggr\".\"quantile\";\n"
3784 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(4), DOUBLE) TO PUBLIC;\n"
3785 : "create aggregate quantile(val DECIMAL(9), q DOUBLE) returns DECIMAL(9)\n"
3786 : " external name \"aggr\".\"quantile\";\n"
3787 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(9), DOUBLE) TO PUBLIC;\n"
3788 : "create aggregate quantile(val DECIMAL(18), q DOUBLE) returns DECIMAL(18)\n"
3789 : " external name \"aggr\".\"quantile\";\n"
3790 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(18), DOUBLE) TO PUBLIC;\n"
3791 : "create aggregate quantile_avg(val DECIMAL(2), q DOUBLE) returns DOUBLE\n"
3792 : " external name \"aggr\".\"quantile_avg\";\n"
3793 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(2), DOUBLE) TO PUBLIC;\n"
3794 : "create aggregate quantile_avg(val DECIMAL(4), q DOUBLE) returns DOUBLE\n"
3795 : " external name \"aggr\".\"quantile_avg\";\n"
3796 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(4), DOUBLE) TO PUBLIC;\n"
3797 : "create aggregate quantile_avg(val DECIMAL(9), q DOUBLE) returns DOUBLE\n"
3798 : " external name \"aggr\".\"quantile_avg\";\n"
3799 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(9), DOUBLE) TO PUBLIC;\n"
3800 : "create aggregate quantile_avg(val DECIMAL(18), q DOUBLE) returns DOUBLE\n"
3801 : " external name \"aggr\".\"quantile_avg\";\n"
3802 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(18), DOUBLE) TO PUBLIC;\n"
3803 : "drop function if exists sys.time_to_str(time with time zone, string) cascade;\n"
3804 : "drop function if exists sys.timestamp_to_str(timestamp with time zone, string) cascade;\n"
3805 : "create function time_to_str(d time, format string) returns string\n"
3806 : " external name mtime.\"time_to_str\";\n"
3807 : "create function time_to_str(d time with time zone, format string) returns string\n"
3808 : " external name mtime.\"timetz_to_str\";\n"
3809 : "create function timestamp_to_str(d timestamp with time zone, format string) returns string\n"
3810 : " external name mtime.\"timestamptz_to_str\";\n"
3811 : "grant execute on function time_to_str(time, string) to public;\n"
3812 : "grant execute on function time_to_str(time with time zone, string) to public;\n"
3813 : "grant execute on function timestamp_to_str(timestamp with time zone, string) to public;\n"
3814 : "update sys.functions set system = true where not system and schema_id = 2000 and name in ('time_to_str', 'timestamp_to_str', 'median', 'median_avg', 'quantile', 'quantile_avg');\n"
3815 : "drop function if exists sys.dump_database(boolean) cascade;\n"
3816 : "drop view sys.dump_comments;\n"
3817 : "drop view sys.dump_tables;\n"
3818 : "drop view sys.dump_functions;\n"
3819 : "drop view sys.dump_function_grants;\n"
3820 : "drop function if exists sys.describe_columns(string, string) cascade;\n"
3821 : "drop view sys.describe_functions;\n"
3822 : "drop view sys.describe_privileges;\n"
3823 : "drop view sys.describe_comments;\n"
3824 : "drop view sys.fully_qualified_functions;\n"
3825 : "drop view sys.describe_tables;\n"
3826 : "drop function if exists sys.describe_type(string, integer, integer) cascade;\n"
3827 : "CREATE FUNCTION sys.describe_type(ctype string, digits integer, tscale integer)\n"
3828 : " RETURNS string\n"
3829 : "BEGIN\n"
3830 : " RETURN sys.sql_datatype(ctype, digits, tscale, false, false);\n"
3831 : "END;\n"
3832 : "CREATE VIEW sys.describe_tables AS\n"
3833 : " SELECT\n"
3834 : " t.id o,\n"
3835 : " s.name sch,\n"
3836 : " t.name tab,\n"
3837 : " ts.table_type_name typ,\n"
3838 : " (SELECT\n"
3839 : " ' (' ||\n"
3840 : " GROUP_CONCAT(\n"
3841 : " sys.DQ(c.name) || ' ' ||\n"
3842 : " sys.describe_type(c.type, c.type_digits, c.type_scale) ||\n"
3843 : " ifthenelse(c.\"null\" = 'false', ' NOT NULL', '')\n"
3844 : " , ', ') || ')'\n"
3845 : " FROM sys._columns c\n"
3846 : " WHERE c.table_id = t.id) col,\n"
3847 : " CASE ts.table_type_name\n"
3848 : " WHEN 'REMOTE TABLE' THEN\n"
3849 : " sys.get_remote_table_expressions(s.name, t.name)\n"
3850 : " WHEN 'MERGE TABLE' THEN\n"
3851 : " sys.get_merge_table_partition_expressions(t.id)\n"
3852 : " WHEN 'VIEW' THEN\n"
3853 : " sys.schema_guard(s.name, t.name, t.query)\n"
3854 : " ELSE\n"
3855 : " ''\n"
3856 : " END opt\n"
3857 : " FROM sys.schemas s, sys.table_types ts, sys.tables t\n"
3858 : " WHERE ts.table_type_name IN ('TABLE', 'VIEW', 'MERGE TABLE', 'REMOTE TABLE', 'REPLICA TABLE', 'UNLOGGED TABLE')\n"
3859 : " AND t.system = FALSE\n"
3860 : " AND s.id = t.schema_id\n"
3861 : " AND ts.table_type_id = t.type\n"
3862 : " AND s.name <> 'tmp';\n"
3863 : "CREATE VIEW sys.fully_qualified_functions AS\n"
3864 : " WITH fqn(id, tpe, sig, num) AS\n"
3865 : " (\n"
3866 : " SELECT\n"
3867 : " f.id,\n"
3868 : " ft.function_type_keyword,\n"
3869 : " CASE WHEN a.type IS NULL THEN\n"
3870 : " sys.fqn(s.name, f.name) || '()'\n"
3871 : " ELSE\n"
3872 : " sys.fqn(s.name, f.name) || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',') OVER (PARTITION BY f.id ORDER BY a.number) || ')'\n"
3873 : " END,\n"
3874 : " a.number\n"
3875 : " FROM sys.schemas s, sys.function_types ft, sys.functions f LEFT JOIN sys.args a ON f.id = a.func_id\n"
3876 : " WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
3877 : " )\n"
3878 : " SELECT\n"
3879 : " fqn1.id id,\n"
3880 : " fqn1.tpe tpe,\n"
3881 : " fqn1.sig nme\n"
3882 : " FROM\n"
3883 : " fqn fqn1 JOIN (SELECT id, max(num) FROM fqn GROUP BY id) fqn2(id, num)\n"
3884 : " ON fqn1.id = fqn2.id AND (fqn1.num = fqn2.num OR fqn1.num IS NULL AND fqn2.num is NULL);\n"
3885 : "CREATE VIEW sys.describe_comments AS\n"
3886 : " SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
3887 : " FROM (\n"
3888 : " SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
3889 : " UNION ALL\n"
3890 : " SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
3891 : " FROM sys.schemas s JOIN sys._tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
3892 : " WHERE NOT t.system\n"
3893 : " UNION ALL\n"
3894 : " SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys._tables t, sys.schemas s WHERE NOT t.system AND c.table_id = t.id AND t.schema_id = s.id\n"
3895 : " UNION ALL\n"
3896 : " SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE NOT t.system AND idx.table_id = t.id AND t.schema_id = s.id\n"
3897 : " UNION ALL\n"
3898 : " SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
3899 : " UNION ALL\n"
3900 : " SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf\n"
3901 : " WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
3902 : " ) AS o(id, tpe, nme)\n"
3903 : " JOIN sys.comments cm ON cm.id = o.id;\n"
3904 : "CREATE VIEW sys.describe_privileges AS\n"
3905 : " SELECT\n"
3906 : " CASE\n"
3907 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN --GLOBAL privileges: SELECT maps to COPY FROM\n"
3908 : " 'COPY FROM'\n"
3909 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN --GLOBAL privileges: UPDATE maps to COPY INTO\n"
3910 : " 'COPY INTO'\n"
3911 : " ELSE\n"
3912 : " o.nme\n"
3913 : " END o_nme,\n"
3914 : " coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
3915 : " pc.privilege_code_name p_nme,\n"
3916 : " a.name a_nme,\n"
3917 : " g.name g_nme,\n"
3918 : " p.grantable grantable\n"
3919 : " FROM\n"
3920 : " sys.privileges p LEFT JOIN\n"
3921 : " (\n"
3922 : " SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
3923 : " from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
3924 : " UNION ALL\n"
3925 : " SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
3926 : " FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
3927 : " UNION ALL\n"
3928 : " SELECT f.id, f.nme, f.tpe\n"
3929 : " FROM sys.fully_qualified_functions f\n"
3930 : " ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
3931 : " sys.privilege_codes pc,\n"
3932 : " auths a, auths g\n"
3933 : " WHERE\n"
3934 : " p.privileges = pc.privilege_code_id AND\n"
3935 : " p.auth_id = a.id AND\n"
3936 : " p.grantor = g.id;\n"
3937 : "CREATE VIEW sys.describe_functions AS\n"
3938 : " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
3939 : " (\n"
3940 : " SELECT\n"
3941 : " func_id,\n"
3942 : " number,\n"
3943 : " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
3944 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
3945 : " FROM sys.args\n"
3946 : " WHERE inout = 1\n"
3947 : " ),\n"
3948 : " func_args(func_id, func_arg) AS\n"
3949 : " (\n"
3950 : " SELECT func_id, func_arg\n"
3951 : " FROM func_args_all\n"
3952 : " WHERE number = max_number\n"
3953 : " ),\n"
3954 : " func_rets_all(func_id, number, max_number, func_ret, func_ret_type) AS\n"
3955 : " (\n"
3956 : " SELECT\n"
3957 : " func_id,\n"
3958 : " number,\n"
3959 : " max(number) OVER (PARTITION BY func_id ORDER BY number DESC),\n"
3960 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number),\n"
3961 : " group_concat(sys.describe_type(type, type_digits, type_scale),', ') OVER (PARTITION BY func_id ORDER BY number)\n"
3962 : " FROM sys.args\n"
3963 : " WHERE inout = 0\n"
3964 : " ),\n"
3965 : " func_rets(func_id, func_ret, func_ret_type) AS\n"
3966 : " (\n"
3967 : " SELECT\n"
3968 : " func_id,\n"
3969 : " func_ret,\n"
3970 : " func_ret_type\n"
3971 : " FROM func_rets_all\n"
3972 : " WHERE number = max_number\n"
3973 : " )\n"
3974 : " SELECT\n"
3975 : " f.id o,\n"
3976 : " s.name sch,\n"
3977 : " f.name fun,\n"
3978 : " CASE WHEN f.language IN (1, 2) THEN f.func ELSE 'CREATE ' || ft.function_type_keyword || ' ' || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ')' || CASE WHEN f.type = 5 THEN ' RETURNS TABLE (' || coalesce(fr.func_ret, '') || ')' WHEN f.type IN (1,3) THEN ' RETURNS ' || fr.func_ret_type ELSE '' END || CASE WHEN fl.language_keyword IS NULL THEN '' ELSE ' LANGUAGE ' || fl.language_keyword END || ' ' || f.func END def\n"
3979 : " FROM sys.functions f\n"
3980 : " LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
3981 : " LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
3982 : " JOIN sys.schemas s ON f.schema_id = s.id\n"
3983 : " JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
3984 : " LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
3985 : " WHERE s.name <> 'tmp' AND NOT f.system;\n"
3986 : "CREATE FUNCTION sys.describe_columns(schemaName string, tableName string)\n"
3987 : " RETURNS TABLE(name string, type string, digits integer, scale integer, Nulls boolean, cDefault string, number integer, sqltype string, remark string)\n"
3988 : "BEGIN\n"
3989 : " RETURN SELECT c.name, c.\"type\", c.type_digits, c.type_scale, c.\"null\", c.\"default\", c.number, sys.describe_type(c.\"type\", c.type_digits, c.type_scale), com.remark\n"
3990 : " FROM sys._tables t, sys.schemas s, sys._columns c\n"
3991 : " LEFT OUTER JOIN sys.comments com ON c.id = com.id\n"
3992 : " WHERE c.table_id = t.id\n"
3993 : " AND t.name = tableName\n"
3994 : " AND t.schema_id = s.id\n"
3995 : " AND s.name = schemaName\n"
3996 : " ORDER BY c.number;\n"
3997 : "END;\n"
3998 : "CREATE VIEW sys.dump_function_grants AS\n"
3999 : " WITH func_args_all(func_id, number, max_number, func_arg) AS\n"
4000 : " (SELECT a.func_id,\n"
4001 : " a.number,\n"
4002 : " max(a.number) OVER (PARTITION BY a.func_id ORDER BY a.number DESC),\n"
4003 : " group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ', ') OVER (PARTITION BY a.func_id ORDER BY a.number)\n"
4004 : " FROM sys.args a\n"
4005 : " WHERE a.inout = 1),\n"
4006 : " func_args(func_id, func_arg) AS\n"
4007 : " (SELECT func_id, func_arg FROM func_args_all WHERE number = max_number)\n"
4008 : " SELECT\n"
4009 : " 'GRANT ' || pc.privilege_code_name || ' ON ' || ft.function_type_keyword || ' '\n"
4010 : " || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ') TO '\n"
4011 : " || ifthenelse(a.name = 'public', 'PUBLIC', sys.dq(a.name))\n"
4012 : " || CASE WHEN p.grantable = 1 THEN ' WITH GRANT OPTION' ELSE '' END || ';' stmt,\n"
4013 : " s.name schema_name,\n"
4014 : " f.name function_name,\n"
4015 : " a.name grantee\n"
4016 : " FROM sys.schemas s,\n"
4017 : " sys.functions f LEFT OUTER JOIN func_args fa ON f.id = fa.func_id,\n"
4018 : " sys.auths a,\n"
4019 : " sys.privileges p,\n"
4020 : " sys.auths g,\n"
4021 : " sys.function_types ft,\n"
4022 : " sys.privilege_codes pc\n"
4023 : " WHERE s.id = f.schema_id\n"
4024 : " AND f.id = p.obj_id\n"
4025 : " AND p.auth_id = a.id\n"
4026 : " AND p.grantor = g.id\n"
4027 : " AND p.privileges = pc.privilege_code_id\n"
4028 : " AND f.type = ft.function_type_id\n"
4029 : " AND NOT f.system\n"
4030 : " ORDER BY s.name, f.name, a.name, g.name, p.grantable;\n"
4031 : "CREATE VIEW sys.dump_functions AS\n"
4032 : " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
4033 : " f.sch schema_name,\n"
4034 : " f.fun function_name\n"
4035 : " FROM sys.describe_functions f;\n"
4036 : "CREATE VIEW sys.dump_tables AS\n"
4037 : " SELECT\n"
4038 : " t.o o,\n"
4039 : " CASE\n"
4040 : " WHEN t.typ <> 'VIEW' THEN\n"
4041 : " 'CREATE ' || t.typ || ' ' || sys.FQN(t.sch, t.tab) || t.col || t.opt || ';'\n"
4042 : " ELSE\n"
4043 : " t.opt\n"
4044 : " END stmt,\n"
4045 : " t.sch schema_name,\n"
4046 : " t.tab table_name\n"
4047 : " FROM sys.describe_tables t;\n"
4048 : "CREATE VIEW sys.dump_comments AS\n"
4049 : " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
4050 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
4051 : "BEGIN\n"
4052 : " SET SCHEMA sys;\n"
4053 : " TRUNCATE sys.dump_statements;\n"
4054 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
4055 : " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
4056 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
4057 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
4058 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
4059 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
4060 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
4061 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
4062 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
4063 : " --functions and table-likes can be interdependent. They should be inserted in the order of their catalogue id.\n"
4064 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
4065 : " FROM (\n"
4066 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
4067 : " UNION ALL\n"
4068 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
4069 : " ) AS stmts(o, s);\n"
4070 : " -- dump table data before adding constraints and fixing sequences\n"
4071 : " IF NOT DESCRIBE THEN\n"
4072 : " CALL sys.dump_table_data();\n"
4073 : " END IF;\n"
4074 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
4075 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
4076 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
4077 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
4078 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
4079 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
4080 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
4081 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
4082 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
4083 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
4084 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
4085 : " --TODO Improve performance of dump_table_data.\n"
4086 : " --TODO loaders, procedures, window and filter sys.functions.\n"
4087 : " --TODO look into order dependent group_concat\n"
4088 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
4089 : " RETURN sys.dump_statements;\n"
4090 : "END;\n"
4091 : "GRANT SELECT ON sys.describe_tables TO PUBLIC;\n"
4092 : "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
4093 : "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
4094 : "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
4095 : "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n"
4096 : "CREATE FUNCTION sys.check_constraint(sname STRING, cname STRING) RETURNS STRING EXTERNAL NAME sql.\"check\";\n"
4097 : "grant execute on function sys.check_constraint to public;\n"
4098 : "update sys.functions set system = true where not system and schema_id = 2000 and name in ('dump_database', 'describe_columns', 'describe_type', 'check_constraint');\n"
4099 : "update sys._tables set system = true where not system and schema_id = 2000 and name in ('dump_comments', 'dump_tables', 'dump_functions', 'dump_function_grants', 'describe_functions', 'describe_privileges', 'describe_comments', 'fully_qualified_functions', 'describe_tables');\n";
4100 8 : if ((t = mvc_bind_table(sql, s, "dump_comments")) != NULL)
4101 8 : t->system = 0;
4102 8 : if ((t = mvc_bind_table(sql, s, "dump_tables")) != NULL)
4103 8 : t->system = 0;
4104 8 : if ((t = mvc_bind_table(sql, s, "dump_functions")) != NULL)
4105 8 : t->system = 0;
4106 8 : if ((t = mvc_bind_table(sql, s, "dump_function_grants")) != NULL)
4107 8 : t->system = 0;
4108 8 : if ((t = mvc_bind_table(sql, s, "describe_functions")) != NULL)
4109 8 : t->system = 0;
4110 8 : if ((t = mvc_bind_table(sql, s, "describe_privileges")) != NULL)
4111 8 : t->system = 0;
4112 8 : if ((t = mvc_bind_table(sql, s, "describe_comments")) != NULL)
4113 8 : t->system = 0;
4114 8 : if ((t = mvc_bind_table(sql, s, "fully_qualified_functions")) != NULL)
4115 8 : t->system = 0;
4116 8 : if ((t = mvc_bind_table(sql, s, "describe_tables")) != NULL)
4117 8 : t->system = 0;
4118 8 : printf("Running database upgrade commands:\n%s\n", query1);
4119 8 : fflush(stdout);
4120 8 : err = SQLstatementIntern(c, query1, "update", true, false, NULL);
4121 : #ifdef HAVE_HGE
4122 8 : if (err == MAL_SUCCEED) {
4123 8 : sql_subtype tp;
4124 8 : sql_find_subtype(&tp, "decimal", 38, 0);
4125 8 : if (!sql_bind_func(sql, s->base.name, "median", &tp, NULL, F_AGGR, true, true)) {
4126 4 : sql->session->status = 0;
4127 4 : sql->errstr[0] = '\0';
4128 4 : const char query0[] =
4129 : "create aggregate median(val DECIMAL(38)) returns DECIMAL(38)\n"
4130 : " external name \"aggr\".\"median\";\n"
4131 : "GRANT EXECUTE ON AGGREGATE median(DECIMAL(38)) TO PUBLIC;\n"
4132 : "create aggregate median_avg(val DECIMAL(38)) returns DOUBLE\n"
4133 : " external name \"aggr\".\"median_avg\";\n"
4134 : "GRANT EXECUTE ON AGGREGATE median_avg(DECIMAL(38)) TO PUBLIC;\n"
4135 : "create aggregate quantile(val DECIMAL(38), q DOUBLE) returns DECIMAL(38)\n"
4136 : " external name \"aggr\".\"quantile\";\n"
4137 : "GRANT EXECUTE ON AGGREGATE quantile(DECIMAL(38), DOUBLE) TO PUBLIC;\n"
4138 : "create aggregate quantile_avg(val DECIMAL(38), q DOUBLE) returns DOUBLE\n"
4139 : " external name \"aggr\".\"quantile_avg\";\n"
4140 : "GRANT EXECUTE ON AGGREGATE quantile_avg(DECIMAL(38), DOUBLE) TO PUBLIC;\n"
4141 : "grant execute on function sys.generate_series(hugeint, hugeint) to public;\n"
4142 : "grant execute on function sys.generate_series(hugeint, hugeint, hugeint) to public;\n"
4143 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('median', 'median_avg', 'quantile', 'quantile_avg');\n";
4144 4 : printf("Running database upgrade commands:\n%s\n", query0);
4145 4 : fflush(stdout);
4146 4 : err = SQLstatementIntern(c, query0, "update", true, false, NULL);
4147 : }
4148 : }
4149 : #endif
4150 8 : if (err == MAL_SUCCEED) {
4151 8 : sql_subtype tp;
4152 8 : sql_find_subtype(&tp, "smallint", 0, 0);
4153 8 : if (!sql_bind_func(sql, s->base.name, "generate_series", &tp, &tp, F_UNION, true, true)) {
4154 2 : sql->session->status = 0;
4155 2 : sql->errstr[0] = '\0';
4156 2 : const char query[] =
4157 : "create function sys.generate_series(first smallint, \"limit\" smallint)\n"
4158 : "returns table (value smallint)\n"
4159 : "external name generator.series;\n"
4160 : "create function sys.generate_series(first smallint, \"limit\" smallint, stepsize smallint)\n"
4161 : "returns table (value smallint)\n"
4162 : "external name generator.series;\n"
4163 : "update sys.functions set system = true where system <> true and name in ('generate_series') and schema_id = 2000;\n";
4164 2 : printf("Running database upgrade commands:\n%s\n", query);
4165 2 : fflush(stdout);
4166 2 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4167 : }
4168 : }
4169 8 : if (err == MAL_SUCCEED) {
4170 8 : const char query[] =
4171 : "create function sys.generate_series(first date, \"limit\" date, stepsize interval month)\n"
4172 : "returns table (value date)\n"
4173 : "external name generator.series;\n"
4174 : "create function sys.generate_series(first date, \"limit\" date, stepsize interval day)\n"
4175 : "returns table (value date)\n"
4176 : "external name generator.series;\n"
4177 : "grant execute on function sys.generate_series(tinyint, tinyint) to public;\n"
4178 : "grant execute on function sys.generate_series(tinyint, tinyint, tinyint) to public;\n"
4179 : "grant execute on function sys.generate_series(smallint, smallint) to public;\n"
4180 : "grant execute on function sys.generate_series(smallint, smallint, smallint) to public;\n"
4181 : "grant execute on function sys.generate_series(int, int) to public;\n"
4182 : "grant execute on function sys.generate_series(int, int, int) to public;\n"
4183 : "grant execute on function sys.generate_series(bigint, bigint) to public;\n"
4184 : "grant execute on function sys.generate_series(bigint, bigint, bigint) to public;\n"
4185 : "grant execute on function sys.generate_series(real, real, real) to public;\n"
4186 : "grant execute on function sys.generate_series(double, double, double) to public;\n"
4187 : "grant execute on function sys.generate_series(decimal(10,2), decimal(10,2), decimal(10,2)) to public;\n"
4188 : "grant execute on function sys.generate_series(date, date, interval month) to public;\n"
4189 : "grant execute on function sys.generate_series(date, date, interval day) to public;\n"
4190 : "grant execute on function sys.generate_series(timestamp, timestamp, interval second) to public;\n"
4191 : "grant execute on function sys.generate_series(timestamp, timestamp, interval day) to public;\n"
4192 : "update sys.functions set system = true where system <> true and name = 'generate_series' and schema_id = 2000;\n";
4193 8 : sql->session->status = 0;
4194 8 : sql->errstr[0] = '\0';
4195 8 : printf("Running database upgrade commands:\n%s\n", query);
4196 8 : fflush(stdout);
4197 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4198 : }
4199 8 : if (err == MAL_SUCCEED) {
4200 8 : const char query[] =
4201 : "drop view sys.sessions;\n"
4202 : "drop function sys.sessions();\n"
4203 : "create function sys.sessions()\n"
4204 : " returns table(\n"
4205 : " \"sessionid\" int,\n"
4206 : " \"username\" string,\n"
4207 : " \"login\" timestamp,\n"
4208 : " \"idle\" timestamp,\n"
4209 : " \"optimizer\" string,\n"
4210 : " \"sessiontimeout\" int,\n"
4211 : " \"querytimeout\" int,\n"
4212 : " \"workerlimit\" int,\n"
4213 : " \"memorylimit\" int,\n"
4214 : " \"language\" string,\n"
4215 : " \"peer\" string,\n"
4216 : " \"hostname\" string,\n"
4217 : " \"application\" string,\n"
4218 : " \"client\" string,\n"
4219 : " \"clientpid\" bigint,\n"
4220 : " \"remark\" string\n"
4221 : " )\n"
4222 : " external name sql.sessions;\n"
4223 : "create view sys.sessions as select * from sys.sessions();\n"
4224 : "grant select on sys.sessions to public;\n"
4225 : "create procedure sys.setclientinfo(property string, value string)\n"
4226 : " external name clients.setinfo;\n"
4227 : "grant execute on procedure sys.setclientinfo(string, string) to public;\n"
4228 : "create table sys.clientinfo_properties(prop varchar(40) NOT NULL, session_attr varchar(40) NOT NULL);\n"
4229 : "insert into sys.clientinfo_properties values\n"
4230 : " ('ClientHostname', 'hostname'),\n"
4231 : " ('ApplicationName', 'application'),\n"
4232 : " ('ClientLibrary', 'client'),\n"
4233 : " ('ClientPid', 'clientpid'),\n"
4234 : " ('ClientRemark', 'remark');\n"
4235 : "grant select on sys.clientinfo_properties to public;\n"
4236 : "update sys.functions set system = true where schema_id = 2000 and name in ('setclientinfo', 'sessions');\n"
4237 : "update sys._tables set system = true where schema_id = 2000 and name in ('clientinfo_properties', 'sessions');\n";
4238 :
4239 8 : t = mvc_bind_table(sql, s, "sessions");
4240 8 : t->system = 0; /* make it non-system else the drop view will fail */
4241 8 : printf("Running database upgrade commands:\n%s\n", query);
4242 8 : fflush(stdout);
4243 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4244 : }
4245 8 : if (err == MAL_SUCCEED) {
4246 8 : const char query[] = "alter table sys.clientinfo_properties SET READ ONLY;\n";
4247 8 : printf("Running database upgrade commands:\n%s\n", query);
4248 8 : fflush(stdout);
4249 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4250 : }
4251 8 : if (err == MAL_SUCCEED) {
4252 8 : const char query[] =
4253 : "DROP TABLE sys.key_types;\n"
4254 : "CREATE TABLE sys.key_types (\n"
4255 : " key_type_id SMALLINT NOT NULL PRIMARY KEY,\n"
4256 : " key_type_name VARCHAR(35) NOT NULL UNIQUE);\n"
4257 : "INSERT INTO sys.key_types VALUES\n"
4258 : "(0, 'Primary Key'),\n"
4259 : "(1, 'Unique Key'),\n"
4260 : "(2, 'Foreign Key'),\n"
4261 : "(3, 'Unique Key With Nulls Not Distinct'),\n"
4262 : "(4, 'Check Constraint');\n"
4263 : "GRANT SELECT ON sys.key_types TO PUBLIC;\n"
4264 : "UPDATE sys._tables SET system = true WHERE schema_id = 2000 AND name = 'key_types';\n";
4265 8 : if ((t = mvc_bind_table(sql, s, "key_types")) != NULL)
4266 8 : t->system = 0;
4267 8 : printf("Running database upgrade commands:\n%s\n", query);
4268 8 : fflush(stdout);
4269 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4270 : }
4271 8 : if (err == MAL_SUCCEED) {
4272 8 : const char query[] = "ALTER TABLE sys.key_types SET READ ONLY;\n";
4273 8 : printf("Running database upgrade commands:\n%s\n", query);
4274 8 : fflush(stdout);
4275 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4276 : }
4277 8 : if (err == MAL_SUCCEED) {
4278 8 : const char query[] =
4279 : "DROP VIEW information_schema.check_constraints CASCADE;\n"
4280 : "DROP VIEW information_schema.table_constraints CASCADE;\n"
4281 : "CREATE VIEW INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS SELECT\n"
4282 : " cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
4283 : " s.\"name\" AS CONSTRAINT_SCHEMA,\n"
4284 : " k.\"name\" AS CONSTRAINT_NAME,\n"
4285 : " cast(sys.check_constraint(s.\"name\", k.\"name\") AS varchar(2048)) AS CHECK_CLAUSE,\n"
4286 : " t.\"schema_id\" AS schema_id,\n"
4287 : " t.\"id\" AS table_id,\n"
4288 : " t.\"name\" AS table_name,\n"
4289 : " k.\"id\" AS key_id\n"
4290 : " FROM (SELECT sk.\"id\", sk.\"table_id\", sk.\"name\" FROM sys.\"keys\" sk WHERE sk.\"type\" = 4 UNION ALL SELECT tk.\"id\", tk.\"table_id\", tk.\"name\" FROM tmp.\"keys\" tk WHERE tk.\"type\" = 4) k\n"
4291 : " INNER JOIN (SELECT st.\"id\", st.\"schema_id\", st.\"name\" FROM sys.\"_tables\" st UNION ALL SELECT tt.\"id\", tt.\"schema_id\", tt.\"name\" FROM tmp.\"_tables\" tt) t ON k.\"table_id\" = t.\"id\"\n"
4292 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
4293 : " ORDER BY s.\"name\", t.\"name\", k.\"name\";\n"
4294 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.CHECK_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
4295 :
4296 : "CREATE VIEW INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS SELECT\n"
4297 : " cast(NULL AS varchar(1)) AS CONSTRAINT_CATALOG,\n"
4298 : " s.\"name\" AS CONSTRAINT_SCHEMA,\n"
4299 : " k.\"name\" AS CONSTRAINT_NAME,\n"
4300 : " cast(NULL AS varchar(1)) AS TABLE_CATALOG,\n"
4301 : " s.\"name\" AS TABLE_SCHEMA,\n"
4302 : " t.\"name\" AS TABLE_NAME,\n"
4303 : " cast(CASE k.\"type\" WHEN 0 THEN 'PRIMARY KEY' WHEN 1 THEN 'UNIQUE' WHEN 2 THEN 'FOREIGN KEY' WHEN 3 THEN 'UNIQUE NULLS NOT DISTINCT' WHEN 4 THEN 'CHECK' ELSE NULL END AS varchar(26)) AS CONSTRAINT_TYPE,\n"
4304 : " cast('NO' AS varchar(3)) AS IS_DEFERRABLE,\n"
4305 : " cast('NO' AS varchar(3)) AS INITIALLY_DEFERRED,\n"
4306 : " cast('YES' AS varchar(3)) AS ENFORCED,\n"
4307 : " t.\"schema_id\" AS schema_id,\n"
4308 : " t.\"id\" AS table_id,\n"
4309 : " k.\"id\" AS key_id,\n"
4310 : " k.\"type\" AS key_type,\n"
4311 : " t.\"system\" AS is_system\n"
4312 : " FROM (SELECT sk.\"id\", sk.\"table_id\", sk.\"name\", sk.\"type\" FROM sys.\"keys\" sk UNION ALL SELECT tk.\"id\", tk.\"table_id\", tk.\"name\", tk.\"type\" FROM tmp.\"keys\" tk) k\n"
4313 : " INNER JOIN (SELECT st.\"id\", st.\"schema_id\", st.\"name\", st.\"system\" FROM sys.\"_tables\" st UNION ALL SELECT tt.\"id\", tt.\"schema_id\", tt.\"name\", tt.\"system\" FROM tmp.\"_tables\" tt) t ON k.\"table_id\" = t.\"id\"\n"
4314 : " INNER JOIN sys.\"schemas\" s ON t.\"schema_id\" = s.\"id\"\n"
4315 : " ORDER BY s.\"name\", t.\"name\", k.\"name\";\n"
4316 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.TABLE_CONSTRAINTS TO PUBLIC WITH GRANT OPTION;\n"
4317 : "\n"
4318 : "UPDATE sys._tables SET system = true where system <> true\n"
4319 : " and schema_id = (select s.id from sys.schemas s where s.name = 'information_schema')\n"
4320 : " and name in ('check_constraints','table_constraints');\n";
4321 8 : sql_schema *infoschema = mvc_bind_schema(sql, "information_schema");
4322 8 : if ((t = mvc_bind_table(sql, infoschema, "check_constraints")) != NULL)
4323 8 : t->system = 0; /* make it non-system else the drop view will fail */
4324 8 : if ((t = mvc_bind_table(sql, infoschema, "table_constraints")) != NULL)
4325 8 : t->system = 0;
4326 8 : printf("Running database upgrade commands:\n%s\n", query);
4327 8 : fflush(stdout);
4328 8 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4329 : }
4330 8 : if (err == MAL_SUCCEED) {
4331 8 : sql_subtype tp;
4332 8 : sql_find_subtype(&tp, "varchar", 0, 0);
4333 8 : if (sql_bind_func(sql, s->base.name, "gzcompress", &tp, &tp, F_PROC, true, true)) {
4334 2 : const char query[] =
4335 : "drop procedure if exists sys.gzcompress(string, string);\n"
4336 : "drop procedure if exists sys.gzdecompress(string, string);\n"
4337 : "drop procedure if exists sys.gztruncate(string, string);\n"
4338 : "drop procedure if exists sys.gzexpand(string, string);\n";
4339 2 : printf("Running database upgrade commands:\n%s\n", query);
4340 2 : fflush(stdout);
4341 2 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4342 : } else {
4343 6 : sql->session->status = 0;
4344 6 : sql->errstr[0] = '\0';
4345 : }
4346 : }
4347 8 : if (err == MAL_SUCCEED) {
4348 8 : sql_subtype tp;
4349 8 : sql_find_subtype(&tp, "varchar", 0, 0);
4350 8 : if (sql_bind_func(sql, s->base.name, "newdictionary", &tp, &tp, F_PROC, true, true)) {
4351 2 : const char query[] =
4352 : "drop procedure if exists sys.newdictionary(string, string);\n"
4353 : "drop procedure if exists sys.dropdictionary(string, string);\n"
4354 : "drop procedure if exists sys.initializedictionary();\n";
4355 2 : printf("Running database upgrade commands:\n%s\n", query);
4356 2 : fflush(stdout);
4357 2 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4358 : } else {
4359 6 : sql->session->status = 0;
4360 6 : sql->errstr[0] = '\0';
4361 : }
4362 : }
4363 : }
4364 115 : BBPunfix(b->batCacheid);
4365 : }
4366 115 : res_table_destroy(output);
4367 :
4368 115 : return err;
4369 : }
4370 :
4371 : static str
4372 115 : sql_update_default_geom(Client c, mvc *sql, sql_schema *s)
4373 : {
4374 115 : str err;
4375 115 : res_table *output = NULL;
4376 :
4377 115 : if ((err = SQLstatementIntern(c, "select * from sys._tables where name = 'geometry_columns' and schema_id = 2000 and query like '%''wkba''%';\n", "update", true, false, &output)) == MAL_SUCCEED) {
4378 115 : BAT *b;
4379 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) > 0) {
4380 16 : sql_table *t = mvc_bind_table(sql, s, "geometry_columns");
4381 16 : t->system = 0;
4382 16 : const char query[] = "drop view sys.geometry_columns;\n"
4383 : "create view sys.geometry_columns as\n"
4384 : " select cast(null as varchar(1)) as f_table_catalog,\n"
4385 : " s.name as f_table_schema,\n"
4386 : " t.name as f_table_name,\n"
4387 : " c.name as f_geometry_column,\n"
4388 : " cast(has_z(c.type_digits) + has_m(c.type_digits) +2 as integer) as coord_dimension,\n"
4389 : " c.type_scale as srid,\n"
4390 : " get_type(c.type_digits, 0) as geometry_type\n"
4391 : " from sys.columns c, sys.tables t, sys.schemas s\n"
4392 : " where c.table_id = t.id and t.schema_id = s.id\n"
4393 : " and c.type in (select sqlname from sys.types where systemname = 'wkb');\n"
4394 : "GRANT SELECT ON sys.geometry_columns TO PUBLIC;\n"
4395 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name = 'geometry_columns';\n"
4396 : /* also clean up sys.privileges after drop of
4397 : * sys.st_interiorrings that was done in C in
4398 : * bat_logger.c */
4399 : "delete from sys.privileges where (obj_id) not in (select id from (SELECT id FROM sys.schemas UNION ALL SELECT id FROM sys._tables UNION ALL SELECT id FROM sys._columns UNION ALL SELECT id FROM sys.functions) as t);\n";
4400 16 : printf("Running database upgrade commands:\n%s\n", query);
4401 16 : fflush(stdout);
4402 16 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4403 : }
4404 115 : res_table_destroy(output);
4405 : }
4406 115 : return err;
4407 : }
4408 :
4409 : static str
4410 115 : sql_update_default(Client c, mvc *sql, sql_schema *s)
4411 : {
4412 115 : char *err = MAL_SUCCEED;
4413 115 : sql_subtype tp;
4414 115 : res_table *output = NULL;
4415 :
4416 115 : sql_find_subtype(&tp, "varchar", 0, 0);
4417 115 : if (!sql_bind_func(sql, s->base.name, "vacuum", &tp, &tp, F_PROC, true, true)) {
4418 16 : sql->session->status = 0; /* if the function was not found clean the error */
4419 16 : sql->errstr[0] = '\0';
4420 16 : sql_table *t;
4421 16 : t = mvc_bind_table(sql, s, "ids");
4422 16 : t->system = 0;
4423 16 : t = mvc_bind_table(sql, s, "dependencies_vw");
4424 16 : t->system = 0;
4425 16 : const char query[] =
4426 : "create procedure sys.vacuum(sname string, tname string)\n"
4427 : "external name sql.vacuum;\n"
4428 : "create procedure sys.vacuum(sname string, tname string, interval int)\n"
4429 : "external name sql.vacuum;\n"
4430 : "create procedure sys.stop_vacuum(sname string, tname string)\n"
4431 : "external name sql.stop_vacuum;\n"
4432 : "create function sys.unclosed_result_sets()\n"
4433 : "returns table(\n"
4434 : " \"query_id\" oid,\n"
4435 : " \"res_id\" int\n"
4436 : ")\n"
4437 : "external name sql.unclosed_result_sets;\n"
4438 : "grant execute on function sys.unclosed_result_sets() to public;\n"
4439 : "update sys.functions set system = true where system <> true and schema_id = 2000 and name in ('vacuum', 'stop_vacuum', 'unclosed_result_sets');\n"
4440 : "drop view sys.dependencies_vw cascade;\n"
4441 : "drop view sys.ids cascade;\n"
4442 : "CREATE VIEW sys.ids (id, name, schema_id, table_id, table_name, obj_type, sys_table, system) AS\n"
4443 : "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'author' AS obj_type, 'sys.auths' AS sys_table, (name in ('public','sysadmin','monetdb','.snapshot')) AS system FROM sys.auths UNION ALL\n"
4444 : "SELECT id, name, cast(null as int) as schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, ifthenelse(system, 'system schema', 'schema'), 'sys.schemas', system FROM sys.schemas UNION ALL\n"
4445 : "SELECT t.id, name, t.schema_id, t.id as table_id, t.name as table_name, cast(lower(tt.table_type_name) as varchar(40)), 'sys.tables', t.system FROM sys.tables t left outer join sys.table_types tt on t.type = tt.table_type_id UNION ALL\n"
4446 : "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, ifthenelse(t.system, 'system column', 'column'), 'sys._columns', t.system FROM sys._columns c JOIN sys._tables t ON c.table_id = t.id UNION ALL\n"
4447 : "SELECT c.id, c.name, t.schema_id, c.table_id, t.name as table_name, 'column', 'tmp._columns', t.system FROM tmp._columns c JOIN tmp._tables t ON c.table_id = t.id UNION ALL\n"
4448 : "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, ifthenelse(t.system, 'system key', 'key'), 'sys.keys', t.system FROM sys.keys k JOIN sys._tables t ON k.table_id = t.id UNION ALL\n"
4449 : "SELECT k.id, k.name, t.schema_id, k.table_id, t.name as table_name, 'key', 'tmp.keys', t.system FROM tmp.keys k JOIN tmp._tables t ON k.table_id = t.id UNION ALL\n"
4450 : "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, ifthenelse(t.system, 'system index', 'index'), 'sys.idxs', t.system FROM sys.idxs i JOIN sys._tables t ON i.table_id = t.id UNION ALL\n"
4451 : "SELECT i.id, i.name, t.schema_id, i.table_id, t.name as table_name, 'index' , 'tmp.idxs', t.system FROM tmp.idxs i JOIN tmp._tables t ON i.table_id = t.id UNION ALL\n"
4452 : "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, ifthenelse(t.system, 'system trigger', 'trigger'), 'sys.triggers', t.system FROM sys.triggers g JOIN sys._tables t ON g.table_id = t.id UNION ALL\n"
4453 : "SELECT g.id, g.name, t.schema_id, g.table_id, t.name as table_name, 'trigger', 'tmp.triggers', t.system FROM tmp.triggers g JOIN tmp._tables t ON g.table_id = t.id UNION ALL\n"
4454 : "SELECT f.id, f.name, f.schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) as varchar(40)), 'sys.functions', f.system FROM sys.functions f left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
4455 : "SELECT a.id, a.name, f.schema_id, a.func_id as table_id, f.name as table_name, cast(ifthenelse(f.system, 'system ', '') || lower(ft.function_type_keyword) || ' arg' as varchar(44)), 'sys.args', f.system FROM sys.args a JOIN sys.functions f ON a.func_id = f.id left outer join sys.function_types ft on f.type = ft.function_type_id UNION ALL\n"
4456 : "SELECT id, name, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'sequence', 'sys.sequences', false FROM sys.sequences UNION ALL\n"
4457 : "SELECT o.id, o.name, pt.schema_id, pt.id, pt.name, 'partition of merge table', 'sys.objects', false FROM sys.objects o JOIN sys._tables pt ON o.sub = pt.id JOIN sys._tables mt ON o.nr = mt.id WHERE mt.type = 3 UNION ALL\n"
4458 : "SELECT id, sqlname, schema_id, cast(null as int) as table_id, cast(null as varchar(124)) as table_name, 'type', 'sys.types', (sqlname in ('inet','json','url','uuid')) FROM sys.types\n"
4459 : " ORDER BY id;\n"
4460 : "GRANT SELECT ON sys.ids TO PUBLIC;\n"
4461 : "CREATE VIEW sys.dependencies_vw AS\n"
4462 : "SELECT d.id, i1.obj_type, i1.name,\n"
4463 : " d.depend_id as used_by_id, i2.obj_type as used_by_obj_type, i2.name as used_by_name,\n"
4464 : " d.depend_type, dt.dependency_type_name\n"
4465 : " FROM sys.dependencies d\n"
4466 : " JOIN sys.ids i1 ON d.id = i1.id\n"
4467 : " JOIN sys.ids i2 ON d.depend_id = i2.id\n"
4468 : " JOIN sys.dependency_types dt ON d.depend_type = dt.dependency_type_id\n"
4469 : " ORDER BY id, depend_id;\n"
4470 : "GRANT SELECT ON sys.dependencies_vw TO PUBLIC;\n"
4471 : "update sys._tables set system = true where system <> true and schema_id = 2000 and name in ('ids', 'dependencies_vw');\n";
4472 16 : printf("Running database upgrade commands:\n%s\n", query);
4473 16 : fflush(stdout);
4474 16 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4475 16 : if (err)
4476 0 : return err;
4477 : }
4478 :
4479 115 : if ((err = SQLstatementIntern(c, "select id from sys.functions where name = 'quantile' and schema_id = 2000 and contains(func, 'ordered');\n", "update", true, false, &output)) == MAL_SUCCEED) {
4480 115 : BAT *b;
4481 115 : if ((b = BBPquickdesc(output->cols[0].b)) && BATcount(b) == 0) {
4482 16 : sql_table *t;
4483 16 : t = mvc_bind_table(sql, s, "describe_comments");
4484 16 : t->system = 0;
4485 16 : t = mvc_bind_table(sql, s, "describe_functions");
4486 16 : t->system = 0;
4487 16 : t = mvc_bind_table(sql, s, "describe_privileges");
4488 16 : t->system = 0;
4489 16 : t = mvc_bind_table(sql, s, "dump_comments");
4490 16 : t->system = 0;
4491 16 : t = mvc_bind_table(sql, s, "dump_functions");
4492 16 : t->system = 0;
4493 16 : t = mvc_bind_table(sql, s, "fully_qualified_functions");
4494 16 : t->system = 0;
4495 16 : sql_schema *is = mvc_bind_schema(sql, "information_schema");
4496 16 : t = mvc_bind_table(sql, is, "parameters");
4497 16 : t->system = 0;
4498 16 : char query[] = "update sys.functions set func = replace(func, E'\\n external', E' ordered\\n external') where name in ('quantile','quantile_avg','median','median_avg') and schema_id = 2000 and language = (select language_id from sys.function_languages where language_name = 'MAL') and type = (select function_type_id from sys.function_types where function_type_keyword = 'AGGREGATE');\n"
4499 : "update sys.functions set func = replace(func, E'\\n\\texternal', E' ordered\\n external') where name in ('quantile','quantile_avg','median','median_avg') and schema_id = 2000 and language = (select language_id from sys.function_languages where language_name = 'MAL') and type = (select function_type_id from sys.function_types where function_type_keyword = 'AGGREGATE');\n"
4500 : "update sys.functions set func = replace(func, E'\\nexternal', E' ordered\\n external') where name in ('quantile','quantile_avg','median','median_avg') and schema_id = 2000 and language = (select language_id from sys.function_languages where language_name = 'MAL') and type = (select function_type_id from sys.function_types where function_type_keyword = 'AGGREGATE');\n"
4501 : "update sys.functions set func = replace(func, E' external', E' with order\\n external') where name = 'group_concat' and schema_id = 2000 and language = (select language_id from sys.function_languages where language_name = 'MAL') and type = (select function_type_id from sys.function_types where function_type_keyword = 'AGGREGATE');\n"
4502 : "drop function sys.dump_database(boolean) cascade;\n"
4503 : "drop view sys.dump_functions cascade;\n"
4504 : "drop view sys.dump_comments cascade;\n"
4505 : "drop view sys.describe_comments cascade;\n"
4506 : "drop view sys.describe_privileges cascade;\n"
4507 : "drop view sys.fully_qualified_functions cascade;\n"
4508 : "drop view sys.describe_functions cascade;\n"
4509 : "CREATE VIEW sys.fully_qualified_functions AS\n"
4510 : " SELECT\n"
4511 : " f.id id,\n"
4512 : " ft.function_type_keyword tpe,\n"
4513 : " sys.fqn(s.name, f.name) || '(' || group_concat(sys.describe_type(a.type, a.type_digits, a.type_scale), ',' order by a.number) || ')' nme\n"
4514 : " FROM sys.schemas s, sys.function_types ft, sys.functions f JOIN sys.args a ON f.id = a.func_id\n"
4515 : " WHERE s.id= f.schema_id AND f.type = ft.function_type_id\n"
4516 : " group by f.id, ft.function_type_keyword, f.name, s.name\n"
4517 : " UNION\n"
4518 : " SELECT f.id id,\n"
4519 : " ft.function_type_keyword tpe,\n"
4520 : " sys.fqn(s.name, f.name) || '()' nme\n"
4521 : " FROM sys.schemas s, sys.function_types ft, sys.functions f\n"
4522 : " WHERE s.id= f.schema_id AND f.type = ft.function_type_id and f.id not in ( select func_id from sys.args )\n"
4523 : " group by f.id, ft.function_type_keyword, f.name, s.name;\n"
4524 : "CREATE VIEW sys.describe_comments AS\n"
4525 : " SELECT o.id AS id, o.tpe AS tpe, o.nme AS fqn, cm.remark AS rem\n"
4526 : " FROM (\n"
4527 : " SELECT id, 'SCHEMA', sys.DQ(name) FROM sys.schemas WHERE NOT system\n"
4528 : " UNION ALL\n"
4529 : " SELECT t.id, ifthenelse(ts.table_type_name = 'VIEW', 'VIEW', 'TABLE'), sys.FQN(s.name, t.name)\n"
4530 : " FROM sys.schemas s JOIN sys._tables t ON s.id = t.schema_id JOIN sys.table_types ts ON t.type = ts.table_type_id\n"
4531 : " WHERE NOT t.system\n"
4532 : " UNION ALL\n"
4533 : " SELECT c.id, 'COLUMN', sys.FQN(s.name, t.name) || '.' || sys.DQ(c.name) FROM sys.columns c, sys._tables t, sys.schemas s WHERE NOT t.system AND c.table_id = t.id AND t.schema_id = s.id\n"
4534 : " UNION ALL\n"
4535 : " SELECT idx.id, 'INDEX', sys.FQN(s.name, idx.name) FROM sys.idxs idx, sys._tables t, sys.schemas s WHERE NOT t.system AND idx.table_id = t.id AND t.schema_id = s.id\n"
4536 : " UNION ALL\n"
4537 : " SELECT seq.id, 'SEQUENCE', sys.FQN(s.name, seq.name) FROM sys.sequences seq, sys.schemas s WHERE seq.schema_id = s.id\n"
4538 : " UNION ALL\n"
4539 : " SELECT f.id, ft.function_type_keyword, qf.nme FROM sys.functions f, sys.function_types ft, sys.schemas s, sys.fully_qualified_functions qf\n"
4540 : " WHERE NOT f.system AND f.type = ft.function_type_id AND f.schema_id = s.id AND qf.id = f.id\n"
4541 : " ) AS o(id, tpe, nme)\n"
4542 : " JOIN sys.comments cm ON cm.id = o.id;\n"
4543 : "CREATE VIEW sys.describe_privileges AS\n"
4544 : " SELECT\n"
4545 : " CASE\n"
4546 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'SELECT' THEN\n"
4547 : " 'COPY FROM'\n"
4548 : " WHEN o.tpe IS NULL AND pc.privilege_code_name = 'UPDATE' THEN\n"
4549 : " 'COPY INTO'\n"
4550 : " ELSE\n"
4551 : " o.nme\n"
4552 : " END o_nme,\n"
4553 : " coalesce(o.tpe, 'GLOBAL') o_tpe,\n"
4554 : " pc.privilege_code_name p_nme,\n"
4555 : " a.name a_nme,\n"
4556 : " g.name g_nme,\n"
4557 : " p.grantable grantable\n"
4558 : " FROM\n"
4559 : " sys.privileges p LEFT JOIN\n"
4560 : " (\n"
4561 : " SELECT t.id, s.name || '.' || t.name , 'TABLE'\n"
4562 : " from sys.schemas s, sys.tables t where s.id = t.schema_id\n"
4563 : " UNION ALL\n"
4564 : " SELECT c.id, s.name || '.' || t.name || '.' || c.name, 'COLUMN'\n"
4565 : " FROM sys.schemas s, sys.tables t, sys.columns c where s.id = t.schema_id AND t.id = c.table_id\n"
4566 : " UNION ALL\n"
4567 : " SELECT f.id, f.nme, f.tpe\n"
4568 : " FROM sys.fully_qualified_functions f\n"
4569 : " ) o(id, nme, tpe) ON o.id = p.obj_id,\n"
4570 : " sys.privilege_codes pc,\n"
4571 : " auths a, auths g\n"
4572 : " WHERE\n"
4573 : " p.privileges = pc.privilege_code_id AND\n"
4574 : " p.auth_id = a.id AND\n"
4575 : " p.grantor = g.id;\n"
4576 : "CREATE VIEW sys.describe_functions AS\n"
4577 : " WITH func_args(func_id, func_arg) AS\n"
4578 : " (\n"
4579 : " SELECT\n"
4580 : " func_id,\n"
4581 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ' order by number)\n"
4582 : " FROM sys.args\n"
4583 : " WHERE inout = 1\n"
4584 : " group by func_id\n"
4585 : " ),\n"
4586 : " func_rets(func_id, func_ret, func_ret_type) AS\n"
4587 : " (\n"
4588 : " SELECT\n"
4589 : " func_id,\n"
4590 : " group_concat(sys.dq(name) || ' ' || sys.describe_type(type, type_digits, type_scale),', ' order by number),\n"
4591 : " group_concat(sys.describe_type(type, type_digits, type_scale),', ' order by number)\n"
4592 : " FROM sys.args\n"
4593 : " WHERE inout = 0\n"
4594 : " group by func_id\n"
4595 : " )\n"
4596 : " SELECT\n"
4597 : " f.id o,\n"
4598 : " s.name sch,\n"
4599 : " f.name fun,\n"
4600 : " CASE WHEN f.language IN (1, 2) THEN f.func ELSE 'CREATE ' || ft.function_type_keyword || ' ' || sys.FQN(s.name, f.name) || '(' || coalesce(fa.func_arg, '') || ')' || CASE WHEN f.type = 5 THEN ' RETURNS TABLE (' || coalesce(fr.func_ret, '') || ')' WHEN f.type IN (1,3) THEN ' RETURNS ' || fr.func_ret_type ELSE '' END || CASE WHEN fl.language_keyword IS NULL THEN '' ELSE ' LANGUAGE ' || fl.language_keyword END || ' ' || f.func END def\n"
4601 : " FROM sys.functions f\n"
4602 : " LEFT OUTER JOIN func_args fa ON fa.func_id = f.id\n"
4603 : " LEFT OUTER JOIN func_rets fr ON fr.func_id = f.id\n"
4604 : " JOIN sys.schemas s ON f.schema_id = s.id\n"
4605 : " JOIN sys.function_types ft ON f.type = ft.function_type_id\n"
4606 : " LEFT OUTER JOIN sys.function_languages fl ON f.language = fl.language_id\n"
4607 : " WHERE s.name <> 'tmp' AND NOT f.system;\n"
4608 : "GRANT SELECT ON sys.describe_comments TO PUBLIC;\n"
4609 : "GRANT SELECT ON sys.fully_qualified_functions TO PUBLIC;\n"
4610 : "GRANT SELECT ON sys.describe_privileges TO PUBLIC;\n"
4611 : "GRANT SELECT ON sys.describe_functions TO PUBLIC;\n"
4612 : "CREATE VIEW sys.dump_functions AS\n"
4613 : " SELECT f.o o, sys.schema_guard(f.sch, f.fun, f.def) stmt,\n"
4614 : " f.sch schema_name,\n"
4615 : " f.fun function_name\n"
4616 : " FROM sys.describe_functions f;\n"
4617 : "CREATE VIEW sys.dump_comments AS\n"
4618 : " SELECT 'COMMENT ON ' || c.tpe || ' ' || c.fqn || ' IS ' || sys.SQ(c.rem) || ';' stmt FROM sys.describe_comments c;\n"
4619 : "CREATE FUNCTION sys.dump_database(describe BOOLEAN) RETURNS TABLE(o int, stmt STRING)\n"
4620 : "BEGIN\n"
4621 : " SET SCHEMA sys;\n"
4622 : " TRUNCATE sys.dump_statements;\n"
4623 : " INSERT INTO sys.dump_statements VALUES (1, 'START TRANSACTION;');\n"
4624 : " INSERT INTO sys.dump_statements VALUES (2, 'SET SCHEMA \"sys\";');\n"
4625 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_roles;\n"
4626 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_users;\n"
4627 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_create_schemas;\n"
4628 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_user_defined_types;\n"
4629 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_add_schemas_to_users;\n"
4630 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_grant_user_privileges;\n"
4631 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_sequences;\n"
4632 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(ORDER BY stmts.o), stmts.s\n"
4633 : " FROM (\n"
4634 : " SELECT f.o, f.stmt FROM sys.dump_functions f\n"
4635 : " UNION ALL\n"
4636 : " SELECT t.o, t.stmt FROM sys.dump_tables t\n"
4637 : " ) AS stmts(o, s);\n"
4638 : " IF NOT DESCRIBE THEN\n"
4639 : " CALL sys.dump_table_data();\n"
4640 : " END IF;\n"
4641 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_start_sequences;\n"
4642 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_defaults;\n"
4643 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_constraint_type;\n"
4644 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_indices;\n"
4645 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_foreign_keys;\n"
4646 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_partition_tables;\n"
4647 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_triggers;\n"
4648 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_comments;\n"
4649 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_table_grants;\n"
4650 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_column_grants;\n"
4651 : " INSERT INTO sys.dump_statements SELECT (SELECT COUNT(*) FROM sys.dump_statements) + RANK() OVER(), stmt FROM sys.dump_function_grants;\n"
4652 : " INSERT INTO sys.dump_statements VALUES ((SELECT COUNT(*) FROM sys.dump_statements) + 1, 'COMMIT;');\n"
4653 : " RETURN sys.dump_statements;\n"
4654 : "END;\n"
4655 : "update sys.functions set system = true where not system and schema_id = 2000 and name = 'dump_database';\n"
4656 : "update sys._tables set system = true where not system and schema_id = 2000 and name in ('dump_comments', 'dump_functions', 'describe_functions', 'describe_privileges', 'describe_comments', 'fully_qualified_functions');\n"
4657 : "drop view information_schema.parameters cascade;\n"
4658 : "CREATE VIEW INFORMATION_SCHEMA.PARAMETERS AS SELECT\n"
4659 : " cast(NULL AS varchar(1)) AS SPECIFIC_CATALOG,\n"
4660 : " s.\"name\" AS SPECIFIC_SCHEMA,\n"
4661 : " cast(f.\"name\"||'('||f.\"id\"||')' AS varchar(270)) AS SPECIFIC_NAME, -- TODO: replace with full routine signature string. Note sys.fully_qualified_functions.nme does not produce the correct signature.\n"
4662 : " cast(sys.ifthenelse((a.\"inout\" = 0 OR f.\"type\" = 2), 1 + a.\"number\", sys.ifthenelse(f.\"type\" = 1, a.\"number\", (1 + a.\"number\" - f.count_out_cols))) AS int) AS ORDINAL_POSITION,\n"
4663 : " cast(sys.ifthenelse(a.\"inout\" = 0, 'OUT', sys.ifthenelse(a.\"inout\" = 1, 'IN', 'INOUT')) as varchar(5)) AS PARAMETER_MODE, -- we do not yet support INOUT\n"
4664 : " cast(sys.ifthenelse(a.\"inout\" = 0, 'YES', 'NO') as varchar(3)) AS IS_RESULT,\n"
4665 : " cast(NULL AS varchar(1)) AS AS_LOCATOR,\n"
4666 : " a.\"name\" AS PARAMETER_NAME,\n"
4667 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_CATALOG,\n"
4668 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_SCHEMA,\n"
4669 : " cast(NULL AS varchar(1)) AS FROM_SQL_SPECIFIC_NAME,\n"
4670 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_CATALOG,\n"
4671 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_SCHEMA,\n"
4672 : " cast(NULL AS varchar(1)) AS TO_SQL_SPECIFIC_NAME,\n"
4673 : " cast(sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true) AS varchar(1024)) AS DATA_TYPE,\n"
4674 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, a.\"type_digits\", NULL) AS int) AS CHARACTER_MAXIMUM_LENGTH,\n"
4675 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml') AND a.\"type_digits\" > 0, 4 * cast(a.\"type_digits\" as bigint), NULL) AS bigint) AS CHARACTER_OCTET_LENGTH,\n"
4676 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_CATALOG,\n"
4677 : " cast(NULL AS varchar(1)) AS CHARACTER_SET_SCHEMA,\n"
4678 : " cast(sys.ifthenelse(a.\"type\" IN ('varchar','clob','char','json','url','xml'), 'UTF-8', NULL) AS varchar(16)) AS CHARACTER_SET_NAME,\n"
4679 : " cast(NULL AS varchar(1)) AS COLLATION_CATALOG,\n"
4680 : " cast(NULL AS varchar(1)) AS COLLATION_SCHEMA,\n"
4681 : " cast(NULL AS varchar(1)) AS COLLATION_NAME,\n"
4682 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_digits\", NULL) AS int) AS NUMERIC_PRECISION,\n"
4683 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','oid'), 2, sys.ifthenelse(a.\"type\" IN ('decimal','numeric'), 10, NULL)) AS int) AS NUMERIC_PRECISION_RADIX,\n"
4684 : " cast(sys.ifthenelse(a.\"type\" IN ('int','smallint','tinyint','bigint','hugeint','float','real','double','decimal','numeric','oid'), a.\"type_scale\", NULL) AS int) AS NUMERIC_SCALE,\n"
4685 : " cast(sys.ifthenelse(a.\"type\" IN ('date','timestamp','timestamptz','time','timetz'), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\" -1, 0), NULL) AS int) AS DATETIME_PRECISION,\n"
4686 : " cast(sys.ifthenelse(a.\"type\" IN ('day_interval','month_interval','sec_interval'), sys.\"sql_datatype\"(a.\"type\", a.\"type_digits\", a.\"type_scale\", true, true), NULL) AS varchar(40)) AS INTERVAL_TYPE,\n"
4687 : " cast(CASE a.\"type\" WHEN 'day_interval' THEN 0 WHEN 'month_interval' THEN 0 WHEN 'sec_interval' THEN (sys.ifthenelse(a.\"type_digits\" IN (7, 10, 12, 13), sys.ifthenelse(a.\"type_scale\" > 0, a.\"type_scale\", 3), 0)) ELSE NULL END AS int) AS INTERVAL_PRECISION,\n"
4688 : " cast(NULL AS varchar(1)) AS UDT_CATALOG,\n"
4689 : " cast(NULL AS varchar(1)) AS UDT_SCHEMA,\n"
4690 : " cast(NULL AS varchar(1)) AS UDT_NAME,\n"
4691 : " cast(NULL AS varchar(1)) AS SCOPE_CATALOG,\n"
4692 : " cast(NULL AS varchar(1)) AS SCOPE_SCHEMA,\n"
4693 : " cast(NULL AS varchar(1)) AS SCOPE_NAME,\n"
4694 : " cast(NULL AS int) AS MAXIMUM_CARDINALITY,\n"
4695 : " cast(NULL AS varchar(1)) AS DTD_IDENTIFIER,\n"
4696 : " cast(NULL AS varchar(1)) AS DECLARED_DATA_TYPE,\n"
4697 : " cast(NULL AS int) AS DECLARED_NUMERIC_PRECISION,\n"
4698 : " cast(NULL AS int) AS DECLARED_NUMERIC_SCALE,\n"
4699 : " cast(NULL AS varchar(1)) AS PARAMETER_DEFAULT,\n"
4700 : " -- MonetDB column extensions\n"
4701 : " f.\"schema_id\" AS schema_id,\n"
4702 : " f.\"id\" AS function_id,\n"
4703 : " a.\"id\" AS arg_id,\n"
4704 : " f.\"name\" AS function_name,\n"
4705 : " f.\"type\" AS function_type,\n"
4706 : " f.\"system\" AS is_system\n"
4707 : " FROM sys.\"args\" a\n"
4708 : " INNER JOIN (SELECT fun.id, fun.schema_id, fun.name, fun.type, fun.system, (select count(*) from sys.args a0 where a0.inout = 0 and a0.func_id = fun.id) as count_out_cols FROM sys.\"functions\" fun WHERE fun.\"type\" in (1, 2, 5, 7)) f ON f.\"id\" = a.\"func_id\"\n"
4709 : " INNER JOIN sys.\"schemas\" s ON s.\"id\" = f.\"schema_id\"\n"
4710 : " ORDER BY s.\"name\", f.\"name\", f.\"id\", a.\"inout\" DESC, a.\"number\";\n"
4711 : "GRANT SELECT ON TABLE INFORMATION_SCHEMA.PARAMETERS TO PUBLIC WITH GRANT OPTION;\n"
4712 : "update sys._tables set system = true where not system and schema_id = (select id from sys.schemas where name = 'information_schema') and name = 'parameters';\n";
4713 16 : printf("Running database upgrade commands:\n%s\n", query);
4714 16 : fflush(stdout);
4715 16 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4716 : }
4717 115 : res_table_destroy(output);
4718 115 : if (err)
4719 : return err;
4720 : }
4721 :
4722 115 : sql_find_subtype(&tp, "date", 0, 0);
4723 115 : if (!sql_bind_func(sql, s->base.name, "dayname", &tp, NULL, F_FUNC, true, true)) {
4724 16 : sql->session->status = 0; /* if the function was not found clean the error */
4725 16 : sql->errstr[0] = '\0';
4726 16 : const char query[] = "create function dayname(d date) returns varchar(10) return date_to_str(d, '%A');\n"
4727 : "create function monthname(d date) returns varchar(10) return date_to_str(d, '%B');\n"
4728 : "grant execute on function dayname(date) to public;\n"
4729 : "grant execute on function monthname(date) to public;\n"
4730 : "update sys.functions set system = true where system <> true and name in ('dayname', 'monthname') and schema_id = 2000 and type = (select function_type_id from sys.function_types where function_type_name = 'Scalar function');\n";
4731 16 : printf("Running database upgrade commands:\n%s\n", query);
4732 16 : fflush(stdout);
4733 16 : err = SQLstatementIntern(c, query, "update", true, false, NULL);
4734 16 : if (err)
4735 0 : return err;
4736 : }
4737 :
4738 : return err;
4739 : }
4740 :
4741 : int
4742 115 : SQLupgrades(Client c, mvc *m)
4743 : {
4744 115 : char *err;
4745 115 : sql_schema *s = mvc_bind_schema(m, "sys");
4746 :
4747 115 : if ((err = check_sys_tables(c, m, s)) != NULL) {
4748 0 : goto handle_error;
4749 : }
4750 :
4751 : #ifdef HAVE_HGE
4752 115 : sql_subtype tp;
4753 115 : sql_find_subtype(&tp, "hugeint", 0, 0);
4754 115 : if (!sql_bind_func(m, s->base.name, "var_pop", &tp, NULL, F_AGGR, true, true)) {
4755 8 : m->session->status = 0; /* if the function was not found clean the error */
4756 8 : m->errstr[0] = '\0';
4757 8 : if ((err = sql_update_hugeint(c, m)) != NULL) {
4758 0 : goto handle_error;
4759 : }
4760 : }
4761 : #endif
4762 :
4763 115 : if ((err = sql_update_jan2022(c, m)) != NULL) {
4764 0 : goto handle_error;
4765 : }
4766 :
4767 115 : if ((err = sql_update_sep2022(c, m, s)) != NULL) {
4768 0 : goto handle_error;
4769 : }
4770 :
4771 115 : if ((err = sql_update_jun2023(c, m, s)) != NULL) {
4772 0 : goto handle_error;
4773 : }
4774 :
4775 115 : if ((err = sql_update_dec2023_geom(c, m, s)) != NULL) {
4776 0 : goto handle_error;
4777 : }
4778 :
4779 115 : if ((err = sql_update_jun2023_sp3(c, m, s)) != NULL) {
4780 0 : goto handle_error;
4781 : }
4782 :
4783 115 : if ((err = sql_update_dec2023(c, m, s)) != NULL) {
4784 0 : goto handle_error;
4785 : }
4786 :
4787 115 : if ((err = sql_update_dec2023_sp1(c, m, s)) != NULL) {
4788 0 : goto handle_error;
4789 : }
4790 :
4791 115 : if ((err = sql_update_dec2023_sp4(c, m, s)) != NULL) {
4792 0 : goto handle_error;
4793 : }
4794 :
4795 115 : if ((err = sql_update_aug2024(c, m, s)) != NULL) {
4796 0 : goto handle_error;
4797 : }
4798 :
4799 115 : if ((err = sql_update_default(c, m, s)) != NULL) {
4800 0 : TRC_CRITICAL(SQL_PARSER, "%s\n", err);
4801 0 : goto handle_error;
4802 : }
4803 :
4804 115 : if ((err = sql_update_default_geom(c, m, s)) != NULL) {
4805 0 : TRC_CRITICAL(SQL_PARSER, "%s\n", err);
4806 0 : goto handle_error;
4807 : }
4808 :
4809 115 : return 0;
4810 :
4811 0 : handle_error:
4812 0 : GDKfatal("%s\n", err);
4813 : freeException(err);
4814 : return -1;
4815 : }
|