1
/*****************************************************************************
3
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/******************************************************
20
SQL parser symbol table
22
Created 12/15/1997 Heikki Tuuri
23
*******************************************************/
28
#include "pars0sym.ic"
32
#include "data0type.h"
33
#include "data0data.h"
36
# include "pars0grm.h"
38
#include "pars0pars.h"
40
#include "eval0eval.h"
43
/**********************************************************************
44
Creates a symbol table for a single stored procedure or query. */
49
/* out, own: symbol table */
50
mem_heap_t* heap) /* in: memory heap where to create */
54
sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
56
UT_LIST_INIT(sym_tab->sym_list);
57
UT_LIST_INIT(sym_tab->func_node_list);
64
/**********************************************************************
65
Frees the memory allocated dynamically AFTER parsing phase for variables
66
etc. in the symbol table. Does not free the mem heap where the table was
67
originally created. Frees also SQL explicit cursor definitions. */
72
sym_tab_t* sym_tab) /* in, own: symbol table */
77
sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
80
eval_node_free_val_buf(sym);
82
if (sym->prefetch_buf) {
83
sel_col_prefetch_buf_free(sym->prefetch_buf);
86
if (sym->cursor_def) {
87
que_graph_free_recursive(sym->cursor_def);
90
sym = UT_LIST_GET_NEXT(sym_list, sym);
93
func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
96
eval_node_free_val_buf(func);
98
func = UT_LIST_GET_NEXT(func_node_list, func);
102
/**********************************************************************
103
Adds an integer literal to a symbol table. */
108
/* out: symbol table node */
109
sym_tab_t* sym_tab, /* in: symbol table */
110
ulint val) /* in: integer value */
115
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
117
node->common.type = QUE_NODE_SYMBOL;
119
node->resolved = TRUE;
120
node->token_type = SYM_LIT;
122
node->indirection = NULL;
124
dtype_set(dfield_get_type(&node->common.val), DATA_INT, 0, 4);
126
data = mem_heap_alloc(sym_tab->heap, 4);
127
mach_write_to_4(data, val);
129
dfield_set_data(&(node->common.val), data, 4);
131
node->common.val_buf_size = 0;
132
node->prefetch_buf = NULL;
133
node->cursor_def = NULL;
135
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
137
node->sym_table = sym_tab;
142
/**********************************************************************
143
Adds a string literal to a symbol table. */
148
/* out: symbol table node */
149
sym_tab_t* sym_tab, /* in: symbol table */
150
byte* str, /* in: string with no quotes around
152
ulint len) /* in: string length */
157
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
159
node->common.type = QUE_NODE_SYMBOL;
161
node->resolved = TRUE;
162
node->token_type = SYM_LIT;
164
node->indirection = NULL;
166
dtype_set(dfield_get_type(&node->common.val),
167
DATA_VARCHAR, DATA_ENGLISH, 0);
170
data = mem_heap_alloc(sym_tab->heap, len);
171
ut_memcpy(data, str, len);
176
dfield_set_data(&(node->common.val), data, len);
178
node->common.val_buf_size = 0;
179
node->prefetch_buf = NULL;
180
node->cursor_def = NULL;
182
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
184
node->sym_table = sym_tab;
189
/**********************************************************************
190
Add a bound literal to a symbol table. */
193
sym_tab_add_bound_lit(
194
/*==================*/
195
/* out: symbol table node */
196
sym_tab_t* sym_tab, /* in: symbol table */
197
const char* name, /* in: name of bound literal */
198
ulint* lit_type) /* out: type of literal (PARS_*_LIT) */
201
pars_bound_lit_t* blit;
204
blit = pars_info_get_bound_lit(sym_tab->info, name);
207
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
209
node->common.type = QUE_NODE_SYMBOL;
211
node->resolved = TRUE;
212
node->token_type = SYM_LIT;
214
node->indirection = NULL;
216
switch (blit->type) {
219
*lit_type = PARS_FIXBINARY_LIT;
223
*lit_type = PARS_BLOB_LIT;
227
*lit_type = PARS_STR_LIT;
231
ut_a(blit->length > 0);
234
*lit_type = PARS_STR_LIT;
238
ut_a(blit->length > 0);
239
ut_a(blit->length <= 8);
242
*lit_type = PARS_INT_LIT;
249
dtype_set(dfield_get_type(&node->common.val),
250
blit->type, blit->prtype, len);
252
dfield_set_data(&(node->common.val), blit->address, blit->length);
254
node->common.val_buf_size = 0;
255
node->prefetch_buf = NULL;
256
node->cursor_def = NULL;
258
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
260
node->sym_table = sym_tab;
265
/**********************************************************************
266
Adds an SQL null literal to a symbol table. */
269
sym_tab_add_null_lit(
270
/*=================*/
271
/* out: symbol table node */
272
sym_tab_t* sym_tab) /* in: symbol table */
276
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
278
node->common.type = QUE_NODE_SYMBOL;
280
node->resolved = TRUE;
281
node->token_type = SYM_LIT;
283
node->indirection = NULL;
285
dfield_get_type(&node->common.val)->mtype = DATA_ERROR;
287
dfield_set_null(&node->common.val);
289
node->common.val_buf_size = 0;
290
node->prefetch_buf = NULL;
291
node->cursor_def = NULL;
293
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
295
node->sym_table = sym_tab;
300
/**********************************************************************
301
Adds an identifier to a symbol table. */
306
/* out: symbol table node */
307
sym_tab_t* sym_tab, /* in: symbol table */
308
byte* name, /* in: identifier name */
309
ulint len) /* in: identifier length */
313
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
315
node->common.type = QUE_NODE_SYMBOL;
317
node->resolved = FALSE;
318
node->indirection = NULL;
320
node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
321
node->name_len = len;
323
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
325
dfield_set_null(&node->common.val);
327
node->common.val_buf_size = 0;
328
node->prefetch_buf = NULL;
329
node->cursor_def = NULL;
331
node->sym_table = sym_tab;
336
/**********************************************************************
337
Add a bound identifier to a symbol table. */
340
sym_tab_add_bound_id(
342
/* out: symbol table node */
343
sym_tab_t* sym_tab, /* in: symbol table */
344
const char* name) /* in: name of bound id */
347
pars_bound_id_t* bid;
349
bid = pars_info_get_bound_id(sym_tab->info, name);
352
node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
354
node->common.type = QUE_NODE_SYMBOL;
356
node->resolved = FALSE;
357
node->indirection = NULL;
359
node->name = mem_heap_strdup(sym_tab->heap, bid->id);
360
node->name_len = strlen(node->name);
362
UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
364
dfield_set_null(&node->common.val);
366
node->common.val_buf_size = 0;
367
node->prefetch_buf = NULL;
368
node->cursor_def = NULL;
370
node->sym_table = sym_tab;