~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/pars/pars0sym.c

  • Committer: Brian Aker
  • Date: 2009-07-11 19:23:04 UTC
  • mfrom: (1089.1.14 merge)
  • Revision ID: brian@gaz-20090711192304-ootijyl5yf9jq9kd
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/******************************************************
2
 
SQL parser symbol table
3
 
 
4
 
(c) 1997 Innobase Oy
5
 
 
6
 
Created 12/15/1997 Heikki Tuuri
7
 
*******************************************************/
8
 
 
9
 
#include "pars0sym.h"
10
 
 
11
 
#ifdef UNIV_NONINL
12
 
#include "pars0sym.ic"
13
 
#endif
14
 
 
15
 
#include "mem0mem.h"
16
 
#include "data0type.h"
17
 
#include "data0data.h"
18
 
#include "pars0grm.h"
19
 
#include "pars0pars.h"
20
 
#include "que0que.h"
21
 
#include "eval0eval.h"
22
 
#include "row0sel.h"
23
 
 
24
 
/**********************************************************************
25
 
Creates a symbol table for a single stored procedure or query. */
26
 
 
27
 
sym_tab_t*
28
 
sym_tab_create(
29
 
/*===========*/
30
 
                                /* out, own: symbol table */
31
 
        mem_heap_t*     heap)   /* in: memory heap where to create */
32
 
{
33
 
        sym_tab_t*      sym_tab;
34
 
 
35
 
        sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
36
 
 
37
 
        UT_LIST_INIT(sym_tab->sym_list);
38
 
        UT_LIST_INIT(sym_tab->func_node_list);
39
 
 
40
 
        sym_tab->heap = heap;
41
 
 
42
 
        return(sym_tab);
43
 
}
44
 
 
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. */
49
 
 
50
 
void
51
 
sym_tab_free_private(
52
 
/*=================*/
53
 
        sym_tab_t*      sym_tab)        /* in, own: symbol table */
54
 
{
55
 
        sym_node_t*     sym;
56
 
        func_node_t*    func;
57
 
 
58
 
        sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
59
 
 
60
 
        while (sym) {
61
 
                eval_node_free_val_buf(sym);
62
 
 
63
 
                if (sym->prefetch_buf) {
64
 
                        sel_col_prefetch_buf_free(sym->prefetch_buf);
65
 
                }
66
 
 
67
 
                if (sym->cursor_def) {
68
 
                        que_graph_free_recursive(sym->cursor_def);
69
 
                }
70
 
 
71
 
                sym = UT_LIST_GET_NEXT(sym_list, sym);
72
 
        }
73
 
 
74
 
        func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
75
 
 
76
 
        while (func) {
77
 
                eval_node_free_val_buf(func);
78
 
 
79
 
                func = UT_LIST_GET_NEXT(func_node_list, func);
80
 
        }
81
 
}
82
 
 
83
 
/**********************************************************************
84
 
Adds an integer literal to a symbol table. */
85
 
 
86
 
sym_node_t*
87
 
sym_tab_add_int_lit(
88
 
/*================*/
89
 
                                        /* out: symbol table node */
90
 
        sym_tab_t*      sym_tab,        /* in: symbol table */
91
 
        ulint           val)            /* in: integer value */
92
 
{
93
 
        sym_node_t*     node;
94
 
        byte*           data;
95
 
 
96
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
97
 
 
98
 
        node->common.type = QUE_NODE_SYMBOL;
99
 
 
100
 
        node->resolved = TRUE;
101
 
        node->token_type = SYM_LIT;
102
 
 
103
 
        node->indirection = NULL;
104
 
 
105
 
        dtype_set(&(node->common.val.type), DATA_INT, 0, 4);
106
 
 
107
 
        data = mem_heap_alloc(sym_tab->heap, 4);
108
 
        mach_write_to_4(data, val);
109
 
 
110
 
        dfield_set_data(&(node->common.val), data, 4);
111
 
 
112
 
        node->common.val_buf_size = 0;
113
 
        node->prefetch_buf = NULL;
114
 
        node->cursor_def = NULL;
115
 
 
116
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
117
 
 
118
 
        node->sym_table = sym_tab;
119
 
 
120
 
        return(node);
121
 
}
122
 
 
123
 
/**********************************************************************
124
 
Adds a string literal to a symbol table. */
125
 
 
126
 
sym_node_t*
127
 
sym_tab_add_str_lit(
128
 
/*================*/
129
 
                                        /* out: symbol table node */
130
 
        sym_tab_t*      sym_tab,        /* in: symbol table */
131
 
        byte*           str,            /* in: string with no quotes around
132
 
                                        it */
133
 
        ulint           len)            /* in: string length */
134
 
{
135
 
        sym_node_t*     node;
136
 
        byte*           data;
137
 
 
138
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
139
 
 
140
 
        node->common.type = QUE_NODE_SYMBOL;
141
 
 
142
 
        node->resolved = TRUE;
143
 
        node->token_type = SYM_LIT;
144
 
 
145
 
        node->indirection = NULL;
146
 
 
147
 
        dtype_set(&(node->common.val.type), DATA_VARCHAR, DATA_ENGLISH, 0);
148
 
 
149
 
        if (len) {
150
 
                data = mem_heap_alloc(sym_tab->heap, len);
151
 
                ut_memcpy(data, str, len);
152
 
        } else {
153
 
                data = NULL;
154
 
        }
155
 
 
156
 
        dfield_set_data(&(node->common.val), data, len);
157
 
 
158
 
        node->common.val_buf_size = 0;
159
 
        node->prefetch_buf = NULL;
160
 
        node->cursor_def = NULL;
161
 
 
162
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
163
 
 
164
 
        node->sym_table = sym_tab;
165
 
 
166
 
        return(node);
167
 
}
168
 
 
169
 
