~drizzle-trunk/drizzle/development

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