~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Moved the last of the libdrizzleclient calls into Protocol.

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