mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / innodb_plugin / pars / pars0sym.c
blob785ecfa76223bc71cc1a9296731bc22ff58059d1
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.,
15 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *****************************************************************************/
19 /**************************************************//**
20 @file pars/pars0sym.c
21 SQL parser symbol table
23 Created 12/15/1997 Heikki Tuuri
24 *******************************************************/
26 #include "pars0sym.h"
28 #ifdef UNIV_NONINL
29 #include "pars0sym.ic"
30 #endif
32 #include "mem0mem.h"
33 #include "data0type.h"
34 #include "data0data.h"
35 #include "pars0grm.h"
36 #include "pars0pars.h"
37 #include "que0que.h"
38 #include "eval0eval.h"
39 #include "row0sel.h"
41 /******************************************************************//**
42 Creates a symbol table for a single stored procedure or query.
43 @return own: symbol table */
44 UNIV_INTERN
45 sym_tab_t*
46 sym_tab_create(
47 /*===========*/
48 mem_heap_t* heap) /*!< in: memory heap where to create */
50 sym_tab_t* sym_tab;
52 sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
54 UT_LIST_INIT(sym_tab->sym_list);
55 UT_LIST_INIT(sym_tab->func_node_list);
57 sym_tab->heap = heap;
59 return(sym_tab);
62 /******************************************************************//**
63 Frees the memory allocated dynamically AFTER parsing phase for variables
64 etc. in the symbol table. Does not free the mem heap where the table was
65 originally created. Frees also SQL explicit cursor definitions. */
66 UNIV_INTERN
67 void
68 sym_tab_free_private(
69 /*=================*/
70 sym_tab_t* sym_tab) /*!< in, own: symbol table */
72 sym_node_t* sym;
73 func_node_t* func;
75 sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
77 while (sym) {
78 eval_node_free_val_buf(sym);
80 if (sym->prefetch_buf) {
81 sel_col_prefetch_buf_free(sym->prefetch_buf);
84 if (sym->cursor_def) {
85 que_graph_free_recursive(sym->cursor_def);
88 sym = UT_LIST_GET_NEXT(sym_list, sym);
91 func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
93 while (func) {
94 eval_node_free_val_buf(func);
96 func = UT_LIST_GET_NEXT(func_node_list, func);
100 /******************************************************************//**
101 Adds an integer literal to a symbol table.
102 @return symbol table node */
103 UNIV_INTERN
104 sym_node_t*
105 sym_tab_add_int_lit(
106 /*================*/
107 sym_tab_t* sym_tab, /*!< in: symbol table */
108 ulint val) /*!< in: integer value */
110 sym_node_t* node;
111 byte* data;
113 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
115 node->common.type = QUE_NODE_SYMBOL;
117 node->resolved = TRUE;
118 node->token_type = SYM_LIT;
120 node->indirection = NULL;
122 dtype_set(dfield_get_type(&node->common.val), DATA_INT, 0, 4);
124 data = mem_heap_alloc(sym_tab->heap, 4);
125 mach_write_to_4(data, val);
127 dfield_set_data(&(node->common.val), data, 4);
129 node->common.val_buf_size = 0;
130 node->prefetch_buf = NULL;
131 node->cursor_def = NULL;
133 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
135 node->sym_table = sym_tab;
137 return(node);
140 /******************************************************************//**
141 Adds a string literal to a symbol table.
142 @return symbol table node */
143 UNIV_INTERN
144 sym_node_t*
145 sym_tab_add_str_lit(
146 /*================*/
147 sym_tab_t* sym_tab, /*!< in: symbol table */
148 byte* str, /*!< in: string with no quotes around
149 it */
150 ulint len) /*!< in: string length */
152 sym_node_t* node;
153 byte* data;
155 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
157 node->common.type = QUE_NODE_SYMBOL;
159 node->resolved = TRUE;
160 node->token_type = SYM_LIT;
162 node->indirection = NULL;
164 dtype_set(dfield_get_type(&node->common.val),
165 DATA_VARCHAR, DATA_ENGLISH, 0);
167 if (len) {
168 data = mem_heap_alloc(sym_tab->heap, len);
169 ut_memcpy(data, str, len);
170 } else {
171 data = NULL;
174 dfield_set_data(&(node->common.val), data, len);
176 node->common.val_buf_size = 0;
177 node->prefetch_buf = NULL;
178 node->cursor_def = NULL;
180 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
182 node->sym_table = sym_tab;
184 return(node);
187 /******************************************************************//**
188 Add a bound literal to a symbol table.
189 @return symbol table node */
190 UNIV_INTERN
191 sym_node_t*
192 sym_tab_add_bound_lit(
193 /*==================*/
194 sym_tab_t* sym_tab, /*!< in: symbol table */
195 const char* name, /*!< in: name of bound literal */
196 ulint* lit_type) /*!< out: type of literal (PARS_*_LIT) */
198 sym_node_t* node;
199 pars_bound_lit_t* blit;
200 ulint len = 0;
202 blit = pars_info_get_bound_lit(sym_tab->info, name);
203 ut_a(blit);
205 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
207 node->common.type = QUE_NODE_SYMBOL;
209 node->resolved = TRUE;
210 node->token_type = SYM_LIT;
212 node->indirection = NULL;
214 switch (blit->type) {
215 case DATA_FIXBINARY:
216 len = blit->length;
217 *lit_type = PARS_FIXBINARY_LIT;
218 break;
220 case DATA_BLOB:
221 *lit_type = PARS_BLOB_LIT;
222 break;
224 case DATA_VARCHAR:
225 *lit_type = PARS_STR_LIT;
226 break;
228 case DATA_CHAR:
229 ut_a(blit->length > 0);
231 len = blit->length;
232 *lit_type = PARS_STR_LIT;
233 break;
235 case DATA_INT:
236 ut_a(blit->length > 0);
237 ut_a(blit->length <= 8);
239 len = blit->length;
240 *lit_type = PARS_INT_LIT;
241 break;
243 default:
244 ut_error;
247 dtype_set(dfield_get_type(&node->common.val),
248 blit->type, blit->prtype, len);
250 dfield_set_data(&(node->common.val), blit->address, blit->length);
252 node->common.val_buf_size = 0;
253 node->prefetch_buf = NULL;
254 node->cursor_def = NULL;
256 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
258 node->sym_table = sym_tab;
260 return(node);
263 /******************************************************************//**
264 Adds an SQL null literal to a symbol table.
265 @return symbol table node */
266 UNIV_INTERN
267 sym_node_t*
268 sym_tab_add_null_lit(
269 /*=================*/
270 sym_tab_t* sym_tab) /*!< in: symbol table */
272 sym_node_t* node;
274 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
276 node->common.type = QUE_NODE_SYMBOL;
278 node->resolved = TRUE;
279 node->token_type = SYM_LIT;
281 node->indirection = NULL;
283 dfield_get_type(&node->common.val)->mtype = DATA_ERROR;
285 dfield_set_null(&node->common.val);
287 node->common.val_buf_size = 0;
288 node->prefetch_buf = NULL;
289 node->cursor_def = NULL;
291 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
293 node->sym_table = sym_tab;
295 return(node);
298 /******************************************************************//**
299 Adds an identifier to a symbol table.
300 @return symbol table node */
301 UNIV_INTERN
302 sym_node_t*
303 sym_tab_add_id(
304 /*===========*/
305 sym_tab_t* sym_tab, /*!< in: symbol table */
306 byte* name, /*!< in: identifier name */
307 ulint len) /*!< in: identifier length */
309 sym_node_t* node;
311 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
313 node->common.type = QUE_NODE_SYMBOL;
315 node->resolved = FALSE;
316 node->indirection = NULL;
318 node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
319 node->name_len = len;
321 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
323 dfield_set_null(&node->common.val);
325 node->common.val_buf_size = 0;
326 node->prefetch_buf = NULL;
327 node->cursor_def = NULL;
329 node->sym_table = sym_tab;
331 return(node);
334 /******************************************************************//**
335 Add a bound identifier to a symbol table.
336 @return symbol table node */
337 UNIV_INTERN
338 sym_node_t*
339 sym_tab_add_bound_id(
340 /*===========*/
341 sym_tab_t* sym_tab, /*!< in: symbol table */
342 const char* name) /*!< in: name of bound id */
344 sym_node_t* node;
345 pars_bound_id_t* bid;
347 bid = pars_info_get_bound_id(sym_tab->info, name);
348 ut_a(bid);
350 node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
352 node->common.type = QUE_NODE_SYMBOL;
354 node->resolved = FALSE;
355 node->indirection = NULL;
357 node->name = mem_heap_strdup(sym_tab->heap, bid->id);
358 node->name_len = strlen(node->name);
360 UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
362 dfield_set_null(&node->common.val);
364 node->common.val_buf_size = 0;
365 node->prefetch_buf = NULL;
366 node->cursor_def = NULL;
368 node->sym_table = sym_tab;
370 return(node);