Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * authors M Kersten, N Nes
15 : * SQL catalog support implementation
16 : * This module contains the wrappers around the SQL catalog operations
17 : */
18 : #include "monetdb_config.h"
19 : #include "sql_cat.h"
20 : #include "sql_gencode.h"
21 : #include "sql_optimizer.h"
22 : #include "sql_scenario.h"
23 : #include "sql_mvc.h"
24 : #include "sql_qc.h"
25 : #include "sql_partition.h"
26 : #include "sql_statistics.h"
27 : #include "mal_namespace.h"
28 : #include "opt_prelude.h"
29 : #include "querylog.h"
30 : #include "mal_builder.h"
31 :
32 : #include "rel_select.h"
33 : #include "rel_prop.h"
34 : #include "rel_rel.h"
35 : #include "rel_exp.h"
36 : #include "rel_bin.h"
37 : #include "rel_dump.h"
38 : #include "orderidx.h"
39 : #include "sql_user.h"
40 :
41 : #define initcontext() \
42 : if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL) \
43 : return msg; \
44 : if ((msg = checkSQLContext(cntxt)) != NULL) \
45 : return msg; \
46 : if (store_readonly(sql->session->tr->store)) \
47 : throw(SQL,"sql.cat",SQLSTATE(25006) "Schema statements cannot be executed on a readonly database.");
48 :
49 : static char *
50 26411 : SaveArgReference(MalStkPtr stk, InstrPtr pci, int arg)
51 : {
52 26411 : char *val = *getArgReference_str(stk, pci, arg);
53 :
54 31945 : if (strNil(val))
55 : val = NULL;
56 26411 : return val;
57 : }
58 :
59 : static int
60 2305 : table_has_updates(sql_trans *tr, sql_table *t)
61 : {
62 2305 : node *n;
63 2305 : int cnt = 0;
64 2305 : sqlstore *store = tr->store;
65 :
66 7185 : for ( n = ol_first_node(t->columns); !cnt && n; n = n->next) {
67 4880 : sql_column *c = n->data;
68 :
69 4880 : size_t upd = store->storage_api.count_col( tr, c, 2/* count updates */);
70 4880 : cnt |= upd > 0;
71 : }
72 2305 : return cnt;
73 : }
74 :
75 : static char *
76 574 : rel_check_tables(mvc *sql, sql_table *nt, sql_table *nnt, const char *errtable)
77 : {
78 574 : node *n, *m, *nn, *mm;
79 :
80 574 : if (ol_length(nt->columns) != ol_length(nnt->columns))
81 1 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table doesn't match %s definition", errtable, errtable);
82 1842 : for (n = ol_first_node(nt->columns), m = ol_first_node(nnt->columns); n && m; n = n->next, m = m->next) {
83 1276 : sql_column *nc = n->data;
84 1276 : sql_column *mc = m->data;
85 :
86 1276 : if (subtype_cmp(&nc->type, &mc->type) != 0)
87 2 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table column type doesn't match %s definition", errtable, errtable);
88 1274 : if (nc->null != mc->null)
89 3 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table column NULL check doesn't match %s definition", errtable, errtable);
90 1271 : if (isRangePartitionTable(nt) || isListPartitionTable(nt)) {
91 461 : if ((nc->def || mc->def) && (!nc->def || !mc->def || strcmp(nc->def, mc->def) != 0))
92 2 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table column DEFAULT value doesn't match %s definition", errtable, errtable);
93 : }
94 : }
95 566 : if (ol_length(nt->keys) != ol_length(nnt->keys))
96 2 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table key doesn't match %s definition", errtable, errtable);
97 564 : if (ol_length(nt->keys))
98 133 : for (n = ol_first_node(nt->keys), m = ol_first_node(nnt->keys); n && m; n = n->next, m = m->next) {
99 71 : sql_key *ni = n->data;
100 71 : sql_key *mi = m->data;
101 :
102 71 : if (ni->type != mi->type)
103 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table key type doesn't match %s definition", errtable, errtable);
104 71 : if (list_length(ni->columns) != list_length(mi->columns))
105 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table key type doesn't match %s definition", errtable, errtable);
106 148 : for (nn = ni->columns->h, mm = mi->columns->h; nn && mm; nn = nn->next, mm = mm->next) {
107 79 : sql_kc *nni = nn->data;
108 79 : sql_kc *mmi = mm->data;
109 :
110 79 : if (nni->c->colnr != mmi->c->colnr)
111 2 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table key's columns doesn't match %s definition", errtable, errtable);
112 : }
113 : }
114 :
115 : /* For indexes, empty ones can be ignored, which makes validation trickier */
116 562 : n = ol_length(nt->idxs) ? ol_first_node(nt->idxs) : NULL;
117 562 : m = ol_length(nnt->idxs) ? ol_first_node(nnt->idxs) : NULL;
118 570 : for (; n || m; n = n->next, m = m->next) {
119 : sql_idx *ni, *mi;
120 :
121 122 : while (n) {
122 68 : ni = n->data;
123 68 : if ((!hash_index(ni->type) || list_length(ni->columns) > 1) && idx_has_column(ni->type))
124 : break;
125 60 : n = n->next;
126 : }
127 122 : while (m) {
128 68 : mi = m->data;
129 68 : if ((!hash_index(mi->type) || list_length(mi->columns) > 1) && idx_has_column(mi->type))
130 : break;
131 60 : m = m->next;
132 : }
133 :
134 62 : if (!n && !m) /* no more idxs to check, done */
135 : break;
136 8 : if ((m && !n) || (!m && n))
137 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table index type doesn't match %s definition", errtable, errtable);
138 :
139 8 : assert(m && n);
140 8 : ni = n->data;
141 8 : mi = m->data;
142 8 : if (ni->type != mi->type)
143 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table index type doesn't match %s definition", errtable, errtable);
144 8 : if (list_length(ni->columns) != list_length(mi->columns))
145 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table key type doesn't match %s definition", errtable, errtable);
146 24 : for (nn = ni->columns->h, mm = mi->columns->h; nn && mm; nn = nn->next, mm = mm->next) {
147 16 : sql_kc *nni = nn->data;
148 16 : sql_kc *mmi = mm->data;
149 :
150 16 : if (nni->c->colnr != mmi->c->colnr)
151 0 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table index's columns doesn't match %s definition", errtable, errtable);
152 : }
153 : }
154 :
155 562 : if (nested_mergetable(sql->session->tr, nt/*mergetable*/, nnt->s->base.name, nnt->base.name/*parts*/))
156 2 : throw(SQL,"sql.rel_check_tables",SQLSTATE(3F000) "ALTER %s: to be added table is a parent of the %s", errtable, errtable);
157 : return MAL_SUCCEED;
158 : }
159 :
160 : static char*
161 585 : validate_alter_table_add_table(mvc *sql, char* call, char *msname, char *mtname, char *psname, char *ptname,
162 : sql_table **mt, sql_table **pt, int update)
163 : {
164 585 : char *msg = MAL_SUCCEED;
165 585 : sql_schema *ms = NULL, *ps = NULL;
166 585 : sql_table *rmt = NULL, *rpt = NULL;
167 :
168 585 : if (!(ms = mvc_bind_schema(sql, msname)))
169 0 : throw(SQL,call,SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", msname);
170 585 : if (!(ps = mvc_bind_schema(sql, psname)))
171 0 : throw(SQL,call,SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", psname);
172 585 : if (!mvc_schema_privs(sql, ms))
173 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ms->base.name);
174 585 : if (!mvc_schema_privs(sql, ps))
175 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ps->base.name);
176 585 : if (!(rmt = mvc_bind_table(sql, ms, mtname)))
177 0 : throw(SQL,call,SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", mtname, ms->base.name);
178 585 : if (!(rpt = mvc_bind_table(sql, ps, ptname)))
179 0 : throw(SQL,call,SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", ptname, ps->base.name);
180 :
181 585 : const char *errtable = TABLE_TYPE_DESCRIPTION(rmt->type, rmt->properties);
182 585 : if (!update && (!isMergeTable(rmt) && !isReplicaTable(rmt)))
183 1 : throw(SQL,call,SQLSTATE(42S02) "ALTER TABLE: cannot add table '%s.%s' to %s '%s.%s'", psname, ptname, errtable, msname, mtname);
184 584 : if (isView(rpt))
185 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a view into a %s", errtable);
186 584 : if (isDeclaredTable(rpt))
187 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a declared table into a %s", errtable);
188 584 : if (isTempSchema(rpt->s))
189 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a temporary table into a %s", errtable);
190 584 : if (ms->base.id != ps->base.id)
191 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: all children tables of '%s.%s' must be part of schema '%s'", msname, mtname, msname);
192 584 : if (rmt->base.id == rpt->base.id)
193 1 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: a %s can't be a child of itself", errtable);
194 583 : node *n = members_find_child_id(rmt->members, rpt->base.id);
195 583 : if (n && !update)
196 7 : throw(SQL,call,SQLSTATE(42S02) "ALTER TABLE: table '%s.%s' is already part of %s '%s.%s'", psname, ptname, errtable, msname, mtname);
197 576 : if (!n && update)
198 2 : throw(SQL,call,SQLSTATE(42S02) "ALTER TABLE: table '%s.%s' isn't part of %s '%s.%s'", psname, ptname, errtable, msname, mtname);
199 574 : if ((msg = rel_check_tables(sql, rmt, rpt, errtable)) != MAL_SUCCEED)
200 : return msg;
201 :
202 560 : *mt = rmt;
203 560 : *pt = rpt;
204 560 : return MAL_SUCCEED;
205 : }
206 :
207 : static char *
208 312 : alter_table_add_table(mvc *sql, char *msname, char *mtname, char *psname, char *ptname)
209 : {
210 312 : sql_table *mt = NULL, *pt = NULL;
211 312 : str msg = validate_alter_table_add_table(sql, "sql.alter_table_add_table", msname, mtname, psname, ptname, &mt, &pt, 0);
212 :
213 312 : if (msg == MAL_SUCCEED) {
214 302 : if (isRangePartitionTable(mt))
215 0 : return createException(SQL, "sql.alter_table_add_table",SQLSTATE(42000) "ALTER TABLE: a range partition is required while adding under a range partition table");
216 302 : if (isListPartitionTable(mt))
217 0 : return createException(SQL, "sql.alter_table_add_table",SQLSTATE(42000) "ALTER TABLE: a value partition is required while adding under a list partition table");
218 302 : switch (sql_trans_add_table(sql->session->tr, mt, pt)) {
219 0 : case -1:
220 0 : return createException(SQL,"sql.alter_table_add_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
221 3 : case -2:
222 : case -3:
223 3 : return createException(SQL,"sql.alter_table_add_table",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
224 : default:
225 : break;
226 : }
227 : }
228 : return msg;
229 : }
230 :
231 :
232 : static char *
233 214 : alter_table_add_range_partition(mvc *sql, char *msname, char *mtname, char *psname, char *ptname, ptr min, ptr max,
234 : bit with_nills, int update, lng cnt)
235 : {
236 214 : sql_table *mt = NULL, *pt = NULL;
237 214 : sql_part *err = NULL;
238 214 : str msg = MAL_SUCCEED, err_min = NULL, err_max = NULL, conflict_err_min = NULL, conflict_err_max = NULL;
239 214 : int tp1 = 0, errcode = 0, min_null = 0, max_null = 0;
240 214 : size_t length = 0;
241 214 : sql_subtype tpe;
242 :
243 214 : if ((msg = validate_alter_table_add_table(sql, "sql.alter_table_add_range_partition", msname, mtname, psname, ptname, &mt, &pt, update))) {
244 : return msg;
245 200 : } else if (!isRangePartitionTable(mt)) {
246 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
247 : "ALTER TABLE: cannot add range partition into a %s table",
248 0 : (isListPartitionTable(mt))?"list partition":"merge");
249 0 : goto finish;
250 200 : } else if (!update && partition_find_part(sql->session->tr, pt, NULL)) {
251 1 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
252 : "ALTER TABLE: table '%s.%s' is already part of another table",
253 : psname, ptname);
254 1 : goto finish;
255 : }
256 :
257 199 : find_partition_type(&tpe, mt);
258 199 : tp1 = tpe.type->localtype;
259 199 : min_null = ATOMcmp(tp1, min, ATOMnilptr(tp1)) == 0;
260 199 : max_null = ATOMcmp(tp1, max, ATOMnilptr(tp1)) == 0;
261 :
262 199 : if (!min_null && !max_null && ATOMcmp(tp1, min, max) > 0) {
263 2 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000) "ALTER TABLE: minimum value is higher than maximum value");
264 2 : goto finish;
265 : }
266 197 : if (!min_null && !max_null && ATOMcmp(tp1, min, max) == 0) {
267 3 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000) "ALTER TABLE: minimum value is equal to the maximum value");
268 3 : goto finish;
269 : }
270 :
271 194 : if (cnt) {
272 18 : if (isPartitionedByColumnTable(mt)) {
273 17 : throw(SQL, "sql.alter_table_add_range_partition", SQLSTATE(M0M29) "ALTER TABLE: there are values in column %s outside the partition range", mt->part.pcol->base.name);
274 1 : } else if (isPartitionedByExpressionTable(mt)) {
275 1 : throw(SQL, "sql.alter_table_add_range_partition", SQLSTATE(M0M29) "ALTER TABLE: there are values in the expression outside the partition range");
276 : } else {
277 0 : assert(0);
278 : }
279 : }
280 :
281 176 : errcode = sql_trans_add_range_partition(sql->session->tr, mt, pt, tpe, min, max, with_nills, update, &err);
282 176 : switch (errcode) {
283 : case 0:
284 : break;
285 0 : case -1:
286 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
287 0 : break;
288 1 : case -2:
289 : case -3:
290 1 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
291 : "ALTER TABLE: failed due to conflict with another transaction");
292 1 : break;
293 0 : case -10:
294 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
295 : "ALTER TABLE: minimum value length is higher than %d", STORAGE_MAX_VALUE_LENGTH);
296 0 : break;
297 0 : case -11:
298 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
299 : "ALTER TABLE: maximum value length is higher than %d", STORAGE_MAX_VALUE_LENGTH);
300 0 : break;
301 38 : case -12:
302 38 : assert(err);
303 38 : if (is_bit_nil(err->with_nills)) {
304 4 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
305 4 : "ALTER TABLE: conflicting partitions: table %s.%s stores every possible value", err->t->s->base.name, err->base.name);
306 34 : } else if (with_nills && err->with_nills) {
307 6 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
308 : "ALTER TABLE: conflicting partitions: table %s.%s stores null values and only "
309 6 : "one partition can store null values at the time", err->t->s->base.name, err->base.name);
310 : } else {
311 28 : ssize_t (*atomtostr)(str *, size_t *, const void *, bool) = BATatoms[tp1].atomToStr;
312 28 : const void *nil = ATOMnilptr(tp1);
313 28 : sql_table *errt = mvc_bind_table(sql, mt->s, err->base.name);
314 :
315 28 : if (!errt) {
316 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
317 0 : "ALTER TABLE: cannot find partition table %s.%s", err->t->s->base.name, err->base.name);
318 0 : goto finish;
319 : }
320 28 : if (!ATOMcmp(tp1, nil, err->part.range.minvalue)) {
321 9 : if (!(conflict_err_min = GDKstrdup("absolute min value")))
322 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
323 19 : } else if (atomtostr(&conflict_err_min, &length, err->part.range.minvalue, true) < 0) {
324 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
325 : }
326 0 : if (msg)
327 0 : goto finish;
328 :
329 28 : if (!ATOMcmp(tp1, nil, err->part.range.maxvalue)) {
330 8 : if (!(conflict_err_max = GDKstrdup("absolute max value")))
331 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
332 20 : } else if (atomtostr(&conflict_err_max, &length, err->part.range.maxvalue, true) < 0) {
333 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
334 : }
335 0 : if (msg)
336 0 : goto finish;
337 :
338 28 : if (!ATOMcmp(tp1, nil, min)) {
339 5 : if (!(err_min = GDKstrdup("absolute min value")))
340 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
341 23 : } else if (atomtostr(&err_min, &length, min, true) < 0) {
342 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
343 : }
344 0 : if (msg)
345 0 : goto finish;
346 :
347 28 : if (!ATOMcmp(tp1, nil, max)) {
348 7 : if (!(err_max = GDKstrdup("absolute max value")))
349 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
350 21 : } else if (atomtostr(&err_max, &length, max, true) < 0) {
351 0 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
352 : }
353 0 : if (msg)
354 0 : goto finish;
355 :
356 28 : msg = createException(SQL,"sql.alter_table_add_range_partition",SQLSTATE(42000)
357 : "ALTER TABLE: conflicting partitions: %s to %s and %s to %s from table %s.%s",
358 28 : err_min, err_max, conflict_err_min, conflict_err_max, errt->s->base.name, errt->base.name);
359 : }
360 : break;
361 : default:
362 0 : assert(0);
363 : }
364 :
365 182 : finish:
366 182 : if (err_min)
367 28 : GDKfree(err_min);
368 182 : if (err_max)
369 28 : GDKfree(err_max);
370 182 : if (conflict_err_min)
371 28 : GDKfree(conflict_err_min);
372 182 : if (conflict_err_max)
373 28 : GDKfree(conflict_err_max);
374 : return msg;
375 : }
376 :
377 : static char *
378 59 : alter_table_add_value_partition(mvc *sql, MalStkPtr stk, InstrPtr pci, char *msname, char *mtname, char *psname,
379 : char *ptname, bit with_nills, int update, lng cnt)
380 : {
381 59 : sql_table *mt = NULL, *pt = NULL;
382 59 : str msg = MAL_SUCCEED;
383 59 : sql_part *err = NULL;
384 59 : int errcode = 0, i = 0, ninserts = 0;
385 59 : sql_subtype tpe;
386 59 : list *values = NULL;
387 :
388 59 : assert(with_nills == false || with_nills == true); /* No nills allowed here */
389 59 : if ((msg = validate_alter_table_add_table(sql, "sql.alter_table_add_value_partition", msname, mtname, psname, ptname,
390 : &mt, &pt, update))) {
391 : return msg;
392 58 : } else if (!isListPartitionTable(mt)) {
393 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
394 : "ALTER TABLE: cannot add value partition into a %s table",
395 0 : (isRangePartitionTable(mt))?"range partition":"merge");
396 0 : goto finish;
397 58 : } else if (!update && partition_find_part(sql->session->tr, pt, NULL)) {
398 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
399 : "ALTER TABLE: table '%s.%s' is already part of another table",
400 : psname, ptname);
401 0 : goto finish;
402 : }
403 :
404 58 : find_partition_type(&tpe, mt);
405 58 : ninserts = pci->argc - pci->retc - 7;
406 58 : if (ninserts <= 0 && !with_nills) {
407 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000) "ALTER TABLE: no values in the list");
408 0 : goto finish;
409 : }
410 :
411 58 : if (cnt) {
412 3 : if (isPartitionedByColumnTable(mt)) {
413 2 : throw(SQL, "sql.alter_table_add_value_partition", SQLSTATE(M0M29) "ALTER TABLE: there are values in column %s outside the partition list of values", mt->part.pcol->base.name);
414 1 : } else if (isPartitionedByExpressionTable(mt)) {
415 1 : throw(SQL, "sql.alter_table_add_value_partition", SQLSTATE(M0M29) "ALTER TABLE: there are values in the expression outside the partition list of values");
416 : } else {
417 0 : assert(0);
418 : }
419 : }
420 :
421 55 : values = list_create((fdestroy) &part_value_destroy);
422 219 : for ( i = pci->retc+7; i < pci->argc; i++){
423 165 : sql_part_value *nextv = NULL;
424 165 : ValRecord *vnext = &(stk)->stk[(pci)->argv[i]];
425 165 : ptr pnext = VALget(vnext);
426 165 : size_t len = ATOMlen(vnext->vtype, pnext);
427 :
428 165 : if (VALisnil(vnext)) { /* check for an eventual null value which cannot be */
429 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
430 : "ALTER TABLE: list value cannot be null");
431 0 : list_destroy2(values, sql->session->tr->store);
432 0 : goto finish;
433 : }
434 :
435 165 : nextv = ZNEW(sql_part_value); /* instantiate the part value */
436 165 : nextv->value = NEW_ARRAY(char, len);
437 165 : memcpy(nextv->value, pnext, len);
438 165 : nextv->length = len;
439 :
440 165 : if (list_append_sorted(values, nextv, &tpe, sql_values_list_element_validate_and_insert) != NULL) {
441 1 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
442 : "ALTER TABLE: there are duplicated values in the list");
443 1 : list_destroy2(values, sql->session->tr->store);
444 1 : _DELETE(nextv->value);
445 1 : _DELETE(nextv);
446 1 : goto finish;
447 : }
448 : }
449 :
450 54 : errcode = sql_trans_add_value_partition(sql->session->tr, mt, pt, tpe, values, with_nills, update, &err);
451 54 : if (errcode <= -10) {
452 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
453 : "ALTER TABLE: value at position %d length is higher than %d",
454 : (errcode * -1) - 10, STORAGE_MAX_VALUE_LENGTH);
455 : } else {
456 54 : switch (errcode) {
457 : case 0:
458 : break;
459 0 : case -1:
460 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(HY013) MAL_MALLOC_FAIL);
461 0 : break;
462 0 : case -2:
463 : case -3:
464 0 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
465 : "ALTER TABLE: failed due to conflict with another transaction");
466 0 : break;
467 5 : case -4:
468 5 : msg = createException(SQL,"sql.alter_table_add_value_partition",SQLSTATE(42000)
469 : "ALTER TABLE: the new partition is conflicting with the existing partition %s.%s",
470 5 : err->t->s->base.name, err->base.name);
471 5 : break;
472 : default:
473 0 : assert(0);
474 : }
475 : }
476 :
477 : finish:
478 : return msg;
479 : }
480 :
481 : static char *
482 180 : alter_table_del_table(mvc *sql, char *msname, char *mtname, char *psname, char *ptname, int drop_action)
483 : {
484 180 : sql_schema *ms = NULL, *ps = NULL;
485 180 : sql_table *mt = NULL, *pt = NULL;
486 180 : node *n = NULL;
487 :
488 180 : if (!(ms = mvc_bind_schema(sql, msname)))
489 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", msname);
490 180 : if (!(ps = mvc_bind_schema(sql, psname)))
491 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", psname);
492 180 : if (!mvc_schema_privs(sql, ms))
493 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ms->base.name);
494 180 : if (!mvc_schema_privs(sql, ps))
495 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ps->base.name);
496 180 : if (!(mt = mvc_bind_table(sql, ms, mtname)))
497 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", mtname, ms->base.name);
498 180 : if (!(pt = mvc_bind_table(sql, ps, ptname)))
499 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", ptname, ps->base.name);
500 180 : const char *errtable = TABLE_TYPE_DESCRIPTION(mt->type, mt->properties);
501 180 : if (!isMergeTable(mt) && !isReplicaTable(mt))
502 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42S02) "ALTER TABLE: cannot drop table '%s.%s' to %s '%s.%s'", psname, ptname, errtable, msname, mtname);
503 180 : if (!(n = members_find_child_id(mt->members, pt->base.id)))
504 10 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42S02) "ALTER TABLE: table '%s.%s' isn't part of %s '%s.%s'", ps->base.name, ptname, errtable, ms->base.name, mtname);
505 :
506 170 : switch (sql_trans_del_table(sql->session->tr, mt, pt, drop_action)) {
507 0 : case -1:
508 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
509 0 : case -2:
510 : case -3:
511 0 : throw(SQL,"sql.alter_table_del_table",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
512 : default:
513 : break;
514 : }
515 : return MAL_SUCCEED;
516 : }
517 :
518 : static char *
519 2310 : alter_table_set_access(mvc *sql, char *sname, char *tname, int access)
520 : {
521 2310 : sql_schema *s = NULL;
522 2310 : sql_table *t = NULL;
523 :
524 2310 : if (!(s = mvc_bind_schema(sql, sname)))
525 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", sname);
526 2310 : if (s && !mvc_schema_privs(sql, s))
527 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
528 2310 : if (!(t = mvc_bind_table(sql, s, tname)))
529 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", tname, s->base.name);
530 2310 : if (!isTable(t))
531 1 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(42000) "ALTER TABLE: access changes on %sS not supported", TABLE_TYPE_DESCRIPTION(t->type, t->properties));
532 2309 : if (t->access != access) {
533 2309 : if (access && table_has_updates(sql->session->tr, t))
534 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(40000) "ALTER TABLE: set READ or INSERT ONLY not possible with outstanding updates (wait until updates are flushed)\n");
535 :
536 2309 : switch (mvc_access(sql, t, access)) {
537 0 : case -1:
538 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(HY013) MAL_MALLOC_FAIL);
539 0 : case -2:
540 : case -3:
541 0 : throw(SQL,"sql.alter_table_set_access",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
542 : default:
543 : break;
544 : }
545 : }
546 : return MAL_SUCCEED;
547 : }
548 :
549 : static char *
550 345 : create_trigger(mvc *sql, char *sname, char *tname, char *triggername, int time, int orientation, int event, char *old_name, char *new_name, char *condition, char *query, int replace)
551 : {
552 345 : sql_trigger *tri = NULL, *other = NULL;
553 345 : sql_schema *s = NULL;
554 345 : sql_table *t = NULL;
555 345 : const char *base = replace ? "CREATE OR REPLACE TRIGGER" : "CREATE TRIGGER";
556 :
557 1031 : if (!strNil(sname) && !strNil(tname)) {
558 341 : if (!(s = mvc_bind_schema(sql, sname)))
559 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
560 341 : if (!mvc_schema_privs(sql, s))
561 0 : throw(SQL,"sql.create_trigger",SQLSTATE(42000) "%s: access denied for %s to schema '%s'", base, get_string_global_var(sql, "current_user"), s->base.name);
562 341 : if (!(t = mvc_bind_table(sql, s, tname)))
563 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: unknown table '%s'", base, tname);
564 341 : if (isView(t))
565 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: cannot create trigger on view '%s'", base, tname);
566 : } else {
567 4 : if (!(s = mvc_bind_schema(sql, "sys")))
568 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
569 : }
570 :
571 345 : if ((other = mvc_bind_trigger(sql, s, triggername)) && !replace)
572 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: name '%s' already in use", base, triggername);
573 :
574 345 : if (replace && other) {
575 5 : if (other->t->base.id != t->base.id) /* defensive line */
576 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: the to be replaced trigger '%s' is not from table '%s'", base, triggername, tname);
577 5 : switch (mvc_drop_trigger(sql, s, other)) {
578 0 : case -1:
579 0 : throw(SQL,"sql.create_trigger", SQLSTATE(HY013) MAL_MALLOC_FAIL);
580 2 : case -2:
581 : case -3:
582 2 : throw(SQL,"sql.create_trigger", SQLSTATE(42000) "%s: transaction conflict detected", base);
583 : default:
584 : break;
585 : }
586 : }
587 343 : switch (mvc_create_trigger(&tri, sql, t, triggername, time, orientation, event, old_name, new_name, condition, query)) {
588 0 : case -1:
589 0 : throw(SQL,"sql.create_trigger", SQLSTATE(HY013) MAL_MALLOC_FAIL);
590 0 : case -2:
591 : case -3:
592 0 : throw(SQL,"sql.create_trigger", SQLSTATE(42000) "%s: transaction conflict detected", base);
593 343 : default: {
594 343 : char *buf;
595 343 : sql_rel *r = NULL;
596 343 : allocator *sa = sql->sa;
597 :
598 343 : if (!(sql->sa = sa_create(sql->pa))) {
599 0 : sql->sa = sa;
600 0 : throw(SQL, "sql.create_trigger", SQLSTATE(HY013) MAL_MALLOC_FAIL);
601 : }
602 343 : if (!(buf = sa_strdup(sql->sa, query))) {
603 0 : sa_destroy(sql->sa);
604 0 : sql->sa = sa;
605 0 : throw(SQL, "sql.create_trigger", SQLSTATE(HY013) MAL_MALLOC_FAIL);
606 : }
607 343 : r = rel_parse(sql, s, buf, m_deps);
608 343 : if (r)
609 338 : r = sql_processrelation(sql, r, 0, 0, 0, 0);
610 338 : if (r) {
611 338 : list *blist = rel_dependencies(sql, r);
612 338 : if (mvc_create_dependencies(sql, blist, tri->base.id, TRIGGER_DEPENDENCY)) {
613 0 : sa_destroy(sql->sa);
614 0 : sql->sa = sa;
615 0 : throw(SQL, "sql.create_trigger", SQLSTATE(HY013) MAL_MALLOC_FAIL);
616 : }
617 : }
618 343 : sa_destroy(sql->sa);
619 343 : sql->sa = sa;
620 343 : if (!r) {
621 5 : if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
622 5 : throw(SQL, "sql.create_trigger", "%s", sql->errstr);
623 : else
624 0 : throw(SQL, "sql.create_trigger", SQLSTATE(42000) "%s", sql->errstr);
625 : }
626 : }
627 : }
628 : return MAL_SUCCEED;
629 : }
630 :
631 : static char *
632 84 : drop_trigger(mvc *sql, char *sname, char *tname, int if_exists)
633 : {
634 84 : sql_trigger *tri = NULL;
635 84 : sql_schema *s = NULL;
636 :
637 168 : if (!strNil(sname) && !(s = mvc_bind_schema(sql, sname))) {
638 0 : if (if_exists)
639 : return MAL_SUCCEED;
640 0 : throw(SQL,"sql.drop_trigger",SQLSTATE(3F000) "DROP TRIGGER: no such schema '%s'", sname);
641 : }
642 3 : if (!s)
643 3 : s = mvc_bind_schema(sql, "sys");
644 84 : if (s && !mvc_schema_privs(sql, s))
645 0 : throw(SQL,"sql.drop_trigger",SQLSTATE(42000) "DROP TRIGGER: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
646 :
647 84 : if ((tri = mvc_bind_trigger(sql, s, tname)) == NULL) {
648 0 : if (if_exists)
649 : return MAL_SUCCEED;
650 0 : throw(SQL,"sql.drop_trigger", SQLSTATE(3F000) "DROP TRIGGER: unknown trigger %s\n", tname);
651 : }
652 84 : switch (mvc_drop_trigger(sql, s, tri)) {
653 0 : case -1:
654 0 : throw(SQL,"sql.drop_trigger",SQLSTATE(HY013) MAL_MALLOC_FAIL);
655 1 : case -2:
656 : case -3:
657 1 : throw(SQL,"sql.drop_trigger",SQLSTATE(42000) "DROP TRIGGER: transaction conflict detected");
658 : default:
659 : break;
660 : }
661 : return MAL_SUCCEED;
662 : }
663 :
664 : char *
665 3673 : drop_table(mvc *sql, char *sname, char *tname, int drop_action, int if_exists)
666 : {
667 3673 : sql_schema *s = NULL;
668 3673 : sql_table *t = NULL;
669 :
670 3673 : if (!(s = mvc_bind_schema(sql, sname))) {
671 0 : if (if_exists)
672 : return MAL_SUCCEED;
673 0 : throw(SQL,"sql.drop_table",SQLSTATE(3F000) "DROP TABLE: no such schema '%s'", sname);
674 : }
675 3673 : if (!(t = mvc_bind_table(sql, s, tname))) {
676 0 : if (if_exists)
677 : return MAL_SUCCEED;
678 0 : throw(SQL,"sql.drop_table", SQLSTATE(42S02) "DROP TABLE: no such table '%s'", tname);
679 : }
680 3673 : if (isView(t))
681 1 : throw(SQL,"sql.drop_table", SQLSTATE(42000) "DROP TABLE: cannot drop VIEW '%s'", tname);
682 3672 : if (t->system)
683 1 : throw(SQL,"sql.drop_table", SQLSTATE(42000) "DROP TABLE: cannot drop system table '%s'", tname);
684 3671 : if (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && t->persistence == SQL_LOCAL_TEMP))
685 2 : throw(SQL,"sql.drop_table", SQLSTATE(42000) "DROP TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
686 :
687 3669 : if (!drop_action && t->keys) {
688 4466 : for (node *n = ol_first_node(t->keys); n; n = n->next) {
689 1252 : sql_key *k = n->data;
690 :
691 1252 : if (k->type == ukey || k->type == pkey) {
692 892 : struct os_iter oi;
693 892 : os_iterator(&oi, k->t->s->keys, sql->session->tr, NULL);
694 22993 : for (sql_base *b = oi_next(&oi); b; b=oi_next(&oi)) {
695 22103 : sql_key *fk = (sql_key*)b;
696 22103 : sql_fkey *rk = (sql_fkey*)b;
697 :
698 22103 : if (fk->type != fkey || rk->rkey != k->base.id)
699 22084 : continue;
700 :
701 : /* make sure it is not a self referencing key */
702 19 : if (fk->t != t)
703 2 : throw(SQL,"sql.drop_table", SQLSTATE(40000) "DROP TABLE: FOREIGN KEY %s.%s depends on %s", k->t->base.name, k->base.name, tname);
704 : }
705 : }
706 : }
707 : }
708 :
709 3214 : if (!drop_action && mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
710 17 : throw (SQL,"sql.drop_table",SQLSTATE(42000) "DROP TABLE: unable to drop table %s (there are database objects which depend on it)\n", t->base.name);
711 :
712 3650 : return mvc_drop_table(sql, s, t, drop_action);
713 : }
714 :
715 : char *
716 314 : drop_view(mvc *sql, char *sname, char *tname, int drop_action, int if_exists)
717 : {
718 314 : sql_table *t = NULL;
719 314 : sql_schema *ss = NULL;
720 :
721 314 : if (!(ss = mvc_bind_schema(sql, sname))) {
722 0 : if (if_exists)
723 : return MAL_SUCCEED;
724 0 : throw(SQL,"sql.drop_view", SQLSTATE(3F000) "DROP VIEW: no such schema '%s'", sname);
725 : }
726 314 : if (!(t = mvc_bind_table(sql, ss, tname))) {
727 0 : if (if_exists)
728 : return MAL_SUCCEED;
729 0 : throw(SQL,"sql.drop_view",SQLSTATE(42S02) "DROP VIEW: unknown view '%s'", tname);
730 : }
731 314 : if (!mvc_schema_privs(sql, ss) && !(isTempSchema(ss) && t && t->persistence == SQL_LOCAL_TEMP))
732 0 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), ss->base.name);
733 314 : if (!isView(t))
734 0 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: unable to drop view '%s': is a table", tname);
735 314 : if (t->system)
736 1 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: cannot drop system view '%s'", tname);
737 313 : if (!drop_action && mvc_check_dependency(sql, t->base.id, VIEW_DEPENDENCY, NULL))
738 3 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: cannot drop view '%s', there are database objects which depend on it", t->base.name);
739 310 : return mvc_drop_table(sql, ss, t, drop_action);
740 : }
741 :
742 : static str
743 150 : drop_key(mvc *sql, char *sname, char *tname, char *kname, int drop_action)
744 : {
745 150 : node *n;
746 150 : sql_schema *s = cur_schema(sql);
747 150 : sql_table *t = NULL;
748 150 : sql_key *key;
749 :
750 150 : if (!(s = mvc_bind_schema(sql, sname)))
751 0 : throw(SQL,"sql.drop_key", SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", sname);
752 150 : if (!mvc_schema_privs(sql, s))
753 0 : throw(SQL,"sql.drop_key", SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
754 150 : if (!(t = mvc_bind_table(sql, s, tname)))
755 0 : throw(SQL,"sql.drop_key", SQLSTATE(42S02) "ALTER TABLE: no such table '%s'", tname);
756 150 : if (!(n = ol_find_name(t->keys, kname)))
757 3 : throw(SQL,"sql.drop_key", SQLSTATE(42000) "ALTER TABLE: no such constraint '%s'", kname);
758 147 : key = n->data;
759 147 : if (!drop_action && mvc_check_dependency(sql, key->base.id, KEY_DEPENDENCY, NULL))
760 1 : throw(SQL,"sql.drop_key", SQLSTATE(42000) "ALTER TABLE: cannot drop constraint '%s': there are database objects which depend on it", key->base.name);
761 146 : switch (mvc_drop_key(sql, s, key, drop_action)) {
762 0 : case -1:
763 0 : throw(SQL,"sql.drop_key",SQLSTATE(HY013) MAL_MALLOC_FAIL);
764 0 : case -2:
765 : case -3:
766 0 : throw(SQL,"sql.drop_key",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
767 : default:
768 : break;
769 : }
770 : return MAL_SUCCEED;
771 : }
772 :
773 : static str
774 109 : IDXdrop(mvc *sql, const char *sname, const char *tname, const char *iname, void (*func)(BAT *))
775 : {
776 109 : BAT *b = mvc_bind(sql, sname, tname, iname, RDONLY), *nb = NULL;
777 :
778 109 : if (!b)
779 0 : throw(SQL,"sql.drop_index", SQLSTATE(HY005) "Column can not be accessed");
780 109 : if (VIEWtparent(b)) {
781 109 : nb = BBP_desc(VIEWtparent(b));
782 109 : BBPunfix(b->batCacheid);
783 109 : if (!(b = BATdescriptor(nb->batCacheid)))
784 0 : throw(SQL,"sql.drop_index", SQLSTATE(HY005) "Column can not be accessed");
785 : }
786 :
787 109 : func(b);
788 109 : BBPunfix(b->batCacheid);
789 109 : return MAL_SUCCEED;
790 : }
791 :
792 : static str
793 160 : drop_index(mvc *sql, char *sname, char *iname)
794 : {
795 160 : sql_schema *s = NULL;
796 160 : sql_idx *i = NULL;
797 160 : str msg = MAL_SUCCEED;
798 :
799 160 : if (!(s = mvc_bind_schema(sql, sname)))
800 0 : throw(SQL,"sql.drop_index", SQLSTATE(3F000) "DROP INDEX: no such schema '%s'", sname);
801 160 : if (!mvc_schema_privs(sql, s))
802 0 : throw(SQL,"sql.drop_index", SQLSTATE(42000) "DROP INDEX: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
803 160 : if (!(i = mvc_bind_idx(sql, s, iname)))
804 0 : throw(SQL,"sql.drop_index", SQLSTATE(42S12) "DROP INDEX: no such index '%s'", iname);
805 160 : if (i->key)
806 1 : throw(SQL,"sql.drop_index", SQLSTATE(42S12) "DROP INDEX: cannot drop index '%s', because the constraint '%s' depends on it", iname, i->key->base.name);
807 159 : if (i->type == ordered_idx || i->type == imprints_idx) {
808 109 : sql_kc *ic = i->columns->h->data;
809 109 : sql_class icls = ic->c->type.type->eclass;
810 151 : if ((msg = IDXdrop(sql, s->base.name, ic->c->t->base.name, ic->c->base.name, i->type == ordered_idx ? OIDXdestroy : (icls == EC_STRING ? STRMPdestroy : IMPSdestroy))))
811 : return msg;
812 : }
813 159 : switch (mvc_drop_idx(sql, s, i)) {
814 0 : case -1:
815 0 : throw(SQL,"sql.drop_index",SQLSTATE(HY013) MAL_MALLOC_FAIL);
816 0 : case -2:
817 : case -3:
818 0 : throw(SQL,"sql.drop_index",SQLSTATE(42000) "DROP INDEX: transaction conflict detected");
819 : default:
820 : break;
821 : }
822 : return NULL;
823 : }
824 :
825 : static str
826 313 : create_seq(mvc *sql, char *sname, char *seqname, sql_sequence *seq)
827 : {
828 313 : sql_schema *s = NULL;
829 :
830 313 : (void)seqname;
831 313 : if (!(s = mvc_bind_schema(sql, sname)))
832 0 : throw(SQL,"sql.create_seq", SQLSTATE(3F000) "CREATE SEQUENCE: no such schema '%s'", sname);
833 313 : if (!mvc_schema_privs(sql, s))
834 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: insufficient privileges for '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
835 313 : if (find_sql_sequence(sql->session->tr, s, seq->base.name))
836 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: name '%s' already in use", seq->base.name);
837 313 : if (is_lng_nil(seq->start) || is_lng_nil(seq->minvalue) || is_lng_nil(seq->maxvalue) ||
838 313 : is_lng_nil(seq->increment) || is_lng_nil(seq->cacheinc) || is_bit_nil(seq->cycle))
839 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence properties must be non-NULL");
840 313 : if (seq->start < seq->minvalue)
841 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: start value is less than the minimum ("LLFMT" < "LLFMT")", seq->start, seq->minvalue);
842 313 : if (seq->start > seq->maxvalue)
843 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: start value is higher than the maximum ("LLFMT" > "LLFMT")", seq->start, seq->maxvalue);
844 313 : if (seq->maxvalue < seq->minvalue)
845 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: maximum value is less than the minimum ("LLFMT" < "LLFMT")", seq->maxvalue, seq->minvalue);
846 313 : if (seq->increment == 0)
847 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence increment cannot be 0");
848 313 : if (seq->cacheinc <= 0)
849 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence cache must be positive");
850 313 : lng calc = llabs(seq->increment) * seq->cacheinc;
851 313 : if (calc < llabs(seq->increment) || calc < seq->cacheinc)
852 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: The specified range of cached values cannot be set. Either reduce increment or cache value");
853 313 : switch (sql_trans_create_sequence(sql->session->tr, s, seq->base.name, seq->start, seq->minvalue, seq->maxvalue, seq->increment, seq->cacheinc, seq->cycle, seq->bedropped)) {
854 0 : case -1:
855 0 : throw(SQL,"sql.create_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
856 0 : case -2:
857 : case -3:
858 0 : throw(SQL,"sql.create_seq",SQLSTATE(42000) "CREATE SEQUENCE: transaction conflict detected");
859 : default:
860 : break;
861 : }
862 : return NULL;
863 : }
864 :
865 : static str
866 45 : alter_seq(mvc *sql, char *sname, char *seqname, sql_sequence *seq, const lng *val)
867 : {
868 45 : sql_schema *s = NULL;
869 45 : sql_sequence *nseq = NULL;
870 :
871 45 : (void)seqname;
872 45 : if (!(s = mvc_bind_schema(sql, sname)))
873 0 : throw(SQL,"sql.alter_seq", SQLSTATE(3F000) "ALTER SEQUENCE: no such schema '%s'", sname);
874 45 : if (!mvc_schema_privs(sql, s))
875 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: insufficient privileges for '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
876 45 : if (!(nseq = find_sql_sequence(sql->session->tr, s, seq->base.name)))
877 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: no such sequence '%s'", seq->base.name);
878 : /* if seq properties hold NULL values, then they should be ignored during the update */
879 : /* first alter the known values */
880 45 : switch (sql_trans_alter_sequence(sql->session->tr, nseq, seq->minvalue, seq->maxvalue, seq->increment, seq->cacheinc, seq->cycle)) {
881 0 : case -1:
882 0 : throw(SQL,"sql.alter_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
883 0 : case -2:
884 : case -3:
885 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: transaction conflict detected");
886 : default:
887 45 : break;
888 : }
889 45 : if (nseq->maxvalue < nseq->minvalue)
890 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: maximum value is less than the minimum ("LLFMT" < "LLFMT")", nseq->maxvalue, nseq->minvalue);
891 45 : if (nseq->increment == 0)
892 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence increment cannot be 0");
893 45 : if (nseq->cacheinc <= 0)
894 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence cache must be positive");
895 45 : lng calc = llabs(nseq->increment) * nseq->cacheinc;
896 45 : if (calc < llabs(nseq->increment) || calc < nseq->cacheinc)
897 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: The specified range of cached values cannot be set. Either reduce increment or cache value");
898 45 : if (val) {
899 45 : if (is_lng_nil(*val))
900 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence value must be non-NULL");
901 45 : if (*val < nseq->minvalue)
902 1 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: cannot set sequence start to a value less than the minimum ("LLFMT" < "LLFMT")", *val, nseq->minvalue);
903 44 : if (*val > nseq->maxvalue)
904 1 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: cannot set sequence start to a value higher than the maximum ("LLFMT" > "LLFMT")", *val, nseq->maxvalue);
905 43 : switch (sql_trans_sequence_restart(sql->session->tr, nseq, *val)) {
906 0 : case -1:
907 0 : throw(SQL,"sql.alter_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
908 0 : case -2:
909 : case -3:
910 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: transaction conflict detected");
911 0 : case -4:
912 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: failed to restart sequence %s.%s", sname, nseq->base.name);
913 : default:
914 : break;
915 : }
916 : }
917 : return MAL_SUCCEED;
918 : }
919 :
920 : static str
921 32 : drop_seq(mvc *sql, char *sname, char *name)
922 : {
923 32 : sql_schema *s = NULL;
924 32 : sql_sequence *seq = NULL;
925 :
926 32 : if (!(s = mvc_bind_schema(sql, sname)))
927 0 : throw(SQL,"sql.drop_seq", SQLSTATE(3F000) "DROP SEQUENCE: no such schema '%s'", sname);
928 32 : if (!mvc_schema_privs(sql, s))
929 0 : throw(SQL,"sql.drop_seq", SQLSTATE(42000) "DROP SEQUENCE: insufficient privileges for '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
930 32 : if (!(seq = find_sql_sequence(sql->session->tr, s, name)))
931 0 : throw(SQL,"sql.drop_seq", SQLSTATE(42M35) "DROP SEQUENCE: no such sequence '%s'", name);
932 32 : if (mvc_check_dependency(sql, seq->base.id, BEDROPPED_DEPENDENCY, NULL))
933 2 : throw(SQL,"sql.drop_seq", SQLSTATE(2B000) "DROP SEQUENCE: unable to drop sequence %s (there are database objects which depend on it)\n", seq->base.name);
934 :
935 30 : switch (sql_trans_drop_sequence(sql->session->tr, s, seq, 0)) {
936 0 : case -1:
937 0 : throw(SQL,"sql.drop_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
938 0 : case -2:
939 : case -3:
940 0 : throw(SQL,"sql.drop_seq",SQLSTATE(42000) "DROP SEQUENCE: transaction conflict detected");
941 : default:
942 : break;
943 : }
944 : return NULL;
945 : }
946 :
947 : static str
948 668 : drop_func(mvc *sql, char *sname, char *name, sqlid fid, sql_ftype type, int action)
949 : {
950 668 : sql_schema *s = NULL;
951 668 : char *F = NULL, *fn = NULL;
952 668 : int res = 0;
953 :
954 668 : FUNC_TYPE_STR(type, F, fn)
955 :
956 668 : if (!(s = mvc_bind_schema(sql, sname))) {
957 0 : if (fid == -2) /* if exists option */
958 : return MAL_SUCCEED;
959 0 : throw(SQL,"sql.drop_func", SQLSTATE(3F000) "DROP %s: no such schema '%s'", F, sname);
960 : }
961 668 : if (!mvc_schema_privs(sql, s))
962 0 : throw(SQL,"sql.drop_func", SQLSTATE(42000) "DROP %s: access denied for %s to schema '%s'", F, get_string_global_var(sql, "current_user"), s->base.name);
963 668 : if (fid >= 0) {
964 638 : sql_base *b = os_find_id(s->funcs, sql->session->tr, fid);
965 638 : if (b) {
966 638 : sql_func *func = (sql_func*)b;
967 :
968 692 : if (!action && mvc_check_dependency(sql, func->base.id, !IS_PROC(func) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, NULL))
969 4 : throw(SQL,"sql.drop_func", SQLSTATE(42000) "DROP %s: there are database objects dependent on %s %s;", F, fn, func->base.name);
970 634 : res = mvc_drop_func(sql, s, func, action);
971 : }
972 30 : } else if (fid == -2) { /* if exists option */
973 : return MAL_SUCCEED;
974 : } else { /* fid == -1 */
975 30 : list *list_func = sql_find_funcs_by_name(sql, s->base.name, name, type, false);
976 :
977 30 : if (!list_empty(list_func))
978 72 : for (node *n = list_func->h; n; n = n->next) {
979 42 : sql_func *func = n->data;
980 :
981 55 : if (!action && mvc_check_dependency(sql, func->base.id, !IS_PROC(func) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, list_func)) {
982 0 : list_destroy(list_func);
983 0 : throw(SQL,"sql.drop_func", SQLSTATE(42000) "DROP %s: there are database objects dependent on %s %s;", F, fn, func->base.name);
984 : }
985 : }
986 30 : res = mvc_drop_all_func(sql, s, list_func, action);
987 30 : list_destroy(list_func);
988 : }
989 :
990 664 : switch (res) {
991 0 : case -1:
992 0 : throw(SQL,"sql.drop_func",SQLSTATE(HY013) MAL_MALLOC_FAIL);
993 0 : case -2:
994 : case -3:
995 0 : throw(SQL,"sql.drop_func",SQLSTATE(42000) "DROP %s: transaction conflict detected", F);
996 : default:
997 : break;
998 : }
999 : return MAL_SUCCEED;
1000 : }
1001 :
1002 : static int
1003 6 : args_cmp(sql_arg *a1, sql_arg *a2)
1004 : {
1005 6 : if (a1->inout != a2->inout)
1006 : return -1;
1007 6 : if (strcmp(a1->name, a2->name) != 0)
1008 : return -1;
1009 6 : return subtype_cmp(&a1->type, &a2->type);
1010 : }
1011 :
1012 : static char *
1013 120295 : create_func(mvc *sql, char *sname, char *fname, sql_func *f, int replace)
1014 : {
1015 120295 : sql_func *nf;
1016 120295 : sql_subfunc *sf;
1017 120295 : sql_schema *s = NULL;
1018 120295 : char *F = NULL, *fn = NULL, *base = replace ? "CREATE OR REPLACE" : "CREATE";
1019 :
1020 120295 : FUNC_TYPE_STR(f->type, F, fn)
1021 :
1022 120295 : (void) fn;
1023 120295 : if (!(s = mvc_bind_schema(sql, sname)))
1024 0 : throw(SQL,"sql.create_func", SQLSTATE(3F000) "%s %s: no such schema '%s'", base, F, sname);
1025 120295 : if (!mvc_schema_privs(sql, s))
1026 0 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: access denied for %s to schema '%s'", base, F, get_string_global_var(sql, "current_user"), s->base.name);
1027 :
1028 120295 : if (replace) {
1029 67 : list *tl = sa_list(sql->sa);
1030 67 : if (!list_empty(f->ops)) {
1031 79 : for (node *n = f->ops->h ; n ; n = n->next ) {
1032 45 : sql_arg *arg = n->data;
1033 :
1034 45 : list_append(tl, &arg->type);
1035 : }
1036 : }
1037 :
1038 67 : if ((sf = sql_bind_func_(sql, s->base.name, fname, tl, f->type, false, true)) != NULL) {
1039 15 : sql_func *sff = sf->func;
1040 :
1041 15 : if (!sff->s || sff->system)
1042 0 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: not allowed to replace system %s %s;", base, F, fn, sff->base.name);
1043 :
1044 : /* if all function parameters are the same, return */
1045 15 : if (sff->lang == f->lang && sff->type == f->type &&
1046 15 : sff->varres == f->varres && sff->vararg == f->vararg &&
1047 18 : ((!sff->query && !f->query) || (sff->query && f->query && strcmp(sff->query, f->query) == 0)) &&
1048 6 : list_cmp(sff->res, f->res, (fcmp) &args_cmp) == 0 &&
1049 3 : list_cmp(sff->ops, f->ops, (fcmp) &args_cmp) == 0)
1050 : return MAL_SUCCEED;
1051 :
1052 12 : if (mvc_check_dependency(sql, sff->base.id, !IS_PROC(sff) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, NULL))
1053 0 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: there are database objects dependent on %s %s;", base, F, fn, sff->base.name);
1054 12 : switch (mvc_drop_func(sql, s, sff, 0)) {
1055 0 : case -1:
1056 0 : throw(SQL,"sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1057 1 : case -2:
1058 : case -3:
1059 1 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: transaction conflict detected", base, F);
1060 : default:
1061 : break;
1062 : }
1063 : } else {
1064 52 : sql->session->status = 0; /* if the function was not found clean the error */
1065 52 : sql->errstr[0] = '\0';
1066 : }
1067 : }
1068 120291 : switch (mvc_create_func(&nf, sql, NULL, s, f->base.name, f->ops, f->res, f->type, f->lang, f->mod, f->imp, f->query, f->varres, f->vararg, f->system, f->side_effect)) {
1069 0 : case -1:
1070 0 : throw(SQL,"sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1071 1 : case -2:
1072 : case -3:
1073 1 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: transaction conflict detected", base, F);
1074 : default:
1075 120290 : break;
1076 : }
1077 120290 : switch (nf->lang) {
1078 112459 : case FUNC_LANG_MAL:
1079 112459 : assert(nf->imp);
1080 112459 : nf->instantiated = TRUE; /* MAL functions get instantiated while being created */
1081 : /* fall through */
1082 120061 : case FUNC_LANG_SQL: {
1083 120061 : char *buf;
1084 120061 : sql_rel *r = NULL;
1085 120061 : allocator *sa = sql->sa;
1086 :
1087 120061 : assert(nf->query);
1088 120061 : if (!(sql->sa = sa_create(sql->pa))) {
1089 0 : sql->sa = sa;
1090 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1091 : }
1092 120061 : if (!(buf = sa_strdup(sql->sa, nf->query))) {
1093 0 : sa_destroy(sql->sa);
1094 0 : sql->sa = sa;
1095 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1096 : }
1097 120061 : r = rel_parse(sql, s, buf, m_deps);
1098 120061 : if (r)
1099 120061 : r = sql_processrelation(sql, r, 0, 0, 0, 0);
1100 120061 : if (r) {
1101 120061 : node *n;
1102 120061 : list *blist = rel_dependencies(sql, r);
1103 :
1104 120061 : if (!f->vararg && f->ops) {
1105 308405 : for (n = f->ops->h; n; n = n->next) {
1106 188344 : sql_arg *a = n->data;
1107 :
1108 188344 : if (a->type.type->s && mvc_create_dependency(sql, &a->type.type->base, nf->base.id, TYPE_DEPENDENCY)) {
1109 0 : sa_destroy(sql->sa);
1110 0 : sql->sa = sa;
1111 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1112 : }
1113 : }
1114 : }
1115 120061 : if (!f->varres && f->res) {
1116 258398 : for (n = f->res->h; n; n = n->next) {
1117 156620 : sql_arg *a = n->data;
1118 :
1119 156620 : if (a->type.type->s && mvc_create_dependency(sql, &a->type.type->base, nf->base.id, TYPE_DEPENDENCY)) {
1120 0 : sa_destroy(sql->sa);
1121 0 : sql->sa = sa;
1122 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1123 : }
1124 : }
1125 : }
1126 134981 : if (mvc_create_dependencies(sql, blist, nf->base.id, !IS_PROC(f) ? FUNC_DEPENDENCY : PROC_DEPENDENCY)) {
1127 0 : sa_destroy(sql->sa);
1128 0 : sql->sa = sa;
1129 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1130 : }
1131 : }
1132 120061 : sa_destroy(sql->sa);
1133 120061 : sql->sa = sa;
1134 120061 : if (!r) {
1135 0 : if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
1136 0 : throw(SQL, "sql.create_func", "%s", sql->errstr);
1137 : else
1138 0 : throw(SQL, "sql.create_func", SQLSTATE(42000) "%s", sql->errstr);
1139 : }
1140 : }
1141 : default:
1142 : break;
1143 : }
1144 : return MAL_SUCCEED;
1145 : }
1146 :
1147 : str
1148 1369 : alter_table(Client cntxt, mvc *sql, char *sname, sql_table *t)
1149 : {
1150 1369 : sql_schema *s = NULL;
1151 1369 : sql_table *nt = NULL;
1152 1369 : node *n;
1153 :
1154 1369 : if (!(s = mvc_bind_schema(sql, sname)))
1155 0 : throw(SQL,"sql.alter_table",
1156 : SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", sname);
1157 :
1158 1369 : if (!mvc_schema_privs(sql, s) &&
1159 0 : !(isTempSchema(s) && t->persistence == SQL_LOCAL_TEMP))
1160 0 : throw(SQL,"sql.alter_table",
1161 : SQLSTATE(42000) "ALTER TABLE: insufficient privileges for"
1162 : " user '%s' in schema '%s'",
1163 : get_string_global_var(sql, "current_user"), s->base.name);
1164 :
1165 1369 : if (!(nt = mvc_bind_table(sql, s, t->base.name)))
1166 0 : throw(SQL,"sql.alter_table",
1167 : SQLSTATE(42S02) "ALTER TABLE: no such table '%s'", t->base.name);
1168 :
1169 1369 : sql_table *gt = NULL;
1170 1369 : if (nt && isTempTable(nt)) {
1171 34 : gt = (sql_table*)os_find_id(s->tables, sql->session->tr, nt->base.id);
1172 34 : if (gt)
1173 1369 : nt = gt;
1174 : }
1175 :
1176 : /* First check if all the changes are allowed */
1177 1369 : if (t->idxs) {
1178 : /* only one pkey */
1179 1369 : if (nt->pkey) {
1180 1423 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1181 696 : sql_idx *i = n->data;
1182 696 : if (!i->base.new || i->base.deleted)
1183 0 : continue;
1184 696 : if (i->key && i->key->type == pkey)
1185 3 : throw(SQL,"sql.alter_table",
1186 : SQLSTATE(40000) "CONSTRAINT PRIMARY KEY: a"
1187 : " table can have only one PRIMARY KEY\n");
1188 : }
1189 : }
1190 : }
1191 :
1192 21092 : for (n = ol_first_node(t->columns); n; n = n->next) {
1193 : /* null or default value changes */
1194 19848 : sql_column *c = n->data;
1195 :
1196 19848 : if (c->base.new)
1197 : break;
1198 :
1199 19741 : sql_column *nc = mvc_bind_column(sql, nt, c->base.name);
1200 19741 : if (c->base.deleted) {
1201 68 : switch (mvc_drop_column(sql, nt, nc, c->drop_action)) {
1202 0 : case -1:
1203 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1204 1 : case -2:
1205 : case -3:
1206 1 : throw(SQL,"sql.alter_table",
1207 : SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
1208 : default:
1209 67 : break;
1210 : }
1211 67 : continue;
1212 : }
1213 19673 : if (c->null != nc->null && isTable(nt)) {
1214 99 : if (c->null && nt->pkey) { /* check for primary keys based on this column */
1215 2 : node *m;
1216 4 : for (m = nt->pkey->k.columns->h; m; m = m->next) {
1217 4 : sql_kc *kc = m->data;
1218 :
1219 4 : if (kc->c->base.id == c->base.id)
1220 2 : throw(SQL,"sql.alter_table", SQLSTATE(40000) "NOT NULL CONSTRAINT: cannot remove NOT NULL CONSTRAINT for column '%s' part of the PRIMARY KEY\n", c->base.name);
1221 : }
1222 : }
1223 97 : switch (mvc_null(sql, nc, c->null)) {
1224 0 : case -1:
1225 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1226 1 : case -2:
1227 : case -3:
1228 1 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "NOT NULL CONSTRAINT: transaction conflict detected");
1229 : default:
1230 96 : break;
1231 : }
1232 : /* for non empty check for nulls */
1233 96 : sqlstore *store = sql->session->tr->store;
1234 96 : if (c->null == 0) {
1235 94 : const void *nilptr = ATOMnilptr(c->type.type->localtype);
1236 94 : rids *nils = store->table_api.rids_select(sql->session->tr, nc, nilptr, NULL, NULL);
1237 94 : if (!nils)
1238 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1239 94 : int has_nils = !is_oid_nil(store->table_api.rids_next(nils));
1240 :
1241 94 : store->table_api.rids_destroy(nils);
1242 94 : if (has_nils)
1243 11 : throw(SQL,"sql.alter_table", SQLSTATE(40002) "ALTER TABLE: NOT NULL constraint violated for column %s.%s", c->t->base.name, c->base.name);
1244 : }
1245 : }
1246 19659 : if ((c->def || nc->def) && (!c->def || !nc->def || strcmp(c->def, nc->def) != 0)) {
1247 23 : switch (mvc_default(sql, nc, c->def)) {
1248 0 : case -1:
1249 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1250 0 : case -2:
1251 : case -3:
1252 0 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "DEFAULT: transaction conflict detected");
1253 : default:
1254 : break;
1255 : }
1256 : }
1257 :
1258 19659 : if ((c->storage_type || nc->storage_type) && (!c->storage_type || !nc->storage_type || strcmp(c->storage_type, nc->storage_type) != 0)) {
1259 0 : if (c->t->access == TABLE_WRITABLE)
1260 0 : throw(SQL,"sql.alter_table", SQLSTATE(40002) "ALTER TABLE: SET STORAGE for column %s.%s only allowed on READ or INSERT ONLY tables", c->t->base.name, c->base.name);
1261 0 : switch (mvc_storage(sql, nc, c->storage_type)) {
1262 0 : case -1:
1263 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1264 0 : case -2:
1265 : case -3:
1266 0 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "ALTER TABLE: SET STORAGE transaction conflict detected");
1267 : default:
1268 : break;
1269 : }
1270 : }
1271 : }
1272 : /* handle new columns */
1273 1456 : for (; n; n = n->next) {
1274 : /* propagate alter table .. add column */
1275 107 : sql_column *c = n->data;
1276 :
1277 107 : if (c->base.deleted) /* skip */
1278 0 : continue;
1279 107 : switch (mvc_copy_column(sql, nt, c, NULL)) {
1280 0 : case -1:
1281 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1282 2 : case -2:
1283 : case -3:
1284 2 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "ALTER TABLE: %s_%s_%s conflicts with another transaction", s->base.name, t->base.name, c->base.name);
1285 : default:
1286 : break;
1287 : }
1288 : }
1289 1349 : if (t->idxs) {
1290 : /* alter drop index */
1291 1349 : if (t->idxs)
1292 2457 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1293 1108 : sql_idx *i = n->data;
1294 1108 : if (i->base.new || !i->base.deleted)
1295 1108 : continue;
1296 0 : sql_idx *ni = mvc_bind_idx(sql, s, i->base.name);
1297 0 : if (ni == NULL)
1298 0 : throw(SQL, "sql.alter_table", "Couldn't bind index %s", i->base.name);
1299 0 : switch (mvc_drop_idx(sql, s, ni)) {
1300 0 : case -1:
1301 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1302 0 : case -2:
1303 : case -3:
1304 0 : throw(SQL,"sql.alter_table",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
1305 : default:
1306 : break;
1307 : }
1308 : }
1309 : /* alter add index */
1310 2440 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1311 1108 : sql_idx *i = n->data;
1312 1108 : BAT *b = NULL, *nb = NULL;
1313 :
1314 1108 : if (!i->base.new || i->base.deleted)
1315 0 : continue;
1316 :
1317 1108 : if (i->type == ordered_idx) {
1318 77 : sql_kc *ic = i->columns->h->data;
1319 77 : if (!(b = mvc_bind(sql, nt->s->base.name, nt->base.name, ic->c->base.name, RDONLY)))
1320 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access ordered index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1321 77 : if (VIEWtparent(b)) {
1322 77 : nb = BBP_desc(VIEWtparent(b));
1323 77 : BBPunfix(b->batCacheid);
1324 77 : if (!(b = BATdescriptor(nb->batCacheid)))
1325 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access ordered index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1326 : }
1327 77 : char *msg = OIDXcreateImplementation(cntxt, newBatType(b->ttype), b, -1);
1328 77 : BBPunfix(b->batCacheid);
1329 77 : if (msg != MAL_SUCCEED) {
1330 0 : char *smsg = createException(SQL,"sql.alter_table", SQLSTATE(40002) "CREATE ORDERED INDEX: %s", msg);
1331 0 : freeException(msg);
1332 0 : return smsg;
1333 : }
1334 1031 : } else if (i->type == imprints_idx) {
1335 71 : gdk_return r;
1336 71 : sql_kc *ic = i->columns->h->data;
1337 71 : if (!(b = mvc_bind(sql, nt->s->base.name, nt->base.name, ic->c->base.name, RDONLY)))
1338 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access imprints index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1339 71 : if (VIEWtparent(b)) {
1340 71 : nb = BBP_desc(VIEWtparent(b));
1341 71 : BBPunfix(b->batCacheid);
1342 71 : if (!(b = BATdescriptor(nb->batCacheid)))
1343 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access imprints index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1344 : }
1345 71 : if(b->ttype == TYPE_str) {
1346 11 : if (t->access != TABLE_READONLY) {
1347 5 : BBPunfix(b->batCacheid);
1348 5 : throw(SQL, "sql.alter_TABLE", SQLSTATE(HY005) "Cannot create string imprint index %s on non read only table %s.%s", i->base.name, s->base.name, t->base.name);
1349 : }
1350 :
1351 : /* We signal that we want a strimp on b. It will be created the next time it is needed, i.e. by
1352 : * PCRElikeselect.
1353 : */
1354 6 : r = BATsetstrimps(b);
1355 : }
1356 : else {
1357 60 : r = BATimprints(b);
1358 : }
1359 :
1360 66 : BBPunfix(b->batCacheid);
1361 66 : if (r != GDK_SUCCEED)
1362 9 : throw(SQL, "sql.alter_table", GDK_EXCEPTION);
1363 : }
1364 1094 : switch (mvc_copy_idx(sql, nt, i, NULL)) {
1365 0 : case -1:
1366 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1367 3 : case -2:
1368 : case -3:
1369 3 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "ALTER TABLE: %s_%s_%s conflicts with another transaction", s->base.name, t->base.name, i->base.name);
1370 : default:
1371 : break;
1372 : }
1373 : }
1374 : }
1375 1332 : if (t->keys) {
1376 : /* alter drop key */
1377 2117 : for (n = ol_first_node(t->keys); n; n = n->next) {
1378 785 : sql_key *k = n->data;
1379 :
1380 785 : if ((!k->base.new && !k->base.deleted) || (k->base.new && k->base.deleted))
1381 0 : continue;
1382 785 : if (k->base.deleted) {
1383 0 : sql_key *nk = mvc_bind_key(sql, s, k->base.name);
1384 0 : if (nk) {
1385 0 : switch (mvc_drop_key(sql, s, nk, k->drop_action)) {
1386 0 : case -1:
1387 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1388 0 : case -2:
1389 : case -3:
1390 0 : throw(SQL,"sql.alter_table",SQLSTATE(42000) "ALTER TABLE: %s_%s_%s conflicts with another transaction", s->base.name, t->base.name, k->base.name);
1391 : default:
1392 : break;
1393 : }
1394 : }
1395 : } else { /* new */
1396 785 : str err;
1397 785 : if ((err = sql_partition_validate_key(sql, t, k, "ALTER")))
1398 0 : return err;
1399 785 : switch (mvc_copy_key(sql, nt, k, NULL)) {
1400 0 : case -1:
1401 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1402 0 : case -2:
1403 : case -3:
1404 0 : throw(SQL,"sql.alter_table",SQLSTATE(42000) "ALTER TABLE: %s_%s_%s conflicts with another transaction", s->base.name, t->base.name, k->base.name);
1405 : default:
1406 : break;
1407 : }
1408 : }
1409 : }
1410 : }
1411 : return MAL_SUCCEED;
1412 : }
1413 :
1414 : /* the MAL wrappers */
1415 : str
1416 313 : SQLcreate_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1417 313 : { mvc *sql = NULL;
1418 313 : str msg;
1419 313 : str sname = *getArgReference_str(stk, pci, 1);
1420 313 : str seqname = *getArgReference_str(stk, pci, 2);
1421 313 : sql_sequence *s = *(sql_sequence **) getArgReference(stk, pci, 3);
1422 :
1423 313 : initcontext();
1424 313 : msg = create_seq(sql, sname, seqname, s);
1425 313 : return msg;
1426 : }
1427 :
1428 : str
1429 46 : SQLalter_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1430 46 : { mvc *sql = NULL;
1431 46 : str msg = MAL_SUCCEED;
1432 46 : str sname = *getArgReference_str(stk, pci, 1);
1433 46 : str seqname = *getArgReference_str(stk, pci, 2);
1434 46 : sql_sequence *s = *(sql_sequence **) getArgReference(stk, pci, 3);
1435 46 : lng *val = NULL;
1436 46 : BAT *b = NULL;
1437 46 : BATiter bi = {0};
1438 :
1439 46 : initcontext();
1440 46 : if (getArgType(mb, pci, 4) == TYPE_lng)
1441 46 : val = getArgReference_lng(stk, pci, 4);
1442 0 : else if (isaBatType(getArgType(mb, pci, 4))) {
1443 0 : bat *bid = getArgReference_bat(stk, pci, 4);
1444 :
1445 0 : if (!(b = BATdescriptor(*bid)))
1446 0 : throw(SQL, "sql.alter_seq", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
1447 0 : if (BATcount(b) != 1) {
1448 0 : BBPunfix(b->batCacheid);
1449 0 : throw(SQL, "sql.alter_seq", SQLSTATE(42000) "Only one value allowed to alter a sequence value");
1450 : }
1451 0 : bi = bat_iterator(b);
1452 0 : if (getBatType(getArgType(mb, pci, 4)) == TYPE_lng) {
1453 0 : val = (lng*)bi.base;
1454 : }
1455 : }
1456 :
1457 46 : if (val == NULL || is_lng_nil(*val))
1458 1 : msg = createException(SQL,"sql.alter_seq", SQLSTATE(42M36) "ALTER SEQUENCE: cannot (re)start with NULL");
1459 : else
1460 45 : msg = alter_seq(sql, sname, seqname, s, val);
1461 :
1462 46 : if (b) {
1463 0 : bat_iterator_end(&bi);
1464 0 : BBPunfix(b->batCacheid);
1465 : }
1466 : return msg;
1467 : }
1468 :
1469 : str
1470 32 : SQLdrop_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1471 32 : { mvc *sql = NULL;
1472 32 : str msg = MAL_SUCCEED;
1473 32 : str sname = *getArgReference_str(stk, pci, 1);
1474 32 : str name = *getArgReference_str(stk, pci, 2);
1475 :
1476 32 : initcontext();
1477 32 : msg = drop_seq(sql, sname, name);
1478 32 : return msg;
1479 : }
1480 :
1481 : str
1482 1082 : SQLcreate_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1483 1082 : { mvc *sql = NULL;
1484 1082 : str msg = MAL_SUCCEED;
1485 1082 : str sname = *getArgReference_str(stk, pci, 1);
1486 1082 : str name = SaveArgReference(stk, pci, 2);
1487 1082 : sqlid auth_id;
1488 :
1489 1082 : initcontext();
1490 1082 : auth_id = sql->role_id;
1491 1120 : if (!strNil(name) && (auth_id = sql_find_auth(sql, name)) < 0)
1492 0 : throw(SQL,"sql.create_schema", SQLSTATE(42M32) "CREATE SCHEMA: no such authorization '%s'", name);
1493 1082 : if (sql->user_id != USER_MONETDB && sql->role_id != ROLE_SYSADMIN)
1494 0 : throw(SQL,"sql.create_schema", SQLSTATE(42000) "CREATE SCHEMA: insufficient privileges for user '%s'", get_string_global_var(sql, "current_user"));
1495 1082 : if (mvc_bind_schema(sql, sname))
1496 0 : throw(SQL,"sql.create_schema", SQLSTATE(3F000) "CREATE SCHEMA: name '%s' already in use", sname);
1497 1082 : switch (mvc_create_schema(sql, sname, auth_id, sql->user_id)) {
1498 0 : case -1:
1499 0 : throw(SQL,"sql.create_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1500 1 : case -2:
1501 : case -3:
1502 1 : throw(SQL,"sql.create_schema",SQLSTATE(42000) "CREATE SCHEMA: transaction conflict detected");
1503 : default:
1504 : break;
1505 : }
1506 : return msg;
1507 : }
1508 :
1509 : str
1510 191 : SQLdrop_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1511 : {
1512 191 : mvc *sql = NULL;
1513 191 : str msg = MAL_SUCCEED;
1514 191 : str sname = *getArgReference_str(stk, pci, 1);
1515 191 : int if_exists = *getArgReference_int(stk, pci, 2);
1516 191 : int action = *getArgReference_int(stk, pci, 3);
1517 191 : sql_schema *s;
1518 :
1519 191 : initcontext();
1520 191 : s = mvc_bind_schema(sql, sname);
1521 191 : if (!s) {
1522 12 : if (!if_exists)
1523 8 : throw(SQL,"sql.drop_schema",SQLSTATE(3F000) "DROP SCHEMA: name %s does not exist", sname);
1524 : return MAL_SUCCEED;
1525 : }
1526 179 : sql_trans *tr = sql->session->tr;
1527 179 : sql_schema *cur = cur_schema(sql);
1528 :
1529 179 : if (!mvc_schema_privs(sql, s))
1530 0 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1531 179 : if (cur && s->base.id == cur->base.id)
1532 1 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: cannot drop current schema");
1533 178 : if (s->system)
1534 8 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: access denied for '%s'", sname);
1535 170 : if (sql_schema_has_user(sql, s))
1536 8 : throw(SQL,"sql.drop_schema",SQLSTATE(2BM37) "DROP SCHEMA: unable to drop schema '%s' (there are database users using it as session's default schema)", sname);
1537 243 : if (!action /* RESTRICT */ && (
1538 159 : os_size(s->tables, tr) || os_size(s->types, tr) || os_size(s->funcs, tr) || os_size(s->seqs, tr)))
1539 3 : throw(SQL,"sql.drop_schema",SQLSTATE(2BM37) "DROP SCHEMA: unable to drop schema '%s' (there are database objects which depend on it)", sname);
1540 :
1541 159 : switch (mvc_drop_schema(sql, s, action)) {
1542 0 : case -1:
1543 0 : throw(SQL,"sql.drop_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1544 0 : case -2:
1545 : case -3:
1546 0 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: transaction conflict detected");
1547 : default:
1548 : break;
1549 : }
1550 : return MAL_SUCCEED;
1551 : }
1552 :
1553 : str
1554 10087 : SQLcreate_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1555 10087 : { mvc *sql = NULL;
1556 10087 : str msg;
1557 10087 : str sname = *getArgReference_str(stk, pci, 1);
1558 : //str tname = *getArgReference_str(stk, pci, 2);
1559 10087 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1560 10087 : int temp = *getArgReference_int(stk, pci, 4), remote = (pci->argc == 7);
1561 10087 : int pw_encrypted = temp;
1562 :
1563 10087 : initcontext();
1564 10079 : if (remote)
1565 101 : temp = 0;
1566 10079 : msg = create_table_or_view(sql, sname, t->base.name, t, temp, 0);
1567 10079 : if (!msg && remote) {
1568 101 : str username = *getArgReference_str(stk, pci, 5);
1569 101 : str password = *getArgReference_str(stk, pci, 6);
1570 :
1571 101 : sql_schema *s = mvc_bind_schema(sql, sname);
1572 101 : t = s?mvc_bind_table(sql, s, t->base.name):NULL;
1573 101 : if (t)
1574 101 : return remote_create(sql, t->base.id, username, password, pw_encrypted);
1575 0 : throw(SQL, "sql.create_table", SQLSTATE(3F000) "Internal error");
1576 : }
1577 : return msg;
1578 : }
1579 :
1580 : str
1581 22358 : SQLcreate_view(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1582 22358 : { mvc *sql = NULL;
1583 22358 : str msg;
1584 22358 : str sname = *getArgReference_str(stk, pci, 1);
1585 : //str vname = *getArgReference_str(stk, pci, 2);
1586 22358 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1587 22358 : int temp = *getArgReference_int(stk, pci, 4);
1588 22358 : int replace = *getArgReference_int(stk, pci, 5);
1589 :
1590 22358 : initcontext();
1591 22357 : msg = create_table_or_view(sql, sname, t->base.name, t, temp, replace);
1592 22357 : return msg;
1593 : }
1594 :
1595 : str
1596 3673 : SQLdrop_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1597 3673 : { mvc *sql = NULL;
1598 3673 : str msg;
1599 3673 : str sname = *getArgReference_str(stk, pci, 1);
1600 3673 : str name = *getArgReference_str(stk, pci, 2);
1601 3673 : int if_exists = *getArgReference_int(stk, pci, 3);
1602 3673 : int action = *getArgReference_int(stk, pci, 4);
1603 :
1604 3673 : initcontext();
1605 3673 : msg = drop_table(sql, sname, name, action, if_exists);
1606 3673 : return msg;
1607 : }
1608 :
1609 : str
1610 314 : SQLdrop_view(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1611 314 : { mvc *sql = NULL;
1612 314 : str msg;
1613 314 : str sname = *getArgReference_str(stk, pci, 1);
1614 314 : str name = *getArgReference_str(stk, pci, 2);
1615 314 : int if_exists = *getArgReference_int(stk, pci, 3);
1616 314 : int action = *getArgReference_int(stk, pci, 4);
1617 :
1618 314 : initcontext();
1619 314 : msg = drop_view(sql, sname, name, action, if_exists);
1620 314 : return msg;
1621 : }
1622 :
1623 : str
1624 150 : SQLdrop_constraint(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1625 150 : { mvc *sql = NULL;
1626 150 : str msg;
1627 150 : str sname = *getArgReference_str(stk, pci, 1);
1628 150 : str tname = *getArgReference_str(stk, pci, 2);
1629 150 : str kname = *getArgReference_str(stk, pci, 3);
1630 150 : int action = *getArgReference_int(stk, pci, 5);
1631 150 : (void) *getArgReference_int(stk, pci, 4); //the if_exists parameter is also passed but not used
1632 :
1633 150 : initcontext();
1634 150 : msg = drop_key(sql, sname, tname, kname, action);
1635 150 : return msg;
1636 : }
1637 :
1638 : str
1639 1369 : SQLalter_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1640 1369 : { mvc *sql = NULL;
1641 1369 : str msg;
1642 1369 : str sname = *getArgReference_str(stk, pci, 1);
1643 1369 : str tname = *getArgReference_str(stk, pci, 2);
1644 1369 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1645 :
1646 1369 : (void)tname;
1647 1369 : initcontext();
1648 1369 : msg = alter_table(cntxt, sql, sname, t);
1649 1369 : return msg;
1650 : }
1651 :
1652 : str
1653 906 : SQLcreate_type(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1654 906 : { mvc *sql = NULL;
1655 906 : str msg;
1656 906 : str sname = *getArgReference_str(stk, pci, 1);
1657 906 : char *name = *getArgReference_str(stk, pci, 2);
1658 906 : char *impl = *getArgReference_str(stk, pci, 3);
1659 906 : sql_schema *s = NULL;
1660 :
1661 906 : initcontext();
1662 :
1663 906 : if (!(s = mvc_bind_schema(sql, sname)))
1664 0 : throw(SQL,"sql.create_type",SQLSTATE(3F000) "CREATE TYPE: no such schema '%s'", sname);
1665 906 : if (!mvc_schema_privs(sql, s))
1666 0 : throw(SQL,"sql.create_type", SQLSTATE(42000) "CREATE TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1667 906 : if (schema_bind_type(sql, s, name))
1668 0 : throw(SQL,"sql.create_type", SQLSTATE(42S02) "CREATE TYPE: type '%s' already exists", name);
1669 906 : switch (mvc_create_type(sql, s, name, 0, 0, 0, impl)) {
1670 0 : case -1:
1671 0 : throw(SQL,"sql.create_type", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1672 1 : case -2:
1673 : case -3:
1674 1 : throw(SQL,"sql.create_type", SQLSTATE(42000) "CREATE TYPE: transaction conflict detected");
1675 0 : case -4:
1676 0 : throw(SQL,"sql.create_type", SQLSTATE(0D000) "CREATE TYPE: unknown external type '%s'", impl);
1677 : default:
1678 : break;
1679 : }
1680 : return msg;
1681 : }
1682 :
1683 : str
1684 4 : SQLdrop_type(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1685 4 : { mvc *sql = NULL;
1686 4 : str msg;
1687 4 : str sname = *getArgReference_str(stk, pci, 1);
1688 4 : char *name = *getArgReference_str(stk, pci, 2);
1689 4 : int drop_action = *getArgReference_int(stk, pci, 3);
1690 4 : sql_schema *s = NULL;
1691 4 : sql_type *t;
1692 :
1693 4 : initcontext();
1694 :
1695 4 : if (!(s = mvc_bind_schema(sql, sname)))
1696 0 : throw(SQL,"sql.drop_type",SQLSTATE(3F000) "DROP TYPE: no such schema '%s'", sname);
1697 4 : if (!mvc_schema_privs(sql, s))
1698 0 : throw(SQL,"sql.drop_type", SQLSTATE(42000) "DROP TYPE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1699 4 : if (!(t = schema_bind_type(sql, s, name)))
1700 0 : throw(SQL,"sql.drop_type", SQLSTATE(3F000) "DROP TYPE: type '%s' does not exist", name);
1701 4 : if (!drop_action && mvc_check_dependency(sql, t->base.id, TYPE_DEPENDENCY, NULL))
1702 1 : throw(SQL,"sql.drop_type", SQLSTATE(42000) "DROP TYPE: unable to drop type %s (there are database objects which depend on it)\n", name);
1703 3 : switch (mvc_drop_type(sql, s, t, drop_action)) {
1704 0 : case -1:
1705 0 : throw(SQL,"sql.drop_type",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1706 0 : case -2:
1707 : case -3:
1708 0 : throw(SQL,"sql.drop_type",SQLSTATE(42000) "DROP TYPE: transaction conflict detected");
1709 : default:
1710 : break;
1711 : }
1712 : return msg;
1713 : }
1714 :
1715 : str
1716 40 : SQLgrant_roles(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1717 40 : { mvc *sql = NULL;
1718 40 : str msg;
1719 40 : str sname = *getArgReference_str(stk, pci, 1);
1720 40 : char *auth = SaveArgReference(stk, pci, 2);
1721 40 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 3);
1722 40 : int admin = *getArgReference_int(stk, pci, 4);
1723 :
1724 40 : initcontext();
1725 40 : msg = sql_grant_role(sql, sname /*grantee */ , auth, grantor, admin);
1726 40 : return msg;
1727 : }
1728 :
1729 : str
1730 10 : SQLrevoke_roles(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1731 10 : { mvc *sql = NULL;
1732 10 : str msg;
1733 10 : str sname = *getArgReference_str(stk, pci, 1);
1734 10 : char *auth = SaveArgReference(stk, pci, 2);
1735 10 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 3);
1736 10 : int admin = *getArgReference_int(stk, pci, 4);
1737 :
1738 10 : initcontext();
1739 10 : msg = sql_revoke_role(sql, sname /*grantee */ , auth, grantor, admin);
1740 10 : return msg;
1741 : }
1742 :
1743 : str
1744 18454 : SQLgrant(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1745 18454 : { mvc *sql = NULL;
1746 18454 : str msg;
1747 18454 : str sname = *getArgReference_str(stk, pci, 1);
1748 18454 : char *tname = *getArgReference_str(stk, pci, 2);
1749 18454 : char *grantee = *getArgReference_str(stk, pci, 3);
1750 18454 : int privs = *getArgReference_int(stk, pci, 4);
1751 18454 : char *cname = SaveArgReference(stk, pci, 5);
1752 18454 : int grant = *getArgReference_int(stk, pci, 6);
1753 18454 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 7);
1754 :
1755 18454 : initcontext();
1756 18454 : if (strNil(tname))
1757 10 : msg = sql_grant_global_privs(sql, grantee, privs, grant, grantor);
1758 : else
1759 18444 : msg = sql_grant_table_privs(sql, grantee, privs, sname, tname, cname, grant, grantor);
1760 : return msg;
1761 : }
1762 :
1763 15 : str SQLrevoke(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1764 15 : { mvc *sql = NULL;
1765 15 : str msg;
1766 15 : str sname = *getArgReference_str(stk, pci, 1);
1767 15 : char *tname = *getArgReference_str(stk, pci, 2);
1768 15 : char *grantee = *getArgReference_str(stk, pci, 3);
1769 15 : int privs = *getArgReference_int(stk, pci, 4);
1770 15 : char *cname = SaveArgReference(stk, pci, 5);
1771 15 : int grant = *getArgReference_int(stk, pci, 6);
1772 15 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 7);
1773 :
1774 15 : initcontext();
1775 15 : if (strNil(tname))
1776 2 : msg = sql_revoke_global_privs(sql, grantee, privs, grant, grantor);
1777 : else
1778 13 : msg = sql_revoke_table_privs(sql, grantee, privs, sname, tname, cname, grant, grantor);
1779 : return msg;
1780 : }
1781 :
1782 : str
1783 99018 : SQLgrant_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1784 99018 : { mvc *sql = NULL;
1785 99018 : str msg;
1786 99018 : str sname = *getArgReference_str(stk, pci, 1);
1787 99018 : sqlid func_id = (sqlid) *getArgReference_int(stk, pci, 2);
1788 99018 : char *grantee = *getArgReference_str(stk, pci, 3);
1789 99018 : int privs = *getArgReference_int(stk, pci, 4);
1790 99018 : int grant = *getArgReference_int(stk, pci, 5);
1791 99018 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 6);
1792 :
1793 99018 : initcontext();
1794 99018 : msg = sql_grant_func_privs(sql, grantee, privs, sname, func_id, grant, grantor);
1795 99018 : return msg;
1796 : }
1797 :
1798 : str
1799 1 : SQLrevoke_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1800 1 : { mvc *sql = NULL;
1801 1 : str msg;
1802 1 : str sname = *getArgReference_str(stk, pci, 1);
1803 1 : sqlid func_id = (sqlid) *getArgReference_int(stk, pci, 2);
1804 1 : char *grantee = *getArgReference_str(stk, pci, 3);
1805 1 : int privs = *getArgReference_int(stk, pci, 4);
1806 1 : int grant = *getArgReference_int(stk, pci, 5);
1807 1 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 6);
1808 :
1809 1 : initcontext();
1810 1 : msg = sql_revoke_func_privs(sql, grantee, privs, sname, func_id, grant, grantor);
1811 1 : return msg;
1812 : }
1813 :
1814 : str
1815 358 : SQLcreate_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1816 358 : { mvc *sql = NULL;
1817 358 : str msg;
1818 358 : str sname = *getArgReference_str(stk, pci, 1);
1819 358 : char *passwd = *getArgReference_str(stk, pci, 2);
1820 358 : int enc = *getArgReference_int(stk, pci, 3);
1821 358 : char *schema = SaveArgReference(stk, pci, 4);
1822 358 : char *schema_path = SaveArgReference(stk, pci, 5);
1823 358 : char *fullname = SaveArgReference(stk, pci, 6);
1824 358 : lng max_memory = *getArgReference_lng(stk, pci, 7);
1825 358 : int max_workers = *getArgReference_int(stk, pci, 8);
1826 358 : char *optimizer = SaveArgReference(stk, pci, 9);
1827 358 : char *default_role = SaveArgReference(stk, pci, 10);
1828 :
1829 358 : initcontext();
1830 358 : msg = sql_create_user(sql, sname, passwd, enc, fullname, schema, schema_path, max_memory, max_workers, optimizer, default_role);
1831 358 : return msg;
1832 : }
1833 :
1834 : str
1835 107 : SQLdrop_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1836 107 : { mvc *sql = NULL;
1837 107 : str msg;
1838 107 : str sname = *getArgReference_str(stk, pci, 1);
1839 :
1840 107 : initcontext();
1841 107 : msg = sql_drop_user(sql, sname);
1842 107 : return msg;
1843 : }
1844 :
1845 : str
1846 83 : SQLalter_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1847 83 : { mvc *sql = NULL;
1848 83 : str msg;
1849 83 : str sname = *getArgReference_str(stk, pci, 1);
1850 83 : char *passwd = SaveArgReference(stk, pci, 2);
1851 83 : int enc = *getArgReference_int(stk, pci, 3);
1852 83 : char *schema = SaveArgReference(stk, pci, 4);
1853 83 : char *schema_path = SaveArgReference(stk, pci, 5);
1854 83 : char *oldpasswd = SaveArgReference(stk, pci, 6);
1855 83 : char *role = SaveArgReference(stk, pci, 7);
1856 83 : lng max_memory = *getArgReference_lng(stk, pci, 8);
1857 83 : int max_workers = *getArgReference_int(stk, pci, 9);
1858 :
1859 83 : initcontext();
1860 83 : msg = sql_alter_user(sql, sname, passwd, enc, schema, schema_path, oldpasswd, role, max_memory, max_workers);
1861 :
1862 83 : return msg;
1863 : }
1864 :
1865 : str
1866 5 : SQLrename_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1867 5 : { mvc *sql = NULL;
1868 5 : str msg;
1869 5 : str sname = *getArgReference_str(stk, pci, 1);
1870 5 : char *newuser = *getArgReference_str(stk, pci, 2);
1871 :
1872 5 : initcontext();
1873 5 : msg = sql_rename_user(sql, sname, newuser);
1874 5 : return msg;
1875 : }
1876 :
1877 : str
1878 26 : SQLcreate_role(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1879 26 : { mvc *sql = NULL;
1880 26 : str msg;
1881 26 : str sname = *getArgReference_str(stk, pci, 1);
1882 26 : char *role = sname;
1883 26 : sqlid grantor = (sqlid)*getArgReference_int(stk, pci, 3);
1884 :
1885 26 : initcontext();
1886 26 : msg = sql_create_role(sql, role, grantor);
1887 26 : return msg;
1888 : }
1889 :
1890 : str
1891 19 : SQLdrop_role(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1892 19 : { mvc *sql = NULL;
1893 19 : str msg;
1894 19 : str sname = *getArgReference_str(stk, pci, 1);
1895 19 : char *role = sname;
1896 :
1897 19 : initcontext();
1898 19 : msg = sql_drop_role(sql, role);
1899 19 : return msg;
1900 : }
1901 :
1902 : str
1903 160 : SQLdrop_index(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1904 160 : { mvc *sql = NULL;
1905 160 : str msg;
1906 160 : str sname = *getArgReference_str(stk, pci, 1);
1907 160 : char *iname = *getArgReference_str(stk, pci, 2);
1908 :
1909 160 : initcontext();
1910 160 : msg = drop_index(sql, sname, iname);
1911 160 : return msg;
1912 : }
1913 :
1914 : str
1915 668 : SQLdrop_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1916 668 : { mvc *sql = NULL;
1917 668 : str msg;
1918 668 : str sname = *getArgReference_str(stk, pci, 1);
1919 668 : char *fname = *getArgReference_str(stk, pci, 2);
1920 668 : sqlid fid = (sqlid)*getArgReference_int(stk, pci, 3);
1921 668 : sql_ftype type = (sql_ftype) *getArgReference_int(stk, pci, 4);
1922 668 : int action = *getArgReference_int(stk, pci, 5);
1923 :
1924 668 : initcontext();
1925 668 : msg = drop_func(sql, sname, fname, fid, type, action);
1926 668 : return msg;
1927 : }
1928 :
1929 : str
1930 120295 : SQLcreate_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1931 120295 : { mvc *sql = NULL;
1932 120295 : str msg;
1933 120295 : str sname = *getArgReference_str(stk, pci, 1);
1934 120295 : str fname = *getArgReference_str(stk, pci, 2);
1935 120295 : sql_func *f = *(sql_func **) getArgReference(stk, pci, 3);
1936 120295 : int replace = *getArgReference_int(stk, pci, 4);
1937 :
1938 120295 : initcontext();
1939 120295 : msg = create_func(sql, sname, fname, f, replace);
1940 120295 : return msg;
1941 : }
1942 :
1943 : str
1944 345 : SQLcreate_trigger(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1945 345 : { mvc *sql = NULL;
1946 345 : str msg;
1947 345 : str sname = *getArgReference_str(stk, pci, 1);
1948 345 : char *tname = *getArgReference_str(stk, pci, 2);
1949 345 : char *triggername = *getArgReference_str(stk, pci, 3);
1950 345 : int time = *getArgReference_int(stk, pci, 4);
1951 345 : int orientation = *getArgReference_int(stk, pci, 5);
1952 345 : int event = *getArgReference_int(stk, pci, 6);
1953 345 : char *old_name = *getArgReference_str(stk, pci, 7);
1954 345 : char *new_name = *getArgReference_str(stk, pci, 8);
1955 345 : char *condition = *getArgReference_str(stk, pci, 9);
1956 345 : char *query = *getArgReference_str(stk, pci, 10);
1957 345 : int replace = *getArgReference_int(stk, pci, 11);
1958 :
1959 345 : initcontext();
1960 374 : old_name=(strNil(old_name))?NULL:old_name;
1961 385 : new_name=(strNil(new_name))?NULL:new_name;
1962 345 : condition=(strNil(condition))?NULL:condition;
1963 345 : msg = create_trigger(sql, sname, tname, triggername, time, orientation, event, old_name, new_name, condition, query, replace);
1964 345 : return msg;
1965 : }
1966 :
1967 : str
1968 84 : SQLdrop_trigger(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1969 84 : { mvc *sql = NULL;
1970 84 : str msg;
1971 84 : str sname = *getArgReference_str(stk, pci, 1);
1972 84 : char *triggername = *getArgReference_str(stk, pci, 2);
1973 84 : int if_exists = *getArgReference_int(stk, pci, 3);
1974 :
1975 84 : initcontext();
1976 84 : msg = drop_trigger(sql, sname, triggername, if_exists);
1977 84 : return msg;
1978 : }
1979 :
1980 : str
1981 312 : SQLalter_add_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1982 312 : { mvc *sql = NULL;
1983 312 : str msg;
1984 312 : str sname = *getArgReference_str(stk, pci, 1);
1985 312 : char *mtname = SaveArgReference(stk, pci, 2);
1986 312 : char *psname = SaveArgReference(stk, pci, 3);
1987 312 : char *ptname = SaveArgReference(stk, pci, 4);
1988 :
1989 312 : initcontext();
1990 312 : msg = alter_table_add_table(sql, sname, mtname, psname, ptname);
1991 312 : return msg;
1992 : }
1993 :
1994 : str
1995 214 : SQLalter_add_range_partition(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1996 214 : { mvc *sql = NULL;
1997 214 : str msg;
1998 214 : str sname = *getArgReference_str(stk, pci, 1);
1999 214 : char *mtname = SaveArgReference(stk, pci, 2);
2000 214 : char *psname = SaveArgReference(stk, pci, 3);
2001 214 : char *ptname = SaveArgReference(stk, pci, 4);
2002 214 : ValRecord *min = &(stk)->stk[(pci)->argv[5]];
2003 214 : ValRecord *max = &(stk)->stk[(pci)->argv[6]];
2004 214 : bit with_nills = *getArgReference_bit(stk, pci, 7);
2005 214 : int update = *getArgReference_int(stk, pci, 8);
2006 214 : lng cnt = 0;
2007 :
2008 214 : if (getArgType(mb, pci, 9) == TYPE_lng) {
2009 212 : cnt = *getArgReference_lng(stk, pci, 9);
2010 : } else {
2011 2 : BAT *c = BATdescriptor(*getArgReference_bat(stk, pci, 9));
2012 2 : if (c && BATcount(c) == 1)
2013 2 : cnt = *(lng*)Tloc(c, 0);
2014 2 : if (c)
2015 2 : BBPunfix(c->batCacheid);
2016 : }
2017 :
2018 214 : initcontext();
2019 214 : msg = alter_table_add_range_partition(sql, sname, mtname, psname, ptname, VALget(min), VALget(max), with_nills, update, cnt);
2020 214 : return msg;
2021 : }
2022 :
2023 : str
2024 59 : SQLalter_add_value_partition(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2025 59 : { mvc *sql = NULL;
2026 59 : str msg;
2027 59 : str sname = *getArgReference_str(stk, pci, 1);
2028 59 : char *mtname = SaveArgReference(stk, pci, 2);
2029 59 : char *psname = SaveArgReference(stk, pci, 3);
2030 59 : char *ptname = SaveArgReference(stk, pci, 4);
2031 59 : bit with_nills = *getArgReference_bit(stk, pci, 5);
2032 59 : int update = *getArgReference_int(stk, pci, 6);
2033 59 : lng cnt = 0;
2034 :
2035 59 : if (getArgType(mb, pci, 7) == TYPE_lng) {
2036 59 : cnt = *getArgReference_lng(stk, pci, 7);
2037 : } else {
2038 0 : BAT *c = BATdescriptor(*getArgReference_bat(stk, pci, 7));
2039 0 : if (c && BATcount(c) == 1)
2040 0 : cnt = *(lng*)Tloc(c, 0);
2041 0 : if (c)
2042 0 : BBPunfix(c->batCacheid);
2043 : }
2044 :
2045 59 : initcontext();
2046 59 : msg = alter_table_add_value_partition(sql, stk, pci, sname, mtname, psname, ptname, with_nills, update, cnt);
2047 59 : return msg;
2048 : }
2049 :
2050 : str
2051 180 : SQLalter_del_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2052 180 : { mvc *sql = NULL;
2053 180 : str msg;
2054 180 : str sname = *getArgReference_str(stk, pci, 1);
2055 180 : char *mtname = SaveArgReference(stk, pci, 2);
2056 180 : char *psname = SaveArgReference(stk, pci, 3);
2057 180 : char *ptname = SaveArgReference(stk, pci, 4);
2058 180 : int drop_action = *getArgReference_int(stk, pci, 5);
2059 :
2060 180 : initcontext();
2061 180 : msg= alter_table_del_table(sql, sname, mtname, psname, ptname, drop_action);
2062 180 : return msg;
2063 : }
2064 :
2065 : str
2066 2310 : SQLalter_set_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2067 2310 : { mvc *sql = NULL;
2068 2310 : str msg;
2069 2310 : str sname = *getArgReference_str(stk, pci, 1);
2070 2310 : char *tname = SaveArgReference(stk, pci, 2);
2071 2310 : int access = *getArgReference_int(stk, pci, 3);
2072 :
2073 2310 : initcontext();
2074 2310 : msg = alter_table_set_access(sql, sname, tname, access);
2075 :
2076 2310 : return msg;
2077 : }
2078 :
2079 : str
2080 354 : SQLcomment_on(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2081 : {
2082 354 : mvc *sql = NULL;
2083 354 : str msg;
2084 354 : sqlid objid = (sqlid) *getArgReference_int(stk, pci, 1);
2085 354 : char *remark = *getArgReference_str(stk, pci, 2);
2086 354 : sql_trans *tx;
2087 354 : sql_schema *sys;
2088 354 : sql_table *comments;
2089 354 : sql_column *id_col, *remark_col;
2090 354 : oid rid;
2091 354 : int ok = LOG_OK;
2092 :
2093 354 : initcontext();
2094 :
2095 : // Manually insert the rows to circumvent permission checks.
2096 354 : tx = sql->session->tr;
2097 354 : sys = mvc_bind_schema(sql, "sys");
2098 354 : if (!sys)
2099 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "Internal error");
2100 354 : comments = mvc_bind_table(sql, sys, "comments");
2101 354 : if (!comments)
2102 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "no table sys.comments");
2103 354 : id_col = mvc_bind_column(sql, comments, "id");
2104 354 : remark_col = find_sql_column(comments, "remark");
2105 354 : if (!id_col || !remark_col)
2106 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "no table sys.comments");
2107 354 : sqlstore *store = tx->store;
2108 354 : rid = store->table_api.column_find_row(tx, id_col, &objid, NULL);
2109 708 : if (!strNil(remark) && *remark) {
2110 345 : if (!is_oid_nil(rid)) {
2111 : // have new remark and found old one, so update field
2112 : /* UPDATE sys.comments SET remark = %s WHERE id = %d */
2113 4 : ok = store->table_api.column_update_value(tx, remark_col, rid, remark);
2114 : } else {
2115 : // have new remark but found none so insert row
2116 : /* INSERT INTO sys.comments (id, remark) VALUES (%d, %s) */
2117 341 : ok = store->table_api.table_insert(tx, comments, &objid, &remark);
2118 : }
2119 345 : if (ok != LOG_OK)
2120 1 : throw(SQL, "sql.comment_on", SQLSTATE(42000) "Comment on failed%s", ok == LOG_CONFLICT ? " due to conflict with another transaction" : "");
2121 344 : if ((ok = sql_trans_add_dependency(tx, objid, ddl)) != LOG_OK) /* At the moment this adds dependencies for old objects :( */
2122 0 : throw(SQL, "sql.comment_on", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2123 : } else {
2124 9 : if (!is_oid_nil(rid)) {
2125 : // have no remark but found one, so delete row
2126 : /* DELETE FROM sys.comments WHERE id = %d */
2127 7 : if ((ok = store->table_api.table_delete(tx, comments, rid)) != LOG_OK)
2128 0 : throw(SQL, "sql.comment_on", SQLSTATE(42000) "Comment on failed%s", ok == LOG_CONFLICT ? " due to conflict with another transaction" : "");
2129 : }
2130 : }
2131 : return MAL_SUCCEED;
2132 : }
2133 :
2134 : str
2135 8 : SQLrename_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2136 : {
2137 8 : mvc *sql = NULL;
2138 8 : str msg = MAL_SUCCEED;
2139 8 : str old_name = *getArgReference_str(stk, pci, 1);
2140 8 : str new_name = *getArgReference_str(stk, pci, 2);
2141 8 : sql_schema *s;
2142 :
2143 8 : initcontext();
2144 8 : sql_schema *cur = cur_schema(sql);
2145 :
2146 8 : if (!(s = mvc_bind_schema(sql, old_name)))
2147 0 : throw(SQL, "sql.rename_schema", SQLSTATE(42S02)
2148 : "ALTER SCHEMA: no such schema '%s'", old_name);
2149 :
2150 8 : if (!mvc_schema_privs(sql, s))
2151 0 : throw(SQL, "sql.rename_schema", SQLSTATE(42000)
2152 : "ALTER SCHEMA: access denied for %s to schema '%s'",
2153 : get_string_global_var(sql, "current_user"), old_name);
2154 :
2155 8 : if (s->system)
2156 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000)
2157 : "ALTER SCHEMA: cannot rename a system schema");
2158 :
2159 16 : if (strNil(new_name) || *new_name == '\0')
2160 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000)
2161 : "ALTER SCHEMA: invalid new schema name");
2162 :
2163 8 : if (mvc_bind_schema(sql, new_name))
2164 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000)
2165 : "ALTER SCHEMA: there is a schema named '%s' in the database", new_name);
2166 :
2167 8 : if (mvc_check_dependency(sql, s->base.id, SCHEMA_DEPENDENCY, NULL) == HAS_DEPENDENCY) {
2168 0 : throw(SQL, "sql.rename_schema", "ALTER SCHEMA: unable to"
2169 : " rename schema '%s', there are database objects"
2170 : " which depend on it", old_name);
2171 : }
2172 :
2173 8 : switch (sql_trans_rename_schema(sql->session->tr, s->base.id, new_name)) {
2174 0 : case -1:
2175 0 : throw(SQL,"sql.rename_schema", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2176 0 : case -2:
2177 : case -3:
2178 0 : throw(SQL,"sql.rename_schema", SQLSTATE(42000)
2179 : "ALTER SCHEMA: transaction conflict detected");
2180 : default:
2181 8 : break;
2182 : }
2183 :
2184 8 : if (cur && s->base.id == cur->base.id) {
2185 1 : if (!mvc_set_schema(sql, new_name))
2186 0 : throw(SQL, "sql.rename_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
2187 :
2188 1 : s = mvc_bind_schema(sql, "sys");
2189 1 : assert(s);
2190 :
2191 1 : if (!sqlvar_set_string(find_global_var(sql, s, "current_schema"), new_name))
2192 0 : throw(SQL, "sql.setVariable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2193 : }
2194 :
2195 : return msg;
2196 : }
2197 :
2198 : str
2199 45 : SQLrename_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2200 : {
2201 45 : mvc *sql = NULL;
2202 45 : str msg = MAL_SUCCEED;
2203 45 : str oschema_name = *getArgReference_str(stk, pci, 1);
2204 45 : str nschema_name = *getArgReference_str(stk, pci, 2);
2205 45 : str otable_name = *getArgReference_str(stk, pci, 3);
2206 45 : str ntable_name = *getArgReference_str(stk, pci, 4);
2207 45 : sql_schema *o, *s;
2208 45 : sql_table *t;
2209 :
2210 45 : initcontext();
2211 :
2212 45 : if (strcmp(oschema_name, nschema_name) == 0) { //renaming the table itself
2213 19 : if (!(s = mvc_bind_schema(sql, oschema_name)))
2214 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", oschema_name);
2215 19 : if (!mvc_schema_privs(sql, s))
2216 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), oschema_name);
2217 19 : if (!(t = mvc_bind_table(sql, s, otable_name)))
2218 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", otable_name, oschema_name);
2219 19 : if (t->system)
2220 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a system table");
2221 19 : if (isView(t))
2222 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a view");
2223 19 : if (isDeclaredTable(t))
2224 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a declared table");
2225 19 : if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
2226 0 : throw (SQL,"sql.rename_table", SQLSTATE(2BM37) "ALTER TABLE: unable to rename table '%s' (there are database objects which depend on it)", otable_name);
2227 38 : if (strNil(ntable_name) || *ntable_name == '\0')
2228 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: invalid new table name");
2229 19 : if (mvc_bind_table(sql, s, ntable_name))
2230 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", ntable_name, oschema_name);
2231 :
2232 19 : switch (sql_trans_rename_table(sql->session->tr, s, t->base.id, ntable_name)) {
2233 0 : case -1:
2234 0 : throw(SQL,"sql.rename_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2235 3 : case -2:
2236 : case -3:
2237 3 : throw(SQL,"sql.rename_table", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2238 : default:
2239 : break;
2240 : }
2241 : } else { //changing the schema of the table
2242 26 : assert(strcmp(otable_name, ntable_name) == 0);
2243 :
2244 26 : if (!(o = mvc_bind_schema(sql, oschema_name)))
2245 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", oschema_name);
2246 26 : if (!mvc_schema_privs(sql, o))
2247 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), oschema_name);
2248 26 : if (!(t = mvc_bind_table(sql, o, otable_name)))
2249 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", otable_name, oschema_name);
2250 26 : if (t->system)
2251 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot set schema of a system table");
2252 26 : if (isTempSchema(o))
2253 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change a temporary table schema");
2254 26 : if (isView(t))
2255 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a view");
2256 26 : if (isDeclaredTable(t))
2257 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a declared table");
2258 26 : if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL) || list_length(t->members) || ol_length(t->triggers))
2259 0 : throw(SQL, "sql.rename_table", SQLSTATE(2BM37) "ALTER TABLE: unable to set schema of table '%s' (there are database objects which depend on it)", otable_name);
2260 26 : if (!(s = mvc_bind_schema(sql, nschema_name)))
2261 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", nschema_name);
2262 26 : if (!mvc_schema_privs(sql, s))
2263 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: access denied for '%s' to schema '%s'", get_string_global_var(sql, "current_user"), nschema_name);
2264 26 : if (isTempSchema(s))
2265 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: not possible to change table's schema to temporary");
2266 26 : if (mvc_bind_table(sql, s, otable_name))
2267 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: table '%s' on schema '%s' already exists", otable_name, nschema_name);
2268 :
2269 26 : switch (sql_trans_set_table_schema(sql->session->tr, t->base.id, o, s)) {
2270 0 : case -1:
2271 0 : throw(SQL,"sql.rename_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2272 1 : case -2:
2273 : case -3:
2274 1 : throw(SQL,"sql.rename_table", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2275 : default:
2276 : break;
2277 : }
2278 : }
2279 :
2280 : return msg;
2281 : }
2282 :
2283 : str
2284 13 : SQLrename_column(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2285 : {
2286 13 : mvc *sql = NULL;
2287 13 : str msg = MAL_SUCCEED;
2288 13 : str schema_name = *getArgReference_str(stk, pci, 1);
2289 13 : str table_name = *getArgReference_str(stk, pci, 2);
2290 13 : str old_name = *getArgReference_str(stk, pci, 3);
2291 13 : str new_name = *getArgReference_str(stk, pci, 4);
2292 13 : sql_schema *s;
2293 13 : sql_table *t;
2294 13 : sql_column *col;
2295 :
2296 13 : initcontext();
2297 13 : if (!(s = mvc_bind_schema(sql, schema_name)))
2298 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", schema_name);
2299 13 : if (!mvc_schema_privs(sql, s))
2300 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), schema_name);
2301 13 : if (!(t = mvc_bind_table(sql, s, table_name)))
2302 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", table_name, schema_name);
2303 13 : if (t->system)
2304 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table");
2305 13 : if (isView(t))
2306 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name);
2307 13 : if (isDeclaredTable(t))
2308 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column in a declared table");
2309 13 : if (!(col = mvc_bind_column(sql, t, old_name)))
2310 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name);
2311 13 : if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL))
2312 0 : throw(SQL, "sql.rename_column", SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name);
2313 26 : if (strNil(new_name) || *new_name == '\0')
2314 0 : throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: invalid new column name");
2315 13 : if (mvc_bind_column(sql, t, new_name))
2316 0 : throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name);
2317 :
2318 13 : switch (sql_trans_rename_column(sql->session->tr, t, col->base.id, old_name, new_name)) {
2319 0 : case -1:
2320 0 : throw(SQL,"sql.rename_column", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2321 0 : case -2:
2322 : case -3:
2323 0 : throw(SQL,"sql.rename_column", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2324 : default:
2325 : break;
2326 : }
2327 : return msg;
2328 : }
|