1
/******************************************************
2
SQL parser symbol table
6
Created 12/15/1997 Heikki Tuuri
7
*******************************************************/
12
#include "pars0sym.ic"
16
#include "data0type.h"
17
#include "data0data.h"
19
#include "pars0pars.h"
21
#include "eval0eval.h"
24
/**********************************************************************
25
Creates a symbol table for a single stored procedure or query. */
30
/* out, own: symbol table */
31
mem_heap_t* heap) /* in: memory heap where to create */
35
sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
37
UT_LIST_INIT(sym_tab->sym_list);
38
UT_LIST_INIT(sym_tab->func_node_list);
45
/**********************************************************************
46
Frees the memory allocated dynamically AFTER parsing phase for variables
47
etc. in the symbol table. Does not free the mem heap where the table was
48
originally created. Frees also SQL explicit cursor definitions. */
53
sym_tab_t* sym_tab) /* in, own: symbol table */
58
sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
61
eval_node_free_val_buf(sym);
63
if (sym->prefetch_buf) {
64
sel_col_prefetch_buf_free(sym->prefetch_buf);
67
if (sym->cursor_def) {
68
que_graph_free_recursive(sym->cursor_def);
71
sym = UT_LIST_GET_NEXT(sym_list, sym);
74
func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
77
eval_node_free_val_buf(func);
79
func = UT_LIST_GET_NEXT(func_node_list, func);
83
/**********************************************************************
84
Adds an integer literal to a symbol table. */
89
/* out: symbol table node */
90
sym_tab_t* sym_tab, /* in: symbol table */
91
ulint val) /* in: integer value */
96
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
98
node->common.type = QUE_NODE_SYMBOL;
100
node->resolved = TRUE;
101
node->token_type = SYM_LIT;
103
node->indirection = NULL;
105
dtype_set(&(node->common.val.type), DATA_INT, 0, 4);
107
data = mem_heap_alloc(sym_tab->heap, 4);
108
mach_write_to_4(data, val);
110
dfield_set_data(&(node->common.val), data, 4);
112
node->common.val_buf_size = 0;
113
node->prefetch_buf = NULL;
114
node->cursor_def = NULL;
116
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
118
node->sym_table = sym_tab;
123
/**********************************************************************
124
Adds a string literal to a symbol table. */
129
/* out: symbol table node */
130
sym_tab_t* sym_tab, /* in: symbol table */
131
byte* str, /* in: string with no quotes around
133
ulint len) /* in: string length */
138
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
140
node->common.type = QUE_NODE_SYMBOL;
142
node->resolved = TRUE;
143
node->token_type = SYM_LIT;
145
node->indirection = NULL;
147
dtype_set(&(node->common.val.type), DATA_VARCHAR, DATA_ENGLISH, 0);
150
data = mem_heap_alloc(sym_tab->heap, len);
151
ut_memcpy(data, str, len);
156
dfield_set_data(&(node->common.val), data, len);
158
node->common.val_buf_size = 0;
159
node->prefetch_buf = NULL;
160
node->cursor_def = NULL;
162
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
164
node->sym_table = sym_tab;
169
/**********************************************************************
170
Add a bound literal to a symbol table. */
173
sym_tab_add_bound_lit(
174
/*==================*/
175
/* out: symbol table node */
176
sym_tab_t* sym_tab, /* in: symbol table */
177
const char* name, /* in: name of bound literal */
178
ulint* lit_type) /* out: type of literal (PARS_*_LIT) */
181
pars_bound_lit_t* blit;
184
blit = pars_info_get_bound_lit(sym_tab->info, name);
187
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
189
node->common.type = QUE_NODE_SYMBOL;
191
node->resolved = TRUE;
192
node->token_type = SYM_LIT;
194
node->indirection = NULL;
196
switch (blit->type) {
199
*lit_type = PARS_FIXBINARY_LIT;
203
*lit_type = PARS_BLOB_LIT;
207
*lit_type = PARS_STR_LIT;
211
ut_a(blit->length > 0);
214
*lit_type = PARS_STR_LIT;
218
ut_a(blit->length > 0);
219
ut_a(blit->length <= 8);
222
*lit_type = PARS_INT_LIT;
229
dtype_set(&(node->common.val.type), blit->type, blit->prtype, len);
231
dfield_set_data(&(node->common.val), blit->address, blit->length);
233
node->common.val_buf_size = 0;
234
node->prefetch_buf = NULL;
235
node->cursor_def = NULL;
237
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
239
node->sym_table = sym_tab;
244
/**********************************************************************
245
Adds an SQL null literal to a symbol table. */
248
sym_tab_add_null_lit(
249
/*=================*/
250
/* out: symbol table node */
251
sym_tab_t* sym_tab) /* in: symbol table */
255
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
257
node->common.type = QUE_NODE_SYMBOL;
259
node->resolved = TRUE;
260
node->token_type = SYM_LIT;
262
node->indirection = NULL;
264
node->common.val.type.mtype = DATA_ERROR;
266
dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
268
node->common.val_buf_size = 0;
269
node->prefetch_buf = NULL;
270
node->cursor_def = NULL;
272
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
274
node->sym_table = sym_tab;
279
/**********************************************************************
280
Adds an identifier to a symbol table. */
285
/* out: symbol table node */
286
sym_tab_t* sym_tab, /* in: symbol table */
287
byte* name, /* in: identifier name */
288
ulint len) /* in: identifier length */
292
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
294
node->common.type = QUE_NODE_SYMBOL;
296
node->resolved = FALSE;
297
node->indirection = NULL;
299
node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
300
node->name_len = len;
302
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
304
dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
306
node->common.val_buf_size = 0;
307
node->prefetch_buf = NULL;
308
node->cursor_def = NULL;
310
node->sym_table = sym_tab;
315
/**********************************************************************
316
Add a bound identifier to a symbol table. */
319
sym_tab_add_bound_id(
321
/* out: symbol table node */
322
sym_tab_t* sym_tab, /* in: symbol table */
323
const char* name) /* in: name of bound id */
326
pars_bound_id_t* bid;
328
bid = pars_info_get_bound_id(sym_tab->info, name);
331
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
333
node->common.type = QUE_NODE_SYMBOL;
335
node->resolved = FALSE;
336
node->indirection = NULL;
338
node->name = mem_heap_strdup(sym_tab->heap, bid->id);
339
node->name_len = strlen(node->name);
341
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
343
dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
345
node->common.val_buf_size = 0;
346
node->prefetch_buf = NULL;
347
node->cursor_def = NULL;
349
node->sym_table = sym_tab;