/**********************************************************************
170
 
Add a bound literal to a symbol table. */
171
 
 
172
 
sym_node_t*
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) */
179
 
{
180
 
        sym_node_t*             node;
181
 
        pars_bound_lit_t*       blit;
182
 
        ulint                   len = 0;
183
 
 
184
 
        blit = pars_info_get_bound_lit(sym_tab->info, name);
185
 
        ut_a(blit);
186
 
 
187
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
188
 
 
189
 
        node->common.type = QUE_NODE_SYMBOL;
190
 
 
191
 
        node->resolved = TRUE;
192
 
        node->token_type = SYM_LIT;
193
 
 
194
 
        node->indirection = NULL;
195
 
 
196
 
        switch (blit->type) {
197
 
        case DATA_FIXBINARY:
198
 
                len = blit->length;
199
 
                *lit_type = PARS_FIXBINARY_LIT;
200
 
                break;
201
 
 
202
 
        case DATA_BLOB:
203
 
                *lit_type = PARS_BLOB_LIT;
204
 
                break;
205
 
 
206
 
        case DATA_VARCHAR:
207
 
                *lit_type = PARS_STR_LIT;
208
 
                break;
209
 
 
210
 
        case DATA_CHAR:
211
 
                ut_a(blit->length > 0);
212
 
 
213
 
                len = blit->length;
214
 
                *lit_type = PARS_STR_LIT;
215
 
                break;
216
 
 
217
 
        case DATA_INT:
218
 
                ut_a(blit->length > 0);
219
 
                ut_a(blit->length <= 8);
220
 
 
221
 
                len = blit->length;
222
 
                *lit_type = PARS_INT_LIT;
223
 
                break;
224
 
 
225
 
        default:
226
 
                ut_error;
227
 
        }
228
 
 
229
 
        dtype_set(&(node->common.val.type), blit->type, blit->prtype, len);
230
 
 
231
 
        dfield_set_data(&(node->common.val), blit->address, blit->length);
232
 
 
233
 
        node->common.val_buf_size = 0;
234
 
        node->prefetch_buf = NULL;
235
 
        node->cursor_def = NULL;
236
 
 
237
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
238
 
 
239
 
        node->sym_table = sym_tab;
240
 
 
241
 
        return(node);
242
 
}
243
 
 
244
 
/**********************************************************************
245
 
Adds an SQL null literal to a symbol table. */
246
 
 
247
 
sym_node_t*
248
 
sym_tab_add_null_lit(
249
 
/*=================*/
250
 
                                        /* out: symbol table node */
251
 
        sym_tab_t*      sym_tab)        /* in: symbol table */
252
 
{
253
 
        sym_node_t*     node;
254
 
 
255
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
256
 
 
257
 
        node->common.type = QUE_NODE_SYMBOL;
258
 
 
259
 
        node->resolved = TRUE;
260
 
        node->token_type = SYM_LIT;
261
 
 
262
 
        node->indirection = NULL;
263
 
 
264
 
        node->common.val.type.mtype = DATA_ERROR;
265
 
 
266
 
        dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
267
 
 
268
 
        node->common.val_buf_size = 0;
269
 
        node->prefetch_buf = NULL;
270
 
        node->cursor_def = NULL;
271
 
 
272
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
273
 
 
274
 
        node->sym_table = sym_tab;
275
 
 
276
 
        return(node);
277
 
}
278
 
 
279
 
/**********************************************************************
280
 
Adds an identifier to a symbol table. */
281
 
 
282
 
sym_node_t*
283
 
sym_tab_add_id(
284
 
/*===========*/
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 */
289
 
{
290
 
        sym_node_t*     node;
291
 
 
292
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
293
 
 
294
 
        node->common.type = QUE_NODE_SYMBOL;
295
 
 
296
 
        node->resolved = FALSE;
297
 
        node->indirection = NULL;
298
 
 
299
 
        node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
300
 
        node->name_len = len;
301
 
 
302
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
303
 
 
304
 
        dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
305
 
 
306
 
        node->common.val_buf_size = 0;
307
 
        node->prefetch_buf = NULL;
308
 
        node->cursor_def = NULL;
309
 
 
310
 
        node->sym_table = sym_tab;
311
 
 
312
 
        return(node);
313
 
}
314
 
 
315
 
/**********************************************************************
316
 
Add a bound identifier to a symbol table. */
317
 
 
318
 
sym_node_t*
319
 
sym_tab_add_bound_id(
320
 
/*===========*/
321
 
                                        /* out: symbol table node */
322
 
        sym_tab_t*      sym_tab,        /* in: symbol table */
323
 
        const char*     name)           /* in: name of bound id */
324
 
{
325
 
        sym_node_t*             node;
326
 
        pars_bound_id_t*        bid;
327
 
 
328
 
        bid = pars_info_get_bound_id(sym_tab->info, name);
329
 
        ut_a(bid);
330
 
 
331
 
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
332
 
 
333
 
        node->common.type = QUE_NODE_SYMBOL;
334
 
 
335
 
        node->resolved = FALSE;
336
 
        node->indirection = NULL;
337
 
 
338
 
        node->name = mem_heap_strdup(sym_tab->heap, bid->id);
339
 
        node->name_len = strlen(node->name);
340
 
 
341
 
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
342
 
 
343
 
        dfield_set_data(&(node->common.val), NULL, UNIV_SQL_NULL);
344
 
 
345
 
        node->common.val_buf_size = 0;
346
 
        node->prefetch_buf = NULL;
347
 
        node->cursor_def = NULL;
348
 
 
349
 
        node->sym_table = sym_tab;
350
 
 
351
 
        return(node);
352
 
}