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_psm.h"
15 : #include "rel_semantic.h"
16 : #include "rel_schema.h"
17 : #include "rel_select.h"
18 : #include "rel_rel.h"
19 : #include "rel_basetable.h"
20 : #include "rel_exp.h"
21 : #include "rel_updates.h"
22 : #include "sql_privileges.h"
23 :
24 : #define psm_zero_or_one(exp) \
25 : do { \
26 : if (exp && exp->card > CARD_AGGR) { \
27 : sql_subfunc *zero_or_one = sql_bind_func(sql, "sys", "zero_or_one", exp_subtype(exp), NULL, F_AGGR, true, true); \
28 : assert(zero_or_one); \
29 : exp = exp_aggr1(sql->sa, exp, zero_or_one, 0, 0, CARD_ATOM, has_nil(exp)); \
30 : } \
31 : } while(0)
32 :
33 : static list *sequential_block(sql_query *query, sql_subtype *restype, list *restypelist, dlist *blk, char *opt_name, int is_func);
34 :
35 : sql_rel *
36 22298 : rel_psm_block(allocator *sa, list *l)
37 : {
38 22298 : if (l) {
39 22298 : sql_rel *r = rel_create(sa);
40 22298 : if(!r)
41 : return NULL;
42 :
43 22298 : r->op = op_ddl;
44 22298 : r->flag = ddl_psm;
45 22298 : r->exps = l;
46 22298 : return r;
47 : }
48 : return NULL;
49 : }
50 :
51 : sql_rel *
52 8962 : rel_psm_stmt(allocator *sa, sql_exp *e)
53 : {
54 8962 : if (e) {
55 8924 : list *l = sa_list(sa);
56 8924 : if(!l)
57 : return NULL;
58 :
59 8924 : list_append(l, e);
60 8926 : return rel_psm_block(sa, l);
61 : }
62 : return NULL;
63 : }
64 :
65 : /* SET [ schema '.' ] variable = value and set ( [ schema1 '.' ] variable1, .., [ schemaN '.' ] variableN) = (query) */
66 : static sql_exp *
67 14478 : psm_set_exp(sql_query *query, dnode *n)
68 : {
69 14478 : mvc *sql = query->sql;
70 14478 : dlist *qname = n->data.lval;
71 14478 : symbol *val = n->next->data.sym;
72 14478 : sql_exp *res = NULL, *e = NULL;
73 14478 : int level = 0, single = (qname->h->type == type_string);
74 14478 : sql_rel *rel = NULL;
75 14478 : sql_subtype *tpe;
76 :
77 14478 : if (single) {
78 14472 : exp_kind ek = {type_value, card_value, FALSE};
79 14472 : const char *sname = qname_schema(qname);
80 14472 : const char *vname = qname_schema_object(qname);
81 14472 : sql_var *var = NULL;
82 14472 : sql_arg *a = NULL;
83 :
84 14472 : if (!find_variable_on_scope(sql, sname, vname, &var, &a, &tpe, &level, "SET"))
85 29 : return NULL;
86 14452 : if (!(e = rel_value_exp2(query, &rel, val, sql_sel | sql_psm, ek)))
87 : return NULL;
88 14444 : psm_zero_or_one(e);
89 :
90 14444 : if (!(e = exp_check_type(sql, tpe, rel, e, type_cast)))
91 : return NULL;
92 15297 : res = exp_set(sql->sa, var && var->sname ? sa_strdup(sql->sa, var->sname) : NULL, sa_strdup(sql->sa, vname), e, level);
93 : } else { /* multi assignment */
94 6 : exp_kind ek = {type_relation, card_value, FALSE};
95 6 : sql_rel *rel_val = rel_subquery(query, val, ek);
96 6 : dlist *vars = n->data.lval;
97 6 : dnode *m;
98 6 : node *n;
99 6 : list *b;
100 :
101 6 : if (!rel_val)
102 0 : return NULL;
103 6 : if (!is_project(rel_val->op))
104 0 : return sql_error(sql, 02, SQLSTATE(42000) "SET: The subquery is not a projection");
105 6 : if (dlist_length(vars) != list_length(rel_val->exps))
106 0 : return sql_error(sql, 02, SQLSTATE(42000) "SET: Number of variables not equal to number of supplied values");
107 6 : rel_val = rel_return_zero_or_one(sql, rel_val, ek);
108 :
109 6 : b = sa_list(sql->sa);
110 6 : append(b, exp_rel(sql, rel_val));
111 :
112 18 : for (m = vars->h, n = rel_val->exps->h; n && m; n = n->next, m = m->next) {
113 12 : dlist *nqname = m->data.lval;
114 12 : const char *sname = qname_schema(nqname);
115 12 : const char *vname = qname_schema_object(nqname);
116 12 : sql_exp *v = n->data;
117 12 : sql_var *var = NULL;
118 12 : sql_arg *a = NULL;
119 :
120 12 : if (!find_variable_on_scope(sql, sname, vname, &var, &a, &tpe, &level, "SET"))
121 0 : return NULL;
122 :
123 12 : v = exp_ref(sql, v);
124 12 : if (!(v = exp_check_type(sql, tpe, rel_val, v, type_cast)))
125 : return NULL;
126 12 : append(b, exp_set(sql->sa, var && var->sname ? sa_strdup(sql->sa, var->sname) : NULL, sa_strdup(sql->sa, vname), v, level));
127 : }
128 6 : res = exp_rel(sql, rel_psm_block(sql->sa, b));
129 : }
130 : return res;
131 : }
132 :
133 : static sql_exp*
134 10235 : rel_psm_call(sql_query * query, symbol *se)
135 : {
136 10235 : mvc *sql = query->sql;
137 10235 : sql_subtype *t;
138 10235 : sql_exp *res = NULL;
139 10235 : exp_kind ek = {type_value, card_none, FALSE};
140 10235 : sql_rel *rel = NULL;
141 :
142 10235 : res = rel_value_exp(query, &rel, se, sql_sel | psm_call, ek);
143 10235 : if (!res || rel || ((t=exp_subtype(res)) && t->type)) /* only procedures */
144 21 : return sql_error(sql, 01, SQLSTATE(42000) "Function calls are ignored");
145 : return res;
146 : }
147 :
148 : static list *
149 8184 : rel_psm_declare(mvc *sql, dnode *n)
150 : {
151 8184 : list *l = sa_list(sql->sa);
152 :
153 17511 : while (n) { /* list of 'identfiers with type' */
154 9330 : dnode *ids = n->data.sym->data.lval->h->data.lval->h;
155 9330 : sql_subtype *ctype = &n->data.sym->data.lval->h->next->data.typeval;
156 18678 : while (ids) {
157 9351 : dlist *qname = ids->data.lval;
158 9351 : const char *sname = qname_schema(qname);
159 9351 : const char *tname = qname_schema_object(qname);
160 9351 : sql_exp *r = NULL;
161 :
162 9351 : if (sname)
163 1 : return sql_error(sql, 01, SQLSTATE(42000) "DECLARE: Declared variables don't have a schema");
164 : /* find if there's a parameter with the same name */
165 9350 : if (sql->frame == 1 && sql_bind_param(sql, tname) >= 0)
166 2 : return sql_error(sql, 01, SQLSTATE(42000) "DECLARE: Variable '%s' declared as a parameter", tname);
167 : /* check if we overwrite a scope local variable declare x; declare x; */
168 9348 : if (frame_find_var(sql, tname))
169 0 : return sql_error(sql, 01, SQLSTATE(42000) "DECLARE: Variable '%s' already declared", tname);
170 : /* variables are put on stack, globals on a separate list */
171 9348 : if (!frame_push_var(sql, tname, ctype))
172 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
173 9348 : r = exp_var(sql->sa, NULL, sa_strdup(sql->sa, tname), ctype, sql->frame);
174 9348 : append(l, r);
175 9348 : ids = ids->next;
176 : }
177 9327 : n = n->next;
178 : }
179 : return l;
180 : }
181 :
182 : static sql_exp *
183 120 : rel_psm_declare_table(sql_query *query, dnode *n)
184 : {
185 120 : mvc *sql = query->sql;
186 120 : sql_rel *rel = NULL, *baset = NULL;
187 120 : dlist *qname = n->next->data.lval;
188 120 : const char *sname = qname_schema(qname);
189 120 : const char *name = qname_schema_object(qname);
190 120 : sql_table *t;
191 :
192 120 : if (sname)
193 1 : return sql_error(sql, 01, SQLSTATE(42000) "DECLARE TABLE: Declared tables don't have a schema");
194 :
195 119 : assert(n->next->next->next->type == type_int);
196 238 : rel = rel_create_table(query, SQL_DECLARED_TABLE, sname, name, false, n->next->next->data.sym,
197 : n->next->next->next->data.i_val, NULL, NULL, NULL, false, NULL,
198 119 : n->next->next->next->next->next->next->data.i_val);
199 :
200 119 : if (!rel)
201 : return NULL;
202 117 : if (rel->op == op_ddl) {
203 : baset = rel;
204 3 : } else if (rel->op == op_insert) {
205 3 : baset = rel->l;
206 : } else {
207 0 : assert(0);
208 : }
209 117 : assert(baset->flag == ddl_create_table);
210 117 : t = (sql_table*)((atom*)((sql_exp*)baset->exps->t->data)->l)->data.val.pval;
211 117 : if (!frame_push_table(sql, t))
212 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
213 117 : return exp_table(sql->sa, sa_strdup(sql->sa, name), t, sql->frame);
214 : }
215 :
216 : /* [ label: ]
217 : while (cond) do
218 : statement_list
219 : end [ label ]
220 : currently we only parse the labels, they cannot be used as there is no
221 :
222 : support for LEAVE and ITERATE (sql multi-level break and continue)
223 : */
224 : static sql_exp *
225 1000 : rel_psm_while_do( sql_query *query, sql_subtype *res, list *restypelist, dnode *w, int is_func )
226 : {
227 1000 : mvc *sql = query->sql;
228 1000 : if (!w)
229 : return NULL;
230 1000 : if (w->type == type_symbol) {
231 1000 : sql_exp *cond;
232 1000 : list *whilestmts;
233 1000 : dnode *n = w;
234 1000 : sql_rel *rel = NULL;
235 1000 : exp_kind ek = {type_value, card_value, FALSE};
236 :
237 1000 : if (!(cond = rel_logical_value_exp(query, &rel, n->data.sym, sql_sel | sql_psm, ek)))
238 : return NULL;
239 1000 : psm_zero_or_one(cond);
240 1000 : n = n->next;
241 1000 : if (!(whilestmts = sequential_block(query, res, restypelist, n->data.lval, n->next->data.sval, is_func)))
242 : return NULL;
243 :
244 1000 : return exp_while( sql->sa, cond, whilestmts );
245 : }
246 : return NULL;
247 : }
248 :
249 : /* if (cond) then statement_list
250 : [ elseif (cond) then statement_list ]*
251 : [ else statement_list ]
252 : end if
253 : */
254 : static list *
255 11335 : psm_if_then_else( sql_query *query, sql_subtype *res, list *restypelist, dnode *elseif, int is_func)
256 : {
257 11335 : mvc *sql = query->sql;
258 11335 : if (!elseif)
259 : return NULL;
260 11335 : assert(elseif->type == type_symbol);
261 11335 : if (elseif->data.sym && elseif->data.sym->token == SQL_IF) {
262 7 : sql_exp *cond;
263 7 : list *ifstmts, *elsestmts;
264 7 : dnode *n = elseif->data.sym->data.lval->h;
265 7 : sql_rel *rel = NULL;
266 7 : exp_kind ek = {type_value, card_value, FALSE};
267 :
268 7 : if (!(cond = rel_logical_value_exp(query, &rel, n->data.sym, sql_sel | sql_psm, ek)))
269 : return NULL;
270 7 : psm_zero_or_one(cond);
271 7 : n = n->next;
272 7 : if (!(ifstmts = sequential_block(query, res, restypelist, n->data.lval, NULL, is_func)))
273 : return NULL;
274 7 : n = n->next;
275 7 : elsestmts = psm_if_then_else( query, res, restypelist, n, is_func);
276 7 : if (sql->session->status)
277 : return NULL;
278 :
279 7 : return append(sa_list(sql->sa), exp_if( sql->sa, cond, ifstmts, elsestmts));
280 : } else { /* else */
281 11328 : symbol *e = elseif->data.sym;
282 :
283 11328 : if (e==NULL || (e->token != SQL_ELSE))
284 : return NULL;
285 1392 : return sequential_block(query, res, restypelist, e->data.lval, NULL, is_func);
286 : }
287 : }
288 :
289 : static sql_exp *
290 11329 : rel_psm_if_then_else( sql_query *query, sql_subtype *res, list *restypelist, dnode *elseif, int is_func)
291 : {
292 11329 : mvc *sql = query->sql;
293 11329 : if (!elseif)
294 : return NULL;
295 11329 : if (elseif->next && elseif->type == type_symbol) { /* if or elseif */
296 11329 : sql_exp *cond;
297 11329 : list *ifstmts, *elsestmts;
298 11329 : dnode *n = elseif;
299 11329 : sql_rel *rel = NULL;
300 11329 : exp_kind ek = {type_value, card_value, FALSE};
301 :
302 11329 : if (!(cond = rel_logical_value_exp(query, &rel, n->data.sym, sql_sel | sql_psm, ek)))
303 : return NULL;
304 11329 : psm_zero_or_one(cond);
305 11329 : n = n->next;
306 11329 : if (!(ifstmts = sequential_block(query, res, restypelist, n->data.lval, NULL, is_func)))
307 : return NULL;
308 11328 : n = n->next;
309 11328 : elsestmts = psm_if_then_else( query, res, restypelist, n, is_func);
310 11328 : if (sql->session->status)
311 : return NULL;
312 :
313 11328 : return exp_if( sql->sa, cond, ifstmts, elsestmts);
314 : }
315 : return NULL;
316 : }
317 :
318 : /* 1
319 : CASE
320 : WHEN search_condition THEN statements
321 : [ WHEN search_condition THEN statements ]
322 : [ ELSE statements ]
323 : END CASE
324 :
325 : 2
326 : CASE case_value
327 : WHEN when_value THEN statements
328 : [ WHEN when_value THEN statements ]
329 : [ ELSE statements ]
330 : END CASE
331 : */
332 : static sql_exp *
333 59 : rel_psm_case( sql_query *query, sql_subtype *res, list *restypelist, dnode *case_when, int is_func )
334 : {
335 59 : mvc *sql = query->sql;
336 59 : sql_exp *case_stmt = NULL, *last_if = NULL, *ifst = NULL;
337 59 : list *else_stmt = NULL;
338 :
339 59 : if (!case_when)
340 : return NULL;
341 :
342 : /* case 1 */
343 59 : if (case_when->type == type_symbol) {
344 12 : dnode *n = case_when;
345 12 : symbol *case_value = n->data.sym;
346 12 : dlist *when_statements = n->next->data.lval;
347 12 : dlist *else_statements = n->next->next->data.lval;
348 12 : sql_rel *rel = NULL;
349 12 : exp_kind ek = {type_value, card_value, FALSE};
350 12 : sql_exp *v = rel_value_exp(query, &rel, case_value, sql_sel | sql_psm, ek);
351 :
352 12 : psm_zero_or_one(v);
353 0 : if (!v)
354 0 : return NULL;
355 12 : if (else_statements && !(else_stmt = sequential_block(query, res, restypelist, else_statements, NULL, is_func)))
356 : return NULL;
357 :
358 12 : n = when_statements->h;
359 60 : while(n) {
360 48 : dnode *m = n->data.sym->data.lval->h;
361 48 : sql_exp *cond=0, *when_value = rel_value_exp(query, &rel, m->data.sym, sql_sel | sql_psm, ek);
362 48 : list *if_stmts = NULL;
363 :
364 48 : psm_zero_or_one(when_value);
365 0 : if (!when_value ||
366 48 : (cond = rel_binop_(sql, rel, v, when_value, "sys", "=", card_value, false)) == NULL ||
367 48 : (if_stmts = sequential_block(query, res, restypelist, m->next->data.lval, NULL, is_func)) == NULL ) {
368 0 : return NULL;
369 : }
370 48 : psm_zero_or_one(cond);
371 48 : ifst = exp_if(sql->sa, cond, if_stmts, NULL);
372 48 : if (last_if) { /* chain if statements for case, keep the last if */
373 36 : last_if->f = list_append(sa_list(sql->sa), ifst);
374 36 : last_if = ifst;
375 : } else {
376 : case_stmt = last_if = ifst;
377 : }
378 48 : n = n->next;
379 : }
380 : } else {
381 : /* case 2 */
382 47 : dnode *n = case_when;
383 47 : dlist *whenlist = n->data.lval;
384 47 : dlist *else_statements = n->next->data.lval;
385 :
386 47 : if (else_statements && !(else_stmt = sequential_block(query, res, restypelist, else_statements, NULL, is_func)))
387 : return NULL;
388 :
389 47 : n = whenlist->h;
390 97 : while(n) {
391 50 : dnode *m = n->data.sym->data.lval->h;
392 50 : sql_rel *rel = NULL;
393 50 : exp_kind ek = {type_value, card_value, FALSE};
394 50 : sql_exp *cond = rel_logical_value_exp(query, &rel, m->data.sym, sql_sel | sql_psm, ek);
395 50 : list *if_stmts = NULL;
396 :
397 50 : psm_zero_or_one(cond);
398 0 : if (!cond ||
399 50 : (if_stmts = sequential_block(query, res, restypelist, m->next->data.lval, NULL, is_func)) == NULL ) {
400 0 : return NULL;
401 : }
402 50 : ifst = exp_if(sql->sa, cond, if_stmts, NULL);
403 50 : if (last_if) { /* chain if statements for case, keep the last if */
404 3 : last_if->f = list_append(sa_list(sql->sa), ifst);
405 3 : last_if = ifst;
406 : } else {
407 : case_stmt = last_if = ifst;
408 : }
409 50 : n = n->next;
410 : }
411 : }
412 59 : if (else_stmt) {
413 51 : assert(case_stmt && last_if && !last_if->f);
414 51 : last_if->f = else_stmt;
415 : }
416 : return case_stmt;
417 : }
418 :
419 : /* return val;
420 : */
421 : static sql_exp *
422 22775 : rel_psm_return( sql_query *query, sql_subtype *restype, list *restypelist, symbol *return_sym )
423 : {
424 22775 : mvc *sql = query->sql;
425 22775 : exp_kind ek = {type_value, card_value, FALSE};
426 22775 : sql_exp *res = NULL;
427 22775 : sql_rel *rel = NULL;
428 22775 : bool requires_proj = false;
429 :
430 22775 : if (restypelist)
431 : ek.card = card_relation;
432 19955 : else if (return_sym->token == SQL_TABLE)
433 2 : return sql_error(sql, 02, SQLSTATE(42000) "RETURN: TABLE return not allowed for non table returning functions");
434 22773 : if (return_sym->token == SQL_COLUMN && restypelist) { /* RETURN x; where x is a reference to a table */
435 562 : dlist *l = return_sym->data.lval;
436 562 : const char *sname = qname_schema(l);
437 562 : const char *tname = qname_schema_object(l);
438 562 : sql_table *t = NULL;
439 :
440 562 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "RETURN", false)))
441 : return NULL;
442 561 : if (isDeclaredTable(t)) {
443 60 : rel = rel_table(sql, ddl_create_table, "sys", t, SQL_DECLARED_TABLE);
444 : } else {
445 501 : rel = rel_basetable(sql, t, t->base.name);
446 501 : rel = rel_base_add_columns(sql, rel);
447 : }
448 : } else { /* other cases */
449 22211 : res = rel_value_exp2(query, &rel, return_sym, sql_sel, ek);
450 22211 : if (!res)
451 : return NULL;
452 22201 : if (!rel && exp_is_rel(res)) {
453 6285 : rel = exp_rel_get_rel(sql->sa, res);
454 6285 : if (rel && !restypelist && !is_groupby(rel->op)) { /* On regular functions return zero or 1 rows for every row */
455 4227 : rel->card = CARD_MULTI;
456 4227 : rel = rel_return_zero_or_one(sql, rel, ek);
457 4227 : if (list_length(rel->exps) != 1)
458 0 : return sql_error(sql, 02, SQLSTATE(42000) "RETURN: must return a single column");
459 4227 : res = exp_ref(sql, (sql_exp*) rel->exps->t->data);
460 4227 : requires_proj = true;
461 : }
462 : }
463 : }
464 :
465 22762 : if (ek.card != card_relation && (!restype || (res = exp_check_type(sql, restype, rel, res, type_equal)) == NULL))
466 0 : return (!restype)?sql_error(sql, 02, SQLSTATE(42000) "RETURN: return type does not match"):NULL;
467 22762 : else if (ek.card == card_relation && !rel)
468 : return NULL;
469 :
470 22762 : if (requires_proj) {
471 4227 : rel = rel_project(sql->sa, rel, list_append(sa_list(sql->sa), res));
472 4227 : res = exp_rel(sql, rel);
473 : }
474 :
475 25515 : if (rel && !is_ddl(rel->op) && ek.card == card_relation) {
476 2754 : list *exps = sa_list(sql->sa), *oexps = rel->exps;
477 2754 : node *n, *m;
478 2754 : int isproject = (rel->op == op_project);
479 2754 : sql_rel *l = rel->l, *oexps_rel = rel;
480 :
481 2754 : if (is_topn(rel->op) || is_sample(rel->op)) {
482 6 : oexps_rel = l;
483 6 : oexps = l->exps;
484 : }
485 2754 : if (list_length(oexps) != list_length(restypelist))
486 1 : return sql_error(sql, 02, SQLSTATE(42000) "RETURN: number of columns do not match");
487 13968 : for (n = oexps->h, m = restypelist->h; n && m; n = n->next, m = m->next) {
488 11215 : sql_exp *e = n->data;
489 11215 : sql_arg *ce = m->data;
490 11215 : const char *cname = exp_name(e);
491 11215 : char name[16];
492 :
493 11215 : if (!cname)
494 136 : cname = sa_strdup(sql->sa, number2name(name, sizeof(name), ++sql->label));
495 11215 : if (!isproject)
496 1095 : e = exp_ref(sql, e);
497 11215 : e = exp_check_type(sql, &ce->type, oexps_rel, e, type_equal);
498 11215 : if (!e)
499 0 : return NULL;
500 11215 : append(exps, e);
501 : }
502 2753 : if (isproject)
503 2201 : rel->exps = exps;
504 : else
505 552 : rel = rel_project(sql->sa, rel, exps);
506 2753 : res = exp_rel(sql, rel);
507 20008 : } else if (rel && restypelist) { /* handle return table-var */
508 60 : list *exps = sa_list(sql->sa);
509 60 : sql_table *t = rel_ddl_table_get(rel);
510 60 : node *n, *m;
511 60 : const char *tname = t->base.name;
512 :
513 60 : if (ol_length(t->columns) != list_length(restypelist))
514 1 : return sql_error(sql, 02, SQLSTATE(42000) "RETURN: number of columns do not match");
515 188 : for (n = ol_first_node(t->columns), m = restypelist->h; n && m; n = n->next, m = m->next) {
516 129 : sql_column *c = n->data;
517 129 : sql_arg *ce = m->data;
518 129 : sql_exp *e = exp_column(sql->sa, tname, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
519 :
520 129 : e = exp_check_type(sql, &ce->type, rel, e, type_equal);
521 129 : if (!e)
522 : return NULL;
523 129 : append(exps, e);
524 : }
525 59 : rel = rel_project(sql->sa, rel, exps);
526 59 : res = exp_rel(sql, rel);
527 : }
528 22760 : res = exp_return(sql->sa, res, stack_nr_of_declared_tables(sql));
529 22760 : if (ek.card != card_relation)
530 19948 : res->card = CARD_ATOM;
531 : else
532 2812 : res->card = CARD_MULTI;
533 : return res;
534 : }
535 :
536 : static list *
537 1445 : rel_select_into( sql_query *query, symbol *sq, exp_kind ek)
538 : {
539 1445 : mvc *sql = query->sql;
540 1445 : SelectNode *sn = (SelectNode*)sq;
541 1445 : dlist *into = sn->into;
542 1445 : node *m;
543 1445 : dnode *n;
544 1445 : sql_rel *r;
545 1445 : list *nl = NULL;
546 :
547 : /* SELECT ... INTO var_list */
548 1445 : sn->into = NULL;
549 1445 : r = rel_subquery(query, sq, ek);
550 1445 : if (!r)
551 : return NULL;
552 1445 : if (!is_project(r->op))
553 0 : return sql_error(sql, 02, SQLSTATE(42000) "SELECT INTO: The subquery is not a projection");
554 1445 : if (list_length(r->exps) != dlist_length(into))
555 2 : return sql_error(sql, 02, SQLSTATE(21S01) "SELECT INTO: number of values doesn't match number of variables to set");
556 1443 : r = rel_return_zero_or_one(sql, r, ek);
557 1443 : nl = sa_list(sql->sa);
558 1443 : append(nl, exp_rel(sql, r));
559 2893 : for (m = r->exps->h, n = into->h; m && n; m = m->next, n = n->next) {
560 1450 : dlist *qname = n->data.lval;
561 1450 : const char *sname = qname_schema(qname);
562 1450 : const char *vname = qname_schema_object(qname);
563 1450 : sql_exp *v = m->data;
564 1450 : int level;
565 1450 : sql_var *var;
566 1450 : sql_subtype *tpe;
567 1450 : sql_arg *a = NULL;
568 :
569 1450 : if (!find_variable_on_scope(sql, sname, vname, &var, &a, &tpe, &level, "SELECT INTO"))
570 0 : return NULL;
571 :
572 1450 : v = exp_ref(sql, v);
573 1450 : if (!(v = exp_check_type(sql, tpe, r, v, type_equal)))
574 : return NULL;
575 1450 : v = exp_set(sql->sa, var && var->sname ? sa_strdup(sql->sa, var->sname) : NULL, sa_strdup(sql->sa, vname), v, level);
576 1450 : list_append(nl, v);
577 : }
578 : return nl;
579 : }
580 :
581 : extern sql_rel *
582 1 : rel_select_with_into(sql_query *query, symbol *sq)
583 : {
584 1 : exp_kind ek = {type_relation, card_value, TRUE};
585 1 : list *reslist = rel_select_into(query, sq, ek);
586 1 : if (!reslist)
587 : return NULL;
588 0 : return rel_psm_block(query->sql->sa, reslist);
589 : }
590 :
591 : static int has_return( list *l );
592 :
593 : static int
594 20086 : exp_has_return(sql_exp *e)
595 : {
596 20086 : if (e->type == e_psm) {
597 19627 : if (e->flag & PSM_RETURN)
598 : return 1;
599 4153 : if (e->flag & PSM_IF) /* for if, both sides must exist and both must have a return */
600 4970 : return has_return(e->r) && e->f && has_return(e->f);
601 : }
602 : return 0;
603 : }
604 :
605 : static int
606 20086 : has_return( list *l )
607 : {
608 20086 : node *n = l->t;
609 :
610 : /* last statment of sequential block */
611 20086 : if (n && exp_has_return(n->data))
612 : return 1;
613 : return 0;
614 : }
615 :
616 : static list *
617 34720 : sequential_block(sql_query *query, sql_subtype *restype, list *restypelist, dlist *blk, char *opt_label, int is_func)
618 : {
619 34720 : mvc *sql = query->sql;
620 34720 : list *l=0;
621 34720 : dnode *n;
622 :
623 34720 : assert(!restype || !restypelist);
624 :
625 34720 : if (mvc_highwater(sql))
626 0 : return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
627 :
628 34720 : if (blk->h)
629 34720 : l = sa_list(sql->sa);
630 34720 : if (!stack_push_frame(sql, opt_label))
631 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
632 114265 : for (n = blk->h; n; n = n->next ) {
633 79587 : sql_exp *res = NULL;
634 79587 : list *reslist = NULL;
635 79587 : symbol *s = n->data.sym;
636 :
637 79587 : switch (s->token) {
638 14111 : case SQL_SET:
639 14111 : res = psm_set_exp(query, s->data.lval->h);
640 14111 : break;
641 8184 : case SQL_DECLARE:
642 8184 : reslist = rel_psm_declare(sql, s->data.lval->h);
643 8184 : break;
644 120 : case SQL_DECLARE_TABLE:
645 : case SQL_CREATE_TABLE:
646 120 : res = rel_psm_declare_table(query, s->data.lval->h);
647 120 : break;
648 1000 : case SQL_WHILE:
649 1000 : res = rel_psm_while_do(query, restype, restypelist, s->data.lval->h, is_func);
650 1000 : break;
651 11329 : case SQL_IF:
652 11329 : res = rel_psm_if_then_else(query, restype, restypelist, s->data.lval->h, is_func);
653 11329 : break;
654 59 : case SQL_CASE:
655 59 : res = rel_psm_case(query, restype, restypelist, s->data.lval->h, is_func);
656 59 : break;
657 1841 : case SQL_CALL:
658 1841 : assert(s->type == type_symbol);
659 1841 : res = rel_psm_call(query, s->data.sym);
660 1841 : break;
661 22775 : case SQL_RETURN:
662 : /*If it is not a function it cannot have a return statement*/
663 22775 : if (!is_func)
664 0 : res = sql_error(sql, 01, SQLSTATE(42000) "Return statement in the procedure body");
665 22775 : else if (n->next) /* should be last statement of a sequential_block */
666 0 : res = sql_error(sql, 01, SQLSTATE(42000) "Statement after return");
667 : else
668 22775 : res = rel_psm_return(query, restype, restypelist, s->data.sym);
669 : break;
670 1444 : case SQL_SELECT: { /* row selections (into variables) */
671 1444 : exp_kind ek = {type_value, card_row, TRUE};
672 1444 : reslist = rel_select_into(query, s, ek);
673 1444 : } break;
674 18724 : case SQL_COPYFROM:
675 : case SQL_BINCOPYFROM:
676 : case SQL_INSERT:
677 : case SQL_UPDATE:
678 : case SQL_DELETE:
679 : case SQL_TRUNCATE:
680 : case SQL_MERGE: {
681 18724 : sql_rel *r = rel_updates(query, s);
682 18724 : if (!r) {
683 7 : stack_pop_frame(sql);
684 7 : return NULL;
685 : }
686 18717 : res = exp_rel(sql, r);
687 18717 : } break;
688 0 : default:
689 0 : res = sql_error(sql, 01, SQLSTATE(42000) "Statement '%s' is not a valid flow control statement",
690 : token2string(s->token));
691 : }
692 79580 : if (!res && !reslist) {
693 : l = NULL;
694 : break;
695 : }
696 79545 : if (res)
697 69921 : list_append(l, res);
698 : else
699 9624 : list_merge(l, reslist, NULL);
700 : }
701 34713 : stack_pop_frame(sql);
702 34713 : return l;
703 : }
704 :
705 : static int
706 580626 : arg_cmp(void *A, void *N)
707 : {
708 580626 : sql_arg *a = A;
709 580626 : char *name = N;
710 580626 : return strcmp(a->name, name);
711 : }
712 :
713 : static list *
714 199819 : result_type(mvc *sql, symbol *res)
715 : {
716 199819 : if (res->token == SQL_TYPE) {
717 176630 : sql_subtype *st = &res->data.lval->h->data.typeval;
718 176630 : sql_arg *a = sql_create_arg(sql->sa, "result", st, ARG_OUT);
719 :
720 176630 : return list_append(sa_list(sql->sa), a);
721 23189 : } else if (res->token == SQL_TABLE) {
722 23189 : sql_arg *a;
723 23189 : dnode *n = res->data.lval->h;
724 23189 : list *types = sa_list(sql->sa);
725 :
726 174523 : for(;n; n = n->next->next) {
727 128146 : sql_subtype *ct = &n->next->data.typeval;
728 :
729 128146 : if (list_find(types, n->data.sval, &arg_cmp) != NULL)
730 1 : return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "CREATE FUNC: identifier '%s' ambiguous", n->data.sval);
731 :
732 128145 : a = sql_create_arg(sql->sa, n->data.sval, ct, ARG_OUT);
733 128145 : list_append(types, a);
734 : }
735 : return types;
736 : }
737 : return NULL;
738 : }
739 :
740 : static list *
741 325453 : create_type_list(mvc *sql, dlist *params, int param)
742 : {
743 325453 : sql_subtype *par_subtype;
744 325453 : list * type_list = sa_list(sql->sa);
745 325453 : dnode * n = NULL;
746 :
747 325453 : if (params) {
748 819362 : for (n = params->h; n; n = n->next) {
749 510966 : dnode *an = n;
750 :
751 510966 : if (param) {
752 367522 : an = n->data.lval->h;
753 367522 : par_subtype = &an->next->data.typeval;
754 367522 : if (par_subtype && !par_subtype->type) /* var arg */
755 : return type_list;
756 367519 : list_append(type_list, par_subtype);
757 : } else {
758 143444 : par_subtype = &an->data.typeval;
759 143444 : list_prepend(type_list, par_subtype);
760 : }
761 : }
762 : }
763 : return type_list;
764 : }
765 :
766 : static sql_rel*
767 227371 : rel_create_function(allocator *sa, const char *sname, sql_func *f, int replace)
768 : {
769 227371 : sql_rel *rel = rel_create(sa);
770 227371 : list *exps = new_exp_list(sa);
771 227371 : if(!rel || !exps)
772 : return NULL;
773 :
774 227371 : append(exps, exp_atom_clob(sa, sname));
775 227371 : if (f)
776 227371 : append(exps, exp_atom_clob(sa, f->base.name));
777 227371 : append(exps, exp_atom_ptr(sa, f));
778 227371 : append(exps, exp_atom_int(sa, replace));
779 227371 : rel->l = NULL;
780 227371 : rel->r = NULL;
781 227371 : rel->op = op_ddl;
782 227371 : rel->flag = ddl_create_function;
783 227371 : rel->exps = exps;
784 227371 : rel->card = 0;
785 227371 : rel->nrcols = 0;
786 227371 : return rel;
787 : }
788 :
789 : static sql_rel *
790 235453 : rel_create_func(sql_query *query, dlist *qname, dlist *params, symbol *res, dlist *ext_name, dlist *body, sql_ftype type, sql_flang lang, int replace)
791 : {
792 235453 : mvc *sql = query->sql;
793 235453 : const char *fname = qname_schema_object(qname);
794 235453 : const char *sname = qname_schema(qname);
795 235453 : sql_schema *s = cur_schema(sql);
796 235453 : sql_func *f = NULL;
797 235453 : sql_subfunc *sf = NULL;
798 235453 : dnode *n;
799 235453 : list *type_list = NULL, *restype = NULL, *l = NULL;
800 235453 : int instantiate = (sql->emode == m_instantiate);
801 235453 : int deps = (sql->emode == m_deps);
802 235453 : int create = (!instantiate && !deps);
803 235453 : bit vararg = FALSE, union_err = 0;
804 235453 : char *F = NULL, *fn = NULL, is_func, *q = QUERY(sql->scanner);
805 :
806 235453 : if (res && res->token == SQL_TABLE) {
807 23190 : if (type == F_FUNC)
808 : type = F_UNION;
809 : else
810 : union_err = 1;
811 : }
812 :
813 212263 : FUNC_TYPE_STR(type, F, fn)
814 :
815 235453 : is_func = (type != F_PROC && type != F_LOADER);
816 235453 : assert(lang != FUNC_LANG_INT);
817 :
818 235453 : if (create && store_readonly(sql->session->tr->store))
819 0 : return sql_error(sql, 06, SQLSTATE(42000) "Schema statements cannot be executed on a readonly database.");
820 :
821 235453 : if (union_err)
822 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %ss cannot return tables", F, fn);
823 235453 : else if (res && type == F_PROC)
824 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: procedures cannot have return parameters", F);
825 235453 : else if (res && (type == F_FILT || type == F_LOADER))
826 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %s functions don't have to specify a return type", F, fn);
827 35624 : else if (!res && !(type == F_PROC || type == F_FILT || type == F_LOADER))
828 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %ss require a return type", F, fn);
829 235453 : else if (lang == FUNC_LANG_MAL && type == F_LOADER)
830 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %s functions creation via MAL not supported", F, fn);
831 235453 : else if (lang == FUNC_LANG_SQL && !(type == F_FUNC || type == F_PROC || type == F_UNION))
832 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %s functions creation via SQL not supported", F, fn);
833 235453 : else if (LANG_EXT(lang) && !(type == F_FUNC || type == F_AGGR || type == F_UNION || type == F_LOADER))
834 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: %ss creation via external programming languages not supported", F, fn);
835 :
836 235453 : if (sname && !(s = mvc_bind_schema(sql, sname)))
837 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "CREATE %s: no such schema '%s'", F, sname);
838 235453 : if (create && !mvc_schema_privs(sql, s))
839 5 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: insufficient privileges for user '%s' in schema '%s'", F,
840 : get_string_global_var(sql, "current_user"), s->base.name);
841 :
842 235448 : type_list = create_type_list(sql, params, 1);
843 :
844 235448 : if ((sf = sql_bind_func_(sql, s->base.name, fname, type_list, type, true, true)) != NULL && create) {
845 31 : if (sf->func->private) { /* cannot create a function using a private name or replace a existing one */
846 2 : list_destroy(type_list);
847 2 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: name '%s' cannot be used", F, fname);
848 : }
849 29 : if (!replace && params) {
850 0 : char *arg_list = NULL;
851 0 : node *n;
852 :
853 0 : for (n = type_list->h; n; n = n->next) {
854 0 : char *tpe = sql_subtype_string(sql->ta, (sql_subtype *) n->data);
855 :
856 0 : if (arg_list) {
857 0 : arg_list = sa_message(sql->ta, "%s, %s", arg_list, tpe);
858 : } else {
859 : arg_list = tpe;
860 : }
861 : }
862 0 : (void)sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: name '%s' (%s) already in use", F, fname, arg_list ? arg_list : "");
863 0 : list_destroy(type_list);
864 0 : return NULL;
865 29 : } else if (!replace) {
866 2 : list_destroy(type_list);
867 2 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: name '%s' already in use", F, fname);
868 27 : } else if (replace && !sf->func->s) {
869 0 : list_destroy(type_list);
870 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: cannot replace system function '%s'", F, fname);
871 : }
872 : } else {
873 235417 : sql->session->status = 0; /* if the function was not found clean the error */
874 235417 : sql->errstr[0] = '\0';
875 : }
876 :
877 235444 : if (create && (type == F_FUNC || type == F_AGGR || type == F_FILT)) {
878 80310 : sql_subfunc *found = NULL;
879 106395 : if ((found = sql_bind_func_(sql, s->base.name, fname, type_list, (type == F_FUNC || type == F_FILT) ? F_AGGR : F_FUNC, true, true))) {
880 2 : list_destroy(type_list);
881 2 : if (found->func->private) /* cannot create a function using a private name or replace a existing one */
882 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: name '%s' cannot be used", F, fname);
883 2 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE %s: there's %s with the name '%s' and the same parameters, which causes ambiguous calls", F,
884 2 : IS_AGGR(found->func) ? "an aggregate" : IS_FILT(found->func) ? "a filter function" : "a function", fname);
885 : }
886 80308 : sql->session->status = 0; /* if the function was not found clean the error */
887 80308 : sql->errstr[0] = '\0';
888 : }
889 :
890 235442 : list_destroy(type_list);
891 :
892 235442 : if (params) {
893 585906 : for (n = params->h; n; n = n->next) {
894 367516 : dnode *an = n->data.lval->h;
895 367516 : sql_add_param(sql, an->data.sval, &an->next->data.typeval);
896 : }
897 218390 : l = sql->params;
898 218390 : if (l && list_length(l) == 1) {
899 105796 : sql_arg *a = l->h->data;
900 :
901 105796 : if (strcmp(a->name, "*") == 0) {
902 : l = NULL;
903 : vararg = TRUE;
904 : }
905 : }
906 : }
907 : if (!l)
908 17055 : l = sa_list(sql->sa);
909 235442 : if (res && !(restype = result_type(sql, res)))
910 1 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: failed to get restype", F);
911 :
912 235441 : if (body && LANG_EXT(lang)) {
913 225 : const char *lang_body = body->h->data.sval, *mod = "unknown", *slang = "Unknown", *imp = "Unknown";
914 225 : switch (lang) {
915 : case FUNC_LANG_R:
916 : mod = "rapi";
917 : slang = "R";
918 : break;
919 42 : case FUNC_LANG_C:
920 42 : mod = "capi";
921 42 : slang = "C";
922 42 : break;
923 1 : case FUNC_LANG_CPP:
924 1 : mod = "capi";
925 1 : slang = "CPP";
926 1 : break;
927 0 : case FUNC_LANG_J:
928 0 : mod = "japi";
929 0 : slang = "Javascript";
930 0 : break;
931 145 : case FUNC_LANG_PY:
932 : case FUNC_LANG_PY3:
933 145 : mod = "pyapi3";
934 145 : slang = "Python";
935 145 : break;
936 0 : default:
937 0 : return sql_error(sql, 01, SQLSTATE(42000) "Function language without a MAL backend");
938 : }
939 225 : switch(type) {
940 : case F_AGGR:
941 : imp = "eval_aggr";
942 : break;
943 : case F_LOADER:
944 11 : imp = "eval_loader";
945 11 : break;
946 : default: /* for every other function type at the moment */
947 185 : imp = "eval";
948 : }
949 :
950 196 : if (type == F_LOADER && !(lang == FUNC_LANG_PY || lang == FUNC_LANG_PY3))
951 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: Language name \"Python[3]\" expected", F);
952 :
953 225 : sql->params = NULL;
954 225 : if (create) {
955 225 : bit side_effect = (list_empty(restype) || (!vararg && list_empty(l))); /* TODO make this more precise? */
956 225 : switch (mvc_create_func(&f, sql, sql->sa, s, fname, l, restype, type, lang, mod, imp, lang_body, (type == F_LOADER)?TRUE:FALSE, vararg, FALSE, side_effect)) {
957 0 : case -1:
958 0 : return sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
959 0 : case -2:
960 : case -3:
961 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: transaction conflict detected", F);
962 : default:
963 : break;
964 : }
965 0 : } else if (!sf) {
966 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: %s function %s.%s not bound", F, slang, s->base.name, fname);
967 : }
968 235216 : } else if (body) { /* SQL implementation */
969 15584 : sql_arg *ra = (restype && type != F_UNION)?restype->h->data:NULL;
970 15584 : list *b = NULL;
971 15584 : sql_schema *os = cur_schema(sql);
972 :
973 15584 : if (create) { /* needed for recursive functions */
974 7556 : bit side_effect = list_empty(restype) == 1; /* TODO make this more precise? */
975 7556 : q = query_cleaned(sql->ta, q);
976 7556 : switch (mvc_create_func(&f, sql, sql->sa, s, fname, l, restype, type, lang, sql_shared_module_name, NULL, q, FALSE, vararg, FALSE, side_effect)) {
977 0 : case -1:
978 0 : return sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
979 0 : case -2:
980 : case -3:
981 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: transaction conflict detected", F);
982 : default:
983 7556 : break;
984 : }
985 7556 : sql->forward = f;
986 8028 : } else if (!sf) {
987 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: SQL function %s.%s not bound", F, s->base.name, fname);
988 : }
989 15584 : sql->session->schema = s;
990 15584 : b = sequential_block(query, (ra)?&ra->type:NULL, ra?NULL:restype, body, NULL, is_func);
991 15584 : sql->forward = NULL;
992 15584 : sql->session->schema = os;
993 15584 : sql->params = NULL;
994 15584 : if (!b)
995 : return NULL;
996 :
997 : /* check if we have a return statement */
998 15548 : if (is_func && restype && !has_return(b))
999 4 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: missing return statement", F);
1000 15544 : if (!is_func && !restype && has_return(b))
1001 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: %ss cannot have return statements", F, fn);
1002 : /* in execute mode we instantiate the function */
1003 15544 : if (instantiate || deps)
1004 8026 : return rel_psm_block(sql->sa, b);
1005 : } else { /* MAL implementation */
1006 219632 : int clientid = sql->clientid;
1007 219632 : char *fmod = qname_module(ext_name);
1008 219632 : char *fnme = qname_schema_object(ext_name);
1009 :
1010 219632 : if (!fmod || !fnme)
1011 4 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: MAL module or function name missing", F);
1012 219632 : if (strlen(fmod) >= IDLENGTH)
1013 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: MAL module name '%s' too large for the backend", F, fmod);
1014 219632 : if (strlen(fnme) >= IDLENGTH)
1015 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: MAL function name '%s' too large for the backend", F, fnme);
1016 219632 : sql->params = NULL;
1017 219632 : if (create) {
1018 109818 : q = query_cleaned(sql->ta, q);
1019 109818 : switch (mvc_create_func(&f, sql, sql->sa, s, fname, l, restype, type, lang, fmod, fnme, q, FALSE, vararg, FALSE, FALSE)) {
1020 0 : case -1:
1021 4 : return sql_error(sql, 01, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1022 0 : case -2:
1023 : case -3:
1024 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: transaction conflict detected", F);
1025 : default:
1026 109818 : break;
1027 : }
1028 : /* instantiate MAL functions while being created. This also sets the side-effects flag */
1029 109818 : bool se = f->side_effect;
1030 109818 : if (!backend_resolve_function(&clientid, f, fnme, &se))
1031 4 : return sql_error(sql, 02, SQLSTATE(3F000) "CREATE %s: external name %s.%s not bound (%s.%s)", F, fmod, fnme, s->base.name, fname );
1032 109814 : f->side_effect = se;
1033 109814 : f->instantiated = TRUE;
1034 109814 : } else if (!sf) {
1035 0 : return sql_error(sql, 01, SQLSTATE(42000) "CREATE %s: external name %s.%s not bound (%s.%s)", F, fmod, fnme, s->base.name, fname );
1036 : }
1037 219628 : if (!f)
1038 109814 : f = sf->func;
1039 219628 : assert(f);
1040 : }
1041 227371 : return rel_create_function(sql->sa, s->base.name, f, replace);
1042 : }
1043 :
1044 : static sql_rel*
1045 794 : rel_drop_function(allocator *sa, const char *sname, const char *name, int nr, sql_ftype type, int action)
1046 : {
1047 794 : sql_rel *rel = rel_create(sa);
1048 794 : list *exps = new_exp_list(sa);
1049 794 : if(!rel || !exps)
1050 : return NULL;
1051 :
1052 794 : append(exps, exp_atom_clob(sa, sname));
1053 794 : append(exps, exp_atom_clob(sa, name));
1054 794 : append(exps, exp_atom_int(sa, nr));
1055 794 : append(exps, exp_atom_int(sa, (int) type));
1056 794 : append(exps, exp_atom_int(sa, action));
1057 794 : rel->l = NULL;
1058 794 : rel->r = NULL;
1059 794 : rel->op = op_ddl;
1060 794 : rel->flag = ddl_drop_function;
1061 794 : rel->exps = exps;
1062 794 : rel->card = 0;
1063 794 : rel->nrcols = 0;
1064 794 : return rel;
1065 : }
1066 :
1067 : sql_func *
1068 94039 : resolve_func(mvc *sql, const char *sname, const char *name, dlist *typelist, sql_ftype type, const char *op, int if_exists)
1069 : {
1070 94039 : sql_func *func = NULL;
1071 94039 : list *list_func = NULL, *type_list = NULL;
1072 94039 : char is_func = (type != F_PROC && type != F_LOADER), *F = NULL, *fn = NULL;
1073 :
1074 94039 : FUNC_TYPE_STR(type, F, fn)
1075 :
1076 94039 : if (typelist) {
1077 90005 : sql_subfunc *sub_func;
1078 :
1079 90005 : type_list = create_type_list(sql, typelist, 0);
1080 90005 : sub_func = sql_bind_func_(sql, sname, name, type_list, type, false, true);
1081 90005 : if (!sub_func && type == F_FUNC) {
1082 1853 : sql->session->status = 0; /* if the function was not found clean the error */
1083 1853 : sql->errstr[0] = '\0';
1084 1853 : sub_func = sql_bind_func_(sql, sname, name, type_list, F_UNION, false, true);
1085 1853 : type = sub_func?F_UNION:F_FUNC;
1086 : }
1087 89986 : if ( sub_func && sub_func->func->type == type)
1088 89982 : func = sub_func->func;
1089 : } else {
1090 4034 : list_func = sql_find_funcs_by_name(sql, sname, name, type, false);
1091 4034 : if (!list_func && type == F_FUNC) {
1092 1221 : sql->session->status = 0; /* if the function was not found clean the error */
1093 1221 : sql->errstr[0] = '\0';
1094 1221 : list_func = sql_find_funcs_by_name(sql, sname, name, F_UNION, false);
1095 : }
1096 4034 : if (list_func && list_func->cnt > 1) {
1097 3 : list_destroy(list_func);
1098 3 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s: there are more than one %s called '%s', please use the full signature", op, F, fn, name);
1099 : }
1100 4026 : if (list_func && list_func->cnt == 1)
1101 4026 : func = (sql_func*) list_func->h->data;
1102 : }
1103 :
1104 94036 : if (!func) {
1105 28 : void *e = NULL;
1106 28 : if (typelist) {
1107 23 : char *arg_list = NULL;
1108 23 : node *n;
1109 :
1110 23 : if (type_list->cnt > 0) {
1111 49 : for (n = type_list->h; n; n = n->next) {
1112 30 : char *tpe = sql_subtype_string(sql->ta, (sql_subtype *) n->data);
1113 :
1114 30 : if (arg_list) {
1115 11 : arg_list = sa_message(sql->ta, "%s, %s", arg_list, tpe);
1116 : } else {
1117 : arg_list = tpe;
1118 : }
1119 : }
1120 19 : list_destroy(list_func);
1121 19 : list_destroy(type_list);
1122 19 : if (!if_exists)
1123 16 : e = sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s: no such %s '%s' (%s)", op, F, fn, name, arg_list);
1124 19 : return e;
1125 : }
1126 4 : list_destroy(list_func);
1127 4 : list_destroy(type_list);
1128 4 : if (!if_exists)
1129 2 : e = sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s: no such %s '%s' ()", op, F, fn, name);
1130 4 : return e;
1131 : } else {
1132 5 : if (!if_exists)
1133 5 : e = sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s: no such %s '%s'", op, F, fn, name);
1134 5 : return e;
1135 : }
1136 94008 : } else if (((is_func && type != F_FILT) && !func->res) || (!is_func && func->res)) {
1137 0 : list_destroy(list_func);
1138 0 : list_destroy(type_list);
1139 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s %s: cannot drop %s '%s'", op, F, fn, name);
1140 : }
1141 :
1142 94008 : list_destroy(list_func);
1143 94008 : list_destroy(type_list);
1144 94008 : return func;
1145 : }
1146 :
1147 : static sql_rel*
1148 796 : rel_drop_func(mvc *sql, dlist *qname, dlist *typelist, int drop_action, sql_ftype type, int if_exists)
1149 : {
1150 796 : const char *name = qname_schema_object(qname);
1151 796 : const char *sname = qname_schema(qname);
1152 796 : sql_func *func = NULL;
1153 796 : char *F = NULL, *fn = NULL;
1154 :
1155 796 : FUNC_TYPE_STR(type, F, fn)
1156 :
1157 796 : if (!(func = resolve_func(sql, sname, name, typelist, type, "DROP", if_exists))) {
1158 30 : if (if_exists) {
1159 5 : sql->errstr[0] = '\0'; /* reset function not found error */
1160 5 : sql->session->status = 0;
1161 5 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1162 : }
1163 : return NULL;
1164 : }
1165 766 : if (!func->s) /* attempting to drop a system function */
1166 0 : return sql_error(sql, 02, SQLSTATE(42000) "DROP %s: cannot drop system %s '%s'", F, fn, name);
1167 766 : if (!mvc_schema_privs(sql, func->s))
1168 0 : return sql_error(sql, 02, SQLSTATE(42000) "DROP %s: insufficient privileges for user '%s' in schema '%s'", F, get_string_global_var(sql, "current_user"), func->s->base.name);
1169 766 : return rel_drop_function(sql->sa, func->s->base.name, name, func->base.id, type, drop_action);
1170 : }
1171 :
1172 : static sql_rel*
1173 106 : rel_drop_all_func(mvc *sql, dlist *qname, int drop_action, sql_ftype type)
1174 : {
1175 106 : const char *name = qname_schema_object(qname);
1176 106 : const char *sname = qname_schema(qname);
1177 106 : sql_schema *s = cur_schema(sql);
1178 106 : list *list_func = NULL;
1179 106 : char *F = NULL, *fn = NULL;
1180 :
1181 106 : FUNC_TYPE_STR(type, F, fn)
1182 :
1183 106 : if (sname && !(s = mvc_bind_schema(sql, sname)))
1184 0 : return sql_error(sql, 02, SQLSTATE(3F000) "DROP ALL %s: no such schema '%s'", F, sname);
1185 106 : if (!mvc_schema_privs(sql, s))
1186 0 : return sql_error(sql, 02, SQLSTATE(42000) "DROP ALL %s: insufficient privileges for user '%s' in schema '%s'", F, get_string_global_var(sql, "current_user"), s->base.name);
1187 :
1188 106 : if (!(list_func = sql_find_funcs_by_name(sql, s->base.name, name, type, false)) || list_empty(list_func)) {
1189 78 : list_destroy(list_func);
1190 78 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "DROP ALL %s: no such %s '%s'", F, fn, name);
1191 : }
1192 28 : list_destroy(list_func);
1193 28 : return rel_drop_function(sql->sa, s->base.name, name, -1, type, drop_action);
1194 : }
1195 :
1196 : static sql_rel *
1197 334 : rel_create_trigger(mvc *sql, const char *sname, const char *tname, const char *triggername, int time, int orientation, int event, const char *old_name, const char *new_name, symbol *condition, const char *query, int replace)
1198 : {
1199 334 : sql_rel *rel = rel_create(sql->sa);
1200 334 : list *exps = new_exp_list(sql->sa);
1201 334 : if(!rel || !exps)
1202 : return NULL;
1203 :
1204 334 : append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
1205 334 : append(exps, exp_atom_str(sql->sa, tname, sql_bind_localtype("str") ));
1206 334 : append(exps, exp_atom_str(sql->sa, triggername, sql_bind_localtype("str") ));
1207 334 : append(exps, exp_atom_int(sql->sa, time));
1208 334 : append(exps, exp_atom_int(sql->sa, orientation));
1209 334 : append(exps, exp_atom_int(sql->sa, event));
1210 334 : append(exps, exp_atom_str(sql->sa, old_name, sql_bind_localtype("str") ));
1211 334 : append(exps, exp_atom_str(sql->sa, new_name, sql_bind_localtype("str") ));
1212 334 : (void)condition;
1213 334 : append(exps, exp_atom_str(sql->sa, NULL, sql_bind_localtype("str") ));
1214 334 : append(exps, exp_atom_str(sql->sa, query, sql_bind_localtype("str") ));
1215 334 : append(exps, exp_atom_int(sql->sa, replace));
1216 334 : rel->l = NULL;
1217 334 : rel->r = NULL;
1218 334 : rel->op = op_ddl;
1219 334 : rel->flag = ddl_create_trigger;
1220 334 : rel->exps = exps;
1221 334 : rel->card = CARD_MULTI;
1222 334 : rel->nrcols = 0;
1223 334 : return rel;
1224 : }
1225 :
1226 : static sql_rel_view*
1227 65 : _stack_push_table(mvc *sql, const char *tname, sql_table *t)
1228 : {
1229 65 : sql_rel *r = rel_basetable(sql, t, tname );
1230 65 : rel_base_use_all(sql, r);
1231 65 : r = rewrite_basetable(sql, r);
1232 65 : return stack_push_rel_view(sql, tname, r);
1233 : }
1234 :
1235 : static sql_rel *
1236 5603 : create_trigger(sql_query *query, dlist *qname, int time, symbol *trigger_event, dlist *tqname, dlist *opt_ref, dlist *triggered_action, int replace)
1237 : {
1238 5603 : mvc *sql = query->sql;
1239 5603 : const char *triggerschema = qname_schema(qname);
1240 5603 : const char *triggername = qname_schema_object(qname);
1241 5603 : char *sname = tqname? qname_schema(tqname) : NULL;
1242 5600 : char *tname = tqname? qname_schema_object(tqname) : NULL;
1243 5603 : int instantiate = (sql->emode == m_instantiate);
1244 5603 : int create = (!instantiate && sql->emode != m_deps), event, orientation;
1245 5603 : sql_schema *ss = cur_schema(sql), *old_schema = cur_schema(sql);
1246 5603 : sql_table *t = NULL;
1247 5603 : list *sq = NULL;
1248 5603 : sql_rel *r = NULL;
1249 5603 : char *q, *base = replace ? "CREATE OR REPLACE TRIGGER" : "CREATE TRIGGER";
1250 5603 : dlist *columns = trigger_event->data.lval;
1251 5603 : const char *old_name = NULL, *new_name = NULL;
1252 5603 : dlist *stmts = triggered_action->h->next->next->data.lval;
1253 5603 : symbol *condition = triggered_action->h->next->data.sym;
1254 5603 : bool old_useviews = sql->use_views;
1255 :
1256 5603 : if (opt_ref) {
1257 173 : dnode *dl = opt_ref->h;
1258 391 : for ( ; dl; dl = dl->next) {
1259 : /* list (new(1)/old(0)), char */
1260 218 : char *n = dl->data.lval->h->next->data.sval;
1261 :
1262 218 : assert(dl->data.lval->h->type == type_int);
1263 218 : if (!dl->data.lval->h->data.i_val) /*?l_val?*/
1264 : old_name = n;
1265 : else
1266 143 : new_name = n;
1267 : }
1268 : }
1269 :
1270 5603 : if (sname && !(ss = mvc_bind_schema(sql, sname)))
1271 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
1272 :
1273 5603 : if (create) {
1274 342 : if (triggerschema)
1275 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: a trigger will be placed on the respective table's schema, specify the schema on the table reference, ie ON clause instead", base);
1276 341 : if (tname) {
1277 340 : if (!(t = mvc_bind_table(sql, ss, tname)))
1278 4 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "%s: no such table %s%s%s'%s'", base, sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname);
1279 338 : if (isView(t))
1280 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: cannot create trigger on view '%s'", base, tname);
1281 337 : sname = t->s->base.name;
1282 337 : tname = t->base.name;
1283 : }
1284 338 : if (!mvc_schema_privs(sql, ss))
1285 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), ss->base.name);
1286 337 : if (!replace && mvc_bind_trigger(sql, ss, triggername) != NULL)
1287 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: name '%s' already in use", base, triggername);
1288 337 : switch (trigger_event->token) {
1289 41 : case SQL_INSERT: {
1290 41 : if (old_name)
1291 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: old name not allowed at insert events", base);
1292 : event = 0;
1293 : } break;
1294 27 : case SQL_DELETE: {
1295 27 : if (new_name)
1296 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: new name not allowed at delete events", base);
1297 : event = 1;
1298 : } break;
1299 1 : case SQL_TRUNCATE: {
1300 1 : if (new_name)
1301 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: new name not allowed at truncate events", base);
1302 : event = 3;
1303 : } break;
1304 267 : case SQL_UPDATE: {
1305 267 : if (old_name && new_name && !strcmp(old_name, new_name))
1306 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: old and new names cannot be the same", base);
1307 266 : if (!old_name && new_name && !strcmp("old", new_name))
1308 1 : return sql_error(sql, 02, SQLSTATE(42000) "%s: old and new names cannot be the same", base);
1309 265 : if (!new_name && old_name && !strcmp("new", old_name))
1310 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: old and new names cannot be the same", base);
1311 : event = 2;
1312 : } break;
1313 : case SQL_LOGIN:
1314 : // TODO any checks here?
1315 : event = LOGIN_EVENT;
1316 : break;
1317 0 : default:
1318 0 : return sql_error(sql, 02, SQLSTATE(42000) "%s: invalid event: %s", base, token2string(trigger_event->token));
1319 : }
1320 :
1321 334 : assert(triggered_action->h->type == type_int);
1322 334 : orientation = triggered_action->h->data.i_val;
1323 334 : q = query_cleaned(sql->ta, QUERY(sql->scanner));
1324 334 : return rel_create_trigger(sql, sname, tname, triggername, time, orientation, event, old_name, new_name, condition, q, replace);
1325 : }
1326 :
1327 5261 : if (!instantiate) {
1328 333 : if (!stack_push_frame(sql, "%OLD-NEW"))
1329 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1330 333 : if (tname) {
1331 331 : if (!(t = mvc_bind_table(sql, ss, tname)))
1332 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S02) "%s: no such table %s%s%s'%s'", base, sname ? "'":"", sname ? sname : "", sname ? "'.":"", tname);
1333 : /* we need to add the old and new tables */
1334 331 : if (new_name && !_stack_push_table(sql, new_name, t)) {
1335 0 : stack_pop_frame(sql);
1336 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1337 : }
1338 331 : if (old_name && !_stack_push_table(sql, old_name, t)) {
1339 0 : stack_pop_frame(sql);
1340 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
1341 : }
1342 : }
1343 : }
1344 5261 : if (condition) {
1345 8 : sql_rel *rel = NULL;
1346 :
1347 8 : if (new_name) /* in case of updates same relations is available via both names */
1348 6 : rel = stack_find_rel_view(sql, new_name);
1349 8 : if (!rel && old_name)
1350 0 : rel = stack_find_rel_view(sql, old_name);
1351 8 : if (!rel)
1352 2 : rel = stack_find_rel_view(sql, "old");
1353 2 : if (!rel)
1354 2 : rel = stack_find_rel_view(sql, "new");
1355 8 : rel = rel_logical_exp(query, rel, condition, sql_where);
1356 8 : if (!rel) {
1357 2 : if (!instantiate)
1358 2 : stack_pop_frame(sql);
1359 2 : return NULL;
1360 : }
1361 : /* transition tables */
1362 : /* insert: rel_select(table [new], searchcondition) */
1363 : /* delete: rel_select(table [old], searchcondition) */
1364 : /* update: rel_select(table [old,new]), searchcondition) */
1365 6 : if (new_name)
1366 6 : stack_update_rel_view(sql, new_name, rel);
1367 6 : if (old_name)
1368 0 : stack_update_rel_view(sql, old_name, new_name?rel_dup(rel):rel);
1369 : }
1370 5259 : sql->use_views = true; /* leave the 'use_views' hack to where it belongs */
1371 5259 : sql->session->schema = ss;
1372 5259 : sq = sequential_block(query, NULL, NULL, stmts, NULL, 1);
1373 5259 : sql->session->schema = old_schema;
1374 5259 : sql->use_views = old_useviews;
1375 5259 : if (!sq) {
1376 5 : if (!instantiate)
1377 2 : stack_pop_frame(sql);
1378 5 : return NULL;
1379 : }
1380 5254 : r = rel_psm_block(sql->sa, sq);
1381 :
1382 5254 : if (!instantiate)
1383 329 : stack_pop_frame(sql);
1384 : /* todo trigger_columns */
1385 : (void)columns;
1386 : return r;
1387 : }
1388 :
1389 : static sql_rel *
1390 80 : rel_drop_trigger(mvc *sql, const char *sname, const char *tname, int if_exists)
1391 : {
1392 80 : sql_rel *rel = rel_create(sql->sa);
1393 80 : list *exps = new_exp_list(sql->sa);
1394 80 : if(!rel || !exps)
1395 : return NULL;
1396 :
1397 80 : append(exps, exp_atom_str(sql->sa, sname, sql_bind_localtype("str") ));
1398 80 : append(exps, exp_atom_str(sql->sa, tname, sql_bind_localtype("str") ));
1399 80 : append(exps, exp_atom_int(sql->sa, if_exists));
1400 80 : rel->l = NULL;
1401 80 : rel->r = NULL;
1402 80 : rel->op = op_ddl;
1403 80 : rel->flag = ddl_drop_trigger;
1404 80 : rel->exps = exps;
1405 80 : rel->card = CARD_MULTI;
1406 80 : rel->nrcols = 0;
1407 80 : return rel;
1408 : }
1409 :
1410 : static sql_rel *
1411 91 : drop_trigger(mvc *sql, dlist *qname, int if_exists)
1412 : {
1413 91 : const char *sname = qname_schema(qname);
1414 91 : const char *tname = qname_schema_object(qname);
1415 91 : sql_trigger *tr = NULL;
1416 :
1417 91 : if (!(tr = find_trigger_on_scope(sql, sname, tname, "DROP TRIGGER"))) {
1418 11 : if (if_exists) {
1419 1 : sql->errstr[0] = '\0'; /* reset trigger not found error */
1420 1 : sql->session->status = 0;
1421 1 : return rel_psm_block(sql->sa, new_exp_list(sql->sa));
1422 : }
1423 : return NULL;
1424 : }
1425 80 : if (!mvc_schema_privs(sql, tr->t->s))
1426 0 : return sql_error(sql, 02, SQLSTATE(3F000) "DROP TRIGGER: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), tr->t->s->base.name);
1427 80 : return rel_drop_trigger(sql, tr->t->s->base.name, tname, if_exists);
1428 : }
1429 :
1430 : static sql_rel *
1431 35 : psm_analyze(sql_query *query, dlist *qname, dlist *columns)
1432 : {
1433 35 : mvc *sql = query->sql;
1434 35 : const char *sname = qname_schema(qname), *tname = qname_schema_object(qname);
1435 35 : list *tl = sa_list(sql->sa), *exps = sa_list(sql->sa), *analyze_calls = sa_list(sql->sa);
1436 35 : sql_subfunc *f = NULL;
1437 35 : sql_subtype tpe;
1438 :
1439 35 : if (!sql_find_subtype(&tpe, "varchar", 1024, 0))
1440 0 : return sql_error(sql, 02, SQLSTATE(HY013) "varchar type missing?");
1441 :
1442 35 : if (sname && tname) {
1443 28 : sql_table *t = NULL;
1444 :
1445 28 : if (!(t = find_table_or_view_on_scope(sql, NULL, sname, tname, "ANALYZE", false)))
1446 : return NULL;
1447 28 : if (isDeclaredTable(t))
1448 0 : return sql_error(sql, 02, SQLSTATE(42000) "Cannot analyze a declared table");
1449 28 : sname = t->s->base.name;
1450 : }
1451 : /* call analyze( [schema, [ table ]] ) */
1452 35 : if (sname) {
1453 28 : sql_exp *sname_exp = exp_atom_str(sql->sa, sname, &tpe);
1454 :
1455 28 : list_append(exps, sname_exp);
1456 28 : list_append(tl, exp_subtype(sname_exp));
1457 : }
1458 35 : if (tname) {
1459 35 : sql_exp *tname_exp = exp_atom_str(sql->sa, tname, &tpe);
1460 :
1461 35 : list_append(exps, tname_exp);
1462 35 : list_append(tl, exp_subtype(tname_exp));
1463 :
1464 35 : if (columns)
1465 3 : list_append(tl, exp_subtype(tname_exp));
1466 : }
1467 3 : if (!columns) {
1468 32 : if (!(f = sql_bind_func_(sql, "sys", "analyze", tl, F_PROC, true, false)))
1469 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Analyze procedure missing");
1470 32 : if (!execute_priv(sql, f->func))
1471 0 : return sql_error(sql, 02, SQLSTATE(42000) "No privilege to call analyze procedure");
1472 32 : list_append(analyze_calls, exp_op(sql->sa, exps, f));
1473 : } else {
1474 3 : if (!sname || !tname)
1475 2 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Analyze schema or table name missing");
1476 1 : if (!(f = sql_bind_func_(sql, "sys", "analyze", tl, F_PROC, true, false)))
1477 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "Analyze procedure missing");
1478 1 : if (!execute_priv(sql, f->func))
1479 0 : return sql_error(sql, 02, SQLSTATE(42000) "No privilege to call analyze procedure");
1480 3 : for(dnode *n = columns->h; n; n = n->next) {
1481 2 : const char *cname = n->data.sval;
1482 2 : list *nexps = list_dup(exps, NULL);
1483 2 : sql_exp *cname_exp = exp_atom_str(sql->sa, cname, &tpe);
1484 :
1485 2 : list_append(nexps, cname_exp);
1486 : /* call analyze( opt_minmax, opt_sample_size, sname, tname, cname) */
1487 2 : list_append(analyze_calls, exp_op(sql->sa, nexps, f));
1488 : }
1489 : }
1490 33 : return rel_psm_block(sql->sa, analyze_calls);
1491 : }
1492 :
1493 : static sql_rel*
1494 2 : create_table_from_loader(sql_query *query, dlist *qname, symbol *fcall)
1495 : {
1496 2 : mvc *sql = query->sql;
1497 2 : sql_schema *s = cur_schema(sql);
1498 2 : char *sname = qname_schema(qname);
1499 2 : char *tname = qname_schema_object(qname);
1500 2 : sql_subfunc *loader = NULL;
1501 2 : sql_rel *rel = NULL;
1502 2 : sql_table *t = NULL;
1503 :
1504 2 : if (sname && !(s = mvc_bind_schema(sql, sname)))
1505 0 : return sql_error(sql, ERR_NOTFOUND, SQLSTATE(3F000) "CREATE TABLE FROM LOADER: no such schema '%s'", sname);
1506 2 : if ((t = mvc_bind_table(sql, s, tname)))
1507 0 : return sql_error(sql, 02, SQLSTATE(42S01) "CREATE TABLE FROM LOADER: name '%s' already in use", tname);
1508 2 : if (!mvc_schema_privs(sql, s))
1509 0 : return sql_error(sql, 02, SQLSTATE(42000) "CREATE TABLE FROM LOADER: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1510 :
1511 2 : rel = rel_loader_function(query, fcall, new_exp_list(sql->sa), &loader);
1512 2 : if (!rel || !loader)
1513 : return NULL;
1514 :
1515 1 : loader->sname = s ? sa_strdup(sql->sa, s->base.name) : NULL;
1516 1 : loader->tname = tname ? sa_strdup(sql->sa, tname) : NULL;
1517 :
1518 1 : return rel;
1519 : }
1520 :
1521 : static list *
1522 4 : rel_paramlist( sql_query *query, symbol *nop)
1523 : {
1524 4 : dnode *ops = nop->data.lval->h->next->next->data.lval->h;
1525 4 : list *exps = sa_list(query->sql->sa);
1526 4 : exp_kind iek = {type_value, card_column, FALSE};
1527 :
1528 9 : for (; ops; ops = ops->next) {
1529 6 : sql_exp *e = rel_value_exp(query, NULL, ops->data.sym, sql_farg, iek);
1530 6 : if (!e)
1531 : return NULL;
1532 6 : ops = ops->next;
1533 6 : sql_arg *a = sql_find_param(query->sql, ops->data.sval);
1534 6 : if (!a)
1535 1 : return sql_error(query->sql, 06, SQLSTATE(42000) "Named placeholder ('%s') not used in the query.", ops->data.sval);
1536 5 : a->type = *exp_subtype(e);
1537 5 : append(exps, e);
1538 : }
1539 : return exps;
1540 : }
1541 :
1542 :
1543 : sql_rel *
1544 250859 : rel_psm(sql_query *query, symbol *s)
1545 : {
1546 250859 : mvc *sql = query->sql;
1547 250859 : sql_rel *ret = NULL;
1548 :
1549 250859 : switch (s->token) {
1550 235453 : case SQL_CREATE_FUNC:
1551 : {
1552 235453 : dlist *l = s->data.lval;
1553 235453 : sql_ftype type = (sql_ftype) l->h->next->next->next->next->next->data.i_val;
1554 235453 : sql_flang lang = (sql_flang) l->h->next->next->next->next->next->next->data.i_val;
1555 235453 : int repl = l->h->next->next->next->next->next->next->next->data.i_val;
1556 :
1557 235453 : ret = rel_create_func(query, l->h->data.lval, l->h->next->data.lval, l->h->next->next->data.sym, l->h->next->next->next->data.lval, l->h->next->next->next->next->data.lval, type, lang, repl);
1558 235453 : sql->type = Q_SCHEMA;
1559 235453 : } break;
1560 902 : case SQL_DROP_FUNC:
1561 : {
1562 902 : dlist *l = s->data.lval;
1563 902 : dlist *qname = l->h->data.lval;
1564 902 : dlist *typelist = l->h->next->data.lval;
1565 902 : sql_ftype type = (sql_ftype) l->h->next->next->data.i_val;
1566 902 : int if_exists = l->h->next->next->next->data.i_val;
1567 902 : int all = l->h->next->next->next->next->data.i_val;
1568 902 : int drop_action = l->h->next->next->next->next->next->data.i_val;
1569 :
1570 902 : if (store_readonly(sql->session->tr->store))
1571 0 : return sql_error(sql, 06, SQLSTATE(42000) "Schema statements cannot be executed on a readonly database.");
1572 :
1573 902 : if (all)
1574 106 : ret = rel_drop_all_func(sql, qname, drop_action, type);
1575 : else
1576 796 : ret = rel_drop_func(sql, qname, typelist, drop_action, type, if_exists);
1577 :
1578 902 : sql->type = Q_SCHEMA;
1579 902 : } break;
1580 367 : case SQL_SET:
1581 367 : ret = rel_psm_stmt(sql->sa, psm_set_exp(query, s->data.lval->h));
1582 367 : sql->type = Q_SCHEMA;
1583 367 : break;
1584 11 : case SQL_DECLARE:
1585 11 : return sql_error(sql, 02, SQLSTATE(42000) "Variables cannot be declared on the global scope");
1586 8395 : case SQL_CALL:
1587 8395 : sql->type = Q_UPDATE;
1588 8395 : if (s->type == type_list) {
1589 4 : list *params = rel_paramlist( query, s->data.lval->h->next->data.sym);
1590 4 : if (!params)
1591 : return NULL;
1592 3 : ret = rel_semantic(query, s->data.lval->h->data.sym);
1593 3 : query->last_rel = ret;
1594 3 : if (ret)
1595 3 : ret = rel_psm_stmt(sql->sa, rel_psm_call(query, s->data.lval->h->next->data.sym));
1596 3 : ret = rel_list(sql->sa, query->last_rel, ret);
1597 : } else
1598 8391 : ret = rel_psm_stmt(sql->sa, rel_psm_call(query, s->data.sym));
1599 : break;
1600 2 : case SQL_CREATE_TABLE_LOADER:
1601 : {
1602 2 : dlist *l = s->data.lval;
1603 2 : dlist *qname = l->h->data.lval;
1604 2 : symbol *sym = l->h->next->data.sym;
1605 :
1606 2 : ret = create_table_from_loader(query, qname, sym);
1607 2 : if (ret == NULL)
1608 : return NULL;
1609 1 : ret = rel_psm_stmt(sql->sa, exp_rel(sql, ret));
1610 1 : sql->type = Q_SCHEMA;
1611 1 : } break;
1612 5603 : case SQL_CREATE_TRIGGER:
1613 : {
1614 5603 : dlist *l = s->data.lval;
1615 :
1616 5603 : assert(l->h->next->type == type_int);
1617 5603 : ret = create_trigger(query, l->h->data.lval, l->h->next->data.i_val, l->h->next->next->data.sym, l->h->next->next->next->data.lval, l->h->next->next->next->next->data.lval, l->h->next->next->next->next->next->data.lval, l->h->next->next->next->next->next->next->data.i_val);
1618 5603 : sql->type = Q_SCHEMA;
1619 5603 : } break;
1620 91 : case SQL_DROP_TRIGGER:
1621 : {
1622 91 : dlist *l = s->data.lval;
1623 91 : dlist *qname = l->h->data.lval;
1624 91 : int if_exists = l->h->next->data.i_val;
1625 :
1626 91 : ret = drop_trigger(sql, qname, if_exists);
1627 91 : sql->type = Q_SCHEMA;
1628 91 : } break;
1629 35 : case SQL_ANALYZE: {
1630 35 : dlist *l = s->data.lval;
1631 :
1632 : /* Jan2022 update: The 'sample' and 'minmax' parameters are now ignored because they are no longer used in the backend */
1633 35 : ret = psm_analyze(query, l->h->data.lval /* qualified table name */, l->h->next->data.lval /* opt list of column */);
1634 35 : sql->type = Q_UPDATE;
1635 35 : } break;
1636 0 : default:
1637 0 : return sql_error(sql, 01, SQLSTATE(42000) "Schema statement unknown symbol(%p)->token = %s", s, token2string(s->token));
1638 : }
1639 : return ret;
1640 : }
|