1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#include "monetdb_config.h"
10#include "rel_trans.h"
11#include "rel_select.h"
12#include "rel_rel.h"
13#include "rel_exp.h"
14#include "sql_parser.h"
15
16static sql_rel *
17rel_trans(mvc *sql, int trans_type, int nr, char *name)
18{
19 sql_rel *rel = rel_create(sql->sa);
20 list *exps = new_exp_list(sql->sa);
21 if(!rel || !exps)
22 return NULL;
23
24 append(exps, exp_atom_int(sql->sa, nr));
25 if (name)
26 append(exps, exp_atom_clob(sql->sa, name));
27 rel->l = NULL;
28 rel->r = NULL;
29 rel->op = op_ddl;
30 rel->flag = trans_type;
31 rel->exps = exps;
32 rel->card = 0;
33 rel->nrcols = 0;
34 return rel;
35}
36
37sql_rel *
38rel_transactions(sql_query *query, symbol *s)
39{
40 mvc *sql = query->sql;
41 sql_rel *ret = NULL;
42
43 switch (s->token) {
44 case TR_RELEASE:
45 ret = rel_trans(sql, ddl_release, 0, s->data.sval);
46 break;
47 case TR_COMMIT:
48 assert(s->type == type_int);
49 ret = rel_trans(sql, ddl_commit, s->data.i_val, NULL);
50 break;
51 case TR_SAVEPOINT:
52 ret = rel_trans(sql, ddl_commit, 0, s->data.sval);
53 break;
54 case TR_ROLLBACK: {
55 dnode *n = s->data.lval->h;
56 assert(n->type == type_int);
57 ret= rel_trans(sql, ddl_rollback, n->data.i_val, n->next->data.sval);
58 } break;
59 case TR_START:
60 case TR_MODE:
61 assert(s->type == type_int);
62 ret = rel_trans(sql, ddl_trans, s->data.i_val, NULL);
63 break;
64 default:
65 return sql_error(sql, 01, SQLSTATE(42000) "Transaction unknown Symbol(%p)->token = %s", s, token2string(s->token));
66 }
67 return ret;
68}
69