~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
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.
 
8
 
 
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.
 
12
 
 
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
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file pars/pars0sym.c
 
21
SQL parser symbol table
 
22
 
 
23
Created 12/15/1997 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#include "pars0sym.h"
 
27
 
 
28
#ifdef UNIV_NONINL
 
29
#include "pars0sym.ic"
 
30
#endif
 
31
 
 
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"
 
40
 
 
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 */
 
49
{
 
50
        sym_tab_t*      sym_tab;
 
51
 
 
52
        sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
 
53
 
 
54
        UT_LIST_INIT(sym_tab->sym_list);
 
55
        UT_LIST_INIT(sym_tab->func_node_list);
 
56
 
 
57
        sym_tab->heap = heap;
 
58
 
 
59
        return(sym_tab);
 
60
}
 
61
 
 
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 */
 
71
{
 
72
        sym_node_t*     sym;
 
73
        func_node_t*    func;
 
74
 
 
75
        sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
 
76
 
 
77
        while (sym) {
 
78
                eval_node_free_val_buf(sym);
 
79
 
 
80
                if (sym->prefetch_buf) {
 
81
                        sel_col_prefetch_buf_free(sym->prefetch_buf);
 
82
                }
 
83
 
 
84
                if (sym->cursor_def) {
 
85
                        que_graph_free_recursive(sym->cursor_def);
 
86
                }
 
87
 
 
88
                sym = UT_LIST_GET_NEXT(sym_list, sym);
 
89
        }
 
90
 
 
91
        func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
 
92
 
 
93
        while (func) {
 
94
                eval_node_free_val_buf(func);
 
95
 
 
96
                func = UT_LIST_GET_NEXT(func_node_list, func);
 
97
        }
 
98
}
 
99
 
 
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 */
 
109
{
 
110
        sym_node_t*     node;
 
111
        byte*           data;
 
112
 
 
113
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
114
 
 
115
        node->common.type = QUE_NODE_SYMBOL;
 
116
 
 
117
        node->resolved = TRUE;
 
118
        node->token_type = SYM_LIT;
 
119
 
 
120
        node->indirection = NULL;
 
121
 
 
122
        dtype_set(dfield_get_type(&node->common.val), DATA_INT, 0, 4);
 
123
 
 
124
        data = mem_heap_alloc(sym_tab->heap, 4);
 
125
        mach_write_to_4(data, val);
 
126
 
 
127
        dfield_set_data(&(node->common.val), data, 4);
 
128
 
 
129
        node->common.val_buf_size = 0;
 
130
        node->prefetch_buf = NULL;
 
131
        node->cursor_def = NULL;
 
132
 
 
133
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
134
 
 
135
        node->sym_table = sym_tab;
 
136
 
 
137
        return(node);
 
138
}
 
139
 
 
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 */
 
151
{
 
152
        sym_node_t*     node;
 
153
        byte*           data;
 
154
 
 
155
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
156
 
 
157
        node->common.type = QUE_NODE_SYMBOL;
 
158
 
 
159
        node->resolved = TRUE;
 
160
        node->token_type = SYM_LIT;
 
161
 
 
162
        node->indirection = NULL;
 
163
 
 
164
        dtype_set(dfield_get_type(&node->common.val),
 
165
                  DATA_VARCHAR, DATA_ENGLISH, 0);
 
166
 
 
167
        if (len) {
 
168
                data = mem_heap_alloc(sym_tab->heap, len);
 
169
                ut_memcpy(data, str, len);
 
170
        } else {
 
171
                data = NULL;
 
172
        }
 
173
 
 
174
        dfield_set_data(&(node->common.val), data, len);
 
175
 
 
176
        node->common.val_buf_size = 0;
 
177
        node->prefetch_buf = NULL;
 
178
        node->cursor_def = NULL;
 
179
 
 
180
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
181
 
 
182
        node->sym_table = sym_tab;
 
183
 
 
184
        return(node);
 
185
}
 
186
 
 
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) */
 
