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_parser.h"
15 : #include "sql_symbol.h"
16 : #include "rel_semantic.h"
17 : #include "rel_select.h"
18 : #include "rel_updates.h"
19 : #include "rel_trans.h"
20 : #include "rel_schema.h"
21 : #include "rel_psm.h"
22 : #include "rel_sequence.h"
23 : #include "rel_exp.h"
24 : #include "sql_privileges.h"
25 :
26 : #include <unistd.h>
27 : #include <string.h>
28 : #include <ctype.h>
29 :
30 : sql_rel *
31 187519 : rel_parse(mvc *m, sql_schema *s, const char *query, char emode)
32 : {
33 187519 : sql_rel *rel = NULL;
34 187519 : buffer *b;
35 187519 : bstream *bs;
36 187519 : stream *buf;
37 187519 : char *n;
38 187519 : size_t len = _strlen(query);
39 187519 : sql_schema *c = cur_schema(m);
40 187519 : sql_query *qc = NULL;
41 :
42 187519 : if ((b = malloc(sizeof(buffer))) == NULL)
43 : return NULL;
44 187519 : if ((n = malloc(len + 1 + 1)) == NULL) {
45 0 : free(b);
46 0 : return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
47 : }
48 187519 : snprintf(n, len + 2, "%s\n", query);
49 187519 : len++;
50 187519 : buffer_init(b, n, len);
51 187519 : buf = buffer_rastream(b, "sqlstatement");
52 187519 : if(buf == NULL) {
53 0 : buffer_destroy(b);
54 0 : return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
55 : }
56 187519 : bs = bstream_create(buf, b->len);
57 187519 : if(bs == NULL) {
58 0 : buffer_destroy(b);
59 0 : return sql_error(m, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
60 : }
61 187519 : mvc o = *m;
62 187519 : scanner_init( &m->scanner, bs, NULL);
63 187519 : m->scanner.mode = LINE_1;
64 187519 : bstream_next(m->scanner.rs);
65 :
66 187519 : m->qc = NULL;
67 187519 : m->emode = emode;
68 187519 : if (s)
69 187519 : m->session->schema = s;
70 187519 : m->params = NULL;
71 187519 : m->sym = NULL;
72 187519 : m->errstr[0] = '\0';
73 187519 : m->session->status = 0;
74 : /* via views we give access to protected objects */
75 187519 : assert(emode == m_instantiate || emode == m_deps || emode == m_prepare);
76 187519 : m->user_id = USER_MONETDB;
77 :
78 187519 : (void) sqlparse(m); /* blindly ignore errors */
79 187519 : qc = query_create(m);
80 187519 : rel = rel_semantic(qc, m->sym);
81 :
82 187519 : buffer_destroy(b);
83 187519 : bstream_destroy(m->scanner.rs);
84 :
85 187519 : m->sym = NULL;
86 187519 : o.frames = m->frames; /* may have been realloc'ed */
87 187519 : o.sizeframes = m->sizeframes;
88 187519 : if (m->session->status || m->errstr[0]) {
89 12 : int status = m->session->status;
90 :
91 12 : strcpy(o.errstr, m->errstr);
92 12 : *m = o;
93 12 : m->session->status = status;
94 : } else {
95 187507 : unsigned int label = m->label;
96 :
97 187507 : while (m->topframes > o.topframes)
98 0 : clear_frame(m, m->frames[--m->topframes]);
99 187507 : *m = o;
100 187507 : m->label = label;
101 : }
102 187519 : m->session->schema = c;
103 187519 : return rel;
104 : }
105 :
106 : sql_rel *
107 753502 : rel_semantic(sql_query *query, symbol *s)
108 : {
109 753502 : mvc *sql = query->sql;
110 753502 : if (!s)
111 : return NULL;
112 :
113 753502 : switch (s->token) {
114 :
115 3154 : case TR_COMMIT:
116 : case TR_SAVEPOINT:
117 : case TR_RELEASE:
118 : case TR_ROLLBACK:
119 : case TR_START:
120 : case TR_MODE:
121 3154 : return rel_transactions(query, s);
122 :
123 206658 : case SQL_CREATE_SCHEMA:
124 : case SQL_DROP_SCHEMA:
125 :
126 : case SQL_DECLARE_TABLE:
127 : case SQL_CREATE_TABLE:
128 : case SQL_CREATE_VIEW:
129 : case SQL_DROP_TABLE:
130 : case SQL_DROP_VIEW:
131 : case SQL_ALTER_TABLE:
132 :
133 : case SQL_COMMENT:
134 :
135 : case SQL_GRANT:
136 : case SQL_REVOKE:
137 : case SQL_GRANT_ROLES:
138 : case SQL_REVOKE_ROLES:
139 :
140 : case SQL_CREATE_ROLE:
141 : case SQL_DROP_ROLE:
142 :
143 : case SQL_CREATE_INDEX:
144 : case SQL_DROP_INDEX:
145 :
146 : case SQL_CREATE_USER:
147 : case SQL_DROP_USER:
148 : case SQL_ALTER_USER:
149 :
150 : case SQL_RENAME_COLUMN:
151 : case SQL_RENAME_SCHEMA:
152 : case SQL_RENAME_TABLE:
153 : case SQL_RENAME_USER:
154 : case SQL_SET_TABLE_SCHEMA:
155 :
156 : case SQL_CREATE_TYPE:
157 : case SQL_DROP_TYPE:
158 206658 : return rel_schemas(query, s);
159 :
160 399 : case SQL_CREATE_SEQ:
161 : case SQL_ALTER_SEQ:
162 : case SQL_DROP_SEQ:
163 399 : return rel_sequences(query, s);
164 :
165 244423 : case SQL_CREATE_FUNC:
166 : case SQL_DROP_FUNC:
167 : case SQL_DECLARE:
168 : case SQL_CALL:
169 : case SQL_SET:
170 :
171 : case SQL_CREATE_TABLE_LOADER:
172 :
173 : case SQL_CREATE_TRIGGER:
174 : case SQL_DROP_TRIGGER:
175 :
176 : case SQL_ANALYZE:
177 244423 : return rel_psm(query, s);
178 :
179 143524 : case SQL_INSERT:
180 : case SQL_UPDATE:
181 : case SQL_DELETE:
182 : case SQL_TRUNCATE:
183 : case SQL_MERGE:
184 : case SQL_COPYFROM:
185 : case SQL_COPYINTO:
186 : case SQL_BINCOPYFROM:
187 : case SQL_BINCOPYINTO:
188 : case SQL_COPYLOADER:
189 143524 : return rel_updates(query, s);
190 :
191 283 : case SQL_WITH:
192 283 : return rel_with_query(query, s);
193 :
194 182 : case SQL_MULSTMT: {
195 182 : dnode *d;
196 182 : sql_rel *r = NULL;
197 :
198 182 : if (!stack_push_frame(sql, "%MUL"))
199 0 : return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
200 557 : for (d = s->data.lval->h; d; d = d->next) {
201 378 : symbol *sym = d->data.sym;
202 378 : sql_rel *nr = rel_semantic(query, sym);
203 :
204 378 : if (!nr) {
205 3 : stack_pop_frame(sql);
206 3 : return NULL;
207 : }
208 375 : if (r)
209 193 : r = rel_list(sql->sa, r, nr);
210 : else
211 : r = nr;
212 : }
213 179 : stack_pop_frame(sql);
214 179 : return r;
215 : }
216 0 : case SQL_PREP:
217 : {
218 0 : dnode *d = s->data.lval->h;
219 0 : symbol *sym = d->data.sym;
220 0 : sql_rel *r = rel_semantic(query, sym);
221 :
222 0 : if (!r)
223 : return NULL;
224 : return r;
225 : }
226 :
227 154879 : case SQL_SELECT:
228 : case SQL_JOIN:
229 : case SQL_UNION:
230 : case SQL_EXCEPT:
231 : case SQL_INTERSECT:
232 : case SQL_VALUES:
233 154879 : return rel_selects(query, s);
234 :
235 0 : default:
236 0 : return sql_error(sql, 02, SQLSTATE(42000) "Symbol type not found");
237 : }
238 : }
|