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