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 : #include "monetdb_config.h"
14 : #include "rel_trans.h"
15 : #include "rel_rel.h"
16 : #include "rel_basetable.h"
17 : #include "rel_select.h"
18 : #include "rel_updates.h"
19 : #include "rel_exp.h"
20 : #include "rel_schema.h"
21 : #include "rel_remote.h"
22 : #include "rel_psm.h"
23 : #include "rel_dump.h"
24 : #include "rel_propagate.h"
25 : #include "rel_unnest.h"
26 : #include "sql_parser.h"
27 : #include "sql_privileges.h"
28 : #include "sql_partition.h"
29 :
30 : #include "mal_authorize.h"
31 : #include "mal_exception.h"
32 :
33 : sql_rel *
34 33895 : rel_table(mvc *sql, int cat_type, const char *sname, sql_table *t, int nr)
35 : {
36 33895 : sql_rel *rel = rel_create(sql->sa);
37 33895 : list *exps = new_exp_list(sql->sa);
38 33895 : if (!rel || !exps)
39 : return NULL;
40 :
41 33895 : append(exps, exp_atom_int(sql->sa, nr));
42 33895 : append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
43 33895 : append(exps, exp_atom_str(sql->sa, t->base.name, sql_bind_localtype("str") ));
44 33895 : append(exps, exp_atom_ptr(sql->sa, t));
45 33895 : rel->l = rel_basetable(sql, t, t->base.name);
46 33895 : rel->r = NULL;
47 33895 : rel->op = op_ddl;
48 33895 : rel->flag = cat_type;
49 33895 : rel->exps = exps;
50 33895 : rel->card = CARD_MULTI;
51 33895 : rel->nrcols = 0;
52 33895 : return rel;
53 : }
54 :
55 : static sql_rel *
56 101 : rel_create_remote(mvc *sql, int cat_type, const char *sname, sql_table *t, int pw_encrypted, const char *username, const char *passwd)
57 : {
58 101 : sql_rel *rel = rel_create(sql->sa);
59 101 : list *exps = new_exp_list(sql->sa);
60 101 : if (!rel || !exps)
61 : return NULL;
62 :
63 101 : append(exps, exp_atom_int(sql->sa, pw_encrypted));
64 101 : append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
65 101 : append(exps, exp_atom_str(sql->sa, t->base.name, sql_bind_localtype("str") ));
66 101 : append(exps, exp_atom_ptr(sql->sa, t));
67 101 : append(exps, exp_atom_str(sql->sa, username, sql_bind_localtype("str") ));
68 101 : append(exps, exp_atom_str(sql->sa, passwd, sql_bind_localtype("str") ));
69 101 : rel->l = rel_basetable(sql, t, t->base.name);
70 101 : rel->r = NULL;
71 101 : rel->op = op_ddl;
72 101 : rel->flag = cat_type;
73 101 : rel->exps = exps;
74 101 : rel->card = CARD_MULTI;
75 101 : rel->nrcols = 0;
76 101 : return rel;
77 : }
78 :
79 : static sql_rel *
80 22358 : rel_create_view_ddl(mvc *sql, int cat_type, const char *sname, sql_table *t, int nr, int replace)
81 : {
82 22358 : sql_rel *rel = rel_table(sql, cat_type, sname, t, nr);
83 22358 : if (!rel)
84 : return NULL;
85 22358 : append(rel->exps, exp_atom_int(sql->sa, replace));
86 22358 : return rel;
87 : }
88 :
89 : static sql_rel *
90 2802 : rel_alter_table(allocator *sa, int cattype, char *sname, char *tname, char *sname2, char *tname2, int action)
91 : {
92 2802 : sql_rel *rel = rel_create(sa);
93 2802 : list *exps = new_exp_list(sa);
94 2802 : if (!rel || !exps)
95 : return NULL;
96 :
97 2802 : append(exps, exp_atom_clob(sa, sname));
98 2802 : append(exps, exp_atom_clob(sa, tname));
99 2802 : assert((sname2 && tname2) || (!sname2 && !tname2));
100 2802 : if (sname2) {
101 492 : append(exps, exp_atom_clob(sa, sname2));
102 492 : append(exps, exp_atom_clob(sa, tname2));
103 : }
104 2802 : append(exps, exp_atom_int(sa, action));
105 2802 : rel->l = NULL;
106 2802 : rel->r = NULL;
107 2802 : rel->op = op_ddl;
108 2802 : rel->flag = cattype;
109 2802 : rel->exps = exps;
110 2802 : rel->card = CARD_MULTI;
111 2802 : rel->nrcols = 0;
112 2802 : return rel;
113 : }
114 :
115 : static sql_rel *
116 12155 : view_rename_columns(mvc *sql, const char *name, sql_rel *sq, dlist *column_spec)
117 : {
118 12155 : dnode *n = column_spec->h;
119 12155 : node *m = sq->exps->h, *p = m;
120 :
121 12155 : assert(is_project(sq->op));
122 53926 : for (; n && m; n = n->next, p = m, m = m->next) {
123 41771 : char *cname = n->data.sval;
124 41771 : sql_exp *e = m->data;
125 41771 : sql_exp *n = e;
126 :
127 41771 : exp_setname(sql, n, name, cname);
128 41771 : set_basecol(n);
129 : }
130 : /* skip any intern columns */
131 12155 : for (; m; m = m->next) {
132 0 : sql_exp *e = m->data;
133 0 : if (!is_intern(e))
134 : break;
135 : }
136 12155 : if (p)
137 12155 : p->next = 0;
138 12155 : if (n || m)
139 0 : return sql_error(sql, 02, SQLSTATE(M0M03) "Column lists do not match");
140 12155 : list_hash_clear(sq->exps);
141 12155 : set_processed(sq);
142 12155 : return sq;
143 : }
144 :
145 : static int
146 22468 : as_subquery(mvc *sql, sql_table *t, table_types tt, sql_rel *sq, dlist *column_spec, const char *msg)
147 : {
148 22468 : sql_rel *r = sq;
149 :
150 22468 : if (!r)
151 : return 0;
152 22468 : if (column_spec) {
153 689 : dnode *n = column_spec->h;
154 689 : node *m = r->exps->h;
155 :
156 3391 : for (; n && m; n = n->next, m = m->next) {
157 2702 : char *cname = n->data.sval;
158 2702 : sql_exp *e = m->data;
159 2702 : sql_subtype *tp = exp_subtype(e);
160 2702 : sql_column *col = NULL;
161 :
162 2702 : if (tt != tt_view && cname && cname[0] == '%') {
163 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", msg);
164 0 : return -1;
165 2702 : } else if (!tp) {
166 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: columns must have a type defined", msg);
167 0 : return -1;
168 2702 : } else if (tp->type->eclass == EC_ANY) {
169 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: any type (plain null value) not allowed as a column type, use an explicit cast", msg);
170 0 : return -1;
171 2702 : } else if (mvc_bind_column(sql, t, cname)) {
172 0 : sql_error(sql, 01, SQLSTATE(42S21) "%s: duplicate column name %s", msg, cname);
173 0 : return -1;
174 : }
175 2702 : switch (mvc_create_column(&col, sql, t, cname, tp)) {
176 0 : case -1:
177 0 : sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
178 0 : return -1;
179 0 : case -2:
180 : case -3:
181 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", msg);
182 0 : return -1;
183 : default:
184 2702 : break;
185 : }
186 : }
187 689 : if (n || m) {
188 0 : sql_error(sql, 01, SQLSTATE(21S02) "%s: number of columns does not match", msg);
189 0 : return -1;
190 : }
191 : } else {
192 222402 : for (node *m = r->exps->h; m; m = m->next) {
193 200627 : sql_exp *e = m->data;
194 200627 : const char *cname = exp_name(e);
195 200627 : sql_subtype *tp = exp_subtype(e);
196 200627 : sql_column *col = NULL;
197 :
198 200627 : if (!cname)
199 4 : cname = "v";
200 200627 : if (tt != tt_view && cname[0] == '%') {
201 1 : sql_error(sql, 01, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", msg);
202 4 : return -1;
203 200626 : } else if (!tp) {
204 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: columns must have a type defined", msg);
205 0 : return -1;
206 200626 : } else if (tp->type->eclass == EC_ANY) {
207 1 : sql_error(sql, 01, SQLSTATE(42000) "%s: any type (plain null value) not allowed as a column type, use an explicit cast", msg);
208 1 : return -1;
209 200625 : } else if (mvc_bind_column(sql, t, cname)) {
210 2 : sql_error(sql, 01, SQLSTATE(42S21) "%s: duplicate column name %s", msg, cname);
211 2 : return -1;
212 : }
213 200623 : switch (mvc_create_column(&col, sql, t, cname, tp)) {
214 0 : case -1:
215 0 : sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
216 0 : return -1;
217 0 : case -2:
218 : case -3:
219 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", msg);
220 0 : return -1;
221 : default:
222 200623 : break;
223 : }
224 : }
225 : }
226 : return 0;
227 : }
228 :
229 : sql_table *
230 105 : mvc_create_table_as_subquery(mvc *sql, sql_rel *sq, sql_schema *s, const char *tname, dlist *column_spec, int temp, int commit_action, const char *action)
231 : {
232 105 : sql_table *t = NULL;
233 105 : table_types tt =(temp == SQL_REMOTE)?tt_remote:
234 : (temp == SQL_MERGE_TABLE)?tt_merge_table:
235 : (temp == SQL_REPLICA_TABLE)?tt_replica_table:
236 : (temp == SQL_UNLOGGED_TABLE)?tt_unlogged_table:tt_table;
237 :
238 105 : switch (mvc_create_table(&t, sql, s, tname, tt, 0, SQL_DECLARED_TABLE, commit_action, -1, 0)) {
239 0 : case -1:
240 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
241 0 : case -2:
242 : case -3:
243 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
244 0 : case -4:
245 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
246 : case -5:
247 : return NULL;
248 : default:
249 105 : break;
250 : }
251 105 : if (as_subquery(sql, t, tt, sq, column_spec, action) != 0)
252 : return NULL;
253 102 : return t;
254 : }
255 :
256 : sql_table *
257 4 : mvc_create_remote_as_subquery(mvc *sql, sql_rel *sq, sql_schema *s, const char *tname, dlist *column_spec, const char *loc, const char *action)
258 : {
259 4 : sql_table *t = NULL;
260 4 : switch(mvc_create_remote(&t, sql, s, tname, SQL_DECLARED_TABLE, loc)) {
261 0 : case -1:
262 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
263 0 : case -2:
264 : case -3:
265 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
266 0 : case -4:
267 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
268 : case -5:
269 : return NULL;
270 : default:
271 4 : break;
272 : }
273 4 : if (as_subquery(sql, t, tt_remote, sq, column_spec, action) != 0)
274 : return NULL;
275 4 : return t;
276 : }
277 :
278 : static char *
279 894 : table_constraint_name(mvc *sql, symbol *s, sql_schema *ss, sql_table *t)
280 : {
281 : /* create a descriptive name like table_col_pkey */
282 894 : char *suffix; /* stores the type of this constraint */
283 894 : dnode *nms = NULL;
284 894 : char *buf;
285 894 : size_t buflen, len, slen;
286 :
287 894 : switch (s->token) {
288 50 : case SQL_UNIQUE:
289 50 : suffix = "_unique";
290 50 : nms = s->data.lval->h; /* list of columns */
291 890 : break;
292 463 : case SQL_PRIMARY_KEY:
293 463 : suffix = "_pkey";
294 463 : nms = s->data.lval->h; /* list of columns */
295 463 : break;
296 377 : case SQL_FOREIGN_KEY:
297 377 : suffix = "_fkey";
298 377 : nms = s->data.lval->h->next->data.lval->h; /* list of columns */
299 377 : break;
300 : case SQL_CHECK:
301 3 : suffix = "_check";
302 3 : char name[512], name2[512], *nme, *nme2;
303 3 : bool found;
304 3 : do {
305 3 : nme = number2name(name, sizeof(name), ++sql->label);
306 3 : buflen = snprintf(name2, sizeof(name2), "%s_%s%s", t->base.name, nme, suffix);
307 3 : nme2 = name2;
308 3 : found = ol_find_name(t->keys, nme2) || mvc_bind_key(sql, ss, nme2);
309 3 : } while (found);
310 3 : buf = SA_NEW_ARRAY(sql->ta, char, buflen);
311 3 : strcpy(buf, nme2);
312 3 : return buf;
313 1 : default:
314 1 : suffix = "_?";
315 1 : nms = NULL;
316 : }
317 :
318 : /* copy table name */
319 891 : len = strlen(t->base.name);
320 891 : buflen = BUFSIZ;
321 891 : slen = strlen(suffix);
322 891 : while (len + slen >= buflen)
323 0 : buflen += BUFSIZ;
324 891 : buf = SA_NEW_ARRAY(sql->ta, char, buflen);
325 891 : strcpy(buf, t->base.name);
326 :
327 : /* add column name(s) */
328 2154 : for (; nms; nms = nms->next) {
329 1263 : slen = strlen(nms->data.sval);
330 1263 : while (len + slen + 1 >= buflen) {
331 0 : size_t nbuflen = buflen + BUFSIZ;
332 0 : char *nbuf = SA_RENEW_ARRAY(sql->ta, char, buf, nbuflen, buflen);
333 : buf = nbuf;
334 : buflen = nbuflen;
335 : }
336 1263 : snprintf(buf + len, buflen - len, "_%s", nms->data.sval);
337 1263 : len += slen + 1;
338 : }
339 :
340 : /* add suffix */
341 891 : slen = strlen(suffix);
342 891 : while (len + slen >= buflen) {
343 0 : size_t nbuflen = buflen + BUFSIZ;
344 0 : char *nbuf = SA_RENEW_ARRAY(sql->ta, char, buf, nbuflen, buflen);
345 : buf = nbuf;
346 : buflen = nbuflen;
347 : }
348 891 : snprintf(buf + len, buflen - len, "%s", suffix);
349 891 : return buf;
350 : }
351 :
352 : static char *
353 21723 : column_constraint_name(mvc *sql, symbol *s, sql_column *sc, sql_table *t)
354 : {
355 : /* create a descriptive name like table_col_pkey */
356 21723 : char *suffix /* stores the type of this constraint */, *buf;
357 21723 : size_t buflen;
358 :
359 21723 : switch (s->token) {
360 : case SQL_UNIQUE:
361 : suffix = "unique";
362 : break;
363 4 : case SQL_UNIQUE_NULLS_NOT_DISTINCT:
364 4 : suffix = "nndunique";
365 4 : break;
366 2858 : case SQL_PRIMARY_KEY:
367 2858 : suffix = "pkey";
368 2858 : break;
369 57 : case SQL_FOREIGN_KEY:
370 57 : suffix = "fkey";
371 57 : break;
372 17 : case SQL_CHECK:
373 17 : suffix = "check";
374 17 : break;
375 17053 : default:
376 17053 : suffix = "?";
377 : }
378 :
379 21723 : buflen = strlen(t->base.name) + strlen(sc->base.name) + strlen(suffix) + 3;
380 21723 : buf = SA_NEW_ARRAY(sql->ta, char, buflen);
381 21723 : snprintf(buf, buflen, "%s_%s_%s", t->base.name, sc->base.name, suffix);
382 21723 : return buf;
383 : }
384 :
385 : #define COL_NULL 0
386 : #define COL_DEFAULT 1
387 :
388 : static bool
389 971 : foreign_key_check_types(sql_subtype *lt, sql_subtype *rt)
390 : {
391 971 : if (lt->type->eclass == EC_EXTERNAL && rt->type->eclass == EC_EXTERNAL)
392 1 : return lt->type->localtype == rt->type->localtype;
393 1931 : return lt->type->eclass == rt->type->eclass || (EC_VARCHAR(lt->type->eclass) && EC_VARCHAR(rt->type->eclass));
394 : }
395 :
396 : static key_type
397 5978 : token2key_type(int token)
398 : {
399 5978 : switch (token) {
400 : case SQL_UNIQUE: return ukey;
401 7 : case SQL_UNIQUE_NULLS_NOT_DISTINCT: return unndkey;
402 3885 : case SQL_PRIMARY_KEY: return pkey;
403 38 : case SQL_CHECK: return ckey;
404 : }
405 0 : assert(0);
406 : return -1;
407 : }
408 :
409 : static sql_rel*
410 38 : create_check_plan(sql_query *query, symbol *s, sql_table *t)
411 : {
412 38 : mvc *sql = query->sql;
413 38 : exp_kind ek = {type_value, card_value, FALSE};
414 38 : sql_rel *rel = rel_basetable(sql, t, t->base.name);
415 38 : sql_exp *e = rel_logical_value_exp(query, &rel, s->data.lval->h->data.sym, sql_sel | sql_no_subquery, ek);
416 :
417 38 : if (!e || !rel || !is_basetable(rel->op))
418 : return NULL;
419 32 : e->comment = sa_strdup(sql->sa, s->data.lval->h->next->data.sval);
420 32 : rel->exps = rel_base_projection(sql, rel, 0);
421 32 : list *pexps = sa_list(sql->sa);
422 32 : pexps = append(pexps, e);
423 32 : rel = rel_project(sql->sa, rel, pexps);
424 32 : return rel;
425 : }
426 :
427 : static int
428 21728 : column_constraint_type(sql_query *query, const char *name, symbol *s, sql_schema *ss, sql_table *t, sql_column *cs, bool isDeclared, int *used)
429 : {
430 21728 : mvc *sql = query->sql;
431 21728 : int res = SQL_ERR;
432 :
433 21728 : if (isDeclared && (s->token != SQL_NULL && s->token != SQL_NOT_NULL)) {
434 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT: constraints on declared tables are not supported");
435 1 : return res;
436 : }
437 21727 : switch (s->token) {
438 4616 : case SQL_UNIQUE:
439 : case SQL_UNIQUE_NULLS_NOT_DISTINCT:
440 : case SQL_PRIMARY_KEY:
441 : case SQL_CHECK: {
442 4616 : key_type kt = token2key_type(s->token);
443 4616 : sql_key *k;
444 4616 : const char *ns = name;
445 :
446 4616 : if (kt == pkey && t->pkey) {
447 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT PRIMARY KEY: a table can have only one PRIMARY KEY");
448 3 : return res;
449 : }
450 4616 : if (!ns || !*ns) { /* add this to be safe */
451 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name name cannot be empty", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
452 0 : return res;
453 : }
454 4616 : while (isdigit((unsigned char) *ns))
455 0 : ns++;
456 4616 : if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
457 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name cannot contain just digit characters (0 through 9)", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
458 0 : return res;
459 : }
460 4616 : if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
461 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key %s already exists", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE", name);
462 0 : return res;
463 : }
464 4616 : if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
465 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: an index named '%s' already exists, and it would conflict with the key", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
466 0 : return res;
467 : }
468 4616 : char* check = NULL;
469 4616 : sql_rel* check_rel = NULL;
470 4616 : if (kt == ckey) {
471 17 : if ((check_rel = create_check_plan(query, s, t)) == NULL) {
472 : return -3;
473 : }
474 14 : check = exp2str(sql, check_rel->exps->h->data);
475 : }
476 4613 : switch (mvc_create_ukey(&k, sql, t, name, kt, check)) {
477 0 : case -1:
478 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
479 0 : return res;
480 0 : case -2:
481 : case -3:
482 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
483 0 : return res;
484 : default:
485 4613 : break;
486 : }
487 4613 : if (check) {
488 14 : sql_rel* btrel = check_rel->l;
489 14 : node* n = NULL;
490 28 : for (n = btrel->exps->h; n; n = n->next) {
491 14 : sql_exp* e = n->data;
492 14 : const char *nm = e->alias.name;
493 14 : sql_column *c = mvc_bind_column(sql, t, nm);
494 14 : if (!c) {
495 0 : (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT CHECK: no such column '%s' for table '%s'",
496 : nm, t->base.name);
497 0 : return SQL_ERR;
498 : }
499 14 : switch (mvc_create_kc(sql, k, c)) {
500 0 : case -1:
501 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
502 0 : return SQL_ERR;
503 0 : case -2:
504 : case -3:
505 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT CHECK: transaction conflict detected");
506 0 : return SQL_ERR;
507 : default:
508 14 : break;
509 : }
510 : }
511 : }
512 : else
513 4599 : switch (mvc_create_kc(sql, k, cs)) {
514 0 : case -1:
515 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
516 0 : return res;
517 0 : case -2:
518 : case -3:
519 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
520 0 : return res;
521 : default:
522 : break;
523 : }
524 4613 : switch (mvc_create_key_done(sql, k)) {
525 0 : case -1:
526 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
527 0 : return res;
528 0 : case -2:
529 : case -3:
530 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
531 0 : return res;
532 : default:
533 4613 : break;
534 : }
535 4613 : res = SQL_OK;
536 4613 : } break;
537 58 : case SQL_FOREIGN_KEY: {
538 58 : dnode *n = s->data.lval->h;
539 58 : char *rsname = qname_schema(n->data.lval);
540 58 : char *rtname = qname_schema_object(n->data.lval);
541 58 : int ref_actions = n->next->next->next->data.i_val;
542 58 : sql_table *rt;
543 58 : sql_fkey *fk;
544 58 : list *cols;
545 58 : sql_key *rk = NULL;
546 58 : sql_kc *kc;
547 58 : const char *ns = name;
548 :
549 58 : assert(n->next->next->next->type == type_int);
550 58 : rt = find_table_or_view_on_scope(sql, ss, rsname, rtname, "CONSTRAINT FOREIGN KEY", false);
551 : /* self referenced table */
552 58 : if (!rt && t->s == ss && strcmp(t->base.name, rtname) == 0) {
553 1 : sql->errstr[0] = '\0'; /* reset table not found error */
554 1 : sql->session->status = 0;
555 1 : rt = t;
556 : }
557 1 : if (!rt)
558 1 : return SQL_ERR;
559 58 : if (!rt->s) { /* disable foreign key on declared table */
560 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with declared tables");
561 0 : return res;
562 : }
563 58 : if (isTempSchema(t->s) != isTempSchema(rt->s)) { /* disable foreign key between temp and non temp */
564 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between temporary and non temporary tables");
565 0 : return res;
566 : }
567 58 : if (isUnloggedTable(t) != isUnloggedTable(rt)) { /* disable foreign key between logged and unlogged */
568 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between logged and unlogged tables");
569 0 : return res;
570 : }
571 58 : if (!ns || !*ns) { /* add this to be safe */
572 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name name cannot be empty");
573 0 : return res;
574 : }
575 58 : while (isdigit((unsigned char) *ns))
576 0 : ns++;
577 58 : if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
578 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name cannot contain just digit characters (0 through 9)");
579 0 : return res;
580 : }
581 58 : if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
582 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key '%s' already exists", name);
583 0 : return res;
584 : }
585 58 : if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
586 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: an index named '%s' already exists, and it would conflict with the key", name);
587 0 : return res;
588 : }
589 :
590 : /* find unique referenced key */
591 58 : if (n->next->data.lval) {
592 53 : char *rcname = n->next->data.lval->h->data.sval;
593 :
594 53 : cols = list_append(sa_list(sql->sa), rcname);
595 53 : rk = mvc_bind_ukey(rt, cols);
596 5 : } else if (rt->pkey) {
597 : /* no columns specified use rt.pkey */
598 5 : rk = &rt->pkey->k;
599 : }
600 58 : if (!rk) {
601 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: could not find referenced PRIMARY KEY in table %s.%s", rsname, rtname);
602 0 : return res;
603 : }
604 58 : if (list_length(rk->columns) != 1) {
605 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: not all columns are handled");
606 1 : return res;
607 : }
608 57 : kc = rk->columns->h->data;
609 57 : if (!foreign_key_check_types(&cs->type, &kc->c->type)) {
610 0 : str tp1 = sql_subtype_string(sql->ta, &cs->type), tp2 = sql_subtype_string(sql->ta, &kc->c->type);
611 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: the type of the FOREIGN KEY column '%s' %s is not compatible with the referenced %s KEY column type %s",
612 0 : cs->base.name, tp1, rk->type == pkey ? "PRIMARY" : "UNIQUE", tp2);
613 0 : return res;
614 : }
615 57 : switch (mvc_create_fkey(&fk, sql, t, name, fkey, rk, ref_actions & 255, (ref_actions>>8) & 255)) {
616 0 : case -1:
617 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
618 0 : return res;
619 0 : case -2:
620 : case -3:
621 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
622 0 : return res;
623 : default:
624 57 : break;
625 : }
626 57 : switch (mvc_create_fkc(sql, fk, cs)) {
627 0 : case -1:
628 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
629 0 : return res;
630 0 : case -2:
631 : case -3:
632 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
633 0 : return res;
634 : default:
635 57 : break;
636 : }
637 57 : switch (mvc_create_key_done(sql, (sql_key*)fk)) {
638 0 : case -1:
639 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
640 0 : return res;
641 0 : case -2:
642 : case -3:
643 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
644 0 : return res;
645 : default:
646 57 : break;
647 : }
648 57 : res = SQL_OK;
649 57 : } break;
650 17053 : case SQL_NOT_NULL:
651 : case SQL_NULL: {
652 17053 : int null = (s->token != SQL_NOT_NULL);
653 :
654 17053 : if (((*used)&(1<<COL_NULL))) {
655 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "NULL constraint for a column may be specified at most once");
656 1 : return SQL_ERR;
657 : }
658 17052 : *used |= (1<<COL_NULL);
659 :
660 17052 : switch (mvc_null(sql, cs, null)) {
661 0 : case -1:
662 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
663 0 : return SQL_ERR;
664 0 : case -2:
665 : case -3:
666 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
667 0 : return SQL_ERR;
668 : default:
669 : break;
670 : }
671 : res = SQL_OK;
672 : } break;
673 : default:{
674 : res = SQL_ERR;
675 : }
676 : }
677 4670 : if (res == SQL_ERR) {
678 0 : (void) sql_error(sql, 02, SQLSTATE(M0M03) "Unknown constraint (%p)->token = %s", s, token2string(s->token));
679 : }
680 : return res;
681 : }
682 :
683 : static int
684 53095 : column_options(sql_query *query, dlist *opt_list, sql_schema *ss, sql_table *t, sql_column *cs, bool isDeclared)
685 : {
686 53095 : mvc *sql = query->sql;
687 53095 : int res = SQL_OK, used = 0;
688 53095 : assert(cs);
689 :
690 53095 : if (opt_list) {
691 41170 : for (dnode *n = opt_list->h; n && res == SQL_OK; n = n->next) {
692 23123 : symbol *s = n->data.sym;
693 :
694 23123 : switch (s->token) {
695 21728 : case SQL_CONSTRAINT: {
696 21728 : dlist *l = s->data.lval;
697 21728 : char *opt_name = l->h->data.sval, *default_name = NULL;
698 21728 : symbol *sym = l->h->next->data.sym;
699 :
700 21728 : if (!opt_name && !(default_name = column_constraint_name(sql, sym, cs, t)))
701 : return SQL_ERR;
702 :
703 21728 : res = column_constraint_type(query, opt_name ? opt_name : default_name, sym, ss, t, cs, isDeclared, &used);
704 21728 : } break;
705 1395 : case SQL_DEFAULT: {
706 1395 : symbol *sym = s->data.sym;
707 1395 : char *err = NULL, *r;
708 :
709 1395 : if ((used&(1<<COL_DEFAULT))) {
710 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "A default value for a column may be specified at most once");
711 4 : return SQL_ERR;
712 : }
713 1394 : used |= (1<<COL_DEFAULT);
714 :
715 1394 : if (sym->token == SQL_COLUMN || sym->token == SQL_IDENT || sym->token == SQL_NEXT) {
716 265 : exp_kind ek = {type_value, card_value, FALSE};
717 265 : sql_exp *e = rel_logical_value_exp(query, NULL, sym, sql_sel, ek);
718 :
719 265 : if (!e)
720 2 : return SQL_ERR;
721 263 : if (e && is_atom(e->type)) {
722 0 : atom *a = exp_value(sql, e);
723 :
724 0 : if (a && atom_null(a)) {
725 0 : switch (mvc_default(sql, cs, NULL)) {
726 0 : case -1:
727 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
728 0 : return SQL_ERR;
729 0 : case -2:
730 : case -3:
731 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
732 0 : return SQL_ERR;
733 : default:
734 0 : break;
735 : }
736 0 : break;
737 : }
738 : }
739 : /* reset error */
740 263 : sql->session->status = 0;
741 263 : sql->errstr[0] = '\0';
742 : }
743 1392 : r = symbol2string(sql, s->data.sym, 0, &err);
744 1392 : if (!r) {
745 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "Incorrect default value '%s'", err?err:"");
746 1 : return SQL_ERR;
747 : } else {
748 1391 : switch (mvc_default(sql, cs, r)) {
749 0 : case -1:
750 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
751 0 : return SQL_ERR;
752 0 : case -2:
753 : case -3:
754 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
755 0 : return SQL_ERR;
756 : default:
757 1391 : break;
758 : }
759 : }
760 1391 : } break;
761 0 : case SQL_NOT_NULL:
762 : case SQL_NULL: {
763 0 : int null = (s->token != SQL_NOT_NULL);
764 :
765 0 : if ((used&(1<<COL_NULL))) {
766 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "NULL constraint for a column may be specified at most once");
767 0 : return SQL_ERR;
768 : }
769 0 : used |= (1<<COL_NULL);
770 :
771 0 : switch (mvc_null(sql, cs, null)) {
772 0 : case -1:
773 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
774 0 : return SQL_ERR;
775 0 : case -2:
776 : case -3:
777 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
778 0 : return SQL_ERR;
779 : default:
780 : break;
781 : }
782 : } break;
783 0 : default: {
784 0 : (void) sql_error(sql, 02, SQLSTATE(M0M03) "Unknown column option (%p)->token = %s", s, token2string(s->token));
785 0 : return SQL_ERR;
786 : }
787 : }
788 : }
789 : }
790 : return res;
791 : }
792 :
793 : static int
794 897 : table_foreign_key(mvc *sql, const char *name, symbol *s, sql_schema *ss, sql_table *t)
795 : {
796 897 : dnode *n = s->data.lval->h;
797 897 : char *rsname = qname_schema(n->data.lval);
798 897 : char *rtname = qname_schema_object(n->data.lval);
799 897 : const char *ns = name;
800 897 : sql_table *ft = NULL;
801 :
802 897 : ft = find_table_or_view_on_scope(sql, ss, rsname, rtname, "CONSTRAINT FOREIGN KEY", false);
803 : /* self referenced table */
804 897 : if (!ft && t->s == ss && strcmp(t->base.name, rtname) == 0) {
805 10 : sql->errstr[0] = '\0'; /* reset table not found error */
806 10 : sql->session->status = 0;
807 10 : ft = t;
808 : }
809 10 : if (!ft) {
810 : return SQL_ERR;
811 : } else {
812 897 : sql_key *rk = NULL;
813 897 : sql_fkey *fk;
814 897 : dnode *nms = n->next->data.lval->h;
815 897 : node *fnms;
816 897 : int ref_actions = n->next->next->next->next->data.i_val;
817 :
818 897 : assert(n->next->next->next->next->type == type_int);
819 897 : if (!ft->s) { /* disable foreign key on declared table */
820 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with declared tables");
821 26 : return SQL_ERR;
822 : }
823 897 : if (isTempSchema(t->s) != isTempSchema(ft->s)) { /* disable foreign key between temp and non temp */
824 2 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between temporary and non temporary tables");
825 2 : return SQL_ERR;
826 : }
827 895 : if (isTempTable(ft) && !isGlobal(ft) && ft != t) { /* disable foreign key on local temporary table */
828 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key with local temporary tables");
829 1 : return SQL_ERR;
830 : }
831 894 : if (isUnloggedTable(t) != isUnloggedTable(ft)) { /* disable foreign key between logged and unlogged */
832 2 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: cannot create foreign key between logged and unlogged tables");
833 2 : return SQL_ERR;
834 : }
835 892 : if (!ns || !*ns) { /* add this to be safe */
836 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name name cannot be empty");
837 0 : return SQL_ERR;
838 : }
839 892 : while (isdigit((unsigned char) *ns))
840 0 : ns++;
841 892 : if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
842 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key name cannot contain just digit characters (0 through 9)");
843 0 : return SQL_ERR;
844 : }
845 892 : if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
846 2 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: key '%s' already exists", name);
847 2 : return SQL_ERR;
848 : }
849 890 : if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
850 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: an index named '%s' already exists, and it would conflict with the key", name);
851 1 : return SQL_ERR;
852 : }
853 889 : if (n->next->next->data.lval) { /* find unique referenced key */
854 852 : dnode *rnms = n->next->next->data.lval->h;
855 852 : list *cols = sa_list(sql->sa);
856 :
857 2587 : for (; rnms; rnms = rnms->next)
858 883 : list_append(cols, rnms->data.sval);
859 :
860 : /* find key in ft->keys */
861 852 : rk = mvc_bind_ukey(ft, cols);
862 37 : } else if (ft->pkey) {
863 : /* no columns specified use ft.pkey */
864 37 : rk = &ft->pkey->k;
865 : }
866 889 : if (!rk) {
867 5 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: could not find referenced PRIMARY KEY in table '%s'", ft->base.name);
868 5 : return SQL_ERR;
869 : }
870 884 : switch (mvc_create_fkey(&fk, sql, t, name, fkey, rk, ref_actions & 255, (ref_actions>>8) & 255)) {
871 0 : case -1:
872 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
873 0 : return SQL_ERR;
874 0 : case -2:
875 : case -3:
876 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
877 0 : return SQL_ERR;
878 : default:
879 884 : break;
880 : }
881 :
882 1789 : for (fnms = rk->columns->h; nms && fnms; nms = nms->next, fnms = fnms->next) {
883 917 : char *nm = nms->data.sval;
884 917 : sql_column *cs = mvc_bind_column(sql, t, nm);
885 917 : sql_kc *kc = fnms->data;
886 :
887 917 : if (!cs) {
888 3 : (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT FOREIGN KEY: no such column '%s' in table '%s'", nm, t->base.name);
889 3 : return SQL_ERR;
890 : }
891 914 : if (!foreign_key_check_types(&cs->type, &kc->c->type)) {
892 9 : str tp1 = sql_subtype_string(sql->ta, &cs->type), tp2 = sql_subtype_string(sql->ta, &kc->c->type);
893 18 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: the type of the FOREIGN KEY column '%s' %s is not compatible with the referenced %s KEY column type %s",
894 9 : cs->base.name, tp1, rk->type == pkey ? "PRIMARY" : "UNIQUE", tp2);
895 9 : return SQL_ERR;
896 : }
897 905 : switch (mvc_create_fkc(sql, fk, cs)) {
898 0 : case -1:
899 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
900 0 : return SQL_ERR;
901 0 : case -2:
902 : case -3:
903 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
904 0 : return SQL_ERR;
905 : default:
906 905 : break;
907 : }
908 : }
909 872 : if (nms || fnms) {
910 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: not all columns are handled");
911 1 : return SQL_ERR;
912 : }
913 871 : switch (mvc_create_key_done(sql, (sql_key*)fk)) {
914 0 : case -1:
915 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
916 0 : return SQL_ERR;
917 0 : case -2:
918 : case -3:
919 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT FOREIGN KEY: transaction conflict detected");
920 0 : return SQL_ERR;
921 : default:
922 871 : break;
923 : }
924 : }
925 871 : return SQL_OK;
926 : }
927 :
928 : static int
929 2259 : table_constraint_type(sql_query *query, const char *name, symbol *s, sql_schema *ss, sql_table *t)
930 : {
931 2259 : mvc *sql = query->sql;
932 2259 : int res = SQL_OK;
933 :
934 2259 : switch (s->token) {
935 1362 : case SQL_UNIQUE:
936 : case SQL_UNIQUE_NULLS_NOT_DISTINCT:
937 : case SQL_PRIMARY_KEY:
938 : case SQL_CHECK: {
939 1362 : key_type kt = token2key_type(s->token);
940 1362 : dnode *nms = s->data.lval->h;
941 1362 : sql_key *k;
942 1362 : const char *ns = name;
943 :
944 1362 : if (kt == pkey && t->pkey) {
945 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT PRIMARY KEY: a table can have only one PRIMARY KEY");
946 12 : return SQL_ERR;
947 : }
948 1362 : if (!ns || !*ns) { /* add this to be safe */
949 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name name cannot be empty", kt == pkey ? "PRIMARY KEY" : "UNIQUE");
950 0 : return SQL_ERR;
951 : }
952 1362 : while (isdigit((unsigned char) *ns))
953 0 : ns++;
954 1362 : if (!*ns) { /* if a key name just contains digit characters, the generated index name can be mistaken with a label */
955 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key name cannot contain just digit characters (0 through 9)", kt == pkey ? "PRIMARY KEY" : "UNIQUE");
956 0 : return SQL_ERR;
957 : }
958 1362 : if (ol_find_name(t->keys, name) || mvc_bind_key(sql, ss, name)) {
959 3 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: key '%s' already exists", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
960 3 : return SQL_ERR;
961 : }
962 1359 : if (ol_find_name(t->idxs, name) || mvc_bind_idx(sql, ss, name)) {
963 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: an index named '%s' already exists, and it would conflict with the key", kt == pkey ? "PRIMARY KEY" : "UNIQUE", name);
964 0 : return SQL_ERR;
965 : }
966 1359 : char* check = NULL;
967 1359 : sql_rel* check_rel = NULL;
968 :
969 1359 : if (kt == ckey) {
970 21 : if ((check_rel = create_check_plan(query, s, t)) == NULL) {
971 : return -3;
972 : }
973 18 : check = exp2str(sql, check_rel->exps->h->data);
974 : }
975 :
976 1356 : switch (mvc_create_ukey(&k, sql, t, name, kt, check)) {
977 0 : case -1:
978 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
979 0 : return SQL_ERR;
980 0 : case -2:
981 : case -3:
982 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
983 0 : return SQL_ERR;
984 : default:
985 1356 : break;
986 : }
987 1356 : node* n = NULL;
988 1356 : if (check) {
989 18 : sql_rel* btrel = check_rel->l;
990 18 : n = btrel->exps->h;
991 : }
992 3310 : while (true) {
993 3310 : const char *nm;
994 3310 : if (check) {
995 40 : if (!n)
996 : break;
997 22 : sql_exp* e = n->data;
998 22 : nm = e->alias.name;
999 22 : n = n->next;
1000 : } else {
1001 3270 : if (!nms)
1002 : break;
1003 1938 : nm = nms->data.sval;
1004 1938 : nms = nms->next;
1005 : }
1006 1960 : sql_column *c = mvc_bind_column(sql, t, nm);
1007 :
1008 1960 : if (!c) {
1009 6 : (void) sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CONSTRAINT %s: no such column '%s' for table '%s'",
1010 : kt == pkey ? "PRIMARY KEY" : "UNIQUE",
1011 : nm, t->base.name);
1012 6 : return SQL_ERR;
1013 : }
1014 1954 : switch (mvc_create_kc(sql, k, c)) {
1015 0 : case -1:
1016 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1017 0 : return SQL_ERR;
1018 0 : case -2:
1019 : case -3:
1020 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
1021 0 : return SQL_ERR;
1022 : default:
1023 : break;
1024 : }
1025 : }
1026 1350 : switch (mvc_create_key_done(sql, k)) {
1027 0 : case -1:
1028 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1029 0 : return SQL_ERR;
1030 0 : case -2:
1031 : case -3:
1032 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "CONSTRAINT %s: transaction conflict detected", (kt == pkey) ? "PRIMARY KEY" : "UNIQUE");
1033 0 : return SQL_ERR;
1034 : default:
1035 1350 : break;
1036 : }
1037 1350 : } break;
1038 897 : case SQL_FOREIGN_KEY:
1039 897 : res = table_foreign_key(sql, name, s, ss, t);
1040 897 : break;
1041 : default:
1042 : res = SQL_ERR;
1043 : }
1044 2247 : if (res != SQL_OK) {
1045 26 : (void) sql_error(sql, 02, SQLSTATE(M0M03) "Table constraint type: wrong token (%p) = %s", s, token2string(s->token));
1046 26 : return SQL_ERR;
1047 : }
1048 : return res;
1049 : }
1050 :
1051 : static int
1052 2259 : table_constraint(sql_query *query, symbol *s, sql_schema *ss, sql_table *t)
1053 : {
1054 2259 : mvc *sql = query->sql;
1055 2259 : int res = SQL_OK;
1056 :
1057 2259 : if (s->token == SQL_CONSTRAINT) {
1058 2259 : dlist *l = s->data.lval;
1059 2259 : char *opt_name = l->h->data.sval;
1060 2259 : symbol *sym = l->h->next->data.sym;
1061 :
1062 2259 : if (!opt_name)
1063 894 : opt_name = table_constraint_name(sql, sym, ss, t);
1064 : else if (s->token)
1065 : if (opt_name == NULL)
1066 : return SQL_ERR;
1067 2259 : res = table_constraint_type(query, opt_name, sym, ss, t);
1068 : }
1069 :
1070 2259 : if (res != SQL_OK) {
1071 38 : (void) sql_error(sql, 02, SQLSTATE(M0M03) "Table constraint: wrong token (%p) = %s", s, token2string(s->token));
1072 38 : return SQL_ERR;
1073 : }
1074 : return res;
1075 : }
1076 :
1077 : static int
1078 53097 : create_column(sql_query *query, symbol *s, sql_schema *ss, sql_table *t, int alter, bool isDeclared)
1079 : {
1080 53097 : mvc *sql = query->sql;
1081 53097 : dlist *l = s->data.lval;
1082 53097 : char *cname = l->h->data.sval;
1083 53097 : sql_subtype *ctype = &l->h->next->data.typeval;
1084 53097 : dlist *opt_list = NULL;
1085 53097 : int res = SQL_OK;
1086 :
1087 53097 : (void) ss;
1088 53097 : if (alter && !(isTable(t) || ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)==0))) {
1089 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot add column to %s '%s'%s",
1090 0 : TABLE_TYPE_DESCRIPTION(t->type, t->properties),
1091 0 : t->base.name, ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)) ? " while it has partitions" : "");
1092 0 : return SQL_ERR;
1093 : }
1094 53097 : if (l->h->next->next)
1095 53097 : opt_list = l->h->next->next->data.lval;
1096 :
1097 53097 : if (cname && ctype) {
1098 53097 : sql_column *cs = NULL;
1099 :
1100 53097 : if (!isView(t) && cname && cname[0] == '%') {
1101 0 : (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: generated labels not allowed in column names, use an alias instead", (alter)?"ALTER":"CREATE");
1102 9 : return SQL_ERR;
1103 53097 : } else if (ctype->type->eclass == EC_ANY) {
1104 0 : (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: any type (plain null value) not allowed as a column type, use an explicit cast", (alter)?"ALTER":"CREATE");
1105 0 : return SQL_ERR;
1106 53097 : } else if ((cs = find_sql_column(t, cname))) {
1107 2 : (void) sql_error(sql, 02, SQLSTATE(42S21) "%s TABLE: a column named '%s' already exists", (alter)?"ALTER":"CREATE", cname);
1108 2 : return SQL_ERR;
1109 : }
1110 53095 : switch (mvc_create_column(&cs, sql, t, cname, ctype)) {
1111 0 : case -1:
1112 0 : (void) sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1113 0 : return SQL_ERR;
1114 0 : case -2:
1115 : case -3:
1116 0 : (void) sql_error(sql, 01, SQLSTATE(42000) "%s TABLE: transaction conflict detected", (alter)?"ALTER":"CREATE");
1117 0 : return SQL_ERR;
1118 : default:
1119 53095 : break;
1120 : }
1121 53095 : if (column_options(query, opt_list, ss, t, cs, isDeclared) == SQL_ERR)
1122 : return SQL_ERR;
1123 : }
1124 : return res;
1125 : }
1126 :
1127 : static int
1128 55754 : table_element(sql_query *query, symbol *s, sql_schema *ss, sql_table *t, int alter, bool isDeclared, const char *action)
1129 : {
1130 55754 : mvc *sql = query->sql;
1131 55754 : int res = SQL_OK;
1132 :
1133 55754 : if (alter &&
1134 1260 : (isView(t) ||
1135 1252 : ((isMergeTable(t) || isReplicaTable(t)) && (s->token != SQL_TABLE && s->token != SQL_DROP_TABLE && list_length(t->members))) ||
1136 2494 : (isTable(t) && (s->token == SQL_TABLE || s->token == SQL_DROP_TABLE)) ||
1137 1247 : (partition_find_part(sql->session->tr, t, NULL) &&
1138 5 : (s->token == SQL_DROP_COLUMN || s->token == SQL_COLUMN || s->token == SQL_CONSTRAINT ||
1139 : s->token == SQL_DEFAULT || s->token == SQL_DROP_DEFAULT || s->token == SQL_NOT_NULL || s->token == SQL_NULL || s->token == SQL_DROP_CONSTRAINT)))){
1140 18 : const char *msg = "";
1141 :
1142 18 : switch (s->token) {
1143 : case SQL_TABLE:
1144 : msg = "add table to";
1145 : break;
1146 3 : case SQL_COLUMN:
1147 3 : msg = "add column to";
1148 3 : break;
1149 4 : case SQL_CONSTRAINT:
1150 4 : msg = "add constraint to";
1151 4 : break;
1152 4 : case SQL_COLUMN_OPTIONS:
1153 : case SQL_DEFAULT:
1154 : case SQL_NOT_NULL:
1155 : case SQL_NULL:
1156 4 : msg = "set column options for";
1157 4 : break;
1158 0 : case SQL_STORAGE:
1159 0 : msg = "set column storage for";
1160 0 : break;
1161 0 : case SQL_DROP_DEFAULT:
1162 0 : msg = "drop default column option from";
1163 0 : break;
1164 0 : case SQL_DROP_TABLE:
1165 0 : msg = "drop table from";
1166 0 : break;
1167 5 : case SQL_DROP_COLUMN:
1168 5 : msg = "drop column from";
1169 5 : break;
1170 2 : case SQL_DROP_CONSTRAINT:
1171 2 : msg = "drop constraint from";
1172 2 : break;
1173 0 : default:
1174 0 : sql_error(sql, 02, SQLSTATE(M0M03) "%s: Unknown table element (%p)->token = %s\n", action, s, token2string(s->token));
1175 0 : return SQL_ERR;
1176 : }
1177 18 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot %s %s '%s'%s\n",
1178 : action,
1179 : msg,
1180 18 : partition_find_part(sql->session->tr, t, NULL)?"a PARTITION of a MERGE or REPLICA TABLE":
1181 13 : TABLE_TYPE_DESCRIPTION(t->type, t->properties),
1182 18 : t->base.name, ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)) ? " while it has partitions" : "");
1183 18 : return SQL_ERR;
1184 : }
1185 :
1186 55736 : switch (s->token) {
1187 53097 : case SQL_COLUMN:
1188 53097 : res = create_column(query, s, ss, t, alter, isDeclared);
1189 53097 : break;
1190 2259 : case SQL_CONSTRAINT:
1191 2259 : res = table_constraint(query, s, ss, t);
1192 2259 : break;
1193 0 : case SQL_COLUMN_OPTIONS:
1194 : {
1195 0 : dnode *n = s->data.lval->h;
1196 0 : char *cname = n->data.sval;
1197 0 : sql_column *c = mvc_bind_column(sql, t, cname);
1198 0 : dlist *olist = n->next->data.lval;
1199 :
1200 0 : if (!c) {
1201 0 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1202 0 : return SQL_ERR;
1203 : } else {
1204 0 : return column_options(query, olist, ss, t, c, isDeclared);
1205 : }
1206 22 : } break;
1207 22 : case SQL_DEFAULT:
1208 : {
1209 22 : char *r, *err = NULL;
1210 22 : dlist *l = s->data.lval;
1211 22 : char *cname = l->h->data.sval;
1212 22 : symbol *sym = l->h->next->data.sym;
1213 22 : sql_column *c = mvc_bind_column(sql, t, cname);
1214 :
1215 22 : if (!c) {
1216 3 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1217 5 : return SQL_ERR;
1218 : }
1219 19 : if (sym->token == SQL_COLUMN || sym->token == SQL_IDENT || sym->token == SQL_NEXT) {
1220 7 : exp_kind ek = {type_value, card_value, FALSE};
1221 7 : sql_exp *e = rel_logical_value_exp(query, NULL, sym, sql_sel, ek);
1222 :
1223 7 : if (!e)
1224 1 : return SQL_ERR;
1225 6 : if (e && is_atom(e->type)) {
1226 0 : atom *a = exp_value(sql, e);
1227 :
1228 0 : if (a && atom_null(a)) {
1229 0 : switch (mvc_default(sql, c, NULL)) {
1230 0 : case -1:
1231 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1232 0 : return SQL_ERR;
1233 0 : case -2:
1234 : case -3:
1235 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
1236 0 : return SQL_ERR;
1237 : default:
1238 0 : break;
1239 : }
1240 0 : break;
1241 : }
1242 : }
1243 : /* reset error */
1244 6 : sql->session->status = 0;
1245 6 : sql->errstr[0] = '\0';
1246 : }
1247 18 : r = symbol2string(sql, sym, 0, &err);
1248 18 : if (!r) {
1249 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "%s: incorrect default value '%s'\n", action, err?err:"");
1250 1 : return SQL_ERR;
1251 : }
1252 17 : switch (mvc_default(sql, c, r)) {
1253 0 : case -1:
1254 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1255 0 : return SQL_ERR;
1256 0 : case -2:
1257 : case -3:
1258 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
1259 0 : return SQL_ERR;
1260 : default:
1261 17 : break;
1262 : }
1263 : }
1264 17 : break;
1265 0 : case SQL_STORAGE:
1266 : {
1267 0 : dlist *l = s->data.lval;
1268 0 : char *cname = l->h->data.sval;
1269 0 : char *storage_type = l->h->next->data.sval;
1270 0 : sql_column *c = mvc_bind_column(sql, t, cname);
1271 :
1272 0 : if (!c) {
1273 0 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1274 0 : return SQL_ERR;
1275 : }
1276 0 : switch (mvc_storage(sql, c, storage_type)) {
1277 0 : case -1:
1278 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1279 0 : return SQL_ERR;
1280 0 : case -2:
1281 : case -3:
1282 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "STORAGE: transaction conflict detected");
1283 0 : return SQL_ERR;
1284 : default:
1285 : break;
1286 : }
1287 : }
1288 : break;
1289 45 : case SQL_NOT_NULL:
1290 : case SQL_NULL:
1291 : {
1292 45 : dnode *n = s->data.lval->h;
1293 45 : char *cname = n->data.sval;
1294 45 : sql_column *c = mvc_bind_column(sql, t, cname);
1295 45 : int null = (s->token != SQL_NOT_NULL);
1296 :
1297 45 : if (!c) {
1298 4 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1299 4 : return SQL_ERR;
1300 : }
1301 41 : switch (mvc_null(sql, c, null)) {
1302 0 : case -1:
1303 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1304 0 : return SQL_ERR;
1305 0 : case -2:
1306 : case -3:
1307 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "NULL CONSTRAINT: transaction conflict detected");
1308 0 : return SQL_ERR;
1309 : default:
1310 : break;
1311 : }
1312 : } break;
1313 8 : case SQL_DROP_DEFAULT:
1314 : {
1315 8 : char *cname = s->data.sval;
1316 8 : sql_column *c = mvc_bind_column(sql, t, cname);
1317 8 : if (!c) {
1318 2 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1319 2 : return SQL_ERR;
1320 : }
1321 6 : switch (mvc_drop_default(sql, c)) {
1322 0 : case -1:
1323 0 : (void) sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1324 0 : return SQL_ERR;
1325 0 : case -2:
1326 : case -3:
1327 0 : (void) sql_error(sql, 02, SQLSTATE(42000) "DEFAULT: transaction conflict detected while setting default value");
1328 0 : return SQL_ERR;
1329 : default:
1330 : break;
1331 : }
1332 : } break;
1333 69 : case SQL_LIKE:
1334 : {
1335 69 : char *sname = qname_schema(s->data.lval);
1336 69 : char *name = qname_schema_object(s->data.lval);
1337 69 : sql_table *ot = NULL;
1338 :
1339 69 : if (!(ot = find_table_or_view_on_scope(sql, ss, sname, name, action, false)))
1340 : return SQL_ERR;
1341 143 : for (node *n = ol_first_node(ot->columns); n; n = n->next) {
1342 76 : sql_column *oc = n->data, *nc = NULL;
1343 :
1344 76 : if (!isView(t) && oc->base.name && oc->base.name[0] == '%') {
1345 0 : sql_error(sql, 02, SQLSTATE(42000) "%s: generated labels not allowed in column names, use an alias instead", action);
1346 2 : return SQL_ERR;
1347 76 : } else if (mvc_bind_column(sql, t, oc->base.name)) {
1348 2 : sql_error(sql, 02, SQLSTATE(42S21) "%s: a column named '%s' already exists\n", action, oc->base.name);
1349 2 : return SQL_ERR;
1350 : }
1351 74 : assert(oc->type.type->eclass != EC_ANY);
1352 74 : switch (mvc_create_column(&nc, sql, t, oc->base.name, &oc->type)) {
1353 0 : case -1:
1354 0 : sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1355 0 : return SQL_ERR;
1356 0 : case -2:
1357 : case -3:
1358 0 : sql_error(sql, 01, SQLSTATE(42000) "%s: transaction conflict detected", action);
1359 0 : return SQL_ERR;
1360 : default:
1361 74 : break;
1362 : }
1363 : }
1364 : } break;
1365 86 : case SQL_DROP_COLUMN:
1366 : {
1367 86 : dlist *l = s->data.lval;
1368 86 : char *cname = l->h->data.sval;
1369 86 : int drop_action = l->h->next->data.i_val;
1370 86 : sql_column *col = mvc_bind_column(sql, t, cname);
1371 :
1372 86 : assert(l->h->next->type == type_int);
1373 86 : if (col == NULL) {
1374 9 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "%s: no such column '%s'\n", action, cname);
1375 9 : return SQL_ERR;
1376 : }
1377 77 : if (ol_length(t->columns) <= 1) {
1378 2 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': table needs at least one column\n", action, cname);
1379 2 : return SQL_ERR;
1380 : }
1381 75 : if (t->system) {
1382 0 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': table is a system table\n", action, cname);
1383 0 : return SQL_ERR;
1384 : }
1385 75 : if (isView(t)) {
1386 0 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': '%s' is a view\n", action, cname, t->base.name);
1387 0 : return SQL_ERR;
1388 : }
1389 75 : if (!drop_action && mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL)) {
1390 3 : sql_error(sql, 02, SQLSTATE(2BM37) "%s: cannot drop column '%s': there are database objects which depend on it\n", action, cname);
1391 3 : return SQL_ERR;
1392 : }
1393 64 : if (!drop_action && t->keys) {
1394 64 : node *n, *m;
1395 :
1396 64 : for (n = ol_first_node(t->keys); n; n = n->next) {
1397 0 : sql_key *k = n->data;
1398 0 : for (m = k->columns->h; m; m = m->next) {
1399 0 : sql_kc *kc = m->data;
1400 0 : if (strcmp(kc->c->base.name, cname) == 0) {
1401 0 : sql_error(sql, 02, SQLSTATE(2BM37) "%s: cannot drop column '%s': there are constraints which depend on it\n", action, cname);
1402 0 : return SQL_ERR;
1403 : }
1404 : }
1405 : }
1406 : }
1407 72 : if (isPartitionedByColumnTable(t) && t->part.pcol->base.id == col->base.id) {
1408 2 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': is the partitioned column on the table '%s'\n", action, cname, t->base.name);
1409 2 : return SQL_ERR;
1410 : }
1411 70 : if (isPartitionedByExpressionTable(t)) {
1412 8 : for (node *n = t->part.pexp->cols->h; n; n = n->next) {
1413 6 : int next = *(int*) n->data;
1414 6 : if (next == col->colnr) {
1415 2 : sql_error(sql, 02, SQLSTATE(42000) "%s: cannot drop column '%s': the expression used in '%s' depends on it\n", action, cname, t->base.name);
1416 2 : return SQL_ERR;
1417 : }
1418 : }
1419 : }
1420 68 : switch (mvc_drop_column(sql, t, col, drop_action)) {
1421 0 : case -1:
1422 0 : sql_error(sql, 02, SQLSTATE(42000) "%s: %s\n", action, MAL_MALLOC_FAIL);
1423 0 : return SQL_ERR;
1424 0 : case -2:
1425 : case -3:
1426 0 : sql_error(sql, 02, SQLSTATE(42000) "%s: transaction conflict detected\n", action);
1427 0 : return SQL_ERR;
1428 : default:
1429 : break;
1430 : }
1431 : } break;
1432 : case SQL_DROP_CONSTRAINT:
1433 : res = SQL_OK;
1434 : break;
1435 : default:
1436 : res = SQL_ERR;
1437 : }
1438 55440 : if (res == SQL_ERR) {
1439 47 : sql_error(sql, 02, SQLSTATE(M0M03) "%s: Unknown table element (%p)->token = %s\n", action, s, token2string(s->token));
1440 47 : return SQL_ERR;
1441 : }
1442 : return res;
1443 : }
1444 :
1445 : static int
1446 10108 : create_partition_definition(mvc *sql, sql_table *t, symbol *partition_def)
1447 : {
1448 10108 : char *err = NULL;
1449 :
1450 10108 : if (partition_def) {
1451 107 : dlist *list = partition_def->data.lval;
1452 107 : symbol *type = list->h->next->data.sym;
1453 107 : dlist *list2 = type->data.lval;
1454 107 : if (isPartitionedByColumnTable(t)) {
1455 84 : str colname = list2->h->data.sval;
1456 84 : node *n;
1457 84 : sql_class sql_ec;
1458 100 : for (n = ol_first_node(t->columns); n ; n = n->next) {
1459 98 : sql_column *col = n->data;
1460 98 : if (!strcmp(col->base.name, colname)) {
1461 82 : t->part.pcol = col;
1462 82 : break;
1463 : }
1464 : }
1465 84 : if (!t->part.pcol) {
1466 2 : sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: the partition column '%s' is not part of the table", colname);
1467 2 : return SQL_ERR;
1468 : }
1469 82 : sql_ec = t->part.pcol->type.type->eclass;
1470 82 : if (!(sql_ec == EC_BIT || EC_VARCHAR(sql_ec) || EC_TEMP(sql_ec) || sql_ec == EC_POS || sql_ec == EC_NUM ||
1471 2 : EC_INTERVAL(sql_ec)|| sql_ec == EC_DEC || sql_ec == EC_BLOB)) {
1472 2 : err = sql_subtype_string(sql->ta, &(t->part.pcol->type));
1473 2 : sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: column type %s not yet supported for the partition column", err);
1474 2 : return SQL_ERR;
1475 : }
1476 23 : } else if (isPartitionedByExpressionTable(t)) {
1477 23 : char *query = symbol2string(sql, list2->h->data.sym, 1, &err);
1478 23 : if (!query) {
1479 1 : (void) sql_error(sql, 02, SQLSTATE(42000) "CREATE MERGE TABLE: error compiling expression '%s'", err?err:"");
1480 1 : return SQL_ERR;
1481 : }
1482 22 : t->part.pexp = SA_ZNEW(sql->sa, sql_expression);
1483 22 : t->part.pexp->exp = query;
1484 22 : t->part.pexp->type = *sql_bind_localtype("void");
1485 : }
1486 : }
1487 : return SQL_OK;
1488 : }
1489 :
1490 : sql_rel *
1491 10268 : rel_create_table(sql_query *query, int temp, const char *sname, const char *name, bool global, symbol *table_elements_or_subquery,
1492 : int commit_action, const char *loc, const char *username, const char *password, bool pw_encrypted,
1493 : symbol* partition_def, int if_not_exists)
1494 : {
1495 10268 : mvc *sql = query->sql;
1496 10268 : int tt = (temp == SQL_REMOTE)?tt_remote:
1497 : (temp == SQL_MERGE_TABLE)?tt_merge_table:
1498 : (temp == SQL_REPLICA_TABLE)?tt_replica_table:
1499 : (temp == SQL_UNLOGGED_TABLE)?tt_unlogged_table:tt_table;
1500 10268 : bit properties = partition_def ? (bit) partition_def->data.lval->h->next->next->data.i_val : 0;
1501 10268 : sql_table *t = NULL;
1502 10268 : const char *action = (temp == SQL_DECLARED_TABLE)?"DECLARE":"CREATE";
1503 10268 : sql_schema *s = cur_schema(sql);
1504 :
1505 10268 : if (sname && !(s = mvc_bind_schema(sql, sname)))
1506 1 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "%s TABLE: no such schema '%s'", action, sname);
1507 :
1508 10267 : if ((temp != SQL_PERSIST && tt == tt_table && commit_action == CA_COMMIT) || temp == SQL_DECLARE)
1509 195 : commit_action = CA_DELETE;
1510 :
1511 10267 : if (temp == SQL_LOCAL_TEMP || temp == SQL_GLOBAL_TEMP) {
1512 112 : if (sname && strcmp(sname, "tmp") != 0)
1513 7 : return sql_error(sql, 02, SQLSTATE(3F000) "%s TABLE: %s temporary tables should be stored in the 'tmp' schema",
1514 : action, (temp == SQL_LOCAL_TEMP) ? "local" : "global");
1515 107 : s = tmp_schema(sql);
1516 : }
1517 :
1518 10262 : if (global && mvc_bind_table(sql, s, name)) {
1519 8 : if (if_not_exists)
1520 2 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1521 6 : return sql_error(sql, 02, SQLSTATE(42S01) "%s TABLE: name '%s' already in use", action, name);
1522 121 : } else if (!global && frame_find_table(sql, name)) {
1523 1 : assert(temp == SQL_DECLARED_TABLE);
1524 1 : if (if_not_exists)
1525 0 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1526 1 : return sql_error(sql, 02, SQLSTATE(42S01) "%s TABLE: name '%s' already declared", action, name);
1527 10253 : } else if (temp != SQL_DECLARED_TABLE && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && temp == SQL_LOCAL_TEMP))){
1528 4 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE TABLE: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1529 10249 : } else if ((temp == SQL_PERSIST || temp == SQL_MERGE_TABLE || temp == SQL_REMOTE || temp == SQL_REPLICA_TABLE || temp == SQL_UNLOGGED_TABLE) && isTempSchema(s)) {
1530 7 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: cannot create persistent table '%s' in the schema '%s'", TABLE_TYPE_DESCRIPTION(tt, properties), name, s->base.name);
1531 10243 : } else if (table_elements_or_subquery->token == SQL_CREATE_TABLE) {
1532 : /* table element list */
1533 10128 : dnode *n;
1534 10128 : dlist *columns = table_elements_or_subquery->data.lval;
1535 10128 : int res = LOG_OK;
1536 :
1537 10128 : if (tt == tt_remote) {
1538 97 : if (!mapiuri_valid(loc))
1539 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: incorrect uri '%s' for remote table '%s'", action, loc, name);
1540 97 : res = mvc_create_remote(&t, sql, s, name, SQL_DECLARED_TABLE, loc);
1541 : } else {
1542 10031 : res = mvc_create_table(&t, sql, s, name, tt, 0, SQL_DECLARED_TABLE, commit_action, -1, properties);
1543 : }
1544 10128 : switch (res) {
1545 0 : case -1:
1546 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1547 0 : case -2:
1548 : case -3:
1549 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: transaction conflict detected", action);
1550 0 : case -4:
1551 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: the partition's expression is too long", action);
1552 : case -5:
1553 : return NULL;
1554 : default:
1555 10128 : break;
1556 : }
1557 :
1558 64602 : for (n = columns->h; n; n = n->next) {
1559 54494 : symbol *sym = n->data.sym;
1560 108765 : int res = table_element(query, sym, s, t, 0, (temp == SQL_DECLARED_TABLE), (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE");
1561 :
1562 54494 : if (res == SQL_ERR)
1563 : return NULL;
1564 : }
1565 :
1566 10108 : if (create_partition_definition(sql, t, partition_def) != SQL_OK)
1567 : return NULL;
1568 :
1569 10103 : temp = (tt == tt_table)?temp:SQL_PERSIST;
1570 :
1571 395 : if (tt == tt_remote)
1572 97 : return rel_create_remote(sql, ddl_create_table, s->base.name, t, pw_encrypted, username, password);
1573 10006 : return rel_table(sql, ddl_create_table, s->base.name, t, temp);
1574 : } else { /* [col name list] as subquery with or without data */
1575 115 : sql_rel *sq = NULL, *res = NULL;
1576 115 : dlist *as_sq = table_elements_or_subquery->data.lval;
1577 115 : dlist *column_spec = as_sq->h->data.lval;
1578 115 : symbol *subquery = as_sq->h->next->data.sym;
1579 115 : int with_data = as_sq->h->next->next->data.i_val;
1580 :
1581 115 : assert(as_sq->h->next->next->type == type_int);
1582 115 : sq = rel_selects(query, subquery);
1583 115 : if (!sq)
1584 : return NULL;
1585 112 : if (!is_project(sq->op)) /* make sure sq is a projection */
1586 6 : sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 1));
1587 :
1588 112 : if ((tt != tt_table && tt != tt_unlogged_table) && with_data)
1589 3 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: cannot create %s 'with data'", action,
1590 3 : TABLE_TYPE_DESCRIPTION(tt, properties));
1591 :
1592 : /* create table */
1593 6 : if (tt == tt_remote) {
1594 4 : if (!mapiuri_valid(loc))
1595 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s TABLE: incorrect uri '%s' for remote table '%s'", action, loc, name);
1596 8 : if ((t = mvc_create_remote_as_subquery(sql, sq, s, name, column_spec, loc, (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE")) == NULL) {
1597 0 : rel_destroy(sq);
1598 0 : return NULL;
1599 : }
1600 : } else {
1601 207 : if ((t = mvc_create_table_as_subquery(sql, sq, s, name, column_spec, temp, commit_action, (temp == SQL_DECLARED_TABLE)?"DECLARE TABLE":"CREATE TABLE")) == NULL) {
1602 3 : rel_destroy(sq);
1603 3 : return NULL;
1604 : }
1605 : }
1606 :
1607 : /* insert query result into this table */
1608 106 : if (tt == tt_remote) {
1609 4 : res = rel_create_remote(sql, ddl_create_table, s->base.name, t, pw_encrypted, username, password);
1610 : /* we cannot insert in remote so just remove the subquery */
1611 4 : rel_destroy(sq);
1612 : } else {
1613 105 : res = rel_table(sql, ddl_create_table, s->base.name, t, (tt == tt_table)?temp:SQL_PERSIST);
1614 102 : if (with_data) {
1615 95 : res = rel_insert(query->sql, res, sq);
1616 : } else {
1617 7 : rel_destroy(sq);
1618 : }
1619 : }
1620 106 : return res;
1621 : }
1622 : /*return NULL;*/ /* never reached as all branches of the above if () end with return ... */
1623 : }
1624 :
1625 : static sql_rel *
1626 79020 : rel_create_view(sql_query *query, dlist *qname, dlist *column_spec, symbol *ast, int check, int persistent, int replace)
1627 : {
1628 79020 : mvc *sql = query->sql;
1629 79020 : const char *name = qname_schema_object(qname);
1630 79020 : const char *sname = qname_schema(qname);
1631 79020 : sql_schema *s = cur_schema(sql);
1632 79020 : sql_table *t = NULL;
1633 79020 : int instantiate = (sql->emode == m_instantiate || !persistent);
1634 79020 : int deps = (sql->emode == m_deps);
1635 79020 : int create = (!instantiate && !deps);
1636 79020 : sqlid pfoundid = 0, foundid = 0;
1637 79020 : const char *base = replace ? "CREATE OR REPLACE VIEW" : "CREATE VIEW";
1638 :
1639 79020 : (void) check; /* Stefan: unused!? */
1640 :
1641 79020 : if (sname && !(s = mvc_bind_schema(sql, sname)))
1642 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
1643 79020 : if (create && (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && persistent == SQL_LOCAL_TEMP)))
1644 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), s->base.name);
1645 22373 : if (create && (t = mvc_bind_table(sql, s, name))) {
1646 21 : if (!replace)
1647 1 : return sql_error(sql, 02, SQLSTATE(42S01) "%s: name '%s' already in use", base, name);
1648 20 : if (!isView(t))
1649 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: '%s' is not a view", base, name);
1650 20 : if (t->system)
1651 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: cannot replace system view '%s'", base, name);
1652 20 : foundid = t->base.id; /* when recreating a view, the view itself can't be found */
1653 : }
1654 :
1655 79019 : if (ast) {
1656 79019 : sql_rel *sq = NULL;
1657 79019 : char *q = QUERY(sql->scanner);
1658 79019 : symbol *sym = ast;
1659 :
1660 79019 : if (sym->token == SQL_WITH)
1661 5663 : sym = sym->data.lval->h->next->data.sym;
1662 79019 : if (sym->token == SQL_SELECT) {
1663 76154 : SelectNode *sn = (SelectNode *) sym;
1664 :
1665 76154 : if (sn->limit || sn->sample)
1666 4 : return sql_error(sql, 01, SQLSTATE(42000) "%s: %s not supported", base, sn->limit ? "LIMIT" : "SAMPLE");
1667 : }
1668 :
1669 79016 : pfoundid = sql->objid;
1670 79016 : sql->objid = foundid; /* when recreating a view, the view itself can't be found */
1671 79016 : sq = schema_selects(query, s, ast);
1672 79016 : sql->objid = pfoundid;
1673 79016 : if (!sq)
1674 : return NULL;
1675 79001 : if (!is_project(sq->op)) /* make sure sq is a projection */
1676 0 : sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 1));
1677 :
1678 79001 : if (!create) {
1679 56642 : if (column_spec) {
1680 13819 : dnode *n = column_spec->h;
1681 13819 : node *m = sq->exps->h;
1682 :
1683 63706 : for (; n && m; n = n->next, m = m->next)
1684 : ;
1685 13819 : if (n || m) {
1686 2 : sql_error(sql, 01, SQLSTATE(21S02) "WITH CLAUSE: number of columns does not match");
1687 2 : rel_destroy(sq);
1688 2 : return NULL;
1689 : }
1690 : }
1691 : }
1692 :
1693 56640 : if (create) {
1694 22359 : q = query_cleaned(sql->ta, q);
1695 22359 : switch (mvc_create_view(&t, sql, s, name, SQL_DECLARED_TABLE, q, 0)) {
1696 0 : case -1:
1697 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1698 0 : case -2:
1699 : case -3:
1700 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: transaction conflict detected", base);
1701 : default:
1702 22359 : break;
1703 : }
1704 22359 : if (as_subquery(sql, t, tt_view, sq, column_spec, base) != 0) {
1705 1 : rel_destroy(sq);
1706 1 : return NULL;
1707 : }
1708 22358 : return rel_create_view_ddl(sql, ddl_create_view, s->base.name, t, SQL_PERSIST, replace);
1709 : }
1710 56640 : if (!persistent && column_spec)
1711 12155 : sq = view_rename_columns(sql, name, sq, column_spec);
1712 56640 : if (sq && is_simple_project(sq->op) && sq->l && sq->exps && sq->card == CARD_AGGR) {
1713 4043 : exps_setcard(sq->exps, CARD_MULTI);
1714 4043 : sq->card = CARD_MULTI;
1715 : }
1716 56640 : return sq;
1717 : }
1718 : return NULL;
1719 : }
1720 :
1721 : static sql_rel *
1722 321 : rel_schema2(allocator *sa, int cat_type, char *sname, char *auth, int nr)
1723 : {
1724 321 : sql_rel *rel = rel_create(sa);
1725 321 : list *exps = new_exp_list(sa);
1726 321 : if (!rel || !exps)
1727 : return NULL;
1728 :
1729 321 : append(exps, exp_atom_clob(sa, sname));
1730 321 : append(exps, exp_atom_clob(sa, auth));
1731 321 : append(exps, exp_atom_int(sa, nr));
1732 321 : rel->l = NULL;
1733 321 : rel->r = NULL;
1734 321 : rel->op = op_ddl;
1735 321 : rel->flag = cat_type;
1736 321 : rel->exps = exps;
1737 321 : rel->card = 0;
1738 321 : rel->nrcols = 0;
1739 321 : return rel;
1740 : }
1741 :
1742 : static sql_rel *
1743 906 : rel_schema3(allocator *sa, int cat_type, char *sname, char *tname, char *name)
1744 : {
1745 906 : sql_rel *rel = rel_create(sa);
1746 906 : list *exps = new_exp_list(sa);
1747 906 : if (!rel || !exps)
1748 : return NULL;
1749 :
1750 906 : append(exps, exp_atom_clob(sa, sname));
1751 906 : append(exps, exp_atom_clob(sa, tname));
1752 906 : append(exps, exp_atom_clob(sa, name));
1753 906 : rel->l = NULL;
1754 906 : rel->r = NULL;
1755 906 : rel->op = op_ddl;
1756 906 : rel->flag = cat_type;
1757 906 : rel->exps = exps;
1758 906 : rel->card = 0;
1759 906 : rel->nrcols = 0;
1760 906 : return rel;
1761 : }
1762 :
1763 : static sql_rel *
1764 5 : rel_drop_type(mvc *sql, dlist *qname, int drop_action)
1765 : {
1766 5 : char *name = qname_schema_object(qname);
1767 5 : char *sname = qname_schema(qname);
1768 5 : sql_type *t = NULL;
1769 :
1770 5 : if (!(t = find_type_on_scope(sql, sname, name, "DROP TYPE")))
1771 : return NULL;
1772 4 : if (!mvc_schema_privs(sql, t->s))
1773 0 : return sql_error(sql, 02, SQLSTATE(42000) "DROP TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
1774 4 : return rel_schema2(sql->sa, ddl_drop_type, t->s->base.name, name, drop_action);
1775 : }
1776 :
1777 : static sql_rel *
1778 906 : rel_create_type(mvc *sql, dlist *qname, char *impl)
1779 : {
1780 906 : char *name = qname_schema_object(qname);
1781 906 : char *sname = qname_schema(qname);
1782 906 : sql_schema *s = cur_schema(sql);
1783 :
1784 906 : if (sname && !(s = mvc_bind_schema(sql, sname)))
1785 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "CREATE TYPE: no such schema '%s'", sname);
1786 906 : if (schema_bind_type(sql, s, name) != NULL)
1787 0 : return sql_error(sql, 02, SQLSTATE(42S01) "CREATE TYPE: name '%s' already in use", name);
1788 906 : if (!mvc_schema_privs(sql, s))
1789 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1790 906 : return rel_schema3(sql->sa, ddl_create_type, s->base.name, name, impl);
1791 : }
1792 :
1793 : static char *
1794 1276 : dlist_get_schema_name(dlist *name_auth)
1795 : {
1796 1276 : assert(name_auth && name_auth->h);
1797 1276 : return name_auth->h->data.sval;
1798 : }
1799 :
1800 : static char *
1801 1085 : schema_auth(dlist *name_auth)
1802 : {
1803 1085 : assert(name_auth && name_auth->h && dlist_length(name_auth) == 2);
1804 1085 : return name_auth->h->next->data.sval;
1805 : }
1806 :
1807 : static sql_rel *
1808 4328 : rel_drop(allocator *sa, int cat_type, char *sname, char *first_val, char *second_val, int nr, int exists_check)
1809 : {
1810 4328 : sql_rel *rel = rel_create(sa);
1811 4328 : list *exps = new_exp_list(sa);
1812 :
1813 4328 : append(exps, exp_atom_int(sa, nr));
1814 4328 : append(exps, exp_atom_clob(sa, sname));
1815 4328 : if (first_val)
1816 4137 : append(exps, exp_atom_clob(sa, first_val));
1817 4328 : if (second_val)
1818 150 : append(exps, exp_atom_clob(sa, second_val));
1819 4328 : append(exps, exp_atom_int(sa, exists_check));
1820 4328 : rel->l = NULL;
1821 4328 : rel->r = NULL;
1822 4328 : rel->op = op_ddl;
1823 4328 : rel->flag = cat_type;
1824 4328 : rel->exps = exps;
1825 4328 : rel->card = 0;
1826 4328 : rel->nrcols = 0;
1827 4328 : return rel;
1828 : }
1829 :
1830 : static sql_rel *
1831 1082 : rel_create_schema_dll(allocator *sa, char *sname, char *auth, int nr)
1832 : {
1833 1082 : sql_rel *rel = rel_create(sa);
1834 1082 : list *exps = new_exp_list(sa);
1835 1082 : if (!rel || !exps)
1836 : return NULL;
1837 :
1838 1082 : append(exps, exp_atom_int(sa, nr));
1839 1082 : append(exps, exp_atom_clob(sa, sname));
1840 1082 : if (auth)
1841 38 : append(exps, exp_atom_clob(sa, auth));
1842 1082 : rel->l = NULL;
1843 1082 : rel->r = NULL;
1844 1082 : rel->op = op_ddl;
1845 1082 : rel->flag = ddl_create_schema;
1846 1082 : rel->exps = exps;
1847 1082 : rel->card = 0;
1848 1082 : rel->nrcols = 0;
1849 1082 : return rel;
1850 : }
1851 :
1852 : static sql_rel *
1853 1085 : rel_create_schema(sql_query *query, dlist *auth_name, dlist *schema_elements, int if_not_exists)
1854 : {
1855 1085 : mvc *sql = query->sql;
1856 1085 : char *name = dlist_get_schema_name(auth_name);
1857 1085 : char *auth = schema_auth(auth_name);
1858 1085 : sqlid auth_id = sql->role_id;
1859 :
1860 1085 : if (auth && (auth_id = sql_find_auth(sql, auth)) < 0)
1861 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(28000) "CREATE SCHEMA: no such authorization '%s'", auth);
1862 1085 : if (sql->user_id != USER_MONETDB && sql->role_id != ROLE_SYSADMIN)
1863 1 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE SCHEMA: insufficient privileges for user '%s'", get_string_global_var(sql, "current_user"));
1864 1084 : if (!name)
1865 0 : name = auth;
1866 0 : assert(name);
1867 1084 : if (mvc_bind_schema(sql, name)) {
1868 2 : if (!if_not_exists)
1869 1 : return sql_error(sql, 02, SQLSTATE(3F000) "CREATE SCHEMA: name '%s' already in use", name);
1870 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1871 : } else {
1872 1082 : sql_schema *os = cur_schema(sql);
1873 1082 : dnode *n = schema_elements->h;
1874 1082 : sql_schema *ss = SA_ZNEW(sql->sa, sql_schema);
1875 1082 : sql_rel *ret = rel_create_schema_dll(sql->sa, name, auth, 0);
1876 :
1877 1082 : ss->base.name = name;
1878 1082 : ss->auth_id = auth_id;
1879 1082 : ss->owner = sql->user_id;
1880 :
1881 1082 : sql->session->schema = ss;
1882 1084 : while (n) {
1883 2 : sql_rel *res = rel_semantic(query, n->data.sym);
1884 2 : if (!res) {
1885 0 : rel_destroy(ret);
1886 0 : sql->session->schema = os;
1887 0 : return NULL;
1888 : }
1889 2 : ret = rel_list(sql->sa, ret, res);
1890 2 : n = n->next;
1891 : }
1892 1082 : sql->session->schema = os;
1893 1082 : return ret;
1894 : }
1895 : }
1896 :
1897 : static sql_rel *
1898 3766 : sql_drop_table(sql_query *query, dlist *qname, int nr, int if_exists)
1899 : {
1900 3766 : mvc *sql = query->sql;
1901 3766 : char *sname = qname_schema(qname);
1902 3766 : char *tname = qname_schema_object(qname);
1903 3766 : sql_table *t = NULL;
1904 :
1905 3766 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "DROP TABLE", false))) {
1906 93 : if (if_exists) {
1907 48 : sql->errstr[0] = '\0'; /* reset table not found error */
1908 48 : sql->session->status = 0;
1909 48 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1910 : }
1911 : return NULL;
1912 : }
1913 3673 : if (isDeclaredTable(t))
1914 0 : return sql_error(sql, 02, SQLSTATE(42000) "DROP TABLE: cannot drop a declared table");
1915 :
1916 3673 : return rel_drop(sql->sa, ddl_drop_table, t->s->base.name, tname, NULL, nr, if_exists);
1917 : }
1918 :
1919 : static sql_rel *
1920 334 : sql_drop_view(sql_query *query, dlist *qname, int nr, int if_exists)
1921 : {
1922 334 : mvc *sql = query->sql;
1923 334 : char *sname = qname_schema(qname);
1924 334 : char *tname = qname_schema_object(qname);
1925 334 : sql_table *t = NULL;
1926 :
1927 334 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "DROP VIEW", true))) {
1928 19 : if (if_exists) {
1929 1 : sql->errstr[0] = '\0'; /* reset table not found error */
1930 1 : sql->session->status = 0;
1931 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1932 : }
1933 : return NULL;
1934 : }
1935 315 : if (!isView(t))
1936 1 : return sql_error(sql, 02, SQLSTATE(42000) "DROP VIEW: unable to drop view '%s': is a table", tname);
1937 :
1938 314 : return rel_drop(sql->sa, ddl_drop_view, t->s->base.name, tname, NULL, nr, if_exists);
1939 : }
1940 :
1941 : static sql_rel *
1942 4383 : sql_alter_table(sql_query *query, dlist *dl, dlist *qname, symbol *te, int if_exists)
1943 : {
1944 4383 : mvc *sql = query->sql;
1945 4383 : char *sname = qname_schema(qname);
1946 4383 : char *tname = qname_schema_object(qname);
1947 4383 : sql_table *t = NULL, *nt = NULL;
1948 4383 : sql_rel *res = NULL, *r;
1949 4383 : sql_exp **updates, *e;
1950 :
1951 4383 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "ALTER TABLE", false))) {
1952 15 : if (if_exists) {
1953 1 : sql->errstr[0] = '\0'; /* reset table not found error */
1954 1 : sql->session->status = 0;
1955 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1956 : }
1957 : return NULL;
1958 : }
1959 4368 : if (isDeclaredTable(t))
1960 0 : return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: can't alter declared table '%s'", tname);
1961 4368 : if (isTempSchema(t->s))
1962 3 : return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: can't alter temporary table '%s'", tname);
1963 4365 : if (!mvc_schema_privs(sql, t->s))
1964 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
1965 :
1966 4365 : assert(te);
1967 4365 : if (t->persistence != SQL_DECLARED_TABLE)
1968 4365 : sname = t->s->base.name;
1969 :
1970 4365 : if ((te->token == SQL_TABLE || te->token == SQL_DROP_TABLE)) {
1971 795 : dlist *nqname = te->data.lval->h->data.lval;
1972 795 : sql_table *pt = NULL;
1973 795 : char *nsname = qname_schema(nqname);
1974 795 : char *ntname = qname_schema_object(nqname);
1975 :
1976 795 : if (!(pt = find_table_or_view_on_scope(sql, t->s, nsname, ntname, "ALTER TABLE", false)))
1977 : return NULL;
1978 785 : if (isView(pt))
1979 1 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a view into a %s",
1980 1 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
1981 784 : if (isDeclaredTable(pt))
1982 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a declared table into a %s",
1983 0 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
1984 784 : if (isTempSchema(pt->s))
1985 2 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a temporary table into a %s",
1986 2 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
1987 782 : if (isReplicaTable(t) && isMergeTable(pt))
1988 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: can't add/drop a %s table into a %s",
1989 0 : TABLE_TYPE_DESCRIPTION(pt->type, pt->properties), TABLE_TYPE_DESCRIPTION(t->type, t->properties));
1990 782 : nsname = pt->s->base.name;
1991 782 : if (strcmp(sname, nsname) != 0)
1992 1 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: all children tables of '%s.%s' must be part of schema '%s'",
1993 : sname, tname, sname);
1994 :
1995 781 : if (te->token == SQL_TABLE) {
1996 601 : symbol *extra = dl->h->next->next->next->data.sym;
1997 :
1998 601 : if (!extra) {
1999 314 : if (isRangePartitionTable(t)) {
2000 1 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: a range partition is required while adding under a %s",
2001 1 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
2002 313 : } else if (isListPartitionTable(t)) {
2003 1 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: a value partition is required while adding under a %s",
2004 1 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
2005 : }
2006 312 : return rel_alter_table(sql->sa, ddl_alter_table_add_table, sname, tname, nsname, ntname, 0);
2007 : }
2008 287 : if ((isMergeTable(pt) || isReplicaTable(pt)) && list_length(pt->members)==0)
2009 2 : return sql_error(sql, 02, SQLSTATE(42000) "%s '%s'.'%s' should have at least one table associated",
2010 2 : TABLE_TYPE_DESCRIPTION(pt->type, pt->properties), pt->s->base.name, pt->base.name);
2011 :
2012 285 : if (extra->token == SQL_MERGE_PARTITION) { /* partition to hold null values only */
2013 17 : dlist* ll = extra->data.lval;
2014 17 : int update = ll->h->next->next->next->data.i_val;
2015 :
2016 17 : if (isRangePartitionTable(t)) {
2017 14 : return rel_alter_table_add_partition_range(query, t, pt, sname, tname, nsname, ntname, NULL, NULL, true, update);
2018 3 : } else if (isListPartitionTable(t)) {
2019 3 : return rel_alter_table_add_partition_list(query, t, pt, sname, tname, nsname, ntname, NULL, true, update);
2020 : } else {
2021 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot add a partition into a %s",
2022 0 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
2023 : }
2024 268 : } else if (extra->token == SQL_PARTITION_RANGE) {
2025 208 : dlist* ll = extra->data.lval;
2026 208 : symbol* min = ll->h->data.sym, *max = ll->h->next->data.sym;
2027 208 : int nills = ll->h->next->next->data.i_val, update = ll->h->next->next->next->data.i_val;
2028 :
2029 208 : if (!isRangePartitionTable(t)) {
2030 2 : return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: cannot add a range partition into a %s",
2031 1 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
2032 : }
2033 :
2034 207 : assert(nills == 0 || nills == 1);
2035 207 : return rel_alter_table_add_partition_range(query, t, pt, sname, tname, nsname, ntname, min, max, (bit) nills, update);
2036 60 : } else if (extra->token == SQL_PARTITION_LIST) {
2037 60 : dlist* ll = extra->data.lval, *values = ll->h->data.lval;
2038 60 : int nills = ll->h->next->data.i_val, update = ll->h->next->next->data.i_val;
2039 :
2040 60 : if (!isListPartitionTable(t)) {
2041 2 : return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: cannot add a value partition into a %s",
2042 1 : TABLE_TYPE_DESCRIPTION(t->type, t->properties));
2043 : }
2044 :
2045 59 : assert(nills == 0 || nills == 1);
2046 59 : return rel_alter_table_add_partition_list(query, t, pt, sname, tname, nsname, ntname, values, (bit) nills, update);
2047 : }
2048 0 : assert(0);
2049 : } else {
2050 180 : int drop_action = te->data.lval->h->next->data.i_val;
2051 :
2052 180 : return rel_alter_table(sql->sa, ddl_alter_table_del_table, sname, tname, nsname, ntname, drop_action);
2053 : }
2054 : }
2055 :
2056 : /* read only or read write */
2057 3570 : if (te->token == SQL_ALTER_TABLE) {
2058 2310 : int state = te->data.i_val;
2059 :
2060 2310 : if (state == tr_readonly) {
2061 : state = TABLE_READONLY;
2062 : } else if (state == tr_append) {
2063 : state = TABLE_APPENDONLY;
2064 : } else {
2065 0 : assert(state == tr_writable);
2066 : state = TABLE_WRITABLE;
2067 : }
2068 2310 : return rel_alter_table(sql->sa, ddl_alter_table_set_access, sname, tname, NULL, NULL, state);
2069 : }
2070 :
2071 1260 : nt = dup_sql_table(sql->sa, t);
2072 1260 : if (!nt || (table_element(query, te, t->s, nt, 1, t->persistence == SQL_DECLARED_TABLE, "ALTER TABLE") == SQL_ERR))
2073 76 : return NULL;
2074 :
2075 1184 : if (te->token == SQL_DROP_CONSTRAINT) {
2076 150 : dlist *l = te->data.lval;
2077 150 : char *kname = l->h->data.sval;
2078 150 : int drop_action = l->h->next->data.i_val;
2079 :
2080 150 : return rel_drop(sql->sa, ddl_drop_constraint, sname, tname, kname, drop_action, 0);
2081 : }
2082 :
2083 1034 : res = rel_table(sql, ddl_alter_table, sname, nt, 0);
2084 1034 : sql_rel *bt = rel_ddl_basetable_get(res);
2085 :
2086 1034 : if (!isTable(nt))
2087 : return res;
2088 :
2089 : /* New columns need update with default values. Add one more element for new column */
2090 1009 : updates = SA_ZNEW_ARRAY(sql->sa, sql_exp*, (ol_length(nt->columns) + 1));
2091 1009 : rel_base_use_tid(sql, bt);
2092 :
2093 1009 : e = basetable_get_tid_or_add_it(sql, bt);
2094 1009 : e = exp_ref(sql, e);
2095 :
2096 : /*
2097 : e = exp_column(sql->sa, nt->base.name, TID, sql_bind_localtype("oid"), CARD_MULTI, 0, 1, 1);
2098 : e->alias.label = rel_base_nid(bt, NULL);
2099 : */
2100 1009 : r = rel_project(sql->sa, res, append(new_exp_list(sql->sa),e));
2101 :
2102 1009 : list *cols = new_exp_list(sql->sa);
2103 1009 : sql_exp *ne;
2104 14571 : for (node *n = ol_first_node(nt->columns); n; n = n->next) {
2105 13562 : sql_column *c = n->data;
2106 :
2107 13562 : rel_base_use(sql, bt, c->colnr);
2108 : /* handle new columns */
2109 13562 : if (!c->base.new || c->base.deleted)
2110 13457 : continue;
2111 105 : if (c->def) {
2112 5 : e = rel_parse_val(sql, nt->s, c->def, &c->type, sql->emode, NULL);
2113 : } else {
2114 100 : e = exp_atom(sql->sa, atom_general(sql->sa, &c->type, NULL, 0));
2115 : }
2116 105 : if (!e || (e = exp_check_type(sql, &c->type, r, e, type_equal)) == NULL) {
2117 0 : rel_destroy(r);
2118 0 : return NULL;
2119 : }
2120 105 : list_append(cols, ne=exp_column(sql->sa, nt->base.name, c->base.name, &c->type, CARD_MULTI, 0, 0, 0));
2121 105 : ne->alias.label = rel_base_nid(bt, c);
2122 105 : ne->nid = ne->alias.label;
2123 :
2124 105 : assert(!updates[c->colnr]);
2125 105 : exp_setname(sql, e, c->t->base.name, c->base.name);
2126 105 : updates[c->colnr] = e;
2127 : }
2128 1009 : res = rel_update(sql, res, r, updates, list_length(cols)?cols:NULL);
2129 1009 : return res;
2130 : }
2131 :
2132 : static sql_rel *
2133 50 : rel_role(allocator *sa, char *grantee, char *auth, int grantor, int admin, int type)
2134 : {
2135 50 : sql_rel *rel = rel_create(sa);
2136 50 : list *exps = new_exp_list(sa);
2137 50 : if (!rel || !exps)
2138 : return NULL;
2139 :
2140 50 : assert(type == ddl_grant_roles || type == ddl_revoke_roles);
2141 50 : append(exps, exp_atom_clob(sa, grantee));
2142 50 : append(exps, exp_atom_clob(sa, auth));
2143 50 : append(exps, exp_atom_int(sa, grantor));
2144 50 : append(exps, exp_atom_int(sa, admin));
2145 50 : rel->l = NULL;
2146 50 : rel->r = NULL;
2147 50 : rel->op = op_ddl;
2148 50 : rel->flag = type;
2149 50 : rel->exps = exps;
2150 50 : rel->card = 0;
2151 50 : rel->nrcols = 0;
2152 50 : return rel;
2153 : }
2154 :
2155 : static sql_rel *
2156 50 : rel_grant_or_revoke_roles(mvc *sql, dlist *roles, dlist *grantees, int grant, int grantor, ddl_statement action)
2157 : {
2158 50 : sql_rel *res = NULL;
2159 : /* grant/revoke roles to the grantees */
2160 :
2161 100 : for (dnode *r = roles->h; r; r = r->next) {
2162 50 : char *role = r->data.sval;
2163 :
2164 100 : for (dnode *g = grantees->h; g; g = g->next) {
2165 50 : char *grantee = g->data.sval;
2166 :
2167 50 : if ((res = rel_list(sql->sa, res, rel_role(sql->sa, grantee, role, grantor, grant, action))) == NULL) {
2168 0 : rel_destroy(res);
2169 0 : return NULL;
2170 : }
2171 : }
2172 : }
2173 : return res;
2174 : }
2175 :
2176 : static sql_rel *
2177 18470 : rel_priv(allocator *sa, char *sname, char *name, char *grantee, int privs, char *cname, int grant, int grantor, int type)
2178 : {
2179 18470 : sql_rel *rel = rel_create(sa);
2180 18470 : list *exps = new_exp_list(sa);
2181 18470 : if (!rel || !exps)
2182 : return NULL;
2183 :
2184 18470 : assert(type == ddl_grant || type == ddl_revoke);
2185 18470 : append(exps, exp_atom_clob(sa, sname));
2186 18470 : append(exps, exp_atom_clob(sa, name));
2187 18470 : append(exps, exp_atom_clob(sa, grantee));
2188 18470 : append(exps, exp_atom_int(sa, privs));
2189 18470 : append(exps, cname?(void*)exp_atom_clob(sa, cname):(void*)cname);
2190 18470 : append(exps, exp_atom_int(sa, grant));
2191 18470 : append(exps, exp_atom_int(sa, grantor));
2192 18470 : rel->l = NULL;
2193 18470 : rel->r = NULL;
2194 18470 : rel->op = op_ddl;
2195 18470 : rel->flag = type;
2196 18470 : rel->exps = exps;
2197 18470 : rel->card = 0;
2198 18470 : rel->nrcols = 0;
2199 18470 : return rel;
2200 : }
2201 :
2202 : static sql_rel *
2203 99019 : rel_func_priv(allocator *sa, char *sname, int func, char *grantee, int privs, int grant, int grantor, int type)
2204 : {
2205 99019 : sql_rel *rel = rel_create(sa);
2206 99019 : list *exps = new_exp_list(sa);
2207 99019 : if (!rel || !exps)
2208 : return NULL;
2209 :
2210 99019 : assert(type == ddl_grant_func || type == ddl_revoke_func);
2211 99019 : append(exps, exp_atom_clob(sa, sname));
2212 99019 : append(exps, exp_atom_int(sa, func));
2213 99019 : append(exps, exp_atom_clob(sa, grantee));
2214 99019 : append(exps, exp_atom_int(sa, privs));
2215 99019 : append(exps, exp_atom_int(sa, grant));
2216 99019 : append(exps, exp_atom_int(sa, grantor));
2217 99019 : rel->l = NULL;
2218 99019 : rel->r = NULL;
2219 99019 : rel->op = op_ddl;
2220 99019 : rel->flag = type;
2221 99019 : rel->exps = exps;
2222 99019 : rel->card = 0;
2223 99019 : rel->nrcols = 0;
2224 99019 : return rel;
2225 : }
2226 :
2227 : static sql_rel *
2228 12 : rel_grant_or_revoke_global(mvc *sql, dlist *privs, dlist *grantees, int grant, int grantor, ddl_statement action)
2229 : {
2230 12 : sql_rel *res = NULL;
2231 12 : char *sname = cur_schema(sql)->base.name;
2232 :
2233 12 : if (!privs)
2234 : return NULL;
2235 24 : for (dnode *gn = grantees->h; gn; gn = gn->next) {
2236 12 : char *grantee = gn->data.sval;
2237 :
2238 12 : if (!grantee)
2239 0 : grantee = "public";
2240 :
2241 24 : for (dnode *opn = privs->h; opn; opn = opn->next) {
2242 12 : int priv = opn->data.i_val;
2243 :
2244 12 : if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, sname, NULL, grantee, priv, NULL, grant, grantor, action))) == NULL) {
2245 0 : rel_destroy(res);
2246 0 : return NULL;
2247 : }
2248 : }
2249 : }
2250 : return res;
2251 : }
2252 :
2253 : static sql_rel *
2254 18420 : rel_grant_or_revoke_table(mvc *sql, dlist *privs, dlist *qname, dlist *grantees, int grant, int grantor, ddl_statement action, const char *err)
2255 : {
2256 18420 : sql_rel *res = NULL;
2257 18420 : int all = PRIV_SELECT | PRIV_UPDATE | PRIV_INSERT | PRIV_DELETE | PRIV_TRUNCATE;
2258 18420 : char *sname = qname_schema(qname);
2259 18420 : char *tname = qname_schema_object(qname);
2260 18420 : sql_table *t = NULL;
2261 :
2262 18420 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, err, false)))
2263 : return NULL;
2264 18419 : if (isDeclaredTable(t))
2265 0 : return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s on a declared table", err);
2266 36838 : for (dnode *gn = grantees->h; gn; gn = gn->next) {
2267 18419 : char *grantee = gn->data.sval;
2268 :
2269 18419 : if (!grantee)
2270 18304 : grantee = "public";
2271 :
2272 18419 : if (!privs) {
2273 7 : if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, all, NULL, grant, grantor, action))) == NULL) {
2274 0 : rel_destroy(res);
2275 0 : return NULL;
2276 : }
2277 7 : continue;
2278 : }
2279 36850 : for (dnode *opn = privs->h; opn; opn = opn->next) {
2280 18438 : symbol *op = opn->data.sym;
2281 18438 : int priv = PRIV_SELECT;
2282 :
2283 18438 : switch (op->token) {
2284 : case SQL_SELECT:
2285 : priv = PRIV_SELECT;
2286 : break;
2287 28 : case SQL_UPDATE:
2288 28 : priv = PRIV_UPDATE;
2289 28 : break;
2290 28 : case SQL_INSERT:
2291 28 : priv = PRIV_INSERT;
2292 28 : break;
2293 21 : case SQL_DELETE:
2294 21 : priv = PRIV_DELETE;
2295 21 : break;
2296 1 : case SQL_TRUNCATE:
2297 1 : priv = PRIV_TRUNCATE;
2298 1 : break;
2299 0 : case SQL_EXECUTE:
2300 : default:
2301 0 : return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s EXECUTE on table name %s", err, tname);
2302 : }
2303 :
2304 18438 : if ((op->token == SQL_SELECT || op->token == SQL_UPDATE) && op->data.lval) {
2305 103 : for (dnode *cn = op->data.lval->h; cn; cn = cn->next) {
2306 58 : char *cname = cn->data.sval;
2307 58 : if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, priv, cname, grant, grantor, action))) == NULL) {
2308 0 : rel_destroy(res);
2309 0 : return NULL;
2310 : }
2311 : }
2312 18393 : } else if ((res = rel_list(sql->sa, res, rel_priv(sql->sa, t->s->base.name, tname, grantee, priv, NULL, grant, grantor, action))) == NULL) {
2313 0 : rel_destroy(res);
2314 0 : return NULL;
2315 : }
2316 : }
2317 : }
2318 : return res;
2319 : }
2320 :
2321 : static sql_rel *
2322 99020 : rel_grant_or_revoke_func(mvc *sql, dlist *privs, dlist *qname, dlist *typelist, sql_ftype type, dlist *grantees, int grant, int grantor, ddl_statement action, const char *err)
2323 : {
2324 99020 : sql_rel *res = NULL;
2325 99020 : char *sname = qname_schema(qname);
2326 99020 : char *fname = qname_schema_object(qname);
2327 99020 : sql_func *func = resolve_func(sql, sname, fname, typelist, type, err, 0);
2328 :
2329 99020 : if (!func)
2330 : return NULL;
2331 99019 : if (!func->s)
2332 0 : return sql_error(sql, 02, SQLSTATE(42000) "Cannot %s EXECUTE on system function '%s'", err, fname);
2333 198038 : for (dnode *gn = grantees->h; gn; gn = gn->next) {
2334 99019 : char *grantee = gn->data.sval;
2335 :
2336 99019 : if (!grantee)
2337 98544 : grantee = "public";
2338 :
2339 99019 : if (!privs) {
2340 4 : if ((res = rel_list(sql->sa, res, rel_func_priv(sql->sa, func->s->base.name, func->base.id, grantee, PRIV_EXECUTE, grant, grantor, action))) == NULL) {
2341 0 : rel_destroy(res);
2342 0 : return NULL;
2343 : }
2344 4 : continue;
2345 : }
2346 198030 : for (dnode *opn = privs->h; opn; opn = opn->next) {
2347 99015 : symbol *op = opn->data.sym;
2348 :
2349 99015 : if (op->token != SQL_EXECUTE)
2350 0 : return sql_error(sql, 02, SQLSTATE(42000) "Can only %s 'EXECUTE' on function '%s'", err, fname);
2351 99015 : if ((res = rel_list(sql->sa, res, rel_func_priv(sql->sa, func->s->base.name, func->base.id, grantee, PRIV_EXECUTE, grant, grantor, action))) == NULL) {
2352 0 : rel_destroy(res);
2353 0 : return NULL;
2354 : }
2355 : }
2356 : }
2357 : return res;
2358 : }
2359 :
2360 : static sql_rel *
2361 117452 : rel_grant_or_revoke_privs(mvc *sql, dlist *privs, dlist *grantees, int grant, int grantor, ddl_statement action)
2362 : {
2363 117452 : dlist *obj_privs = privs->h->data.lval;
2364 117452 : symbol *obj = privs->h->next->data.sym;
2365 117452 : tokens token = obj->token;
2366 117452 : const char *err = (action == ddl_grant) ? "GRANT" : "REVOKE";
2367 :
2368 117452 : switch (token) {
2369 12 : case SQL_GRANT:
2370 12 : return rel_grant_or_revoke_global(sql, obj_privs, grantees, grant, grantor, action);
2371 18420 : case SQL_TABLE:
2372 : case SQL_NAME:
2373 18420 : return rel_grant_or_revoke_table(sql, obj_privs, obj->data.lval, grantees, grant, grantor, action, err);
2374 99020 : case SQL_FUNC: {
2375 99020 : dlist *r = obj->data.lval;
2376 99020 : dlist *qname = r->h->data.lval;
2377 99020 : dlist *typelist = r->h->next->data.lval;
2378 99020 : sql_ftype type = (sql_ftype) r->h->next->next->data.i_val;
2379 :
2380 99021 : return rel_grant_or_revoke_func(sql, obj_privs, qname, typelist, type, grantees, grant, grantor, (action == ddl_grant) ? ddl_grant_func : ddl_revoke_func, err);
2381 : }
2382 0 : default:
2383 0 : return sql_error(sql, 02, SQLSTATE(M0M03) "%s: unknown token %d", err, (int) token);
2384 : }
2385 : }
2386 :
2387 : /* iname, itype, sname.tname (col1 .. coln) */
2388 : static sql_rel *
2389 343 : rel_create_index(mvc *sql, char *iname, idx_type itype, dlist *qname, dlist *column_list)
2390 : {
2391 343 : sql_table *t = NULL, *nt;
2392 343 : sql_rel *r, *res;
2393 343 : sql_exp **updates, *e;
2394 343 : sql_idx *i;
2395 343 : dnode *n;
2396 343 : char *sname = qname_schema(qname), *tname = qname_schema_object(qname), *s = iname;
2397 :
2398 343 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "CREATE INDEX", false)))
2399 : return NULL;
2400 342 : if (isDeclaredTable(t))
2401 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: cannot create index on a declared table");
2402 342 : if (!mvc_schema_privs(sql, t->s))
2403 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
2404 342 : if (!s || !*s) /* add this to be safe */
2405 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: index name cannot be empty");
2406 342 : while (isdigit((unsigned char) *s))
2407 0 : s++;
2408 342 : if (!*s) /* if an index name just contains digit characters, it can be mistaken with a label */
2409 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: index name cannot contain just digit characters (0 through 9)");
2410 342 : if ((i = mvc_bind_idx(sql, t->s, iname)))
2411 3 : return sql_error(sql, 02, SQLSTATE(42S11) "CREATE INDEX: name '%s' already in use", iname);
2412 339 : if (ol_find_name(t->keys, iname) || mvc_bind_key(sql, t->s, iname))
2413 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: a key named '%s' already exists, and it would conflict with the index", iname);
2414 339 : if (!isTable(t))
2415 2 : return sql_error(sql, 02, SQLSTATE(42S02) "CREATE INDEX: cannot create index on %s '%s'", TABLE_TYPE_DESCRIPTION(t->type, t->properties), tname);
2416 337 : nt = dup_sql_table(sql->sa, t);
2417 :
2418 337 : if (t->persistence != SQL_DECLARED_TABLE)
2419 337 : sname = t->s->base.name;
2420 :
2421 : /* add index here */
2422 337 : switch (mvc_create_idx(&i, sql, nt, iname, itype)) {
2423 0 : case -1:
2424 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
2425 0 : case -2:
2426 : case -3:
2427 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: transaction conflict detected");
2428 : default:
2429 337 : break;
2430 : }
2431 768 : for (n = column_list->h; n; n = n->next) {
2432 433 : sql_column *c = mvc_bind_column(sql, nt, n->data.sval);
2433 :
2434 433 : if (!c)
2435 2 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "CREATE INDEX: no such column '%s'", n->data.sval);
2436 431 : switch (mvc_create_ic(sql, i, c)) {
2437 0 : case -1:
2438 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
2439 0 : case -2:
2440 : case -3:
2441 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE INDEX: transaction conflict detected");
2442 : default:
2443 431 : break;
2444 : }
2445 : }
2446 335 : mvc_create_idx_done(sql, i);
2447 :
2448 : /* new columns need update with default values */
2449 335 : updates = SA_ZNEW_ARRAY(sql->sa, sql_exp*, ol_length(nt->columns));
2450 :
2451 335 : res = rel_table(sql, ddl_alter_table, sname, nt, 0);
2452 335 : e = exp_column(sql->sa, nt->base.name, TID, sql_bind_localtype("oid"), CARD_MULTI, 0, 1, 1);
2453 335 : sql_rel *bt = rel_ddl_basetable_get(res);
2454 335 : e->alias.label = rel_base_nid(bt, NULL);
2455 335 : e->nid = e->alias.label;
2456 335 : r = rel_project(sql->sa, res, append(new_exp_list(sql->sa),e));
2457 335 : res = rel_update(sql, res, r, updates, NULL);
2458 335 : return res;
2459 : }
2460 :
2461 : static sql_rel *
2462 358 : rel_create_user(allocator *sa, char *user, char *passwd, int enc, char *fullname, char *schema, char *schema_path, lng max_memory, int max_workers, char *optimizer, char *default_role)
2463 : {
2464 358 : sql_rel *rel = rel_create(sa);
2465 358 : list *exps = new_exp_list(sa);
2466 358 : if (!rel || !exps)
2467 : return NULL;
2468 :
2469 358 : append(exps, exp_atom_clob(sa, user));
2470 358 : append(exps, exp_atom_clob(sa, passwd));
2471 358 : append(exps, exp_atom_int(sa, enc));
2472 358 : append(exps, exp_atom_clob(sa, schema));
2473 358 : append(exps, exp_atom_clob(sa, schema_path));
2474 358 : append(exps, exp_atom_clob(sa, fullname));
2475 358 : append(exps, exp_atom_lng(sa, max_memory >= 0 ? max_memory : 0));
2476 358 : append(exps, exp_atom_int(sa, max_workers >= 0 ? max_workers: 0));
2477 358 : append(exps, exp_atom_clob(sa, optimizer));
2478 358 : append(exps, exp_atom_clob(sa, default_role));
2479 358 : rel->l = NULL;
2480 358 : rel->r = NULL;
2481 358 : rel->op = op_ddl;
2482 358 : rel->flag = ddl_create_user;
2483 358 : rel->exps = exps;
2484 358 : rel->card = 0;
2485 358 : rel->nrcols = 0;
2486 358 : return rel;
2487 : }
2488 :
2489 : static sql_rel *
2490 83 : rel_alter_user(allocator *sa, char *user, char *passwd, int enc, char *schema, char *schema_path, char *oldpasswd, char *role, lng max_memory, int max_workers)
2491 : {
2492 83 : sql_rel *rel = rel_create(sa);
2493 83 : list *exps = new_exp_list(sa);
2494 83 : if (!rel || !exps)
2495 : return NULL;
2496 :
2497 83 : append(exps, exp_atom_clob(sa, user));
2498 83 : append(exps, exp_atom_clob(sa, passwd));
2499 83 : append(exps, exp_atom_int(sa, enc));
2500 83 : append(exps, exp_atom_clob(sa, schema));
2501 83 : append(exps, exp_atom_clob(sa, schema_path));
2502 83 : append(exps, exp_atom_clob(sa, oldpasswd));
2503 83 : append(exps, exp_atom_clob(sa, role));
2504 83 : append(exps, exp_atom_lng(sa, max_memory));
2505 83 : append(exps, exp_atom_int(sa, max_workers));
2506 :
2507 83 : rel->l = NULL;
2508 83 : rel->r = NULL;
2509 83 : rel->op = op_ddl;
2510 83 : rel->flag = ddl_alter_user;
2511 83 : rel->exps = exps;
2512 83 : rel->card = 0;
2513 83 : rel->nrcols = 0;
2514 83 : return rel;
2515 : }
2516 :
2517 : static sqlid
2518 245 : rel_find_designated_schema(mvc *sql, symbol *sym, sql_schema **schema_out) {
2519 245 : char *sname;
2520 245 : sql_schema *s;
2521 :
2522 245 : assert(sym->type == type_string);
2523 245 : sname = sym->data.sval;
2524 245 : if (!(s = mvc_bind_schema(sql, sname))) {
2525 0 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "COMMENT ON: no such schema: '%s'", sname);
2526 0 : return 0;
2527 : }
2528 :
2529 245 : *schema_out = s;
2530 245 : return s->base.id;
2531 : }
2532 :
2533 : static sqlid
2534 29 : rel_find_designated_table(mvc *sql, symbol *sym, sql_schema **schema_out) {
2535 29 : dlist *qname;
2536 29 : char *sname, *tname;
2537 29 : sql_table *t;
2538 29 : int want_table = sym->token == SQL_TABLE;
2539 :
2540 29 : assert(sym->type == type_list);
2541 29 : qname = sym->data.lval;
2542 29 : sname = qname_schema(qname);
2543 29 : tname = qname_schema_object(qname);
2544 29 : t = find_table_or_view_on_scope(sql, NULL, sname, tname, "COMMENT ON", !want_table);
2545 29 : if (t && isDeclaredTable(t)) {
2546 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON declared table not allowed");
2547 0 : return 0;
2548 : }
2549 29 : if (t && t->s && isTempSchema(t->s)) {
2550 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
2551 0 : return 0;
2552 : }
2553 51 : if (t && !want_table == !isKindOfTable(t)) { /* comparing booleans can be tricky */
2554 29 : *schema_out = t->s;
2555 29 : return t->base.id;
2556 : }
2557 :
2558 0 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "COMMENT ON: no such %s: %s%s%s'%s'",
2559 : want_table ? "table" : "view", sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname);
2560 0 : return 0;
2561 : }
2562 :
2563 : static sqlid
2564 24 : rel_find_designated_column(mvc *sql, symbol *sym, sql_schema **schema_out) {
2565 24 : char *sname, *tname, *cname;
2566 24 : dlist *colname;
2567 24 : sql_table *t;
2568 24 : sql_column *c;
2569 :
2570 24 : assert(sym->type == type_list);
2571 24 : colname = sym->data.lval;
2572 24 : assert(colname->cnt == 2 || colname->cnt == 3);
2573 24 : assert(colname->h->type == type_string);
2574 24 : assert(colname->h->next->type == type_string);
2575 24 : if (colname->cnt == 2) {
2576 10 : sname = NULL;
2577 10 : tname = colname->h->data.sval;
2578 10 : cname = colname->h->next->data.sval;
2579 : } else {
2580 : // cnt == 3
2581 14 : sname = colname->h->data.sval;
2582 14 : tname = colname->h->next->data.sval;
2583 14 : assert(colname->h->next->next->type == type_string);
2584 14 : cname = colname->h->next->next->data.sval;
2585 : }
2586 24 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "COMMENT ON", false)))
2587 : return 0;
2588 24 : if (t && isDeclaredTable(t)) {
2589 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON declared table not allowed");
2590 0 : return 0;
2591 : }
2592 24 : if (t && t->s && isTempSchema(t->s)) {
2593 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
2594 0 : return 0;
2595 : }
2596 24 : if (!(c = mvc_bind_column(sql, t, cname))) {
2597 0 : sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S12) "COMMENT ON: no such column: %s%s%s'%s'.'%s'",
2598 : sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname, cname);
2599 0 : return 0;
2600 : }
2601 24 : *schema_out = t->s;
2602 24 : return c->base.id;
2603 : }
2604 :
2605 : static sqlid
2606 21 : rel_find_designated_index(mvc *sql, symbol *sym, sql_schema **schema_out) {
2607 21 : dlist *qname;
2608 21 : char *iname, *sname;
2609 21 : sql_idx *idx;
2610 :
2611 21 : assert(sym->type == type_list);
2612 21 : qname = sym->data.lval;
2613 21 : sname = qname_schema(qname);
2614 21 : iname = qname_schema_object(qname);
2615 21 : if (!(idx = find_idx_on_scope(sql, sname, iname, "COMMENT ON")))
2616 : return 0;
2617 21 : if (idx && idx->t->s && isTempSchema(idx->t->s)) {
2618 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
2619 0 : return 0;
2620 : }
2621 : if (idx) {
2622 21 : *schema_out = idx->t->s;
2623 21 : return idx->base.id;
2624 : }
2625 :
2626 : return 0;
2627 : }
2628 :
2629 : static sqlid
2630 12 : rel_find_designated_sequence(mvc *sql, symbol *sym, sql_schema **schema_out) {
2631 12 : (void)sql;
2632 12 : (void)sym;
2633 12 : dlist *qname;
2634 12 : char *seqname, *sname;
2635 12 : sql_sequence *seq;
2636 :
2637 12 : assert(sym->type == type_list);
2638 12 : qname = sym->data.lval;
2639 12 : sname = qname_schema(qname);
2640 12 : seqname = qname_schema_object(qname);
2641 :
2642 12 : seq = find_sequence_on_scope(sql, sname, seqname, "COMMENT ON");
2643 12 : if (seq && seq->s && isTempSchema(seq->s)) {
2644 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
2645 0 : return 0;
2646 : }
2647 : if (seq) {
2648 12 : *schema_out = seq->s;
2649 12 : return seq->base.id;
2650 : }
2651 :
2652 : return 0;
2653 : }
2654 :
2655 : static sqlid
2656 24 : rel_find_designated_routine(mvc *sql, symbol *sym, sql_schema **schema_out) {
2657 24 : (void)sql;
2658 24 : (void)sym;
2659 24 : dlist *designator;
2660 24 : dlist *qname;
2661 24 : dlist *typelist;
2662 24 : sql_ftype func_type;
2663 24 : char *fname, *sname;
2664 24 : sql_func *func;
2665 :
2666 24 : assert(sym->type == type_list);
2667 24 : designator = sym->data.lval;
2668 24 : assert(designator->cnt == 3);
2669 24 : qname = designator->h->data.lval;
2670 24 : sname = qname_schema(qname);
2671 24 : typelist = designator->h->next->data.lval;
2672 24 : func_type = (sql_ftype) designator->h->next->next->data.i_val;
2673 :
2674 24 : fname = qname_schema_object(qname);
2675 24 : func = resolve_func(sql, sname, fname, typelist, func_type, "COMMENT", 0);
2676 24 : if (func && func->s && isTempSchema(func->s)) {
2677 0 : sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON tmp object not allowed");
2678 0 : return 0;
2679 : }
2680 : if (func) {
2681 24 : *schema_out = func->s ? func->s : mvc_bind_schema(sql, "sys");
2682 24 : return func->base.id;
2683 : }
2684 :
2685 : return 0;
2686 : }
2687 :
2688 : static sqlid
2689 355 : rel_find_designated_object(mvc *sql, symbol *sym, sql_schema **schema_out)
2690 : {
2691 355 : sql_schema *dummy = NULL;
2692 :
2693 355 : if (schema_out == NULL)
2694 0 : schema_out = &dummy;
2695 355 : switch (sym->token) {
2696 245 : case SQL_SCHEMA:
2697 245 : return rel_find_designated_schema(sql, sym, schema_out);
2698 22 : case SQL_TABLE:
2699 22 : return rel_find_designated_table(sql, sym, schema_out);
2700 7 : case SQL_VIEW:
2701 7 : return rel_find_designated_table(sql, sym, schema_out);
2702 24 : case SQL_COLUMN:
2703 24 : return rel_find_designated_column(sql, sym, schema_out);
2704 21 : case SQL_INDEX:
2705 21 : return rel_find_designated_index(sql, sym, schema_out);
2706 12 : case SQL_SEQUENCE:
2707 12 : return rel_find_designated_sequence(sql, sym, schema_out);
2708 24 : case SQL_ROUTINE:
2709 24 : return rel_find_designated_routine(sql, sym, schema_out);
2710 0 : default:
2711 0 : sql_error(sql, 2, SQLSTATE(42000) "COMMENT ON %s is not supported", token2string(sym->token));
2712 0 : return 0;
2713 : }
2714 : }
2715 :
2716 : static sql_rel *
2717 354 : rel_comment_on(allocator *sa, sqlid obj_id, const char *remark)
2718 : {
2719 354 : sql_rel *rel = rel_create(sa);
2720 354 : list *exps = new_exp_list(sa);
2721 :
2722 354 : if (rel == NULL || exps == NULL)
2723 : return NULL;
2724 :
2725 354 : append(exps, exp_atom_int(sa, obj_id));
2726 354 : append(exps, exp_atom_clob(sa, remark));
2727 354 : rel->l = NULL;
2728 354 : rel->r = NULL;
2729 354 : rel->op = op_ddl;
2730 354 : rel->flag = ddl_comment_on;
2731 354 : rel->exps = exps;
2732 354 : rel->card = 0;
2733 354 : rel->nrcols = 0;
2734 354 : return rel;
2735 : }
2736 :
2737 : static char *
2738 10147 : credentials_username(dlist *credentials)
2739 : {
2740 10147 : if (credentials == NULL) {
2741 : return NULL;
2742 : }
2743 104 : assert(credentials->h);
2744 :
2745 104 : if (credentials->h->data.sval != NULL) {
2746 : return credentials->h->data.sval;
2747 : }
2748 :
2749 : // No username specified.
2750 : return NULL;
2751 : }
2752 :
2753 : static char *
2754 10147 : credentials_password(dlist *credentials)
2755 : {
2756 10147 : if (credentials == NULL) {
2757 : return NULL;
2758 : }
2759 104 : assert(credentials->h);
2760 :
2761 104 : char *password = credentials->h->next->next->data.sval;
2762 :
2763 104 : return password;
2764 : }
2765 :
2766 : static sql_rel *
2767 11 : rel_rename_schema(mvc *sql, char *old_name, char *new_name, int if_exists)
2768 : {
2769 11 : sql_schema *s;
2770 11 : sql_rel *rel;
2771 11 : list *exps;
2772 :
2773 11 : assert(old_name && new_name);
2774 11 : if (!(s = mvc_bind_schema(sql, old_name))) {
2775 1 : if (if_exists)
2776 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
2777 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000)
2778 : "ALTER SCHEMA: no such schema '%s'", old_name);
2779 : }
2780 :
2781 10 : if (!mvc_schema_privs(sql, s))
2782 0 : return sql_error(sql, 02, SQLSTATE(3F000)
2783 : "ALTER SCHEMA: access denied for %s to schema '%s'",
2784 : get_string_global_var(sql, "current_user"), old_name);
2785 :
2786 10 : if (s->system)
2787 1 : return sql_error(sql, 02, SQLSTATE(3F000)
2788 : "ALTER SCHEMA: cannot rename a system schema");
2789 :
2790 9 : if (strNil(new_name) || *new_name == '\0')
2791 0 : return sql_error(sql, 02, SQLSTATE(3F000)
2792 : "ALTER SCHEMA: invalid new schema name");
2793 :
2794 9 : if (mvc_bind_schema(sql, new_name))
2795 0 : return sql_error(sql, 02, SQLSTATE(3F000)
2796 : "ALTER SCHEMA: there is a schema named '%s' in the database", new_name);
2797 :
2798 9 : if (mvc_check_dependency(sql, s->base.id, SCHEMA_DEPENDENCY, NULL) != NO_DEPENDENCY) {
2799 1 : return sql_error(sql, 02,
2800 : SQLSTATE(2BM37) "ALTER SCHEMA: unable to"
2801 : " rename schema '%s', there are database objects"
2802 : " which depend on it", old_name);
2803 : }
2804 :
2805 8 : rel = rel_create(sql->sa);
2806 8 : exps = new_exp_list(sql->sa);
2807 8 : append(exps, exp_atom_clob(sql->sa, old_name));
2808 8 : append(exps, exp_atom_clob(sql->sa, new_name));
2809 8 : rel->op = op_ddl;
2810 8 : rel->flag = ddl_rename_schema;
2811 8 : rel->exps = exps;
2812 :
2813 8 : return rel;
2814 : }
2815 :
2816 : static sql_rel *
2817 23 : rel_rename_table(mvc *sql, char *schema_name, char *old_name, char *new_name, int if_exists)
2818 : {
2819 23 : sql_table *t = NULL;
2820 23 : sql_rel *rel;
2821 23 : list *exps;
2822 :
2823 23 : assert(old_name && new_name);
2824 :
2825 23 : if (!(t = find_table_or_view_on_scope(sql, NULL, schema_name, old_name, "ALTER TABLE", false))) {
2826 1 : if (if_exists) {
2827 1 : sql->errstr[0] = '\0'; /* reset table not found error */
2828 1 : sql->session->status = 0;
2829 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
2830 : }
2831 : return NULL;
2832 : }
2833 22 : if (isDeclaredTable(t))
2834 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a declared table");
2835 22 : if (!mvc_schema_privs(sql, t->s))
2836 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
2837 22 : if (t->system)
2838 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a system table");
2839 22 : if (isView(t))
2840 1 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a view");
2841 21 : if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
2842 2 : return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: unable to rename table '%s' (there are database objects which depend on it)", old_name);
2843 19 : if (strNil(new_name) || *new_name == '\0')
2844 0 : return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new table name");
2845 19 : if (mvc_bind_table(sql, t->s, new_name))
2846 0 : return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", new_name, t->s->base.name);
2847 :
2848 19 : rel = rel_create(sql->sa);
2849 19 : exps = new_exp_list(sql->sa);
2850 19 : append(exps, exp_atom_clob(sql->sa, t->s->base.name));
2851 19 : append(exps, exp_atom_clob(sql->sa, t->s->base.name));
2852 19 : append(exps, exp_atom_clob(sql->sa, old_name));
2853 19 : append(exps, exp_atom_clob(sql->sa, new_name));
2854 19 : rel->op = op_ddl;
2855 19 : rel->flag = ddl_rename_table;
2856 19 : rel->exps = exps;
2857 19 : return rel;
2858 : }
2859 :
2860 : static sql_rel *
2861 20 : rel_rename_column(mvc *sql, char *schema_name, char *table_name, char *old_name, char *new_name, int if_exists)
2862 : {
2863 20 : sql_table *t = NULL;
2864 20 : sql_column *col;
2865 20 : sql_rel *rel;
2866 20 : list *exps;
2867 :
2868 20 : assert(table_name && old_name && new_name);
2869 :
2870 20 : if (!(t = find_table_or_view_on_scope(sql, NULL, schema_name, table_name, "ALTER TABLE", false))) {
2871 2 : if (if_exists) {
2872 1 : sql->errstr[0] = '\0'; /* reset table not found error */
2873 1 : sql->session->status = 0;
2874 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
2875 : }
2876 : return NULL;
2877 : }
2878 18 : if (isDeclaredTable(t))
2879 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a declared table");
2880 18 : if (!mvc_schema_privs(sql, t->s))
2881 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), t->s->base.name);
2882 18 : if (t->system)
2883 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table");
2884 18 : if (isView(t))
2885 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name);
2886 18 : if (!(col = mvc_bind_column(sql, t, old_name)))
2887 5 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name);
2888 13 : if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL))
2889 0 : return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name);
2890 13 : if (strNil(new_name) || *new_name == '\0')
2891 0 : return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new column name");
2892 13 : if (mvc_bind_column(sql, t, new_name))
2893 0 : return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name);
2894 :
2895 13 : rel = rel_create(sql->sa);
2896 13 : exps = new_exp_list(sql->sa);
2897 13 : append(exps, exp_atom_clob(sql->sa, t->s->base.name));
2898 13 : append(exps, exp_atom_clob(sql->sa, table_name));
2899 13 : append(exps, exp_atom_clob(sql->sa, old_name));
2900 13 : append(exps, exp_atom_clob(sql->sa, new_name));
2901 13 : rel->op = op_ddl;
2902 13 : rel->flag = ddl_rename_column;
2903 13 : rel->exps = exps;
2904 13 : return rel;
2905 : }
2906 :
2907 : static sql_rel *
2908 29 : rel_set_table_schema(sql_query *query, char *old_schema, char *tname, char *new_schema, int if_exists)
2909 : {
2910 29 : mvc *sql = query->sql;
2911 29 : sql_schema *ns = NULL;
2912 29 : sql_table *ot = NULL;
2913 29 : sql_rel *rel;
2914 29 : list *exps;
2915 :
2916 29 : assert(tname && new_schema);
2917 :
2918 29 : if (!(ot = find_table_or_view_on_scope(sql, NULL, old_schema, tname, "ALTER TABLE", false))) {
2919 0 : if (if_exists) {
2920 0 : sql->errstr[0] = '\0'; /* reset table not found error */
2921 0 : sql->session->status = 0;
2922 0 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
2923 : }
2924 : return NULL;
2925 : }
2926 29 : if (isDeclaredTable(ot))
2927 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a declared table");
2928 29 : if (!mvc_schema_privs(sql, ot->s))
2929 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ot->s->base.name);
2930 29 : if (ot->system)
2931 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot set schema of a system table");
2932 29 : if (isTempSchema(ot->s))
2933 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change a temporary table schema");
2934 29 : if (isView(ot))
2935 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a view");
2936 29 : if (mvc_check_dependency(sql, ot->base.id, TABLE_DEPENDENCY, NULL) || list_length(ot->members) || ol_length(ot->triggers))
2937 3 : return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: unable to set schema of table '%s' (there are database objects which depend on it)", tname);
2938 26 : if (!(ns = mvc_bind_schema(sql, new_schema)))
2939 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", new_schema);
2940 26 : if (!mvc_schema_privs(sql, ns))
2941 0 : return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for '%s' to schema '%s'", get_string_global_var(sql, "current_user"), new_schema);
2942 26 : if (isTempSchema(ns))
2943 0 : return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: not possible to change table's schema to temporary");
2944 26 : if (mvc_bind_table(sql, ns, tname))
2945 0 : return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: table '%s' on schema '%s' already exists", tname, new_schema);
2946 :
2947 26 : rel = rel_create(sql->sa);
2948 26 : exps = new_exp_list(sql->sa);
2949 26 : append(exps, exp_atom_clob(sql->sa, ot->s->base.name));
2950 26 : append(exps, exp_atom_clob(sql->sa, new_schema));
2951 26 : append(exps, exp_atom_clob(sql->sa, tname));
2952 26 : append(exps, exp_atom_clob(sql->sa, tname));
2953 26 : rel->op = op_ddl;
2954 26 : rel->flag = ddl_rename_table;
2955 26 : rel->exps = exps;
2956 26 : return rel;
2957 : }
2958 :
2959 : sql_rel *
2960 218918 : rel_schemas(sql_query *query, symbol *s)
2961 : {
2962 218918 : mvc *sql = query->sql;
2963 218918 : sql_rel *ret = NULL;
2964 :
2965 218918 : if (s->token != SQL_CREATE_TABLE && s->token != SQL_CREATE_VIEW && store_readonly(sql->session->tr->store))
2966 2 : return sql_error(sql, 06, SQLSTATE(25006) "Schema statements cannot be executed on a readonly database.");
2967 :
2968 218916 : switch (s->token) {
2969 1085 : case SQL_CREATE_SCHEMA:
2970 : {
2971 1085 : dlist *l = s->data.lval;
2972 :
2973 1085 : ret = rel_create_schema(query, l->h->data.lval,
2974 1085 : l->h->next->next->next->data.lval,
2975 1085 : l->h->next->next->next->next->data.i_val); /* if not exists */
2976 1085 : } break;
2977 191 : case SQL_DROP_SCHEMA:
2978 : {
2979 191 : dlist *l = s->data.lval;
2980 191 : dlist *auth_name = l->h->data.lval;
2981 :
2982 191 : assert(l->h->next->type == type_int);
2983 382 : ret = rel_drop(sql->sa, ddl_drop_schema,
2984 : dlist_get_schema_name(auth_name),
2985 : NULL,
2986 : NULL,
2987 : l->h->next->data.i_val, /* drop_action */
2988 191 : l->h->next->next->data.i_val); /* if exists */
2989 191 : } break;
2990 1 : case SQL_DECLARE_TABLE:
2991 1 : return sql_error(sql, 02, SQLSTATE(42000) "Tables cannot be declared on the global scope");
2992 10147 : case SQL_CREATE_TABLE:
2993 : {
2994 10147 : dlist *l = s->data.lval;
2995 10147 : dlist *qname = l->h->next->data.lval;
2996 10147 : char *sname = qname_schema(qname);
2997 10147 : char *name = qname_schema_object(qname);
2998 10147 : int temp = l->h->data.i_val;
2999 10147 : dlist *credentials = l->h->next->next->next->next->next->data.lval;
3000 10147 : char *username = credentials_username(credentials);
3001 10147 : char *password = credentials_password(credentials);
3002 10147 : bool pw_encrypted = credentials == NULL || credentials->h->next->data.i_val == SQL_PW_ENCRYPTED;
3003 10147 : if (username == NULL) {
3004 : // No username specified, get the current username
3005 10131 : username = get_string_global_var(sql, "current_user");
3006 : }
3007 :
3008 10147 : assert(l->h->type == type_int);
3009 10147 : assert(l->h->next->next->next->type == type_int);
3010 10147 : ret = rel_create_table(query, temp, sname, name, true,
3011 10147 : l->h->next->next->data.sym, /* elements or subquery */
3012 : l->h->next->next->next->data.i_val, /* commit action */
3013 10147 : l->h->next->next->next->next->data.sval, /* location */
3014 : username, password, pw_encrypted,
3015 10147 : l->h->next->next->next->next->next->next->next->data.sym,
3016 10147 : l->h->next->next->next->next->next->next->data.i_val); /* if not exists */
3017 10147 : } break;
3018 79020 : case SQL_CREATE_VIEW:
3019 : {
3020 79020 : dlist *l = s->data.lval;
3021 :
3022 79020 : assert(l->h->next->next->next->type == type_int);
3023 79020 : assert(l->h->next->next->next->next->type == type_int);
3024 79020 : ret = rel_create_view(query, l->h->data.lval,
3025 79020 : l->h->next->data.lval,
3026 79020 : l->h->next->next->data.sym,
3027 : l->h->next->next->next->data.i_val,
3028 : l->h->next->next->next->next->data.i_val,
3029 79020 : l->h->next->next->next->next->next->data.i_val); /* or replace */
3030 79020 : } break;
3031 3766 : case SQL_DROP_TABLE:
3032 : {
3033 3766 : dlist *l = s->data.lval;
3034 :
3035 3766 : assert(l->h->next->type == type_int);
3036 3766 : ret = sql_drop_table(query, l->h->data.lval,
3037 : l->h->next->data.i_val,
3038 3766 : l->h->next->next->data.i_val); /* if exists */
3039 3766 : } break;
3040 334 : case SQL_DROP_VIEW:
3041 : {
3042 334 : dlist *l = s->data.lval;
3043 :
3044 334 : assert(l->h->next->type == type_int);
3045 334 : ret = sql_drop_view(query, l->h->data.lval,
3046 : l->h->next->data.i_val,
3047 334 : l->h->next->next->data.i_val); /* if exists */
3048 334 : } break;
3049 4383 : case SQL_ALTER_TABLE:
3050 : {
3051 4383 : dlist *l = s->data.lval;
3052 :
3053 4383 : ret = sql_alter_table(query, l,
3054 4383 : l->h->data.lval, /* table name */
3055 4383 : l->h->next->data.sym, /* table element */
3056 4383 : l->h->next->next->data.i_val); /* if exists */
3057 4383 : } break;
3058 40 : case SQL_GRANT_ROLES:
3059 : {
3060 40 : dlist *l = s->data.lval;
3061 :
3062 40 : assert(l->h->next->next->type == type_int);
3063 40 : assert(l->h->next->next->next->type == type_int);
3064 40 : ret = rel_grant_or_revoke_roles(sql, l->h->data.lval, /* authids */
3065 40 : l->h->next->data.lval, /* grantees */
3066 : l->h->next->next->data.i_val, /* admin? */
3067 40 : l->h->next->next->next->data.i_val == cur_user ? sql->user_id : sql->role_id, ddl_grant_roles);
3068 : /* grantor ? */
3069 40 : } break;
3070 10 : case SQL_REVOKE_ROLES:
3071 : {
3072 10 : dlist *l = s->data.lval;
3073 :
3074 10 : assert(l->h->next->next->type == type_int);
3075 10 : assert(l->h->next->next->next->type == type_int);
3076 10 : ret = rel_grant_or_revoke_roles(sql, l->h->data.lval, /* authids */
3077 10 : l->h->next->data.lval, /* grantees */
3078 : l->h->next->next->data.i_val, /* admin? */
3079 10 : l->h->next->next->next->data.i_val == cur_user? sql->user_id : sql->role_id, ddl_revoke_roles);
3080 : /* grantor ? */
3081 10 : } break;
3082 117436 : case SQL_GRANT:
3083 : {
3084 117436 : dlist *l = s->data.lval;
3085 :
3086 117436 : assert(l->h->next->next->type == type_int);
3087 117436 : assert(l->h->next->next->next->type == type_int);
3088 117436 : ret = rel_grant_or_revoke_privs(sql, l->h->data.lval, /* privileges */
3089 117436 : l->h->next->data.lval, /* grantees */
3090 : l->h->next->next->data.i_val, /* grant ? */
3091 117436 : l->h->next->next->next->data.i_val == cur_user? sql->user_id : sql->role_id, ddl_grant);
3092 : /* grantor ? */
3093 117436 : } break;
3094 16 : case SQL_REVOKE:
3095 : {
3096 16 : dlist *l = s->data.lval;
3097 :
3098 16 : assert(l->h->next->next->type == type_int);
3099 16 : assert(l->h->next->next->next->type == type_int);
3100 16 : ret = rel_grant_or_revoke_privs(sql, l->h->data.lval, /* privileges */
3101 16 : l->h->next->data.lval, /* grantees */
3102 : l->h->next->next->data.i_val, /* grant ? */
3103 16 : l->h->next->next->next->data.i_val == cur_user? sql->user_id : sql->role_id, ddl_revoke);
3104 : /* grantor ? */
3105 16 : } break;
3106 26 : case SQL_CREATE_ROLE:
3107 : {
3108 26 : dlist *l = s->data.lval;
3109 26 : char *rname = l->h->data.sval;
3110 26 : ret = rel_schema2(sql->sa, ddl_create_role, rname, NULL,
3111 26 : l->h->next->data.i_val == cur_user? sql->user_id : sql->role_id);
3112 26 : } break;
3113 19 : case SQL_DROP_ROLE:
3114 : {
3115 19 : char *rname = s->data.sval;
3116 19 : ret = rel_schema2(sql->sa, ddl_drop_role, rname, NULL, 0);
3117 19 : } break;
3118 343 : case SQL_CREATE_INDEX: {
3119 343 : dlist *l = s->data.lval;
3120 :
3121 343 : assert(l->h->next->type == type_int);
3122 343 : ret = rel_create_index(sql, l->h->data.sval, (idx_type) l->h->next->data.i_val, l->h->next->next->data.lval, l->h->next->next->next->data.lval);
3123 343 : } break;
3124 197 : case SQL_DROP_INDEX: {
3125 197 : dlist *l = s->data.lval;
3126 197 : char *sname = qname_schema(l);
3127 197 : char *iname = qname_schema_object(l);
3128 197 : sql_idx *idx = NULL;
3129 :
3130 197 : if (!(idx = find_idx_on_scope(sql, sname, iname, "DROP INDEX")))
3131 : return NULL;
3132 160 : ret = rel_schema2(sql->sa, ddl_drop_index, idx->t->s->base.name, iname, 0);
3133 160 : } break;
3134 358 : case SQL_CREATE_USER: {
3135 358 : dlist *l = s->data.lval;
3136 358 : dlist *schema_details = l->h->next->next->next->data.lval;
3137 :
3138 358 : ret = rel_create_user(sql->sa, l->h->data.sval, /* user name */
3139 : l->h->next->data.sval, /* password */
3140 358 : l->h->next->next->next->next->data.i_val == SQL_PW_ENCRYPTED, /* encrypted */
3141 : l->h->next->next->data.sval, /* fullname */
3142 : schema_details->h->data.sval, /* schema ident*/
3143 358 : schema_details->h->next->data.sval, /* schema path */
3144 : l->h->next->next->next->next->next->data.l_val, /* max memory */
3145 : l->h->next->next->next->next->next->next->data.i_val, /* max workers */
3146 : l->h->next->next->next->next->next->next->next->data.sval, /* optimizer */
3147 358 : l->h->next->next->next->next->next->next->next->next->data.sval); /* default role */
3148 358 : } break;
3149 107 : case SQL_DROP_USER:
3150 107 : ret = rel_schema2(sql->sa, ddl_drop_user, s->data.sval, NULL, 0);
3151 107 : break;
3152 83 : case SQL_ALTER_USER: {
3153 83 : dlist *l = s->data.lval;
3154 83 : dnode *a = l->h->next->data.lval->h;
3155 :
3156 83 : ret = rel_alter_user(sql->sa, l->h->data.sval, /* user */
3157 : a->data.sval, /* passwd */
3158 83 : a->next->next->next->data.i_val == SQL_PW_ENCRYPTED, /* encrypted */
3159 : a->next->data.sval, /* schema */
3160 : a->next->next->data.sval, /* schema path */
3161 83 : a->next->next->next->next->data.sval, /* old passwd */
3162 : l->h->next->next->data.sval, /* default role */
3163 : l->h->next->next->next->data.l_val, /* max_memory */
3164 83 : l->h->next->next->next->next->data.i_val /* max_workers */
3165 : );
3166 83 : } break;
3167 5 : case SQL_RENAME_USER: {
3168 5 : dlist *l = s->data.lval;
3169 :
3170 5 : ret = rel_schema2(sql->sa, ddl_rename_user, l->h->data.sval, l->h->next->data.sval, 0);
3171 5 : } break;
3172 11 : case SQL_RENAME_SCHEMA: {
3173 11 : dlist *l = s->data.lval;
3174 11 : ret = rel_rename_schema(sql, l->h->data.sval, l->h->next->data.sval, l->h->next->next->data.i_val);
3175 11 : } break;
3176 23 : case SQL_RENAME_TABLE: {
3177 23 : dlist *l = s->data.lval;
3178 23 : char *sname = qname_schema(l->h->data.lval);
3179 23 : char *tname = qname_schema_object(l->h->data.lval);
3180 23 : ret = rel_rename_table(sql, sname, tname, l->h->next->data.sval, l->h->next->next->data.i_val);
3181 23 : } break;
3182 20 : case SQL_RENAME_COLUMN: {
3183 20 : dlist *l = s->data.lval;
3184 20 : char *sname = qname_schema(l->h->data.lval);
3185 20 : char *tname = qname_schema_object(l->h->data.lval);
3186 20 : ret = rel_rename_column(sql, sname, tname, l->h->next->data.sval, l->h->next->next->data.sval, l->h->next->next->next->data.i_val);
3187 20 : } break;
3188 29 : case SQL_SET_TABLE_SCHEMA: {
3189 29 : dlist *l = s->data.lval;
3190 29 : char *sname = qname_schema(l->h->data.lval);
3191 29 : char *tname = qname_schema_object(l->h->data.lval);
3192 29 : ret = rel_set_table_schema(query, sname, tname, l->h->next->data.sval, l->h->next->next->data.i_val);
3193 29 : } break;
3194 906 : case SQL_CREATE_TYPE: {
3195 906 : dlist *l = s->data.lval;
3196 :
3197 906 : ret = rel_create_type(sql, l->h->data.lval, l->h->next->data.sval);
3198 906 : } break;
3199 5 : case SQL_DROP_TYPE: {
3200 5 : dlist *l = s->data.lval;
3201 5 : ret = rel_drop_type(sql, l->h->data.lval, l->h->next->data.i_val);
3202 5 : } break;
3203 355 : case SQL_COMMENT:
3204 : {
3205 355 : dlist *l = s->data.lval;
3206 355 : symbol *catalog_object = l->h->data.sym;
3207 355 : char *remark;
3208 355 : sql_schema *s;
3209 355 : sqlid id;
3210 :
3211 355 : assert(l->cnt == 2);
3212 355 : remark = l->h->next->data.sval;
3213 :
3214 355 : id = rel_find_designated_object(sql, catalog_object, &s);
3215 355 : if (!id) {
3216 : /* rel_find_designated_object has already set the error message so we don't have to */
3217 : return NULL;
3218 : }
3219 :
3220 : // Check authorization
3221 355 : if (!mvc_schema_privs(sql, s)) {
3222 1 : return sql_error(sql, 02, SQLSTATE(42000) "COMMENT ON: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
3223 : }
3224 :
3225 354 : return rel_comment_on(sql->sa, id, remark);
3226 : }
3227 0 : default:
3228 0 : return sql_error(sql, 01, SQLSTATE(M0M03) "Schema statement unknown symbol(%p)->token = %s", s, token2string(s->token));
3229 : }
3230 :
3231 218523 : sql->type = Q_SCHEMA;
3232 218523 : return ret;
3233 : }
|