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 /**************************************************//**
21 SQL parser symbol table
23 Created 12/15/1997 Heikki Tuuri
24 *******************************************************/
29 #include "pars0sym.ic"
33 #include "data0type.h"
34 #include "data0data.h"
36 #include "pars0pars.h"
38 #include "eval0eval.h"
41 /******************************************************************//**
42 Creates a symbol table for a single stored procedure or query.
43 @return own: symbol table */
48 mem_heap_t
* heap
) /*!< in: memory heap where to create */
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
);
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. */
70 sym_tab_t
* sym_tab
) /*!< in, own: symbol table */
75 sym
= UT_LIST_GET_FIRST(sym_tab
->sym_list
);
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
);
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 */
107 sym_tab_t
* sym_tab
, /*!< in: symbol table */
108 ulint val
) /*!< in: integer value */
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
;
140 /******************************************************************//**
141 Adds a string literal to a symbol table.
142 @return symbol table node */
147 sym_tab_t
* sym_tab
, /*!< in: symbol table */
148 byte
* str
, /*!< in: string with no quotes around
150 ulint len
) /*!< in: string length */
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);
168 data
= mem_heap_alloc(sym_tab
->heap
, len
);
169 ut_memcpy(data
, str
, len
);
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
;
187 /******************************************************************//**
188 Add a bound literal to a symbol table.
189 @return symbol table node */
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) */
199 pars_bound_lit_t
* blit
;
202 blit
= pars_info_get_bound_lit(sym_tab
->info
, name
);
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
) {
217 *lit_type
= PARS_FIXBINARY_LIT
;
221 *lit_type
= PARS_BLOB_LIT
;
225 *lit_type
= PARS_STR_LIT
;
229 ut_a(blit
->length
> 0);
232 *lit_type
= PARS_STR_LIT
;
236 ut_a(blit
->length
> 0);
237 ut_a(blit
->length
<= 8);
240 *lit_type
= PARS_INT_LIT
;
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
;
263 /******************************************************************//**
264 Adds an SQL null literal to a symbol table.
265 @return symbol table node */
268 sym_tab_add_null_lit(
269 /*=================*/
270 sym_tab_t
* sym_tab
) /*!< in: symbol table */
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
;
298 /******************************************************************//**
299 Adds an identifier to a symbol table.
300 @return symbol table node */
305 sym_tab_t
* sym_tab
, /*!< in: symbol table */
306 byte
* name
, /*!< in: identifier name */
307 ulint len
) /*!< in: identifier length */
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
;
334 /******************************************************************//**
335 Add a bound identifier to a symbol table.
336 @return symbol table node */
339 sym_tab_add_bound_id(
341 sym_tab_t
* sym_tab
, /*!< in: symbol table */
342 const char* name
) /*!< in: name of bound id */
345 pars_bound_id_t
* bid
;
347 bid
= pars_info_get_bound_id(sym_tab
->info
, name
);
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
;