Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #include "monetdb_config.h"
14 : #include "sql_types.h"
15 : #include "sql_storage.h"
16 : #include "store_dependency.h"
17 : #include "store_sequence.h"
18 : #include "mutils.h"
19 :
20 : #include "bat/bat_utils.h"
21 : #include "bat/bat_storage.h"
22 : #include "bat/bat_table.h"
23 : #include "bat/bat_logger.h"
24 :
25 : /* version 05.23.03 of catalog */
26 : #define CATALOG_VERSION 52303 /* first in Aug2024 */
27 :
28 : ulng
29 292 : store_function_counter(sqlstore *store)
30 : {
31 292 : ulng ts = ATOMIC_INC(&store->function_counter);
32 292 : return ts;
33 : }
34 :
35 : static ulng
36 332975 : store_timestamp(sqlstore *store)
37 : {
38 332975 : ulng ts = ATOMIC_INC(&store->timestamp);
39 332975 : return ts;
40 : }
41 :
42 : ulng
43 403069 : store_get_timestamp(sqlstore *store)
44 : {
45 403069 : ulng ts = ATOMIC_GET(&store->timestamp);
46 403069 : return ts;
47 : }
48 :
49 : static ulng
50 39231 : store_transaction_id(sqlstore *store)
51 : {
52 39231 : ulng tid = ATOMIC_INC(&store->transaction);
53 39231 : return tid;
54 : }
55 :
56 : ulng
57 2238063 : store_oldest(sqlstore *store, sql_trans *tr)
58 : {
59 2238063 : if (tr && tr->ts == (ulng) ATOMIC_GET(&store->oldest)) {
60 43653 : sql_trans *otr = store->active->h->data;
61 43653 : if (otr == tr && store->active->h->next) {
62 36472 : otr = store->active->h->next->data;
63 36472 : return otr->ts;
64 : }
65 : }
66 2201591 : return (ulng) ATOMIC_GET(&store->oldest);
67 : }
68 :
69 : static ulng
70 1430 : store_oldest_pending(sqlstore *store)
71 : {
72 1430 : assert(store->oldest_pending != TRANSACTION_ID_BASE);
73 1430 : return store->oldest_pending;
74 : }
75 :
76 : static inline bool
77 146969 : instore(sqlid id)
78 : {
79 227 : if (id >= 2000 && id <= 2166)
80 15435 : return true;
81 : return false;
82 : }
83 :
84 : static void
85 1304 : id_destroy(sqlstore *store, void *p)
86 : {
87 1304 : (void)store;
88 1304 : GDKfree(p);
89 1304 : }
90 :
91 : static void
92 747399 : dep_destroy(sqlstore *store, sql_dependency_change *dep)
93 : {
94 747399 : (void)store;
95 747399 : GDKfree(dep);
96 747451 : }
97 :
98 : static void
99 1329 : type_destroy(sqlstore *store, sql_type *t)
100 : {
101 1329 : (void)store;
102 1329 : assert(t->base.refcnt > 0);
103 1329 : if (ATOMIC_DEC(&t->base.refcnt) > 0)
104 : return;
105 1326 : _DELETE(t->impl);
106 1326 : _DELETE(t->base.name);
107 1326 : _DELETE(t);
108 : }
109 :
110 : void
111 502842 : arg_destroy(sql_store store, sql_arg *a)
112 : {
113 502842 : (void)store;
114 502842 : _DELETE(a->name);
115 502842 : _DELETE(a);
116 502842 : }
117 :
118 : static void
119 175887 : func_destroy(sqlstore *store, sql_func *f)
120 : {
121 175887 : assert(f->base.refcnt > 0);
122 175887 : if (ATOMIC_DEC(&f->base.refcnt) > 0)
123 : return;
124 175185 : if (f->lang == FUNC_LANG_SQL && f->instantiated) {
125 : /* clean backend code */
126 290 : backend_freecode(sql_shared_module_name, 0, f->imp);
127 : }
128 175185 : if (f->res)
129 147727 : list_destroy2(f->res, store);
130 175185 : list_destroy2(f->ops, store);
131 175185 : _DELETE(f->imp);
132 175185 : _DELETE(f->mod);
133 175185 : _DELETE(f->query);
134 175185 : _DELETE(f->base.name);
135 175185 : _DELETE(f);
136 : }
137 :
138 : static void
139 520 : seq_destroy(sqlstore *store, sql_sequence *s)
140 : {
141 520 : (void)store;
142 520 : assert(s->base.refcnt > 0);
143 520 : if (ATOMIC_DEC(&s->base.refcnt) > 0)
144 : return;
145 348 : _DELETE(s->base.name);
146 348 : _DELETE(s);
147 : }
148 :
149 : static void
150 24894 : kc_destroy(sqlstore *store, sql_kc *kc)
151 : {
152 24894 : (void)store;
153 24894 : _DELETE(kc);
154 24894 : }
155 :
156 : static void
157 35143 : key_destroy(sqlstore *store, sql_key *k)
158 : {
159 35143 : assert(k->base.refcnt > 0);
160 35143 : if (ATOMIC_DEC(&k->base.refcnt) > 0)
161 : return;
162 10505 : list_destroy2(k->columns, store);
163 10505 : _DELETE(k->check);
164 10505 : k->columns = NULL;
165 10505 : _DELETE(k->base.name);
166 10505 : _DELETE(k);
167 : }
168 :
169 : void
170 29109 : idx_destroy(sqlstore *store, sql_idx * i)
171 : {
172 29109 : assert(i->base.refcnt > 0);
173 29109 : if (ATOMIC_DEC(&i->base.refcnt) > 0)
174 : return;
175 12239 : list_destroy2(i->columns, store);
176 12239 : i->columns = NULL;
177 :
178 12239 : if (ATOMIC_PTR_GET(&i->data))
179 10645 : store->storage_api.destroy_idx(store, i);
180 12239 : ATOMIC_PTR_DESTROY(&i->data);
181 12239 : _DELETE(i->base.name);
182 12239 : _DELETE(i);
183 : }
184 :
185 : static void
186 1074 : trigger_destroy(sqlstore *store, sql_trigger *t)
187 : {
188 1074 : assert(t->base.refcnt > 0);
189 1074 : if (ATOMIC_DEC(&t->base.refcnt) > 0)
190 : return;
191 : /* remove trigger from schema */
192 489 : if (t->columns) {
193 485 : list_destroy2(t->columns, store);
194 485 : t->columns = NULL;
195 : }
196 489 : _DELETE(t->old_name);
197 489 : _DELETE(t->new_name);
198 489 : _DELETE(t->condition);
199 489 : _DELETE(t->statement);
200 489 : _DELETE(t->base.name);
201 489 : _DELETE(t);
202 : }
203 :
204 : void
205 485739 : column_destroy(sqlstore *store, sql_column *c)
206 : {
207 485739 : assert(c->base.refcnt > 0);
208 485739 : if (ATOMIC_DEC(&c->base.refcnt) > 0)
209 : return;
210 467739 : if (ATOMIC_PTR_GET(&c->data))
211 168238 : store->storage_api.destroy_col(store, c);
212 467739 : ATOMIC_PTR_DESTROY(&c->data);
213 467739 : _DELETE(c->min);
214 467739 : _DELETE(c->max);
215 467739 : _DELETE(c->def);
216 467739 : _DELETE(c->storage_type);
217 467739 : _DELETE(c->base.name);
218 467739 : _DELETE(c);
219 : }
220 :
221 : static void
222 84 : int_destroy(sqlstore *store, int *v)
223 : {
224 84 : (void)store;
225 84 : _DELETE(v);
226 84 : }
227 :
228 : void
229 310514 : table_destroy(sqlstore *store, sql_table *t)
230 : {
231 310514 : assert(t->base.refcnt > 0);
232 310514 : if (ATOMIC_DEC(&t->base.refcnt) > 0)
233 : return;
234 60716 : if (isTable(t))
235 26674 : store->storage_api.destroy_del(store, t);
236 60716 : ATOMIC_PTR_DESTROY(&t->data);
237 : /* cleanup its parts */
238 60716 : list_destroy2(t->members, store);
239 60716 : ol_destroy(t->idxs, store);
240 60716 : ol_destroy(t->keys, store);
241 60716 : ol_destroy(t->triggers, store);
242 60716 : ol_destroy(t->columns, store);
243 60716 : if (isPartitionedByExpressionTable(t)) {
244 75 : if (t->part.pexp->cols)
245 75 : list_destroy2(t->part.pexp->cols, store);
246 75 : _DELETE(t->part.pexp->exp);
247 75 : _DELETE(t->part.pexp);
248 : }
249 60716 : _DELETE(t->query);
250 60716 : _DELETE(t->base.name);
251 60716 : _DELETE(t);
252 : }
253 :
254 : void
255 441 : part_value_destroy(sql_store store, sql_part_value *pv)
256 : {
257 441 : (void)store;
258 441 : _DELETE(pv->value);
259 441 : _DELETE(pv);
260 441 : }
261 :
262 : static void
263 2732 : part_destroy(sqlstore *store, sql_part *p)
264 : {
265 2732 : assert(p->base.refcnt > 0);
266 2732 : if (ATOMIC_DEC(&p->base.refcnt) > 0)
267 : return;
268 1240 : if (p->part.range.maxvalue) {
269 500 : _DELETE(p->part.range.minvalue);
270 500 : _DELETE(p->part.range.maxvalue);
271 740 : } else if (p->part.values)
272 147 : list_destroy2(p->part.values, store);
273 1240 : _DELETE(p->base.name);
274 1240 : _DELETE(p);
275 : }
276 :
277 : static void
278 2783 : schema_destroy(sqlstore *store, sql_schema *s)
279 : {
280 2783 : assert(s->base.refcnt > 0);
281 2783 : if (ATOMIC_DEC(&s->base.refcnt) > 0)
282 : return;
283 : /* cleanup its parts */
284 2584 : os_destroy(s->parts, store);
285 2584 : os_destroy(s->triggers, store);
286 2584 : os_destroy(s->idxs, store);
287 2584 : os_destroy(s->keys, store);
288 2584 : os_destroy(s->seqs, store);
289 2584 : os_destroy(s->tables, store);
290 2584 : os_destroy(s->funcs, store);
291 2584 : os_destroy(s->types, store);
292 2584 : _DELETE(s->base.name);
293 2584 : _DELETE(s);
294 : }
295 :
296 : static void
297 21881 : predicate_destroy(sqlstore *store, pl *p)
298 : {
299 21881 : (void) store;
300 21881 : if (p->r) {
301 1905 : VALclear(&p->r->data);
302 1905 : _DELETE(p->r);
303 : }
304 21881 : if (p->f) {
305 11 : VALclear(&p->f->data);
306 11 : _DELETE(p->f);
307 : }
308 21881 : _DELETE(p);
309 21881 : }
310 :
311 : int
312 21880 : sql_trans_add_predicate(sql_trans* tr, sql_column *c, unsigned int cmp, atom *r, atom *f, bool anti, bool semantics)
313 : {
314 21880 : pl *p = ZNEW(pl);
315 :
316 21881 : if (!p)
317 : return LOG_ERR;
318 21881 : p->c = c;
319 21881 : p->cmp = cmp;
320 21881 : p->anti = anti;
321 21881 : p->semantics = semantics;
322 21881 : p->r = r;
323 21881 : p->f = f;
324 21881 : if (!tr->predicates && !(tr->predicates = list_create((fdestroy) &predicate_destroy))) {
325 0 : predicate_destroy(tr->store, p);
326 0 : return LOG_ERR;
327 : }
328 21881 : if (!list_append(tr->predicates, p)) {
329 0 : predicate_destroy(tr->store, p);
330 0 : return LOG_ERR;
331 : }
332 : return LOG_OK;
333 : }
334 :
335 : int
336 590589 : sql_trans_add_dependency(sql_trans* tr, sqlid id, sql_dependency_change_type tp)
337 : {
338 590589 : sql_dependency_change *dep = MNEW(sql_dependency_change);
339 :
340 590589 : if (!dep)
341 : return LOG_ERR;
342 590589 : *dep = (sql_dependency_change) {
343 : .objid = id,
344 : .type = tp
345 : };
346 590589 : if (!tr->dependencies && !(tr->dependencies = list_create((fdestroy) &dep_destroy))) {
347 0 : _DELETE(dep);
348 0 : return LOG_ERR;
349 : }
350 590589 : if (!list_append(tr->dependencies, dep)) {
351 0 : _DELETE(dep);
352 0 : return LOG_ERR;
353 : }
354 : return LOG_OK;
355 : }
356 :
357 : int
358 156370 : sql_trans_add_dependency_change(sql_trans *tr, sqlid id, sql_dependency_change_type tp)
359 : {
360 156370 : sql_dependency_change *dep = MNEW(sql_dependency_change);
361 :
362 156850 : if (!dep)
363 : return LOG_ERR;
364 156850 : *dep = (sql_dependency_change) {
365 : .objid = id,
366 : .type = tp
367 : };
368 156850 : if (!tr->depchanges && !(tr->depchanges = list_create((fdestroy) &dep_destroy))) {
369 0 : _DELETE(dep);
370 0 : return LOG_ERR;
371 : }
372 156854 : if (!list_append(tr->depchanges, dep)) {
373 0 : _DELETE(dep);
374 0 : return LOG_ERR;
375 : }
376 : return LOG_OK;
377 : }
378 :
379 : static void
380 2108 : load_keycolumn(sql_trans *tr, sql_key *k, res_table *rt_keycols/*, oid rid*/)
381 : {
382 2108 : sql_kc *kc = ZNEW(sql_kc);
383 2108 : sql_schema *syss = find_sql_schema(tr, "sys");
384 2108 : sql_table *objects = find_sql_table(tr, syss, "objects");
385 2108 : sqlstore *store = tr->store;
386 :
387 2108 : str v = (char*)store->table_api.table_fetch_value(rt_keycols, find_sql_column(objects, "name"));
388 2108 : kc->c = find_sql_column(k->t, v);
389 2108 : list_append(k->columns, kc);
390 2108 : assert(kc->c);
391 2108 : }
392 :
393 : static sql_key *
394 2069 : load_key(sql_trans *tr, sql_table *t, res_table *rt_keys, res_table *rt_keycols/*, oid rid*/)
395 : {
396 2069 : sql_schema *syss = find_sql_schema(tr, "sys");
397 2069 : sql_table *keys = find_sql_table(tr, syss, "keys");
398 2069 : sql_table *objects = find_sql_table(tr, syss, "objects");
399 2069 : sqlstore *store = tr->store;
400 :
401 2069 : key_type ktype = (key_type) *(int*) store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "type"));
402 2069 : sql_key *nk = (ktype != fkey)?(sql_key*)ZNEW(sql_ukey):(sql_key*)ZNEW(sql_fkey);
403 2069 : sqlid kid = *(sqlid*) store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "id"));
404 2069 : str v = (char*) store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "name"));
405 :
406 2069 : base_init(NULL, &nk->base, kid, 0, v);
407 2069 : nk->type = ktype;
408 2069 : nk->columns = list_create((fdestroy) &kc_destroy);
409 2069 : nk->t = t;
410 :
411 2069 : switch (ktype) {
412 6 : case ckey: {
413 6 : str ch = (char*)store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "check"));
414 12 : if (!strNil(ch))
415 6 : nk->check =_STRDUP(ch);
416 : } break;
417 2027 : case ukey:
418 : case unndkey:
419 : case pkey: {
420 2027 : sql_ukey *uk = (sql_ukey *) nk;
421 :
422 2027 : if (ktype == pkey)
423 1258 : t->pkey = uk;
424 : break;
425 : }
426 36 : case fkey: {
427 36 : sql_fkey *fk = (sql_fkey *) nk;
428 36 : int action = *(int*)store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "action"));
429 36 : fk->on_delete = action & 255;
430 36 : fk->on_update = (action>>8) & 255;
431 :
432 36 : fk->rkey = *(sqlid*)store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "rkey"));
433 36 : break;
434 : }
435 : }
436 :
437 4177 : for ( ; rt_keycols->cur_row < rt_keycols->nr_rows; rt_keycols->cur_row++) {
438 4072 : sqlid nid = *(sqlid*)store->table_api.table_fetch_value(rt_keycols, find_sql_column(objects, "id"));
439 4072 : if (nid != nk->base.id)
440 : break;
441 2108 : load_keycolumn(tr, nk, rt_keycols);
442 : }
443 :
444 : /* find idx with same name */
445 2069 : node *n = ol_find_name(t->idxs, nk->base.name);
446 2069 : if (n) {
447 2063 : nk->idx = (sql_idx*)n->data;
448 2063 : nk->idx->key = nk;
449 : }
450 2069 : return nk;
451 : }
452 :
453 : static void
454 2144 : load_idxcolumn(sql_trans *tr, sql_idx * i, res_table *rt_idxcols/*, oid rid*/)
455 : {
456 2144 : sql_kc *kc = ZNEW(sql_kc);
457 2144 : sql_schema *syss = find_sql_schema(tr, "sys");
458 2144 : sql_table *objects = find_sql_table(tr, syss, "objects");
459 2144 : sqlstore *store = tr->store;
460 2144 : str v;
461 :
462 2144 : v = (char*)store->table_api.table_fetch_value(rt_idxcols, find_sql_column(objects, "name"));
463 2144 : kc->c = find_sql_column(i->t, v);
464 2144 : assert(kc->c);
465 2144 : list_append(i->columns, kc);
466 2144 : }
467 :
468 : static sql_idx *
469 2095 : load_idx(sql_trans *tr, sql_table *t, res_table *rt_idx, res_table *rt_idxcols/*, oid rid*/)
470 : {
471 2095 : sql_idx *ni = ZNEW(sql_idx);
472 2095 : sql_schema *syss = find_sql_schema(tr, "sys");
473 2095 : sql_table *idxs = find_sql_table(tr, syss, "idxs");
474 2095 : sql_table *objects = find_sql_table(tr, syss, "objects");
475 2095 : sqlstore *store = tr->store;
476 :
477 2095 : sqlid iid = *(sqlid*)store->table_api.table_fetch_value(rt_idx, find_sql_column(idxs, "id"));
478 2095 : str v = (char*)store->table_api.table_fetch_value(rt_idx, find_sql_column(idxs, "name"));
479 2095 : base_init(NULL, &ni->base, iid, 0, v);
480 2095 : ni->type = (idx_type) *(int*) store->table_api.table_fetch_value(rt_idx, find_sql_column(idxs, "type"));
481 2095 : ni->columns = list_create((fdestroy) &kc_destroy);
482 2095 : ni->t = t;
483 2095 : ni->key = NULL;
484 2095 : ATOMIC_PTR_INIT(&ni->data, NULL);
485 :
486 2095 : if (isTable(ni->t) && idx_has_column(ni->type))
487 2043 : store->storage_api.create_idx(tr, ni);
488 :
489 4239 : for ( ; rt_idxcols->cur_row < rt_idxcols->nr_rows; rt_idxcols->cur_row++) {
490 4134 : sqlid nid = *(sqlid*)store->table_api.table_fetch_value(rt_idxcols, find_sql_column(objects, "id"));
491 4134 : if (nid != ni->base.id)
492 : break;
493 2144 : load_idxcolumn(tr, ni, rt_idxcols);
494 : }
495 2095 : return ni;
496 : }
497 :
498 : static void
499 0 : load_triggercolumn(sql_trans *tr, sql_trigger * i, res_table *rt_triggercols)
500 : {
501 0 : sql_kc *kc = ZNEW(sql_kc);
502 0 : sql_schema *syss = find_sql_schema(tr, "sys");
503 0 : sql_table *objects = find_sql_table(tr, syss, "objects");
504 0 : sqlstore *store = tr->store;
505 :
506 0 : str v = (char*)store->table_api.table_fetch_value(rt_triggercols, find_sql_column(objects, "name"));
507 0 : kc->c = find_sql_column(i->t, v);
508 0 : list_append(i->columns, kc);
509 0 : assert(kc->c);
510 0 : }
511 :
512 : static sql_trigger *
513 19 : load_trigger(sql_trans *tr, sql_table *t, res_table *rt_triggers, res_table *rt_triggercols)
514 : {
515 19 : sql_trigger *nt = ZNEW(sql_trigger);
516 19 : sql_schema *syss = find_sql_schema(tr, "sys");
517 19 : sql_table *triggers = find_sql_table(tr, syss, "triggers");
518 19 : sql_table *objects = find_sql_table(tr, syss, "objects");
519 19 : sqlstore *store = tr->store;
520 :
521 19 : sqlid tid = *(sqlid*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "id"));
522 19 : str v = (char*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "name"));
523 19 : base_init(NULL, &nt->base, tid, 0, v);
524 :
525 19 : nt->time = *(sht*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "time"));
526 19 : nt->orientation = *(sht*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "orientation"));
527 19 : nt->event = *(sht*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "event"));
528 :
529 19 : v = (char*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "old_name"));
530 38 : if (!strNil(v))
531 0 : nt->old_name =_STRDUP(v);
532 19 : v = (char*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "new_name"));
533 38 : if (!strNil(v))
534 19 : nt->new_name =_STRDUP(v);
535 19 : v = (char*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "condition"));
536 38 : if (!strNil(v))
537 0 : nt->condition =_STRDUP(v);
538 19 : v = (char*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "statement"));
539 38 : if (!strNil(v))
540 19 : nt->statement =_STRDUP(v);
541 :
542 19 : nt->t = t;
543 19 : if (t)
544 19 : nt->columns = list_create((fdestroy) &kc_destroy);
545 :
546 19 : if (rt_triggercols) {
547 19 : for ( ; rt_triggercols->cur_row < rt_triggercols->nr_rows; rt_triggercols->cur_row++) {
548 0 : sqlid nid = *(sqlid*)store->table_api.table_fetch_value(rt_triggercols, find_sql_column(objects, "id"));
549 0 : if (nid != nt->base.id)
550 : break;
551 0 : load_triggercolumn(tr, nt, rt_triggercols);
552 : }
553 : }
554 19 : return nt;
555 : }
556 :
557 : static sql_column *
558 101680 : load_column(sql_trans *tr, sql_table *t, res_table *rt_cols)
559 : {
560 101680 : sql_column *c = ZNEW(sql_column);
561 101680 : sql_schema *syss = find_sql_schema(tr, "sys");
562 101680 : sql_table *columns = find_sql_table(tr, syss, "_columns");
563 101680 : sqlstore *store = tr->store;
564 101680 : str v, def, tpe, st;
565 101680 : int sz, d;
566 :
567 101680 : sqlid cid = *(sqlid*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "id"));
568 101680 : v = (char*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "name"));
569 101680 : base_init(NULL, &c->base, cid, 0, v);
570 :
571 101680 : sz = *(int*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "type_digits"));
572 101680 : d = *(int*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "type_scale"));
573 101680 : tpe = (char*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "type"));
574 101680 : if (tpe && strcmp(tpe, "clob") == 0)
575 610 : tpe = "varchar";
576 101680 : if (!sql_find_subtype(&c->type, tpe, sz, d)) {
577 30 : sql_type *lt = sql_trans_bind_type(tr, t->s, tpe);
578 30 : if (lt == NULL) {
579 0 : TRC_ERROR(SQL_STORE, "SQL type '%s' is missing\n", tpe);
580 0 : return NULL;
581 : }
582 30 : sql_init_subtype(&c->type, lt, sz, d);
583 : }
584 101680 : c->def = NULL;
585 101680 : def = (char*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "default"));
586 203360 : if (!strNil(def))
587 256 : c->def =_STRDUP(def);
588 101680 : c->null = *(bit*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "null"));
589 101680 : c->colnr = *(int*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "number"));
590 101680 : c->unique = 0;
591 101680 : c->storage_type = NULL;
592 101680 : st = (char*)store->table_api.table_fetch_value(rt_cols, find_sql_column(columns, "storage"));
593 203360 : if (!strNil(st))
594 4 : c->storage_type =_STRDUP(st);
595 101680 : ATOMIC_PTR_INIT(&c->data, NULL);
596 101680 : c->t = t;
597 101680 : if (isTable(c->t))
598 10397 : store->storage_api.create_col(tr, c);
599 101680 : TRC_DEBUG(SQL_STORE, "Load column: %s\n", c->base.name);
600 101680 : if (!c->null)
601 3615 : store->storage_api.col_not_null(tr, c, !c->null);
602 : return c;
603 : }
604 :
605 : static int
606 268 : col_set_range(sql_trans *tr, sql_part *pt, bool add_range)
607 : {
608 268 : sql_table *t = find_sql_table_id(tr, pt->t->s /* schema of both member and merge tbale */, pt->member);
609 268 : sql_column *c = t? find_sql_column(t, pt->t->part.pcol->base.name):NULL;
610 :
611 268 : if (c) {
612 257 : sqlstore *store = tr->store;
613 257 : return store->storage_api.col_set_range(tr, c, pt, add_range);
614 : }
615 : return 0;
616 : }
617 :
618 : static int
619 66 : load_range_partition(sql_trans *tr, sql_schema *syss, sql_part *pt)
620 : {
621 66 : oid rid;
622 66 : rids *rs;
623 66 : sqlstore *store = tr->store;
624 66 : sql_table *ranges = find_sql_table(tr, syss, "range_partitions");
625 66 : assert(ranges);
626 :
627 66 : rs = store->table_api.rids_select(tr, find_sql_column(ranges, "table_id"), &pt->member, &pt->member, NULL);
628 66 : if (rs == NULL)
629 : return -1;
630 66 : if ((rid = store->table_api.rids_next(rs)) != oid_nil) {
631 66 : ptr cbat;
632 66 : const char *v;
633 :
634 66 : pt->with_nills = (bit) store->table_api.column_find_bte(tr, find_sql_column(ranges, "with_nulls"), rid);
635 66 : v = store->table_api.column_find_string_start(tr, find_sql_column(ranges, "minimum"), rid, &cbat);
636 66 : pt->part.range.minvalue =_STRDUP(v);
637 66 : pt->part.range.minlength = strLen(v);
638 66 : store->table_api.column_find_string_end(cbat);
639 66 : v = store->table_api.column_find_string_start(tr, find_sql_column(ranges, "maximum"), rid, &cbat);
640 66 : pt->part.range.maxvalue =_STRDUP(v);
641 66 : pt->part.range.maxlength = strLen(v);
642 66 : store->table_api.column_find_string_end(cbat);
643 : }
644 66 : store->table_api.rids_destroy(rs);
645 66 : return 0;
646 : }
647 :
648 : static int
649 33 : load_value_partition(sql_trans *tr, sql_schema *syss, sql_part *pt)
650 : {
651 33 : sqlstore *store = tr->store;
652 33 : list *vals = NULL;
653 33 : oid rid;
654 33 : sql_table *values = find_sql_table(tr, syss, "value_partitions");
655 33 : assert(values);
656 :
657 33 : rids *rs = store->table_api.rids_select(tr, find_sql_column(values, "table_id"), &pt->member, &pt->member, NULL);
658 33 : if (rs == NULL)
659 : return -1;
660 :
661 33 : if (!rs)
662 : return -1;
663 33 : vals = list_create((fdestroy) &part_value_destroy);
664 33 : if (!vals) {
665 0 : store->table_api.rids_destroy(rs);
666 0 : return -1;
667 : }
668 :
669 140 : for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
670 107 : ptr cbat;
671 :
672 107 : const char *v = store->table_api.column_find_string_start(tr, find_sql_column(values, "value"), rid, &cbat);
673 107 : if (strNil(v)) { /* check for null value */
674 11 : pt->with_nills = true;
675 : } else {
676 96 : sql_part_value *nextv = ZNEW(sql_part_value);
677 96 : nextv->value =_STRDUP(v);
678 96 : nextv->length = strLen(v);
679 96 : list_append(vals, nextv);
680 : }
681 107 : store->table_api.column_find_string_end(cbat);
682 : }
683 33 : store->table_api.rids_destroy(rs);
684 33 : pt->part.values = vals;
685 33 : return 0;
686 : }
687 :
688 : static sql_part*
689 213 : load_part(sql_trans *tr, sql_table *mt, oid rid)
690 : {
691 213 : sql_part *pt = ZNEW(sql_part);
692 213 : sql_schema *syss = find_sql_schema(tr, "sys");
693 213 : sql_table *objects = find_sql_table(tr, syss, "objects");
694 213 : sqlid id;
695 213 : sqlstore *store = tr->store;
696 213 : const char *v;
697 213 : ptr cbat;
698 :
699 213 : assert(isMergeTable(mt) || isReplicaTable(mt));
700 213 : id = store->table_api.column_find_sqlid(tr, find_sql_column(objects, "id"), rid);
701 213 : if (is_int_nil(id)) { /* upgrade case, the id it's not initialized */
702 0 : id = store_next_oid(store);
703 0 : store->table_api.column_update_value(tr, find_sql_column(objects, "id"), rid, &id);
704 : }
705 213 : v = store->table_api.column_find_string_start(tr, find_sql_column(objects, "name"), rid, &cbat);
706 213 : base_init(NULL, &pt->base, id, 0, v);
707 213 : store->table_api.column_find_string_end(cbat);
708 213 : pt->t = mt;
709 213 : pt->member = store->table_api.column_find_sqlid(tr, find_sql_column(objects, "sub"), rid);
710 213 : list_append(mt->members, pt);
711 213 : return pt;
712 : }
713 :
714 : void
715 0 : sql_trans_update_tables(sql_trans* tr, sql_schema *s)
716 : {
717 0 : (void)tr;
718 0 : (void)s;
719 0 : }
720 :
721 : sql_base *
722 312485 : dup_base(sql_base *b)
723 : {
724 312485 : ATOMIC_INC(&b->refcnt);
725 312485 : return b;
726 : }
727 :
728 : static sql_table *
729 13258 : load_table(sql_trans *tr, sql_schema *s, res_table *rt_tables, res_table *rt_parts,
730 : res_table *rt_cols, res_table *rt_idx, res_table *rt_idxcols,
731 : res_table *rt_keys, res_table *rt_keycols,
732 : res_table *rt_triggers, res_table *rt_triggercols,
733 : sqlid tid)
734 : {
735 13258 : sqlstore *store = tr->store;
736 13258 : sql_table *t = ZNEW(sql_table);
737 13258 : sql_schema *syss = find_sql_schema(tr, "sys");
738 13258 : sql_table *tables = find_sql_table(tr, syss, "_tables");
739 13258 : sql_table *cols = find_sql_table(tr, syss, "_columns");
740 13258 : sql_table *idxs = find_sql_table(tr, syss, "idxs");
741 13258 : sql_table *keys = find_sql_table(tr, syss, "keys");
742 13258 : sql_table *triggers = find_sql_table(tr, syss, "triggers");
743 13258 : sqlid pcolid = int_nil;
744 13258 : str v, exp = NULL;
745 :
746 13258 : sqlid ntid = *(sqlid*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "id"));
747 13258 : assert(tid == ntid);
748 13258 : v = (char*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "name"));
749 13258 : base_init(NULL, &t->base, tid, 0, v);
750 13258 : t->query = NULL;
751 13258 : v = (char*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "query"));
752 26516 : if (!strNil(v))
753 10335 : t->query =_STRDUP(v);
754 13258 : t->type = *(sht*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "type"));
755 13258 : t->system = *(bit*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "system"));
756 13258 : t->commit_action = (ca_t)*(sht*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "commit_action"));
757 13258 : t->persistence = SQL_PERSIST;
758 13258 : if (t->commit_action)
759 0 : t->persistence = SQL_GLOBAL_TEMP;
760 13258 : if (isRemote(t))
761 34 : t->persistence = SQL_REMOTE;
762 13258 : t->access = *(sht*)store->table_api.table_fetch_value(rt_tables, find_sql_column(tables, "access"));
763 :
764 13258 : t->pkey = NULL;
765 13258 : t->s = s;
766 13258 : t->sz = COLSIZE;
767 :
768 13258 : t->columns = ol_new(NULL, (destroy_fptr) &column_destroy, store);
769 13258 : t->idxs = ol_new(NULL, (destroy_fptr) &idx_destroy, store);
770 13258 : t->keys = ol_new(NULL, (destroy_fptr) &key_destroy, store);
771 13258 : t->triggers = ol_new(NULL, (destroy_fptr) &trigger_destroy, store);
772 13258 : if (isMergeTable(t) || isReplicaTable(t))
773 99 : t->members = list_create((fdestroy) &part_destroy);
774 13258 : ATOMIC_PTR_INIT(&t->data, NULL);
775 :
776 13258 : if (isTable(t)) {
777 2824 : if (store->storage_api.create_del(tr, t) != LOG_OK) {
778 0 : TRC_ERROR(SQL_STORE, "Load table '%s' is missing 'deletes'", t->base.name);
779 0 : ATOMIC_PTR_DESTROY(&t->data);
780 0 : return NULL;
781 : }
782 : }
783 :
784 13258 : TRC_DEBUG(SQL_STORE, "Load table: %s\n", t->base.name);
785 :
786 13258 : sql_table *partitions = find_sql_table(tr, syss, "table_partitions");
787 13258 : assert(partitions);
788 :
789 13258 : if (rt_parts->cur_row < rt_parts->nr_rows) {
790 1839 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_parts, find_sql_column(partitions, "table_id"));
791 1839 : if (ntid == tid) {
792 45 : t->properties |= *(bte*)store->table_api.table_fetch_value(rt_parts, find_sql_column(partitions, "type"));
793 :
794 45 : if (isPartitionedByColumnTable(t)) {
795 42 : pcolid = *(sqlid*)store->table_api.table_fetch_value(rt_parts, find_sql_column(partitions, "column_id"));
796 : } else {
797 3 : v = (char*)store->table_api.table_fetch_value(rt_parts, find_sql_column(partitions, "expression"));
798 6 : assert(!strNil(v));
799 3 : exp =_STRDUP(v);
800 : }
801 45 : rt_parts->cur_row++;
802 : }
803 : }
804 :
805 13258 : assert((!isRangePartitionTable(t) && !isListPartitionTable(t)) || (!exp && !is_int_nil(pcolid)) || (exp && is_int_nil(pcolid)));
806 13258 : if (isPartitionedByExpressionTable(t)) {
807 3 : t->part.pexp = ZNEW(sql_expression);
808 3 : t->part.pexp->exp = exp;
809 3 : t->part.pexp->type = *sql_bind_localtype("void"); /* initialized at initialize_sql_parts */
810 3 : t->part.pexp->cols = list_create((fdestroy) &int_destroy);
811 : }
812 114938 : for ( ; rt_cols->cur_row < rt_cols->nr_rows; rt_cols->cur_row++) {
813 114833 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_cols, find_sql_column(cols, "table_id"));
814 129953 : while (instore(ntid)) {
815 15120 : rt_cols->cur_row++;
816 15120 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_cols, find_sql_column(cols, "table_id"));
817 : }
818 :
819 114833 : if (ntid != t->base.id)
820 : break;
821 101680 : sql_column* next = load_column(tr, t, rt_cols);
822 101680 : if (next == NULL) {
823 0 : table_destroy(store, t);
824 0 : return NULL;
825 : }
826 101680 : if (ol_add(t->columns, &next->base)) {
827 0 : table_destroy(store, t);
828 0 : return NULL;
829 : }
830 101680 : if (pcolid == next->base.id)
831 42 : t->part.pcol = next;
832 : }
833 :
834 13258 : if (!isKindOfTable(t))
835 : return t;
836 :
837 : /* load idx's first as the may be needed by the keys */
838 5052 : for ( ; rt_idx->cur_row < rt_idx->nr_rows; rt_idx->cur_row++) {
839 4187 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_idx, find_sql_column(idxs, "table_id"));
840 :
841 4187 : if (ntid != t->base.id)
842 : break;
843 2095 : sql_idx *i = load_idx(tr, t, rt_idx, rt_idxcols);
844 :
845 4190 : if (!i || ol_add(t->idxs, &i->base) ||
846 2095 : os_add(s->idxs, tr, i->base.name, dup_base(&i->base))) {
847 0 : table_destroy(store, t);
848 0 : return NULL;
849 : }
850 : }
851 :
852 5026 : for ( ; rt_keys->cur_row < rt_keys->nr_rows; rt_keys->cur_row++) {
853 4145 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_keys, find_sql_column(keys, "table_id"));
854 :
855 4145 : if (ntid != t->base.id)
856 : break;
857 2069 : sql_key *k = load_key(tr, t, rt_keys, rt_keycols);
858 :
859 4138 : if (!k || ol_add(t->keys, &k->base) ||
860 2069 : os_add(s->keys, tr, k->base.name, dup_base(&k->base)) ||
861 2069 : (isGlobal(t) && os_add(tr->cat->objects, tr, k->base.name, dup_base(&k->base)))) {
862 0 : table_destroy(store, t);
863 0 : return NULL;
864 : }
865 : }
866 :
867 : /* after loading keys and idxs, update properties derived from indexes that require keys */
868 2957 : if (ol_length(t->idxs))
869 3387 : for (node *n = ol_first_node(t->idxs); n; n = n->next)
870 2095 : create_sql_idx_done(tr, n->data);
871 :
872 3081 : for ( ; rt_triggers->cur_row < rt_triggers->nr_rows; rt_triggers->cur_row++) {
873 626 : ntid = *(sqlid*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(triggers, "table_id"));
874 :
875 626 : if (ntid < t->base.id && instore(ntid)) /* skip triggers on system tables ugh */
876 105 : continue;
877 521 : if (ntid != t->base.id)
878 : break;
879 19 : sql_trigger *k = load_trigger(tr, t, rt_triggers, rt_triggercols);
880 :
881 38 : if (!k || ol_add(t->triggers, &k->base) ||
882 19 : os_add(s->triggers, tr, k->base.name, dup_base(&k->base))) {
883 0 : table_destroy(store, t);
884 0 : return NULL;
885 : }
886 : }
887 : return t;
888 : }
889 :
890 : static sql_type *
891 428 : load_type(sql_trans *tr, sql_schema *s, oid rid)
892 : {
893 428 : sqlstore *store = tr->store;
894 428 : sql_type *t = ZNEW(sql_type);
895 428 : sql_schema *syss = find_sql_schema(tr, "sys");
896 428 : sql_table *types = find_sql_table(tr, syss, "types");
897 428 : sqlid tid;
898 428 : const char *v;
899 428 : ptr cbat;
900 :
901 428 : tid = store->table_api.column_find_sqlid(tr, find_sql_column(types, "id"), rid);
902 428 : v = store->table_api.column_find_string_start(tr, find_sql_column(types, "sqlname"), rid, &cbat);
903 428 : base_init(NULL, &t->base, tid, 0, v);
904 428 : store->table_api.column_find_string_end(cbat);
905 428 : v = store->table_api.column_find_string_start(tr, find_sql_column(types, "systemname"), rid, &cbat);
906 428 : t->impl =_STRDUP(v);
907 428 : store->table_api.column_find_string_end(cbat);
908 428 : t->digits = store->table_api.column_find_int(tr, find_sql_column(types, "digits"), rid);
909 428 : t->scale = store->table_api.column_find_int(tr, find_sql_column(types, "scale"), rid);
910 428 : t->radix = store->table_api.column_find_int(tr, find_sql_column(types, "radix"), rid);
911 428 : t->eclass = (sql_class)store->table_api.column_find_int(tr, find_sql_column(types, "eclass"), rid);
912 428 : t->localtype = ATOMindex(t->impl);
913 428 : t->s = s;
914 428 : return t;
915 : }
916 :
917 : static sql_arg *
918 156454 : load_arg(sql_trans *tr, sql_func *f, oid rid)
919 : {
920 156454 : sqlstore *store = tr->store;
921 156454 : sql_arg *a = ZNEW(sql_arg);
922 156454 : unsigned int digits, scale;
923 156454 : sql_schema *syss = find_sql_schema(tr, "sys");
924 156454 : sql_table *args = find_sql_table(tr, syss, "args");
925 156454 : const char *v, *tpe;
926 156454 : ptr cbat;
927 :
928 156454 : v = store->table_api.column_find_string_start(tr, find_sql_column(args, "name"), rid, &cbat);
929 156454 : a->name =_STRDUP(v);
930 156454 : store->table_api.column_find_string_end(cbat);
931 156454 : a->inout = store->table_api.column_find_bte(tr, find_sql_column(args, "inout"), rid);
932 156454 : digits = store->table_api.column_find_int(tr, find_sql_column(args, "type_digits"), rid);
933 156454 : scale = store->table_api.column_find_int(tr, find_sql_column(args, "type_scale"), rid);
934 :
935 156454 : tpe = store->table_api.column_find_string_start(tr, find_sql_column(args, "type"), rid, &cbat);
936 156454 : if (tpe && strcmp(tpe, "clob") == 0)
937 2566 : tpe = "varchar";
938 156454 : if (!sql_find_subtype(&a->type, tpe, digits, scale)) {
939 6284 : sql_type *lt = sql_trans_bind_type(tr, f->s, tpe);
940 6284 : if (lt == NULL) {
941 0 : TRC_ERROR(SQL_STORE, "SQL type '%s' is missing\n", tpe);
942 0 : store->table_api.column_find_string_end(cbat);
943 0 : return NULL;
944 : }
945 6284 : sql_init_subtype(&a->type, lt, digits, scale);
946 : }
947 156454 : store->table_api.column_find_string_end(cbat);
948 156454 : return a;
949 : }
950 :
951 : static sql_func *
952 54395 : load_func(sql_trans *tr, sql_schema *s, sqlid fid, subrids *rs)
953 : {
954 54395 : sqlstore *store = tr->store;
955 54395 : sql_func *t = ZNEW(sql_func);
956 54395 : sql_schema *syss = find_sql_schema(tr, "sys");
957 54395 : sql_table *funcs = find_sql_table(tr, syss, "functions");
958 54395 : oid rid;
959 54395 : bool update_env; /* hacky way to update env function */
960 54395 : const char *v;
961 54395 : ptr cbat;
962 :
963 54395 : rid = store->table_api.column_find_row(tr, find_sql_column(funcs, "id"), &fid, NULL);
964 54395 : v = store->table_api.column_find_string_start(tr, find_sql_column(funcs, "name"), rid, &cbat);
965 54395 : update_env = strcmp(v, "env") == 0;
966 54395 : base_init(NULL, &t->base, fid, 0, v);
967 54395 : store->table_api.column_find_string_end(cbat);
968 54395 : v = store->table_api.column_find_string_start(tr, find_sql_column(funcs, "func"), rid, &cbat);
969 54395 : update_env = update_env && strstr(v, "EXTERNAL NAME sql.sql_environment") != NULL;
970 54395 : if (update_env) {
971 : /* see creation of env in sql_create_env()
972 : * also see upgrade code in sql_upgrades.c */
973 : v = "CREATE FUNCTION env() RETURNS TABLE( name varchar(1024), value varchar(2048)) EXTERNAL NAME inspect.\"getEnvironment\";";
974 : }
975 54395 : t->imp =_STRDUP(v);
976 54395 : store->table_api.column_find_string_end(cbat);
977 54395 : if (update_env) {
978 : v = "inspect";
979 : } else {
980 54395 : v = store->table_api.column_find_string_start(tr, find_sql_column(funcs, "mod"), rid, &cbat);
981 : }
982 54395 : if (strcmp(v, "pyapi") == 0 || /* pyapi module no longer used */
983 54395 : strcmp(v, "pyapi3map") == 0) /* pyapi3map module no longer used */
984 0 : t->mod =_STRDUP("pypapi3");
985 : else
986 54395 : t->mod =_STRDUP(v);
987 54395 : if (!update_env)
988 54395 : store->table_api.column_find_string_end(cbat);
989 54395 : t->lang = (sql_flang) store->table_api.column_find_int(tr, find_sql_column(funcs, "language"), rid);
990 54395 : t->instantiated = t->lang != FUNC_LANG_SQL && t->lang != FUNC_LANG_MAL;
991 54395 : t->type = (sql_ftype) store->table_api.column_find_int(tr, find_sql_column(funcs, "type"), rid);
992 54395 : t->side_effect = (bool) store->table_api.column_find_bte(tr, find_sql_column(funcs, "side_effect"), rid);
993 54395 : t->varres = (bool) store->table_api.column_find_bte(tr, find_sql_column(funcs, "varres"), rid);
994 54395 : t->vararg = (bool) store->table_api.column_find_bte(tr, find_sql_column(funcs, "vararg"), rid);
995 54395 : t->system = (bool) store->table_api.column_find_bte(tr, find_sql_column(funcs, "system"), rid);
996 54395 : t->semantics = (bool) store->table_api.column_find_bte(tr, find_sql_column(funcs, "semantics"), rid);
997 54395 : t->res = NULL;
998 54395 : t->s = s;
999 54395 : t->fix_scale = SCALE_EQ;
1000 54395 : t->sa = NULL;
1001 54395 : if (t->lang != FUNC_LANG_INT) {
1002 54395 : t->query = t->imp;
1003 54395 : t->imp = NULL;
1004 : }
1005 : /* convert old PYTHON2 and PYTHON2_MAP to PYTHON and PYTHON_MAP
1006 : * see also function sql_update_jun2020() in sql_upgrades.c */
1007 54395 : if ((int) t->lang == 7 || (int) t->lang == 8) /* MAP_PY old FUNC_LANG_PY2 */
1008 0 : t->lang = FUNC_LANG_PY;
1009 54395 : else if ((int) t->lang == 9 || (int) t->lang == 11) /* old FUNC_LANG_MAP_PY2 or MAP_PY3 */
1010 0 : t->lang = FUNC_LANG_PY;
1011 54395 : if (LANG_EXT(t->lang)) { /* instantiate functions other than sql and mal */
1012 76 : switch(t->type) {
1013 10 : case F_AGGR:
1014 10 : t->imp =_STRDUP("eval_aggr");
1015 10 : break;
1016 0 : case F_LOADER:
1017 0 : t->imp =_STRDUP("eval_loader");
1018 0 : break;
1019 66 : default: /* for every other function type at the moment */
1020 66 : t->imp =_STRDUP("eval");
1021 : }
1022 : }
1023 :
1024 54395 : TRC_DEBUG(SQL_STORE, "Load function: %s\n", t->base.name);
1025 :
1026 54395 : t->ops = list_create((fdestroy) &arg_destroy);
1027 54395 : if (rs) {
1028 209377 : for (rid = store->table_api.subrids_next(rs); !is_oid_nil(rid); rid = store->table_api.subrids_next(rs)) {
1029 156454 : sql_arg *a = load_arg(tr, t, rid);
1030 :
1031 156454 : if (a == NULL)
1032 : return NULL;
1033 156454 : if (a->inout == ARG_OUT) {
1034 71702 : if (!t->res)
1035 45801 : t->res = list_create((fdestroy) &arg_destroy);
1036 71702 : list_append(t->res, a);
1037 : } else {
1038 84752 : list_append(t->ops, a);
1039 : }
1040 : }
1041 : }
1042 : return t;
1043 : }
1044 :
1045 : static sql_sequence *
1046 36 : load_seq(sql_trans *tr, sql_schema * s, oid rid)
1047 : {
1048 36 : sqlstore *store = tr->store;
1049 36 : sql_sequence *seq = ZNEW(sql_sequence);
1050 36 : sql_schema *syss = find_sql_schema(tr, "sys");
1051 36 : sql_table *seqs = find_sql_table(tr, syss, "sequences");
1052 36 : sqlid sid;
1053 36 : const char *v;
1054 36 : ptr cbat;
1055 :
1056 36 : sid = store->table_api.column_find_sqlid(tr, find_sql_column(seqs, "id"), rid);
1057 36 : v = store->table_api.column_find_string_start(tr, find_sql_column(seqs, "name"), rid, &cbat);
1058 36 : base_init(NULL, &seq->base, sid, 0, v);
1059 36 : store->table_api.column_find_string_end(cbat);
1060 36 : seq->start = store->table_api.column_find_lng(tr, find_sql_column(seqs, "start"), rid);
1061 36 : seq->minvalue = store->table_api.column_find_lng(tr, find_sql_column(seqs, "minvalue"), rid);
1062 36 : seq->maxvalue = store->table_api.column_find_lng(tr, find_sql_column(seqs, "maxvalue"), rid);
1063 36 : seq->increment = store->table_api.column_find_lng(tr, find_sql_column(seqs, "increment"), rid);
1064 36 : seq->cacheinc = store->table_api.column_find_lng(tr, find_sql_column(seqs, "cacheinc"), rid);
1065 36 : seq->cycle = (bit) store->table_api.column_find_bte(tr, find_sql_column(seqs, "cycle"), rid);
1066 36 : seq->s = s;
1067 36 : return seq;
1068 : }
1069 :
1070 : static void
1071 1344 : sql_trans_update_schema(sql_trans *tr, oid rid)
1072 : {
1073 1344 : sqlstore *store = tr->store;
1074 1344 : sql_schema *s = NULL, *syss = find_sql_schema(tr, "sys");
1075 1344 : sql_table *ss = find_sql_table(tr, syss, "schemas");
1076 1344 : sqlid sid;
1077 1344 : str v;
1078 :
1079 1344 : sid = store->table_api.column_find_sqlid(tr, find_sql_column(ss, "id"), rid);
1080 1344 : s = find_sql_schema_id(tr, sid);
1081 :
1082 1344 : if (s==NULL)
1083 : return ;
1084 :
1085 1344 : TRC_DEBUG(SQL_STORE, "Update schema: %s %d\n", s->base.name, s->base.id);
1086 :
1087 1344 : v = store->table_api.column_find_value(tr, find_sql_column(ss, "name"), rid);
1088 1344 : if (!v)
1089 : return; /* TODO do better than this */
1090 1344 : _DELETE(s->base.name);
1091 1344 : base_init(NULL, &s->base, sid, 0, v);
1092 1344 : _DELETE(v);
1093 1344 : s->auth_id = store->table_api.column_find_sqlid(tr, find_sql_column(ss, "authorization"), rid);
1094 1344 : s->system = (bit) store->table_api.column_find_bte(tr, find_sql_column(ss, "system"), rid);
1095 1344 : s->owner = store->table_api.column_find_sqlid(tr, find_sql_column(ss, "owner"), rid);
1096 : }
1097 :
1098 : static sql_schema *
1099 663 : load_schema(sql_trans *tr, res_table *rt_schemas, res_table *rt_tables, res_table *rt_parts,
1100 : res_table *rt_cols, res_table *rt_idx, res_table *rt_idxcols,
1101 : res_table *rt_keys, res_table *rt_keycols,
1102 : res_table *rt_triggers, res_table *rt_triggercols)
1103 : {
1104 663 : sqlstore *store = tr->store;
1105 663 : sql_schema *s = NULL, *syss = find_sql_schema(tr, "sys");
1106 663 : sql_table *ss = find_sql_table(tr, syss, "schemas");
1107 663 : sql_table *types = find_sql_table(tr, syss, "types");
1108 663 : sql_table *tables = find_sql_table(tr, syss, "_tables");
1109 663 : sql_table *funcs = find_sql_table(tr, syss, "functions");
1110 663 : sql_table *seqs = find_sql_table(tr, syss, "sequences");
1111 663 : sql_column *type_schema, *type_id, *table_schema, *table_id;
1112 663 : sql_column *func_schema, *func_id, *seq_schema, *seq_id;
1113 663 : rids *rs;
1114 :
1115 663 : sqlid sid = *(sqlid*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "id"));
1116 663 : if (instore(sid)) {
1117 210 : s = find_sql_schema_id(tr, sid);
1118 :
1119 210 : if (s==NULL) {
1120 0 : char *name = (char*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "name"));
1121 0 : s = find_sql_schema(tr, name);
1122 0 : if (s == NULL) {
1123 0 : GDKerror("SQL schema missing or incompatible, rebuild from archive");
1124 0 : return NULL;
1125 : }
1126 : }
1127 210 : s->base.id = sid;
1128 : } else {
1129 453 : s = ZNEW(sql_schema);
1130 453 : if (s == NULL)
1131 : return NULL;
1132 453 : char *name = (char*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "name"));
1133 453 : base_init(NULL, &s->base, sid, 0, name);
1134 453 : s->auth_id = *(sqlid*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "authorization"));
1135 453 : s->system = *(bte*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "system"));
1136 453 : s->owner = *(sqlid*)store->table_api.table_fetch_value(rt_schemas, find_sql_column(ss, "owner"));
1137 :
1138 453 : s->tables = os_new(NULL, (destroy_fptr) &table_destroy, false, true, true, false, store);
1139 453 : s->types = os_new(NULL, (destroy_fptr) &type_destroy, false, true, true, false, store);
1140 453 : s->funcs = os_new(NULL, (destroy_fptr) &func_destroy, false, false, false, false, store);
1141 453 : s->seqs = os_new(NULL, (destroy_fptr) &seq_destroy, false, true, true, false, store);
1142 453 : s->keys = os_new(NULL, (destroy_fptr) &key_destroy, false, true, true, false, store);
1143 453 : s->idxs = os_new(NULL, (destroy_fptr) &idx_destroy, false, true, true, false, store);
1144 453 : s->triggers = os_new(NULL, (destroy_fptr) &trigger_destroy, false, true, true, false, store);
1145 453 : s->parts = os_new(NULL, (destroy_fptr) &part_destroy, false, false, true, false, store);
1146 : }
1147 :
1148 663 : TRC_DEBUG(SQL_STORE, "Load schema: %s %d\n", s->base.name, s->base.id);
1149 :
1150 663 : sqlid tmpid = FUNC_OIDS;
1151 :
1152 : /* first load simple types */
1153 663 : type_schema = find_sql_column(types, "schema_id");
1154 663 : type_id = find_sql_column(types, "id");
1155 663 : rs = store->table_api.rids_select(tr, type_schema, &s->base.id, &s->base.id, type_id, &tmpid, NULL, NULL);
1156 663 : if (!rs) {
1157 0 : schema_destroy(store, s);
1158 0 : return NULL;
1159 : }
1160 1091 : for (oid rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
1161 428 : sql_type *t = load_type(tr, s, rid);
1162 428 : if (os_add(s->types, tr, t->base.name, &t->base)) {
1163 0 : schema_destroy(store, s);
1164 0 : store->table_api.rids_destroy(rs);
1165 0 : return NULL;
1166 : }
1167 : }
1168 663 : store->table_api.rids_destroy(rs);
1169 :
1170 : /* second tables */
1171 663 : table_schema = find_sql_column(tables, "schema_id");
1172 663 : table_id = find_sql_column(tables, "id");
1173 16789 : for ( ; rt_tables->cur_row < rt_tables->nr_rows; rt_tables->cur_row++) {
1174 16017 : sqlid nsid = *(sqlid*)store->table_api.table_fetch_value(rt_tables, table_schema);
1175 16017 : sqlid tid = *(sqlid*)store->table_api.table_fetch_value(rt_tables, table_id);
1176 16017 : if (nsid > s->base.id)
1177 : break;
1178 15463 : if (nsid < s->base.id)
1179 0 : assert(0);
1180 28721 : if (!instore(tid)) {
1181 13258 : sql_table *t = load_table(tr, s, rt_tables, rt_parts,
1182 : rt_cols, rt_idx, rt_idxcols, rt_keys, rt_keycols, rt_triggers, rt_triggercols, tid);
1183 13258 : if (t == NULL && store->debug&8) /* try to continue without this table */
1184 0 : continue;
1185 0 : if (t == NULL) {
1186 0 : schema_destroy(store, s);
1187 0 : return NULL;
1188 : }
1189 13258 : if (os_add(s->tables, tr, t->base.name, &t->base)) {
1190 0 : schema_destroy(store, s);
1191 0 : return NULL;
1192 : }
1193 : }
1194 : }
1195 :
1196 : /* next functions which could use these types */
1197 663 : func_schema = find_sql_column(funcs, "schema_id");
1198 663 : func_id = find_sql_column(funcs, "id");
1199 663 : rs = store->table_api.rids_select(tr, func_schema, &s->base.id, &s->base.id, func_id, &tmpid, NULL, NULL);
1200 663 : if (rs == NULL) {
1201 0 : schema_destroy(store, s);
1202 0 : return NULL;
1203 : }
1204 663 : if (!store->table_api.rids_empty(rs)) {
1205 439 : sql_table *args = find_sql_table(tr, syss, "args");
1206 439 : sql_column *arg_func_id = find_sql_column(args, "func_id");
1207 439 : sql_column *arg_number = find_sql_column(args, "number");
1208 439 : subrids *nrs = store->table_api.subrids_create(tr, rs, func_id, arg_func_id, arg_number);
1209 439 : if (!nrs) {
1210 0 : store->table_api.rids_destroy(rs);
1211 0 : schema_destroy(store, s);
1212 0 : return NULL;
1213 : }
1214 439 : sqlid fid;
1215 439 : sql_func *f;
1216 :
1217 53362 : for (fid = store->table_api.subrids_nextid(nrs); fid >= 0; fid = store->table_api.subrids_nextid(nrs)) {
1218 52923 : f = load_func(tr, s, fid, nrs);
1219 52923 : if (f == NULL) {
1220 0 : store->table_api.subrids_destroy(nrs);
1221 0 : store->table_api.rids_destroy(rs);
1222 0 : schema_destroy(store, s);
1223 0 : return NULL;
1224 : }
1225 52923 : if (os_add(s->funcs, tr, f->base.name, &f->base)) {
1226 0 : schema_destroy(store, s);
1227 0 : return NULL;
1228 : }
1229 : }
1230 : /* Handle all procedures without arguments (no args) */
1231 439 : rs = store->table_api.rids_diff(tr, rs, func_id, nrs, arg_func_id);
1232 439 : if (!rs) {
1233 0 : store->table_api.subrids_destroy(nrs);
1234 0 : schema_destroy(store, s);
1235 0 : return NULL;
1236 : }
1237 1911 : for (oid rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
1238 1472 : fid = store->table_api.column_find_sqlid(tr, func_id, rid);
1239 1472 : f = load_func(tr, s, fid, NULL);
1240 1472 : if (f == NULL) {
1241 0 : store->table_api.subrids_destroy(nrs);
1242 0 : store->table_api.rids_destroy(rs);
1243 0 : schema_destroy(store, s);
1244 0 : return NULL;
1245 : }
1246 1472 : if (os_add(s->funcs, tr, f->base.name, &f->base)) {
1247 0 : store->table_api.subrids_destroy(nrs);
1248 0 : store->table_api.rids_destroy(rs);
1249 0 : schema_destroy(store, s);
1250 0 : return NULL;
1251 : }
1252 : }
1253 439 : store->table_api.subrids_destroy(nrs);
1254 : }
1255 663 : store->table_api.rids_destroy(rs);
1256 :
1257 : /* last sequence numbers */
1258 663 : seq_schema = find_sql_column(seqs, "schema_id");
1259 663 : seq_id = find_sql_column(seqs, "id");
1260 663 : rs = store->table_api.rids_select(tr, seq_schema, &s->base.id, &s->base.id, seq_id, &tmpid, NULL, NULL);
1261 663 : if (rs == NULL) {
1262 0 : schema_destroy(store, s);
1263 0 : return NULL;
1264 : }
1265 699 : for (oid rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
1266 36 : sql_sequence *seq = load_seq(tr, s, rid);
1267 36 : if (os_add(s->seqs, tr, seq->base.name, &seq->base)) {
1268 0 : schema_destroy(store, s);
1269 0 : store->table_api.rids_destroy(rs);
1270 0 : return NULL;
1271 : }
1272 : }
1273 663 : store->table_api.rids_destroy(rs);
1274 :
1275 663 : struct os_iter oi;
1276 663 : os_iterator(&oi, s->tables, tr, NULL);
1277 16126 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
1278 15463 : sql_table *t = (sql_table*)b;
1279 15463 : if (isMergeTable(t) || isReplicaTable(t)) {
1280 99 : sql_table *objects = find_sql_table(tr, syss, "objects");
1281 99 : sql_column *mt_nr = find_sql_column(objects, "nr");
1282 99 : sql_column *mt_sub = find_sql_column(objects, "sub");
1283 99 : rids *rs = store->table_api.rids_select(tr, mt_nr, &t->base.id, &t->base.id, NULL);
1284 99 : if (rs == NULL) {
1285 0 : schema_destroy(store, s);
1286 0 : return NULL;
1287 : }
1288 99 : rs = store->table_api.rids_orderby(tr, rs, mt_sub);
1289 99 : if (rs == NULL) {
1290 0 : schema_destroy(store, s);
1291 0 : return NULL;
1292 : }
1293 312 : for (oid rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
1294 213 : sql_part *pt = load_part(tr, t, rid);
1295 213 : if (isRangePartitionTable(t)) {
1296 66 : if (load_range_partition(tr, syss, pt) < 0) {
1297 0 : schema_destroy(store, s);
1298 0 : return NULL;
1299 : }
1300 147 : } else if (isListPartitionTable(t)) {
1301 33 : load_value_partition(tr, syss, pt);
1302 : }
1303 213 : if (os_add(s->parts, tr, pt->base.name, dup_base(&pt->base))) {
1304 0 : schema_destroy(store, s);
1305 0 : store->table_api.rids_destroy(rs);
1306 0 : return NULL;
1307 : }
1308 : }
1309 99 : store->table_api.rids_destroy(rs);
1310 : }
1311 : }
1312 : return s;
1313 : }
1314 :
1315 : int
1316 224 : sql_trans_update_schemas(sql_trans* tr)
1317 : {
1318 224 : sqlstore *store = tr->store;
1319 224 : sql_schema *syss = find_sql_schema(tr, "sys");
1320 224 : sql_table *sysschema = find_sql_table(tr, syss, "schemas");
1321 224 : sql_column *sysschema_ids = find_sql_column(sysschema, "id");
1322 224 : rids *schemas = store->table_api.rids_select(tr, sysschema_ids, NULL, NULL);
1323 224 : oid rid;
1324 :
1325 224 : if (schemas == NULL)
1326 : return -1;
1327 224 : TRC_DEBUG(SQL_STORE, "Update schemas\n");
1328 :
1329 1568 : for (rid = store->table_api.rids_next(schemas); !is_oid_nil(rid); rid = store->table_api.rids_next(schemas)) {
1330 1344 : sql_trans_update_schema(tr, rid);
1331 : }
1332 224 : store->table_api.rids_destroy(schemas);
1333 224 : return 0;
1334 : }
1335 :
1336 : static bool
1337 105 : load_trans(sql_trans* tr)
1338 : {
1339 105 : sqlstore *store = tr->store;
1340 105 : sql_schema *syss = find_sql_schema(tr, "sys");
1341 105 : sql_table *sysschema = find_sql_table(tr, syss, "schemas");
1342 105 : sql_table *systables = find_sql_table(tr, syss, "_tables");
1343 105 : sql_table *sysparts = find_sql_table(tr, syss, "table_partitions");
1344 105 : sql_table *syscols = find_sql_table(tr, syss, "_columns");
1345 105 : sql_table *sysidx = find_sql_table(tr, syss, "idxs");
1346 105 : sql_table *syskeys = find_sql_table(tr, syss, "keys");
1347 105 : sql_table *systriggers = find_sql_table(tr, syss, "triggers");
1348 105 : sql_table *sysobjects = find_sql_table(tr, syss, "objects");
1349 105 : sql_column *sysschema_ids = find_sql_column(sysschema, "id");
1350 105 : bool ok = true;
1351 :
1352 105 : TRC_DEBUG(SQL_STORE, "Load transaction\n");
1353 105 : assert(sysschema);
1354 105 : assert(systables);
1355 105 : assert(sysparts);
1356 105 : assert(syscols);
1357 105 : assert(sysidx);
1358 105 : assert(syskeys);
1359 105 : assert(systriggers);
1360 105 : assert(sysobjects);
1361 105 : assert(sysschema_ids);
1362 :
1363 105 : res_table *rt_schemas = store->table_api.table_orderby(tr, sysschema, NULL, NULL, NULL, NULL, sysschema_ids, NULL);
1364 105 : res_table *rt_tables = store->table_api.table_orderby(tr, systables, NULL, NULL, NULL, NULL,
1365 : find_sql_column(systables, "schema_id"),
1366 : find_sql_column(systables, "id"), NULL);
1367 105 : res_table *rt_parts = store->table_api.table_orderby(tr, sysparts,
1368 : find_sql_column(sysparts, "table_id"),
1369 : find_sql_column(systables, "id"),
1370 : NULL, NULL,
1371 : find_sql_column(systables, "schema_id"), /* order also on schema_id */
1372 : find_sql_column(sysparts, "table_id"), NULL);
1373 105 : res_table *rt_cols = store->table_api.table_orderby(tr, syscols,
1374 : find_sql_column(syscols, "table_id"),
1375 : find_sql_column(systables, "id"),
1376 : NULL, NULL,
1377 : find_sql_column(systables, "schema_id"), /* order also on schema_id */
1378 : find_sql_column(syscols, "table_id"),
1379 : find_sql_column(syscols, "number"), NULL);
1380 :
1381 105 : res_table *rt_idx = store->table_api.table_orderby(tr, sysidx,
1382 : find_sql_column(sysidx, "table_id"),
1383 : find_sql_column(systables, "id"),
1384 : NULL, NULL,
1385 : find_sql_column(systables, "schema_id"), /* order also on schema_id */
1386 : find_sql_column(sysidx, "table_id"),
1387 : find_sql_column(sysidx, "id"), NULL);
1388 105 : res_table *rt_idxcols = store->table_api.table_orderby(tr, sysobjects,
1389 : find_sql_column(sysobjects, "id"),
1390 : find_sql_column(sysidx, "id"),
1391 : find_sql_column(sysidx, "table_id"),
1392 : find_sql_column(systables, "id"),
1393 : find_sql_column(systables, "schema_id"), /* order also on schema_id, table_id */
1394 : find_sql_column(sysidx, "table_id"),
1395 : find_sql_column(sysobjects, "id"),
1396 : find_sql_column(sysobjects, "nr"), NULL);
1397 105 : res_table *rt_keys = store->table_api.table_orderby(tr, syskeys,
1398 : find_sql_column(syskeys, "table_id"),
1399 : find_sql_column(systables, "id"),
1400 : NULL, NULL,
1401 : find_sql_column(systables, "schema_id"), /* order also on schema_id */
1402 : find_sql_column(syskeys, "table_id"),
1403 : find_sql_column(syskeys, "id"), NULL);
1404 105 : res_table *rt_keycols = store->table_api.table_orderby(tr, sysobjects,
1405 : find_sql_column(sysobjects, "id"),
1406 : find_sql_column(syskeys, "id"),
1407 : find_sql_column(syskeys, "table_id"),
1408 : find_sql_column(systables, "id"),
1409 : find_sql_column(systables, "schema_id"), /* order also on schema_id, table_id */
1410 : find_sql_column(syskeys, "table_id"),
1411 : find_sql_column(sysobjects, "id"),
1412 : find_sql_column(sysobjects, "nr"), NULL);
1413 105 : res_table *rt_tabletriggers = store->table_api.table_orderby(tr, systriggers,
1414 : find_sql_column(systriggers, "table_id"),
1415 : find_sql_column(systables, "id"),
1416 : NULL, NULL,
1417 : find_sql_column(systables, "schema_id"), /* order also on schema_id */
1418 : find_sql_column(systriggers, "table_id"),
1419 : find_sql_column(systriggers, "id"), NULL);
1420 105 : res_table *rt_triggercols = store->table_api.table_orderby(tr, sysobjects,
1421 : find_sql_column(sysobjects, "id"),
1422 : find_sql_column(systriggers, "id"),
1423 : find_sql_column(systriggers, "table_id"),
1424 : find_sql_column(systables, "id"),
1425 : find_sql_column(systables, "schema_id"), /* order also on schema_id, table_id */
1426 : find_sql_column(systriggers, "table_id"),
1427 : find_sql_column(sysobjects, "id"),
1428 : find_sql_column(sysobjects, "nr"), NULL);
1429 105 : res_table *rt_triggers = store->table_api.table_orderby(tr, systriggers,
1430 : NULL, NULL, NULL, NULL,
1431 : find_sql_column(systriggers, "id"),
1432 : find_sql_column(systriggers, "table_id"), NULL);
1433 873 : for ( ; rt_schemas->cur_row < rt_schemas->nr_rows; rt_schemas->cur_row++) {
1434 663 : sql_schema *ns = load_schema(tr, rt_schemas, rt_tables, rt_parts,
1435 : rt_cols, rt_idx, rt_idxcols, rt_keys, rt_keycols, rt_tabletriggers, rt_triggercols);
1436 663 : if (ns == NULL) {
1437 0 : ok = false;
1438 0 : goto finish;
1439 : }
1440 1116 : if (!instore(ns->base.id)) {
1441 453 : if (os_add(tr->cat->schemas, tr, ns->base.name, &ns->base)) {
1442 0 : ok = false;
1443 0 : goto finish;
1444 : }
1445 453 : if (isTempSchema(ns))
1446 0 : tr->tmp = ns;
1447 : }
1448 : }
1449 105 : if (rt_triggers) {
1450 229 : for ( ; rt_triggers->cur_row < rt_triggers->nr_rows; rt_triggers->cur_row++) {
1451 124 : sqlid ntid = *(sqlid*)store->table_api.table_fetch_value(rt_triggers, find_sql_column(systriggers, "table_id"));
1452 124 : if (ntid != int_nil)
1453 124 : continue;
1454 0 : sql_trigger *k = load_trigger(tr, NULL, rt_triggers, NULL);
1455 :
1456 0 : if (!k || os_add(syss->triggers, tr, k->base.name, &k->base)) {
1457 0 : ok = false;
1458 0 : goto finish;
1459 : }
1460 : }
1461 : }
1462 :
1463 105 : finish:
1464 105 : store->table_api.table_result_destroy(rt_schemas);
1465 105 : store->table_api.table_result_destroy(rt_tables);
1466 105 : store->table_api.table_result_destroy(rt_parts);
1467 105 : store->table_api.table_result_destroy(rt_cols);
1468 105 : store->table_api.table_result_destroy(rt_idx);
1469 105 : store->table_api.table_result_destroy(rt_idxcols);
1470 105 : store->table_api.table_result_destroy(rt_keys);
1471 105 : store->table_api.table_result_destroy(rt_keycols);
1472 105 : store->table_api.table_result_destroy(rt_tabletriggers);
1473 105 : store->table_api.table_result_destroy(rt_triggercols);
1474 105 : store->table_api.table_result_destroy(rt_triggers);
1475 105 : return ok;
1476 : }
1477 :
1478 : static sqlid
1479 1396992 : next_oid(sqlstore *store)
1480 : {
1481 1396992 : sqlid id = 0;
1482 1396992 : MT_lock_set(&store->lock);
1483 1396992 : id = store->obj_id++;
1484 1396992 : assert(id < 2000000000);
1485 1396992 : MT_lock_unset(&store->lock);
1486 1396992 : return id;
1487 : }
1488 :
1489 : sqlid
1490 31038 : store_next_oid(sqlstore *store)
1491 : {
1492 31038 : return next_oid(store);
1493 : }
1494 :
1495 : static int
1496 224 : insert_schemas(sql_trans *tr)
1497 : {
1498 224 : int res = LOG_OK;
1499 224 : sqlstore *store = tr->store;
1500 224 : sql_schema *syss = find_sql_schema(tr, "sys");
1501 224 : sql_table *sysschema = find_sql_table(tr, syss, "schemas");
1502 224 : sql_table *systable = find_sql_table(tr, syss, "_tables");
1503 224 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
1504 224 : node *o;
1505 :
1506 224 : struct os_iter si;
1507 224 : os_iterator(&si, tr->cat->schemas, tr, NULL);
1508 896 : for (sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
1509 672 : sql_schema *s = (sql_schema*)b;
1510 672 : char *strnil = (char*)ATOMnilptr(TYPE_str);
1511 :
1512 672 : if (isDeclaredSchema(s))
1513 224 : continue;
1514 448 : if ((res = store->table_api.table_insert(tr, sysschema, &s->base.id, &s->base.name, &s->auth_id, &s->owner, &s->system)))
1515 0 : return res;
1516 448 : struct os_iter oi;
1517 448 : os_iterator(&oi, s->tables, tr, NULL);
1518 5152 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
1519 4704 : sql_table *t = (sql_table*)b;
1520 4704 : sht ca = t->commit_action;
1521 :
1522 4704 : if ((res = store->table_api.table_insert(tr, systable, &t->base.id, &t->base.name, &s->base.id, &strnil, &t->type, &t->system, &ca, &t->access)))
1523 0 : return res;
1524 36960 : for (o = t->columns->l->h; o; o = o->next) {
1525 32256 : sql_column *c = o->data;
1526 :
1527 32256 : if ((res = store->table_api.table_insert(tr, syscolumn, &c->base.id, &c->base.name, &c->type.type->base.name, &c->type.digits, &c->type.scale,
1528 32256 : &t->base.id, (c->def) ? &c->def : &strnil, &c->null, &c->colnr, (c->storage_type)? &c->storage_type : &strnil)))
1529 0 : return res;
1530 : }
1531 : }
1532 : }
1533 : return res;
1534 : }
1535 :
1536 : static int
1537 224 : insert_types(sql_trans *tr, sql_table *systype)
1538 : {
1539 224 : int res = LOG_OK;
1540 224 : sqlstore *store = tr->store;
1541 7162 : for (node *n = types->h; n; n = n->next) {
1542 6938 : sql_type *t = n->data;
1543 6938 : int radix = t->radix, eclass = (int) t->eclass;
1544 6938 : sqlid next_schema = t->s ? t->s->base.id : 0;
1545 :
1546 6938 : if ((res = store->table_api.table_insert(tr, systype, &t->base.id, &t->impl, &t->base.name, &t->digits, &t->scale, &radix, &eclass, &next_schema)))
1547 0 : return res;
1548 : }
1549 : return res;
1550 : }
1551 :
1552 : static int
1553 319752 : insert_args(sql_trans *tr, sql_table *sysarg, list *args, sqlid funcid, const char *arg_def, int *number)
1554 : {
1555 319752 : int res = LOG_OK;
1556 319752 : sqlstore *store = tr->store;
1557 746068 : for (node *n = args->h; n; n = n->next) {
1558 426316 : sql_arg *a = n->data;
1559 426316 : sqlid id = next_oid(tr->store);
1560 426316 : int next_number = (*number)++;
1561 426316 : char buf[32], *next_name;
1562 :
1563 426316 : if (a->name) {
1564 0 : next_name = a->name;
1565 : } else {
1566 426316 : snprintf(buf, sizeof(buf), arg_def, next_number);
1567 426316 : next_name = buf;
1568 : }
1569 426316 : if ((res = store->table_api.table_insert(tr, sysarg, &id, &funcid, &next_name, &a->type.type->base.name, &a->type.digits, &a->type.scale, &a->inout, &next_number)))
1570 0 : return res;
1571 : }
1572 : return res;
1573 : }
1574 :
1575 : static int
1576 224 : insert_functions(sql_trans *tr, sql_table *sysfunc, list *funcs_list, sql_table *sysarg)
1577 : {
1578 224 : int res = LOG_OK;
1579 224 : sqlstore *store = tr->store;
1580 211620 : for (node *n = funcs_list->h; n; n = n->next) {
1581 211396 : sql_func *f = n->data;
1582 211396 : int number = 0, ftype = (int) f->type, flang = (int) FUNC_LANG_INT;
1583 211396 : sqlid next_schema = f->s ? f->s->base.id : 0;
1584 211396 : bit se = f->side_effect, vares = f->varres, varg = f->vararg, system = f->system, sem = f->semantics;
1585 :
1586 211396 : if (f->private) /* don't serialize private functions because they cannot be seen by users */
1587 51296 : continue;
1588 160100 : if ((res = store->table_api.table_insert(tr, sysfunc, &f->base.id, &f->base.name, &f->imp, &f->mod, &flang, &ftype, &se, &vares, &varg, &next_schema, &system, &sem)))
1589 0 : return res;
1590 160100 : if (f->res && (res = insert_args(tr, sysarg, f->res, f->base.id, "res_%d", &number)))
1591 0 : return res;
1592 160100 : if (f->ops && (res = insert_args(tr, sysarg, f->ops, f->base.id, "arg_%d", &number)))
1593 0 : return res;
1594 : }
1595 : return res;
1596 : }
1597 :
1598 : static int
1599 312823 : table_next_column_nr(sql_table *t)
1600 : {
1601 312823 : int nr = ol_length(t->columns);
1602 312823 : if (nr) {
1603 271597 : node *n = ol_last_node(t->columns);
1604 271597 : if (n) {
1605 271597 : sql_column *c = n->data;
1606 :
1607 271597 : nr = c->colnr+1;
1608 : }
1609 : }
1610 312823 : return nr;
1611 : }
1612 :
1613 : static sql_column *
1614 47376 : bootstrap_create_column(sql_trans *tr, sql_table *t, const char *name, sqlid id, const char *sqltype, unsigned int digits)
1615 : {
1616 47376 : sqlstore *store = tr->store;
1617 47376 : sql_column *col = ZNEW(sql_column);
1618 :
1619 47376 : if (store->obj_id <= id)
1620 8883 : store->obj_id = id+1;
1621 47376 : TRC_DEBUG(SQL_STORE, "Create column: %s\n", name);
1622 :
1623 47376 : base_init(NULL, &col->base, id, t->base.new, name);
1624 47376 : assert(col->base.id > 0);
1625 47376 : sql_find_subtype(&col->type, sqltype, digits, 0);
1626 47376 : col->def = NULL;
1627 47376 : col->null = 1;
1628 47376 : col->colnr = table_next_column_nr(t);
1629 47376 : col->t = t;
1630 47376 : col->unique = 0;
1631 47376 : col->storage_type = NULL;
1632 47376 : if (ol_add(t->columns, &col->base))
1633 : return NULL;
1634 :
1635 47376 : ATOMIC_PTR_INIT(&col->data, NULL);
1636 47376 : if (isTable(col->t))
1637 47376 : store->storage_api.create_col(tr, col);
1638 : return col;
1639 : }
1640 :
1641 : static sql_table *
1642 75128 : create_sql_table_with_id(allocator *sa, sqlid id, const char *name, sht type, bit system, int persistence, int commit_action, bte properties)
1643 : {
1644 75128 : sql_table *t = SA_ZNEW(sa, sql_table);
1645 :
1646 75128 : assert((persistence==SQL_PERSIST ||
1647 : persistence==SQL_DECLARED_TABLE ||
1648 : commit_action || type) && commit_action>=0);
1649 75128 : assert(id);
1650 75128 : base_init(sa, &t->base, id, true, name);
1651 75128 : t->type = type;
1652 75128 : t->system = system;
1653 75128 : t->persistence = (temp_t)persistence;
1654 75128 : t->commit_action = (ca_t)commit_action;
1655 75128 : t->query = NULL;
1656 75128 : t->access = 0;
1657 75128 : t->columns = ol_new(sa, (destroy_fptr) &column_destroy, NULL);
1658 75128 : t->idxs = ol_new(sa, (destroy_fptr) &idx_destroy, NULL);
1659 75128 : t->keys = ol_new(sa, (destroy_fptr) &key_destroy, NULL);
1660 75128 : t->triggers = ol_new(sa, (destroy_fptr) &trigger_destroy, NULL);
1661 75128 : if (isMergeTable(t) || isReplicaTable(t))
1662 625 : t->members = list_new(sa, (fdestroy) &part_destroy);
1663 75128 : t->pkey = NULL;
1664 75128 : t->sz = COLSIZE;
1665 75128 : t->s = NULL;
1666 75128 : t->properties = properties;
1667 75128 : memset(&t->part, 0, sizeof(t->part));
1668 75128 : ATOMIC_PTR_INIT(&t->data, NULL);
1669 75128 : return t;
1670 : }
1671 :
1672 : sql_table *
1673 32511 : create_sql_table(sqlstore *store, allocator *sa, const char *name, sht type, bit system, int persistence, int commit_action, bte properties)
1674 : {
1675 32511 : return create_sql_table_with_id(sa, next_oid(store), name, type, system, persistence, commit_action, properties);
1676 : }
1677 :
1678 : static void
1679 602746 : dup_sql_type(sql_trans *tr, sql_schema *s, sql_subtype *oc, sql_subtype *nc)
1680 : {
1681 602746 : nc->digits = oc->digits;
1682 602746 : nc->scale = oc->scale;
1683 602746 : nc->type = oc->type;
1684 602746 : if (s && nc->type->s) { /* user type */
1685 13675 : sql_type *lt = NULL;
1686 :
1687 13675 : if (s->base.id == nc->type->s->base.id) {
1688 : /* Current user type belongs to current schema. So search there for current user type. */
1689 8703 : lt = find_sql_type(tr, s, nc->type->base.name);
1690 : } else {
1691 : /* Current user type belongs to another schema in the current transaction. Search there for current user type. */
1692 4972 : lt = sql_trans_bind_type(tr, NULL, nc->type->base.name);
1693 : }
1694 13675 : if (lt == NULL)
1695 0 : GDKfatal("SQL type %s missing", nc->type->base.name);
1696 13675 : sql_init_subtype(nc, lt, nc->digits, nc->scale);
1697 : }
1698 602746 : }
1699 :
1700 : static sql_column *
1701 24458 : dup_sql_column(allocator *sa, sql_table *t, sql_column *c)
1702 : {
1703 24458 : sql_column *col = SA_ZNEW(sa, sql_column);
1704 :
1705 48916 : base_init(sa, &col->base, c->base.id, t->persistence==SQL_DECLARED_TABLE?false:c->base.new, c->base.name);
1706 24458 : col->type = c->type; /* Both types belong to the same transaction, so no dup_sql_type call is needed */
1707 24458 : col->def = NULL;
1708 24458 : if (c->def)
1709 1728 : col->def = SA_STRDUP(sa, c->def);
1710 24458 : col->null = c->null;
1711 24458 : col->colnr = c->colnr;
1712 24458 : col->t = t;
1713 24458 : col->unique = c->unique;
1714 24458 : col->storage_type = NULL;
1715 24458 : if (c->storage_type)
1716 2 : col->storage_type = SA_STRDUP(sa, c->storage_type);
1717 24458 : if (ol_add(t->columns, &col->base))
1718 0 : return NULL;
1719 : return col;
1720 : }
1721 :
1722 : static sql_part *
1723 7 : dup_sql_part(allocator *sa, sql_table *mt, sql_part *op)
1724 : {
1725 7 : sql_part *p = SA_ZNEW(sa, sql_part);
1726 :
1727 14 : base_init(sa, &p->base, op->base.id, mt->persistence==SQL_DECLARED_TABLE?false:op->base.new, op->base.name);
1728 7 : p->with_nills = op->with_nills;
1729 :
1730 7 : if (isRangePartitionTable(mt)) {
1731 3 : p->part.range.minvalue = SA_NEW_ARRAY(sa, char, op->part.range.minlength);
1732 3 : p->part.range.maxvalue = SA_NEW_ARRAY(sa, char, op->part.range.maxlength);
1733 3 : memcpy(p->part.range.minvalue, op->part.range.minvalue, op->part.range.minlength);
1734 3 : memcpy(p->part.range.maxvalue, op->part.range.maxvalue, op->part.range.maxlength);
1735 3 : p->part.range.minlength = op->part.range.minlength;
1736 3 : p->part.range.maxlength = op->part.range.maxlength;
1737 4 : } else if (isListPartitionTable(mt)) {
1738 4 : p->part.values = list_new(sa, (fdestroy) &part_value_destroy);
1739 16 : for (node *n = op->part.values->h ; n ; n = n->next) {
1740 12 : sql_part_value *prev = (sql_part_value*) n->data, *nextv = SA_ZNEW(sa, sql_part_value);
1741 12 : nextv->value = SA_NEW_ARRAY(sa, char, prev->length);
1742 12 : memcpy(nextv->value, prev->value, prev->length);
1743 12 : nextv->length = prev->length;
1744 12 : list_append(p->part.values, nextv);
1745 : }
1746 : }
1747 7 : list_append(mt->members, p);
1748 7 : p->t = mt;
1749 7 : p->member = op->member;
1750 7 : assert(p->member);
1751 7 : return p;
1752 : }
1753 :
1754 : sql_table *
1755 1597 : dup_sql_table(allocator *sa, sql_table *t)
1756 : {
1757 1597 : node *n;
1758 1597 : sql_table *nt = create_sql_table_with_id(sa, t->base.id, t->base.name, t->type, t->system, SQL_DECLARED_TABLE, t->commit_action, t->properties);
1759 :
1760 1597 : nt->base.new = t->base.new;
1761 :
1762 1597 : nt->access = t->access;
1763 1597 : nt->query = (t->query) ? SA_STRDUP(sa, t->query) : NULL;
1764 1597 : nt->s = t->s;
1765 :
1766 1597 : if (isPartitionedByExpressionTable(nt)) {
1767 7 : nt->part.pexp = SA_ZNEW(sa, sql_expression);
1768 7 : nt->part.pexp->exp = SA_STRDUP(sa, t->part.pexp->exp);
1769 7 : nt->part.pexp->type = t->part.pexp->type; /* No dup_sql_type call needed */
1770 7 : nt->part.pexp->cols = SA_LIST(sa, (fdestroy) &int_destroy);
1771 20 : for (n = t->part.pexp->cols->h; n; n = n->next) {
1772 13 : int *nid = SA_NEW(sa, int);
1773 13 : *nid = *(int *) n->data;
1774 13 : list_append(nt->part.pexp->cols, nid);
1775 : }
1776 : }
1777 :
1778 26055 : for (n = t->columns->l->h; n; n = n->next) {
1779 24458 : sql_column *c = n->data;
1780 24458 : sql_column *dup = dup_sql_column(sa, nt, c);
1781 24458 : if (isPartitionedByColumnTable(nt) && c->base.id == t->part.pcol->base.id)
1782 16 : nt->part.pcol = dup;
1783 : }
1784 :
1785 1597 : if (t->members)
1786 37 : for (n = t->members->h; n; n = n->next)
1787 7 : dup_sql_part(sa, nt, n->data);
1788 1597 : return nt;
1789 : }
1790 :
1791 : static sql_table *
1792 6909 : bootstrap_create_table(sql_trans *tr, sql_schema *s, const char *name, sqlid id)
1793 : {
1794 6909 : sqlstore *store = tr->store;
1795 6909 : int istmp = isTempSchema(s);
1796 6909 : int persistence = istmp?SQL_GLOBAL_TEMP:SQL_PERSIST;
1797 4935 : sht commit_action = istmp?CA_PRESERVE:CA_COMMIT;
1798 6909 : sql_table *t;
1799 :
1800 6909 : if (store->obj_id <= id)
1801 987 : store->obj_id = id+1;
1802 6909 : t = create_sql_table_with_id(NULL, id, name, tt_table, 1, persistence, commit_action, 0);
1803 6909 : t->bootstrap = 1;
1804 :
1805 6909 : TRC_DEBUG(SQL_STORE, "Create table: %s\n", name);
1806 :
1807 6909 : t->base.new = s->base.new;
1808 6909 : t->query = NULL;
1809 6909 : t->s = s;
1810 6909 : if (isTable(t) && store->storage_api.create_del(tr, t) != LOG_OK) {
1811 0 : table_destroy(store, t);
1812 0 : return NULL;
1813 : }
1814 6909 : if (os_add(s->tables, tr, name, &t->base)) {
1815 0 : table_destroy(store, t);
1816 0 : return NULL;
1817 : }
1818 : return t;
1819 : }
1820 :
1821 : static sql_schema *
1822 987 : bootstrap_create_schema(sql_trans *tr, const char *name, sqlid id, sqlid auth_id, int owner)
1823 : {
1824 987 : sqlstore *store = tr->store;
1825 987 : sql_schema *s = ZNEW(sql_schema);
1826 :
1827 987 : if (store->obj_id <= id)
1828 329 : store->obj_id = id+1;
1829 987 : TRC_DEBUG(SQL_STORE, "Create schema: %s %d %d\n", name, auth_id, owner);
1830 :
1831 987 : if (strcmp(name, dt_schema) == 0) {
1832 329 : base_init(NULL, &s->base, (sqlid) FUNC_OIDS - 1, true, name);
1833 : } else {
1834 658 : base_init(NULL, &s->base, id, true, name);
1835 : }
1836 987 : s->base.new = store->first;
1837 987 : s->auth_id = auth_id;
1838 987 : s->owner = owner;
1839 987 : s->system = TRUE;
1840 987 : s->tables = os_new(NULL, (destroy_fptr) &table_destroy, false, true, true, false, store);
1841 987 : s->types = os_new(NULL, (destroy_fptr) &type_destroy, false, true, true, false, store);
1842 987 : s->funcs = os_new(NULL, (destroy_fptr) &func_destroy, false, false, false, false, store);
1843 987 : s->seqs = os_new(NULL, (destroy_fptr) &seq_destroy, false, true, true, false, store);
1844 987 : s->keys = os_new(NULL, (destroy_fptr) &key_destroy, false, true, true, false, store);
1845 987 : s->idxs = os_new(NULL, (destroy_fptr) &idx_destroy, false, true, true, false, store);
1846 987 : s->triggers = os_new(NULL, (destroy_fptr) &trigger_destroy, false, true, true, false, store);
1847 987 : s->parts = os_new(NULL, (destroy_fptr) &part_destroy, false, false, true, false, store);
1848 987 : if (os_add(tr->cat->schemas, tr, s->base.name, &s->base)) {
1849 : return NULL;
1850 : }
1851 987 : if (isTempSchema(s))
1852 329 : tr->tmp = s;
1853 :
1854 987 : s->store = tr->store;
1855 987 : return s;
1856 : }
1857 :
1858 : /* TODO clean this */
1859 : static inline int
1860 68714 : dep_hash(sql_dependency_change *dep)
1861 : {
1862 68714 : return (int) BATatoms[TYPE_int].atomHash(&dep->objid);
1863 : }
1864 :
1865 : static void
1866 167596 : dep_hash_clear(sql_hash *h)
1867 : {
1868 167596 : if (h == NULL || h->sa || h->entries == 0)
1869 : return;
1870 15675 : for (int i = 0; i < h->size; i++) {
1871 15200 : sql_hash_e *e = h->buckets[i];
1872 :
1873 16268 : while (e) {
1874 1068 : sql_hash_e *next = e->chain;
1875 :
1876 1068 : _DELETE(e->value);
1877 1068 : _DELETE(e);
1878 1068 : e = next;
1879 : }
1880 15200 : h->buckets[i] = NULL;
1881 : }
1882 475 : h->entries = 0;
1883 : }
1884 :
1885 : static void
1886 658 : dep_hash_destroy(sql_hash *h)
1887 : {
1888 658 : if (h == NULL || h->sa)
1889 : return;
1890 21648 : for (int i = 0; i < h->size; i++) {
1891 20992 : sql_hash_e *e = h->buckets[i];
1892 :
1893 20992 : while (e) {
1894 0 : sql_hash_e *next = e->chain;
1895 :
1896 0 : _DELETE(e->value);
1897 0 : _DELETE(e);
1898 0 : e = next;
1899 : }
1900 : }
1901 656 : _DELETE(h->buckets);
1902 656 : _DELETE(h);
1903 : }
1904 :
1905 : static sqlstore *
1906 330 : store_load(sqlstore *store, allocator *pa)
1907 : {
1908 330 : sql_trans *tr;
1909 330 : sql_table *t, *types, *functions, *arguments;
1910 330 : sql_schema *s;
1911 330 : lng lng_store_oid;
1912 :
1913 330 : assert(pa);
1914 330 : store->sa = pa;
1915 :
1916 330 : store->first = store->logger_api.log_isnew(store);
1917 :
1918 330 : if (store->first && store->readonly) {
1919 : /* cannot initialize database in readonly mode */
1920 1 : TRC_CRITICAL(SQL_STORE, "Cannot initialize store in readonly mode\n");
1921 1 : return NULL;
1922 : }
1923 :
1924 329 : types_init(store->sa); /* initialize global lists of types and functions, TODO: needs to move */
1925 :
1926 : /* we store some spare oids */
1927 329 : store->obj_id = FUNC_OIDS;
1928 :
1929 329 : tr = sql_trans_create(store, NULL, NULL);
1930 329 : if (!tr) {
1931 0 : TRC_CRITICAL(SQL_STORE, "Failed to start a transaction while loading the storage\n");
1932 0 : return NULL;
1933 : }
1934 329 : tr->store = store;
1935 329 : tr->active = 1;
1936 :
1937 : /* for now use malloc and free */
1938 329 : store->active = list_create(NULL);
1939 329 : store->dependencies = hash_new(NULL, 32, (fkeyvalue)&dep_hash);
1940 329 : store->depchanges = hash_new(NULL, 32, (fkeyvalue)&dep_hash);
1941 329 : store->sequences = hash_new(NULL, 32, (fkeyvalue)&seq_hash);
1942 329 : store->seqchanges = list_create(NULL);
1943 329 : if (!store->active || !store->dependencies || !store->depchanges || !store->sequences || !store->seqchanges) {
1944 0 : goto critical;
1945 : }
1946 :
1947 329 : s = bootstrap_create_schema(tr, "sys", 2000, ROLE_SYSADMIN, USER_MONETDB);
1948 329 : if (s == NULL) {
1949 0 : goto critical;
1950 : }
1951 329 : if (!store->first)
1952 105 : s->base.new = 0;
1953 :
1954 658 : if ((t = bootstrap_create_table(tr, s, "schemas", 2001)) == NULL ||
1955 658 : bootstrap_create_column(tr, t, "id", 2002, "int", 31) == NULL ||
1956 658 : bootstrap_create_column(tr, t, "name", 2003, "varchar", 1024) == NULL ||
1957 658 : bootstrap_create_column(tr, t, "authorization", 2004, "int", 31) == NULL ||
1958 658 : bootstrap_create_column(tr, t, "owner", 2005, "int", 31) == NULL ||
1959 329 : bootstrap_create_column(tr, t, "system", 2006, "boolean", 1) == NULL ||
1960 :
1961 658 : (types = t = bootstrap_create_table(tr, s, "types", 2007)) == NULL ||
1962 658 : bootstrap_create_column(tr, t, "id", 2008, "int", 31) == NULL ||
1963 658 : bootstrap_create_column(tr, t, "systemname", 2009, "varchar", 256) == NULL ||
1964 658 : bootstrap_create_column(tr, t, "sqlname", 2010, "varchar", 1024) == NULL ||
1965 658 : bootstrap_create_column(tr, t, "digits", 2011, "int", 31) == NULL ||
1966 658 : bootstrap_create_column(tr, t, "scale", 2012, "int", 31) == NULL ||
1967 658 : bootstrap_create_column(tr, t, "radix", 2013, "int", 31) == NULL ||
1968 658 : bootstrap_create_column(tr, t, "eclass", 2014, "int", 31) == NULL ||
1969 329 : bootstrap_create_column(tr, t, "schema_id", 2015, "int", 31) == NULL ||
1970 :
1971 658 : (functions = t = bootstrap_create_table(tr, s, "functions", 2016)) == NULL ||
1972 658 : bootstrap_create_column(tr, t, "id", 2017, "int", 31) == NULL ||
1973 658 : bootstrap_create_column(tr, t, "name", 2018, "varchar", 256) == NULL ||
1974 658 : bootstrap_create_column(tr, t, "func", 2019, "varchar", 8196) == NULL ||
1975 658 : bootstrap_create_column(tr, t, "mod", 2020, "varchar", 8196) == NULL ||
1976 :
1977 : /* language asm=0, sql=1, R=2, C=3, J=4 */
1978 658 : bootstrap_create_column(tr, t, "language", 2021, "int", 31) == NULL ||
1979 :
1980 : /* func, proc, aggr or filter */
1981 658 : bootstrap_create_column(tr, t, "type", 2022, "int", 31) == NULL ||
1982 658 : bootstrap_create_column(tr, t, "side_effect", 2023, "boolean", 1) == NULL ||
1983 658 : bootstrap_create_column(tr, t, "varres", 2024, "boolean", 1) == NULL ||
1984 658 : bootstrap_create_column(tr, t, "vararg", 2025, "boolean", 1) == NULL ||
1985 658 : bootstrap_create_column(tr, t, "schema_id", 2026, "int", 31) == NULL ||
1986 658 : bootstrap_create_column(tr, t, "system", 2027, "boolean", 1) == NULL ||
1987 329 : bootstrap_create_column(tr, t, "semantics", 2162, "boolean", 1) == NULL ||
1988 :
1989 658 : (arguments = t = bootstrap_create_table(tr, s, "args", 2028)) == NULL ||
1990 658 : bootstrap_create_column(tr, t, "id", 2029, "int", 31) == NULL ||
1991 658 : bootstrap_create_column(tr, t, "func_id", 2030, "int", 31) == NULL ||
1992 658 : bootstrap_create_column(tr, t, "name", 2031, "varchar", 256) == NULL ||
1993 658 : bootstrap_create_column(tr, t, "type", 2032, "varchar", 1024) == NULL ||
1994 658 : bootstrap_create_column(tr, t, "type_digits", 2033, "int", 31) == NULL ||
1995 658 : bootstrap_create_column(tr, t, "type_scale", 2034, "int", 31) == NULL ||
1996 658 : bootstrap_create_column(tr, t, "inout", 2035, "tinyint", 7) == NULL ||
1997 329 : bootstrap_create_column(tr, t, "number", 2036, "int", 31) == NULL ||
1998 :
1999 658 : (t = bootstrap_create_table(tr, s, "sequences", 2037)) == NULL ||
2000 658 : bootstrap_create_column(tr, t, "id", 2038, "int", 31) == NULL ||
2001 658 : bootstrap_create_column(tr, t, "schema_id", 2039, "int", 31) == NULL ||
2002 658 : bootstrap_create_column(tr, t, "name", 2040, "varchar", 256) == NULL ||
2003 658 : bootstrap_create_column(tr, t, "start", 2041, "bigint", 63) == NULL ||
2004 658 : bootstrap_create_column(tr, t, "minvalue", 2042, "bigint", 63) == NULL ||
2005 658 : bootstrap_create_column(tr, t, "maxvalue", 2043, "bigint", 63) == NULL ||
2006 658 : bootstrap_create_column(tr, t, "increment", 2044, "bigint", 63) == NULL ||
2007 658 : bootstrap_create_column(tr, t, "cacheinc", 2045, "bigint", 63) == NULL ||
2008 329 : bootstrap_create_column(tr, t, "cycle", 2046, "boolean", 1) == NULL ||
2009 :
2010 658 : (t = bootstrap_create_table(tr, s, "table_partitions", 2047)) == NULL ||
2011 658 : bootstrap_create_column(tr, t, "id", 2048, "int", 31) == NULL ||
2012 658 : bootstrap_create_column(tr, t, "table_id", 2049, "int", 31) == NULL ||
2013 658 : bootstrap_create_column(tr, t, "column_id", 2050, "int", 31) == NULL ||
2014 658 : bootstrap_create_column(tr, t, "expression", 2051, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2015 329 : bootstrap_create_column(tr, t, "type", 2052, "tinyint", 7) == NULL ||
2016 :
2017 658 : (t = bootstrap_create_table(tr, s, "range_partitions", 2053)) == NULL ||
2018 658 : bootstrap_create_column(tr, t, "table_id", 2054, "int", 31) == NULL ||
2019 658 : bootstrap_create_column(tr, t, "partition_id", 2055, "int", 31) == NULL ||
2020 658 : bootstrap_create_column(tr, t, "minimum", 2056, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2021 658 : bootstrap_create_column(tr, t, "maximum", 2057, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2022 329 : bootstrap_create_column(tr, t, "with_nulls", 2058, "boolean", 1) == NULL ||
2023 :
2024 658 : (t = bootstrap_create_table(tr, s, "value_partitions", 2059)) == NULL ||
2025 658 : bootstrap_create_column(tr, t, "table_id", 2060, "int", 31) == NULL ||
2026 658 : bootstrap_create_column(tr, t, "partition_id", 2061, "int", 31) == NULL ||
2027 329 : bootstrap_create_column(tr, t, "value", 2062, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2028 :
2029 658 : (t = bootstrap_create_table(tr, s, "dependencies", 2063)) == NULL ||
2030 658 : bootstrap_create_column(tr, t, "id", 2064, "int", 31) == NULL ||
2031 658 : bootstrap_create_column(tr, t, "depend_id", 2065, "int", 31) == NULL ||
2032 329 : bootstrap_create_column(tr, t, "depend_type", 2066, "smallint", 15) == NULL ||
2033 :
2034 :
2035 658 : (t = bootstrap_create_table(tr, s, "_tables", 2067)) == NULL ||
2036 658 : bootstrap_create_column(tr, t, "id", 2068, "int", 31) == NULL ||
2037 658 : bootstrap_create_column(tr, t, "name", 2069, "varchar", 1024) == NULL ||
2038 658 : bootstrap_create_column(tr, t, "schema_id", 2070, "int", 31) == NULL ||
2039 658 : bootstrap_create_column(tr, t, "query", 2071, "varchar", 1 << 20) == NULL||
2040 658 : bootstrap_create_column(tr, t, "type", 2072, "smallint", 15) == NULL ||
2041 658 : bootstrap_create_column(tr, t, "system", 2073, "boolean", 1) == NULL ||
2042 658 : bootstrap_create_column(tr, t, "commit_action", 2074, "smallint", 15) == NULL ||
2043 329 : bootstrap_create_column(tr, t, "access", 2075, "smallint", 15) == NULL ||
2044 :
2045 658 : (t = bootstrap_create_table(tr, s, "_columns", 2076)) == NULL ||
2046 658 : bootstrap_create_column(tr, t, "id", 2077, "int", 31) == NULL ||
2047 658 : bootstrap_create_column(tr, t, "name", 2078, "varchar", 1024) == NULL ||
2048 658 : bootstrap_create_column(tr, t, "type", 2079, "varchar", 1024) == NULL ||
2049 658 : bootstrap_create_column(tr, t, "type_digits", 2080, "int", 31) == NULL ||
2050 658 : bootstrap_create_column(tr, t, "type_scale", 2081, "int", 31) == NULL ||
2051 658 : bootstrap_create_column(tr, t, "table_id", 2082, "int", 31) == NULL ||
2052 658 : bootstrap_create_column(tr, t, "default", 2083, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2053 658 : bootstrap_create_column(tr, t, "null", 2084, "boolean", 1) == NULL ||
2054 658 : bootstrap_create_column(tr, t, "number", 2085, "int", 31) == NULL ||
2055 329 : bootstrap_create_column(tr, t, "storage", 2086, "varchar", 2048) == NULL ||
2056 :
2057 658 : (t = bootstrap_create_table(tr, s, "keys", 2087)) == NULL ||
2058 658 : bootstrap_create_column(tr, t, "id", 2088, "int", 31) == NULL ||
2059 658 : bootstrap_create_column(tr, t, "table_id", 2089, "int", 31) == NULL ||
2060 658 : bootstrap_create_column(tr, t, "type", 2090, "int", 31) == NULL ||
2061 658 : bootstrap_create_column(tr, t, "name", 2091, "varchar", 1024) == NULL ||
2062 658 : bootstrap_create_column(tr, t, "rkey", 2092, "int", 31) == NULL ||
2063 658 : bootstrap_create_column(tr, t, "action", 2093, "int", 31) == NULL ||
2064 329 : bootstrap_create_column(tr, t, "check", 2165, "varchar", 2048) == NULL ||
2065 :
2066 658 : (t = bootstrap_create_table(tr, s, "idxs", 2094)) == NULL ||
2067 658 : bootstrap_create_column(tr, t, "id", 2095, "int", 31) == NULL ||
2068 658 : bootstrap_create_column(tr, t, "table_id", 2096, "int", 31) == NULL ||
2069 658 : bootstrap_create_column(tr, t, "type", 2097, "int", 31) == NULL ||
2070 329 : bootstrap_create_column(tr, t, "name", 2098, "varchar", 1024) == NULL ||
2071 :
2072 658 : (t = bootstrap_create_table(tr, s, "triggers", 2099)) == NULL ||
2073 658 : bootstrap_create_column(tr, t, "id", 2100, "int", 31) == NULL ||
2074 658 : bootstrap_create_column(tr, t, "name", 2101, "varchar", 1024) == NULL ||
2075 658 : bootstrap_create_column(tr, t, "table_id", 2102, "int", 31) == NULL ||
2076 658 : bootstrap_create_column(tr, t, "time", 2103, "smallint", 15) == NULL ||
2077 658 : bootstrap_create_column(tr, t, "orientation", 2104, "smallint", 15) == NULL ||
2078 658 : bootstrap_create_column(tr, t, "event", 2105, "smallint", 15) == NULL ||
2079 658 : bootstrap_create_column(tr, t, "old_name", 2106, "varchar", 1024) == NULL ||
2080 658 : bootstrap_create_column(tr, t, "new_name", 2107, "varchar", 1024) == NULL ||
2081 658 : bootstrap_create_column(tr, t, "condition", 2108, "varchar", 2048) == NULL ||
2082 329 : bootstrap_create_column(tr, t, "statement", 2109, "varchar", 2048) == NULL ||
2083 :
2084 658 : (t = bootstrap_create_table(tr, s, "objects", 2110)) == NULL ||
2085 658 : bootstrap_create_column(tr, t, "id", 2111, "int", 31) == NULL ||
2086 658 : bootstrap_create_column(tr, t, "name", 2112, "varchar", 1024) == NULL ||
2087 658 : bootstrap_create_column(tr, t, "nr", 2113, "int", 31) == NULL ||
2088 329 : bootstrap_create_column(tr, t, "sub", 2163, "int", 31) == NULL) {
2089 0 : goto critical;
2090 : }
2091 :
2092 329 : if ((s = bootstrap_create_schema(tr, "tmp", 2114, ROLE_SYSADMIN, USER_MONETDB)) == NULL) {
2093 0 : goto critical;
2094 : }
2095 329 : store->tmp = s;
2096 :
2097 658 : if ((t = bootstrap_create_table(tr, s, "_tables", 2115)) == NULL ||
2098 658 : bootstrap_create_column(tr, t, "id", 2116, "int", 31) == NULL ||
2099 658 : bootstrap_create_column(tr, t, "name", 2117, "varchar", 1024) == NULL ||
2100 658 : bootstrap_create_column(tr, t, "schema_id", 2118, "int", 31) == NULL ||
2101 658 : bootstrap_create_column(tr, t, "query", 2119, "varchar", 1 << 20) == NULL ||
2102 658 : bootstrap_create_column(tr, t, "type", 2120, "smallint", 15) == NULL ||
2103 658 : bootstrap_create_column(tr, t, "system", 2121, "boolean", 1) == NULL ||
2104 658 : bootstrap_create_column(tr, t, "commit_action", 2122, "smallint", 15) == NULL ||
2105 329 : bootstrap_create_column(tr, t, "access", 2123, "smallint", 15) == NULL ||
2106 :
2107 658 : (t = bootstrap_create_table(tr, s, "_columns", 2124)) == NULL ||
2108 658 : bootstrap_create_column(tr, t, "id", 2125, "int", 31) == NULL ||
2109 658 : bootstrap_create_column(tr, t, "name", 2126, "varchar", 1024) == NULL ||
2110 658 : bootstrap_create_column(tr, t, "type", 2127, "varchar", 1024) == NULL ||
2111 658 : bootstrap_create_column(tr, t, "type_digits", 2128, "int", 31) == NULL ||
2112 658 : bootstrap_create_column(tr, t, "type_scale", 2129, "int", 31) == NULL ||
2113 658 : bootstrap_create_column(tr, t, "table_id", 2130, "int", 31) == NULL ||
2114 658 : bootstrap_create_column(tr, t, "default", 2131, "varchar", STORAGE_MAX_VALUE_LENGTH) == NULL ||
2115 658 : bootstrap_create_column(tr, t, "null", 2132, "boolean", 1) == NULL ||
2116 658 : bootstrap_create_column(tr, t, "number", 2133, "int", 31) == NULL ||
2117 329 : bootstrap_create_column(tr, t, "storage", 2134, "varchar", 2048) == NULL ||
2118 :
2119 658 : (t = bootstrap_create_table(tr, s, "keys", 2135)) == NULL ||
2120 658 : bootstrap_create_column(tr, t, "id", 2136, "int", 31) == NULL ||
2121 658 : bootstrap_create_column(tr, t, "table_id", 2137, "int", 31) == NULL ||
2122 658 : bootstrap_create_column(tr, t, "type", 2138, "int", 31) == NULL ||
2123 658 : bootstrap_create_column(tr, t, "name", 2139, "varchar", 1024) == NULL ||
2124 658 : bootstrap_create_column(tr, t, "rkey", 2140, "int", 31) == NULL ||
2125 658 : bootstrap_create_column(tr, t, "action", 2141, "int", 31) == NULL ||
2126 329 : bootstrap_create_column(tr, t, "check", 2166, "varchar", 2048) == NULL ||
2127 :
2128 658 : (t = bootstrap_create_table(tr, s, "idxs", 2142)) == NULL ||
2129 658 : bootstrap_create_column(tr, t, "id", 2143, "int", 31) == NULL ||
2130 658 : bootstrap_create_column(tr, t, "table_id", 2144, "int", 31) == NULL ||
2131 658 : bootstrap_create_column(tr, t, "type", 2145, "int", 31) == NULL ||
2132 329 : bootstrap_create_column(tr, t, "name", 2146, "varchar", 1024) == NULL ||
2133 :
2134 658 : (t = bootstrap_create_table(tr, s, "triggers", 2147)) == NULL ||
2135 658 : bootstrap_create_column(tr, t, "id", 2148, "int", 31) == NULL ||
2136 658 : bootstrap_create_column(tr, t, "name", 2149, "varchar", 1024) == NULL ||
2137 658 : bootstrap_create_column(tr, t, "table_id", 2150, "int", 31) == NULL ||
2138 658 : bootstrap_create_column(tr, t, "time", 2151, "smallint", 15) == NULL ||
2139 658 : bootstrap_create_column(tr, t, "orientation", 2152, "smallint", 15) == NULL ||
2140 658 : bootstrap_create_column(tr, t, "event", 2153, "smallint", 15) == NULL ||
2141 658 : bootstrap_create_column(tr, t, "old_name", 2154, "varchar", 1024) == NULL ||
2142 658 : bootstrap_create_column(tr, t, "new_name", 2155, "varchar", 1024) == NULL ||
2143 658 : bootstrap_create_column(tr, t, "condition", 2156, "varchar", 2048) == NULL ||
2144 329 : bootstrap_create_column(tr, t, "statement", 2157, "varchar", 2048) == NULL ||
2145 :
2146 658 : (t = bootstrap_create_table(tr, s, "objects", 2158)) == NULL ||
2147 658 : bootstrap_create_column(tr, t, "id", 2159, "int", 31) == NULL ||
2148 658 : bootstrap_create_column(tr, t, "name", 2160, "varchar", 1024) == NULL ||
2149 658 : bootstrap_create_column(tr, t, "nr", 2161, "int", 31) == NULL ||
2150 329 : bootstrap_create_column(tr, t, "sub", 2164, "int", 31) == NULL) {
2151 0 : goto critical;
2152 : }
2153 :
2154 329 : if (bootstrap_create_schema(tr, dt_schema, -1, ROLE_SYSADMIN, USER_MONETDB) == NULL) {
2155 0 : goto critical;
2156 : }
2157 :
2158 329 : if (store->first) {
2159 224 : if (insert_types(tr, types) || insert_functions(tr, functions, funcs, arguments) || insert_schemas(tr)) {
2160 0 : TRC_CRITICAL(SQL_STORE, "Cannot load catalog\n");
2161 0 : sql_trans_destroy(tr);
2162 0 : return NULL;
2163 : }
2164 : } else {
2165 105 : tr->active = 0;
2166 : }
2167 :
2168 329 : if (sql_trans_commit(tr) != SQL_OK) {
2169 0 : TRC_CRITICAL(SQL_STORE, "Cannot commit initial transaction\n");
2170 0 : sql_trans_destroy(tr);
2171 0 : return NULL;
2172 : }
2173 329 : tr->ts = store_timestamp(store);
2174 :
2175 329 : store->logger_api.get_sequence(store, OBJ_SID, &lng_store_oid);
2176 329 : store->prev_oid = (sqlid)lng_store_oid;
2177 329 : if (store->obj_id < store->prev_oid)
2178 105 : store->obj_id = store->prev_oid;
2179 :
2180 : /* load remaining schemas, tables, columns etc */
2181 329 : tr->active = 1;
2182 329 : if (!store->first && !load_trans(tr)) {
2183 0 : TRC_CRITICAL(SQL_STORE, "Cannot load catalog tables\n");
2184 0 : sql_trans_destroy(tr);
2185 0 : return NULL;
2186 : }
2187 329 : if (sql_trans_commit(tr) != SQL_OK) {
2188 0 : TRC_CRITICAL(SQL_STORE, "Cannot commit loaded objects transaction\n");
2189 0 : sql_trans_destroy(tr);
2190 0 : return NULL;
2191 : }
2192 329 : tr->active = 0;
2193 329 : sql_trans_destroy(tr);
2194 329 : store->initialized = 1;
2195 329 : return store;
2196 :
2197 0 : critical:
2198 0 : TRC_CRITICAL(SQL_STORE, "Allocation failure while initializing store\n");
2199 0 : sql_trans_destroy(tr);
2200 0 : return NULL;
2201 : }
2202 :
2203 : sqlstore *
2204 330 : store_init(int debug, store_type store_tpe, int readonly, int singleuser)
2205 : {
2206 330 : allocator *pa;
2207 330 : sqlstore *store = MNEW(sqlstore);
2208 :
2209 330 : if (debug&2)
2210 0 : GDKtracer_set_layer_level("sql_all", "debug");
2211 :
2212 330 : if (!store) {
2213 0 : TRC_CRITICAL(SQL_STORE, "Allocation failure while initializing store\n");
2214 0 : return NULL;
2215 : }
2216 :
2217 330 : if (!(pa = sa_create(NULL))) {
2218 0 : TRC_CRITICAL(SQL_STORE, "Allocation failure while initializing store\n");
2219 0 : _DELETE(store);
2220 0 : return NULL;
2221 : }
2222 :
2223 330 : *store = (sqlstore) {
2224 : .readonly = readonly,
2225 : .singleuser = singleuser,
2226 : .debug = debug,
2227 : .transaction = ATOMIC_VAR_INIT(TRANSACTION_ID_BASE),
2228 : .nr_active = ATOMIC_VAR_INIT(0),
2229 : .timestamp = ATOMIC_VAR_INIT(0),
2230 : .lastactive = ATOMIC_VAR_INIT(0),
2231 : .function_counter = ATOMIC_VAR_INIT(0),
2232 : .oldest = ATOMIC_VAR_INIT(0),
2233 : .sa = pa,
2234 : };
2235 :
2236 330 : (void)store_timestamp(store); /* increment once */
2237 330 : MT_lock_init(&store->lock, "sqlstore_lock");
2238 330 : MT_lock_init(&store->commit, "sqlstore_commit");
2239 330 : MT_lock_init(&store->flush, "sqlstore_flush");
2240 21450 : for(int i = 0; i<NR_TABLE_LOCKS; i++)
2241 21120 : MT_lock_init(&store->table_locks[i], "sqlstore_table");
2242 169290 : for(int i = 0; i<NR_COLUMN_LOCKS; i++)
2243 168960 : MT_lock_init(&store->column_locks[i], "sqlstore_column");
2244 :
2245 330 : MT_lock_set(&store->flush);
2246 330 : MT_lock_set(&store->lock);
2247 :
2248 : /* initialize empty bats */
2249 330 : switch (store_tpe) {
2250 330 : case store_bat:
2251 : case store_mem:
2252 330 : if (bat_utils_init() == -1) {
2253 0 : TRC_CRITICAL(SQL_STORE, "Allocation failure while initializing store\n");
2254 0 : MT_lock_unset(&store->lock);
2255 0 : MT_lock_unset(&store->flush);
2256 0 : store_exit(store);
2257 0 : return NULL;
2258 : }
2259 330 : bat_storage_init(&store->storage_api);
2260 330 : bat_table_init(&store->table_api);
2261 330 : bat_logger_init(&store->logger_api);
2262 330 : break;
2263 : default:
2264 : break;
2265 : }
2266 330 : store->active_type = store_tpe;
2267 330 : int v = 1;
2268 660 : if (!store->logger_api.create ||
2269 330 : store->logger_api.create(store, debug, "sql_logs", CATALOG_VERSION*v) != LOG_OK) {
2270 0 : MT_lock_unset(&store->lock);
2271 0 : MT_lock_unset(&store->flush);
2272 0 : store_exit(store);
2273 0 : return NULL;
2274 : }
2275 :
2276 : /* create the initial store structure or re-load previous data */
2277 330 : MT_lock_unset(&store->lock);
2278 330 : MT_lock_unset(&store->flush);
2279 330 : if (!store_load(store, pa)) {
2280 : /* zap current change list */
2281 1 : store->changes = NULL;
2282 1 : store_exit(store);
2283 1 : return NULL;
2284 : }
2285 : return store;
2286 : }
2287 :
2288 : void
2289 329 : store_exit(sqlstore *store)
2290 : {
2291 329 : allocator *sa = store->sa;
2292 329 : MT_lock_set(&store->commit);
2293 329 : MT_lock_set(&store->flush);
2294 329 : MT_lock_set(&store->lock);
2295 :
2296 329 : TRC_DEBUG(SQL_STORE, "Store locked\n");
2297 :
2298 329 : if (store->cat) {
2299 328 : while (ATOMIC_GET(&store->nr_active) > 0) {
2300 0 : const int sleeptime = 100;
2301 0 : MT_lock_unset(&store->flush);
2302 0 : MT_lock_unset(&store->lock);
2303 0 : MT_lock_unset(&store->commit);
2304 0 : MT_sleep_ms(sleeptime);
2305 0 : MT_lock_set(&store->commit);
2306 0 : MT_lock_set(&store->lock);
2307 328 : MT_lock_set(&store->flush);
2308 : }
2309 328 : if (!list_empty(store->changes)) {
2310 32 : ulng oldest = store_timestamp(store)+1;
2311 89 : for(node *n=store->changes->h; n; n = n->next) {
2312 57 : sql_change *c = n->data;
2313 :
2314 57 : if (c->cleanup && !c->cleanup(store, c, oldest)) {
2315 : /* try again with newer oldest, should cleanup any pending issues */
2316 0 : if (!c->cleanup(store, c, oldest+1))
2317 0 : TRC_DEBUG(SQL_STORE, "not deleted\n");
2318 : else
2319 0 : _DELETE(c);
2320 : } else
2321 57 : _DELETE(c);
2322 : }
2323 : }
2324 328 : MT_lock_unset(&store->commit);
2325 328 : list_destroy(store->changes);
2326 328 : os_destroy(store->cat->objects, store);
2327 328 : os_destroy(store->cat->schemas, store);
2328 328 : _DELETE(store->cat);
2329 : } else {
2330 1 : MT_lock_unset(&store->commit);
2331 : }
2332 329 : store->logger_api.destroy(store);
2333 :
2334 329 : list_destroy(store->active);
2335 329 : dep_hash_destroy(store->dependencies);
2336 329 : dep_hash_destroy(store->depchanges);
2337 329 : list_destroy(store->seqchanges);
2338 329 : seq_hash_destroy(store->sequences);
2339 :
2340 329 : TRC_DEBUG(SQL_STORE, "Store unlocked\n");
2341 329 : MT_lock_unset(&store->flush);
2342 329 : MT_lock_unset(&store->lock);
2343 329 : sa_destroy(sa);
2344 329 : MT_lock_destroy(&store->lock);
2345 329 : MT_lock_destroy(&store->commit);
2346 329 : MT_lock_destroy(&store->flush);
2347 21385 : for(int i = 0; i<NR_TABLE_LOCKS; i++)
2348 21056 : MT_lock_destroy(&store->table_locks[i]);
2349 168777 : for(int i = 0; i<NR_COLUMN_LOCKS; i++)
2350 168448 : MT_lock_destroy(&store->column_locks[i]);
2351 329 : _DELETE(store);
2352 329 : }
2353 :
2354 : /* call locked! */
2355 : static int
2356 1430 : store_apply_deltas(sqlstore *store)
2357 : {
2358 1430 : int res = LOG_OK;
2359 :
2360 1430 : store_lock(store);
2361 1430 : ulng oldest = store_oldest_pending(store);
2362 1430 : store_unlock(store);
2363 1430 : TRC_DEBUG(SQL_STORE, "Store apply deltas (" ULLFMT ")\n", oldest-1);
2364 1430 : if (oldest)
2365 1430 : res = store->logger_api.flush(store, oldest-1);
2366 1430 : return res;
2367 : }
2368 :
2369 : void
2370 0 : store_suspend_log(sqlstore *store)
2371 : {
2372 0 : MT_lock_set(&store->lock);
2373 0 : MT_lock_unset(&store->lock);
2374 0 : }
2375 :
2376 : void
2377 0 : store_resume_log(sqlstore *store)
2378 : {
2379 0 : MT_lock_set(&store->flush);
2380 0 : MT_lock_unset(&store->flush);
2381 0 : }
2382 :
2383 : static void
2384 151836 : id_hash_clear_older(sql_hash *h, ulng oldest)
2385 : {
2386 151836 : if (h->entries == 0)
2387 : return;
2388 1440384 : for (int i = 0; i < h->size; i++) {
2389 1396736 : sql_hash_e *e = h->buckets[i], *last = NULL, *first = NULL;
2390 :
2391 1707587 : while (e) {
2392 310851 : sql_hash_e *next = e->chain;
2393 310851 : sql_dependency_change *dc = e->value;
2394 :
2395 310851 : if (dc->ts < oldest) {
2396 41866 : _DELETE(e->value);
2397 41866 : _DELETE(e);
2398 41866 : h->entries--;
2399 : } else {
2400 268985 : if (last)
2401 102961 : last->chain = e;
2402 : else
2403 : first = e;
2404 : last = e;
2405 : }
2406 : e = next;
2407 : }
2408 1396736 : if (last)
2409 166024 : last->chain = NULL;
2410 1396736 : h->buckets[i] = first;
2411 : }
2412 : }
2413 :
2414 : static void
2415 159716 : store_pending_changes(sqlstore *store, ulng oldest, sql_trans *tr)
2416 : {
2417 159716 : ulng oldest_changes = store_get_timestamp(store);
2418 159716 : if (!list_empty(store->changes)) { /* lets first cleanup old stuff */
2419 945516 : for(node *n=store->changes->h; n; ) {
2420 894425 : node *next = n->next;
2421 894425 : sql_change *c = n->data;
2422 :
2423 894425 : assert(c->cleanup);
2424 894425 : if (c->cleanup(store, c, oldest)) {
2425 183336 : list_remove_node(store->changes, store, n);
2426 183336 : _DELETE(c);
2427 711089 : } else if (!c->handled && c->ts < oldest_changes) {
2428 894425 : oldest_changes = c->ts;
2429 : }
2430 : n = next;
2431 : }
2432 : }
2433 159716 : if (ATOMIC_GET(&store->nr_active) < 2) { /* one or no transaction running */
2434 83798 : dep_hash_clear(store->dependencies);
2435 83798 : dep_hash_clear(store->depchanges);
2436 : } else {
2437 75918 : ulng stoldest = store_oldest(store, tr);
2438 75918 : id_hash_clear_older(store->dependencies, stoldest);
2439 75918 : id_hash_clear_older(store->depchanges, stoldest);
2440 : }
2441 159716 : store->oldest_pending = oldest_changes;
2442 159716 : }
2443 :
2444 : #define IDLE_TIME 30 /* in seconds */
2445 :
2446 : void
2447 328 : store_manager(sqlstore *store)
2448 : {
2449 328 : MT_thread_setworking("sleeping");
2450 :
2451 : // In the main loop we always hold the lock except when sleeping or doing cleanups
2452 328 : MT_lock_set(&store->flush);
2453 :
2454 7785 : for (;;) {
2455 7785 : const int idle = ATOMIC_GET(&GDKdebug) & FORCEMITOMASK ? 5000 : IDLE_TIME * 1000000;
2456 : /* if debug bit 1024 is set, attempt immediate log activation
2457 : * and clear the bit */
2458 7785 : if (store->debug&(128|1024) || ATOMIC_GET(&store->lastactive) + idle < (ATOMIC_BASE_TYPE) GDKusec()) {
2459 5346 : store->debug &= ~1024;
2460 5346 : MT_lock_unset(&store->flush);
2461 5346 : store_lock(store);
2462 5346 : if (ATOMIC_GET(&store->nr_active) == 0) {
2463 2681 : ulng oldest = store_timestamp(store)+1;
2464 2681 : store_pending_changes(store, oldest, NULL);
2465 : }
2466 5346 : store_unlock(store);
2467 5346 : MT_lock_set(&store->flush);
2468 5346 : store->logger_api.activate(store); /* rotate to new log file */
2469 5346 : ATOMIC_SET(&store->lastactive, GDKusec());
2470 : }
2471 :
2472 7785 : if (GDKexiting())
2473 : break;
2474 7785 : const int sleeptime = 100;
2475 7785 : MT_lock_unset(&store->flush);
2476 7785 : MT_sleep_ms(sleeptime);
2477 7792 : for (;;) {
2478 7788 : MT_lock_set(&store->commit);
2479 7788 : if (MT_lock_try(&store->flush))
2480 : break;
2481 4 : MT_lock_unset(&store->commit);
2482 4 : MT_sleep_ms(sleeptime);
2483 : }
2484 :
2485 7784 : if (GDKexiting()) {
2486 326 : MT_lock_unset(&store->commit);
2487 326 : break;
2488 : }
2489 :
2490 7458 : if (store->logger_api.changes(store) <= 0) {
2491 6028 : TRC_DEBUG(SQL_STORE, "Store flusher, no changes\n");
2492 6028 : MT_lock_unset(&store->commit);
2493 6028 : continue;
2494 : }
2495 1430 : MT_lock_unset(&store->commit);
2496 :
2497 1430 : MT_thread_setworking("flushing");
2498 1430 : if (store_apply_deltas(store) != LOG_OK) {
2499 0 : MT_lock_unset(&store->flush);
2500 0 : if (!GDKexiting())
2501 0 : GDKfatal("write-ahead logging failure");
2502 : }
2503 :
2504 1430 : if (GDKexiting())
2505 : break;
2506 1429 : MT_thread_setworking("sleeping");
2507 1429 : TRC_DEBUG(SQL_STORE, "Store flusher done\n");
2508 : }
2509 :
2510 : // End of loop, end of lock
2511 327 : MT_lock_unset(&store->flush);
2512 327 : }
2513 :
2514 : void
2515 642698 : store_lock(sqlstore *store)
2516 : {
2517 642698 : MT_lock_set(&store->lock);
2518 : /* tell GDK allocation functions to ignore limits */
2519 643026 : MT_thread_setworking("store locked");
2520 643026 : }
2521 :
2522 : void
2523 643026 : store_unlock(sqlstore *store)
2524 : {
2525 643026 : TRC_DEBUG(SQL_STORE, "Store unlocked\n");
2526 : /* tell GDK allocation functions to honor limits again */
2527 643026 : MT_thread_setworking("store unlocked");
2528 643026 : MT_lock_unset(&store->lock);
2529 643018 : }
2530 :
2531 : int
2532 734619 : store_readonly(sqlstore *store)
2533 : {
2534 734619 : return store->readonly;
2535 : }
2536 :
2537 : // Helper function for tar_write_header.
2538 : // Our stream.h makes sure __attribute__ exists.
2539 : static void __attribute__((__format__(__printf__, 3, 4)))
2540 16720 : tar_write_header_field(char **cursor_ptr, size_t size, const char *fmt, ...)
2541 : {
2542 16720 : va_list ap;
2543 :
2544 16720 : va_start(ap, fmt);
2545 16720 : (void)vsnprintf(*cursor_ptr, size + 1, fmt, ap);
2546 16720 : va_end(ap);
2547 :
2548 : /* At first reading you might wonder why add `size` instead
2549 : * of the byte count returned by vsnprintf. The reason is
2550 : * that we want to move `*cursor_ptr` to the start of the next
2551 : * field, not to the unused part of this field.
2552 : */
2553 16720 : *cursor_ptr += size;
2554 16720 : }
2555 :
2556 : #define TAR_BLOCK_SIZE (512)
2557 :
2558 : // Write a tar header to the given stream.
2559 : static gdk_return __attribute__((__warn_unused_result__))
2560 1045 : tar_write_header(stream *tarfile, const char *path, time_t mtime, int64_t size)
2561 : {
2562 1045 : char buf[TAR_BLOCK_SIZE] = {0};
2563 1045 : char *cursor = buf;
2564 1045 : char *size_field;
2565 1045 : char *chksum_field;
2566 :
2567 : // We set the uid/gid fields to 0 and the uname/gname fields to "".
2568 : // When unpacking as a normal user, they are ignored and the files are
2569 : // owned by that user. When unpacking as root it is reasonable that
2570 : // the resulting files are owned by root.
2571 :
2572 : // The following is taken directly from the definition found
2573 : // in /usr/include/tar.h on a Linux system.
2574 1045 : tar_write_header_field(&cursor, 100, "%s", path); // name[100]
2575 1045 : tar_write_header_field(&cursor, 8, "0000644"); // mode[8]
2576 1045 : tar_write_header_field(&cursor, 8, "%07o", 0U); // uid[8]
2577 1045 : tar_write_header_field(&cursor, 8, "%07o", 0U); // gid[8]
2578 1045 : size_field = cursor;
2579 1045 : cursor += 12; // size[12]
2580 1045 : tar_write_header_field(&cursor, 12, "%011lo", (unsigned long)mtime); // mtime[12]
2581 1045 : chksum_field = cursor; // use this later to set the computed checksum
2582 1045 : tar_write_header_field(&cursor, 8, "%8s", ""); // chksum[8]
2583 1045 : *cursor++ = '0'; // typeflag REGTYPE
2584 1045 : tar_write_header_field(&cursor, 100, "%s", ""); // linkname[100]
2585 1045 : tar_write_header_field(&cursor, 6, "%s", "ustar"); // magic[6]
2586 1045 : tar_write_header_field(&cursor, 2, "%02o", 0U); // version, not null terminated
2587 1045 : tar_write_header_field(&cursor, 32, "%s", ""); // uname[32]
2588 1045 : tar_write_header_field(&cursor, 32, "%s", ""); // gname[32]
2589 1045 : tar_write_header_field(&cursor, 8, "%07o", 0U); // devmajor[8]
2590 1045 : tar_write_header_field(&cursor, 8, "%07o", 0U); // devminor[8]
2591 1045 : tar_write_header_field(&cursor, 155, "%s", ""); // prefix[155]
2592 1045 : assert(cursor - buf == 500);
2593 :
2594 1045 : int64_t max_oct_size = 077777777777; // 0_777_7777_7777, 11 octal digits
2595 : // max_oct_size = 077; // for testing
2596 1045 : if (size <= max_oct_size) {
2597 1045 : tar_write_header_field(&size_field, 12, "%011"PRIo64, size); // size[12]
2598 : } else {
2599 0 : uint8_t *field = (uint8_t *)size_field;
2600 0 : field[0] = 0x80;
2601 0 : for (int i = 11; i >= 4; i--) {
2602 0 : field[i] = size & 0xFF;
2603 0 : size >>= 8;
2604 : }
2605 : }
2606 :
2607 : // checksum
2608 1045 : unsigned sum = 0;
2609 536085 : for (int i = 0; i < TAR_BLOCK_SIZE; i++)
2610 535040 : sum += (unsigned char) buf[i];
2611 :
2612 1045 : tar_write_header_field(&chksum_field, 8, "%06o", sum);
2613 :
2614 1045 : if (mnstr_write(tarfile, buf, TAR_BLOCK_SIZE, 1) != 1) {
2615 0 : GDKerror("error writing tar header %s: %s", path, mnstr_peek_error(tarfile));
2616 0 : return GDK_FAIL;
2617 : }
2618 :
2619 : return GDK_SUCCEED;
2620 : }
2621 :
2622 : /* Write data to the stream, padding it with zeroes up to the next
2623 : * multiple of TAR_BLOCK_SIZE. Make sure all writes are in multiples
2624 : * of TAR_BLOCK_SIZE.
2625 : */
2626 : static gdk_return __attribute__((__warn_unused_result__))
2627 1060 : tar_write(stream *outfile, const char *path, const char *data, size_t size)
2628 : {
2629 1060 : const size_t tail = size % TAR_BLOCK_SIZE;
2630 1060 : const size_t bulk = size - tail;
2631 :
2632 1060 : if (bulk) {
2633 490 : size_t written = mnstr_write(outfile, data, 1, bulk);
2634 490 : if (written != bulk) {
2635 0 : GDKerror("Wrote only %zu bytes of %s instead of first %zu", written, path, bulk);
2636 0 : return GDK_FAIL;
2637 : }
2638 : }
2639 :
2640 1060 : if (tail) {
2641 1045 : char buf[TAR_BLOCK_SIZE] = {0};
2642 1045 : memcpy(buf, data + bulk, tail);
2643 1045 : size_t written = mnstr_write(outfile, buf, 1, TAR_BLOCK_SIZE);
2644 1045 : if (written != TAR_BLOCK_SIZE) {
2645 0 : GDKerror("Wrote only %zu tail bytes of %s instead of %d", written, path, TAR_BLOCK_SIZE);
2646 0 : return GDK_FAIL;
2647 : }
2648 : }
2649 :
2650 : return GDK_SUCCEED;
2651 : }
2652 :
2653 : static gdk_return __attribute__((__warn_unused_result__))
2654 15 : tar_write_data(stream *tarfile, const char *path, time_t mtime, const char *data, size_t size)
2655 : {
2656 15 : gdk_return res;
2657 :
2658 15 : res = tar_write_header(tarfile, path, mtime, size);
2659 15 : if (res != GDK_SUCCEED)
2660 : return res;
2661 :
2662 15 : return tar_write(tarfile, path, data, size);
2663 : }
2664 :
2665 : static gdk_return __attribute__((__warn_unused_result__))
2666 1030 : tar_copy_stream(stream *tarfile, const char *path, time_t mtime, stream *contents, int64_t size, char *buf, size_t bufsize)
2667 : {
2668 1030 : assert( (bufsize % TAR_BLOCK_SIZE) == 0);
2669 1030 : assert(bufsize >= TAR_BLOCK_SIZE);
2670 :
2671 1030 : if (tar_write_header(tarfile, path, mtime, size) != GDK_SUCCEED)
2672 : return GDK_FAIL;
2673 :
2674 : int64_t to_do = size;
2675 2065 : while (to_do > 0) {
2676 1035 : size_t chunk = (to_do <= (int64_t)bufsize) ? (size_t)to_do : bufsize;
2677 1035 : ssize_t nbytes = mnstr_read(contents, buf, 1, chunk);
2678 1035 : if (nbytes > 0) {
2679 1035 : if (tar_write(tarfile, path, buf, nbytes) != GDK_SUCCEED)
2680 : return GDK_FAIL;
2681 1035 : to_do -= (int64_t)nbytes;
2682 1035 : continue;
2683 : }
2684 : // error handling
2685 0 : if (nbytes < 0) {
2686 0 : GDKerror("Error after reading %"PRId64"/%"PRId64" bytes: %s",
2687 : size - to_do, size, mnstr_peek_error(contents));
2688 0 : return GDK_FAIL;
2689 : } else {
2690 0 : GDKerror("Unexpected end of file after reading %"PRId64"/%"PRId64" bytes of %s",
2691 : size - to_do, size, path);
2692 0 : return GDK_FAIL;
2693 : }
2694 : }
2695 :
2696 : return GDK_SUCCEED;
2697 : }
2698 :
2699 : static gdk_return __attribute__((__warn_unused_result__))
2700 5 : hot_snapshot_write_tar(stream *out, const char *prefix, const char *plan)
2701 : {
2702 5 : if (plan == NULL)
2703 : return GDK_FAIL;
2704 :
2705 5 : gdk_return ret = GDK_FAIL;
2706 5 : const char *p = plan; // our cursor in the plan
2707 5 : time_t timestamp = 1234567890; // dummy date, Sat 14 Feb 2009 12:31:30 AM CET
2708 : // Name convention: _path for the absolute path
2709 : // and _name for the corresponding local relative path
2710 5 : char abs_src_path[2 * FILENAME_MAX];
2711 5 : char *src_name = abs_src_path;
2712 5 : char dest_path[100]; // size imposed by tar format.
2713 5 : char *dest_name = dest_path + snprintf(dest_path, sizeof(dest_path), "%s/", prefix);
2714 5 : stream *infile = NULL;
2715 5 : const char *bufsize_env_var = "hot_snapshot_buffer_size";
2716 5 : int bufsize = GDKgetenv_int(bufsize_env_var, 1024 * 1024);
2717 5 : char *buffer = NULL;
2718 :
2719 5 : if (bufsize < TAR_BLOCK_SIZE || (bufsize % TAR_BLOCK_SIZE) != 0) {
2720 0 : GDKerror("invalid value for setting %s=%d: must be a multiple of %d",
2721 : bufsize_env_var, bufsize, TAR_BLOCK_SIZE);
2722 0 : goto end;
2723 : }
2724 5 : buffer = GDKmalloc(bufsize);
2725 5 : if (!buffer) {
2726 0 : GDKerror("could not allocate buffer");
2727 0 : goto end;
2728 : }
2729 :
2730 5 : QryCtx *qry_ctx = MT_thread_get_qry_ctx();
2731 :
2732 5 : int len;
2733 5 : if (sscanf(p, "%[^\n]\n%n", abs_src_path, &len) != 1) {
2734 0 : GDKerror("internal error: first line of plan is malformed");
2735 0 : goto end;
2736 : }
2737 5 : p += len;
2738 5 : src_name = abs_src_path + len - 1; // - 1 because len includes the trailing newline
2739 5 : *src_name++ = DIR_SEP;
2740 :
2741 : // When testing it's sometimes useful to include the plan in the snapshot file
2742 : // strcpy(dest_name, "_snapshot.plan");
2743 : // if (tar_write_data(out, dest_path, timestamp, plan, strlen(plan)) != GDK_SUCCEED)
2744 : // goto end;
2745 :
2746 5 : char command;
2747 5 : int64_t size;
2748 1050 : while (sscanf(p, "%c %"SCNi64" %100s\n%n", &command, &size, src_name, &len) == 3) {
2749 1045 : GDK_CHECK_TIMEOUT_BODY(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(end, qry_ctx));
2750 1045 : p += len;
2751 1045 : strcpy(dest_name, src_name);
2752 1045 : if (size < 0) {
2753 0 : GDKerror("malformed snapshot plan for %s: size %"PRId64" < 0", src_name, size);
2754 0 : goto end;
2755 : }
2756 1045 : switch (command) {
2757 1030 : case 'c':
2758 1030 : infile = open_rstream(abs_src_path);
2759 1030 : if (!infile) {
2760 0 : GDKerror("%s", mnstr_peek_error(NULL));
2761 0 : goto end;
2762 : }
2763 1030 : if (tar_copy_stream(out, dest_path, timestamp, infile, size, buffer, (size_t)bufsize) != GDK_SUCCEED)
2764 0 : goto end;
2765 1030 : close_stream(infile);
2766 1030 : infile = NULL;
2767 1030 : break;
2768 15 : case 'w':
2769 15 : if (tar_write_data(out, dest_path, timestamp, p, (size_t)size) != GDK_SUCCEED)
2770 0 : goto end;
2771 15 : p += (size_t)size;
2772 15 : break;
2773 0 : default:
2774 0 : GDKerror("Unknown command in snapshot plan: %c (%s)", command, src_name);
2775 0 : goto end;
2776 : }
2777 1045 : mnstr_flush(out, MNSTR_FLUSH_ALL);
2778 : }
2779 :
2780 : // write a trailing block of zeros. If it succeeds, this function succeeds.
2781 5 : char *descr = "end-of-archive marker";
2782 5 : char a;
2783 5 : a = '\0';
2784 5 : ret = tar_write(out, descr, &a, 1);
2785 5 : if (ret == GDK_SUCCEED)
2786 5 : ret = tar_write(out, descr, &a, 1);
2787 :
2788 0 : end:
2789 5 : free((char*)plan);
2790 5 : GDKfree(buffer);
2791 5 : if (infile)
2792 0 : close_stream(infile);
2793 : return ret;
2794 : }
2795 :
2796 : /* Pick a name for the temporary tar file. Make sure it has the same extension
2797 : * so as not to confuse the streams library.
2798 : *
2799 : * This function is not entirely safe as compared to for example mkstemp.
2800 : */
2801 : static str __attribute__((__warn_unused_result__))
2802 5 : pick_tmp_name(str filename)
2803 : {
2804 5 : str name = GDKmalloc(strlen(filename) + 10);
2805 5 : if (name == NULL) {
2806 0 : GDKerror("malloc failed");
2807 0 : return NULL;
2808 : }
2809 5 : strcpy(name, filename);
2810 :
2811 : // Look for an extension.
2812 : // Make sure it's part of the basename
2813 :
2814 5 : char *ext = strrchr(name, '.');
2815 5 : char *sep = strrchr(name, DIR_SEP);
2816 5 : char *slash = strrchr(name, '/'); // on Windows, / and \ both work
2817 5 : if (ext != NULL) {
2818 : // is ext actually after sep and slash?
2819 5 : if ((sep != NULL && sep > ext) || (slash != NULL && slash > ext))
2820 : ext = NULL;
2821 : }
2822 :
2823 5 : if (ext == NULL) {
2824 0 : return strcat(name, "..tmp");
2825 : } else {
2826 5 : const char tmp[] = "..tmp.";
2827 5 : size_t tmplen = strlen(tmp);
2828 5 : memmove(ext + tmplen, ext, strlen(ext) + 1);
2829 5 : memmove(ext, tmp, tmplen);
2830 : }
2831 :
2832 5 : return name;
2833 : }
2834 :
2835 : lng
2836 5 : store_hot_snapshot_to_stream(sqlstore *store, stream *tar_stream)
2837 : {
2838 5 : int locked = 0;
2839 5 : lng result = 0;
2840 5 : buffer *plan_buf = NULL;
2841 5 : stream *plan_stream = NULL;
2842 5 : gdk_return r;
2843 :
2844 5 : if (!store->logger_api.get_snapshot_files) {
2845 0 : GDKerror("backend does not support hot snapshots");
2846 0 : goto end;
2847 : }
2848 :
2849 5 : plan_buf = buffer_create(64 * 1024);
2850 5 : if (!plan_buf) {
2851 0 : GDKerror("Failed to allocate plan buffer");
2852 0 : goto end;
2853 : }
2854 5 : plan_stream = buffer_wastream(plan_buf, "write_snapshot_plan");
2855 5 : if (!plan_stream) {
2856 0 : GDKerror("Failed to allocate buffer stream");
2857 0 : goto end;
2858 : }
2859 :
2860 5 : MT_lock_set(&store->flush);
2861 5 : MT_lock_set(&store->lock);
2862 5 : BBPtmlock();
2863 5 : locked = 1;
2864 5 : if (GDKexiting())
2865 0 : goto end;
2866 :
2867 5 : r = store->logger_api.get_snapshot_files(store, plan_stream);
2868 5 : if (r != GDK_SUCCEED)
2869 0 : goto end; // should already have set a GDK error
2870 5 : close_stream(plan_stream);
2871 5 : plan_stream = NULL;
2872 5 : MT_lock_unset(&store->lock);
2873 5 : locked = 2;
2874 5 : r = hot_snapshot_write_tar(tar_stream, GDKgetenv("gdk_dbname"), buffer_get_buf(plan_buf));
2875 5 : if (r != GDK_SUCCEED)
2876 0 : goto end;
2877 :
2878 : // the original idea was to return a sort of sequence number of the
2879 : // database that identifies exactly which version has been snapshotted
2880 : // but no such number is available:
2881 : // logger_functions.read_last_transaction_id is not implemented
2882 : // anywhere.
2883 : //
2884 : // So we return a random positive integer instead.
2885 : result = 42;
2886 :
2887 5 : end:
2888 5 : if (locked) {
2889 5 : BBPtmunlock();
2890 5 : if (locked == 1)
2891 0 : MT_lock_unset(&store->lock);
2892 5 : MT_lock_unset(&store->flush);
2893 : }
2894 5 : if (plan_stream)
2895 0 : close_stream(plan_stream);
2896 5 : if (plan_buf)
2897 5 : buffer_destroy(plan_buf);
2898 5 : return result;
2899 : }
2900 :
2901 :
2902 : lng
2903 5 : store_hot_snapshot(sqlstore *store, str tarfile)
2904 : {
2905 5 : lng result = 0;
2906 5 : struct stat st = {0};
2907 5 : char *tmppath = NULL;
2908 5 : char *dirpath = NULL;
2909 5 : int do_remove = 0;
2910 5 : int dir_fd = -1;
2911 5 : stream *tar_stream = NULL;
2912 5 : buffer *plan_buf = NULL;
2913 5 : stream *plan_stream = NULL;
2914 :
2915 5 : if (!store->logger_api.get_snapshot_files) {
2916 0 : GDKerror("backend does not support hot snapshots");
2917 0 : goto end;
2918 : }
2919 :
2920 5 : if (!MT_path_absolute(tarfile)) {
2921 0 : GDKerror("Hot snapshot requires an absolute path");
2922 0 : goto end;
2923 : }
2924 :
2925 5 : if (MT_stat(tarfile, &st) == 0) {
2926 0 : GDKerror("File already exists: %s", tarfile);
2927 0 : goto end;
2928 : }
2929 :
2930 5 : tmppath = pick_tmp_name(tarfile);
2931 5 : if (tmppath == NULL) {
2932 0 : goto end;
2933 : }
2934 5 : tar_stream = open_wstream(tmppath);
2935 5 : if (!tar_stream) {
2936 0 : GDKerror("%s", mnstr_peek_error(NULL));
2937 0 : goto end;
2938 : }
2939 5 : do_remove = 1;
2940 :
2941 : #ifdef HAVE_FSYNC
2942 : // The following only makes sense on POSIX, where fsync'ing a file
2943 : // guarantees the bytes of the file to go to disk, but not necessarily
2944 : // guarantees the existence of the file in a directory to be persistent.
2945 : // Hence the fsync-the-parent ceremony.
2946 :
2947 : // Set dirpath to the directory containing the tar file.
2948 : // Call realpath(2) to make the path absolute so it has at least
2949 : // one DIR_SEP in it. Realpath requires the file to exist so
2950 : // we feed it tmppath rather than tarfile.
2951 5 : dirpath = GDKmalloc(PATH_MAX);
2952 5 : if (dirpath == NULL) {
2953 0 : GDKsyserror("malloc failed");
2954 0 : goto end;
2955 : }
2956 5 : if (realpath(tmppath, dirpath) == NULL) {
2957 0 : GDKsyserror("couldn't resolve path %s: %s", tarfile, strerror(errno));
2958 0 : goto end;
2959 : }
2960 5 : *strrchr(dirpath, DIR_SEP) = '\0';
2961 :
2962 : // Open the directory so we can call fsync on it.
2963 : // We use raw posix calls because this is not available in the streams library
2964 : // and I'm not quite sure what a generic streams-api should look like.
2965 5 : dir_fd = open(dirpath, O_RDONLY); // ERROR no o_rdonly
2966 5 : if (dir_fd < 0) {
2967 0 : GDKsyserror("couldn't open directory %s", dirpath);
2968 0 : goto end;
2969 : }
2970 :
2971 : // Fsync the directory beforehand too.
2972 : // Postgres believes this is necessary for durability.
2973 5 : if (fsync(dir_fd) < 0) {
2974 0 : GDKsyserror("First fsync on %s failed", dirpath);
2975 0 : goto end;
2976 : }
2977 : #else
2978 : (void)dirpath;
2979 : #endif
2980 :
2981 5 : result = store_hot_snapshot_to_stream(store, tar_stream);
2982 5 : if (result == 0)
2983 0 : goto end;
2984 :
2985 : // Now sync and atomically rename the temp file to the real file,
2986 : // also fsync'ing the directory
2987 5 : mnstr_fsync(tar_stream);
2988 5 : close_stream(tar_stream);
2989 5 : tar_stream = NULL;
2990 5 : if (MT_rename(tmppath, tarfile) < 0) {
2991 0 : GDKsyserror("rename %s to %s failed", tmppath, tarfile);
2992 0 : goto end;
2993 : }
2994 5 : do_remove = 0;
2995 : #ifdef HAVE_FSYNC
2996 : // More POSIX fsync-the-parent-dir ceremony
2997 5 : if (fsync(dir_fd) < 0) {
2998 0 : GDKsyserror("fsync on dir %s failed", dirpath);
2999 0 : goto end;
3000 : }
3001 : #endif
3002 5 : end:
3003 5 : GDKfree(dirpath);
3004 5 : if (dir_fd >= 0)
3005 5 : close(dir_fd);
3006 5 : if (tar_stream)
3007 0 : close_stream(tar_stream);
3008 5 : if (plan_stream)
3009 : close_stream(plan_stream);
3010 5 : if (plan_buf)
3011 : buffer_destroy(plan_buf);
3012 5 : if (do_remove)
3013 0 : (void) MT_remove(tmppath); // Best effort, ignore the result
3014 5 : GDKfree(tmppath);
3015 5 : return result;
3016 : }
3017 :
3018 : static int
3019 54723 : column_dup(sql_trans *tr, sql_column *oc, sql_table *t, sql_column **cres)
3020 : {
3021 54723 : sqlstore *store = tr->store;
3022 54723 : sql_column *c = ZNEW(sql_column);
3023 54723 : int res = LOG_OK;
3024 :
3025 54723 : base_init(NULL, &c->base, oc->base.id, 0, oc->base.name);
3026 54723 : c->type = oc->type;
3027 54723 : c->def = NULL;
3028 54723 : if (oc->def)
3029 519 : c->def =_STRDUP(oc->def);
3030 54723 : c->null = oc->null;
3031 54723 : c->colnr = oc->colnr;
3032 54723 : c->unique = oc->unique;
3033 54723 : c->t = t;
3034 54723 : c->storage_type = NULL;
3035 54723 : if (oc->storage_type)
3036 85 : c->storage_type =_STRDUP(oc->storage_type);
3037 54723 : ATOMIC_PTR_INIT(&c->data, NULL);
3038 :
3039 54723 : if (isTable(c->t)) {
3040 53552 : if (isTempTable(c->t)) {
3041 35617 : if ((res = store->storage_api.create_col(tr, c))) {
3042 : ATOMIC_PTR_DESTROY(&c->data);
3043 : return res;
3044 : }
3045 : } else {
3046 17935 : ATOMIC_PTR_SET(&c->data, store->storage_api.col_dup(oc));
3047 : }
3048 : }
3049 54723 : *cres = c;
3050 54723 : return res;
3051 : }
3052 :
3053 : static sql_kc *
3054 20228 : kc_dup(sql_kc *kc, sql_table *t)
3055 : {
3056 20228 : sql_kc *nkc = ZNEW(sql_kc);
3057 20228 : sql_column *c = find_sql_column(t, kc->c->base.name);
3058 :
3059 20228 : assert(c);
3060 20228 : nkc->c = c;
3061 20228 : c->unique = kc->c->unique;
3062 20228 : return nkc;
3063 : }
3064 :
3065 : static int
3066 8229 : key_dup(sql_trans *tr, sql_key *k, sql_table *t, sql_key **kres)
3067 : {
3068 8229 : sql_key *nk = (k->type != fkey) ? (sql_key *) ZNEW(sql_ukey) : (sql_key *) ZNEW(sql_fkey);
3069 8229 : node *n;
3070 8229 : int res = LOG_OK;
3071 :
3072 8229 : base_init(NULL, &nk->base, k->base.id?k->base.id:next_oid(tr->store), 0, k->base.name);
3073 8229 : nk->type = k->type;
3074 8229 : nk->columns = list_create((fdestroy) &kc_destroy);
3075 8229 : nk->t = t;
3076 8229 : nk->idx = NULL;
3077 :
3078 8229 : if (k->idx) {
3079 8175 : node *n = ol_find_name(t->idxs, nk->base.name);
3080 8175 : nk->idx = (sql_idx *)n->data;
3081 8175 : nk->idx->key = nk;
3082 : }
3083 :
3084 8229 : if (nk->type != fkey) {
3085 6706 : sql_ukey *tk = (sql_ukey *) nk;
3086 :
3087 6706 : if (nk->type == pkey)
3088 4507 : t->pkey = tk;
3089 :
3090 6706 : if (nk->type == ckey)
3091 54 : nk->check = _STRDUP(k->check);
3092 : } else {
3093 1523 : sql_fkey *fk = (sql_fkey *) nk;
3094 1523 : sql_fkey *ok = (sql_fkey *) k;
3095 :
3096 1523 : fk->rkey = ok->rkey;
3097 1523 : fk->on_delete = ok->on_delete;
3098 1523 : fk->on_update = ok->on_update;
3099 : }
3100 :
3101 17405 : for (n = k->columns->h; n; n = n->next) {
3102 9176 : sql_kc *okc = n->data;
3103 :
3104 9176 : list_append(nk->columns, kc_dup(okc, t));
3105 : }
3106 :
3107 8229 : if (isGlobal(t) &&
3108 8162 : ((res = os_add(t->s->keys, tr, nk->base.name, dup_base(&nk->base))) ||
3109 8162 : (res = os_add(tr->cat->objects, tr, nk->base.name, dup_base(&nk->base))))) {
3110 0 : return res;
3111 : }
3112 8229 : *kres = nk;
3113 8229 : return res;
3114 : }
3115 :
3116 : static int
3117 2792 : idx_dup(sql_trans *tr, sql_idx * i, sql_table *t, sql_idx **ires)
3118 : {
3119 2792 : sqlstore *store = tr->store;
3120 2792 : sql_idx *ni = ZNEW(sql_idx);
3121 2792 : node *n;
3122 2792 : int res = LOG_OK;
3123 :
3124 2792 : base_init(NULL, &ni->base, i->base.id, 0, i->base.name);
3125 :
3126 2792 : ni->columns = list_create((fdestroy) &kc_destroy);
3127 2792 : ni->t = t;
3128 2792 : ni->type = i->type;
3129 2792 : ni->key = NULL;
3130 2792 : ATOMIC_PTR_INIT(&ni->data, NULL);
3131 :
3132 2792 : if (isTable(i->t)) {
3133 2739 : if (isTempTable(i->t)) {
3134 86 : if ((res = store->storage_api.create_idx(tr, ni))) {
3135 : ATOMIC_PTR_DESTROY(&ni->data);
3136 : return res;
3137 : }
3138 : } else {
3139 2653 : ATOMIC_PTR_SET(&ni->data, store->storage_api.idx_dup(i));
3140 : }
3141 : }
3142 :
3143 5975 : for (n = i->columns->h; n; n = n->next) {
3144 3183 : sql_kc *okc = n->data;
3145 :
3146 3183 : list_append(ni->columns, kc_dup(okc, t));
3147 : }
3148 2792 : if (isGlobal(t) && (res = os_add(t->s->idxs, tr, ni->base.name, dup_base(&ni->base)))) {
3149 : ATOMIC_PTR_DESTROY(&ni->data);
3150 : return res;
3151 : }
3152 2792 : *ires = ni;
3153 2792 : return res;
3154 : }
3155 :
3156 : static int
3157 504 : part_dup(sql_trans *tr, sql_part *op, sql_table *mt, sql_part **pres)
3158 : {
3159 504 : sql_part *p = ZNEW(sql_part);
3160 504 : int res = LOG_OK;
3161 :
3162 504 : assert(isMergeTable(mt) || isReplicaTable(mt));
3163 504 : base_init(NULL, &p->base, op->base.id, 0, op->base.name);
3164 504 : p->with_nills = op->with_nills;
3165 504 : p->t = mt;
3166 504 : p->member = op->member;
3167 :
3168 504 : if (isRangePartitionTable(mt)) {
3169 262 : p->part.range.minvalue = NEW_ARRAY(char, op->part.range.minlength);
3170 262 : p->part.range.maxvalue = NEW_ARRAY(char, op->part.range.maxlength);
3171 262 : memcpy(p->part.range.minvalue, op->part.range.minvalue, op->part.range.minlength);
3172 262 : memcpy(p->part.range.maxvalue, op->part.range.maxvalue, op->part.range.maxlength);
3173 262 : p->part.range.minlength = op->part.range.minlength;
3174 262 : p->part.range.maxlength = op->part.range.maxlength;
3175 242 : } else if (isListPartitionTable(mt)) {
3176 62 : p->part.values = list_create((fdestroy) &part_value_destroy);
3177 243 : for (node *n = op->part.values->h ; n ; n = n->next) {
3178 181 : sql_part_value *prev = (sql_part_value*) n->data, *nextv = ZNEW(sql_part_value);
3179 181 : nextv->value = NEW_ARRAY(char, prev->length);
3180 181 : memcpy(nextv->value, prev->value, prev->length);
3181 181 : nextv->length = prev->length;
3182 181 : list_append(p->part.values, nextv);
3183 : }
3184 : }
3185 504 : if (isGlobal(mt) && (res = os_add(mt->s->parts, tr, p->base.name, dup_base(&p->base))))
3186 : return res;
3187 504 : *pres = p;
3188 504 : return res;
3189 : }
3190 :
3191 : static int
3192 128 : trigger_dup(sql_trans *tr, sql_trigger *i, sql_table *t, sql_trigger **tres)
3193 : {
3194 128 : sql_trigger *nt = ZNEW(sql_trigger);
3195 128 : int res = LOG_OK;
3196 :
3197 128 : base_init(NULL, &nt->base, i->base.id, 0, i->base.name);
3198 :
3199 128 : nt->columns = list_create((fdestroy) &kc_destroy);
3200 128 : nt->t = t;
3201 128 : nt->time = i->time;
3202 128 : nt->orientation = i->orientation;
3203 128 : nt->event = i->event;
3204 128 : nt->old_name = nt->new_name = nt->condition = NULL;
3205 128 : if (i->old_name)
3206 39 : nt->old_name =_STRDUP(i->old_name);
3207 128 : if (i->new_name)
3208 55 : nt->new_name =_STRDUP(i->new_name);
3209 128 : if (i->condition)
3210 0 : nt->condition =_STRDUP(i->condition);
3211 128 : nt->statement =_STRDUP(i->statement);
3212 :
3213 128 : for (node *n = i->columns->h; n; n = n->next) {
3214 0 : sql_kc *okc = n->data;
3215 :
3216 0 : list_append(nt->columns, kc_dup(okc, t));
3217 : }
3218 128 : if (isGlobal(t) && (res = os_add(t->s->triggers, tr, nt->base.name, dup_base(&nt->base))))
3219 : return res;
3220 128 : *tres = nt;
3221 128 : return res;
3222 : }
3223 :
3224 : static int
3225 6575 : table_dup(sql_trans *tr, sql_table *ot, sql_schema *s, const char *name,
3226 : sql_table **tres, bool dup_global_as_global)
3227 : {
3228 6575 : sqlstore *store = tr->store;
3229 6575 : sql_table *t = ZNEW(sql_table);
3230 6575 : node *n;
3231 6575 : int res = LOG_OK;
3232 :
3233 6575 : if (t == NULL)
3234 : return LOG_ERR;
3235 6575 : base_init(NULL, &t->base, ot->base.id, 0, name?name:ot->base.name);
3236 6575 : t->type = ot->type;
3237 6575 : t->system = ot->system;
3238 6575 : t->bootstrap = ot->bootstrap;
3239 6575 : t->persistence = (s || dup_global_as_global)?ot->persistence:SQL_LOCAL_TEMP;
3240 6575 : t->commit_action = ot->commit_action;
3241 6575 : t->access = ot->access;
3242 6575 : t->query = (ot->query) ?_STRDUP(ot->query) : NULL;
3243 6575 : t->properties = ot->properties;
3244 :
3245 6575 : t->columns = ol_new(NULL, (destroy_fptr) &column_destroy, store);
3246 6575 : t->idxs = ol_new(NULL, (destroy_fptr) &idx_destroy, store);
3247 6575 : t->keys = ol_new(NULL, (destroy_fptr) &key_destroy, store);
3248 6575 : t->triggers = ol_new(NULL, (destroy_fptr) &trigger_destroy, store);
3249 6575 : if (ot->members)
3250 578 : t->members = list_create((fdestroy) &part_destroy);
3251 :
3252 6575 : t->pkey = NULL;
3253 6575 : t->s = s ? s : tr->tmp;
3254 6575 : t->sz = ot->sz;
3255 6575 : ATOMIC_PTR_INIT(&t->data, NULL);
3256 :
3257 6575 : if (isPartitionedByExpressionTable(ot)) {
3258 50 : t->part.pexp = ZNEW(sql_expression);
3259 50 : t->part.pexp->exp =_STRDUP(ot->part.pexp->exp);
3260 50 : t->part.pexp->type = ot->part.pexp->type;
3261 50 : t->part.pexp->cols = list_create((fdestroy) &int_destroy);
3262 107 : for (n = ot->part.pexp->cols->h; n; n = n->next) {
3263 57 : int *nid = MNEW(int);
3264 57 : *nid = *(int *) n->data;
3265 57 : list_append(t->part.pexp->cols, nid);
3266 : }
3267 : }
3268 6575 : if (ot->columns)
3269 61298 : for (n = ol_first_node(ot->columns); n; n = n->next) {
3270 54723 : sql_column *c = NULL;
3271 :
3272 54723 : if ((res = column_dup(tr, n->data, t, &c)) || (res = ol_add(t->columns, &c->base)))
3273 0 : goto cleanup;
3274 54723 : if (isPartitionedByColumnTable(ot) && ot->part.pcol->base.id == c->base.id)
3275 250 : t->part.pcol = c;
3276 : }
3277 6575 : if (ot->idxs)
3278 9367 : for (n = ol_first_node(ot->idxs); n; n = n->next) {
3279 2792 : sql_idx *i = NULL;
3280 :
3281 2792 : if ((res = idx_dup(tr, n->data, t, &i)) || (res = ol_add(t->idxs, &i->base)))
3282 0 : goto cleanup;
3283 : }
3284 6575 : if (ot->keys)
3285 7953 : for (n = ol_first_node(ot->keys); n; n = n->next) {
3286 1378 : sql_key *k = NULL;
3287 :
3288 1378 : if ((res = key_dup(tr, n->data, t, &k)) || (res = ol_add(t->keys, &k->base)))
3289 0 : goto cleanup;
3290 : }
3291 6575 : if (ot->triggers)
3292 6703 : for (n = ol_first_node(ot->triggers); n; n = n->next) {
3293 128 : sql_trigger *tri = NULL;
3294 :
3295 128 : if ((res = trigger_dup(tr, n->data, t, &tri)) || (res = ol_add(t->triggers, &tri->base)))
3296 0 : goto cleanup;
3297 : }
3298 6575 : if (ot->members)
3299 1082 : for (n = ot->members->h; n; n = n->next) {
3300 504 : sql_part *pt = NULL;
3301 :
3302 504 : if ((res = part_dup(tr, n->data, t, &pt)))
3303 0 : goto cleanup;
3304 504 : list_append(t->members, pt);
3305 : }
3306 6575 : if (isTable(t)) {
3307 5989 : if (isTempTable(t)) {
3308 4451 : if ((res = store->storage_api.create_del(tr, t)))
3309 0 : goto cleanup;
3310 : } else {
3311 1538 : ATOMIC_PTR_SET(&t->data, store->storage_api.del_dup(ot));
3312 : }
3313 : }
3314 6575 : if ((res = os_add(isLocalTemp(t) ? tr->localtmps : t->s->tables, tr, t->base.name, &t->base)))
3315 10 : goto cleanup;
3316 :
3317 6565 : cleanup:
3318 10 : if (res) {
3319 : ATOMIC_PTR_DESTROY(&t->data);
3320 : t = NULL;
3321 : }
3322 6575 : *tres = t;
3323 6575 : return res;
3324 : }
3325 :
3326 : sql_table *
3327 4425 : globaltmp_instantiate(sql_trans *tr, sql_table *ot)
3328 : {
3329 4425 : assert(isGlobal(ot)&& isTempTable(ot));
3330 4425 : sql_table *t = NULL;
3331 4425 : if (table_dup(tr, ot, NULL, NULL, &t, false) == LOG_OK)
3332 4425 : return t;
3333 : return NULL;
3334 : }
3335 :
3336 : static int
3337 274559 : new_table(sql_trans *tr, sql_table *t, sql_table **tres)
3338 : {
3339 274559 : int res = LOG_OK;
3340 274559 : if (!isGlobalTemp(t))
3341 274427 : t = find_sql_table(tr, t->s, t->base.name); /* could have changed by depending changes */
3342 :
3343 274559 : if (!isLocalTemp(t) && !isNew(t) && !os_obj_intransaction(t->s->tables, tr, &t->base))
3344 2108 : res = table_dup(tr, t, t->s, NULL, tres, true);
3345 : else
3346 272451 : *tres = t;
3347 274559 : return res;
3348 : }
3349 :
3350 : static sql_arg *
3351 347505 : arg_dup(sql_trans *tr, sql_schema *s, sql_arg *oa)
3352 : {
3353 347505 : sql_arg *a = ZNEW(sql_arg);
3354 :
3355 347505 : if (a) {
3356 347505 : a->name =_STRDUP(oa->name);
3357 347505 : a->inout = oa->inout;
3358 347505 : dup_sql_type(tr, s, &(oa->type), &(a->type));
3359 : }
3360 347505 : return a;
3361 : }
3362 :
3363 : static int
3364 13 : func_dup(sql_trans *tr, sql_func *of, sql_schema *s)
3365 : {
3366 13 : sql_func *f = ZNEW(sql_func);
3367 :
3368 : /* 'func_dup' is aimed at FUNC_LANG_SQL functions ONLY, so f->imp and f->instantiated won't be set */
3369 13 : base_init(NULL, &f->base, of->base.id, 0, of->base.name);
3370 13 : f->mod =_STRDUP(of->mod);
3371 13 : f->type = of->type;
3372 13 : f->lang = of->lang;
3373 13 : f->semantics = of->semantics;
3374 13 : f->side_effect = of->side_effect;
3375 13 : f->varres = of->varres;
3376 13 : f->vararg = of->vararg;
3377 13 : f->fix_scale = of->fix_scale;
3378 13 : f->system = of->system;
3379 13 : f->private = of->private;
3380 13 : f->query = (of->query)?_STRDUP(of->query):NULL;
3381 13 : f->s = s;
3382 13 : f->sa = NULL;
3383 :
3384 13 : f->ops = list_create((fdestroy) &arg_destroy);
3385 30 : for (node *n=of->ops->h; n; n = n->next)
3386 17 : list_append(f->ops, arg_dup(tr, s, n->data));
3387 13 : if (of->res) {
3388 11 : f->res = list_create((fdestroy) &arg_destroy);
3389 22 : for (node *n=of->res->h; n; n = n->next)
3390 11 : list_append(f->res, arg_dup(tr, s, n->data));
3391 : }
3392 :
3393 13 : return os_add(s->funcs, tr, f->base.name, &f->base);
3394 : }
3395 :
3396 : static int
3397 20353 : store_reset_sql_functions(sql_trans *tr, sqlid id)
3398 : {
3399 20353 : sqlstore *store = tr->store;
3400 20353 : int res = LOG_OK, sql_lang = (int) FUNC_LANG_SQL; /* functions other than SQL don't require to be instantiated again */
3401 20353 : sql_schema *syss = find_sql_schema(tr, "sys");
3402 20353 : sql_table *deps = find_sql_table(tr, syss, "dependencies");
3403 20353 : rids *sql_funcs = NULL, *depends = NULL, *joined = NULL;
3404 :
3405 : /* Find dependencies from the object */
3406 20353 : depends = store->table_api.rids_select(tr, find_sql_column(deps, "id"), &id, &id, NULL);
3407 20353 : if (depends == NULL)
3408 : return -1;
3409 20353 : if (store->table_api.rids_empty(depends)) { /* nothing depends on the object, return */
3410 19558 : store->table_api.rids_destroy(depends);
3411 19558 : return res;
3412 : }
3413 : /* Get SQL functions */
3414 795 : sql_table *funcs = find_sql_table(tr, syss, "functions");
3415 795 : sql_column *func_id = find_sql_column(funcs, "id");
3416 795 : if (!(sql_funcs = store->table_api.rids_select(tr, find_sql_column(funcs, "language"), &sql_lang, &sql_lang, NULL))) {
3417 0 : store->table_api.rids_destroy(depends);
3418 0 : return -1;
3419 : }
3420 : /* Do the semijoin */
3421 795 : joined = store->table_api.rids_semijoin(tr, sql_funcs, func_id, depends, find_sql_column(deps, "depend_id"));
3422 795 : store->table_api.rids_destroy(depends);
3423 795 : if (!joined) {
3424 0 : store->table_api.rids_destroy(sql_funcs);
3425 0 : return -1;
3426 : }
3427 :
3428 1152 : for (oid rid = store->table_api.rids_next(joined); !is_oid_nil(rid); rid = store->table_api.rids_next(joined)) {
3429 357 : sqlid fid = store->table_api.column_find_sqlid(tr, func_id, rid);
3430 357 : sql_func *f = sql_trans_find_func(tr, fid); /* could have changed by depending changes */
3431 : /* if it is on the same transaction, then don't dup it again */
3432 357 : if (isNew(f) || os_obj_intransaction(f->s->funcs, tr, &f->base)) {
3433 344 : if (f->instantiated) {
3434 : /* clean backend code */
3435 0 : backend_freecode(sql_shared_module_name, 0, f->imp);
3436 0 : f->instantiated = FALSE;
3437 0 : _DELETE(f->imp);
3438 : }
3439 13 : } else if ((res = func_dup(tr, f, f->s))) {
3440 0 : store->table_api.rids_destroy(joined);
3441 0 : return res;
3442 : }
3443 : }
3444 795 : store->table_api.rids_destroy(joined);
3445 795 : return res;
3446 : }
3447 :
3448 : int
3449 6851 : sql_trans_copy_key( sql_trans *tr, sql_table *t, sql_key *k, sql_key **kres)
3450 : {
3451 6851 : sqlstore *store = tr->store;
3452 6888 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
3453 6851 : sql_table *syskey = find_sql_table(tr, syss, "keys");
3454 6851 : sql_table *syskc = find_sql_table(tr, syss, "objects");
3455 6851 : int neg = -1, action = -1, nr, res = LOG_OK;
3456 6851 : node *n;
3457 6851 : sql_key *nk;
3458 6851 : sql_table *dup = NULL;
3459 :
3460 6851 : if ((res = new_table(tr, t, &dup)))
3461 : return res;
3462 6851 : t = dup;
3463 6851 : if ((res = key_dup(tr, k, t, &nk)))
3464 : return res;
3465 6851 : sql_fkey *fk = (sql_fkey*)nk;
3466 6851 : if ((res = ol_add(t->keys, &nk->base)))
3467 : return res;
3468 :
3469 6851 : if (nk->type == fkey)
3470 925 : action = (fk->on_update<<8) + fk->on_delete;
3471 :
3472 6851 : char *strnil = (char*)ATOMnilptr(TYPE_str);
3473 6851 : if ((res = store->table_api.table_insert(tr, syskey, &nk->base.id, &t->base.id, &nk->type, &nk->base.name, (nk->type == fkey) ? &((sql_fkey *) nk)->rkey : &neg, &action
3474 : , (nk->type == ckey)? &nk->check : &strnil )))
3475 : return res;
3476 :
3477 6851 : if (nk->type == fkey) {
3478 925 : sql_key *rkey = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)k)->rkey);
3479 925 : if (!rkey)
3480 : return LOG_ERR;
3481 :
3482 924 : if ((res = sql_trans_create_dependency(tr, rkey->base.id, nk->base.id, FKEY_DEPENDENCY)))
3483 : return res;
3484 : /* TODO this has to be cleaned out once the sql_cat.c cleanup is done */
3485 924 : if (!isNew(rkey) && (res = sql_trans_add_dependency(tr, rkey->base.id, ddl)))
3486 : return res;
3487 924 : if (!isNew(rkey) && (res = sql_trans_add_dependency(tr, rkey->t->base.id, ddl))) /* this dependency is needed for merge tables */
3488 : return res;
3489 924 : if (!isNew(rkey) && isGlobal(rkey->t) && !isGlobalTemp(rkey->t) && (res = sql_trans_add_dependency(tr, rkey->t->base.id, dml))) /* disallow concurrent updates on other key */
3490 : return res;
3491 924 : if ((res = store_reset_sql_functions(tr, rkey->t->base.id))) /* reset sql functions depending on the table */
3492 : return res;
3493 : }
3494 :
3495 14331 : for (n = nk->columns->h, nr = 0; n; n = n->next, nr++) {
3496 7481 : sql_kc *kc = n->data;
3497 :
3498 7481 : if ((res = store->table_api.table_insert(tr, syskc, &nk->base.id, &kc->c->base.name, &nr, ATOMnilptr(TYPE_int))))
3499 0 : return res;
3500 :
3501 7481 : if (nk->type == fkey) {
3502 956 : if ((res = sql_trans_create_dependency(tr, kc->c->base.id, nk->base.id, FKEY_DEPENDENCY)))
3503 0 : return res;
3504 6525 : } else if (nk->type == ukey || nk->type == ckey) {
3505 2349 : if ((res = sql_trans_create_dependency(tr, kc->c->base.id, nk->base.id, KEY_DEPENDENCY)))
3506 0 : return res;
3507 4176 : } else if (nk->type == pkey) {
3508 4168 : if ((res = sql_trans_create_dependency(tr, kc->c->base.id, nk->base.id, KEY_DEPENDENCY)))
3509 0 : return res;
3510 4168 : if ((res = sql_trans_alter_null(tr, kc->c, 0)))
3511 0 : return res;
3512 : }
3513 :
3514 : /* TODO this has to be cleaned out too */
3515 7481 : if (!isNew(kc->c) && (res = sql_trans_add_dependency(tr, kc->c->base.id, ddl)))
3516 0 : return res;
3517 : }
3518 6850 : if ((res = store_reset_sql_functions(tr, t->base.id))) /* reset sql functions depending on the table */
3519 : return res;
3520 :
3521 : /* TODO this has to be cleaned out too */
3522 6850 : if (!isNew(t) && (res = sql_trans_add_dependency(tr, t->base.id, ddl))) /* this dependency is needed for merge tables */
3523 : return res;
3524 6850 : if (!isNew(t) && isGlobal(t) && !isGlobalTemp(t) && (res = sql_trans_add_dependency(tr, t->base.id, dml))) /* disallow concurrent updates on t */
3525 : return res;
3526 6850 : if (kres)
3527 0 : *kres = nk;
3528 : return res;
3529 : }
3530 :
3531 : int
3532 7148 : sql_trans_copy_idx( sql_trans *tr, sql_table *t, sql_idx *i, sql_idx **ires)
3533 : {
3534 7148 : sqlstore *store = tr->store;
3535 7198 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
3536 7148 : sql_table *sysidx = find_sql_table(tr, syss, "idxs");
3537 7148 : sql_table *sysic = find_sql_table(tr, syss, "objects");
3538 7148 : node *n;
3539 7148 : int nr, res = LOG_OK, ncols = list_length(i->columns);
3540 7148 : sql_table *dup = NULL;
3541 :
3542 7148 : if ((res = new_table(tr, t, &dup)))
3543 : return res;
3544 7145 : t = dup;
3545 7145 : sql_idx *ni = ZNEW(sql_idx);
3546 7145 : base_init(NULL, &ni->base, i->base.id?i->base.id:next_oid(tr->store), true, i->base.name);
3547 7145 : ni->columns = list_create((fdestroy) &kc_destroy);
3548 7145 : ni->t = t;
3549 7145 : ni->type = i->type;
3550 7145 : ni->key = NULL;
3551 7145 : ATOMIC_PTR_INIT(&ni->data, NULL);
3552 :
3553 15014 : for (n = i->columns->h, nr = 0; n; n = n->next, nr++) {
3554 7869 : sql_kc *okc = n->data, *ic;
3555 :
3556 7869 : list_append(ni->columns, ic = kc_dup(okc, t));
3557 7869 : if (i->key && hash_index(i->type))
3558 6496 : ic->c->unique = (ncols == 1) ? 2 : MAX(ic->c->unique, 1);
3559 7869 : if ((res = store->table_api.table_insert(tr, sysic, &ni->base.id, &ic->c->base.name, &nr, ATOMnilptr(TYPE_int)))) {
3560 0 : idx_destroy(store, ni);
3561 0 : return res;
3562 : }
3563 7869 : if ((res = sql_trans_create_dependency(tr, ic->c->base.id, ni->base.id, INDEX_DEPENDENCY))) {
3564 0 : idx_destroy(store, ni);
3565 0 : return res;
3566 : }
3567 : }
3568 7145 : if ((res = ol_add(t->idxs, &ni->base)))
3569 : return res;
3570 :
3571 7145 : if (isGlobal(t) && (res = os_add(t->s->idxs, tr, ni->base.name, dup_base(&ni->base))))
3572 : return res;
3573 7145 : if ((res = store_reset_sql_functions(tr, t->base.id))) /* reset sql functions depending on the table */
3574 : return res;
3575 :
3576 : /* this dependency is needed for merge tables */
3577 7145 : if (!isNew(t) && (res = sql_trans_add_dependency(tr, t->base.id, ddl)))
3578 : return res;
3579 7145 : if (!isNew(t) && isGlobal(t) && !isGlobalTemp(t) && (res = sql_trans_add_dependency(tr, t->base.id, dml)))
3580 : return res;
3581 :
3582 7145 : if (isDeclaredTable(i->t))
3583 7145 : if (!isDeclaredTable(t) && isTable(ni->t) && idx_has_column(ni->type))
3584 6943 : if ((res = store->storage_api.create_idx(tr, ni))) {
3585 : ATOMIC_PTR_DESTROY(&ni->data);
3586 : return res;
3587 : }
3588 7145 : if (!isDeclaredTable(t))
3589 7145 : if ((res = store->table_api.table_insert(tr, sysidx, &ni->base.id, &t->base.id, &ni->type, &ni->base.name))) {
3590 : ATOMIC_PTR_DESTROY(&ni->data);
3591 : return res;
3592 : }
3593 :
3594 7145 : if (ires)
3595 0 : *ires = ni;
3596 : return res;
3597 : }
3598 :
3599 : int
3600 0 : sql_trans_copy_trigger( sql_trans *tr, sql_table *t, sql_trigger *tri, sql_trigger **tres)
3601 : {
3602 0 : sqlstore *store = tr->store;
3603 0 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
3604 0 : sql_table *systr = find_sql_table(tr, syss, "triggers");
3605 0 : sql_table *sysic = find_sql_table(tr, syss, "objects");
3606 0 : node *n;
3607 0 : int nr, res = LOG_OK;
3608 0 : sql_trigger *nt = ZNEW(sql_trigger);
3609 0 : char *strnil = (char*)ATOMnilptr(TYPE_str);
3610 :
3611 0 : base_init(NULL, &nt->base, tri->base.id?tri->base.id:next_oid(tr->store), true, tri->base.name);
3612 0 : nt->columns = list_create((fdestroy) &kc_destroy);
3613 0 : nt->t = t;
3614 0 : nt->time = tri->time;
3615 0 : nt->orientation = tri->orientation;
3616 0 : nt->event = tri->event;
3617 0 : nt->old_name = nt->new_name = nt->condition = NULL;
3618 0 : if (tri->old_name)
3619 0 : nt->old_name =_STRDUP(tri->old_name);
3620 0 : if (tri->new_name)
3621 0 : nt->new_name =_STRDUP(tri->new_name);
3622 0 : if (tri->condition)
3623 0 : nt->condition =_STRDUP(tri->condition);
3624 0 : nt->statement =_STRDUP(tri->statement);
3625 :
3626 0 : for (n = tri->columns->h, nr = 0; n; n = n->next, nr++) {
3627 0 : sql_kc *okc = n->data, *ic;
3628 :
3629 0 : list_append(nt->columns, ic = kc_dup(okc, t));
3630 0 : if ((res = store->table_api.table_insert(tr, sysic, &nt->base.id, &ic->c->base.name, &nr, ATOMnilptr(TYPE_int)))) {
3631 0 : trigger_destroy(store, nt);
3632 0 : return res;
3633 : }
3634 0 : if ((res = sql_trans_create_dependency(tr, ic->c->base.id, nt->base.id, TRIGGER_DEPENDENCY))) {
3635 0 : trigger_destroy(store, nt);
3636 0 : return res;
3637 : }
3638 : }
3639 0 : if ((res = ol_add(t->triggers, &nt->base)))
3640 : return res;
3641 :
3642 0 : assert(isGlobal(t));
3643 0 : if ((res = os_add(t->s->triggers, tr, nt->base.name, dup_base(&nt->base))))
3644 : return res;
3645 0 : if ((res = store_reset_sql_functions(tr, t->base.id))) /* reset sql functions depending on the table */
3646 : return res;
3647 :
3648 0 : if (!isDeclaredTable(t))
3649 0 : if ((res = store->table_api.table_insert(tr, systr, &nt->base.id, &nt->base.name, &t->base.id, &nt->time, &nt->orientation,
3650 0 : &nt->event, (nt->old_name)?&nt->old_name:&strnil, (nt->new_name)?&nt->new_name:&strnil,
3651 0 : (nt->condition)?&nt->condition:&strnil, &nt->statement))) {
3652 : return res;
3653 : }
3654 :
3655 0 : if (tres)
3656 0 : *tres = nt;
3657 : return res;
3658 : }
3659 :
3660 : static int
3661 265025 : type_digits(sql_subtype *type)
3662 : {
3663 265025 : int digits = type->digits;
3664 :
3665 265025 : if (digits && type->type->eclass == EC_NUM) {
3666 107941 : if(type->type->localtype == TYPE_bte) {
3667 : digits = 7;
3668 : } else if(type->type->localtype == TYPE_sht) {
3669 : digits = 15;
3670 : } else if(type->type->localtype == TYPE_int) {
3671 : digits = 31;
3672 : } else if(type->type->localtype == TYPE_lng) {
3673 : digits = 63;
3674 : #ifdef HAVE_HGE
3675 : } else if(type->type->localtype == TYPE_hge) {
3676 265025 : digits = 127;
3677 : #endif
3678 : }
3679 : }
3680 265025 : return digits;
3681 : }
3682 :
3683 : int
3684 255243 : sql_trans_copy_column( sql_trans *tr, sql_table *t, sql_column *c, sql_column **cres)
3685 : {
3686 255243 : sqlstore *store = tr->store;
3687 255360 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
3688 255243 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
3689 255243 : sql_table *dup = NULL;
3690 255243 : int res = LOG_OK;
3691 :
3692 255243 : if ((res = new_table(tr, t, &dup)))
3693 : return res;
3694 255241 : t = dup;
3695 255241 : sql_column *col = ZNEW(sql_column);
3696 255241 : base_init(NULL, &col->base, c->base.id?c->base.id:next_oid(tr->store), true, c->base.name);
3697 255241 : dup_sql_type(tr, t->s, &(c->type), &(col->type));
3698 255241 : col->def = NULL;
3699 255241 : if (c->def)
3700 1386 : col->def =_STRDUP(c->def);
3701 255241 : col->null = c->null;
3702 255241 : col->colnr = c->colnr;
3703 255241 : col->unique = c->unique;
3704 255241 : col->t = t;
3705 255241 : col->storage_type = NULL;
3706 255241 : if (c->storage_type)
3707 0 : col->storage_type =_STRDUP(c->storage_type);
3708 :
3709 255241 : if ((res = ol_add(t->columns, &col->base)))
3710 : return res;
3711 :
3712 : /* this dependency is needed for merge tables */
3713 255241 : if (!isNew(t) && (res = sql_trans_add_dependency(tr, t->base.id, ddl)))
3714 : return res;
3715 255241 : if (!isNew(t) && isGlobal(t) && !isGlobalTemp(t) && (res = sql_trans_add_dependency(tr, t->base.id, dml)))
3716 : return res;
3717 :
3718 255241 : ATOMIC_PTR_INIT(&col->data, NULL);
3719 255241 : if (isDeclaredTable(c->t))
3720 255241 : if (isTable(t))
3721 51741 : if ((res = store->storage_api.create_col(tr, col))) {
3722 : ATOMIC_PTR_DESTROY(&col->data);
3723 : return res;
3724 : }
3725 :
3726 255241 : if (!isDeclaredTable(t)) {
3727 255241 : char *strnil = (char*)ATOMnilptr(TYPE_str);
3728 255241 : int digits = type_digits(&col->type);
3729 255241 : if ((res = store->table_api.table_insert(tr, syscolumn, &col->base.id, &col->base.name, &col->type.type->base.name,
3730 : &digits, &col->type.scale, &t->base.id,
3731 255241 : (col->def) ? &col->def : &strnil, &col->null, &col->colnr,
3732 255241 : (col->storage_type) ? &col->storage_type : &strnil))) {
3733 : ATOMIC_PTR_DESTROY(&col->data);
3734 0 : return res;
3735 : }
3736 255241 : if (c->type.type->s) { /* column depends on type */
3737 188 : if ((res = sql_trans_create_dependency(tr, c->type.type->base.id, col->base.id, TYPE_DEPENDENCY))) {
3738 : ATOMIC_PTR_DESTROY(&col->data);
3739 : return res;
3740 : }
3741 188 : if (!isNew(c->type.type) && (res = sql_trans_add_dependency(tr, c->type.type->base.id, ddl))) {
3742 : ATOMIC_PTR_DESTROY(&col->data);
3743 : return res;
3744 : }
3745 : }
3746 : }
3747 255241 : if (cres)
3748 255136 : *cres = col;
3749 : return res;
3750 : }
3751 :
3752 : static int
3753 157430 : clean_predicates_and_propagate_to_parent(sql_trans *tr)
3754 : {
3755 157430 : int res = LOG_OK;
3756 :
3757 157430 : if (!list_empty(tr->predicates)) {
3758 6028 : if (tr->parent) { /* propagate to the parent */
3759 4 : for(node *n=tr->predicates->h; n && res == LOG_OK ; n = n->next) {
3760 2 : pl *p = (pl*) n->data;
3761 2 : atom *e1 = p->r ? atom_copy(NULL, p->r) : NULL, *e2 = p->f ? atom_copy(NULL, p->f) : NULL;
3762 :
3763 2 : res = sql_trans_add_predicate(tr->parent, p->c, p->cmp, e1, e2, p->anti, p->semantics);
3764 : }
3765 : }
3766 6028 : list_destroy(tr->predicates);
3767 6028 : tr->predicates = NULL;
3768 : }
3769 157428 : if (!list_empty(tr->dependencies)) {
3770 2708 : if (tr->parent) { /* propagate to the parent */
3771 6 : for(node *n=tr->dependencies->h; n && res == LOG_OK ; n = n->next) {
3772 4 : sql_dependency_change *dp = (sql_dependency_change*)n->data;
3773 4 : res = sql_trans_add_dependency(tr->parent, dp->objid, dp->type);
3774 : }
3775 : }
3776 2708 : list_destroy(tr->dependencies);
3777 2708 : tr->dependencies = NULL;
3778 : }
3779 157426 : if (!list_empty(tr->depchanges)) {
3780 56687 : if (tr->parent) { /* propagate to the parent */
3781 6 : for(node *n=tr->depchanges->h; n && res == LOG_OK ; n = n->next) {
3782 3 : sql_dependency_change *dp = (sql_dependency_change*)n->data;
3783 3 : res = sql_trans_add_dependency_change(tr->parent, dp->objid, dp->type);
3784 : }
3785 : }
3786 56687 : list_destroy(tr->depchanges);
3787 56699 : tr->depchanges = NULL;
3788 : }
3789 157426 : return res;
3790 : }
3791 :
3792 : static void
3793 64206 : sql_trans_rollback(sql_trans *tr, bool commit_lock)
3794 : {
3795 64206 : sqlstore *store = tr->store;
3796 64206 : if (!list_empty(tr->changes)) {
3797 2116 : struct os_iter oi;
3798 2116 : os_iterator(&oi, tr->localtmps, tr, NULL);
3799 3288 : for(sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
3800 1172 : sql_table *t = (sql_table *) b;
3801 1172 : if (t->commit_action == CA_DROP && !b->deleted) {
3802 3 : (void) sql_trans_drop_table_id(tr, t->s, b->id, DROP_RESTRICT);// TODO transaction_layer_revamp: fix result code sql_trans_drop_table_id
3803 : }
3804 : }
3805 : /* revert the change list */
3806 2116 : list *nl = tr->changes;
3807 2116 : list_revert(nl);
3808 :
3809 : /* rollback */
3810 2116 : if (!commit_lock)
3811 1137 : MT_lock_set(&store->commit);
3812 2116 : store_lock(store);
3813 2116 : ulng oldest = store_oldest(store, tr);
3814 2116 : ulng commit_ts = store_get_timestamp(store); /* use most recent timestamp such that we can cleanup safely */
3815 13967 : for(node *n=nl->h; n; n = n->next) {
3816 11851 : sql_change *c = n->data;
3817 :
3818 11851 : if (!c)
3819 0 : continue;
3820 11851 : if (c->commit)
3821 11851 : c->commit(tr, c, 0 /* ie rollback */, oldest);
3822 11851 : c->ts = commit_ts;
3823 : }
3824 2116 : store_pending_changes(store, oldest, tr);
3825 13967 : for(node *n=nl->h; n; n = n->next) {
3826 11851 : sql_change *c = n->data;
3827 :
3828 11851 : if (!c)
3829 0 : continue;
3830 11851 : if (!c->cleanup) {
3831 0 : _DELETE(c);
3832 11851 : } else if (c->cleanup && !c->cleanup(store, c, oldest)) {
3833 : /* TODO change to node stealing (no allocs here) */
3834 8595 : store->changes = list_add(store->changes, c);
3835 : } else
3836 11851 : _DELETE(c);
3837 : }
3838 2116 : store_unlock(store);
3839 2116 : if (!commit_lock)
3840 1137 : MT_lock_unset(&store->commit);
3841 2116 : list_destroy(tr->changes);
3842 2116 : tr->changes = NULL;
3843 2116 : tr->logchanges = 0;
3844 : } else {
3845 62094 : if (commit_lock || MT_lock_try(&store->commit)) {
3846 45682 : store_lock(store);
3847 45683 : ulng oldest = store_oldest(store, tr);
3848 45683 : store_pending_changes(store, oldest, tr);
3849 45683 : store_unlock(store);
3850 45683 : if (!commit_lock)
3851 45683 : MT_lock_unset(&store->commit);
3852 : }
3853 : }
3854 :
3855 64213 : if (!list_empty(tr->predicates)) {
3856 1170 : list_destroy(tr->predicates);
3857 1170 : tr->predicates = NULL;
3858 : }
3859 64211 : if (!list_empty(tr->dependencies)) {
3860 179 : list_destroy(tr->dependencies);
3861 179 : tr->dependencies = NULL;
3862 : }
3863 64213 : if (!list_empty(tr->depchanges)) {
3864 1038 : list_destroy(tr->depchanges);
3865 1038 : tr->depchanges = NULL;
3866 : }
3867 64212 : }
3868 :
3869 : sql_trans *
3870 39231 : sql_trans_destroy(sql_trans *tr)
3871 : {
3872 39231 : sql_trans *res = tr->parent;
3873 :
3874 39231 : TRC_DEBUG(SQL_STORE, "Destroy transaction: %p\n", tr);
3875 39231 : _DELETE(tr->name);
3876 39231 : assert(!tr->active || tr->parent);
3877 39231 : if (!list_empty(tr->changes))
3878 19 : sql_trans_rollback(tr, false);
3879 39231 : sqlstore *store = tr->store;
3880 39231 : os_destroy(tr->localtmps, store);
3881 39231 : MT_lock_destroy(&tr->lock);
3882 39231 : if (!list_empty(tr->dropped))
3883 0 : list_destroy(tr->dropped);
3884 39231 : _DELETE(tr);
3885 39231 : return res;
3886 : }
3887 :
3888 : static sql_trans *
3889 39231 : sql_trans_create_(sqlstore *store, sql_trans *parent, const char *name)
3890 : {
3891 39231 : if (name && !parent) /* unlikely */
3892 : return NULL;
3893 :
3894 39231 : sql_trans *tr = ZNEW(sql_trans);
3895 :
3896 39231 : if (!tr)
3897 : return NULL;
3898 39231 : MT_lock_init(&tr->lock, "trans_lock");
3899 39231 : tr->parent = parent;
3900 39231 : if (name) {
3901 62 : _DELETE(parent->name);
3902 62 : parent->name = _STRDUP(name);
3903 : }
3904 :
3905 39231 : if (!parent) {
3906 39169 : tr->localtmps = os_new(NULL, (destroy_fptr) &table_destroy, true, true, false, false, store);
3907 : }
3908 : else {
3909 62 : tr->localtmps = os_dup(parent->localtmps);
3910 : }
3911 :
3912 39231 : store_lock(store);
3913 39231 : tr->store = store;
3914 39231 : tr->tid = store_transaction_id(store);
3915 39231 : tr->cat = store->cat;
3916 39231 : if (!tr->cat) {
3917 329 : store->cat = tr->cat = ZNEW(sql_catalog);
3918 329 : store->cat->schemas = os_new(NULL, (destroy_fptr) &schema_destroy, false, true, true, true, store);
3919 329 : store->cat->objects = os_new(NULL, (destroy_fptr) &key_destroy, false, false, true, false, store);
3920 329 : ATOMIC_INIT(&store->cat->schema_version, 0);
3921 : }
3922 39231 : tr->tmp = store->tmp;
3923 39231 : TRC_DEBUG(SQL_STORE, "New transaction: %p\n", tr);
3924 39231 : store_unlock(store);
3925 39231 : return tr;
3926 : }
3927 :
3928 : static int
3929 40 : schema_dup(sql_trans *tr, sql_schema *s, const char *name, sql_schema **rs)
3930 : {
3931 40 : sql_schema *ns = ZNEW(sql_schema);
3932 40 : int res = LOG_OK;
3933 :
3934 40 : base_init(NULL, &ns->base, s->base.id, 0, name);
3935 40 : ns->auth_id = s->auth_id;
3936 40 : ns->owner = s->owner;
3937 40 : ns->system = s->system;
3938 :
3939 40 : sqlstore *store = tr->store;
3940 40 : assert(!isTempSchema(s)); // TODO transaction_layer_revamp: check if this is really true
3941 40 : ns->tables = os_new(NULL, (destroy_fptr) &table_destroy, false, true, true, false, store);
3942 40 : ns->seqs = os_new(NULL, (destroy_fptr) &seq_destroy, false, true, true, false, store);
3943 40 : ns->keys = os_new(NULL, (destroy_fptr) &key_destroy, false, true, true, false, store);
3944 40 : ns->idxs = os_new(NULL, (destroy_fptr) &idx_destroy, false, true, true, false, store);
3945 40 : ns->triggers = os_new(NULL, (destroy_fptr) &trigger_destroy, false, true, true, false, store);
3946 40 : ns->parts = os_new(NULL, (destroy_fptr) &part_destroy, false, false, true, false, store);
3947 :
3948 : /* table_dup will dup keys, idxs, triggers and parts */
3949 40 : struct os_iter oi;
3950 40 : os_iterator(&oi, s->tables, tr, NULL);
3951 41 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
3952 1 : sql_table *t = NULL;
3953 1 : if ((res = table_dup(tr, (sql_table*)b, ns, NULL, &t, true))) {
3954 0 : schema_destroy(tr->store, ns);
3955 0 : return res;
3956 : }
3957 : }
3958 :
3959 : /* we can share the funcs and types */
3960 40 : ns->funcs = os_dup(s->funcs);
3961 40 : ns->types = os_dup(s->types);
3962 40 : ns->store = s->store;
3963 40 : ns->internal = NULL;
3964 40 : *rs = ns;
3965 40 : return res;
3966 : }
3967 :
3968 : sql_trans *
3969 391 : sql_trans_create(sqlstore *store, sql_trans *parent, const char *name)
3970 : {
3971 391 : sql_trans *tr = sql_trans_create_(store, parent, name);
3972 391 : if (tr) {
3973 391 : tr->ts = store_timestamp(store);
3974 391 : tr->active = 1;
3975 : }
3976 391 : return tr;
3977 : }
3978 :
3979 : static int
3980 6905 : sql_trans_valid(sql_trans *tr)
3981 : {
3982 6905 : int ok = LOG_OK;
3983 6905 : sqlstore *store = tr->store;
3984 :
3985 6905 : if (!list_empty(tr->predicates)) {
3986 : /* for each predicate check if that table/column has changes */
3987 27135 : for(node *n = tr->predicates->h; n; n = n->next) {
3988 21165 : pl *p = n->data;
3989 21165 : sql_column *c = p->c;
3990 :
3991 21165 : if (c->t && isTable(c->t) && !isNew(c) && !isTempTable(c->t)) {
3992 21163 : if ((ok = store->storage_api.tab_validate(tr, c->t, 0)))
3993 : break;
3994 : }
3995 : }
3996 : }
3997 6905 : return ok;
3998 : }
3999 :
4000 : static inline int
4001 68637 : transaction_add_hash_entry(sql_hash *h, sqlid id, sql_dependency_change_type tpe, ulng ts)
4002 : {
4003 68637 : sql_dependency_change *next_change = MNEW(sql_dependency_change);
4004 :
4005 68637 : if (!next_change)
4006 : return LOG_ERR;
4007 68637 : *next_change = (sql_dependency_change) {
4008 : .objid = id,
4009 : .type = tpe,
4010 : .ts = ts
4011 : };
4012 :
4013 68637 : int key = h->key(next_change);
4014 68637 : sql_hash_e *he = h->buckets[key&(h->size-1)];
4015 :
4016 177281 : for (; he ; he = he->chain) { /* find if the entry is already present */
4017 134347 : sql_dependency_change *schange = (sql_dependency_change*) he->value;
4018 :
4019 134347 : if (schange->objid == id && schange->ts == ts && schange->type == tpe) {
4020 25703 : _DELETE(next_change);
4021 25703 : return LOG_OK;
4022 : }
4023 : }
4024 :
4025 42934 : if (!hash_add(h, key, next_change)) {
4026 0 : _DELETE(next_change);
4027 0 : return LOG_ERR;
4028 : }
4029 : return LOG_OK;
4030 : }
4031 :
4032 : static int
4033 58680 : transaction_check_dependencies_and_removals(sql_trans *tr)
4034 : {
4035 58680 : int ok = LOG_OK;
4036 58680 : sqlstore *store = tr->store;
4037 :
4038 : /* test dependencies and removals crossed for conflicts */
4039 58680 : if (!list_empty(tr->dependencies) && !hash_empty(store->depchanges)) {
4040 47 : for (node *n = tr->dependencies->h; n && ok == LOG_OK; n = n->next) {
4041 28 : sql_dependency_change *lchange = (sql_dependency_change*) n->data;
4042 28 : int key = store->depchanges->key(lchange);
4043 28 : sql_hash_e *he = store->depchanges->buckets[key&(store->depchanges->size-1)];
4044 :
4045 49 : for (; he && ok == LOG_OK; he = he->chain) {
4046 21 : sql_dependency_change *schange = (sql_dependency_change*) he->value;
4047 :
4048 : /* conflict only if transactions overlap */
4049 21 : if (schange->ts >= tr->ts && lchange->objid == schange->objid && lchange->type == schange->type)
4050 21 : ok = LOG_CONFLICT;
4051 : }
4052 : }
4053 : }
4054 58680 : if (ok == LOG_OK && !list_empty(tr->depchanges) && !hash_empty(store->dependencies)) {
4055 85 : for (node *n = tr->depchanges->h; n && ok == LOG_OK; n = n->next) {
4056 49 : sql_dependency_change *lchange = (sql_dependency_change*) n->data;
4057 49 : int key = store->dependencies->key(lchange);
4058 49 : sql_hash_e *he = store->dependencies->buckets[key&(store->dependencies->size-1)];
4059 :
4060 88 : for (; he && ok == LOG_OK; he = he->chain) {
4061 39 : sql_dependency_change *schange = (sql_dependency_change*) he->value;
4062 :
4063 : /* conflict only if transactions overlap */
4064 39 : if (schange->ts >= tr->ts && lchange->objid == schange->objid && lchange->type == schange->type)
4065 39 : ok = LOG_CONFLICT;
4066 : }
4067 : }
4068 : }
4069 58680 : return ok;
4070 : }
4071 :
4072 : int
4073 158322 : sql_trans_commit(sql_trans *tr)
4074 : {
4075 158322 : int ok = LOG_OK;
4076 158322 : sqlstore *store = tr->store;
4077 :
4078 158322 : if (!list_empty(tr->changes)) {
4079 66322 : struct os_iter oi;
4080 66322 : os_iterator(&oi, tr->localtmps, tr, NULL);
4081 88092 : for(sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
4082 21734 : sql_table *t = (sql_table *) b;
4083 21734 : if (t->commit_action == CA_DROP && !b->deleted) {
4084 21734 : (void) sql_trans_drop_table_id(tr, t->s, b->id, DROP_RESTRICT);;// TODO transaction_layer_revamp: fix result code sql_trans_drop_table_id
4085 : }
4086 : }
4087 :
4088 66297 : bool flush = false;
4089 66297 : ulng commit_ts = 0, oldest = 0, log_file_id = 0;
4090 :
4091 66297 : MT_lock_set(&store->commit);
4092 :
4093 66350 : if (!tr->parent && !list_empty(tr->predicates)) {
4094 6905 : ok = sql_trans_valid(tr);
4095 6905 : if (ok != LOG_OK) {
4096 935 : sql_trans_rollback(tr, true);
4097 935 : MT_lock_unset(&store->commit);
4098 1914 : return ok == LOG_CONFLICT ? SQL_CONFLICT : SQL_ERR;
4099 : }
4100 : }
4101 :
4102 130813 : if (!tr->parent &&
4103 128068 : (!list_empty(tr->dependencies) || !list_empty(tr->depchanges))) {
4104 58680 : ok = transaction_check_dependencies_and_removals(tr);
4105 58680 : if (ok != LOG_OK) {
4106 44 : sql_trans_rollback(tr, true);
4107 44 : MT_lock_unset(&store->commit);
4108 44 : return ok == LOG_CONFLICT ? SQL_CONFLICT : SQL_ERR;
4109 : }
4110 : }
4111 :
4112 : /* log changes should only be done if there is something to log */
4113 65371 : const bool log = !tr->parent && tr->logchanges > 0;
4114 :
4115 62478 : if (log) {
4116 62478 : const lng min_changes = ATOMIC_GET(&GDKdebug) & FORCEMITOMASK ? 5 : 1000000;
4117 62478 : flush = (tr->logchanges > min_changes && list_empty(store->changes));
4118 : }
4119 :
4120 5399 : if (flush)
4121 5399 : MT_lock_set(&store->flush);
4122 65371 : if (log) {
4123 62478 : ok = store->logger_api.log_tstart(store, flush, &log_file_id); /* wal start */
4124 : /* log */
4125 520123 : for(node *n=tr->changes->h; n && ok == LOG_OK; n = n->next) {
4126 457645 : sql_change *c = n->data;
4127 :
4128 457645 : if (c->log && ok == LOG_OK)
4129 253281 : ok = c->log(tr, c);
4130 : }
4131 62478 : if (ok == LOG_OK && !list_empty(store->seqchanges)) {
4132 629 : sequences_lock(store);
4133 1393 : for(node *n = store->seqchanges->h; n; ) {
4134 764 : node *next = n->next;
4135 764 : log_store_sequence(store, n->data);
4136 764 : list_remove_node(store->seqchanges, NULL, n);
4137 764 : n = next;
4138 : }
4139 629 : sequences_unlock(store);
4140 : }
4141 62478 : if (ok == LOG_OK && store->prev_oid != store->obj_id) {
4142 6089 : if (!flush)
4143 2085 : MT_lock_set(&store->flush);
4144 6089 : ok = store->logger_api.log_tsequence(store, OBJ_SID, store->obj_id);
4145 6089 : if (!flush)
4146 2085 : MT_lock_unset(&store->flush);
4147 : }
4148 62478 : store->prev_oid = store->obj_id;
4149 :
4150 :
4151 62478 : if (ok == LOG_OK)
4152 62478 : ok = store->logger_api.log_tend(store); /* wal end */
4153 : }
4154 65371 : store_lock(store);
4155 :
4156 65371 : if (tr->parent) {
4157 17 : commit_ts = oldest = tr->parent->tid;
4158 17 : tr->parent->logchanges += tr->logchanges;
4159 : } else {
4160 65354 : commit_ts = store_timestamp(store);
4161 65354 : oldest = store_oldest(store, tr);
4162 : }
4163 65371 : tr->logchanges = 0;
4164 65371 : TRC_DEBUG(SQL_STORE, "Forwarding changes (" ULLFMT ", " ULLFMT ") -> " ULLFMT "\n", tr->tid, tr->ts, commit_ts);
4165 : /* apply committed changes */
4166 65371 : if (ATOMIC_GET(&store->nr_active) == 1 && !tr->parent)
4167 65371 : oldest = commit_ts;
4168 65371 : store_pending_changes(store, oldest, tr);
4169 606744 : for(node *n=tr->changes->h; n && ok == LOG_OK; n = n->next) {
4170 541373 : sql_change *c = n->data;
4171 :
4172 541373 : if (c->commit && ok == LOG_OK)
4173 541373 : ok = c->commit(tr, c, commit_ts, oldest);
4174 : else
4175 0 : c->obj->new = 0;
4176 541373 : c->ts = commit_ts;
4177 : }
4178 : /* propagate transaction dependencies to the storage only if other transactions are running */
4179 65371 : if (ok == LOG_OK && !tr->parent && ATOMIC_GET(&store->nr_active) > 1) {
4180 42818 : if (!list_empty(tr->dependencies)) {
4181 244 : for (node *n = tr->dependencies->h; n && ok == LOG_OK; n = n->next) {
4182 185 : sql_dependency_change *lchange = (sql_dependency_change*) n->data;
4183 185 : ok = transaction_add_hash_entry(store->dependencies, lchange->objid, lchange->type, commit_ts);
4184 : }
4185 : }
4186 42818 : if (!list_empty(tr->depchanges)) {
4187 111211 : for (node *n = tr->depchanges->h; n && ok == LOG_OK; n = n->next) {
4188 68452 : sql_dependency_change *lchange = (sql_dependency_change*) n->data;
4189 68452 : ok = transaction_add_hash_entry(store->depchanges, lchange->objid, lchange->type, commit_ts);
4190 : }
4191 : }
4192 : }
4193 : /* garbage collect */
4194 606744 : for(node *n=tr->changes->h; n && ok == LOG_OK; ) {
4195 541373 : node *next = n->next;
4196 541373 : sql_change *c = n->data;
4197 :
4198 541373 : n->data = NULL;
4199 541373 : if (c) {
4200 541373 : if (!c->cleanup || c->cleanup(store, c, oldest)) {
4201 366538 : _DELETE(c);
4202 174835 : } else if (tr->parent) { /* need to keep everything */
4203 37 : tr->parent->changes = list_add(tr->parent->changes, c);
4204 : } else {
4205 174798 : store->changes = list_add(store->changes, c);
4206 : }
4207 : }
4208 : n = next;
4209 : }
4210 65371 : tr->active = 2; /* small hack enabling to signal that this transaction has committed */
4211 65371 : tr->ts = commit_ts; /* fix profiler output */
4212 65371 : store_unlock(store);
4213 : /* flush the log structure */
4214 65371 : if (log) {
4215 62478 : if (!flush)
4216 57079 : MT_lock_unset(&store->commit); /* release the commit log when flushing to disk */
4217 62478 : if (ok == LOG_OK)
4218 62478 : ok = store->logger_api.log_tflush(store, log_file_id, commit_ts); /* flush/sync */
4219 62478 : if (!flush)
4220 57079 : MT_lock_set(&store->commit); /* release the commit log when flushing to disk */
4221 57079 : if (flush)
4222 5399 : MT_lock_unset(&store->flush);
4223 : }
4224 65371 : MT_lock_unset(&store->commit);
4225 65360 : if (ok == LOG_OK) {
4226 65360 : list_destroy(tr->changes);
4227 65368 : tr->changes = NULL;
4228 : }
4229 92066 : } else if (ATOMIC_GET(&store->nr_active) == 1) { /* just me cleanup */
4230 43865 : MT_lock_set(&store->commit);
4231 43865 : store_lock(store);
4232 43865 : ulng oldest = store_timestamp(store);
4233 43865 : store_pending_changes(store, oldest, tr);
4234 43865 : store_unlock(store);
4235 43865 : MT_lock_unset(&store->commit);
4236 : }
4237 :
4238 109233 : if (ok == LOG_OK)
4239 157434 : ok = clean_predicates_and_propagate_to_parent(tr);
4240 :
4241 157414 : return (ok==LOG_OK)?SQL_OK:SQL_ERR;
4242 : }
4243 :
4244 : static sql_table *
4245 5 : find_table_by_columnid(sql_trans *tr, const char *schema, sqlid id)
4246 : {
4247 5 : sqlstore *store = tr->store;
4248 5 : sql_table *syscolumn = find_sql_table(tr, find_sql_schema(tr, schema), "_columns");
4249 :
4250 5 : oid rid = store->table_api.column_find_row(tr, find_sql_column(syscolumn, "id"), &id, NULL);
4251 5 : if (!is_oid_nil(rid)) {
4252 5 : sqlid tid = store->table_api.column_find_sqlid(tr, find_sql_column(syscolumn, "table_id"), rid);
4253 5 : return sql_trans_find_table(tr, tid);
4254 : }
4255 : return NULL;
4256 : }
4257 :
4258 : static int
4259 5150 : sql_trans_drop_all_dependencies(sql_trans *tr, sqlid id, sql_dependency type)
4260 : {
4261 5150 : sqlid dep_id=0, t_id = -1;
4262 5150 : sht dep_type = 0;
4263 5150 : list *dep = sql_trans_get_dependents(tr, id, type, NULL);
4264 5150 : node *n;
4265 5150 : int res = LOG_OK;
4266 :
4267 5150 : if (!dep)
4268 : return -1;
4269 :
4270 5150 : n = dep->h;
4271 :
4272 5523 : while (n && !res) {
4273 373 : dep_id = *(sqlid*) n->data;
4274 373 : dep_type = (sql_dependency) *(sht*) n->next->data;
4275 :
4276 373 : if (!list_find_id(tr->dropped, dep_id)) {
4277 :
4278 369 : switch (dep_type) {
4279 0 : case SCHEMA_DEPENDENCY:
4280 0 : res = sql_trans_drop_schema(tr, dep_id, DROP_CASCADE);
4281 0 : break;
4282 49 : case TABLE_DEPENDENCY:
4283 : case VIEW_DEPENDENCY: {
4284 49 : sql_table *t = sql_trans_find_table(tr, dep_id);
4285 49 : if (t)
4286 49 : res = sql_trans_drop_table_id(tr, t->s, dep_id, DROP_CASCADE);
4287 : } break;
4288 0 : case COLUMN_DEPENDENCY: {
4289 0 : if ((t_id = sql_trans_get_dependency_type(tr, dep_id, TABLE_DEPENDENCY)) > 0) {
4290 0 : sql_table *t = sql_trans_find_table(tr, dep_id);
4291 0 : if (t && ol_length(t->columns) == 1) /* only column left, drop the table instead */
4292 0 : res = sql_trans_drop_table_id(tr, t->s, t->base.id, DROP_CASCADE);
4293 0 : else if (t)
4294 0 : res = sql_trans_drop_column(tr, t, dep_id, DROP_CASCADE);
4295 : }
4296 : } break;
4297 2 : case TRIGGER_DEPENDENCY: {
4298 2 : sql_trigger *t = sql_trans_find_trigger(tr, dep_id);
4299 2 : if (t && !list_find_id(tr->dropped, t->t->base.id)) /* table not yet dropped */
4300 2 : res = sql_trans_drop_trigger(tr, t->t->s, dep_id, DROP_CASCADE);
4301 : } break;
4302 278 : case KEY_DEPENDENCY:
4303 : case FKEY_DEPENDENCY: {
4304 278 : sql_key *k = sql_trans_find_key(tr, dep_id);
4305 278 : if (k && !list_find_id(tr->dropped, k->t->base.id)) /* table not yet dropped */
4306 277 : res = sql_trans_drop_key(tr, k->t->s, dep_id, DROP_CASCADE);
4307 : } break;
4308 8 : case INDEX_DEPENDENCY: {
4309 8 : sql_idx *i = sql_trans_find_idx(tr, dep_id);
4310 8 : if (i && !list_find_id(tr->dropped, i->t->base.id)) /* table not yet dropped */
4311 8 : res = sql_trans_drop_idx(tr, i->t->s, dep_id, DROP_CASCADE);
4312 : } break;
4313 18 : case PROC_DEPENDENCY:
4314 : case FUNC_DEPENDENCY: {
4315 18 : sql_func *f = sql_trans_find_func(tr, dep_id);
4316 18 : if (f)
4317 18 : res = sql_trans_drop_func(tr, f->s, dep_id, DROP_CASCADE);
4318 : } break;
4319 14 : case TYPE_DEPENDENCY: {
4320 : /* Unlike other dependencies, for type dependencies,
4321 : the dependent object depends on the type, rather the other way around.
4322 : At this moment, only functions or columns depend on types, so try both */
4323 14 : sql_table *t = NULL;
4324 14 : sql_func *f = sql_trans_find_func(tr, dep_id);
4325 14 : if (f) {
4326 9 : res = sql_trans_drop_func(tr, f->s, dep_id, DROP_CASCADE);
4327 5 : } else if ((t = find_table_by_columnid(tr, "sys", dep_id)) ||
4328 0 : (t = find_table_by_columnid(tr, "tmp", dep_id))) {
4329 5 : if (ol_length(t->columns) == 1) /* only column left, drop the table instead */
4330 2 : res = sql_trans_drop_table_id(tr, t->s, t->base.id, DROP_CASCADE);
4331 : else
4332 3 : res = sql_trans_drop_column(tr, t, dep_id, DROP_CASCADE);
4333 : }
4334 : } break;
4335 : case USER_DEPENDENCY: /*TODO schema and users dependencies*/
4336 : break;
4337 : }
4338 : }
4339 :
4340 373 : n = n->next->next;
4341 : }
4342 5150 : list_destroy(dep);
4343 5150 : return res;
4344 : }
4345 :
4346 : static int
4347 2645 : sys_drop_ic(sql_trans *tr, sql_idx * i, sql_kc *kc)
4348 : {
4349 2645 : sqlstore *store = tr->store;
4350 2700 : sql_schema *syss = find_sql_schema(tr, isGlobal(i->t)?"sys":"tmp");
4351 2645 : sql_table *sysic = find_sql_table(tr, syss, "objects");
4352 2645 : oid rid = store->table_api.column_find_row(tr, find_sql_column(sysic, "id"), &i->base.id, find_sql_column(sysic, "name"), kc->c->base.name, NULL);
4353 2645 : int res = LOG_OK;
4354 :
4355 2645 : if (is_oid_nil(rid))
4356 : return -1;
4357 2645 : if ((res = store->table_api.table_delete(tr, sysic, rid)))
4358 : return res;
4359 : return res;
4360 : }
4361 :
4362 : static int
4363 36339 : sql_trans_drop_any_comment(sql_trans *tr, sqlid id)
4364 : {
4365 36339 : sqlstore *store = tr->store;
4366 36339 : sql_table *comments;
4367 36339 : sql_column *id_col;
4368 36339 : oid row;
4369 :
4370 36339 : comments = find_sql_table(tr, find_sql_schema(tr, "sys"), "comments");
4371 36339 : if (!comments) /* for example during upgrades */
4372 : return 0;
4373 :
4374 36339 : id_col = find_sql_column(comments, "id");
4375 36339 : assert(id_col);
4376 :
4377 36339 : row = store->table_api.column_find_row(tr, id_col, &id, NULL);
4378 36339 : if (!is_oid_nil(row) && store->table_api.table_delete(tr, comments, row))
4379 : return -2;
4380 : return 0;
4381 : }
4382 :
4383 : static int
4384 2163 : sys_drop_idx(sql_trans *tr, sql_idx * i, int drop_action)
4385 : {
4386 2163 : sqlstore *store = tr->store;
4387 2163 : node *n;
4388 2207 : sql_schema *syss = find_sql_schema(tr, isGlobal(i->t)?"sys":"tmp");
4389 2163 : sql_table *sysidx = find_sql_table(tr, syss, "idxs");
4390 2163 : oid rid = store->table_api.column_find_row(tr, find_sql_column(sysidx, "id"), &i->base.id, NULL);
4391 2163 : int res = LOG_OK;
4392 :
4393 2163 : if (is_oid_nil(rid))
4394 : return -1;
4395 2163 : if ((res = store->table_api.table_delete(tr, sysidx, rid)))
4396 : return res;
4397 2163 : if ((res = sql_trans_drop_any_comment(tr, i->base.id)))
4398 : return res;
4399 4808 : for (n = i->columns->h; n; n = n->next) {
4400 2645 : sql_kc *ic = n->data;
4401 :
4402 2645 : if (i->key && hash_index(i->type)) { /* update new column's unique value */
4403 1525 : int unique = 0;
4404 1525 : sqlid cid = ic->c->base.id;
4405 1525 : struct os_iter oi;
4406 :
4407 1525 : os_iterator(&oi, i->t->s->idxs, tr, NULL);
4408 44097 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
4409 42572 : sql_idx *ti = (sql_idx*)b;
4410 :
4411 42572 : if (ti->base.id != i->base.id && ti->key && hash_index(ti->type)) {
4412 35082 : bool found = false;
4413 76303 : for (node *m = ti->columns->h; m && !found; m = m->next) {
4414 41221 : sql_kc *tic = m->data;
4415 :
4416 41221 : found |= tic->c->base.id == cid;
4417 : }
4418 35082 : if (found)
4419 21 : unique = MAX(unique, list_length(ti->columns) == 1 ? 2 : 1);
4420 : }
4421 : }
4422 1525 : ic->c->unique = unique;
4423 : }
4424 2645 : if ((res = sys_drop_ic(tr, i, ic)))
4425 0 : return res;
4426 : }
4427 :
4428 2163 : i->base.deleted = 1;
4429 2163 : if (!isNew(i) && !isTempTable(i->t))
4430 1961 : if ((res = store->storage_api.drop_idx(tr, (sql_idx*)dup_base(&i->base))))
4431 : return res;
4432 :
4433 : /* remove idx from schema and table */
4434 2163 : if (isGlobal(i->t) && (res = os_del(i->t->s->idxs, tr, i->base.name, dup_base(&i->base))))
4435 : return res;
4436 2163 : if (!isNew(i) && (res = sql_trans_add_dependency_change(tr, i->base.id, ddl)))
4437 : return res;
4438 2163 : if ((res = sql_trans_drop_dependencies(tr, i->base.id)))
4439 : return res;
4440 :
4441 2163 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, i->base.id, INDEX_DEPENDENCY)))
4442 : return res;
4443 : return res;
4444 : }
4445 :
4446 : static int
4447 2326 : sys_drop_kc(sql_trans *tr, sql_key *k, sql_kc *kc)
4448 : {
4449 2326 : sqlstore *store = tr->store;
4450 2357 : sql_schema *syss = find_sql_schema(tr, isGlobal(k->t)?"sys":"tmp");
4451 2326 : sql_table *syskc = find_sql_table(tr, syss, "objects");
4452 2326 : oid rid = store->table_api.column_find_row(tr, find_sql_column(syskc, "id"), &k->base.id, find_sql_column(syskc, "name"), kc->c->base.name, NULL);
4453 2326 : int res = LOG_OK;
4454 :
4455 2326 : if (is_oid_nil(rid))
4456 : return -1;
4457 2326 : if ((res = store->table_api.table_delete(tr, syskc, rid)))
4458 : return res;
4459 : return res;
4460 : }
4461 :
4462 : static int
4463 1912 : sys_drop_key(sql_trans *tr, sql_key *k, int drop_action)
4464 : {
4465 1912 : sqlstore *store = tr->store;
4466 1912 : node *n;
4467 1943 : sql_schema *syss = find_sql_schema(tr, isGlobal(k->t)?"sys":"tmp");
4468 1912 : sql_table *syskey = find_sql_table(tr, syss, "keys");
4469 1912 : oid rid = store->table_api.column_find_row(tr, find_sql_column(syskey, "id"), &k->base.id, NULL);
4470 1912 : int res = LOG_OK;
4471 :
4472 1912 : if (is_oid_nil(rid))
4473 : return -1;
4474 1912 : if ((res = store->table_api.table_delete(tr, syskey, rid)))
4475 : return res;
4476 :
4477 4238 : for (n = k->columns->h; n; n = n->next) {
4478 2326 : sql_kc *kc = n->data;
4479 2326 : if ((res = sys_drop_kc(tr, k, kc)))
4480 0 : return res;
4481 : }
4482 : /* remove key from schema */
4483 1912 : if (isGlobal(k->t) && (res = os_del(k->t->s->keys, tr, k->base.name, dup_base(&k->base))))
4484 : return res;
4485 1912 : if (isGlobal(k->t) && (res = os_del(tr->cat->objects, tr, k->base.name, dup_base(&k->base))))
4486 : return res;
4487 1912 : if (k->t->pkey == (sql_ukey*)k)
4488 941 : k->t->pkey = NULL;
4489 :
4490 1912 : if (!isNew(k) && (res = sql_trans_add_dependency_change(tr, k->base.id, ddl)))
4491 : return res;
4492 1912 : if ((res = sql_trans_drop_dependencies(tr, k->base.id)))
4493 : return res;
4494 :
4495 2146 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, k->base.id, (k->type == fkey) ? FKEY_DEPENDENCY : KEY_DEPENDENCY)))
4496 : return res;
4497 : return res;
4498 : }
4499 :
4500 : static int
4501 0 : sys_drop_tc(sql_trans *tr, sql_trigger * i, sql_kc *kc)
4502 : {
4503 0 : sqlstore *store = tr->store;
4504 0 : sql_schema *syss = find_sql_schema(tr, isGlobal(i->t)?"sys":"tmp");
4505 0 : sql_table *systc = find_sql_table(tr, syss, "objects");
4506 0 : oid rid = store->table_api.column_find_row(tr, find_sql_column(systc, "id"), &i->base.id, find_sql_column(systc, "name"), kc->c->base.name, NULL);
4507 0 : int res = LOG_OK;
4508 :
4509 0 : if (is_oid_nil(rid))
4510 : return -1;
4511 0 : if ((res = store->table_api.table_delete(tr, systc, rid)))
4512 : return res;
4513 : return res;
4514 : }
4515 :
4516 : static int
4517 173 : sys_drop_sequence(sql_trans *tr, sql_sequence * seq, int drop_action)
4518 : {
4519 173 : sqlstore *store = tr->store;
4520 173 : sql_schema *syss = find_sql_schema(tr, "sys");
4521 173 : sql_table *sysseqs = find_sql_table(tr, syss, "sequences");
4522 173 : oid rid = store->table_api.column_find_row(tr, find_sql_column(sysseqs, "id"), &seq->base.id, NULL);
4523 173 : int res = LOG_OK;
4524 :
4525 173 : if (is_oid_nil(rid))
4526 : return -1;
4527 :
4528 173 : if ((res = store->table_api.table_delete(tr, sysseqs, rid)))
4529 : return res;
4530 173 : if (!isNew(seq) && (res = sql_trans_add_dependency_change(tr, seq->base.id, ddl)))
4531 : return res;
4532 173 : if ((res = sql_trans_drop_dependencies(tr, seq->base.id)))
4533 : return res;
4534 173 : if ((res = sql_trans_drop_any_comment(tr, seq->base.id)))
4535 : return res;
4536 173 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, seq->base.id, SEQ_DEPENDENCY)))
4537 : return res;
4538 : return res;
4539 : }
4540 :
4541 : static int
4542 29059 : sys_drop_default_object(sql_trans *tr, sql_column *col, int drop_action)
4543 : {
4544 29059 : const char next_value_for[] = "next value for ";
4545 29059 : int res = LOG_OK;
4546 :
4547 : /* Drop sequence for generated column if it's the case */
4548 29059 : if (col->def && !strncmp(col->def, next_value_for, strlen(next_value_for))) {
4549 164 : sql_schema *s = NULL;
4550 164 : sql_sequence *seq = NULL;
4551 164 : char *schema = NULL, *seq_name = NULL;
4552 :
4553 164 : extract_schema_and_sequence_name(NULL, col->def + strlen(next_value_for), &schema, &seq_name);
4554 164 : if (!schema || !seq_name || !(s = find_sql_schema(tr, schema))) {
4555 : /* didn't find the schema so no generated sequence */
4556 0 : _DELETE(schema);
4557 0 : _DELETE(seq_name);
4558 0 : return res;
4559 : }
4560 :
4561 164 : seq = find_sql_sequence(tr, s, seq_name);
4562 164 : _DELETE(schema);
4563 164 : _DELETE(seq_name);
4564 164 : if (seq && sql_trans_get_dependency_type(tr, seq->base.id, BEDROPPED_DEPENDENCY) > 0) {
4565 143 : if ((res = sys_drop_sequence(tr, seq, drop_action)))
4566 : return res;
4567 143 : if ((res = os_del(s->seqs, tr, seq->base.name, dup_base(&seq->base))))
4568 : return res;
4569 : }
4570 : }
4571 : return res;
4572 : }
4573 :
4574 : static int
4575 34003 : sql_trans_drop_obj_priv(sql_trans *tr, sqlid obj_id)
4576 : {
4577 34003 : sqlstore *store = tr->store;
4578 34003 : sql_schema *sys = find_sql_schema(tr, "sys");
4579 34003 : sql_table *privs = find_sql_table(tr, sys, "privileges");
4580 34003 : int res = LOG_OK;
4581 :
4582 34003 : assert(sys && privs);
4583 : /* select privileges of this obj_id */
4584 34003 : rids *A = store->table_api.rids_select(tr, find_sql_column(privs, "obj_id"), &obj_id, &obj_id, NULL);
4585 34003 : if (A == NULL)
4586 : return LOG_ERR;
4587 : /* remove them */
4588 34257 : for(oid rid = store->table_api.rids_next(A); !is_oid_nil(rid) && res == LOG_OK; rid = store->table_api.rids_next(A))
4589 254 : res = store->table_api.table_delete(tr, privs, rid);
4590 34003 : store->table_api.rids_destroy(A);
4591 34003 : return res;
4592 : }
4593 :
4594 : static int
4595 103 : sys_drop_trigger(sql_trans *tr, sql_trigger * i)
4596 : {
4597 103 : sqlstore *store = tr->store;
4598 103 : node *n;
4599 206 : sql_schema *syss = find_sql_schema(tr, (!i->t || isGlobal(i->t))?"sys":"tmp");
4600 103 : sql_table *systrigger = find_sql_table(tr, syss, "triggers");
4601 103 : oid rid = store->table_api.column_find_row(tr, find_sql_column(systrigger, "id"), &i->base.id, NULL);
4602 103 : int res = LOG_OK;
4603 :
4604 103 : if (is_oid_nil(rid))
4605 : return -1;
4606 103 : if ((res = store->table_api.table_delete(tr, systrigger, rid)))
4607 : return res;
4608 :
4609 100 : if (i->t) {
4610 97 : for (n = i->columns->h; n; n = n->next) {
4611 0 : sql_kc *tc = n->data;
4612 :
4613 0 : if ((res = sys_drop_tc(tr, i, tc)))
4614 0 : return res;
4615 : }
4616 : }
4617 : /* remove trigger from schema */
4618 100 : if ((res = os_del(i->t?i->t->s->triggers:syss->triggers, tr, i->base.name, dup_base(&i->base))))
4619 : return res;
4620 100 : if (!isNew(i) && (res = sql_trans_add_dependency_change(tr, i->base.id, ddl)))
4621 : return res;
4622 100 : if ((res = sql_trans_drop_dependencies(tr, i->base.id)))
4623 : return res;
4624 : return res;
4625 : }
4626 :
4627 : static int
4628 29036 : sys_drop_column(sql_trans *tr, sql_column *col, int drop_action)
4629 : {
4630 29036 : sqlstore *store = tr->store;
4631 29119 : sql_schema *syss = find_sql_schema(tr, isGlobal(col->t)?"sys":"tmp");
4632 29036 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
4633 29036 : oid rid = store->table_api.column_find_row(tr, find_sql_column(syscolumn, "id"), &col->base.id, NULL);
4634 29036 : int res = LOG_OK;
4635 :
4636 29036 : if (is_oid_nil(rid))
4637 : return -1;
4638 29036 : if ((res = store->table_api.table_delete(tr, syscolumn, rid)))
4639 : return res;
4640 29036 : if (!isNew(col) && (res = sql_trans_add_dependency_change(tr, col->base.id, ddl)))
4641 : return res;
4642 29036 : if ((res = sql_trans_drop_dependencies(tr, col->base.id)))
4643 : return res;
4644 29036 : if ((res = sql_trans_drop_any_comment(tr, col->base.id)))
4645 : return res;
4646 29036 : if ((res = sql_trans_drop_obj_priv(tr, col->base.id)))
4647 : return res;
4648 29036 : if ((res = sys_drop_default_object(tr, col, drop_action)))
4649 : return res;
4650 :
4651 29036 : col->base.deleted = 1;
4652 29036 : if (!isNew(col) && !isTempTable(col->t))
4653 17846 : if ((res = store->storage_api.drop_col(tr, (sql_column*)dup_base(&col->base))))
4654 : return res;
4655 :
4656 29036 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, col->base.id, COLUMN_DEPENDENCY)))
4657 : return res;
4658 29036 : if (col->type.type->s && (res = sql_trans_drop_dependency(tr, col->type.type->base.id, col->base.id, TYPE_DEPENDENCY)))
4659 : return res;
4660 : return res;
4661 : }
4662 :
4663 : static int
4664 4086 : sys_drop_keys(sql_trans *tr, sql_table *t, int drop_action)
4665 : {
4666 4086 : node *n;
4667 4086 : int res = LOG_OK;
4668 :
4669 4086 : if (ol_length(t->keys))
4670 2554 : for (n = ol_first_node(t->keys); n; n = n->next) {
4671 1489 : sql_key *k = n->data;
4672 :
4673 1489 : if ((res = sys_drop_key(tr, k, drop_action)))
4674 0 : return res;
4675 : }
4676 : return res;
4677 : }
4678 :
4679 : static int
4680 4086 : sys_drop_idxs(sql_trans *tr, sql_table *t, int drop_action)
4681 : {
4682 4086 : node *n;
4683 4086 : int res = LOG_OK;
4684 :
4685 4086 : if (ol_length(t->idxs))
4686 2669 : for (n = ol_first_node(t->idxs); n; n = n->next) {
4687 1581 : sql_idx *k = n->data;
4688 :
4689 1581 : if ((res = sys_drop_idx(tr, k, drop_action)))
4690 0 : return res;
4691 : }
4692 : return res;
4693 : }
4694 :
4695 : static int
4696 4086 : sys_drop_triggers(sql_trans *tr, sql_table *t)
4697 : {
4698 4086 : node *n;
4699 4086 : int res = LOG_OK;
4700 :
4701 4086 : if (ol_length(t->triggers))
4702 21 : for (n = ol_first_node(t->triggers); n; n = n->next) {
4703 12 : sql_trigger *i = n->data;
4704 :
4705 12 : if ((res = sys_drop_trigger(tr, i)))
4706 0 : return res;
4707 : }
4708 : return res;
4709 : }
4710 :
4711 : static int
4712 4086 : sys_drop_columns(sql_trans *tr, sql_table *t, int drop_action)
4713 : {
4714 4086 : node *n;
4715 4086 : int res = LOG_OK;
4716 :
4717 4086 : if (ol_length(t->columns))
4718 33052 : for (n = t->columns->l->h; n; n = n->next) {
4719 28966 : sql_column *c = n->data;
4720 :
4721 28966 : if ((res = sys_drop_column(tr, c, drop_action)))
4722 0 : return res;
4723 : }
4724 : return res;
4725 : }
4726 :
4727 : static int
4728 295 : sys_drop_part(sql_trans *tr, sql_part *pt, int drop_action)
4729 : {
4730 295 : sqlstore *store = tr->store;
4731 295 : sql_table *mt = pt->t;
4732 295 : sql_schema *syss = find_sql_schema(tr, isGlobal(mt)?"sys":"tmp");
4733 295 : sql_table *sysobj = find_sql_table(tr, syss, "objects");
4734 295 : oid obj_oid = store->table_api.column_find_row(tr, find_sql_column(sysobj, "id"), &pt->base.id, NULL);
4735 295 : int res = LOG_OK;
4736 :
4737 295 : (void)drop_action;
4738 295 : if (is_oid_nil(obj_oid))
4739 : return -1;
4740 :
4741 295 : if ((res = store->table_api.table_delete(tr, sysobj, obj_oid)))
4742 : return res;
4743 295 : if (isRangePartitionTable(mt)) {
4744 93 : if (isPartitionedByColumnTable(pt->t))
4745 80 : col_set_range(tr, pt, false);
4746 93 : sql_table *ranges = find_sql_table(tr, syss, "range_partitions");
4747 93 : assert(ranges);
4748 93 : oid rid = store->table_api.column_find_row(tr, find_sql_column(ranges, "table_id"), &pt->member, NULL);
4749 93 : if ((res = store->table_api.table_delete(tr, ranges, rid)))
4750 : return res;
4751 202 : } else if (isListPartitionTable(mt)) {
4752 25 : sql_table *values = find_sql_table(tr, syss, "value_partitions");
4753 25 : assert(values);
4754 25 : rids *rs = store->table_api.rids_select(tr, find_sql_column(values, "table_id"), &pt->member, &pt->member, NULL);
4755 25 : if (rs == NULL)
4756 : return LOG_ERR;
4757 107 : for (oid rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
4758 82 : if ((res = store->table_api.table_delete(tr, values, rid))) {
4759 0 : store->table_api.rids_destroy(rs);
4760 0 : return res;
4761 : }
4762 : }
4763 25 : store->table_api.rids_destroy(rs);
4764 : }
4765 : /* merge table depends on part table */
4766 295 : if (!isNew(pt) && (res = sql_trans_add_dependency_change(tr, mt->base.id, ddl)))
4767 : return res;
4768 295 : if ((res = sql_trans_drop_dependency(tr, pt->member, mt->base.id, TABLE_DEPENDENCY)))
4769 : return res;
4770 :
4771 295 : if ((res = os_del(mt->s->parts, tr, pt->base.name, dup_base(&pt->base))))
4772 : return res;
4773 : return res;
4774 : }
4775 :
4776 : static int
4777 199 : sys_drop_members(sql_trans *tr, sql_table *t, int drop_action)
4778 : {
4779 199 : int res = LOG_OK;
4780 :
4781 199 : if (!list_empty(t->members)) {
4782 185 : for (node *n = t->members->h; n; ) {
4783 104 : sql_part *pt = n->data;
4784 :
4785 104 : n = n->next;
4786 104 : if ((res = sys_drop_part(tr, pt, drop_action)))
4787 0 : return res;
4788 : }
4789 : }
4790 : return res;
4791 : }
4792 :
4793 : static int
4794 21 : sys_drop_parts(sql_trans *tr, sql_table *t, int drop_action)
4795 : {
4796 21 : int res = LOG_OK;
4797 :
4798 42 : for(sql_part *pt = partition_find_part(tr, t, NULL); pt; pt = partition_find_part(tr, t, pt)) {
4799 21 : if ((res = sql_trans_del_table(tr, pt->t, t, drop_action)))
4800 0 : return res;
4801 : }
4802 : return res;
4803 : }
4804 :
4805 : static int
4806 4086 : sys_drop_table(sql_trans *tr, sql_table *t, int drop_action)
4807 : {
4808 4086 : sqlstore *store = tr->store;
4809 4136 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
4810 4086 : sql_table *systable = find_sql_table(tr, syss, "_tables");
4811 4086 : sql_column *syscol = find_sql_column(systable, "id");
4812 4086 : oid rid = store->table_api.column_find_row(tr, syscol, &t->base.id, NULL);
4813 4086 : int res = LOG_OK;
4814 :
4815 4086 : if (is_oid_nil(rid))
4816 : return -1;
4817 :
4818 4086 : if ((res = store->table_api.table_delete(tr, systable, rid)))
4819 : return res;
4820 4086 : if ((res = sys_drop_keys(tr, t, drop_action)))
4821 : return res;
4822 4086 : if ((res = sys_drop_idxs(tr, t, drop_action)))
4823 : return res;
4824 4086 : if ((res = sys_drop_triggers(tr, t)))
4825 : return res;
4826 :
4827 4086 : if (partition_find_part(tr, t, NULL))
4828 21 : if ((res = sys_drop_parts(tr, t, drop_action)))
4829 : return res;
4830 :
4831 4086 : if (isMergeTable(t) || isReplicaTable(t))
4832 199 : if ((res = sys_drop_members(tr, t, drop_action)))
4833 : return res;
4834 :
4835 4086 : if (isRangePartitionTable(t) || isListPartitionTable(t)) {
4836 66 : sql_table *partitions = find_sql_table(tr, syss, "table_partitions");
4837 66 : assert(partitions);
4838 66 : sql_column *pcols = find_sql_column(partitions, "table_id");
4839 66 : assert(pcols);
4840 66 : rids *rs = store->table_api.rids_select(tr, pcols, &t->base.id, &t->base.id, NULL);
4841 66 : if (!rs)
4842 : return -1;
4843 66 : oid poid;
4844 66 : if (rs == NULL)
4845 : return LOG_ERR;
4846 66 : if ((poid = store->table_api.rids_next(rs)) != oid_nil) {
4847 66 : if ((res = store->table_api.table_delete(tr, partitions, poid))) {
4848 0 : store->table_api.rids_destroy(rs);
4849 0 : return res;
4850 : }
4851 : }
4852 66 : store->table_api.rids_destroy(rs);
4853 : }
4854 :
4855 4086 : if ((res = sql_trans_drop_any_comment(tr, t->base.id)))
4856 : return res;
4857 4086 : if (!isNew(t) && (res = sql_trans_add_dependency_change(tr, t->base.id, ddl)))
4858 : return res;
4859 4086 : if ((res = sql_trans_drop_dependencies(tr, t->base.id)))
4860 : return res;
4861 4086 : if ((res = sql_trans_drop_obj_priv(tr, t->base.id)))
4862 : return res;
4863 4086 : if ((res = sys_drop_columns(tr, t, drop_action)))
4864 : return res;
4865 :
4866 4086 : if (isTable(t) && !isNew(t))
4867 3309 : if ((res = store->storage_api.drop_del(tr, t)))
4868 : return res;
4869 :
4870 4149 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, t->base.id, !isView(t) ? TABLE_DEPENDENCY : VIEW_DEPENDENCY)))
4871 : return res;
4872 : return res;
4873 : }
4874 :
4875 : static int
4876 3 : sys_drop_type(sql_trans *tr, sql_type *type, int drop_action)
4877 : {
4878 3 : sqlstore *store = tr->store;
4879 3 : sql_schema *syss = find_sql_schema(tr, "sys");
4880 3 : sql_table *sys_tab_type = find_sql_table(tr, syss, "types");
4881 3 : sql_column *sys_type_col = find_sql_column(sys_tab_type, "id");
4882 3 : oid rid = store->table_api.column_find_row(tr, sys_type_col, &type->base.id, NULL);
4883 3 : int res = LOG_OK;
4884 :
4885 3 : if (is_oid_nil(rid))
4886 : return -1;
4887 :
4888 3 : if ((res = store->table_api.table_delete(tr, sys_tab_type, rid)))
4889 : return res;
4890 3 : if (!isNew(type) && (res = sql_trans_add_dependency_change(tr, type->base.id, ddl)))
4891 : return res;
4892 3 : if ((res = sql_trans_drop_dependencies(tr, type->base.id)))
4893 : return res;
4894 :
4895 3 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, type->base.id, TYPE_DEPENDENCY)))
4896 : return res;
4897 : return res;
4898 : }
4899 :
4900 : static int
4901 723 : sys_drop_func(sql_trans *tr, sql_func *func, int drop_action)
4902 : {
4903 723 : sqlstore *store = tr->store;
4904 723 : sql_schema *syss = find_sql_schema(tr, "sys");
4905 723 : sql_table *sys_tab_func = find_sql_table(tr, syss, "functions");
4906 723 : sql_column *sys_func_col = find_sql_column(sys_tab_func, "id");
4907 723 : oid rid_func = store->table_api.column_find_row(tr, sys_func_col, &func->base.id, NULL);
4908 723 : if (is_oid_nil(rid_func))
4909 : return -1;
4910 723 : sql_table *sys_tab_args = find_sql_table(tr, syss, "args");
4911 723 : sql_column *sys_args_col = find_sql_column(sys_tab_args, "func_id");
4912 723 : rids *args = store->table_api.rids_select(tr, sys_args_col, &func->base.id, &func->base.id, NULL);
4913 723 : int res = LOG_OK;
4914 :
4915 723 : if (args == NULL)
4916 : return LOG_ERR;
4917 2701 : for (oid r = store->table_api.rids_next(args); !is_oid_nil(r); r = store->table_api.rids_next(args)) {
4918 1979 : if ((res = store->table_api.table_delete(tr, sys_tab_args, r))) {
4919 1 : store->table_api.rids_destroy(args);
4920 1 : return res;
4921 : }
4922 : }
4923 722 : store->table_api.rids_destroy(args);
4924 :
4925 722 : assert(!is_oid_nil(rid_func));
4926 722 : if ((res = store->table_api.table_delete(tr, sys_tab_func, rid_func)))
4927 : return res;
4928 :
4929 722 : if (!isNew(func) && (res = sql_trans_add_dependency_change(tr, func->base.id, ddl)))
4930 : return res;
4931 722 : if ((res = sql_trans_drop_dependencies(tr, func->base.id)))
4932 : return res;
4933 722 : if ((res = sql_trans_drop_any_comment(tr, func->base.id)))
4934 : return res;
4935 722 : if ((res = sql_trans_drop_obj_priv(tr, func->base.id)))
4936 : return res;
4937 :
4938 798 : if (drop_action && (res = sql_trans_drop_all_dependencies(tr, func->base.id, !IS_PROC(func) ? FUNC_DEPENDENCY : PROC_DEPENDENCY)))
4939 : return res;
4940 722 : if (!func->vararg && func->ops) {
4941 1729 : for (node *n = func->ops->h; n; n = n->next) {
4942 1010 : sql_arg *a = n->data;
4943 :
4944 1010 : if (a->type.type->s && (res = sql_trans_drop_dependency(tr, a->type.type->base.id, func->base.id, TYPE_DEPENDENCY)))
4945 0 : return res;
4946 : }
4947 : }
4948 722 : if (!func->varres && func->res) {
4949 1603 : for (node *n = func->res->h; n; n = n->next) {
4950 968 : sql_arg *a = n->data;
4951 :
4952 968 : if (a->type.type->s && (res = sql_trans_drop_dependency(tr, a->type.type->base.id, func->base.id, TYPE_DEPENDENCY)))
4953 0 : return res;
4954 : }
4955 : }
4956 : return res;
4957 : }
4958 :
4959 : static int
4960 159 : sys_drop_types(sql_trans *tr, sql_schema *s, int drop_action)
4961 : {
4962 159 : int res = LOG_OK;
4963 159 : struct os_iter oi;
4964 159 : os_iterator(&oi, s->types, tr, NULL);
4965 159 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
4966 0 : sql_type *t = (sql_type*)b;
4967 :
4968 0 : if ((res = sys_drop_type(tr, t, drop_action)))
4969 0 : return res;
4970 : }
4971 : return res;
4972 : }
4973 :
4974 : static int
4975 159 : sys_drop_tables(sql_trans *tr, sql_schema *s, int drop_action)
4976 : {
4977 159 : int res = LOG_OK;
4978 159 : struct os_iter oi;
4979 159 : os_iterator(&oi, s->tables, tr, NULL);
4980 202 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
4981 43 : sql_table *t = (sql_table*)b;
4982 :
4983 43 : if ((res = sys_drop_table(tr, t, drop_action)))
4984 0 : return res;
4985 : }
4986 : return res;
4987 : }
4988 :
4989 : static int
4990 159 : sys_drop_funcs(sql_trans *tr, sql_schema *s, int drop_action)
4991 : {
4992 159 : int res = LOG_OK;
4993 159 : struct os_iter oi;
4994 159 : os_iterator(&oi, s->funcs, tr, NULL);
4995 179 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
4996 20 : sql_func *f = (sql_func*)b;
4997 :
4998 20 : if ((res = sys_drop_func(tr, f, drop_action)))
4999 0 : return res;
5000 : }
5001 : return res;
5002 : }
5003 :
5004 : static int
5005 159 : sys_drop_sequences(sql_trans *tr, sql_schema *s, int drop_action)
5006 : {
5007 159 : int res = LOG_OK;
5008 159 : struct os_iter oi;
5009 159 : os_iterator(&oi, s->seqs, tr, NULL);
5010 160 : for (sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
5011 1 : sql_sequence *seq = (sql_sequence*)b;
5012 :
5013 1 : if ((res = sys_drop_sequence(tr, seq, drop_action)))
5014 0 : return res;
5015 : }
5016 : return res;
5017 : }
5018 :
5019 : int
5020 902 : sql_trans_create_type(sql_trans *tr, sql_schema *s, const char *sqlname, unsigned int digits, unsigned int scale, int radix, const char *impl)
5021 : {
5022 902 : sqlstore *store = tr->store;
5023 902 : sql_type *t;
5024 902 : sql_table *systype;
5025 902 : int localtype = ATOMindex(impl);
5026 902 : sql_class eclass = EC_EXTERNAL;
5027 902 : int eclass_cast = (int) eclass, res = LOG_OK;
5028 :
5029 902 : if (localtype < 0)
5030 : return -4;
5031 902 : t = ZNEW(sql_type);
5032 902 : systype = find_sql_table(tr, find_sql_schema(tr, "sys"), "types");
5033 902 : base_init(NULL, &t->base, next_oid(tr->store), true, sqlname);
5034 902 : t->impl =_STRDUP(impl);
5035 902 : t->digits = digits;
5036 902 : t->scale = scale;
5037 902 : t->radix = radix;
5038 902 : t->eclass = eclass;
5039 902 : t->localtype = localtype;
5040 902 : t->s = s;
5041 :
5042 902 : if ((res = os_add(s->types, tr, t->base.name, &t->base)))
5043 : return res;
5044 901 : if ((res = store->table_api.table_insert(tr, systype, &t->base.id, &t->impl, &t->base.name, &t->digits, &t->scale, &radix, &eclass_cast, &s->base.id)))
5045 : return res;
5046 : return res;
5047 : }
5048 :
5049 : int
5050 3 : sql_trans_drop_type(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
5051 : {
5052 3 : sql_type *t = sql_trans_find_type(tr, s, id);
5053 3 : int res = LOG_OK;
5054 :
5055 3 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
5056 1 : sqlid* local_id = MNEW(sqlid);
5057 1 : if (!local_id)
5058 : return -1;
5059 :
5060 1 : if (!tr->dropped) {
5061 1 : tr->dropped = list_create((fdestroy) &id_destroy);
5062 1 : if (!tr->dropped) {
5063 0 : _DELETE(local_id);
5064 0 : return -1;
5065 : }
5066 : }
5067 1 : *local_id = t->base.id;
5068 1 : list_append(tr->dropped, local_id);
5069 : }
5070 :
5071 3 : if ((res = sys_drop_type(tr, t, drop_action)))
5072 : return res;
5073 3 : if ((res = os_del(s->types, tr, t->base.name, dup_base(&t->base))))
5074 : return res;
5075 :
5076 3 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
5077 1 : list_destroy(tr->dropped);
5078 1 : tr->dropped = NULL;
5079 : }
5080 : return res;
5081 : }
5082 :
5083 : sql_func *
5084 120755 : create_sql_func(sqlstore *store, allocator *sa, const char *func, list *args, list *res, sql_ftype type, sql_flang lang, const char *mod,
5085 : const char *impl, const char *query, bit varres, bit vararg, bit system, bit side_effect)
5086 : {
5087 120755 : sql_func *t = SA_ZNEW(sa, sql_func);
5088 :
5089 120755 : base_init(sa, &t->base, next_oid(store), true, func);
5090 120755 : assert(mod);
5091 120755 : t->imp = (impl)?SA_STRDUP(sa, impl):NULL;
5092 120755 : t->mod = SA_STRDUP(sa, mod);
5093 120755 : t->type = type;
5094 120755 : t->lang = lang;
5095 120755 : t->instantiated = lang != FUNC_LANG_SQL && lang != FUNC_LANG_MAL;
5096 120755 : t->semantics = TRUE;
5097 120755 : t->side_effect = side_effect;
5098 120755 : t->varres = varres;
5099 120755 : t->vararg = vararg;
5100 120755 : t->ops = args;
5101 120755 : t->res = res;
5102 120755 : t->query = (query)?SA_STRDUP(sa, query):NULL;
5103 120755 : t->fix_scale = SCALE_EQ;
5104 120755 : t->s = NULL;
5105 120755 : t->system = system;
5106 120755 : return t;
5107 : }
5108 :
5109 : int
5110 121157 : sql_trans_create_func(sql_func **fres, sql_trans *tr, sql_schema *s, const char *func, list *args, list *ffres, sql_ftype type, sql_flang lang,
5111 : const char *mod, const char *impl, const char *query, bit varres, bit vararg, bit system, bit side_effect)
5112 : {
5113 121157 : sqlstore *store = tr->store;
5114 121157 : sql_schema *syss = find_sql_schema(tr, "sys");
5115 121157 : sql_table *sysfunc = find_sql_table(tr, syss, "functions");
5116 121157 : sql_table *sysarg = find_sql_table(tr, syss, "args");
5117 121157 : node *n;
5118 121157 : int number = 0, ftype = (int) type, flang = (int) lang, res = LOG_OK;
5119 121157 : bit semantics = TRUE;
5120 :
5121 121157 : sql_func *t = ZNEW(sql_func);
5122 121157 : base_init(NULL, &t->base, next_oid(tr->store), true, func);
5123 121157 : assert(mod);
5124 121157 : t->imp = (impl)?_STRDUP(impl):NULL;
5125 121157 : t->mod =_STRDUP(mod);
5126 121157 : t->type = type;
5127 121157 : t->lang = lang;
5128 121157 : t->instantiated = lang != FUNC_LANG_SQL && lang != FUNC_LANG_MAL;
5129 121157 : t->semantics = semantics;
5130 121157 : t->side_effect = side_effect;
5131 121157 : t->varres = varres;
5132 121157 : t->vararg = vararg;
5133 121157 : t->ops = list_create((fdestroy) &arg_destroy);
5134 121157 : t->fix_scale = SCALE_EQ;
5135 121157 : t->system = system;
5136 310605 : for (n=args->h; n; n = n->next)
5137 189448 : list_append(t->ops, arg_dup(tr, s, n->data));
5138 121157 : if (ffres) {
5139 102225 : t->res = list_create((fdestroy) &arg_destroy);
5140 260254 : for (n=ffres->h; n; n = n->next)
5141 158029 : list_append(t->res, arg_dup(tr, s, n->data));
5142 : }
5143 121157 : t->query = (query)?_STRDUP(query):NULL;
5144 121157 : t->s = s;
5145 :
5146 121157 : if ((res = os_add(s->funcs, tr, t->base.name, &t->base)))
5147 : return res;
5148 121156 : if ((res = store->table_api.table_insert(tr, sysfunc, &t->base.id, &t->base.name, query?(char**)&query:&t->imp, &t->mod, &flang, &ftype, &side_effect,
5149 : &varres, &vararg, &s->base.id, &system, &semantics)))
5150 : return res;
5151 279184 : if (t->res) for (n = t->res->h; n; n = n->next, number++) {
5152 158028 : sql_arg *a = n->data;
5153 158028 : sqlid id = next_oid(tr->store);
5154 158028 : if ((res = store->table_api.table_insert(tr, sysarg, &id, &t->base.id, &a->name, &a->type.type->base.name, &a->type.digits, &a->type.scale, &a->inout, &number)))
5155 0 : return res;
5156 : }
5157 310604 : if (t->ops) for (n = t->ops->h; n; n = n->next, number++) {
5158 189448 : sql_arg *a = n->data;
5159 189448 : sqlid id = next_oid(tr->store);
5160 189448 : if ((res = store->table_api.table_insert(tr, sysarg, &id, &t->base.id, &a->name, &a->type.type->base.name, &a->type.digits, &a->type.scale, &a->inout, &number)))
5161 0 : return res;
5162 : }
5163 121156 : *fres = t;
5164 121156 : return res;
5165 : }
5166 :
5167 : int
5168 660 : sql_trans_drop_func(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
5169 : {
5170 660 : sql_base *b = os_find_id(s->funcs, tr, id);
5171 660 : int res = LOG_OK;
5172 :
5173 660 : if (!b)
5174 : return res;
5175 :
5176 660 : sql_func *func = (sql_func*)b;
5177 660 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
5178 69 : sqlid *local_id = MNEW(sqlid);
5179 69 : if (!local_id)
5180 : return -1;
5181 :
5182 69 : if (! tr->dropped) {
5183 42 : tr->dropped = list_create((fdestroy) &id_destroy);
5184 42 : if (!tr->dropped) {
5185 0 : _DELETE(local_id);
5186 0 : return -1;
5187 : }
5188 : }
5189 69 : *local_id = func->base.id;
5190 69 : list_append(tr->dropped, local_id);
5191 : }
5192 :
5193 660 : if ((res = sys_drop_func(tr, func, DROP_CASCADE)))
5194 : return res;
5195 659 : if ((res = os_del(s->funcs, tr, func->base.name, dup_base(&func->base))))
5196 : return res;
5197 :
5198 659 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
5199 42 : list_destroy(tr->dropped);
5200 42 : tr->dropped = NULL;
5201 : }
5202 : return res;
5203 : }
5204 :
5205 : static int
5206 43 : build_drop_func_list_item(sql_trans *tr, sql_schema *s, sqlid id)
5207 : {
5208 43 : sql_base *b = os_find_id(s->funcs, tr, id);
5209 43 : int res = LOG_OK;
5210 :
5211 43 : if (b) {
5212 43 : sql_func *func = (sql_func*)b;
5213 43 : if ((res = sys_drop_func(tr, func, DROP_CASCADE)))
5214 : return res;
5215 43 : if ((res = os_del(s->funcs, tr, func->base.name, dup_base(&func->base))))
5216 : return res;
5217 : }
5218 : return res;
5219 : }
5220 :
5221 : int
5222 31 : sql_trans_drop_all_func(sql_trans *tr, sql_schema *s, list *list_func, int drop_action)
5223 : {
5224 31 : list *to_drop = NULL;
5225 31 : int res = LOG_OK;
5226 :
5227 31 : (void) drop_action;
5228 31 : if (!tr->dropped && !(tr->dropped = list_create((fdestroy) &id_destroy)))
5229 : return -1;
5230 :
5231 74 : for (node *n = list_func->h; n ; n = n->next ) {
5232 43 : sql_func *func = (sql_func *) n->data;
5233 :
5234 43 : if (!list_find_id(tr->dropped, func->base.id)) {
5235 43 : sqlid *local_id = MNEW(sqlid);
5236 43 : if (!local_id) {
5237 0 : list_destroy(tr->dropped);
5238 0 : tr->dropped = NULL;
5239 0 : if (to_drop)
5240 0 : list_destroy(to_drop);
5241 0 : return -1;
5242 : }
5243 43 : if (!to_drop && !(to_drop = list_create(NULL))) {
5244 0 : list_destroy(tr->dropped);
5245 0 : tr->dropped = NULL;
5246 0 : return -1;
5247 : }
5248 43 : *local_id = func->base.id;
5249 43 : list_append(tr->dropped, local_id);
5250 43 : list_append(to_drop, func);
5251 : }
5252 : }
5253 :
5254 31 : if (to_drop) {
5255 74 : for (node *n = to_drop->h; n ; n = n->next ) {
5256 43 : sql_func *func = (sql_func *) n->data;
5257 43 : if ((res = build_drop_func_list_item(tr, s, func->base.id))) {
5258 0 : list_destroy(tr->dropped);
5259 0 : tr->dropped = NULL;
5260 0 : list_destroy(to_drop);
5261 0 : return res;
5262 : }
5263 : }
5264 31 : list_destroy(to_drop);
5265 : }
5266 :
5267 31 : list_destroy(tr->dropped);
5268 31 : tr->dropped = NULL;
5269 31 : return res;
5270 : }
5271 :
5272 : int
5273 1111 : sql_trans_create_schema(sql_trans *tr, const char *name, sqlid auth_id, sqlid owner, sqlid *schema_id_ptr)
5274 : {
5275 1111 : sqlstore *store = tr->store;
5276 1111 : sql_schema *s = ZNEW(sql_schema);
5277 1111 : sql_table *sysschema = find_sql_table(tr, find_sql_schema(tr, "sys"), "schemas");
5278 1111 : int res = LOG_OK;
5279 :
5280 1111 : base_init(NULL, &s->base, next_oid(tr->store), true, name);
5281 1111 : s->auth_id = auth_id;
5282 1111 : s->owner = owner;
5283 1111 : s->system = FALSE;
5284 1111 : assert(!isTempSchema(s)); // TODO transaction_layer_revamp: check if this is really true
5285 1111 : s->tables = os_new(NULL, (destroy_fptr) &table_destroy, false, true, true, false, store);
5286 1111 : s->types = os_new(NULL, (destroy_fptr) &type_destroy, false, true, true, false, store);
5287 1111 : s->funcs = os_new(NULL, (destroy_fptr) &func_destroy, false, false, false, false, store);
5288 1111 : s->seqs = os_new(NULL, (destroy_fptr) &seq_destroy, false, true, true, false, store);
5289 1111 : s->keys = os_new(NULL, (destroy_fptr) &key_destroy, false, true, true, false, store);
5290 1111 : s->idxs = os_new(NULL, (destroy_fptr) &idx_destroy, false, true, true, false, store);
5291 1111 : s->triggers = os_new(NULL, (destroy_fptr) &trigger_destroy, false, true, true, false, store);
5292 1111 : s->parts = os_new(NULL, (destroy_fptr) &part_destroy, false, false, true, false, store);
5293 1111 : s->store = tr->store;
5294 :
5295 1111 : if ((res = store->table_api.table_insert(tr, sysschema, &s->base.id, &s->base.name, &s->auth_id, &s->owner, &s->system))) {
5296 0 : schema_destroy(store, s);
5297 0 : return res;
5298 : }
5299 1111 : if ((res = os_add(tr->cat->schemas, tr, s->base.name, &s->base)))
5300 : return res;
5301 1110 : if ((res = sql_trans_add_dependency(tr, s->auth_id, ddl)))
5302 : return res;
5303 1110 : if ((res = sql_trans_add_dependency(tr, s->owner, ddl)))
5304 : return res;
5305 1110 : if (schema_id_ptr)
5306 33 : *schema_id_ptr = s->base.id;
5307 : return res;
5308 : }
5309 :
5310 : int
5311 8 : sql_trans_rename_schema(sql_trans *tr, sqlid id, const char *new_name)
5312 : {
5313 8 : sqlstore *store = tr->store;
5314 8 : table_functions table_api = store->table_api;
5315 8 : sql_table *schemas = find_sql_table(tr, find_sql_schema(tr, "sys"), "schemas");
5316 8 : sql_schema *s = find_sql_schema_id(tr, id), *ns = NULL;
5317 8 : oid rid;
5318 8 : int res = LOG_OK;
5319 :
5320 16 : assert(!strNil(new_name));
5321 :
5322 8 : rid = table_api.column_find_row(tr, find_sql_column(schemas, "id"), &id, NULL);
5323 8 : assert(!is_oid_nil(rid));
5324 8 : if ((res = table_api.column_update_value(tr, find_sql_column(schemas, "name"), rid, (void*) new_name)))
5325 : return res;
5326 :
5327 8 : if (!isNew(s) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
5328 : return res;
5329 :
5330 : /* delete schema, add schema */
5331 8 : if ((res = os_del(tr->cat->schemas, tr, s->base.name, dup_base(&s->base))))
5332 : return res;
5333 :
5334 8 : if ((res = schema_dup(tr, s, new_name, &ns)) ||
5335 8 : (res = os_add(tr->cat->schemas, tr, ns->base.name, &ns->base))) {
5336 0 : return res;
5337 : }
5338 :
5339 : return res;
5340 : }
5341 :
5342 : int
5343 32 : sql_trans_change_schema_authorization(sql_trans *tr, sqlid id, sqlid auth_id)
5344 : {
5345 32 : sqlstore *store = tr->store;
5346 32 : sql_table *sysschema = find_sql_table(tr, find_sql_schema(tr, "sys"), "schemas");
5347 32 : sql_schema *s = find_sql_schema_id(tr, id), *ns = NULL;
5348 32 : oid rid;
5349 32 : int res = LOG_OK;
5350 :
5351 32 : assert(auth_id);
5352 32 : s->auth_id = auth_id;
5353 :
5354 32 : rid = store->table_api.column_find_row(tr, find_sql_column(sysschema, "id"), &id, NULL);
5355 32 : assert(!is_oid_nil(rid));
5356 32 : if ((res = store->table_api.column_update_value(tr, find_sql_column(sysschema, "authorization"), rid, &auth_id)))
5357 : return res;
5358 :
5359 32 : if (!isNew(s) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
5360 : return res;
5361 : /* delete schema, add schema */
5362 32 : if ((res = os_del(tr->cat->schemas, tr, s->base.name, dup_base(&s->base))))
5363 : return res;
5364 32 : if ((res = schema_dup(tr, s, s->base.name, &ns)) || (res = os_add(tr->cat->schemas, tr, ns->base.name, &ns->base))) {
5365 0 : return res;
5366 : }
5367 : return res;
5368 : }
5369 :
5370 : int
5371 159 : sql_trans_drop_schema(sql_trans *tr, sqlid id, int drop_action)
5372 : {
5373 159 : sqlstore *store = tr->store;
5374 159 : sql_schema *s = find_sql_schema_id(tr, id);
5375 159 : sql_table *sysschema = find_sql_table(tr, find_sql_schema(tr, "sys"), "schemas");
5376 159 : oid rid = store->table_api.column_find_row(tr, find_sql_column(sysschema, "id"), &s->base.id, NULL);
5377 159 : int res = LOG_OK;
5378 :
5379 159 : if (is_oid_nil(rid))
5380 : return 0;
5381 159 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
5382 81 : sqlid* local_id = MNEW(sqlid);
5383 81 : if (!local_id)
5384 : return -1;
5385 :
5386 81 : if (!tr->dropped) {
5387 81 : tr->dropped = list_create((fdestroy) &id_destroy);
5388 81 : if (!tr->dropped) {
5389 0 : _DELETE(local_id);
5390 0 : return -1;
5391 : }
5392 : }
5393 81 : *local_id = s->base.id;
5394 81 : list_append(tr->dropped, local_id);
5395 : }
5396 :
5397 159 : if ((res = store->table_api.table_delete(tr, sysschema, rid)))
5398 : return res;
5399 159 : if (!isNew(s) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
5400 : return res;
5401 159 : if ((res = sys_drop_funcs(tr, s, drop_action)))
5402 : return res;
5403 159 : if ((res = sys_drop_tables(tr, s, drop_action)))
5404 : return res;
5405 159 : if ((res = sys_drop_types(tr, s, drop_action)))
5406 : return res;
5407 159 : if ((res = sys_drop_sequences(tr, s, drop_action)))
5408 : return res;
5409 159 : if ((res = sql_trans_drop_any_comment(tr, s->base.id)))
5410 : return res;
5411 159 : if ((res = sql_trans_drop_obj_priv(tr, s->base.id)))
5412 : return res;
5413 :
5414 159 : if ((res = os_del(tr->cat->schemas, tr, s->base.name, dup_base(&s->base))))
5415 : return res;
5416 :
5417 159 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
5418 81 : list_destroy(tr->dropped);
5419 81 : tr->dropped = NULL;
5420 : }
5421 : return res;
5422 : }
5423 :
5424 : static int
5425 485 : sql_trans_propagate_dependencies_parents(sql_trans *tr, sql_table *mt, bool *child_of_partitioned)
5426 : {
5427 485 : int res = LOG_OK;
5428 485 : sql_part *pt = NULL;
5429 :
5430 1000 : for (; mt; mt = pt?pt->t:NULL) {
5431 500 : if ((res = store_reset_sql_functions(tr, mt->base.id))) /* reset sql functions depending on the table */
5432 0 : return res;
5433 500 : if (!isNew(mt) && (res = sql_trans_add_dependency(tr, mt->base.id, ddl))) /* protect from another transaction changing the table's schema */
5434 0 : return res;
5435 500 : if (child_of_partitioned)
5436 620 : *child_of_partitioned |= (isRangePartitionTable(mt) || isListPartitionTable(mt));
5437 500 : pt = partition_find_part(tr, mt, NULL);
5438 : }
5439 : return res;
5440 : }
5441 :
5442 : static int
5443 504 : sql_trans_propagate_dependencies_children(sql_trans *tr, sql_table *pt, bool child_of_partitioned)
5444 : {
5445 504 : int res = LOG_OK;
5446 :
5447 504 : if (THRhighwater()) /* for now throw malloc failure error */
5448 : return -1;
5449 :
5450 504 : if ((res = store_reset_sql_functions(tr, pt->base.id))) /* reset sql functions depending on the table */
5451 : return res;
5452 504 : if (!isNew(pt)) {
5453 354 : if ((res = sql_trans_add_dependency(tr, pt->base.id, ddl))) /* protect from another transaction changing the table's schema */
5454 : return res;
5455 354 : if ((res = sql_trans_add_dependency_change(tr, pt->base.id, ddl))) /* protect from being added twice */
5456 : return res;
5457 354 : if (child_of_partitioned && isTable(pt) && (res = sql_trans_add_dependency(tr, pt->base.id, dml))) /* disallow concurrent updates on pt */
5458 : return res;
5459 : }
5460 504 : if ((isMergeTable(pt) || isReplicaTable(pt)) && !list_empty(pt->members)) {
5461 33 : for (node *nt = pt->members->h; nt; nt = nt->next) {
5462 19 : sql_part *pd = nt->data;
5463 19 : sql_table *t = find_sql_table_id(tr, pt->s, pd->member);
5464 :
5465 19 : child_of_partitioned |= (isRangePartitionTable(t) || isListPartitionTable(t));
5466 19 : if ((res = sql_trans_propagate_dependencies_children(tr, t, child_of_partitioned)))
5467 0 : return res;
5468 : }
5469 : }
5470 : return res;
5471 : }
5472 :
5473 : int
5474 302 : sql_trans_add_table(sql_trans *tr, sql_table *mt, sql_table *pt)
5475 : {
5476 302 : sqlstore *store = tr->store;
5477 302 : sql_schema *syss = find_sql_schema(tr, isGlobal(mt)?"sys":"tmp");
5478 302 : sql_table *sysobj = find_sql_table(tr, syss, "objects");
5479 302 : int res = LOG_OK;
5480 302 : sql_table *dup = NULL;
5481 302 : bool child_of_partitioned = false;
5482 :
5483 : /* merge table depends on part table */
5484 302 : if ((res = sql_trans_create_dependency(tr, pt->base.id, mt->base.id, TABLE_DEPENDENCY)))
5485 : return res;
5486 302 : assert(isMergeTable(mt) || isReplicaTable(mt));
5487 :
5488 302 : if ((res = new_table(tr, mt, &dup)))
5489 : return res;
5490 299 : mt = dup;
5491 299 : if (!mt->members)
5492 0 : mt->members = list_create((fdestroy) &part_destroy);
5493 299 : sql_part *p = ZNEW(sql_part);
5494 299 : p->t = mt;
5495 299 : p->member = pt->base.id;
5496 :
5497 299 : base_init(NULL, &p->base, next_oid(store), true, pt->base.name);
5498 299 : list_append(mt->members, p);
5499 299 : if ((res = store->table_api.table_insert(tr, sysobj, &p->base.id, &p->base.name, &mt->base.id, &pt->base.id)))
5500 : return res;
5501 299 : if ((res = os_add(mt->s->parts, tr, p->base.name, dup_base(&p->base))))
5502 : return res;
5503 299 : if ((res = sql_trans_propagate_dependencies_parents(tr, mt, &child_of_partitioned)))
5504 : return res;
5505 299 : if ((res = sql_trans_propagate_dependencies_children(tr, pt, child_of_partitioned)))
5506 : return res;
5507 : return res;
5508 : }
5509 :
5510 : int
5511 176 : sql_trans_add_range_partition(sql_trans *tr, sql_table *mt, sql_table *pt, sql_subtype tpe, ptr min, ptr max,
5512 : bit with_nills, int update, sql_part **err)
5513 : {
5514 176 : sqlstore *store = tr->store;
5515 176 : sql_schema *syss = find_sql_schema(tr, isGlobal(mt)?"sys":"tmp");
5516 176 : sql_table *sysobj = find_sql_table(tr, syss, "objects");
5517 176 : sql_table *partitions = find_sql_table(tr, syss, "table_partitions");
5518 176 : sql_table *ranges = find_sql_table(tr, syss, "range_partitions");
5519 176 : sql_part *p;
5520 176 : int localtype = tpe.type->localtype, res = LOG_OK;
5521 176 : ValRecord vmin, vmax;
5522 176 : size_t smin, smax;
5523 176 : bit to_insert = with_nills;
5524 176 : oid rid;
5525 176 : ptr ok;
5526 176 : sql_table *dup = NULL;
5527 :
5528 176 : assert(sysobj);
5529 176 : assert(partitions);
5530 176 : assert(ranges);
5531 :
5532 176 : vmin = vmax = (ValRecord) {.vtype = TYPE_void,};
5533 :
5534 176 : if ((res = new_table(tr, mt, &dup)))
5535 : return res;
5536 175 : mt = dup;
5537 175 : if (!mt->members)
5538 0 : mt->members = list_create((fdestroy) &part_destroy);
5539 175 : if (min) {
5540 175 : ok = VALinit(&vmin, localtype, min);
5541 175 : if (ok && localtype != TYPE_str)
5542 168 : ok = VALconvert(TYPE_str, &vmin);
5543 : } else {
5544 0 : ok = VALinit(&vmin, TYPE_str, ATOMnilptr(TYPE_str));
5545 0 : min = (ptr) ATOMnilptr(localtype);
5546 : }
5547 175 : if (!ok) {
5548 0 : res = -1;
5549 0 : goto finish;
5550 : }
5551 175 : smin = ATOMlen(localtype, min);
5552 175 : if (smin > STORAGE_MAX_VALUE_LENGTH) {
5553 0 : res = -10;
5554 0 : goto finish;
5555 : }
5556 :
5557 175 : if (max) {
5558 175 : ok = VALinit(&vmax, localtype, max);
5559 175 : if (ok && localtype != TYPE_str)
5560 168 : ok = VALconvert(TYPE_str, &vmax);
5561 : } else {
5562 0 : ok = VALinit(&vmax, TYPE_str, ATOMnilptr(TYPE_str));
5563 0 : max = (ptr) ATOMnilptr(localtype);
5564 : }
5565 175 : if (!ok) {
5566 0 : res = -1;
5567 0 : goto finish;
5568 : }
5569 175 : smax = ATOMlen(localtype, max);
5570 175 : if (smax > STORAGE_MAX_VALUE_LENGTH) {
5571 0 : res = -11;
5572 0 : goto finish;
5573 : }
5574 :
5575 175 : if (!update) {
5576 172 : p = ZNEW(sql_part);
5577 172 : base_init(NULL, &p->base, next_oid(store), true, pt->base.name);
5578 172 : assert(isMergeTable(mt) || isReplicaTable(mt));
5579 172 : p->t = mt;
5580 172 : assert(pt);
5581 172 : p->member = pt->base.id;
5582 : } else {
5583 3 : node *n = members_find_child_id(mt->members, pt->base.id);
5584 3 : p = (sql_part*) n->data;
5585 : }
5586 :
5587 : /* add range partition values */
5588 175 : if (update) {
5589 3 : _DELETE(p->part.range.minvalue);
5590 3 : _DELETE(p->part.range.maxvalue);
5591 : }
5592 175 : p->part.range.minvalue = NEW_ARRAY(char, smin);
5593 175 : p->part.range.maxvalue = NEW_ARRAY(char, smax);
5594 175 : memcpy(p->part.range.minvalue, min, smin);
5595 175 : memcpy(p->part.range.maxvalue, max, smax);
5596 175 : p->part.range.minlength = smin;
5597 175 : p->part.range.maxlength = smax;
5598 175 : p->with_nills = with_nills;
5599 :
5600 175 : if (!update) {
5601 172 : *err = list_append_with_validate(mt->members, p, &localtype, sql_range_part_validate_and_insert);
5602 172 : if (*err)
5603 38 : part_destroy(store, p);
5604 : } else {
5605 3 : *err = list_transverse_with_validate(mt->members, p, &localtype, sql_range_part_validate_and_insert);
5606 : }
5607 175 : if (*err) {
5608 38 : res = -12;
5609 38 : goto finish;
5610 : }
5611 :
5612 137 : if (isPartitionedByColumnTable(p->t))
5613 124 : col_set_range(tr, p, true);
5614 :
5615 137 : if (!update) {
5616 134 : rid = store->table_api.column_find_row(tr, find_sql_column(partitions, "table_id"), &mt->base.id, NULL);
5617 134 : assert(!is_oid_nil(rid));
5618 :
5619 : /* add merge table dependency */
5620 134 : if ((res = sql_trans_create_dependency(tr, pt->base.id, mt->base.id, TABLE_DEPENDENCY)))
5621 0 : goto finish;
5622 134 : sqlid id = store->table_api.column_find_sqlid(tr, find_sql_column(partitions, "id"), rid);
5623 134 : if ((res = store->table_api.table_insert(tr, sysobj, &p->base.id, &p->base.name, &mt->base.id, &pt->base.id)))
5624 0 : goto finish;
5625 134 : char *vmin_val = VALget(&vmin);
5626 134 : char *vmax_val = VALget(&vmax);
5627 134 : if ((res = store->table_api.table_insert(tr, ranges, &pt->base.id, &id, &vmin_val, &vmax_val, &to_insert)))
5628 0 : goto finish;
5629 : } else {
5630 3 : sql_column *cmin = find_sql_column(ranges, "minimum"), *cmax = find_sql_column(ranges, "maximum"),
5631 3 : *wnulls = find_sql_column(ranges, "with_nulls");
5632 :
5633 3 : rid = store->table_api.column_find_row(tr, find_sql_column(ranges, "table_id"), &pt->base.id, NULL);
5634 3 : assert(!is_oid_nil(rid));
5635 :
5636 3 : if ((res = store->table_api.column_update_value(tr, cmin, rid, VALget(&vmin))))
5637 0 : goto finish;
5638 3 : if ((res = store->table_api.column_update_value(tr, cmax, rid, VALget(&vmax))))
5639 0 : goto finish;
5640 3 : if ((res = store->table_api.column_update_value(tr, wnulls, rid, &to_insert)))
5641 0 : goto finish;
5642 : }
5643 :
5644 137 : if (!update && (res = os_add(mt->s->parts, tr, p->base.name, dup_base(&p->base))))
5645 0 : goto finish;
5646 137 : if ((res = sql_trans_propagate_dependencies_parents(tr, mt, NULL)))
5647 0 : goto finish;
5648 137 : res = sql_trans_propagate_dependencies_children(tr, pt, true);
5649 :
5650 175 : finish:
5651 175 : VALclear(&vmin);
5652 175 : VALclear(&vmax);
5653 175 : return res;
5654 : }
5655 :
5656 : int
5657 54 : sql_trans_add_value_partition(sql_trans *tr, sql_table *mt, sql_table *pt, sql_subtype tpe, list* vals, bit with_nills,
5658 : int update, sql_part **err)
5659 : {
5660 54 : sqlstore *store = tr->store;
5661 54 : sql_schema *syss = find_sql_schema(tr, isGlobal(mt)?"sys":"tmp");
5662 54 : sql_table *sysobj = find_sql_table(tr, syss, "objects");
5663 54 : sql_table *partitions = find_sql_table(tr, syss, "table_partitions");
5664 54 : sql_table *values = find_sql_table(tr, syss, "value_partitions");
5665 54 : sql_part *p;
5666 54 : oid rid;
5667 54 : int localtype = tpe.type->localtype, i = 0, res = LOG_OK;
5668 54 : sql_table *dup = NULL;
5669 :
5670 54 : assert(sysobj);
5671 54 : assert(partitions);
5672 54 : assert(values);
5673 :
5674 54 : if ((res = new_table(tr, mt, &dup)))
5675 : return res;
5676 54 : mt = dup;
5677 54 : if (!mt->members)
5678 0 : mt->members = list_create((fdestroy) &part_destroy);
5679 54 : if (!update) {
5680 52 : p = ZNEW(sql_part);
5681 52 : base_init(NULL, &p->base, next_oid(store), true, pt->base.name);
5682 52 : assert(isMergeTable(mt) || isReplicaTable(mt));
5683 52 : p->t = mt;
5684 52 : assert(pt);
5685 52 : p->member = pt->base.id;
5686 : } else {
5687 2 : rids *rs;
5688 2 : node *n = members_find_child_id(mt->members, pt->base.id);
5689 2 : p = (sql_part*) n->data;
5690 :
5691 2 : rs = store->table_api.rids_select(tr, find_sql_column(values, "table_id"), &pt->base.id, &pt->base.id, NULL);
5692 2 : if (rs == NULL)
5693 : return LOG_ERR;
5694 8 : for (rid = store->table_api.rids_next(rs); !is_oid_nil(rid); rid = store->table_api.rids_next(rs)) {
5695 6 : if ((res = store->table_api.table_delete(tr, values, rid))) { /* eliminate the old values */
5696 0 : store->table_api.rids_destroy(rs);
5697 0 : return res;
5698 : }
5699 : }
5700 2 : store->table_api.rids_destroy(rs);
5701 : }
5702 54 : p->with_nills = with_nills;
5703 :
5704 54 : rid = store->table_api.column_find_row(tr, find_sql_column(partitions, "table_id"), &mt->base.id, NULL);
5705 54 : assert(!is_oid_nil(rid));
5706 :
5707 54 : sqlid id = store->table_api.column_find_sqlid(tr, find_sql_column(partitions, "id"), rid);
5708 :
5709 54 : if (with_nills) { /* store the null value first */
5710 11 : ValRecord vnnil;
5711 11 : if (VALinit(&vnnil, TYPE_str, ATOMnilptr(TYPE_str)) == NULL) {
5712 0 : if (!update)
5713 0 : part_destroy(store, p);
5714 0 : list_destroy2(vals, store);
5715 0 : return -1;
5716 : }
5717 11 : char *vnnil_val = VALget(&vnnil);
5718 11 : if ((res = store->table_api.table_insert(tr, values, &pt->base.id, &id, &vnnil_val))) {
5719 0 : list_destroy2(vals, store);
5720 0 : return res;
5721 : }
5722 11 : VALclear(&vnnil);
5723 : }
5724 :
5725 217 : for (node *n = vals->h ; n ; n = n->next) {
5726 163 : sql_part_value *next = (sql_part_value*) n->data;
5727 163 : ValRecord vvalue;
5728 163 : ptr ok;
5729 :
5730 163 : if (ATOMlen(localtype, next->value) > STORAGE_MAX_VALUE_LENGTH) {
5731 0 : if (!update)
5732 0 : part_destroy(store, p);
5733 0 : list_destroy2(vals, store);
5734 0 : return -i - 10;
5735 : }
5736 163 : ok = VALinit(&vvalue, localtype, next->value);
5737 163 : if (ok && localtype != TYPE_str)
5738 163 : ok = VALconvert(TYPE_str, &vvalue);
5739 163 : if (!ok) {
5740 0 : if (!update)
5741 0 : part_destroy(store, p);
5742 0 : VALclear(&vvalue);
5743 0 : list_destroy2(vals, store);
5744 0 : return -i - 10;
5745 : }
5746 163 : char *vvalue_val = VALget(&vvalue);
5747 163 : if ((res = store->table_api.table_insert(tr, values, &pt->base.id, &id, &vvalue_val))) {
5748 0 : VALclear(&vvalue);
5749 0 : list_destroy2(vals, store);
5750 0 : return res;
5751 : }
5752 :
5753 163 : VALclear(&vvalue);
5754 163 : i++;
5755 : }
5756 :
5757 54 : if (p->part.values)
5758 2 : list_destroy2(p->part.values, store);
5759 54 : p->part.values = vals;
5760 :
5761 54 : if (!update) {
5762 52 : *err = list_append_with_validate(mt->members, p, &localtype, sql_values_part_validate_and_insert);
5763 52 : if (*err)
5764 5 : part_destroy(store, p);
5765 : } else {
5766 2 : *err = list_transverse_with_validate(mt->members, p, &localtype, sql_values_part_validate_and_insert);
5767 : }
5768 54 : if (*err)
5769 : return -4;
5770 :
5771 49 : if (!update) {
5772 : /* add merge table dependency */
5773 47 : if ((res = sql_trans_create_dependency(tr, pt->base.id, mt->base.id, TABLE_DEPENDENCY)))
5774 : return res;
5775 47 : if ((res = store->table_api.table_insert(tr, sysobj, &p->base.id, &p->base.name, &mt->base.id, &pt->base.id)))
5776 : return res;
5777 47 : if ((res = os_add(mt->s->parts, tr, p->base.name, dup_base(&p->base))))
5778 : return res;
5779 : }
5780 49 : if ((res = sql_trans_propagate_dependencies_parents(tr, mt, NULL)))
5781 : return res;
5782 49 : if ((res = sql_trans_propagate_dependencies_children(tr, pt, true)))
5783 : return res;
5784 : return res;
5785 : }
5786 :
5787 : int
5788 19 : sql_trans_rename_table(sql_trans *tr, sql_schema *s, sqlid id, const char *new_name)
5789 : {
5790 19 : sqlstore *store = tr->store;
5791 37 : sql_table *systable = find_sql_table(tr, find_sql_schema(tr, isTempSchema(s) ? "tmp":"sys"), "_tables");
5792 19 : sql_table *t = find_sql_table_id(tr, s, id), *dup = NULL;
5793 19 : oid rid;
5794 19 : int res = LOG_OK;
5795 :
5796 38 : assert(!strNil(new_name));
5797 :
5798 19 : rid = store->table_api.column_find_row(tr, find_sql_column(systable, "id"), &id, NULL);
5799 19 : assert(!is_oid_nil(rid));
5800 19 : if ((res = store->table_api.column_update_value(tr, find_sql_column(systable, "name"), rid, (void*) new_name)))
5801 : return res;
5802 :
5803 17 : if (isGlobal(t)) {
5804 16 : if (!isNew(t) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
5805 : return res;
5806 16 : if ((res = os_del(s->tables, tr, t->base.name, dup_base(&t->base))))
5807 : return res;
5808 : } else {
5809 1 : assert(isTempTable(t));
5810 1 : sql_base *b = os_find_id(tr->localtmps, tr, t->base.id);
5811 1 : if ((res = os_del(tr->localtmps, tr, b->name, dup_base(b))))
5812 : return res;
5813 : }
5814 :
5815 16 : if ((res = table_dup(tr, t, t->s, new_name, &dup, true)))
5816 : return res;
5817 : return res;
5818 : }
5819 :
5820 : int
5821 26 : sql_trans_set_table_schema(sql_trans *tr, sqlid id, sql_schema *os, sql_schema *ns)
5822 : {
5823 26 : sqlstore *store = tr->store;
5824 52 : sql_table *systable = find_sql_table(tr, find_sql_schema(tr, isTempSchema(os) ? "tmp":"sys"), "_tables");
5825 26 : sql_base *b = os_find_id(os->tables, tr, id);
5826 26 : sql_table *t = (sql_table*)b, *dup = NULL;
5827 26 : oid rid;
5828 26 : int res = LOG_OK;
5829 :
5830 26 : rid = store->table_api.column_find_row(tr, find_sql_column(systable, "id"), &t->base.id, NULL);
5831 26 : assert(!is_oid_nil(rid));
5832 26 : if ((res = store->table_api.column_update_value(tr, find_sql_column(systable, "schema_id"), rid, &(ns->base.id))))
5833 : return res;
5834 :
5835 26 : if (!isNew(t) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
5836 : return res;
5837 26 : if ((res = os_del(os->tables, tr, t->base.name, dup_base(&t->base))))
5838 : return res;
5839 25 : return table_dup(tr, t, ns, NULL, &dup, true);
5840 : }
5841 :
5842 : int
5843 191 : sql_trans_del_table(sql_trans *tr, sql_table *mt, sql_table *pt, int drop_action)
5844 : {
5845 191 : int res = LOG_OK;
5846 191 : sqlstore *store = tr->store;
5847 191 : sql_table *dup = NULL, *mt_it;
5848 191 : sql_part *mti_part = NULL;
5849 :
5850 191 : if ((res = new_table(tr, mt, &dup)))
5851 : return res;
5852 191 : mt = dup;
5853 191 : node *n = members_find_child_id(mt->members, pt->base.id); /* get sqlpart id*/
5854 191 : sqlid part_id = ((sql_part*)n->data)->base.id;
5855 191 : sql_base *b = os_find_id(mt->s->parts, tr, part_id); /* fetch updated part */
5856 191 : sql_part *p = (sql_part*)b;
5857 :
5858 191 : if ((res = sys_drop_part(tr, p, drop_action)))
5859 : return res;
5860 : /*Clean the part from members*/
5861 191 : list_remove_node(mt->members, store, n);
5862 :
5863 191 : if (drop_action == DROP_CASCADE && (res = sql_trans_drop_table_id(tr, mt->s, pt->base.id, drop_action)))
5864 : return res;
5865 406 : for (mt_it = mt; mt_it; mt_it = mti_part?mti_part->t:NULL) {
5866 : /* functions depending on parent tables need to be recompiled, ugh */
5867 203 : if ((res = store_reset_sql_functions(tr, mt_it->base.id))) /* reset sql functions depending on the table */
5868 0 : return res;
5869 203 : mti_part = partition_find_part(tr, mt_it, NULL);
5870 : }
5871 191 : if ((res = store_reset_sql_functions(tr, pt->base.id))) /* reset sql functions depending on the table */
5872 : return res;
5873 : return res;
5874 : }
5875 :
5876 : int
5877 34111 : sql_trans_create_table(sql_table **tres, sql_trans *tr, sql_schema *s, const char *name, const char *sql, int tt, bit system,
5878 : int persistence, int commit_action, int sz, bte properties)
5879 : {
5880 34111 : sqlstore *store = tr->store;
5881 34111 : sql_table *t = create_sql_table_with_id(NULL, next_oid(tr->store), name, tt, system, persistence, commit_action, properties);
5882 34183 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
5883 34111 : sql_table *systable = find_sql_table(tr, syss, "_tables");
5884 34111 : sht ca;
5885 34111 : int res = LOG_OK;
5886 :
5887 : /* temps all belong to a special tmp schema and only views/remote have a query */
5888 34111 : assert( (isTable(t) ||
5889 : (!isTempTable(t) || (strcmp(s->base.name, "tmp") == 0) || isDeclaredTable(t))) || (isView(t) && !sql) || (isRemote(t) && !sql));
5890 :
5891 34111 : t->query = sql ?_STRDUP(sql) : NULL;
5892 34111 : t->s = s;
5893 34111 : t->sz = sz;
5894 34111 : if (sz < 0)
5895 1344 : t->sz = COLSIZE;
5896 :
5897 34111 : if ((res = os_add(isGlobal(t)?s->tables:tr->localtmps, tr, t->base.name, &t->base)))
5898 : return res;
5899 :
5900 34107 : if (isUnloggedTable(t))
5901 6 : t->persistence = SQL_PERSIST; // It's not a temporary
5902 :
5903 34107 : if (isRemote(t))
5904 101 : t->persistence = SQL_REMOTE;
5905 :
5906 34107 : if (isTable(t))
5907 10988 : if ((res = store->storage_api.create_del(tr, t))) {
5908 : ATOMIC_PTR_DESTROY(&t->data);
5909 : return res;
5910 : }
5911 34107 : if (isPartitionedByExpressionTable(t)) {
5912 22 : t->part.pexp = ZNEW(sql_expression);
5913 22 : t->part.pexp->type = *sql_bind_localtype("void"); /* leave it non-initialized, at the backend the copy of this table will get the type */
5914 22 : t->part.pexp->cols = list_create((fdestroy) &int_destroy);
5915 : }
5916 :
5917 34107 : ca = t->commit_action;
5918 34107 : if (!isDeclaredTable(t)) {
5919 34107 : char *strnil = (char*)ATOMnilptr(TYPE_str);
5920 34107 : if ((res = store->table_api.table_insert(tr, systable, &t->base.id, &t->base.name, &s->base.id,
5921 34107 : (t->query) ? &t->query : &strnil, &t->type, &t->system, &ca, &t->access))) {
5922 0 : ATOMIC_PTR_DESTROY(&t->data);
5923 0 : return res;
5924 : }
5925 : }
5926 34107 : *tres = t;
5927 34107 : return res;
5928 : }
5929 :
5930 : int
5931 33639 : sql_trans_set_partition_table(sql_trans *tr, sql_table *t)
5932 : {
5933 33639 : int res = LOG_OK;
5934 33639 : sqlstore *store = tr->store;
5935 :
5936 33639 : if (t && (isRangePartitionTable(t) || isListPartitionTable(t))) {
5937 99 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
5938 99 : sql_table *partitions = find_sql_table(tr, syss, "table_partitions");
5939 99 : assert(partitions);
5940 99 : sqlid next = next_oid(tr->store);
5941 99 : if (isPartitionedByColumnTable(t)) {
5942 80 : assert(t->part.pcol);
5943 80 : if ((res = store->table_api.table_insert(tr, partitions, &next, &t->base.id, &t->part.pcol->base.id, &ATOMnilptr(TYPE_str), &t->properties)))
5944 0 : return res;
5945 19 : } else if (isPartitionedByExpressionTable(t)) {
5946 19 : assert(t->part.pexp->exp);
5947 19 : if (strlen(t->part.pexp->exp) > STORAGE_MAX_VALUE_LENGTH)
5948 : return -4;
5949 19 : if ((res = store->table_api.table_insert(tr, partitions, &next, &t->base.id, ATOMnilptr(TYPE_int), &t->part.pexp->exp, &t->properties)))
5950 : return res;
5951 : } else {
5952 0 : assert(0);
5953 : }
5954 : }
5955 : return res;
5956 : }
5957 :
5958 : sql_key *
5959 7512 : create_sql_kc(sqlstore *store, allocator *sa, sql_key *k, sql_column *c)
5960 : {
5961 7512 : sql_kc *kc = SA_ZNEW(sa, sql_kc);
5962 :
5963 7512 : kc->c = c;
5964 7512 : list_append(k->columns, kc);
5965 7512 : if (k->idx)
5966 962 : create_sql_ic(store, sa, k->idx, c);
5967 7512 : if (k->type == pkey)
5968 4191 : c->null = 0;
5969 7512 : return k;
5970 : }
5971 :
5972 : sql_key *
5973 5952 : create_sql_ukey(sqlstore *store, allocator *sa, sql_table *t, const char *name, key_type kt, const char* check)
5974 : {
5975 5952 : sql_key *nk = NULL;
5976 5952 : sql_ukey *tk;
5977 :
5978 5952 : nk = (kt != fkey) ? (sql_key *) SA_ZNEW(sa, sql_ukey) : (sql_key *) SA_ZNEW(sa, sql_fkey);
5979 5952 : tk = (sql_ukey *) nk;
5980 5952 : assert(name);
5981 :
5982 5952 : base_init(sa, &nk->base, next_oid(store), true, name);
5983 5952 : nk->type = kt;
5984 5952 : nk->columns = SA_LIST(sa, (fdestroy) NULL);
5985 5952 : nk->idx = NULL;
5986 5952 : nk->t = t;
5987 5952 : nk->check = check ? SA_STRDUP(sa, check) : NULL;
5988 :
5989 5952 : if (nk->type == pkey)
5990 3873 : t->pkey = tk;
5991 5952 : if (ol_add(t->keys, &nk->base))
5992 0 : return NULL;
5993 : return nk;
5994 : }
5995 :
5996 : sql_fkey *
5997 941 : create_sql_fkey(sqlstore *store, allocator *sa, sql_table *t, const char *name, key_type kt, sql_key *rkey, int on_delete, int on_update)
5998 : {
5999 941 : sql_key *nk;
6000 941 : sql_fkey *fk = NULL;
6001 :
6002 941 : nk = (kt != fkey) ? (sql_key *) SA_ZNEW(sa, sql_ukey) : (sql_key *) SA_ZNEW(sa, sql_fkey);
6003 :
6004 941 : assert(name);
6005 941 : base_init(sa, &nk->base, next_oid(store), true, name);
6006 941 : nk->type = kt;
6007 941 : nk->columns = SA_LIST(sa, (fdestroy) NULL);
6008 941 : nk->t = t;
6009 941 : nk->idx = create_sql_idx(store, sa, t, name, (nk->type == fkey) ? join_idx : hash_idx);
6010 941 : nk->idx->key = nk;
6011 :
6012 941 : fk = (sql_fkey *) nk;
6013 :
6014 941 : fk->on_delete = on_delete;
6015 941 : fk->on_update = on_update;
6016 :
6017 941 : fk->rkey = rkey->base.id;
6018 941 : if (ol_add(t->keys, &nk->base))
6019 0 : return NULL;
6020 : return (sql_fkey*) nk;
6021 : }
6022 :
6023 : sql_idx *
6024 7192 : create_sql_idx(sqlstore *store, allocator *sa, sql_table *t, const char *name, idx_type it)
6025 : {
6026 7192 : sql_idx *ni = SA_ZNEW(sa, sql_idx);
6027 :
6028 7192 : base_init(sa, &ni->base, next_oid(store), true, name);
6029 7192 : ni->columns = SA_LIST(sa, (fdestroy) NULL);
6030 7192 : ni->t = t;
6031 7192 : ni->type = it;
6032 7192 : ni->key = NULL;
6033 7192 : if (ol_add(t->idxs, &ni->base))
6034 0 : return NULL;
6035 : return ni;
6036 : }
6037 :
6038 : sql_idx *
6039 7907 : create_sql_ic(sqlstore *store, allocator *sa, sql_idx *i, sql_column *c)
6040 : {
6041 7907 : sql_kc *ic = SA_ZNEW(sa, sql_kc);
6042 :
6043 7907 : ic->c = c;
6044 7907 : list_append(i->columns, ic);
6045 :
6046 7907 : (void)store;
6047 7907 : return i;
6048 : }
6049 :
6050 : sql_idx *
6051 9528 : create_sql_idx_done(sql_trans *tr, sql_idx *i)
6052 : {
6053 9528 : (void) tr;
6054 9528 : if (i && i->key && hash_index(i->type)) {
6055 8165 : int ncols = list_length(i->columns);
6056 16951 : for (node *n = i->columns->h ; n ; n = n->next) {
6057 8786 : sql_kc *kc = n->data;
6058 :
6059 8786 : kc->c->unique = (ncols == 1) ? 2 : MAX(kc->c->unique, 1);
6060 : }
6061 : }
6062 9528 : return i;
6063 : }
6064 :
6065 : static sql_column *
6066 265447 : create_sql_column_with_id(allocator *sa, sqlid id, sql_table *t, const char *name, sql_subtype *tpe)
6067 : {
6068 265447 : sql_column *col = SA_ZNEW(sa, sql_column);
6069 :
6070 265447 : base_init(sa, &col->base, id, true, name);
6071 265447 : col->type = *tpe;
6072 265447 : col->def = NULL;
6073 265447 : col->null = 1;
6074 265447 : col->colnr = table_next_column_nr(t);
6075 265447 : col->t = t;
6076 265447 : col->unique = 0;
6077 265447 : col->storage_type = NULL;
6078 :
6079 265447 : if (ol_add(t->columns, &col->base))
6080 : return NULL;
6081 265447 : ATOMIC_PTR_INIT(&col->data, NULL);
6082 265447 : return col;
6083 : }
6084 :
6085 : sql_column *
6086 255663 : create_sql_column(sqlstore *store, allocator *sa, sql_table *t, const char *name, sql_subtype *tpe)
6087 : {
6088 255663 : return create_sql_column_with_id(sa, next_oid(store), t, name, tpe);
6089 : }
6090 :
6091 : int
6092 4043 : sql_trans_drop_table(sql_trans *tr, sql_schema *s, const char *name, int drop_action)
6093 : {
6094 4043 : sql_table *t = find_sql_table(tr, s, name);
6095 :
6096 4043 : if (!t) {
6097 0 : TRC_ERROR(SQL_STORE, "sql_trans_drop_table: Table %s.%s does not exist\n", s->base.name, name);
6098 0 : return -1;
6099 : }
6100 :
6101 4043 : sql_table *gt = t;
6102 4043 : if (t && isTempTable(t)) {
6103 111 : gt = (sql_table*)os_find_id(s->tables, tr, t->base.id);
6104 : }
6105 :
6106 4043 : assert(t == gt || !gt || (isTempTable(gt) && !isLocalTemp(gt) && isLocalTemp(t)));
6107 :
6108 4043 : int res = LOG_OK;
6109 :
6110 4043 : if ((drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) &&
6111 537 : tr->dropped && list_find_id(tr->dropped, t->base.id))
6112 : return res;
6113 :
6114 537 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
6115 537 : sqlid *local_id = MNEW(sqlid);
6116 537 : if (!local_id)
6117 : return -1;
6118 :
6119 537 : if (! tr->dropped) {
6120 486 : tr->dropped = list_create((fdestroy) &id_destroy);
6121 486 : if (!tr->dropped) {
6122 0 : _DELETE(local_id);
6123 0 : return -1;
6124 : }
6125 : }
6126 537 : *local_id = t->base.id;
6127 537 : list_append(tr->dropped, local_id);
6128 : }
6129 :
6130 4043 : if (!isDeclaredTable(t))
6131 4093 : if ((res = sys_drop_table(tr, gt?gt:t, drop_action)))
6132 : return res;
6133 :
6134 4043 : if (isNew(t))
6135 171 : t->base.deleted = 1;
6136 4043 : if (gt && (res = os_del(s->tables, tr, gt->base.name, dup_base(>->base))))
6137 : return res;
6138 4043 : if (t != gt && (res = os_del(tr->localtmps, tr, t->base.name, dup_base(&t->base))))
6139 : return res;
6140 :
6141 4043 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
6142 486 : list_destroy(tr->dropped);
6143 486 : tr->dropped = NULL;
6144 : }
6145 : return res;
6146 : }
6147 :
6148 : int
6149 57 : sql_trans_drop_table_id(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
6150 : {
6151 57 : sql_table *t = find_sql_table_id(tr, s, id);
6152 :
6153 57 : if (t)
6154 57 : return sql_trans_drop_table(tr, s, t->base.name, drop_action);
6155 : else
6156 : return SQL_ERR;
6157 : }
6158 :
6159 : BUN
6160 41801 : sql_trans_clear_table(sql_trans *tr, sql_table *t)
6161 : {
6162 41801 : sqlstore *store = tr->store;
6163 41801 : return store->storage_api.clear_table(tr, t);
6164 : }
6165 :
6166 : int
6167 9784 : sql_trans_create_column(sql_column **rcol, sql_trans *tr, sql_table *t, const char *name, sql_subtype *tpe)
6168 : {
6169 9784 : sqlstore *store = tr->store;
6170 9784 : sql_column *col;
6171 9784 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
6172 9784 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
6173 9784 : int res = LOG_OK;
6174 :
6175 9784 : if (!tpe)
6176 : return -1; /* TODO not sure what to do here */
6177 :
6178 9784 : col = create_sql_column_with_id(NULL, next_oid(tr->store), t, name, tpe);
6179 :
6180 9784 : if (isTable(col->t))
6181 5376 : if ((res = store->storage_api.create_col(tr, col))) {
6182 : ATOMIC_PTR_DESTROY(&col->data);
6183 : return res;
6184 : }
6185 9784 : if (!isDeclaredTable(t)) {
6186 9784 : char *strnil = (char*)ATOMnilptr(TYPE_str);
6187 9784 : int digits = type_digits(&col->type);
6188 9784 : if ((res = store->table_api.table_insert(tr, syscolumn, &col->base.id, &col->base.name, &col->type.type->base.name, &digits, &col->type.scale,
6189 9784 : &t->base.id, (col->def) ? &col->def : &strnil, &col->null, &col->colnr, (col->storage_type) ? &col->storage_type : &strnil))) {
6190 0 : ATOMIC_PTR_DESTROY(&col->data);
6191 0 : return res;
6192 : }
6193 : }
6194 :
6195 9784 : if (tpe->type->s) {/* column depends on type */
6196 0 : if ((res = sql_trans_create_dependency(tr, tpe->type->base.id, col->base.id, TYPE_DEPENDENCY))) {
6197 : ATOMIC_PTR_DESTROY(&col->data);
6198 : return res;
6199 : }
6200 0 : if (!isNew(tpe->type) && (res = sql_trans_add_dependency(tr, tpe->type->base.id, ddl))) {
6201 : ATOMIC_PTR_DESTROY(&col->data);
6202 : return res;
6203 : }
6204 : }
6205 9784 : *rcol = col;
6206 9784 : return res;
6207 : }
6208 :
6209 : void
6210 68 : drop_sql_column(sql_table *t, sqlid id, int drop_action)
6211 : {
6212 68 : node *n = ol_find_id(t->columns, id);
6213 68 : sql_column *col = n->data;
6214 :
6215 68 : col->drop_action = drop_action;
6216 68 : col->base.deleted = 1;
6217 : //ol_del(t->columns, t->s->store, n);
6218 68 : }
6219 :
6220 : void
6221 0 : drop_sql_idx(sql_table *t, sqlid id)
6222 : {
6223 0 : node *n = ol_find_id(t->idxs, id);
6224 0 : sql_idx *i = n->data;
6225 :
6226 0 : i->base.deleted = 1;
6227 : //ol_del(t->idxs, t->s->store, n);
6228 0 : }
6229 :
6230 : void
6231 0 : drop_sql_key(sql_table *t, sqlid id, int drop_action)
6232 : {
6233 0 : node *n = ol_find_id(t->keys, id);
6234 0 : sql_key *k = n->data;
6235 :
6236 0 : k->drop_action = drop_action;
6237 0 : k->base.deleted = 1;
6238 : //ol_del(t->keys, t->s->store, n);
6239 0 : }
6240 :
6241 : int
6242 13 : sql_trans_rename_column(sql_trans *tr, sql_table *t, sqlid id, const char *old_name, const char *new_name)
6243 : {
6244 13 : sqlstore *store = tr->store;
6245 13 : sql_table *syscolumn = find_sql_table(tr, find_sql_schema(tr, isGlobal(t)?"sys":"tmp"), "_columns");
6246 13 : oid rid;
6247 13 : int res = LOG_OK;
6248 13 : sql_table *dup = NULL;
6249 13 : node *n;
6250 :
6251 26 : assert(!strNil(new_name));
6252 :
6253 13 : rid = store->table_api.column_find_row(tr, find_sql_column(syscolumn, "id"), &id, NULL);
6254 13 : assert(!is_oid_nil(rid));
6255 13 : if ((res = store->table_api.column_update_value(tr, find_sql_column(syscolumn, "name"), rid, (void*) new_name)))
6256 : return res;
6257 :
6258 13 : if ((res = new_table(tr, t, &dup)))
6259 : return res;
6260 13 : t = dup;
6261 13 : if (!(n = ol_find_name(t->columns, old_name)))
6262 : return -1;
6263 13 : sql_column *c = n->data;
6264 :
6265 13 : if (!isNew(c) && (res = sql_trans_add_dependency_change(tr, c->t->base.id, ddl)))
6266 : return res;
6267 13 : if (!isNew(c) && (res = sql_trans_add_dependency_change(tr, id, ddl)))
6268 : return res;
6269 :
6270 13 : _DELETE(c->base.name);
6271 13 : c->base.name =_STRDUP(new_name);
6272 13 : if (ol_rehash(t->columns, old_name, n) == NULL)
6273 : return -1;
6274 : return res;
6275 : }
6276 :
6277 : static int
6278 644 : new_column(sql_trans *tr, sql_column *col, sql_column **cres)
6279 : {
6280 644 : int res = LOG_OK;
6281 644 : sql_table *t = NULL;
6282 :
6283 644 : if ((res = new_table(tr, col->t, &t)))
6284 : return res;
6285 644 : *cres = find_sql_column(t, col->base.name);
6286 644 : return res;
6287 : }
6288 :
6289 : int
6290 71 : sql_trans_drop_column(sql_trans *tr, sql_table *t, sqlid id, int drop_action)
6291 : {
6292 71 : sqlstore *store = tr->store;
6293 71 : node *n = NULL;
6294 71 : sql_table *syscolumn = find_sql_table(tr, find_sql_schema(tr, isGlobal(t)?"sys":"tmp"), "_columns");
6295 71 : sql_column *col = NULL, *cid = find_sql_column(syscolumn, "id"), *cnr = find_sql_column(syscolumn, "number");
6296 71 : int res = LOG_OK;
6297 71 : sql_table *dup = NULL;
6298 :
6299 71 : if ((res = new_table(tr, t, &dup)))
6300 : return res;
6301 70 : t = dup;
6302 537 : for (node *nn = t->columns->l->h ; nn ; nn = nn->next) {
6303 467 : sql_column *next = (sql_column *) nn->data;
6304 467 : if (next->base.id == id) {
6305 : n = nn;
6306 : col = next;
6307 397 : } else if (col) { /* if the column to be dropped was found, decrease the column number for others after it */
6308 92 : next->colnr--;
6309 :
6310 92 : if (!isDeclaredTable(t)) {
6311 92 : oid rid = store->table_api.column_find_row(tr, cid, &next->base.id, NULL);
6312 92 : assert(!is_oid_nil(rid));
6313 92 : if ((res = store->table_api.column_update_value(tr, cnr, rid, &next->colnr)))
6314 0 : return res;
6315 : }
6316 : }
6317 : }
6318 :
6319 70 : assert(n && col); /* the column to be dropped must have been found */
6320 :
6321 70 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
6322 11 : sqlid *local_id = MNEW(sqlid);
6323 11 : if (!local_id)
6324 : return -1;
6325 :
6326 11 : if (! tr->dropped) {
6327 8 : tr->dropped = list_create((fdestroy) &id_destroy);
6328 8 : if (!tr->dropped) {
6329 0 : _DELETE(local_id);
6330 0 : return -1;
6331 : }
6332 : }
6333 11 : *local_id = col->base.id;
6334 11 : list_append(tr->dropped, local_id);
6335 : }
6336 :
6337 70 : if (!isNew(col) && (res = sql_trans_add_dependency_change(tr, col->t->base.id, ddl)))
6338 : return res;
6339 70 : if (!isNew(col) && isGlobal(col->t) && !isGlobalTemp(col->t) && (res = sql_trans_add_dependency(tr, col->t->base.id, dml)))
6340 : return res;
6341 70 : if ((res = sys_drop_column(tr, col, drop_action)))
6342 : return res;
6343 :
6344 70 : if (isNew(col)) { /* remove create from changes */
6345 7 : trans_del(tr, &col->base);
6346 7 : if (!isNew(col->t))
6347 1 : column_destroy(store, col);
6348 : }
6349 70 : ol_del(t->columns, store, n);
6350 :
6351 70 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
6352 8 : list_destroy(tr->dropped);
6353 8 : tr->dropped = NULL;
6354 : }
6355 : return res;
6356 : }
6357 :
6358 : int
6359 4713 : sql_trans_alter_null(sql_trans *tr, sql_column *col, int isnull)
6360 : {
6361 4713 : int res = LOG_OK;
6362 4713 : sqlstore *store = tr->store;
6363 :
6364 4713 : if (col->null != isnull) {
6365 563 : sql_schema *syss = find_sql_schema(tr, isGlobal(col->t)?"sys":"tmp");
6366 563 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
6367 563 : oid rid = store->table_api.column_find_row(tr, find_sql_column(syscolumn, "id"),
6368 563 : &col->base.id, NULL);
6369 563 : sql_column *dup = NULL;
6370 :
6371 563 : if (is_oid_nil(rid))
6372 1 : return -1;
6373 563 : if ((res = store->table_api.column_update_value(tr, find_sql_column(syscolumn, "null"), rid, &isnull)))
6374 : return res;
6375 :
6376 562 : if ((res = new_column(tr, col, &dup)))
6377 : return res;
6378 562 : col = dup;
6379 562 : dup->null = isnull;
6380 :
6381 : /* disallow concurrent updates on the column if not null is set */
6382 : /* this dependency is needed for merge tables */
6383 562 : if (!isNew(col) && (res = sql_trans_add_dependency(tr, col->t->base.id, ddl)))
6384 : return res;
6385 562 : if (!isnull && !isNew(col) && isGlobal(col->t) && !isGlobalTemp(col->t) && (res = sql_trans_add_dependency(tr, col->t->base.id, dml)))
6386 : return res;
6387 562 : if ((res = store_reset_sql_functions(tr, col->t->base.id))) /* reset sql functions depending on the table */
6388 : return res;
6389 562 : if (isNew(col) || isnull) /* new ie can still change, or persistent only widen semantics */
6390 457 : store->storage_api.col_not_null(tr, col, !isnull);
6391 : }
6392 : return res;
6393 : }
6394 :
6395 : int
6396 2299 : sql_trans_alter_access(sql_trans *tr, sql_table *t, sht access)
6397 : {
6398 2299 : int res = LOG_OK;
6399 2299 : sqlstore *store = tr->store;
6400 :
6401 2299 : if (t->access != access) {
6402 2299 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
6403 2299 : sql_table *systable = find_sql_table(tr, syss, "_tables");
6404 2299 : oid rid = store->table_api.column_find_row(tr, find_sql_column(systable, "id"),
6405 2299 : &t->base.id, NULL);
6406 2299 : sql_table *dup = NULL;
6407 :
6408 2299 : if (is_oid_nil(rid))
6409 0 : return -1;
6410 2299 : if ((res = store->table_api.column_update_value(tr, find_sql_column(systable, "access"), rid, &access)))
6411 : return res;
6412 2299 : if ((res = new_table(tr, t, &dup)))
6413 : return res;
6414 2299 : t = dup;
6415 2299 : t->access = access;
6416 2299 : if (!isNew(t) && isGlobal(t) && !isGlobalTemp(t) && (res = sql_trans_add_dependency(tr, t->base.id, dml)))
6417 : return res;
6418 2299 : if ((res = store_reset_sql_functions(tr, t->base.id))) /* reset sql functions depending on the table */
6419 : return res;
6420 : }
6421 : return res;
6422 : }
6423 :
6424 : int
6425 23 : sql_trans_alter_default(sql_trans *tr, sql_column *col, char *val)
6426 : {
6427 23 : int res = LOG_OK;
6428 23 : sqlstore *store = tr->store;
6429 :
6430 23 : if ((col->def || val) && (!col->def || !val || strcmp(col->def, val) != 0)) {
6431 19 : void *p = val ? val : (void *) ATOMnilptr(TYPE_str);
6432 23 : sql_schema *syss = find_sql_schema(tr, isGlobal(col->t)?"sys":"tmp");
6433 23 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
6434 23 : sql_column *col_ids = find_sql_column(syscolumn, "id");
6435 23 : sql_column *col_dfs = find_sql_column(syscolumn, "default");
6436 23 : oid rid = store->table_api.column_find_row(tr, col_ids, &col->base.id, NULL);
6437 23 : sql_column *dup = NULL;
6438 :
6439 23 : if (is_oid_nil(rid))
6440 0 : return -1;
6441 23 : if ((res = sys_drop_default_object(tr, col, 0)))
6442 : return res;
6443 23 : if ((res = store->table_api.column_update_value(tr, col_dfs, rid, p)))
6444 : return res;
6445 :
6446 23 : if ((res = new_column(tr, col, &dup)))
6447 : return res;
6448 23 : _DELETE(dup->def);
6449 23 : if (val)
6450 17 : dup->def =_STRDUP(val);
6451 23 : if ((res = store_reset_sql_functions(tr, col->t->base.id))) /* reset sql functions depending on the table */
6452 : return res;
6453 : }
6454 : return res;
6455 : }
6456 :
6457 : int
6458 59 : sql_trans_alter_storage(sql_trans *tr, sql_column *col, char *storage)
6459 : {
6460 59 : int res = LOG_OK;
6461 59 : sqlstore *store = tr->store;
6462 :
6463 59 : if ((col->storage_type || storage) && (!col->storage_type || !storage || strcmp(col->storage_type, storage) != 0)) {
6464 59 : void *p = storage ? storage : (void *) ATOMnilptr(TYPE_str);
6465 59 : sql_schema *syss = find_sql_schema(tr, isGlobal(col->t)?"sys":"tmp");
6466 59 : sql_table *syscolumn = find_sql_table(tr, syss, "_columns");
6467 59 : sql_column *col_ids = find_sql_column(syscolumn, "id");
6468 59 : sql_column *col_dfs = find_sql_column(syscolumn, "storage");
6469 59 : oid rid = store->table_api.column_find_row(tr, col_ids, &col->base.id, NULL);
6470 59 : sql_column *dup = NULL;
6471 :
6472 59 : if (is_oid_nil(rid))
6473 0 : return -1;
6474 59 : if ((res = store->table_api.column_update_value(tr, col_dfs, rid, p)))
6475 : return res;
6476 :
6477 59 : if ((res = new_column(tr, col, &dup)))
6478 : return res;
6479 59 : _DELETE(dup->storage_type);
6480 59 : if (storage)
6481 58 : dup->storage_type =_STRDUP(storage);
6482 59 : if (!isNew(col) && isGlobal(col->t) && !isGlobalTemp(col->t) && (res = sql_trans_add_dependency(tr, col->t->base.id, dml)))
6483 : return res;
6484 59 : if ((res = store_reset_sql_functions(tr, col->t->base.id))) /* reset sql functions depending on the table */
6485 : return res;
6486 : }
6487 : return res;
6488 : }
6489 :
6490 : int
6491 21316 : sql_trans_is_sorted( sql_trans *tr, sql_column *col )
6492 : {
6493 21316 : sqlstore *store = tr->store;
6494 21316 : if (col && isTable(col->t) && store->storage_api.sorted_col && store->storage_api.sorted_col(tr, col))
6495 : return 1;
6496 : return 0;
6497 : }
6498 :
6499 : int
6500 7781 : sql_trans_is_unique( sql_trans *tr, sql_column *col )
6501 : {
6502 7781 : sqlstore *store = tr->store;
6503 7781 : if (col && isTable(col->t) && store->storage_api.unique_col && store->storage_api.unique_col(tr, col))
6504 : return 1;
6505 : return 0;
6506 : }
6507 :
6508 : int
6509 2045 : sql_trans_is_duplicate_eliminated( sql_trans *tr, sql_column *col )
6510 : {
6511 2045 : sqlstore *store = tr->store;
6512 2045 : if (col && isTable(col->t) && store->storage_api.double_elim_col)
6513 2045 : return store->storage_api.double_elim_col(tr, col);
6514 : return 0;
6515 : }
6516 :
6517 : int
6518 4137750 : sql_trans_col_stats( sql_trans *tr, sql_column *col, bool *nonil, bool *unique, double *unique_est, ValPtr min, ValPtr max )
6519 : {
6520 4137750 : sqlstore *store = tr->store;
6521 4137750 : if (col && isTable(col->t) && store->storage_api.col_stats)
6522 3835573 : return store->storage_api.col_stats(tr, col, nonil, unique, unique_est, min, max);
6523 : return 0;
6524 : }
6525 :
6526 : size_t
6527 0 : sql_trans_dist_count( sql_trans *tr, sql_column *col )
6528 : {
6529 0 : sqlstore *store = tr->store;
6530 :
6531 0 : if (col && isTable(col->t)) {
6532 0 : if (!col->dcount)
6533 0 : col->dcount = store->storage_api.dcount_col(tr, col);
6534 0 : return col->dcount;
6535 : }
6536 : return 0;
6537 : }
6538 :
6539 : int
6540 87 : sql_trans_ranges( sql_trans *tr, sql_column *col, void **min, void **max )
6541 : {
6542 87 : sqlstore *store = tr->store;
6543 :
6544 87 : *min = NULL;
6545 87 : *max = NULL;
6546 87 : if (col && isTable(col->t)) {
6547 87 : if (!col->min || !col->max)
6548 19 : (void) store->storage_api.min_max_col(tr, col);
6549 87 : *min = col->min;
6550 87 : *max = col->max;
6551 : }
6552 87 : return *min != NULL && *max != NULL;
6553 : }
6554 :
6555 : int
6556 224 : sql_trans_create_ukey(sql_key **kres, sql_trans *tr, sql_table *t, const char *name, key_type kt, const char* check)
6557 : {
6558 : /* can only have keys between persistent tables */
6559 224 : sqlstore *store = tr->store;
6560 224 : int neg = -1, action = -1, res = LOG_OK;
6561 224 : sql_key *nk;
6562 224 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
6563 224 : sql_table *syskey = find_sql_table(tr, syss, "keys");
6564 224 : sql_ukey *uk = NULL;
6565 224 : sql_table *dup = NULL;
6566 :
6567 224 : if (isTempTable(t))
6568 : return -1; /* TODO not sure here */
6569 :
6570 224 : if ((res = new_table(tr, t, &dup)))
6571 : return res;
6572 224 : t = dup;
6573 224 : nk = (kt != fkey) ? (sql_key *) ZNEW(sql_ukey)
6574 224 : : (sql_key *) ZNEW(sql_fkey);
6575 :
6576 224 : assert(name);
6577 224 : base_init(NULL, &nk->base, next_oid(tr->store), true, name);
6578 224 : nk->type = kt;
6579 224 : nk->columns = list_create((fdestroy) &kc_destroy);
6580 224 : nk->t = t;
6581 224 : nk->idx = NULL;
6582 :
6583 224 : uk = (sql_ukey *) nk;
6584 :
6585 224 : if (nk->type == pkey)
6586 224 : t->pkey = uk;
6587 :
6588 224 : if ((res = ol_add(t->keys, &nk->base)))
6589 : return res;
6590 224 : if ((res = os_add(t->s->keys, tr, nk->base.name, dup_base(&nk->base))) ||
6591 224 : (isGlobal(t) && (res = os_add(tr->cat->objects, tr, nk->base.name, dup_base(&nk->base)))))
6592 0 : return res;
6593 :
6594 224 : const char *strnil = (const char*)ATOMnilptr(TYPE_str);
6595 448 : if ((res = store->table_api.table_insert(tr, syskey, &nk->base.id, &t->base.id, &nk->type, &nk->base.name, (nk->type == fkey) ? &((sql_fkey *) nk)->rkey : &neg, &action, (check) ? &check : &strnil)))
6596 : return res;
6597 224 : *kres = nk;
6598 224 : return res;
6599 : }
6600 :
6601 : int
6602 0 : sql_trans_create_fkey(sql_fkey **kres, sql_trans *tr, sql_table *t, const char *name, key_type kt, sql_key *rkey, int on_delete, int on_update)
6603 : {
6604 : /* can only have keys between persistent tables */
6605 0 : sqlstore *store = tr->store;
6606 0 : int neg = -1, action = (on_update<<8) + on_delete, res = LOG_OK;
6607 0 : sql_key *nk;
6608 0 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
6609 0 : sql_table *syskey = find_sql_table(tr, syss, "keys");
6610 0 : sql_fkey *fk = NULL;
6611 0 : sql_table *dup = NULL;
6612 :
6613 0 : if (isTempTable(t))
6614 : return -1; /* TODO not sure here */
6615 :
6616 0 : if ((res = new_table(tr, t, &dup)))
6617 : return res;
6618 0 : t = dup;
6619 0 : nk = (kt != fkey) ? (sql_key *) ZNEW(sql_ukey)
6620 0 : : (sql_key *) ZNEW(sql_fkey);
6621 :
6622 0 : assert(name);
6623 0 : base_init(NULL, &nk->base, next_oid(tr->store), true, name);
6624 0 : nk->type = kt;
6625 0 : nk->columns = list_create((fdestroy) &kc_destroy);
6626 0 : nk->t = t;
6627 0 : if ((res = sql_trans_create_idx(&nk->idx, tr, t, name, (nk->type == fkey) ? join_idx : hash_idx)))
6628 : return res;
6629 0 : nk->idx->key = nk;
6630 :
6631 0 : fk = (sql_fkey *) nk;
6632 :
6633 0 : fk->on_delete = on_delete;
6634 0 : fk->on_update = on_update;
6635 :
6636 0 : fk->rkey = rkey->base.id;
6637 :
6638 0 : if ((res = ol_add(t->keys, &nk->base)))
6639 : return res;
6640 0 : if ((res = os_add(t->s->keys, tr, nk->base.name, dup_base(&nk->base))) ||
6641 0 : (isGlobal(t) && (res = os_add(tr->cat->objects, tr, nk->base.name, dup_base(&nk->base)))))
6642 0 : return res;
6643 :
6644 0 : if ((res = store->table_api.table_insert(tr, syskey, &nk->base.id, &t->base.id, &nk->type, &nk->base.name, (nk->type == fkey) ? &((sql_fkey *) nk)->rkey : &neg, &action)))
6645 : return res;
6646 :
6647 0 : if ((res = sql_trans_create_dependency(tr, ((sql_fkey *) nk)->rkey, nk->base.id, FKEY_DEPENDENCY)))
6648 : return res;
6649 0 : *kres = (sql_fkey*) nk;
6650 0 : return res;
6651 : }
6652 :
6653 : int
6654 224 : sql_trans_create_kc(sql_trans *tr, sql_key *k, sql_column *c)
6655 : {
6656 224 : sqlstore *store = tr->store;
6657 224 : sql_kc *kc = ZNEW(sql_kc);
6658 224 : int nr = list_length(k->columns);
6659 224 : sql_schema *syss = find_sql_schema(tr, isGlobal(k->t)?"sys":"tmp");
6660 224 : sql_table *syskc = find_sql_table(tr, syss, "objects");
6661 224 : int res = LOG_OK;
6662 :
6663 224 : assert(c);
6664 224 : kc->c = c;
6665 224 : list_append(k->columns, kc);
6666 224 : if (k->idx && (res = sql_trans_create_ic(tr, k->idx, c)))
6667 : return res;
6668 :
6669 224 : if (k->type == pkey) {
6670 224 : if ((res = sql_trans_create_dependency(tr, c->base.id, k->base.id, KEY_DEPENDENCY)))
6671 : return res;
6672 224 : if ((res = sql_trans_alter_null(tr, c, 0))) /* should never trigger error */
6673 : return res;
6674 : }
6675 :
6676 224 : if ((res = store->table_api.table_insert(tr, syskc, &k->base.id, &kc->c->base.name, &nr, ATOMnilptr(TYPE_int))))
6677 : return res;
6678 : return res;
6679 : }
6680 :
6681 : int
6682 0 : sql_trans_create_fkc(sql_trans *tr, sql_fkey *fk, sql_column *c)
6683 : {
6684 0 : sqlstore *store = tr->store;
6685 0 : sql_key *k = (sql_key *) fk;
6686 0 : sql_kc *kc = ZNEW(sql_kc);
6687 0 : int nr = list_length(k->columns);
6688 0 : sql_schema *syss = find_sql_schema(tr, isGlobal(k->t)?"sys":"tmp");
6689 0 : sql_table *syskc = find_sql_table(tr, syss, "objects");
6690 0 : int res = LOG_OK;
6691 :
6692 0 : assert(c);
6693 0 : kc->c = c;
6694 0 : list_append(k->columns, kc);
6695 0 : if (k->idx && (res = sql_trans_create_ic(tr, k->idx, c)))
6696 : return res;
6697 :
6698 0 : if ((res = sql_trans_create_dependency(tr, c->base.id, k->base.id, FKEY_DEPENDENCY)))
6699 : return res;
6700 :
6701 0 : if ((res = store->table_api.table_insert(tr, syskc, &k->base.id, &kc->c->base.name, &nr, ATOMnilptr(TYPE_int))))
6702 : return res;
6703 : return res;
6704 : }
6705 :
6706 : static sql_idx *
6707 6138 : table_has_idx( sql_table *t, list *keycols)
6708 : {
6709 6138 : node *n, *m, *o;
6710 6138 : char *found = NULL;
6711 6138 : int len = list_length(keycols);
6712 6138 : found = NEW_ARRAY(char, len);
6713 6138 : if (!found)
6714 : return NULL;
6715 7998 : if (t->idxs) for ( n = ol_first_node(t->idxs); n; n = n->next ) {
6716 1954 : sql_idx *i = n->data;
6717 1954 : int nr;
6718 :
6719 1954 : memset(found, 0, len);
6720 4035 : for (m = keycols->h, nr = 0; m; m = m->next, nr++ ) {
6721 2081 : sql_kc *kc = m->data;
6722 :
6723 4080 : for (o = i->columns->h; o; o = o->next) {
6724 2157 : sql_kc *ikc = o->data;
6725 :
6726 2157 : if (kc->c == ikc->c) {
6727 158 : found[nr] = 1;
6728 158 : break;
6729 : }
6730 : }
6731 : }
6732 2078 : for (nr = 0; nr<len; nr++)
6733 1984 : if (!found[nr])
6734 : break;
6735 1954 : if (nr == len) {
6736 94 : _DELETE(found);
6737 94 : return i;
6738 : }
6739 : }
6740 6044 : if (found)
6741 6044 : _DELETE(found);
6742 6044 : return NULL;
6743 : }
6744 :
6745 : sql_key *
6746 6874 : key_create_done(sql_trans *tr, allocator *sa, sql_key *k)
6747 : {
6748 6874 : sql_idx *i;
6749 6874 : sqlstore *store = tr->store;
6750 :
6751 6874 : if (k->type != fkey && k->type != ckey) {
6752 5914 : if ((i = table_has_idx(k->t, k->columns)) != NULL) {
6753 : /* use available hash, or use the order */
6754 94 : if (hash_index(i->type)) {
6755 93 : k->idx = i;
6756 93 : if (!k->idx->key)
6757 0 : k->idx->key = k;
6758 : }
6759 : }
6760 :
6761 : /* we need to create an index */
6762 5914 : k->idx = create_sql_idx(store, sa, k->t, k->base.name, hash_idx);
6763 5914 : k->idx->key = k;
6764 :
6765 12428 : for (node *n=k->columns->h; n; n = n->next) {
6766 6514 : sql_kc *kc = n->data;
6767 :
6768 6514 : create_sql_ic(store, sa, k->idx, kc->c);
6769 : }
6770 : }
6771 6874 : k->idx = create_sql_idx_done(tr, k->idx);
6772 6874 : return k;
6773 : }
6774 :
6775 : int
6776 224 : sql_trans_key_done(sql_trans *tr, sql_key *k)
6777 : {
6778 224 : sql_idx *i;
6779 224 : int res = LOG_OK;
6780 :
6781 224 : if (k->type != fkey) {
6782 224 : if ((i = table_has_idx(k->t, k->columns)) != NULL) {
6783 : /* use available hash, or use the order */
6784 0 : if (hash_index(i->type)) {
6785 0 : k->idx = i;
6786 0 : if (!k->idx->key)
6787 0 : k->idx->key = k;
6788 : }
6789 0 : return res;
6790 : }
6791 :
6792 : /* we need to create an index */
6793 224 : if ((res = sql_trans_create_idx(&k->idx, tr, k->t, k->base.name, hash_idx)))
6794 : return res;
6795 224 : k->idx->key = k;
6796 :
6797 448 : for (node *n=k->columns->h; n; n = n->next) {
6798 224 : sql_kc *kc = n->data;
6799 :
6800 224 : if ((res = sql_trans_create_ic(tr, k->idx, kc->c)))
6801 0 : return res;
6802 : }
6803 : }
6804 224 : k->idx = create_sql_idx_done(tr, k->idx);
6805 224 : return res;
6806 : }
6807 :
6808 : int
6809 423 : sql_trans_drop_key(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
6810 : {
6811 423 : sqlstore *store = tr->store;
6812 423 : sql_base *b = os_find_id(s->keys, tr, id);
6813 423 : sql_key *k = (sql_key*)b;
6814 423 : sql_table *t = k->t;
6815 423 : int res = LOG_OK;
6816 423 : sql_table *dup = NULL;
6817 :
6818 423 : if ((res = new_table(tr, t, &dup)))
6819 : return res;
6820 423 : t = dup;
6821 423 : k = (sql_key*)os_find_id(s->keys, tr, id); /* fetch updated key */
6822 :
6823 423 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
6824 278 : sqlid *local_id = MNEW(sqlid);
6825 278 : if (!local_id)
6826 : return -1;
6827 :
6828 278 : if (!tr->dropped) {
6829 2 : tr->dropped = list_create((fdestroy) &id_destroy);
6830 2 : if (!tr->dropped) {
6831 0 : _DELETE(local_id);
6832 0 : return -1;
6833 : }
6834 : }
6835 278 : *local_id = k->base.id;
6836 278 : list_append(tr->dropped, local_id);
6837 : }
6838 :
6839 423 : if (k->idx && (res = sql_trans_drop_idx(tr, s, k->idx->base.id, drop_action)))
6840 : return res;
6841 423 : if ((res = store_reset_sql_functions(tr, t->base.id))) /* reset sql functions depending on the table */
6842 : return res;
6843 :
6844 423 : if ((res = sys_drop_key(tr, k, drop_action)))
6845 : return res;
6846 :
6847 : /*Clean the key from the keys*/
6848 423 : node *n = ol_find_name(k->t->keys, k->base.name);
6849 423 : if (n)
6850 423 : ol_del(k->t->keys, store, n);
6851 :
6852 423 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
6853 1 : list_destroy(tr->dropped);
6854 1 : tr->dropped = NULL;
6855 : }
6856 : return res;
6857 : }
6858 :
6859 : int
6860 224 : sql_trans_create_idx(sql_idx **i, sql_trans *tr, sql_table *t, const char *name, idx_type it)
6861 : {
6862 : /* can only have idxs between persistent tables */
6863 224 : sqlstore *store = tr->store;
6864 224 : sql_idx *ni = ZNEW(sql_idx);
6865 224 : sql_schema *syss = find_sql_schema(tr, isGlobal(t)?"sys":"tmp");
6866 224 : sql_table *sysidx = find_sql_table(tr, syss, "idxs");
6867 224 : int res = LOG_OK;
6868 :
6869 224 : assert(it != oph_idx && it != no_idx && it != new_idx_types);
6870 224 : assert(name);
6871 224 : base_init(NULL, &ni->base, next_oid(tr->store), true, name);
6872 224 : ni->type = it;
6873 224 : ni->columns = list_create((fdestroy) &kc_destroy);
6874 224 : ni->t = t;
6875 224 : ni->key = NULL;
6876 :
6877 224 : if ((res = ol_add(t->idxs, &ni->base)))
6878 : return res;
6879 224 : if (isGlobal(t) && (res = os_add(t->s->idxs, tr, ni->base.name, dup_base(&ni->base))))
6880 : return res;
6881 :
6882 224 : ATOMIC_PTR_INIT(&ni->data, NULL);
6883 224 : if (!isDeclaredTable(t) && isTable(ni->t) && idx_has_column(ni->type))
6884 224 : if ((res = store->storage_api.create_idx(tr, ni))) {
6885 : ATOMIC_PTR_DESTROY(&ni->data);
6886 : return res;
6887 : }
6888 224 : if (!isDeclaredTable(t))
6889 224 : if ((res = store->table_api.table_insert(tr, sysidx, &ni->base.id, &t->base.id, &ni->type, &ni->base.name))) {
6890 : ATOMIC_PTR_DESTROY(&ni->data);
6891 : return res;
6892 : }
6893 224 : *i = ni;
6894 224 : return res;
6895 : }
6896 :
6897 : int
6898 224 : sql_trans_create_ic(sql_trans *tr, sql_idx *i, sql_column *c)
6899 : {
6900 224 : sqlstore *store = tr->store;
6901 224 : sql_kc *ic = ZNEW(sql_kc);
6902 224 : int nr = list_length(i->columns);
6903 224 : sql_schema *syss = find_sql_schema(tr, isGlobal(i->t)?"sys":"tmp");
6904 224 : sql_table *sysic = find_sql_table(tr, syss, "objects");
6905 224 : int res = LOG_OK;
6906 :
6907 224 : assert(c);
6908 224 : ic->c = c;
6909 224 : list_append(i->columns, ic);
6910 :
6911 224 : if ((res = store->table_api.table_insert(tr, sysic, &i->base.id, &ic->c->base.name, &nr, ATOMnilptr(TYPE_int))))
6912 : return res;
6913 : return res;
6914 : }
6915 :
6916 : int
6917 586 : sql_trans_drop_idx(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
6918 : {
6919 586 : sqlstore *store = tr->store;
6920 586 : sql_idx *i = schema_find_idx_id(tr, s, id);
6921 586 : int res = LOG_OK;
6922 :
6923 586 : if (!i) /* already dropped */
6924 : return res;
6925 :
6926 582 : sql_table *t = NULL;
6927 582 : if ((res = new_table(tr, i->t, &t)))
6928 : return res;
6929 582 : i = schema_find_idx_id(tr, s, id); /* fetch updated idx */
6930 :
6931 582 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
6932 282 : sqlid *local_id = MNEW(sqlid);
6933 282 : if (!local_id)
6934 : return -1;
6935 :
6936 282 : if (!tr->dropped) {
6937 0 : tr->dropped = list_create((fdestroy) &id_destroy);
6938 0 : if (!tr->dropped) {
6939 0 : _DELETE(local_id);
6940 0 : return -1;
6941 : }
6942 : }
6943 282 : *local_id = i->base.id;
6944 282 : list_append(tr->dropped, local_id);
6945 : }
6946 :
6947 582 : if (!isNew(i) && (res = sql_trans_add_dependency_change(tr, i->t->base.id, ddl)))
6948 : return res;
6949 582 : if (!isNew(i) && isGlobal(i->t) && !isGlobalTemp(i->t) && (res = sql_trans_add_dependency(tr, i->t->base.id, dml)))
6950 : return res;
6951 582 : if ((res = sys_drop_idx(tr, i, drop_action)))
6952 : return res;
6953 582 : if ((res = store_reset_sql_functions(tr, i->t->base.id))) /* reset sql functions depending on the table */
6954 : return res;
6955 :
6956 582 : node *n = ol_find_name(i->t->idxs, i->base.name);
6957 582 : if (n)
6958 582 : ol_del(i->t->idxs, store, n);
6959 :
6960 582 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
6961 1 : list_destroy(tr->dropped);
6962 1 : tr->dropped = NULL;
6963 : }
6964 : return res;
6965 : }
6966 :
6967 :
6968 : static int
6969 338 : sql_trans_create_table_trigger(sql_trigger **tres, sql_trans *tr, sql_table *t, const char *name,
6970 : sht time, sht orientation, sht event, const char *old_name, const char *new_name,
6971 : const char *condition, const char *statement )
6972 : {
6973 338 : sqlstore *store = tr->store;
6974 338 : sql_schema *syss = (t != NULL) ? find_sql_schema(tr, isGlobal(t) ? "sys":"tmp") : find_sql_schema(tr, "sys");
6975 338 : sql_table *systrigger = find_sql_table(tr, syss, "triggers");
6976 338 : char *strnil = (char*)ATOMnilptr(TYPE_str);
6977 338 : sql_table *dup = NULL;
6978 338 : int res = LOG_OK;
6979 :
6980 338 : assert(name);
6981 :
6982 338 : if ( t && (res = new_table(tr, t, &dup)))
6983 : return res;
6984 338 : t = dup;
6985 338 : sql_trigger *nt = ZNEW(sql_trigger);
6986 338 : base_init(NULL, &nt->base, next_oid(tr->store), true, name);
6987 338 : nt->columns = list_create((fdestroy) &kc_destroy);
6988 338 : nt->t = t;
6989 338 : nt->time = time;
6990 338 : nt->orientation = orientation;
6991 338 : nt->event = event;
6992 338 : nt->old_name = nt->new_name = nt->condition = NULL;
6993 338 : if (old_name)
6994 27 : nt->old_name =_STRDUP(old_name);
6995 338 : if (new_name)
6996 40 : nt->new_name =_STRDUP(new_name);
6997 338 : if (condition)
6998 0 : nt->condition =_STRDUP(condition);
6999 338 : nt->statement =_STRDUP(statement);
7000 338 : if(t) {
7001 338 : assert(isGlobal(t));
7002 338 : if ((res = ol_add(t->triggers, &nt->base)) ||
7003 338 : (res = os_add(t->s->triggers, tr, nt->base.name, dup_base(&nt->base)))) {
7004 0 : return res;
7005 : }
7006 : }
7007 338 : sqlid tid = t? t->base.id : int_nil;
7008 :
7009 378 : if ((res = store->table_api.table_insert(tr, systrigger, &nt->base.id, &nt->base.name, &tid, &nt->time, &nt->orientation,
7010 378 : &nt->event, (nt->old_name)?&nt->old_name:&strnil, (nt->new_name)?&nt->new_name:&strnil,
7011 338 : (nt->condition)?&nt->condition:&strnil, &nt->statement))) {
7012 : return res;
7013 : }
7014 338 : *tres = nt;
7015 338 : return res;
7016 : }
7017 :
7018 : int
7019 342 : sql_trans_create_trigger(sql_trigger **tres, sql_trans *tr, sql_table *t, const char *name,
7020 : sht time, sht orientation, sht event, const char *old_name, const char *new_name,
7021 : const char *condition, const char *statement )
7022 : {
7023 342 : if (t)
7024 338 : return sql_trans_create_table_trigger(
7025 : tres, tr, t, name, time,
7026 : orientation, event, old_name,
7027 : new_name, condition, statement);
7028 :
7029 : // triggers not bound to objects (e.g. table)
7030 : // are added to sys->triggers
7031 :
7032 4 : sqlstore *store = tr->store;
7033 4 : sql_schema *syss = find_sql_schema(tr, "sys");
7034 4 : sql_table *systrigger = find_sql_table(tr, syss, "triggers");
7035 4 : char *strnil = (char*)ATOMnilptr(TYPE_str);
7036 4 : int res = LOG_OK;
7037 :
7038 4 : assert(name);
7039 :
7040 4 : sql_trigger *nt = ZNEW(sql_trigger);
7041 4 : base_init(NULL, &nt->base, next_oid(tr->store), true, name);
7042 4 : nt->time = time;
7043 4 : nt->orientation = orientation;
7044 4 : nt->event = event;
7045 4 : nt->old_name = nt->new_name = nt->condition = NULL;
7046 4 : if (old_name)
7047 0 : nt->old_name =_STRDUP(old_name);
7048 4 : if (new_name)
7049 0 : nt->new_name =_STRDUP(new_name);
7050 4 : if (condition)
7051 0 : nt->condition =_STRDUP(condition);
7052 4 : nt->statement =_STRDUP(statement);
7053 4 : if ((res = os_add(syss->triggers, tr, nt->base.name, &nt->base))) {
7054 0 : trigger_destroy(store, nt);
7055 0 : return res;
7056 : }
7057 4 : sqlid tid = int_nil;
7058 :
7059 4 : if ((res = store->table_api.table_insert(tr, systrigger, &nt->base.id, &nt->base.name, &tid, &nt->time, &nt->orientation,
7060 4 : &nt->event, (nt->old_name)?&nt->old_name:&strnil, (nt->new_name)?&nt->new_name:&strnil,
7061 4 : (nt->condition)?&nt->condition:&strnil, &nt->statement))) {
7062 0 : trigger_destroy(store, nt);
7063 0 : return res;
7064 : }
7065 :
7066 4 : *tres = nt;
7067 4 : return res;
7068 : }
7069 :
7070 : int
7071 91 : sql_trans_drop_trigger(sql_trans *tr, sql_schema *s, sqlid id, int drop_action)
7072 : {
7073 91 : sqlstore *store = tr->store;
7074 91 : sql_base *b = os_find_id(s->triggers, tr, id);
7075 91 : int res = LOG_OK;
7076 :
7077 91 : if (!b) /* already dropped */
7078 : return res;
7079 :
7080 91 : sql_trigger *i = (sql_trigger*)b;
7081 91 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
7082 2 : sqlid *local_id = MNEW(sqlid);
7083 2 : if (!local_id)
7084 : return -1;
7085 :
7086 2 : if (! tr->dropped) {
7087 0 : tr->dropped = list_create((fdestroy) &id_destroy);
7088 0 : if (!tr->dropped) {
7089 0 : _DELETE(local_id);
7090 0 : return -1;
7091 : }
7092 : }
7093 2 : *local_id = i->base.id;
7094 2 : list_append(tr->dropped, local_id);
7095 : }
7096 :
7097 91 : if (i->t) {
7098 88 : if ((res = store_reset_sql_functions(tr, i->t->base.id))) /* reset sql functions depending on the table */
7099 : return res;
7100 88 : node *n = ol_find_name(i->t->triggers, i->base.name);
7101 88 : if (n)
7102 85 : ol_del(i->t->triggers, store, n);
7103 : }
7104 91 : if ((res = sys_drop_trigger(tr, i)))
7105 : return res;
7106 :
7107 88 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
7108 0 : list_destroy(tr->dropped);
7109 0 : tr->dropped = NULL;
7110 : }
7111 : return res;
7112 : }
7113 :
7114 : static sql_sequence *
7115 671 : create_sql_sequence_with_id(allocator *sa, sqlid id, sql_schema *s, const char *name, lng start, lng min, lng max, lng inc,
7116 : lng cacheinc, bit cycle)
7117 : {
7118 671 : sql_sequence *seq = SA_ZNEW(sa, sql_sequence);
7119 :
7120 671 : assert(name);
7121 671 : base_init(sa, &seq->base, id, true, name);
7122 671 : seq->start = start;
7123 671 : seq->minvalue = min;
7124 671 : seq->maxvalue = max;
7125 671 : seq->increment = inc;
7126 671 : seq->cacheinc = cacheinc;
7127 671 : seq->cycle = cycle;
7128 671 : seq->s = s;
7129 671 : return seq;
7130 : }
7131 :
7132 : sql_sequence *
7133 359 : create_sql_sequence(sqlstore *store, allocator *sa, sql_schema *s, const char *name, lng start, lng min, lng max, lng inc,
7134 : lng cacheinc, bit cycle)
7135 : {
7136 359 : return create_sql_sequence_with_id(sa, next_oid(store), s, name, start, min, max, inc, cacheinc, cycle);
7137 : }
7138 :
7139 : int
7140 312 : sql_trans_create_sequence(sql_trans *tr, sql_schema *s, const char *name, lng start, lng min, lng max, lng inc,
7141 : lng cacheinc, bit cycle, bit bedropped)
7142 : {
7143 312 : sqlstore *store = tr->store;
7144 312 : sql_schema *syss = find_sql_schema(tr, "sys");
7145 312 : sql_table *sysseqs = find_sql_table(tr, syss, "sequences");
7146 312 : sql_sequence *seq = create_sql_sequence_with_id(NULL, next_oid(tr->store), s, name, start, min, max, inc, cacheinc, cycle);
7147 312 : int res = LOG_OK;
7148 :
7149 312 : if ((res = os_add(s->seqs, tr, seq->base.name, &seq->base)))
7150 : return res;
7151 312 : if ((res = store->table_api.table_insert(tr, sysseqs, &seq->base.id, &s->base.id, &seq->base.name, &seq->start, &seq->minvalue,
7152 : &seq->maxvalue, &seq->increment, &seq->cacheinc, &seq->cycle)))
7153 : return res;
7154 :
7155 : /*Create a BEDROPPED dependency for a SERIAL COLUMN*/
7156 312 : if (bedropped) {
7157 194 : if ((res = sql_trans_create_dependency(tr, seq->base.id, seq->base.id, BEDROPPED_DEPENDENCY)))
7158 : return res;
7159 194 : if (!isNew(seq) && (res = sql_trans_add_dependency(tr, seq->base.id, ddl)))
7160 : return res;
7161 : }
7162 : return res;
7163 : }
7164 :
7165 : int
7166 29 : sql_trans_drop_sequence(sql_trans *tr, sql_schema *s, sql_sequence *seq, int drop_action)
7167 : {
7168 29 : int res = LOG_OK;
7169 :
7170 29 : if (drop_action == DROP_CASCADE_START || drop_action == DROP_CASCADE) {
7171 0 : sqlid* local_id = MNEW(sqlid);
7172 0 : if (!local_id)
7173 : return -1;
7174 :
7175 0 : if (!tr->dropped) {
7176 0 : tr->dropped = list_create((fdestroy) &id_destroy);
7177 0 : if (!tr->dropped) {
7178 0 : _DELETE(local_id);
7179 0 : return -1;
7180 : }
7181 : }
7182 0 : *local_id = seq->base.id;
7183 0 : list_append(tr->dropped, local_id);
7184 : }
7185 :
7186 29 : if ((res = sys_drop_sequence(tr, seq, drop_action)))
7187 : return res;
7188 29 : if ((res = os_del(s->seqs, tr, seq->base.name, dup_base(&seq->base))))
7189 : return res;
7190 :
7191 29 : if (drop_action == DROP_CASCADE_START && tr->dropped) {
7192 0 : list_destroy(tr->dropped);
7193 0 : tr->dropped = NULL;
7194 : }
7195 : return res;
7196 : }
7197 :
7198 : int
7199 43 : sql_trans_alter_sequence(sql_trans *tr, sql_sequence *seq, lng min, lng max, lng inc, lng cache, bit cycle)
7200 : {
7201 43 : sqlstore *store = tr->store;
7202 43 : sql_schema *syss = find_sql_schema(tr, "sys");
7203 43 : sql_table *seqs = find_sql_table(tr, syss, "sequences");
7204 43 : oid rid = store->table_api.column_find_row(tr, find_sql_column(seqs, "id"), &seq->base.id, NULL);
7205 43 : sql_column *c;
7206 43 : int res = LOG_OK;
7207 :
7208 43 : if (is_oid_nil(rid))
7209 : return -1;
7210 43 : if (!is_lng_nil(min) && seq->minvalue != min) {
7211 33 : seq->minvalue = min;
7212 33 : c = find_sql_column(seqs, "minvalue");
7213 33 : if ((res = store->table_api.column_update_value(tr, c, rid, &seq->minvalue)))
7214 : return res;
7215 : }
7216 43 : if (!is_lng_nil(max) && seq->maxvalue != max) {
7217 7 : seq->maxvalue = max;
7218 7 : c = find_sql_column(seqs, "maxvalue");
7219 7 : if ((res = store->table_api.column_update_value(tr, c, rid, &seq->maxvalue)))
7220 : return res;
7221 : }
7222 43 : if (!is_lng_nil(inc) && seq->increment != inc) {
7223 7 : seq->increment = inc;
7224 7 : c = find_sql_column(seqs, "increment");
7225 7 : if ((res = store->table_api.column_update_value(tr, c, rid, &seq->increment)))
7226 : return res;
7227 : }
7228 43 : if (!is_lng_nil(cache) && seq->cacheinc != cache) {
7229 0 : seq->cacheinc = cache;
7230 0 : c = find_sql_column(seqs, "cacheinc");
7231 0 : if ((res = store->table_api.column_update_value(tr, c, rid, &seq->cacheinc)))
7232 : return res;
7233 : }
7234 43 : if (!is_bit_nil(cycle) && seq->cycle != cycle) {
7235 7 : seq->cycle = cycle != 0;
7236 7 : c = find_sql_column(seqs, "cycle");
7237 7 : if ((res = store->table_api.column_update_value(tr, c, rid, &seq->cycle)))
7238 : return res;
7239 : }
7240 : return res;
7241 : }
7242 :
7243 : int
7244 45 : sql_trans_sequence_restart(sql_trans *tr, sql_sequence *seq, lng start)
7245 : {
7246 45 : int res = LOG_OK;
7247 45 : sqlstore *store = tr->store;
7248 :
7249 45 : if (!is_lng_nil(start) && seq->start != start) { /* new valid value, change */
7250 16 : sql_schema *syss = find_sql_schema(tr, "sys");
7251 16 : sql_table *seqs = find_sql_table(tr, syss, "sequences");
7252 16 : oid rid = store->table_api.column_find_row(tr, find_sql_column(seqs, "id"), &seq->base.id, NULL);
7253 16 : sql_column *c = find_sql_column(seqs, "start");
7254 :
7255 16 : assert(!is_oid_nil(rid));
7256 16 : seq->start = start;
7257 16 : if ((res = store->table_api.column_update_value(tr, c, rid, &start)))
7258 : return res;
7259 : }
7260 45 : return seq_restart(tr->store, seq, start) ? 0 : -4;
7261 : }
7262 :
7263 : sql_session *
7264 38840 : sql_session_create(sqlstore *store, allocator *sa, int ac)
7265 : {
7266 38840 : sql_session *s;
7267 :
7268 38840 : if (store->singleuser > 1) {
7269 0 : TRC_ERROR(SQL_STORE, "No second connection allowed in singleuser mode\n");
7270 0 : return NULL;
7271 : }
7272 :
7273 38840 : s = ZNEW(sql_session);
7274 38840 : if (!s)
7275 : return NULL;
7276 38840 : s->sa = sa;
7277 38840 : assert(sa);
7278 38840 : s->tr = sql_trans_create_(store, NULL, NULL);
7279 38840 : if (!s->tr) {
7280 0 : _DELETE(s);
7281 0 : return NULL;
7282 : }
7283 38840 : s->tr->active = 0;
7284 38840 : if (!sql_session_reset(s, ac)) {
7285 0 : sql_trans_destroy(s->tr);
7286 0 : _DELETE(s);
7287 0 : return NULL;
7288 : }
7289 38840 : if (store->singleuser)
7290 0 : store->singleuser = 2;
7291 : return s;
7292 : }
7293 :
7294 : void
7295 38840 : sql_session_destroy(sql_session *s)
7296 : {
7297 38840 : if (s->tr) {
7298 38840 : sqlstore *store = s->tr->store;
7299 38840 : if (store->singleuser)
7300 0 : store->singleuser = 1;
7301 : }
7302 : // TODO check if s->tr is not always there
7303 38840 : assert(!s->tr || s->tr->active == 0);
7304 38840 : if (s->tr)
7305 38840 : sql_trans_destroy(s->tr);
7306 38840 : _DELETE(s);
7307 38840 : }
7308 :
7309 : int
7310 38840 : sql_session_reset(sql_session *s, int ac)
7311 : {
7312 38840 : if (!s->tr)
7313 : return 0;
7314 :
7315 38840 : assert(s->tr && s->tr->active == 0);
7316 38840 : s->schema_name = s->def_schema_name;
7317 38840 : s->schema = NULL;
7318 38840 : s->auto_commit = s->ac_on_commit = ac;
7319 38840 : s->level = tr_serializable;
7320 38840 : return 1;
7321 : }
7322 :
7323 : int
7324 219892 : sql_trans_begin(sql_session *s)
7325 : {
7326 219892 : sql_trans *tr = s->tr;
7327 219892 : sqlstore *store = tr->store;
7328 :
7329 219892 : store_lock(store);
7330 219993 : TRC_DEBUG(SQL_STORE, "Enter sql_trans_begin for transaction: " ULLFMT "\n", tr->tid);
7331 219993 : tr->ts = store_timestamp(store);
7332 219993 : if (s->schema_name && !(s->schema = find_sql_schema(tr, s->schema_name)))
7333 3 : s->schema_name = s->def_schema_name;
7334 219993 : if (!s->schema_name)
7335 38839 : s->schema_name = "sys";
7336 219993 : if (s->schema_name && !(s->schema = find_sql_schema(tr, s->schema_name))) {
7337 2 : TRC_DEBUG(SQL_STORE, "Exit sql_trans_begin for transaction: " ULLFMT " with error, the schema %s was not found\n", tr->tid, s->schema_name);
7338 2 : store_unlock(store);
7339 2 : return -3;
7340 : }
7341 219991 : tr->active = 1;
7342 :
7343 439982 : int res = ATOMIC_GET(&s->schema_version) ?
7344 219991 : ATOMIC_GET(&s->schema_version) != ATOMIC_GET(&tr->cat->schema_version) : 0;
7345 219991 : ATOMIC_SET(&s->schema_version, tr->cat->schema_version);
7346 :
7347 219991 : ATOMIC_INC(&store->nr_active);
7348 219991 : list_append(store->active, tr);
7349 :
7350 219991 : TRC_DEBUG(SQL_STORE, "Exit sql_trans_begin for transaction: " ULLFMT "\n", tr->tid);
7351 219991 : store_unlock(store);
7352 219989 : s->status = tr->status = 0;
7353 219989 : return res;
7354 : }
7355 :
7356 : int
7357 219864 : sql_trans_end(sql_session *s, int ok)
7358 : {
7359 219864 : int res = SQL_OK;
7360 219864 : TRC_DEBUG(SQL_STORE, "End of transaction: " ULLFMT "\n", s->tr->tid);
7361 219864 : if (ok == SQL_OK) {
7362 157636 : assert(!s->status && !s->tr->status);
7363 157636 : res = sql_trans_commit(s->tr);
7364 : }
7365 219933 : if (ok == SQL_ERR || res != SQL_OK) /* if a conflict happened, it was already rollbacked */
7366 63212 : sql_trans_rollback(s->tr, false);
7367 219935 : assert(s->tr->active);
7368 219935 : sqlstore *store = s->tr->store;
7369 219935 : store_lock(store);
7370 219991 : s->tr->active = 0;
7371 219991 : s->tr->status = 0;
7372 219991 : s->auto_commit = s->ac_on_commit;
7373 219991 : s->schema = NULL;
7374 219991 : list_remove_data(store->active, NULL, s->tr);
7375 219991 : ATOMIC_SET(&store->lastactive, GDKusec());
7376 219991 : ATOMIC_DEC(&store->nr_active);
7377 219991 : ulng oldest = store_get_timestamp(store);
7378 219991 : if (store->active && store->active->h) {
7379 1558877 : for(node *n = store->active->h; n; n = n->next) {
7380 1419580 : sql_trans *tr = n->data;
7381 1419580 : if (tr->ts < oldest)
7382 : oldest = tr->ts;
7383 : }
7384 : }
7385 219991 : ATOMIC_SET(&store->oldest, oldest);
7386 219991 : assert(list_length(store->active) == (int) ATOMIC_GET(&store->nr_active));
7387 219991 : store_unlock(store);
7388 219991 : return res;
7389 : }
7390 :
7391 : void
7392 1029 : find_partition_type(sql_subtype *tpe, sql_table *mt)
7393 : {
7394 1029 : if (isPartitionedByColumnTable(mt)) {
7395 913 : *tpe = mt->part.pcol->type;
7396 116 : } else if (isPartitionedByExpressionTable(mt)) {
7397 116 : *tpe = mt->part.pexp->type;
7398 : } else {
7399 0 : assert(0);
7400 : }
7401 1029 : }
7402 :
7403 : static int
7404 45 : convert_part_values(sql_trans *tr, sql_table *mt )
7405 : {
7406 45 : sql_subtype found = { 0 };
7407 45 : int localtype;
7408 45 : find_partition_type(&found, mt);
7409 45 : localtype = found.type->localtype;
7410 :
7411 45 : if (localtype != TYPE_str && mt->members && list_length(mt->members)) {
7412 144 : for (node *n = mt->members->h; n; n = n->next) {
7413 99 : sql_part *p = n->data;
7414 :
7415 99 : if (isListPartitionTable(mt)) {
7416 129 : for (node *m = p->part.values->h; m; m = m->next) {
7417 96 : sql_part_value *v = (sql_part_value*) m->data, ov = *v;
7418 96 : ValRecord vvalue;
7419 96 : ptr ok;
7420 :
7421 96 : vvalue = (ValRecord) {.vtype = TYPE_void,};
7422 96 : ok = VALinit(&vvalue, TYPE_str, v->value);
7423 96 : if (ok)
7424 96 : ok = VALconvert(localtype, &vvalue);
7425 96 : if (ok) {
7426 96 : ok = v->value = NEW_ARRAY(char, vvalue.len);
7427 96 : if (ok) {
7428 96 : memcpy(v->value, VALget(&vvalue), vvalue.len);
7429 96 : v->length = vvalue.len;
7430 : }
7431 : }
7432 96 : VALclear(&vvalue);
7433 96 : if (!ok)
7434 0 : return -1;
7435 96 : _DELETE(ov.value);
7436 : }
7437 66 : } else if (isRangePartitionTable(mt)) {
7438 66 : ValRecord vmin, vmax;
7439 66 : ptr ok;
7440 :
7441 66 : vmin = vmax = (ValRecord) {.vtype = TYPE_void,};
7442 66 : ok = VALinit(&vmin, TYPE_str, p->part.range.minvalue);
7443 66 : if (ok)
7444 66 : ok = VALinit(&vmax, TYPE_str, p->part.range.maxvalue);
7445 66 : _DELETE(p->part.range.minvalue);
7446 66 : _DELETE(p->part.range.maxvalue);
7447 66 : if (ok) {
7448 66 : if (strNil((const char *)VALget(&vmin)) &&
7449 42 : strNil((const char *)VALget(&vmax))) {
7450 32 : const void *nil_ptr = ATOMnilptr(localtype);
7451 32 : size_t nil_len = ATOMlen(localtype, nil_ptr);
7452 :
7453 32 : p->part.range.minvalue = NEW_ARRAY(char, nil_len);
7454 32 : p->part.range.maxvalue = NEW_ARRAY(char, nil_len);
7455 32 : if (p->part.range.minvalue == NULL ||
7456 : p->part.range.maxvalue == NULL) {
7457 0 : ok = NULL;
7458 0 : _DELETE(p->part.range.minvalue);
7459 0 : _DELETE(p->part.range.maxvalue);
7460 : } else {
7461 32 : memcpy(p->part.range.minvalue, nil_ptr, nil_len);
7462 32 : memcpy(p->part.range.maxvalue, nil_ptr, nil_len);
7463 32 : p->part.range.minlength = nil_len;
7464 32 : p->part.range.maxlength = nil_len;
7465 : }
7466 : } else {
7467 34 : ok = VALconvert(localtype, &vmin);
7468 34 : if (ok)
7469 34 : ok = VALconvert(localtype, &vmax);
7470 34 : if (ok) {
7471 34 : p->part.range.minvalue = NEW_ARRAY(char, vmin.len);
7472 34 : p->part.range.maxvalue = NEW_ARRAY(char, vmax.len);
7473 34 : if (p->part.range.minvalue == NULL ||
7474 : p->part.range.maxvalue == NULL) {
7475 0 : ok = NULL;
7476 0 : _DELETE(p->part.range.minvalue);
7477 0 : _DELETE(p->part.range.maxvalue);
7478 : } else {
7479 34 : memcpy(p->part.range.minvalue, VALget(&vmin), vmin.len);
7480 34 : memcpy(p->part.range.maxvalue, VALget(&vmax), vmax.len);
7481 34 : p->part.range.minlength = vmin.len;
7482 34 : p->part.range.maxlength = vmax.len;
7483 : }
7484 : }
7485 : }
7486 66 : if (ok && isPartitionedByColumnTable(p->t))
7487 64 : col_set_range(tr, p, true);
7488 : }
7489 66 : VALclear(&vmin);
7490 66 : VALclear(&vmax);
7491 66 : if (!ok)
7492 0 : return -1;
7493 : }
7494 : }
7495 : }
7496 : return 0;
7497 : }
7498 :
7499 : int
7500 329 : sql_trans_convert_partitions(sql_trans *tr)
7501 : {
7502 329 : struct os_iter si;
7503 329 : os_iterator(&si, tr->cat->schemas, tr, NULL);
7504 1769 : for(sql_base *b = oi_next(&si); b; b = oi_next(&si)) {
7505 1440 : sql_schema *ss = (sql_schema*)b;
7506 1440 : struct os_iter oi;
7507 1440 : os_iterator(&oi, ss->tables, tr, NULL);
7508 23399 : for(sql_base *b = oi_next(&oi); b; b = oi_next(&oi)) {
7509 21959 : sql_table *tt = (sql_table*)b;
7510 21959 : if (isPartitionedByColumnTable(tt) || isPartitionedByExpressionTable(tt)) {
7511 45 : if (convert_part_values(tr, tt) < 0)
7512 0 : return -1;
7513 : }
7514 : }
7515 : }
7516 : return 0;
7517 : }
7518 :
7519 : void
7520 116 : store_printinfo(sqlstore *store)
7521 : {
7522 116 : if (!MT_lock_trytime(&store->commit, 1000)) {
7523 0 : printf("WAL is currently locked, so no WAL information\n");
7524 0 : return;
7525 : }
7526 116 : printf("WAL:\n");
7527 116 : printf("SQL store oldest pending "ULLFMT"\n", store->oldest_pending);
7528 116 : log_printinfo(store->logger);
7529 116 : MT_lock_unset(&store->commit);
7530 : }
|