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 25287 : SaveArgReference(MalStkPtr stk, InstrPtr pci, int arg)
51 : {
52 25287 : char *val = *getArgReference_str(stk, pci, arg);
53 :
54 30405 : if (strNil(val))
55 : val = NULL;
56 25287 : return val;
57 : }
58 :
59 : static int
60 2054 : table_has_updates(sql_trans *tr, sql_table *t)
61 : {
62 2054 : node *n;
63 2054 : int cnt = 0;
64 2054 : sqlstore *store = tr->store;
65 :
66 6438 : for ( n = ol_first_node(t->columns); !cnt && n; n = n->next) {
67 4384 : sql_column *c = n->data;
68 :
69 4384 : size_t upd = store->storage_api.count_col( tr, c, 2/* count updates */);
70 4384 : cnt |= upd > 0;
71 : }
72 2054 : return cnt;
73 : }
74 :
75 : static char *
76 534 : rel_check_tables(mvc *sql, sql_table *nt, sql_table *nnt, const char *errtable)
77 : {
78 534 : node *n, *m, *nn, *mm;
79 :
80 534 : 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 1732 : for (n = ol_first_node(nt->columns), m = ol_first_node(nnt->columns); n && m; n = n->next, m = m->next) {
83 1206 : sql_column *nc = n->data;
84 1206 : sql_column *mc = m->data;
85 :
86 1206 : 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 1204 : 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 1201 : if (isRangePartitionTable(nt) || isListPartitionTable(nt)) {
91 425 : 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 526 : 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 524 : if (ol_length(nt->keys))
98 109 : for (n = ol_first_node(nt->keys), m = ol_first_node(nnt->keys); n && m; n = n->next, m = m->next) {
99 59 : sql_key *ni = n->data;
100 59 : sql_key *mi = m->data;
101 :
102 59 : 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 59 : 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 124 : for (nn = ni->columns->h, mm = mi->columns->h; nn && mm; nn = nn->next, mm = mm->next) {
107 67 : sql_kc *nni = nn->data;
108 67 : sql_kc *mmi = mm->data;
109 :
110 67 : 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 522 : n = ol_length(nt->idxs) ? ol_first_node(nt->idxs) : NULL;
117 522 : m = ol_length(nnt->idxs) ? ol_first_node(nnt->idxs) : NULL;
118 530 : for (; n || m; n = n->next, m = m->next) {
119 : sql_idx *ni, *mi;
120 :
121 98 : while (n) {
122 56 : ni = n->data;
123 56 : if ((!hash_index(ni->type) || list_length(ni->columns) > 1) && idx_has_column(ni->type))
124 : break;
125 48 : n = n->next;
126 : }
127 98 : while (m) {
128 56 : mi = m->data;
129 56 : if ((!hash_index(mi->type) || list_length(mi->columns) > 1) && idx_has_column(mi->type))
130 : break;
131 48 : m = m->next;
132 : }
133 :
134 50 : 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 522 : 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 545 : 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 545 : char *msg = MAL_SUCCEED;
165 545 : sql_schema *ms = NULL, *ps = NULL;
166 545 : sql_table *rmt = NULL, *rpt = NULL;
167 :
168 545 : if (!(ms = mvc_bind_schema(sql, msname)))
169 0 : throw(SQL,call,SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", msname);
170 545 : if (!(ps = mvc_bind_schema(sql, psname)))
171 0 : throw(SQL,call,SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", psname);
172 545 : 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 545 : 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 545 : 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 545 : 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 545 : const char *errtable = TABLE_TYPE_DESCRIPTION(rmt->type, rmt->properties);
182 545 : 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 544 : if (isView(rpt))
185 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a view into a %s", errtable);
186 544 : if (isDeclaredTable(rpt))
187 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a declared table into a %s", errtable);
188 544 : if (isTempSchema(rpt->s))
189 0 : throw(SQL,call,SQLSTATE(42000) "ALTER TABLE: can't add a temporary table into a %s", errtable);
190 544 : 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 544 : 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 543 : node *n = members_find_child_id(rmt->members, rpt->base.id);
195 543 : 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 536 : 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 534 : if ((msg = rel_check_tables(sql, rmt, rpt, errtable)) != MAL_SUCCEED)
200 : return msg;
201 :
202 520 : *mt = rmt;
203 520 : *pt = rpt;
204 520 : return MAL_SUCCEED;
205 : }
206 :
207 : static char *
208 290 : alter_table_add_table(mvc *sql, char *msname, char *mtname, char *psname, char *ptname)
209 : {
210 290 : sql_table *mt = NULL, *pt = NULL;
211 290 : str msg = validate_alter_table_add_table(sql, "sql.alter_table_add_table", msname, mtname, psname, ptname, &mt, &pt, 0);
212 :
213 290 : if (msg == MAL_SUCCEED) {
214 280 : 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 280 : 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 280 : 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 202 : 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 202 : sql_table *mt = NULL, *pt = NULL;
237 202 : sql_part *err = NULL;
238 202 : str msg = MAL_SUCCEED, err_min = NULL, err_max = NULL, conflict_err_min = NULL, conflict_err_max = NULL;
239 202 : int tp1 = 0, errcode = 0, min_null = 0, max_null = 0;
240 202 : size_t length = 0;
241 202 : sql_subtype tpe;
242 :
243 202 : 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 188 : } 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 188 : } 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 187 : find_partition_type(&tpe, mt);
258 187 : tp1 = tpe.type->localtype;
259 187 : min_null = ATOMcmp(tp1, min, ATOMnilptr(tp1)) == 0;
260 187 : max_null = ATOMcmp(tp1, max, ATOMnilptr(tp1)) == 0;
261 :
262 187 : 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 185 : 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 182 : 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 164 : errcode = sql_trans_add_range_partition(sql->session->tr, mt, pt, tpe, min, max, with_nills, update, &err);
282 164 : 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 170 : finish:
366 170 : if (err_min)
367 28 : GDKfree(err_min);
368 170 : if (err_max)
369 28 : GDKfree(err_max);
370 170 : if (conflict_err_min)
371 28 : GDKfree(conflict_err_min);
372 170 : if (conflict_err_max)
373 28 : GDKfree(conflict_err_max);
374 : return msg;
375 : }
376 :
377 : static char *
378 53 : 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 53 : sql_table *mt = NULL, *pt = NULL;
382 53 : str msg = MAL_SUCCEED;
383 53 : sql_part *err = NULL;
384 53 : int errcode = 0, i = 0, ninserts = 0;
385 53 : sql_subtype tpe;
386 53 : list *values = NULL;
387 :
388 53 : assert(with_nills == false || with_nills == true); /* No nills allowed here */
389 53 : 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 52 : } 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 52 : } 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 52 : find_partition_type(&tpe, mt);
405 52 : ninserts = pci->argc - pci->retc - 7;
406 52 : 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 52 : 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 49 : values = list_create((fdestroy) &part_value_destroy);
422 195 : for ( i = pci->retc+7; i < pci->argc; i++){
423 147 : sql_part_value *nextv = NULL;
424 147 : ValRecord *vnext = &(stk)->stk[(pci)->argv[i]];
425 147 : ptr pnext = VALget(vnext);
426 147 : size_t len = ATOMlen(vnext->vtype, pnext);
427 :
428 147 : 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 147 : nextv = ZNEW(sql_part_value); /* instantiate the part value */
436 147 : nextv->value = NEW_ARRAY(char, len);
437 147 : memcpy(nextv->value, pnext, len);
438 147 : nextv->length = len;
439 :
440 147 : 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 48 : errcode = sql_trans_add_value_partition(sql->session->tr, mt, pt, tpe, values, with_nills, update, &err);
451 48 : 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 48 : 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 2067 : alter_table_set_access(mvc *sql, char *sname, char *tname, int access)
520 : {
521 2067 : sql_schema *s = NULL;
522 2067 : sql_table *t = NULL;
523 :
524 2067 : 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 2067 : 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 2067 : 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 2067 : 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 2066 : if (t->access != access) {
533 2066 : 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 2066 : 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 336 : 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 336 : sql_trigger *tri = NULL, *other = NULL;
553 336 : sql_schema *s = NULL;
554 336 : sql_table *t = NULL;
555 336 : const char *base = replace ? "CREATE OR REPLACE TRIGGER" : "CREATE TRIGGER";
556 :
557 1007 : if (!strNil(sname) && !strNil(tname)) {
558 335 : if (!(s = mvc_bind_schema(sql, sname)))
559 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: no such schema '%s'", base, sname);
560 335 : 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 335 : if (!(t = mvc_bind_table(sql, s, tname)))
563 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: unknown table '%s'", base, tname);
564 335 : if (isView(t))
565 0 : throw(SQL,"sql.create_trigger",SQLSTATE(3F000) "%s: cannot create trigger on view '%s'", base, tname);
566 : } else {
567 1 : 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 336 : 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 336 : 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 334 : 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 334 : default: {
594 334 : char *buf;
595 334 : sql_rel *r = NULL;
596 334 : sql_allocator *sa = sql->sa;
597 :
598 334 : 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 334 : 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 334 : r = rel_parse(sql, s, buf, m_deps);
608 334 : if (r)
609 329 : r = sql_processrelation(sql, r, 0, 0, 0, 0);
610 329 : if (r) {
611 329 : list *blist = rel_dependencies(sql, r);
612 329 : 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 334 : sa_destroy(sql->sa);
619 334 : sql->sa = sa;
620 334 : 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 81 : drop_trigger(mvc *sql, char *sname, char *tname, int if_exists)
633 : {
634 81 : sql_trigger *tri = NULL;
635 81 : sql_schema *s = NULL;
636 :
637 81 : if (!(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 81 : if (!mvc_schema_privs(sql, s))
643 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);
644 :
645 81 : if ((tri = mvc_bind_trigger(sql, s, tname)) == NULL) {
646 0 : if (if_exists)
647 : return MAL_SUCCEED;
648 0 : throw(SQL,"sql.drop_trigger", SQLSTATE(3F000) "DROP TRIGGER: unknown trigger %s\n", tname);
649 : }
650 81 : switch (mvc_drop_trigger(sql, s, tri)) {
651 0 : case -1:
652 0 : throw(SQL,"sql.drop_trigger",SQLSTATE(HY013) MAL_MALLOC_FAIL);
653 1 : case -2:
654 : case -3:
655 1 : throw(SQL,"sql.drop_trigger",SQLSTATE(42000) "DROP TRIGGER: transaction conflict detected");
656 : default:
657 : break;
658 : }
659 : return MAL_SUCCEED;
660 : }
661 :
662 : char *
663 3542 : drop_table(mvc *sql, char *sname, char *tname, int drop_action, int if_exists)
664 : {
665 3542 : sql_schema *s = NULL;
666 3542 : sql_table *t = NULL;
667 :
668 3542 : if (!(s = mvc_bind_schema(sql, sname))) {
669 0 : if (if_exists)
670 : return MAL_SUCCEED;
671 0 : throw(SQL,"sql.drop_table",SQLSTATE(3F000) "DROP TABLE: no such schema '%s'", sname);
672 : }
673 3542 : if (!(t = mvc_bind_table(sql, s, tname))) {
674 0 : if (if_exists)
675 : return MAL_SUCCEED;
676 0 : throw(SQL,"sql.drop_table", SQLSTATE(42S02) "DROP TABLE: no such table '%s'", tname);
677 : }
678 3542 : if (isView(t))
679 1 : throw(SQL,"sql.drop_table", SQLSTATE(42000) "DROP TABLE: cannot drop VIEW '%s'", tname);
680 3541 : if (t->system)
681 1 : throw(SQL,"sql.drop_table", SQLSTATE(42000) "DROP TABLE: cannot drop system table '%s'", tname);
682 3540 : if (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && t->persistence == SQL_LOCAL_TEMP))
683 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);
684 :
685 3538 : if (!drop_action && t->keys) {
686 4394 : for (node *n = ol_first_node(t->keys); n; n = n->next) {
687 1214 : sql_key *k = n->data;
688 :
689 1214 : if (k->type == ukey || k->type == pkey) {
690 871 : struct os_iter oi;
691 871 : os_iterator(&oi, k->t->s->keys, sql->session->tr, NULL);
692 22584 : for (sql_base *b = oi_next(&oi); b; b=oi_next(&oi)) {
693 21715 : sql_key *fk = (sql_key*)b;
694 21715 : sql_fkey *rk = (sql_fkey*)b;
695 :
696 21715 : if (fk->type != fkey || rk->rkey != k->base.id)
697 21696 : continue;
698 :
699 : /* make sure it is not a self referencing key */
700 19 : if (fk->t != t)
701 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);
702 : }
703 : }
704 : }
705 : }
706 :
707 3180 : if (!drop_action && mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
708 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);
709 :
710 3519 : return mvc_drop_table(sql, s, t, drop_action);
711 : }
712 :
713 : char *
714 237 : drop_view(mvc *sql, char *sname, char *tname, int drop_action, int if_exists)
715 : {
716 237 : sql_table *t = NULL;
717 237 : sql_schema *ss = NULL;
718 :
719 237 : if (!(ss = mvc_bind_schema(sql, sname))) {
720 0 : if (if_exists)
721 : return MAL_SUCCEED;
722 0 : throw(SQL,"sql.drop_view", SQLSTATE(3F000) "DROP VIEW: no such schema '%s'", sname);
723 : }
724 237 : if (!(t = mvc_bind_table(sql, ss, tname))) {
725 0 : if (if_exists)
726 : return MAL_SUCCEED;
727 0 : throw(SQL,"sql.drop_view",SQLSTATE(42S02) "DROP VIEW: unknown view '%s'", tname);
728 : }
729 237 : if (!mvc_schema_privs(sql, ss) && !(isTempSchema(ss) && t && t->persistence == SQL_LOCAL_TEMP))
730 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);
731 237 : if (!isView(t))
732 0 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: unable to drop view '%s': is a table", tname);
733 237 : if (t->system)
734 1 : throw(SQL,"sql.drop_view", SQLSTATE(42000) "DROP VIEW: cannot drop system view '%s'", tname);
735 236 : if (!drop_action && mvc_check_dependency(sql, t->base.id, VIEW_DEPENDENCY, NULL))
736 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);
737 233 : return mvc_drop_table(sql, ss, t, drop_action);
738 : }
739 :
740 : static str
741 146 : drop_key(mvc *sql, char *sname, char *tname, char *kname, int drop_action)
742 : {
743 146 : node *n;
744 146 : sql_schema *s = cur_schema(sql);
745 146 : sql_table *t = NULL;
746 146 : sql_key *key;
747 :
748 146 : if (!(s = mvc_bind_schema(sql, sname)))
749 0 : throw(SQL,"sql.drop_key", SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", sname);
750 146 : if (!mvc_schema_privs(sql, s))
751 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);
752 146 : if (!(t = mvc_bind_table(sql, s, tname)))
753 0 : throw(SQL,"sql.drop_key", SQLSTATE(42S02) "ALTER TABLE: no such table '%s'", tname);
754 146 : if (!(n = ol_find_name(t->keys, kname)))
755 2 : throw(SQL,"sql.drop_key", SQLSTATE(42000) "ALTER TABLE: no such constraint '%s'", kname);
756 144 : key = n->data;
757 144 : if (!drop_action && mvc_check_dependency(sql, key->base.id, KEY_DEPENDENCY, NULL))
758 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);
759 143 : switch (mvc_drop_key(sql, s, key, drop_action)) {
760 0 : case -1:
761 0 : throw(SQL,"sql.drop_key",SQLSTATE(HY013) MAL_MALLOC_FAIL);
762 0 : case -2:
763 : case -3:
764 0 : throw(SQL,"sql.drop_key",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
765 : default:
766 : break;
767 : }
768 : return MAL_SUCCEED;
769 : }
770 :
771 : static str
772 109 : IDXdrop(mvc *sql, const char *sname, const char *tname, const char *iname, void (*func)(BAT *))
773 : {
774 109 : BAT *b = mvc_bind(sql, sname, tname, iname, RDONLY), *nb = NULL;
775 :
776 109 : if (!b)
777 0 : throw(SQL,"sql.drop_index", SQLSTATE(HY005) "Column can not be accessed");
778 109 : if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
779 109 : BBPunfix(b->batCacheid);
780 109 : if (!(b = BATdescriptor(nb->batCacheid)))
781 0 : throw(SQL,"sql.drop_index", SQLSTATE(HY005) "Column can not be accessed");
782 : }
783 :
784 109 : func(b);
785 109 : BBPunfix(b->batCacheid);
786 109 : return MAL_SUCCEED;
787 : }
788 :
789 : static str
790 161 : drop_index(mvc *sql, char *sname, char *iname)
791 : {
792 161 : sql_schema *s = NULL;
793 161 : sql_idx *i = NULL;
794 161 : str msg = MAL_SUCCEED;
795 :
796 161 : if (!(s = mvc_bind_schema(sql, sname)))
797 0 : throw(SQL,"sql.drop_index", SQLSTATE(3F000) "DROP INDEX: no such schema '%s'", sname);
798 161 : if (!mvc_schema_privs(sql, s))
799 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);
800 161 : if (!(i = mvc_bind_idx(sql, s, iname)))
801 0 : throw(SQL,"sql.drop_index", SQLSTATE(42S12) "DROP INDEX: no such index '%s'", iname);
802 161 : if (i->key)
803 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);
804 160 : if (i->type == ordered_idx || i->type == imprints_idx) {
805 109 : sql_kc *ic = i->columns->h->data;
806 109 : sql_class icls = ic->c->type.type->eclass;
807 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))))
808 : return msg;
809 : }
810 160 : switch (mvc_drop_idx(sql, s, i)) {
811 0 : case -1:
812 0 : throw(SQL,"sql.drop_index",SQLSTATE(HY013) MAL_MALLOC_FAIL);
813 0 : case -2:
814 : case -3:
815 0 : throw(SQL,"sql.drop_index",SQLSTATE(42000) "DROP INDEX: transaction conflict detected");
816 : default:
817 : break;
818 : }
819 : return NULL;
820 : }
821 :
822 : static str
823 308 : create_seq(mvc *sql, char *sname, char *seqname, sql_sequence *seq)
824 : {
825 308 : sql_schema *s = NULL;
826 :
827 308 : (void)seqname;
828 308 : if (!(s = mvc_bind_schema(sql, sname)))
829 0 : throw(SQL,"sql.create_seq", SQLSTATE(3F000) "CREATE SEQUENCE: no such schema '%s'", sname);
830 308 : if (!mvc_schema_privs(sql, s))
831 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);
832 308 : if (find_sql_sequence(sql->session->tr, s, seq->base.name))
833 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: name '%s' already in use", seq->base.name);
834 308 : if (is_lng_nil(seq->start) || is_lng_nil(seq->minvalue) || is_lng_nil(seq->maxvalue) ||
835 308 : is_lng_nil(seq->increment) || is_lng_nil(seq->cacheinc) || is_bit_nil(seq->cycle))
836 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence properties must be non-NULL");
837 308 : if (seq->start < seq->minvalue)
838 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: start value is less than the minimum ("LLFMT" < "LLFMT")", seq->start, seq->minvalue);
839 308 : if (seq->start > seq->maxvalue)
840 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: start value is higher than the maximum ("LLFMT" > "LLFMT")", seq->start, seq->maxvalue);
841 308 : if (seq->maxvalue < seq->minvalue)
842 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: maximum value is less than the minimum ("LLFMT" < "LLFMT")", seq->maxvalue, seq->minvalue);
843 308 : if (seq->increment == 0)
844 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence increment cannot be 0");
845 308 : if (seq->cacheinc <= 0)
846 0 : throw(SQL,"sql.create_seq", SQLSTATE(42000) "CREATE SEQUENCE: sequence cache must be positive");
847 308 : lng calc = llabs(seq->increment) * seq->cacheinc;
848 308 : if (calc < llabs(seq->increment) || calc < seq->cacheinc)
849 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");
850 308 : 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)) {
851 0 : case -1:
852 0 : throw(SQL,"sql.create_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
853 0 : case -2:
854 : case -3:
855 0 : throw(SQL,"sql.create_seq",SQLSTATE(42000) "CREATE SEQUENCE: transaction conflict detected");
856 : default:
857 : break;
858 : }
859 : return NULL;
860 : }
861 :
862 : static str
863 42 : alter_seq(mvc *sql, char *sname, char *seqname, sql_sequence *seq, const lng *val)
864 : {
865 42 : sql_schema *s = NULL;
866 42 : sql_sequence *nseq = NULL;
867 :
868 42 : (void)seqname;
869 42 : if (!(s = mvc_bind_schema(sql, sname)))
870 0 : throw(SQL,"sql.alter_seq", SQLSTATE(3F000) "ALTER SEQUENCE: no such schema '%s'", sname);
871 42 : if (!mvc_schema_privs(sql, s))
872 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);
873 42 : if (!(nseq = find_sql_sequence(sql->session->tr, s, seq->base.name)))
874 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: no such sequence '%s'", seq->base.name);
875 : /* if seq properties hold NULL values, then they should be ignored during the update */
876 : /* first alter the known values */
877 42 : switch (sql_trans_alter_sequence(sql->session->tr, nseq, seq->minvalue, seq->maxvalue, seq->increment, seq->cacheinc, seq->cycle)) {
878 0 : case -1:
879 0 : throw(SQL,"sql.alter_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
880 0 : case -2:
881 : case -3:
882 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: transaction conflict detected");
883 : default:
884 42 : break;
885 : }
886 42 : if (nseq->maxvalue < nseq->minvalue)
887 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: maximum value is less than the minimum ("LLFMT" < "LLFMT")", nseq->maxvalue, nseq->minvalue);
888 42 : if (nseq->increment == 0)
889 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence increment cannot be 0");
890 42 : if (nseq->cacheinc <= 0)
891 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence cache must be positive");
892 42 : lng calc = llabs(nseq->increment) * nseq->cacheinc;
893 42 : if (calc < llabs(nseq->increment) || calc < nseq->cacheinc)
894 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");
895 42 : if (val) {
896 42 : if (is_lng_nil(*val))
897 0 : throw(SQL,"sql.alter_seq", SQLSTATE(42000) "ALTER SEQUENCE: sequence value must be non-NULL");
898 42 : if (*val < nseq->minvalue)
899 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);
900 41 : if (*val > nseq->maxvalue)
901 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);
902 40 : switch (sql_trans_sequence_restart(sql->session->tr, nseq, *val)) {
903 0 : case -1:
904 0 : throw(SQL,"sql.alter_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
905 0 : case -2:
906 : case -3:
907 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: transaction conflict detected");
908 0 : case -4:
909 0 : throw(SQL,"sql.alter_seq",SQLSTATE(42000) "ALTER SEQUENCE: failed to restart sequence %s.%s", sname, nseq->base.name);
910 : default:
911 : break;
912 : }
913 : }
914 : return MAL_SUCCEED;
915 : }
916 :
917 : static str
918 32 : drop_seq(mvc *sql, char *sname, char *name)
919 : {
920 32 : sql_schema *s = NULL;
921 32 : sql_sequence *seq = NULL;
922 :
923 32 : if (!(s = mvc_bind_schema(sql, sname)))
924 0 : throw(SQL,"sql.drop_seq", SQLSTATE(3F000) "DROP SEQUENCE: no such schema '%s'", sname);
925 32 : if (!mvc_schema_privs(sql, s))
926 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);
927 32 : if (!(seq = find_sql_sequence(sql->session->tr, s, name)))
928 0 : throw(SQL,"sql.drop_seq", SQLSTATE(42M35) "DROP SEQUENCE: no such sequence '%s'", name);
929 32 : if (mvc_check_dependency(sql, seq->base.id, BEDROPPED_DEPENDENCY, NULL))
930 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);
931 :
932 30 : switch (sql_trans_drop_sequence(sql->session->tr, s, seq, 0)) {
933 0 : case -1:
934 0 : throw(SQL,"sql.drop_seq",SQLSTATE(HY013) MAL_MALLOC_FAIL);
935 0 : case -2:
936 : case -3:
937 0 : throw(SQL,"sql.drop_seq",SQLSTATE(42000) "DROP SEQUENCE: transaction conflict detected");
938 : default:
939 : break;
940 : }
941 : return NULL;
942 : }
943 :
944 : static str
945 654 : drop_func(mvc *sql, char *sname, char *name, sqlid fid, sql_ftype type, int action)
946 : {
947 654 : sql_schema *s = NULL;
948 654 : char *F = NULL, *fn = NULL;
949 654 : int res = 0;
950 :
951 654 : FUNC_TYPE_STR(type, F, fn)
952 :
953 654 : if (!(s = mvc_bind_schema(sql, sname))) {
954 0 : if (fid == -2) /* if exists option */
955 : return MAL_SUCCEED;
956 0 : throw(SQL,"sql.drop_func", SQLSTATE(3F000) "DROP %s: no such schema '%s'", F, sname);
957 : }
958 654 : if (!mvc_schema_privs(sql, s))
959 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);
960 654 : if (fid >= 0) {
961 626 : sql_base *b = os_find_id(s->funcs, sql->session->tr, fid);
962 626 : if (b) {
963 626 : sql_func *func = (sql_func*)b;
964 :
965 666 : if (!action && mvc_check_dependency(sql, func->base.id, !IS_PROC(func) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, NULL))
966 4 : throw(SQL,"sql.drop_func", SQLSTATE(42000) "DROP %s: there are database objects dependent on %s %s;", F, fn, func->base.name);
967 622 : res = mvc_drop_func(sql, s, func, action);
968 : }
969 28 : } else if (fid == -2) { /* if exists option */
970 : return MAL_SUCCEED;
971 : } else { /* fid == -1 */
972 28 : list *list_func = sql_find_funcs_by_name(sql, s->base.name, name, type, false);
973 :
974 28 : if (!list_empty(list_func))
975 68 : for (node *n = list_func->h; n; n = n->next) {
976 40 : sql_func *func = n->data;
977 :
978 53 : if (!action && mvc_check_dependency(sql, func->base.id, !IS_PROC(func) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, list_func)) {
979 0 : list_destroy(list_func);
980 0 : throw(SQL,"sql.drop_func", SQLSTATE(42000) "DROP %s: there are database objects dependent on %s %s;", F, fn, func->base.name);
981 : }
982 : }
983 28 : res = mvc_drop_all_func(sql, s, list_func, action);
984 28 : list_destroy(list_func);
985 : }
986 :
987 650 : switch (res) {
988 0 : case -1:
989 0 : throw(SQL,"sql.drop_func",SQLSTATE(HY013) MAL_MALLOC_FAIL);
990 0 : case -2:
991 : case -3:
992 0 : throw(SQL,"sql.drop_func",SQLSTATE(42000) "DROP %s: transaction conflict detected", F);
993 : default:
994 : break;
995 : }
996 : return MAL_SUCCEED;
997 : }
998 :
999 : static int
1000 6 : args_cmp(sql_arg *a1, sql_arg *a2)
1001 : {
1002 6 : if (a1->inout != a2->inout)
1003 : return -1;
1004 6 : if (strcmp(a1->name, a2->name) != 0)
1005 : return -1;
1006 6 : return subtype_cmp(&a1->type, &a2->type);
1007 : }
1008 :
1009 : static char *
1010 114393 : create_func(mvc *sql, char *sname, char *fname, sql_func *f, int replace)
1011 : {
1012 114393 : sql_func *nf;
1013 114393 : sql_subfunc *sf;
1014 114393 : sql_schema *s = NULL;
1015 114393 : char *F = NULL, *fn = NULL, *base = replace ? "CREATE OR REPLACE" : "CREATE";
1016 :
1017 114393 : FUNC_TYPE_STR(f->type, F, fn)
1018 :
1019 114393 : (void) fn;
1020 114393 : if (!(s = mvc_bind_schema(sql, sname)))
1021 0 : throw(SQL,"sql.create_func", SQLSTATE(3F000) "%s %s: no such schema '%s'", base, F, sname);
1022 114393 : if (!mvc_schema_privs(sql, s))
1023 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);
1024 :
1025 114393 : if (replace) {
1026 66 : list *tl = sa_list(sql->sa);
1027 66 : if (!list_empty(f->ops)) {
1028 79 : for (node *n = f->ops->h ; n ; n = n->next ) {
1029 45 : sql_arg *arg = n->data;
1030 :
1031 45 : list_append(tl, &arg->type);
1032 : }
1033 : }
1034 :
1035 66 : if ((sf = sql_bind_func_(sql, s->base.name, fname, tl, f->type, false)) != NULL) {
1036 15 : sql_func *sff = sf->func;
1037 :
1038 15 : if (!sff->s || sff->system)
1039 0 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: not allowed to replace system %s %s;", base, F, fn, sff->base.name);
1040 :
1041 : /* if all function parameters are the same, return */
1042 15 : if (sff->lang == f->lang && sff->type == f->type &&
1043 15 : sff->varres == f->varres && sff->vararg == f->vararg &&
1044 18 : ((!sff->query && !f->query) || (sff->query && f->query && strcmp(sff->query, f->query) == 0)) &&
1045 6 : list_cmp(sff->res, f->res, (fcmp) &args_cmp) == 0 &&
1046 3 : list_cmp(sff->ops, f->ops, (fcmp) &args_cmp) == 0)
1047 : return MAL_SUCCEED;
1048 :
1049 12 : if (mvc_check_dependency(sql, sff->base.id, !IS_PROC(sff) ? FUNC_DEPENDENCY : PROC_DEPENDENCY, NULL))
1050 0 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: there are database objects dependent on %s %s;", base, F, fn, sff->base.name);
1051 12 : switch (mvc_drop_func(sql, s, sff, 0)) {
1052 0 : case -1:
1053 0 : throw(SQL,"sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1054 1 : case -2:
1055 : case -3:
1056 1 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: transaction conflict detected", base, F);
1057 : default:
1058 : break;
1059 : }
1060 : } else {
1061 51 : sql->session->status = 0; /* if the function was not found clean the error */
1062 51 : sql->errstr[0] = '\0';
1063 : }
1064 : }
1065 114389 : 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)) {
1066 0 : case -1:
1067 0 : throw(SQL,"sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1068 1 : case -2:
1069 : case -3:
1070 1 : throw(SQL,"sql.create_func", SQLSTATE(42000) "%s %s: transaction conflict detected", base, F);
1071 : default:
1072 114388 : break;
1073 : }
1074 114388 : switch (nf->lang) {
1075 106646 : case FUNC_LANG_MAL:
1076 106646 : assert(nf->imp);
1077 106646 : nf->instantiated = TRUE; /* MAL functions get instantiated while being created */
1078 : /* fall through */
1079 114174 : case FUNC_LANG_SQL: {
1080 114174 : char *buf;
1081 114174 : sql_rel *r = NULL;
1082 114174 : sql_allocator *sa = sql->sa;
1083 :
1084 114174 : assert(nf->query);
1085 114174 : if (!(sql->sa = sa_create(sql->pa))) {
1086 0 : sql->sa = sa;
1087 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1088 : }
1089 114174 : if (!(buf = sa_strdup(sql->sa, nf->query))) {
1090 0 : sa_destroy(sql->sa);
1091 0 : sql->sa = sa;
1092 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1093 : }
1094 114174 : r = rel_parse(sql, s, buf, m_deps);
1095 114174 : if (r)
1096 114174 : r = sql_processrelation(sql, r, 0, 0, 0, 0);
1097 114174 : if (r) {
1098 114174 : node *n;
1099 114174 : list *blist = rel_dependencies(sql, r);
1100 :
1101 114174 : if (!f->vararg && f->ops) {
1102 292684 : for (n = f->ops->h; n; n = n->next) {
1103 178510 : sql_arg *a = n->data;
1104 :
1105 178510 : if (a->type.type->s && mvc_create_dependency(sql, &a->type.type->base, nf->base.id, TYPE_DEPENDENCY)) {
1106 0 : sa_destroy(sql->sa);
1107 0 : sql->sa = sa;
1108 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1109 : }
1110 : }
1111 : }
1112 114174 : if (!f->varres && f->res) {
1113 245073 : for (n = f->res->h; n; n = n->next) {
1114 148849 : sql_arg *a = n->data;
1115 :
1116 148849 : if (a->type.type->s && mvc_create_dependency(sql, &a->type.type->base, nf->base.id, TYPE_DEPENDENCY)) {
1117 0 : sa_destroy(sql->sa);
1118 0 : sql->sa = sa;
1119 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1120 : }
1121 : }
1122 : }
1123 128743 : if (mvc_create_dependencies(sql, blist, nf->base.id, !IS_PROC(f) ? FUNC_DEPENDENCY : PROC_DEPENDENCY)) {
1124 0 : sa_destroy(sql->sa);
1125 0 : sql->sa = sa;
1126 0 : throw(SQL, "sql.create_func", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1127 : }
1128 : }
1129 114174 : sa_destroy(sql->sa);
1130 114174 : sql->sa = sa;
1131 114174 : if (!r) {
1132 0 : if (strlen(sql->errstr) > 6 && sql->errstr[5] == '!')
1133 0 : throw(SQL, "sql.create_func", "%s", sql->errstr);
1134 : else
1135 0 : throw(SQL, "sql.create_func", SQLSTATE(42000) "%s", sql->errstr);
1136 : }
1137 : }
1138 : default:
1139 : break;
1140 : }
1141 : return MAL_SUCCEED;
1142 : }
1143 :
1144 : str
1145 1342 : alter_table(Client cntxt, mvc *sql, char *sname, sql_table *t)
1146 : {
1147 1342 : sql_schema *s = NULL;
1148 1342 : sql_table *nt = NULL;
1149 1342 : node *n;
1150 :
1151 1342 : if (!(s = mvc_bind_schema(sql, sname)))
1152 0 : throw(SQL,"sql.alter_table", SQLSTATE(3F000) "ALTER TABLE: no such schema '%s'", sname);
1153 1342 : if (!mvc_schema_privs(sql, s) && !(isTempSchema(s) && t->persistence == SQL_LOCAL_TEMP))
1154 0 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "ALTER TABLE: insufficient privileges for user '%s' in schema '%s'", get_string_global_var(sql, "current_user"), s->base.name);
1155 1342 : if (!(nt = mvc_bind_table(sql, s, t->base.name)))
1156 0 : throw(SQL,"sql.alter_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s'", t->base.name);
1157 :
1158 1342 : sql_table *gt = NULL;
1159 1342 : if (nt && isTempTable(nt)) {
1160 34 : gt = (sql_table*)os_find_id(s->tables, sql->session->tr, nt->base.id);
1161 34 : if (gt)
1162 1342 : nt = gt;
1163 : }
1164 :
1165 : /* First check if all the changes are allowed */
1166 1342 : if (t->idxs) {
1167 : /* only one pkey */
1168 1342 : if (nt->pkey) {
1169 1414 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1170 696 : sql_idx *i = n->data;
1171 696 : if (!i->base.new || i->base.deleted)
1172 0 : continue;
1173 696 : if (i->key && i->key->type == pkey)
1174 3 : throw(SQL,"sql.alter_table", SQLSTATE(40000) "CONSTRAINT PRIMARY KEY: a table can have only one PRIMARY KEY\n");
1175 : }
1176 : }
1177 : }
1178 :
1179 21014 : for (n = ol_first_node(t->columns); n; n = n->next) {
1180 : /* null or default value changes */
1181 19796 : sql_column *c = n->data;
1182 :
1183 19796 : if (c->base.new)
1184 : break;
1185 :
1186 19690 : sql_column *nc = mvc_bind_column(sql, nt, c->base.name);
1187 19690 : if (c->base.deleted) {
1188 68 : switch (mvc_drop_column(sql, nt, nc, c->drop_action)) {
1189 0 : case -1:
1190 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1191 1 : case -2:
1192 : case -3:
1193 1 : throw(SQL,"sql.alter_table",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
1194 : default:
1195 67 : break;
1196 : }
1197 67 : continue;
1198 : }
1199 19622 : if (c->null != nc->null && isTable(nt)) {
1200 99 : if (c->null && nt->pkey) { /* check for primary keys based on this column */
1201 2 : node *m;
1202 4 : for (m = nt->pkey->k.columns->h; m; m = m->next) {
1203 4 : sql_kc *kc = m->data;
1204 :
1205 4 : if (kc->c->base.id == c->base.id)
1206 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);
1207 : }
1208 : }
1209 97 : switch (mvc_null(sql, nc, c->null)) {
1210 0 : case -1:
1211 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1212 1 : case -2:
1213 : case -3:
1214 1 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "NOT NULL CONSTRAINT: transaction conflict detected");
1215 : default:
1216 96 : break;
1217 : }
1218 : /* for non empty check for nulls */
1219 96 : sqlstore *store = sql->session->tr->store;
1220 96 : if (c->null == 0) {
1221 94 : const void *nilptr = ATOMnilptr(c->type.type->localtype);
1222 94 : rids *nils = store->table_api.rids_select(sql->session->tr, nc, nilptr, NULL, NULL);
1223 94 : if (!nils)
1224 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1225 94 : int has_nils = !is_oid_nil(store->table_api.rids_next(nils));
1226 :
1227 94 : store->table_api.rids_destroy(nils);
1228 94 : if (has_nils)
1229 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);
1230 : }
1231 : }
1232 19608 : if ((c->def || nc->def) && (!c->def || !nc->def || strcmp(c->def, nc->def) != 0)) {
1233 19 : switch (mvc_default(sql, nc, c->def)) {
1234 0 : case -1:
1235 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1236 0 : case -2:
1237 : case -3:
1238 0 : throw(SQL,"sql.alter_table", SQLSTATE(42000) "DEFAULT: transaction conflict detected");
1239 : default:
1240 : break;
1241 : }
1242 : }
1243 :
1244 19608 : if ((c->storage_type || nc->storage_type) && (!c->storage_type || !nc->storage_type || strcmp(c->storage_type, nc->storage_type) != 0)) {
1245 0 : if (c->t->access == TABLE_WRITABLE)
1246 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);
1247 0 : switch (mvc_storage(sql, nc, c->storage_type)) {
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) "ALTER TABLE: SET STORAGE transaction conflict detected");
1253 : default:
1254 : break;
1255 : }
1256 : }
1257 : }
1258 : /* handle new columns */
1259 1428 : for (; n; n = n->next) {
1260 : /* propagate alter table .. add column */
1261 106 : sql_column *c = n->data;
1262 :
1263 106 : if (c->base.deleted) /* skip */
1264 0 : continue;
1265 106 : switch (mvc_copy_column(sql, nt, c, NULL)) {
1266 0 : case -1:
1267 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1268 2 : case -2:
1269 : case -3:
1270 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);
1271 : default:
1272 : break;
1273 : }
1274 : }
1275 1322 : if (t->idxs) {
1276 : /* alter drop index */
1277 1322 : if (t->idxs)
1278 2421 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1279 1099 : sql_idx *i = n->data;
1280 1099 : if (i->base.new || !i->base.deleted)
1281 1099 : continue;
1282 0 : sql_idx *ni = mvc_bind_idx(sql, s, i->base.name);
1283 0 : if (ni == NULL)
1284 0 : throw(SQL, "sql.alter_table", "Couldn't bind index %s", i->base.name);
1285 0 : switch (mvc_drop_idx(sql, s, ni)) {
1286 0 : case -1:
1287 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1288 0 : case -2:
1289 : case -3:
1290 0 : throw(SQL,"sql.alter_table",SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
1291 : default:
1292 : break;
1293 : }
1294 : }
1295 : /* alter add index */
1296 2404 : for (n = ol_first_node(t->idxs); n; n = n->next) {
1297 1099 : sql_idx *i = n->data;
1298 1099 : BAT *b = NULL, *nb = NULL;
1299 :
1300 1099 : if (!i->base.new || i->base.deleted)
1301 0 : continue;
1302 :
1303 1099 : if (i->type == ordered_idx) {
1304 75 : sql_kc *ic = i->columns->h->data;
1305 75 : if (!(b = mvc_bind(sql, nt->s->base.name, nt->base.name, ic->c->base.name, RDONLY)))
1306 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access ordered index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1307 75 : if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
1308 75 : BBPunfix(b->batCacheid);
1309 75 : if (!(b = BATdescriptor(nb->batCacheid)))
1310 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access ordered index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1311 : }
1312 75 : char *msg = OIDXcreateImplementation(cntxt, newBatType(b->ttype), b, -1);
1313 75 : BBPunfix(b->batCacheid);
1314 75 : if (msg != MAL_SUCCEED) {
1315 0 : char *smsg = createException(SQL,"sql.alter_table", SQLSTATE(40002) "CREATE ORDERED INDEX: %s", msg);
1316 0 : freeException(msg);
1317 0 : return smsg;
1318 : }
1319 1024 : } else if (i->type == imprints_idx) {
1320 69 : gdk_return r;
1321 69 : sql_kc *ic = i->columns->h->data;
1322 69 : if (!(b = mvc_bind(sql, nt->s->base.name, nt->base.name, ic->c->base.name, RDONLY)))
1323 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access imprints index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1324 69 : if (VIEWtparent(b) && (nb = BBP_desc(VIEWtparent(b)))) {
1325 69 : BBPunfix(b->batCacheid);
1326 69 : if (!(b = BATdescriptor(nb->batCacheid)))
1327 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY005) "Cannot access imprints index %s_%s_%s", s->base.name, t->base.name, i->base.name);
1328 : }
1329 69 : if(b->ttype == TYPE_str) {
1330 11 : if (t->access != TABLE_READONLY) {
1331 5 : BBPunfix(b->batCacheid);
1332 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);
1333 : }
1334 :
1335 : /* We signal that we want a strimp on b. It will be created the next time it is needed, i.e. by
1336 : * PCRElikeselect.
1337 : */
1338 6 : r = BATsetstrimps(b);
1339 : }
1340 : else {
1341 58 : r = BATimprints(b);
1342 : }
1343 :
1344 64 : BBPunfix(b->batCacheid);
1345 64 : if (r != GDK_SUCCEED)
1346 9 : throw(SQL, "sql.alter_table", GDK_EXCEPTION);
1347 : }
1348 1085 : switch (mvc_copy_idx(sql, nt, i, NULL)) {
1349 0 : case -1:
1350 0 : throw(SQL,"sql.alter_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1351 3 : case -2:
1352 : case -3:
1353 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);
1354 : default:
1355 : break;
1356 : }
1357 : }
1358 : }
1359 1305 : if (t->keys) {
1360 : /* alter drop key */
1361 2073 : for (n = ol_first_node(t->keys); n; n = n->next) {
1362 768 : sql_key *k = n->data;
1363 :
1364 768 : if ((!k->base.new && !k->base.deleted) || (k->base.new && k->base.deleted))
1365 0 : continue;
1366 768 : if (k->base.deleted) {
1367 0 : sql_key *nk = mvc_bind_key(sql, s, k->base.name);
1368 0 : if (nk) {
1369 0 : switch (mvc_drop_key(sql, s, nk, k->drop_action)) {
1370 0 : case -1:
1371 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1372 0 : case -2:
1373 : case -3:
1374 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);
1375 : default:
1376 : break;
1377 : }
1378 : }
1379 : } else { /* new */
1380 768 : str err;
1381 768 : if ((err = sql_partition_validate_key(sql, t, k, "ALTER")))
1382 0 : return err;
1383 768 : switch (mvc_copy_key(sql, nt, k, NULL)) {
1384 0 : case -1:
1385 0 : throw(SQL,"sql.alter_table",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1386 0 : case -2:
1387 : case -3:
1388 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);
1389 : default:
1390 : break;
1391 : }
1392 : }
1393 : }
1394 : }
1395 : return MAL_SUCCEED;
1396 : }
1397 :
1398 : /* the MAL wrappers */
1399 : str
1400 308 : SQLcreate_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1401 308 : { mvc *sql = NULL;
1402 308 : str msg;
1403 308 : str sname = *getArgReference_str(stk, pci, 1);
1404 308 : str seqname = *getArgReference_str(stk, pci, 2);
1405 308 : sql_sequence *s = *(sql_sequence **) getArgReference(stk, pci, 3);
1406 :
1407 308 : initcontext();
1408 308 : msg = create_seq(sql, sname, seqname, s);
1409 308 : return msg;
1410 : }
1411 :
1412 : str
1413 43 : SQLalter_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1414 43 : { mvc *sql = NULL;
1415 43 : str msg = MAL_SUCCEED;
1416 43 : str sname = *getArgReference_str(stk, pci, 1);
1417 43 : str seqname = *getArgReference_str(stk, pci, 2);
1418 43 : sql_sequence *s = *(sql_sequence **) getArgReference(stk, pci, 3);
1419 43 : lng *val = NULL;
1420 43 : BAT *b = NULL;
1421 43 : BATiter bi = {0};
1422 :
1423 43 : initcontext();
1424 43 : if (getArgType(mb, pci, 4) == TYPE_lng)
1425 43 : val = getArgReference_lng(stk, pci, 4);
1426 0 : else if (isaBatType(getArgType(mb, pci, 4))) {
1427 0 : bat *bid = getArgReference_bat(stk, pci, 4);
1428 :
1429 0 : if (!(b = BATdescriptor(*bid)))
1430 0 : throw(SQL, "sql.alter_seq", SQLSTATE(HY005) "Cannot access column descriptor");
1431 0 : if (BATcount(b) != 1) {
1432 0 : BBPunfix(b->batCacheid);
1433 0 : throw(SQL, "sql.alter_seq", SQLSTATE(42000) "Only one value allowed to alter a sequence value");
1434 : }
1435 0 : bi = bat_iterator(b);
1436 0 : if (getBatType(getArgType(mb, pci, 4)) == TYPE_lng) {
1437 0 : val = (lng*)bi.base;
1438 : }
1439 : }
1440 :
1441 43 : if (val == NULL || is_lng_nil(*val))
1442 1 : msg = createException(SQL,"sql.alter_seq", SQLSTATE(42M36) "ALTER SEQUENCE: cannot (re)start with NULL");
1443 : else
1444 42 : msg = alter_seq(sql, sname, seqname, s, val);
1445 :
1446 43 : if (b) {
1447 0 : bat_iterator_end(&bi);
1448 0 : BBPunfix(b->batCacheid);
1449 : }
1450 : return msg;
1451 : }
1452 :
1453 : str
1454 32 : SQLdrop_seq(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1455 32 : { mvc *sql = NULL;
1456 32 : str msg = MAL_SUCCEED;
1457 32 : str sname = *getArgReference_str(stk, pci, 1);
1458 32 : str name = *getArgReference_str(stk, pci, 2);
1459 :
1460 32 : initcontext();
1461 32 : msg = drop_seq(sql, sname, name);
1462 32 : return msg;
1463 : }
1464 :
1465 : str
1466 1075 : SQLcreate_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1467 1075 : { mvc *sql = NULL;
1468 1075 : str msg = MAL_SUCCEED;
1469 1075 : str sname = *getArgReference_str(stk, pci, 1);
1470 1075 : str name = SaveArgReference(stk, pci, 2);
1471 1075 : sqlid auth_id;
1472 :
1473 1075 : initcontext();
1474 1075 : auth_id = sql->role_id;
1475 1110 : if (!strNil(name) && (auth_id = sql_find_auth(sql, name)) < 0)
1476 0 : throw(SQL,"sql.create_schema", SQLSTATE(42M32) "CREATE SCHEMA: no such authorization '%s'", name);
1477 1075 : if (sql->user_id != USER_MONETDB && sql->role_id != ROLE_SYSADMIN)
1478 0 : throw(SQL,"sql.create_schema", SQLSTATE(42000) "CREATE SCHEMA: insufficient privileges for user '%s'", get_string_global_var(sql, "current_user"));
1479 1075 : if (mvc_bind_schema(sql, sname))
1480 0 : throw(SQL,"sql.create_schema", SQLSTATE(3F000) "CREATE SCHEMA: name '%s' already in use", sname);
1481 1075 : switch (mvc_create_schema(sql, sname, auth_id, sql->user_id)) {
1482 0 : case -1:
1483 0 : throw(SQL,"sql.create_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1484 1 : case -2:
1485 : case -3:
1486 1 : throw(SQL,"sql.create_schema",SQLSTATE(42000) "CREATE SCHEMA: transaction conflict detected");
1487 : default:
1488 : break;
1489 : }
1490 : return msg;
1491 : }
1492 :
1493 : str
1494 184 : SQLdrop_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1495 : {
1496 184 : mvc *sql = NULL;
1497 184 : str msg = MAL_SUCCEED;
1498 184 : str sname = *getArgReference_str(stk, pci, 1);
1499 184 : int if_exists = *getArgReference_int(stk, pci, 2);
1500 184 : int action = *getArgReference_int(stk, pci, 3);
1501 184 : sql_schema *s;
1502 :
1503 184 : initcontext();
1504 184 : s = mvc_bind_schema(sql, sname);
1505 184 : if (!s) {
1506 11 : if (!if_exists)
1507 7 : throw(SQL,"sql.drop_schema",SQLSTATE(3F000) "DROP SCHEMA: name %s does not exist", sname);
1508 : return MAL_SUCCEED;
1509 : }
1510 173 : sql_trans *tr = sql->session->tr;
1511 173 : sql_schema *cur = cur_schema(sql);
1512 :
1513 173 : if (!mvc_schema_privs(sql, s))
1514 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);
1515 173 : if (cur && s->base.id == cur->base.id)
1516 1 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: cannot drop current schema");
1517 172 : if (s->system)
1518 8 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: access denied for '%s'", sname);
1519 164 : if (sql_schema_has_user(sql, s))
1520 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);
1521 235 : if (!action /* RESTRICT */ && (
1522 155 : os_size(s->tables, tr) || os_size(s->types, tr) || os_size(s->funcs, tr) || os_size(s->seqs, tr)))
1523 3 : throw(SQL,"sql.drop_schema",SQLSTATE(2BM37) "DROP SCHEMA: unable to drop schema '%s' (there are database objects which depend on it)", sname);
1524 :
1525 153 : switch (mvc_drop_schema(sql, s, action)) {
1526 0 : case -1:
1527 0 : throw(SQL,"sql.drop_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1528 0 : case -2:
1529 : case -3:
1530 0 : throw(SQL,"sql.drop_schema",SQLSTATE(42000) "DROP SCHEMA: transaction conflict detected");
1531 : default:
1532 : break;
1533 : }
1534 : return MAL_SUCCEED;
1535 : }
1536 :
1537 : str
1538 9606 : SQLcreate_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1539 9606 : { mvc *sql = NULL;
1540 9606 : str msg;
1541 9606 : str sname = *getArgReference_str(stk, pci, 1);
1542 : //str tname = *getArgReference_str(stk, pci, 2);
1543 9606 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1544 9606 : int temp = *getArgReference_int(stk, pci, 4), remote = (pci->argc == 7);
1545 9606 : int pw_encrypted = temp;
1546 :
1547 9606 : initcontext();
1548 9598 : if (remote)
1549 94 : temp = 0;
1550 9598 : msg = create_table_or_view(sql, sname, t->base.name, t, temp, 0);
1551 9598 : if (!msg && remote) {
1552 94 : str username = *getArgReference_str(stk, pci, 5);
1553 94 : str password = *getArgReference_str(stk, pci, 6);
1554 :
1555 94 : sql_schema *s = mvc_bind_schema(sql, sname);
1556 94 : t = s?mvc_bind_table(sql, s, t->base.name):NULL;
1557 94 : if (t)
1558 94 : return remote_create(sql, t->base.id, username, password, pw_encrypted);
1559 0 : throw(SQL, "sql.create_table", SQLSTATE(3F000) "Internal error");
1560 : }
1561 : return msg;
1562 : }
1563 :
1564 : str
1565 22182 : SQLcreate_view(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1566 22182 : { mvc *sql = NULL;
1567 22182 : str msg;
1568 22182 : str sname = *getArgReference_str(stk, pci, 1);
1569 : //str vname = *getArgReference_str(stk, pci, 2);
1570 22182 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1571 22182 : int temp = *getArgReference_int(stk, pci, 4);
1572 22182 : int replace = *getArgReference_int(stk, pci, 5);
1573 :
1574 22182 : initcontext();
1575 22181 : msg = create_table_or_view(sql, sname, t->base.name, t, temp, replace);
1576 22181 : return msg;
1577 : }
1578 :
1579 : str
1580 3542 : SQLdrop_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1581 3542 : { mvc *sql = NULL;
1582 3542 : str msg;
1583 3542 : str sname = *getArgReference_str(stk, pci, 1);
1584 3542 : str name = *getArgReference_str(stk, pci, 2);
1585 3542 : int if_exists = *getArgReference_int(stk, pci, 3);
1586 3542 : int action = *getArgReference_int(stk, pci, 4);
1587 :
1588 3542 : initcontext();
1589 3542 : msg = drop_table(sql, sname, name, action, if_exists);
1590 3542 : return msg;
1591 : }
1592 :
1593 : str
1594 237 : SQLdrop_view(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1595 237 : { mvc *sql = NULL;
1596 237 : str msg;
1597 237 : str sname = *getArgReference_str(stk, pci, 1);
1598 237 : str name = *getArgReference_str(stk, pci, 2);
1599 237 : int if_exists = *getArgReference_int(stk, pci, 3);
1600 237 : int action = *getArgReference_int(stk, pci, 4);
1601 :
1602 237 : initcontext();
1603 237 : msg = drop_view(sql, sname, name, action, if_exists);
1604 237 : return msg;
1605 : }
1606 :
1607 : str
1608 146 : SQLdrop_constraint(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1609 146 : { mvc *sql = NULL;
1610 146 : str msg;
1611 146 : str sname = *getArgReference_str(stk, pci, 1);
1612 146 : str tname = *getArgReference_str(stk, pci, 2);
1613 146 : str kname = *getArgReference_str(stk, pci, 3);
1614 146 : int action = *getArgReference_int(stk, pci, 5);
1615 146 : (void) *getArgReference_int(stk, pci, 4); //the if_exists parameter is also passed but not used
1616 :
1617 146 : initcontext();
1618 146 : msg = drop_key(sql, sname, tname, kname, action);
1619 146 : return msg;
1620 : }
1621 :
1622 : str
1623 1342 : SQLalter_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1624 1342 : { mvc *sql = NULL;
1625 1342 : str msg;
1626 1342 : str sname = *getArgReference_str(stk, pci, 1);
1627 1342 : str tname = *getArgReference_str(stk, pci, 2);
1628 1342 : sql_table *t = *(sql_table **) getArgReference(stk, pci, 3);
1629 :
1630 1342 : (void)tname;
1631 1342 : initcontext();
1632 1342 : msg = alter_table(cntxt, sql, sname, t);
1633 1342 : return msg;
1634 : }
1635 :
1636 : str
1637 898 : SQLcreate_type(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1638 898 : { mvc *sql = NULL;
1639 898 : str msg;
1640 898 : str sname = *getArgReference_str(stk, pci, 1);
1641 898 : char *name = *getArgReference_str(stk, pci, 2);
1642 898 : char *impl = *getArgReference_str(stk, pci, 3);
1643 898 : sql_schema *s = NULL;
1644 :
1645 898 : initcontext();
1646 :
1647 898 : if (!(s = mvc_bind_schema(sql, sname)))
1648 0 : throw(SQL,"sql.create_type",SQLSTATE(3F000) "CREATE TYPE: no such schema '%s'", sname);
1649 898 : if (!mvc_schema_privs(sql, s))
1650 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);
1651 898 : if (schema_bind_type(sql, s, name))
1652 0 : throw(SQL,"sql.create_type", SQLSTATE(42S02) "CREATE TYPE: type '%s' already exists", name);
1653 898 : switch (mvc_create_type(sql, s, name, 0, 0, 0, impl)) {
1654 0 : case -1:
1655 0 : throw(SQL,"sql.create_type", SQLSTATE(HY013) MAL_MALLOC_FAIL);
1656 1 : case -2:
1657 : case -3:
1658 1 : throw(SQL,"sql.create_type", SQLSTATE(42000) "CREATE TYPE: transaction conflict detected");
1659 0 : case -4:
1660 0 : throw(SQL,"sql.create_type", SQLSTATE(0D000) "CREATE TYPE: unknown external type '%s'", impl);
1661 : default:
1662 : break;
1663 : }
1664 : return msg;
1665 : }
1666 :
1667 : str
1668 4 : SQLdrop_type(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1669 4 : { mvc *sql = NULL;
1670 4 : str msg;
1671 4 : str sname = *getArgReference_str(stk, pci, 1);
1672 4 : char *name = *getArgReference_str(stk, pci, 2);
1673 4 : int drop_action = *getArgReference_int(stk, pci, 3);
1674 4 : sql_schema *s = NULL;
1675 4 : sql_type *t;
1676 :
1677 4 : initcontext();
1678 :
1679 4 : if (!(s = mvc_bind_schema(sql, sname)))
1680 0 : throw(SQL,"sql.drop_type",SQLSTATE(3F000) "DROP TYPE: no such schema '%s'", sname);
1681 4 : if (!mvc_schema_privs(sql, s))
1682 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);
1683 4 : if (!(t = schema_bind_type(sql, s, name)))
1684 0 : throw(SQL,"sql.drop_type", SQLSTATE(3F000) "DROP TYPE: type '%s' does not exist", name);
1685 4 : if (!drop_action && mvc_check_dependency(sql, t->base.id, TYPE_DEPENDENCY, NULL))
1686 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);
1687 3 : switch (mvc_drop_type(sql, s, t, drop_action)) {
1688 0 : case -1:
1689 0 : throw(SQL,"sql.drop_type",SQLSTATE(HY013) MAL_MALLOC_FAIL);
1690 0 : case -2:
1691 : case -3:
1692 0 : throw(SQL,"sql.drop_type",SQLSTATE(42000) "DROP TYPE: transaction conflict detected");
1693 : default:
1694 : break;
1695 : }
1696 : return msg;
1697 : }
1698 :
1699 : str
1700 38 : SQLgrant_roles(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1701 38 : { mvc *sql = NULL;
1702 38 : str msg;
1703 38 : str sname = *getArgReference_str(stk, pci, 1);
1704 38 : char *auth = SaveArgReference(stk, pci, 2);
1705 38 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 3);
1706 38 : int admin = *getArgReference_int(stk, pci, 4);
1707 :
1708 38 : initcontext();
1709 38 : msg = sql_grant_role(sql, sname /*grantee */ , auth, grantor, admin);
1710 38 : return msg;
1711 : }
1712 :
1713 : str
1714 10 : SQLrevoke_roles(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1715 10 : { mvc *sql = NULL;
1716 10 : str msg;
1717 10 : str sname = *getArgReference_str(stk, pci, 1);
1718 10 : char *auth = SaveArgReference(stk, pci, 2);
1719 10 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 3);
1720 10 : int admin = *getArgReference_int(stk, pci, 4);
1721 :
1722 10 : initcontext();
1723 10 : msg = sql_revoke_role(sql, sname /*grantee */ , auth, grantor, admin);
1724 10 : return msg;
1725 : }
1726 :
1727 : str
1728 17872 : SQLgrant(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1729 17872 : { mvc *sql = NULL;
1730 17872 : str msg;
1731 17872 : str sname = *getArgReference_str(stk, pci, 1);
1732 17872 : char *tname = *getArgReference_str(stk, pci, 2);
1733 17872 : char *grantee = *getArgReference_str(stk, pci, 3);
1734 17872 : int privs = *getArgReference_int(stk, pci, 4);
1735 17872 : char *cname = SaveArgReference(stk, pci, 5);
1736 17872 : int grant = *getArgReference_int(stk, pci, 6);
1737 17872 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 7);
1738 :
1739 17872 : initcontext();
1740 17872 : if (strNil(tname))
1741 8 : msg = sql_grant_global_privs(sql, grantee, privs, grant, grantor);
1742 : else
1743 17864 : msg = sql_grant_table_privs(sql, grantee, privs, sname, tname, cname, grant, grantor);
1744 : return msg;
1745 : }
1746 :
1747 15 : str SQLrevoke(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1748 15 : { mvc *sql = NULL;
1749 15 : str msg;
1750 15 : str sname = *getArgReference_str(stk, pci, 1);
1751 15 : char *tname = *getArgReference_str(stk, pci, 2);
1752 15 : char *grantee = *getArgReference_str(stk, pci, 3);
1753 15 : int privs = *getArgReference_int(stk, pci, 4);
1754 15 : char *cname = SaveArgReference(stk, pci, 5);
1755 15 : int grant = *getArgReference_int(stk, pci, 6);
1756 15 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 7);
1757 :
1758 15 : initcontext();
1759 15 : if (strNil(tname))
1760 2 : msg = sql_revoke_global_privs(sql, grantee, privs, grant, grantor);
1761 : else
1762 13 : msg = sql_revoke_table_privs(sql, grantee, privs, sname, tname, cname, grant, grantor);
1763 : return msg;
1764 : }
1765 :
1766 : str
1767 89896 : SQLgrant_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1768 89896 : { mvc *sql = NULL;
1769 89896 : str msg;
1770 89896 : str sname = *getArgReference_str(stk, pci, 1);
1771 89896 : sqlid func_id = (sqlid) *getArgReference_int(stk, pci, 2);
1772 89896 : char *grantee = *getArgReference_str(stk, pci, 3);
1773 89896 : int privs = *getArgReference_int(stk, pci, 4);
1774 89896 : int grant = *getArgReference_int(stk, pci, 5);
1775 89896 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 6);
1776 :
1777 89896 : initcontext();
1778 89896 : msg = sql_grant_func_privs(sql, grantee, privs, sname, func_id, grant, grantor);
1779 89896 : return msg;
1780 : }
1781 :
1782 : str
1783 1 : SQLrevoke_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1784 1 : { mvc *sql = NULL;
1785 1 : str msg;
1786 1 : str sname = *getArgReference_str(stk, pci, 1);
1787 1 : sqlid func_id = (sqlid) *getArgReference_int(stk, pci, 2);
1788 1 : char *grantee = *getArgReference_str(stk, pci, 3);
1789 1 : int privs = *getArgReference_int(stk, pci, 4);
1790 1 : int grant = *getArgReference_int(stk, pci, 5);
1791 1 : sqlid grantor = (sqlid) *getArgReference_int(stk, pci, 6);
1792 :
1793 1 : initcontext();
1794 1 : msg = sql_revoke_func_privs(sql, grantee, privs, sname, func_id, grant, grantor);
1795 1 : return msg;
1796 : }
1797 :
1798 : str
1799 331 : SQLcreate_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1800 331 : { mvc *sql = NULL;
1801 331 : str msg;
1802 331 : str sname = *getArgReference_str(stk, pci, 1);
1803 331 : char *passwd = *getArgReference_str(stk, pci, 2);
1804 331 : int enc = *getArgReference_int(stk, pci, 3);
1805 331 : char *schema = SaveArgReference(stk, pci, 4);
1806 331 : char *schema_path = SaveArgReference(stk, pci, 5);
1807 331 : char *fullname = SaveArgReference(stk, pci, 6);
1808 331 : lng max_memory = *getArgReference_lng(stk, pci, 7);
1809 331 : int max_workers = *getArgReference_int(stk, pci, 8);
1810 331 : char *optimizer = SaveArgReference(stk, pci, 9);
1811 331 : char *default_role = SaveArgReference(stk, pci, 10);
1812 :
1813 331 : initcontext();
1814 331 : msg = sql_create_user(sql, sname, passwd, enc, fullname, schema, schema_path, max_memory, max_workers, optimizer, default_role);
1815 331 : return msg;
1816 : }
1817 :
1818 : str
1819 104 : SQLdrop_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1820 104 : { mvc *sql = NULL;
1821 104 : str msg;
1822 104 : str sname = *getArgReference_str(stk, pci, 1);
1823 :
1824 104 : initcontext();
1825 104 : msg = sql_drop_user(sql, sname);
1826 104 : return msg;
1827 : }
1828 :
1829 : str
1830 76 : SQLalter_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1831 76 : { mvc *sql = NULL;
1832 76 : str msg;
1833 76 : str sname = *getArgReference_str(stk, pci, 1);
1834 76 : char *passwd = SaveArgReference(stk, pci, 2);
1835 76 : int enc = *getArgReference_int(stk, pci, 3);
1836 76 : char *schema = SaveArgReference(stk, pci, 4);
1837 76 : char *schema_path = SaveArgReference(stk, pci, 5);
1838 76 : char *oldpasswd = SaveArgReference(stk, pci, 6);
1839 76 : char *role = SaveArgReference(stk, pci, 7);
1840 76 : lng max_memory = *getArgReference_lng(stk, pci, 8);
1841 76 : int max_workers = *getArgReference_int(stk, pci, 9);
1842 :
1843 76 : initcontext();
1844 76 : msg = sql_alter_user(sql, sname, passwd, enc, schema, schema_path, oldpasswd, role, max_memory, max_workers);
1845 :
1846 76 : return msg;
1847 : }
1848 :
1849 : str
1850 5 : SQLrename_user(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1851 5 : { mvc *sql = NULL;
1852 5 : str msg;
1853 5 : str sname = *getArgReference_str(stk, pci, 1);
1854 5 : char *newuser = *getArgReference_str(stk, pci, 2);
1855 :
1856 5 : initcontext();
1857 5 : msg = sql_rename_user(sql, sname, newuser);
1858 5 : return msg;
1859 : }
1860 :
1861 : str
1862 25 : SQLcreate_role(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1863 25 : { mvc *sql = NULL;
1864 25 : str msg;
1865 25 : str sname = *getArgReference_str(stk, pci, 1);
1866 25 : char *role = sname;
1867 25 : sqlid grantor = (sqlid)*getArgReference_int(stk, pci, 3);
1868 :
1869 25 : initcontext();
1870 25 : msg = sql_create_role(sql, role, grantor);
1871 25 : return msg;
1872 : }
1873 :
1874 : str
1875 19 : SQLdrop_role(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1876 19 : { mvc *sql = NULL;
1877 19 : str msg;
1878 19 : str sname = *getArgReference_str(stk, pci, 1);
1879 19 : char *role = sname;
1880 :
1881 19 : initcontext();
1882 19 : msg = sql_drop_role(sql, role);
1883 19 : return msg;
1884 : }
1885 :
1886 : str
1887 161 : SQLdrop_index(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1888 161 : { mvc *sql = NULL;
1889 161 : str msg;
1890 161 : str sname = *getArgReference_str(stk, pci, 1);
1891 161 : char *iname = *getArgReference_str(stk, pci, 2);
1892 :
1893 161 : initcontext();
1894 161 : msg = drop_index(sql, sname, iname);
1895 161 : return msg;
1896 : }
1897 :
1898 : str
1899 654 : SQLdrop_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1900 654 : { mvc *sql = NULL;
1901 654 : str msg;
1902 654 : str sname = *getArgReference_str(stk, pci, 1);
1903 654 : char *fname = *getArgReference_str(stk, pci, 2);
1904 654 : sqlid fid = (sqlid)*getArgReference_int(stk, pci, 3);
1905 654 : sql_ftype type = (sql_ftype) *getArgReference_int(stk, pci, 4);
1906 654 : int action = *getArgReference_int(stk, pci, 5);
1907 :
1908 654 : initcontext();
1909 654 : msg = drop_func(sql, sname, fname, fid, type, action);
1910 654 : return msg;
1911 : }
1912 :
1913 : str
1914 114393 : SQLcreate_function(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1915 114393 : { mvc *sql = NULL;
1916 114393 : str msg;
1917 114393 : str sname = *getArgReference_str(stk, pci, 1);
1918 114393 : str fname = *getArgReference_str(stk, pci, 2);
1919 114393 : sql_func *f = *(sql_func **) getArgReference(stk, pci, 3);
1920 114393 : int replace = *getArgReference_int(stk, pci, 4);
1921 :
1922 114393 : initcontext();
1923 114393 : msg = create_func(sql, sname, fname, f, replace);
1924 114393 : return msg;
1925 : }
1926 :
1927 : str
1928 336 : SQLcreate_trigger(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1929 336 : { mvc *sql = NULL;
1930 336 : str msg;
1931 336 : str sname = *getArgReference_str(stk, pci, 1);
1932 336 : char *tname = *getArgReference_str(stk, pci, 2);
1933 336 : char *triggername = *getArgReference_str(stk, pci, 3);
1934 336 : int time = *getArgReference_int(stk, pci, 4);
1935 336 : int orientation = *getArgReference_int(stk, pci, 5);
1936 336 : int event = *getArgReference_int(stk, pci, 6);
1937 336 : char *old_name = *getArgReference_str(stk, pci, 7);
1938 336 : char *new_name = *getArgReference_str(stk, pci, 8);
1939 336 : char *condition = *getArgReference_str(stk, pci, 9);
1940 336 : char *query = *getArgReference_str(stk, pci, 10);
1941 336 : int replace = *getArgReference_int(stk, pci, 11);
1942 :
1943 336 : initcontext();
1944 365 : old_name=(strNil(old_name))?NULL:old_name;
1945 374 : new_name=(strNil(new_name))?NULL:new_name;
1946 336 : condition=(strNil(condition))?NULL:condition;
1947 336 : msg = create_trigger(sql, sname, tname, triggername, time, orientation, event, old_name, new_name, condition, query, replace);
1948 336 : return msg;
1949 : }
1950 :
1951 : str
1952 81 : SQLdrop_trigger(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1953 81 : { mvc *sql = NULL;
1954 81 : str msg;
1955 81 : str sname = *getArgReference_str(stk, pci, 1);
1956 81 : char *triggername = *getArgReference_str(stk, pci, 2);
1957 81 : int if_exists = *getArgReference_int(stk, pci, 3);
1958 :
1959 81 : initcontext();
1960 81 : msg = drop_trigger(sql, sname, triggername, if_exists);
1961 81 : return msg;
1962 : }
1963 :
1964 : str
1965 290 : SQLalter_add_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1966 290 : { mvc *sql = NULL;
1967 290 : str msg;
1968 290 : str sname = *getArgReference_str(stk, pci, 1);
1969 290 : char *mtname = SaveArgReference(stk, pci, 2);
1970 290 : char *psname = SaveArgReference(stk, pci, 3);
1971 290 : char *ptname = SaveArgReference(stk, pci, 4);
1972 :
1973 290 : initcontext();
1974 290 : msg = alter_table_add_table(sql, sname, mtname, psname, ptname);
1975 290 : return msg;
1976 : }
1977 :
1978 : str
1979 202 : SQLalter_add_range_partition(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
1980 202 : { mvc *sql = NULL;
1981 202 : str msg;
1982 202 : str sname = *getArgReference_str(stk, pci, 1);
1983 202 : char *mtname = SaveArgReference(stk, pci, 2);
1984 202 : char *psname = SaveArgReference(stk, pci, 3);
1985 202 : char *ptname = SaveArgReference(stk, pci, 4);
1986 202 : ValRecord *min = &(stk)->stk[(pci)->argv[5]];
1987 202 : ValRecord *max = &(stk)->stk[(pci)->argv[6]];
1988 202 : bit with_nills = *getArgReference_bit(stk, pci, 7);
1989 202 : int update = *getArgReference_int(stk, pci, 8);
1990 202 : lng cnt = 0;
1991 :
1992 202 : if (getArgType(mb, pci, 9) == TYPE_lng) {
1993 200 : cnt = *getArgReference_lng(stk, pci, 9);
1994 : } else {
1995 2 : BAT *c = BATdescriptor(*getArgReference_bat(stk, pci, 9));
1996 2 : if (c && BATcount(c) == 1)
1997 2 : cnt = *(lng*)Tloc(c, 0);
1998 2 : if (c)
1999 2 : BBPunfix(c->batCacheid);
2000 : }
2001 :
2002 202 : initcontext();
2003 202 : msg = alter_table_add_range_partition(sql, sname, mtname, psname, ptname, VALget(min), VALget(max), with_nills, update, cnt);
2004 202 : return msg;
2005 : }
2006 :
2007 : str
2008 53 : SQLalter_add_value_partition(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2009 53 : { mvc *sql = NULL;
2010 53 : str msg;
2011 53 : str sname = *getArgReference_str(stk, pci, 1);
2012 53 : char *mtname = SaveArgReference(stk, pci, 2);
2013 53 : char *psname = SaveArgReference(stk, pci, 3);
2014 53 : char *ptname = SaveArgReference(stk, pci, 4);
2015 53 : bit with_nills = *getArgReference_bit(stk, pci, 5);
2016 53 : int update = *getArgReference_int(stk, pci, 6);
2017 53 : lng cnt = 0;
2018 :
2019 53 : if (getArgType(mb, pci, 7) == TYPE_lng) {
2020 53 : cnt = *getArgReference_lng(stk, pci, 7);
2021 : } else {
2022 0 : BAT *c = BATdescriptor(*getArgReference_bat(stk, pci, 7));
2023 0 : if (c && BATcount(c) == 1)
2024 0 : cnt = *(lng*)Tloc(c, 0);
2025 0 : if (c)
2026 0 : BBPunfix(c->batCacheid);
2027 : }
2028 :
2029 53 : initcontext();
2030 53 : msg = alter_table_add_value_partition(sql, stk, pci, sname, mtname, psname, ptname, with_nills, update, cnt);
2031 53 : return msg;
2032 : }
2033 :
2034 : str
2035 180 : SQLalter_del_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2036 180 : { mvc *sql = NULL;
2037 180 : str msg;
2038 180 : str sname = *getArgReference_str(stk, pci, 1);
2039 180 : char *mtname = SaveArgReference(stk, pci, 2);
2040 180 : char *psname = SaveArgReference(stk, pci, 3);
2041 180 : char *ptname = SaveArgReference(stk, pci, 4);
2042 180 : int drop_action = *getArgReference_int(stk, pci, 5);
2043 :
2044 180 : initcontext();
2045 180 : msg= alter_table_del_table(sql, sname, mtname, psname, ptname, drop_action);
2046 180 : return msg;
2047 : }
2048 :
2049 : str
2050 2067 : SQLalter_set_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2051 2067 : { mvc *sql = NULL;
2052 2067 : str msg;
2053 2067 : str sname = *getArgReference_str(stk, pci, 1);
2054 2067 : char *tname = SaveArgReference(stk, pci, 2);
2055 2067 : int access = *getArgReference_int(stk, pci, 3);
2056 :
2057 2067 : initcontext();
2058 2067 : msg = alter_table_set_access(sql, sname, tname, access);
2059 :
2060 2067 : return msg;
2061 : }
2062 :
2063 : str
2064 336 : SQLcomment_on(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2065 : {
2066 336 : mvc *sql = NULL;
2067 336 : str msg;
2068 336 : sqlid objid = (sqlid) *getArgReference_int(stk, pci, 1);
2069 336 : char *remark = *getArgReference_str(stk, pci, 2);
2070 336 : sql_trans *tx;
2071 336 : sql_schema *sys;
2072 336 : sql_table *comments;
2073 336 : sql_column *id_col, *remark_col;
2074 336 : oid rid;
2075 336 : int ok = LOG_OK;
2076 :
2077 336 : initcontext();
2078 :
2079 : // Manually insert the rows to circumvent permission checks.
2080 336 : tx = sql->session->tr;
2081 336 : sys = mvc_bind_schema(sql, "sys");
2082 336 : if (!sys)
2083 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "Internal error");
2084 336 : comments = mvc_bind_table(sql, sys, "comments");
2085 336 : if (!comments)
2086 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "no table sys.comments");
2087 336 : id_col = mvc_bind_column(sql, comments, "id");
2088 336 : remark_col = find_sql_column(comments, "remark");
2089 336 : if (!id_col || !remark_col)
2090 0 : throw(SQL, "sql.comment_on", SQLSTATE(3F000) "no table sys.comments");
2091 336 : sqlstore *store = tx->store;
2092 336 : rid = store->table_api.column_find_row(tx, id_col, &objid, NULL);
2093 672 : if (!strNil(remark) && *remark) {
2094 327 : if (!is_oid_nil(rid)) {
2095 : // have new remark and found old one, so update field
2096 : /* UPDATE sys.comments SET remark = %s WHERE id = %d */
2097 4 : ok = store->table_api.column_update_value(tx, remark_col, rid, remark);
2098 : } else {
2099 : // have new remark but found none so insert row
2100 : /* INSERT INTO sys.comments (id, remark) VALUES (%d, %s) */
2101 323 : ok = store->table_api.table_insert(tx, comments, &objid, &remark);
2102 : }
2103 327 : if (ok != LOG_OK)
2104 1 : throw(SQL, "sql.comment_on", SQLSTATE(42000) "Comment on failed%s", ok == LOG_CONFLICT ? " due to conflict with another transaction" : "");
2105 326 : if ((ok = sql_trans_add_dependency(tx, objid, ddl)) != LOG_OK) /* At the moment this adds dependencies for old objects :( */
2106 0 : throw(SQL, "sql.comment_on", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2107 : } else {
2108 9 : if (!is_oid_nil(rid)) {
2109 : // have no remark but found one, so delete row
2110 : /* DELETE FROM sys.comments WHERE id = %d */
2111 7 : if ((ok = store->table_api.table_delete(tx, comments, rid)) != LOG_OK)
2112 0 : throw(SQL, "sql.comment_on", SQLSTATE(42000) "Comment on failed%s", ok == LOG_CONFLICT ? " due to conflict with another transaction" : "");
2113 : }
2114 : }
2115 : return MAL_SUCCEED;
2116 : }
2117 :
2118 : str
2119 7 : SQLrename_schema(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2120 : {
2121 7 : mvc *sql = NULL;
2122 7 : str msg = MAL_SUCCEED;
2123 7 : str old_name = *getArgReference_str(stk, pci, 1);
2124 7 : str new_name = *getArgReference_str(stk, pci, 2);
2125 7 : sql_schema *s;
2126 :
2127 7 : initcontext();
2128 7 : sql_trans *tr = sql->session->tr;
2129 7 : sql_schema *cur = cur_schema(sql);
2130 :
2131 7 : if (!(s = mvc_bind_schema(sql, old_name)))
2132 0 : throw(SQL, "sql.rename_schema", SQLSTATE(42S02) "ALTER SCHEMA: no such schema '%s'", old_name);
2133 7 : if (!mvc_schema_privs(sql, s))
2134 0 : throw(SQL, "sql.rename_schema", SQLSTATE(42000) "ALTER SCHEMA: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), old_name);
2135 7 : if (s->system)
2136 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: cannot rename a system schema");
2137 7 : if (os_size(s->tables, tr) || os_size(s->types, tr) || os_size(s->funcs, tr) || os_size(s->seqs, tr))
2138 0 : throw(SQL, "sql.rename_schema", SQLSTATE(2BM37) "ALTER SCHEMA: unable to rename schema '%s' (there are database objects which depend on it)", old_name);
2139 14 : if (strNil(new_name) || *new_name == '\0')
2140 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: invalid new schema name");
2141 7 : if (mvc_bind_schema(sql, new_name))
2142 0 : throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: there is a schema named '%s' in the database", new_name);
2143 :
2144 7 : switch (sql_trans_rename_schema(sql->session->tr, s->base.id, new_name)) {
2145 0 : case -1:
2146 0 : throw(SQL,"sql.rename_schema", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2147 0 : case -2:
2148 : case -3:
2149 0 : throw(SQL,"sql.rename_schema", SQLSTATE(42000) "ALTER SCHEMA: transaction conflict detected");
2150 : default:
2151 7 : break;
2152 : }
2153 7 : if (cur && s->base.id == cur->base.id) /* change current session schema name */
2154 1 : if (!mvc_set_schema(sql, new_name))
2155 0 : throw(SQL, "sql.rename_schema",SQLSTATE(HY013) MAL_MALLOC_FAIL);
2156 : return msg;
2157 : }
2158 :
2159 : str
2160 45 : SQLrename_table(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2161 : {
2162 45 : mvc *sql = NULL;
2163 45 : str msg = MAL_SUCCEED;
2164 45 : str oschema_name = *getArgReference_str(stk, pci, 1);
2165 45 : str nschema_name = *getArgReference_str(stk, pci, 2);
2166 45 : str otable_name = *getArgReference_str(stk, pci, 3);
2167 45 : str ntable_name = *getArgReference_str(stk, pci, 4);
2168 45 : sql_schema *o, *s;
2169 45 : sql_table *t;
2170 :
2171 45 : initcontext();
2172 :
2173 45 : if (strcmp(oschema_name, nschema_name) == 0) { //renaming the table itself
2174 19 : if (!(s = mvc_bind_schema(sql, oschema_name)))
2175 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", oschema_name);
2176 19 : if (!mvc_schema_privs(sql, s))
2177 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);
2178 19 : if (!(t = mvc_bind_table(sql, s, otable_name)))
2179 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", otable_name, oschema_name);
2180 19 : if (t->system)
2181 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a system table");
2182 19 : if (isView(t))
2183 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a view");
2184 19 : if (isDeclaredTable(t))
2185 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a declared table");
2186 19 : if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL))
2187 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);
2188 38 : if (strNil(ntable_name) || *ntable_name == '\0')
2189 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: invalid new table name");
2190 19 : if (mvc_bind_table(sql, s, ntable_name))
2191 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", ntable_name, oschema_name);
2192 :
2193 19 : switch (sql_trans_rename_table(sql->session->tr, s, t->base.id, ntable_name)) {
2194 0 : case -1:
2195 0 : throw(SQL,"sql.rename_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2196 3 : case -2:
2197 : case -3:
2198 3 : throw(SQL,"sql.rename_table", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2199 : default:
2200 : break;
2201 : }
2202 : } else { //changing the schema of the table
2203 26 : assert(strcmp(otable_name, ntable_name) == 0);
2204 :
2205 26 : if (!(o = mvc_bind_schema(sql, oschema_name)))
2206 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", oschema_name);
2207 26 : if (!mvc_schema_privs(sql, o))
2208 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);
2209 26 : if (!(t = mvc_bind_table(sql, o, otable_name)))
2210 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", otable_name, oschema_name);
2211 26 : if (t->system)
2212 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot set schema of a system table");
2213 26 : if (isTempSchema(o))
2214 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change a temporary table schema");
2215 26 : if (isView(t))
2216 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a view");
2217 26 : if (isDeclaredTable(t))
2218 0 : throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: not possible to change schema of a declared table");
2219 26 : if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL) || list_length(t->members) || ol_length(t->triggers))
2220 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);
2221 26 : if (!(s = mvc_bind_schema(sql, nschema_name)))
2222 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", nschema_name);
2223 26 : if (!mvc_schema_privs(sql, s))
2224 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);
2225 26 : if (isTempSchema(s))
2226 0 : throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: not possible to change table's schema to temporary");
2227 26 : if (mvc_bind_table(sql, s, otable_name))
2228 0 : throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: table '%s' on schema '%s' already exists", otable_name, nschema_name);
2229 :
2230 26 : switch (sql_trans_set_table_schema(sql->session->tr, t->base.id, o, s)) {
2231 0 : case -1:
2232 0 : throw(SQL,"sql.rename_table", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2233 1 : case -2:
2234 : case -3:
2235 1 : throw(SQL,"sql.rename_table", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2236 : default:
2237 : break;
2238 : }
2239 : }
2240 :
2241 : return msg;
2242 : }
2243 :
2244 : str
2245 13 : SQLrename_column(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
2246 : {
2247 13 : mvc *sql = NULL;
2248 13 : str msg = MAL_SUCCEED;
2249 13 : str schema_name = *getArgReference_str(stk, pci, 1);
2250 13 : str table_name = *getArgReference_str(stk, pci, 2);
2251 13 : str old_name = *getArgReference_str(stk, pci, 3);
2252 13 : str new_name = *getArgReference_str(stk, pci, 4);
2253 13 : sql_schema *s;
2254 13 : sql_table *t;
2255 13 : sql_column *col;
2256 :
2257 13 : initcontext();
2258 13 : if (!(s = mvc_bind_schema(sql, schema_name)))
2259 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", schema_name);
2260 13 : if (!mvc_schema_privs(sql, s))
2261 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);
2262 13 : if (!(t = mvc_bind_table(sql, s, table_name)))
2263 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", table_name, schema_name);
2264 13 : if (t->system)
2265 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table");
2266 13 : if (isView(t))
2267 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name);
2268 13 : if (isDeclaredTable(t))
2269 0 : throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column in a declared table");
2270 13 : if (!(col = mvc_bind_column(sql, t, old_name)))
2271 0 : throw(SQL, "sql.rename_column", SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name);
2272 13 : if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL))
2273 0 : throw(SQL, "sql.rename_column", SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name);
2274 26 : if (strNil(new_name) || *new_name == '\0')
2275 0 : throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: invalid new column name");
2276 13 : if (mvc_bind_column(sql, t, new_name))
2277 0 : throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name);
2278 :
2279 13 : switch (sql_trans_rename_column(sql->session->tr, t, col->base.id, old_name, new_name)) {
2280 0 : case -1:
2281 0 : throw(SQL,"sql.rename_column", SQLSTATE(HY013) MAL_MALLOC_FAIL);
2282 0 : case -2:
2283 : case -3:
2284 0 : throw(SQL,"sql.rename_column", SQLSTATE(42000) "ALTER TABLE: transaction conflict detected");
2285 : default:
2286 : break;
2287 : }
2288 : return msg;
2289 : }
|