197
{
 
198
        sym_node_t*             node;
 
199
        pars_bound_lit_t*       blit;
 
200
        ulint                   len = 0;
 
201
 
 
202
        blit = pars_info_get_bound_lit(sym_tab->info, name);
 
203
        ut_a(blit);
 
204
 
 
205
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
206
 
 
207
        node->common.type = QUE_NODE_SYMBOL;
 
208
 
 
209
        node->resolved = TRUE;
 
210
        node->token_type = SYM_LIT;
 
211
 
 
212
        node->indirection = NULL;
 
213
 
 
214
        switch (blit->type) {
 
215
        case DATA_FIXBINARY:
 
216
                len = blit->length;
 
217
                *lit_type = PARS_FIXBINARY_LIT;
 
218
                break;
 
219
 
 
220
        case DATA_BLOB:
 
221
                *lit_type = PARS_BLOB_LIT;
 
222
                break;
 
223
 
 
224
        case DATA_VARCHAR:
 
225
                *lit_type = PARS_STR_LIT;
 
226
                break;
 
227
 
 
228
        case DATA_CHAR:
 
229
                ut_a(blit->length > 0);
 
230
 
 
231
                len = blit->length;
 
232
                *lit_type = PARS_STR_LIT;
 
233
                break;
 
234
 
 
235
        case DATA_INT:
 
236
                ut_a(blit->length > 0);
 
237
                ut_a(blit->length <= 8);
 
238
 
 
239
                len = blit->length;
 
240
                *lit_type = PARS_INT_LIT;
 
241
                break;
 
242
 
 
243
        default:
 
244
                ut_error;
 
245
        }
 
246
 
 
247
        dtype_set(dfield_get_type(&node->common.val),
 
248
                  blit->type, blit->prtype, len);
 
249
 
 
250
        dfield_set_data(&(node->common.val), blit->address, blit->length);
 
251
 
 
252
        node->common.val_buf_size = 0;
 
253
        node->prefetch_buf = NULL;
 
254
        node->cursor_def = NULL;
 
255
 
 
256
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
257
 
 
258
        node->sym_table = sym_tab;
 
259
 
 
260
        return(node);
 
261
}
 
262
 
 
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 */
 
271
{
 
272
        sym_node_t*     node;
 
273
 
 
274
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
275
 
 
276
        node->common.type = QUE_NODE_SYMBOL;
 
277
 
 
278
        node->resolved = TRUE;
 
279
        node->token_type = SYM_LIT;
 
280
 
 
281
        node->indirection = NULL;
 
282
 
 
283
        dfield_get_type(&node->common.val)->mtype = DATA_ERROR;
 
284
 
 
285
        dfield_set_null(&node->common.val);
 
286
 
 
287
        node->common.val_buf_size = 0;
 
288
        node->prefetch_buf = NULL;
 
289
        node->cursor_def = NULL;
 
290
 
 
291
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
292
 
 
293
        node->sym_table = sym_tab;
 
294
 
 
295
        return(node);
 
296
}
 
297
 
 
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 */
 
308
{
 
309
        sym_node_t*     node;
 
310
 
 
311
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
312
 
 
313
        node->common.type = QUE_NODE_SYMBOL;
 
314
 
 
315
        node->resolved = FALSE;
 
316
        node->indirection = NULL;
 
317
 
 
318
        node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
 
319
        node->name_len = len;
 
320
 
 
321
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
322
 
 
323
        dfield_set_null(&node->common.val);
 
324
 
 
325
        node->common.val_buf_size = 0;
 
326
        node->prefetch_buf = NULL;
 
327
        node->cursor_def = NULL;
 
328
 
 
329
        node->sym_table = sym_tab;
 
330
 
 
331
        return(node);
 
332
}
 
333
 
 
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 */
 
343
{
 
344
        sym_node_t*             node;
 
345
        pars_bound_id_t*        bid;
 
346
 
 
347
        bid = pars_info_get_bound_id(sym_tab->info, name);
 
348
        ut_a(bid);
 
349
 
 
350
        node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
 
351
 
 
352
        node->common.type = QUE_NODE_SYMBOL;
 
353
 
 
354
        node->resolved = FALSE;
 
355
        node->indirection = NULL;
 
356
 
 
357
        node->name = mem_heap_strdup(sym_tab->heap, bid->id);
 
358
        node->name_len = strlen(node->name);
 
359
 
 
360
        UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
 
361
 
 
362
        dfield_set_null(&node->common.val);
 
363
 
 
364
        node->common.val_buf_size = 0;
 
365
        node->prefetch_buf = NULL;
 
366
        node->cursor_def = NULL;
 
367
 
 
368
        node->sym_table = sym_tab;
 
369
 
 
370
        return(node);
 
371
}