1 /*****************************************************************************
2 
3 Copyright (c) 1997, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 2019, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 
18 *****************************************************************************/
19 
20 /**************************************************//**
21 @file include/pars0sym.h
22 SQL parser symbol table
23 
24 Created 12/15/1997 Heikki Tuuri
25 *******************************************************/
26 
27 #ifndef pars0sym_h
28 #define pars0sym_h
29 
30 #include "que0types.h"
31 #include "pars0types.h"
32 #include "row0types.h"
33 
34 /******************************************************************//**
35 Creates a symbol table for a single stored procedure or query.
36 @return own: symbol table */
37 sym_tab_t*
38 sym_tab_create(
39 /*===========*/
40 	mem_heap_t*	heap);	/*!< in: memory heap where to create */
41 /******************************************************************//**
42 Frees the memory allocated dynamically AFTER parsing phase for variables
43 etc. in the symbol table. Does not free the mem heap where the table was
44 originally created. Frees also SQL explicit cursor definitions. */
45 void
46 sym_tab_free_private(
47 /*=================*/
48 	sym_tab_t*	sym_tab);	/*!< in, own: symbol table */
49 /******************************************************************//**
50 Adds an integer literal to a symbol table.
51 @return symbol table node */
52 sym_node_t*
53 sym_tab_add_int_lit(
54 /*================*/
55 	sym_tab_t*	sym_tab,	/*!< in: symbol table */
56 	ulint		val);		/*!< in: integer value */
57 /******************************************************************//**
58 Adds an string literal to a symbol table.
59 @return symbol table node */
60 sym_node_t*
61 sym_tab_add_str_lit(
62 /*================*/
63 	sym_tab_t*	sym_tab,	/*!< in: symbol table */
64 	const byte*	str,		/*!< in: string with no quotes around
65 					it */
66 	ulint		len);		/*!< in: string length */
67 /******************************************************************//**
68 Add a bound literal to a symbol table.
69 @return symbol table node */
70 sym_node_t*
71 sym_tab_add_bound_lit(
72 /*==================*/
73 	sym_tab_t*	sym_tab,	/*!< in: symbol table */
74 	const char*	name,		/*!< in: name of bound literal */
75 	ulint*		lit_type);	/*!< out: type of literal (PARS_*_LIT) */
76 /**********************************************************************
77 Rebind literal to a node in the symbol table. */
78 sym_node_t*
79 sym_tab_rebind_lit(
80 /*===============*/
81                                         /* out: symbol table node */
82         sym_node_t*     node,           /* in: node that is bound to literal*/
83         const void*     address,        /* in: pointer to data */
84         ulint           length);        /* in: length of data */
85 /******************************************************************//**
86 Adds an SQL null literal to a symbol table.
87 @return symbol table node */
88 sym_node_t*
89 sym_tab_add_null_lit(
90 /*=================*/
91 	sym_tab_t*	sym_tab);	/*!< in: symbol table */
92 /******************************************************************//**
93 Adds an identifier to a symbol table.
94 @return symbol table node */
95 sym_node_t*
96 sym_tab_add_id(
97 /*===========*/
98 	sym_tab_t*	sym_tab,	/*!< in: symbol table */
99 	byte*		name,		/*!< in: identifier name */
100 	ulint		len);		/*!< in: identifier length */
101 
102 /******************************************************************//**
103 Add a bound identifier to a symbol table.
104 @return symbol table node */
105 sym_node_t*
106 sym_tab_add_bound_id(
107 /*===========*/
108 	sym_tab_t*	sym_tab,	/*!< in: symbol table */
109 	const char*	name);		/*!< in: name of bound id */
110 
111 /** Index of sym_node_t::field_nos corresponding to the clustered index */
112 #define	SYM_CLUST_FIELD_NO	0
113 /** Index of sym_node_t::field_nos corresponding to a secondary index */
114 #define	SYM_SEC_FIELD_NO	1
115 
116 /** Types of a symbol table node */
117 enum sym_tab_entry {
118 	SYM_UNSET,		/*!< Unset entry. */
119 	SYM_VAR = 91,		/*!< declared parameter or local
120 				variable of a procedure */
121 	SYM_IMPLICIT_VAR,	/*!< storage for a intermediate result
122 				of a calculation */
123 	SYM_LIT,		/*!< literal */
124 	SYM_TABLE_REF_COUNTED,	/*!< database table name, ref counted. Must
125 				be closed explicitly. */
126 	SYM_TABLE,		/*!< database table name */
127 	SYM_COLUMN,		/*!< database table name */
128 	SYM_CURSOR,		/*!< named cursor */
129 	SYM_PROCEDURE_NAME,	/*!< stored procedure name */
130 	SYM_INDEX,		/*!< database index name */
131 	SYM_FUNCTION		/*!< user function name */
132 };
133 
134 /** Symbol table node */
135 struct sym_node_t{
136 	que_common_t			common;		/*!< node type:
137 							QUE_NODE_SYMBOL */
138 	/* NOTE: if the data field in 'common.val' is not NULL and the symbol
139 	table node is not for a temporary column, the memory for the value has
140 	been allocated from dynamic memory and it should be freed when the
141 	symbol table is discarded */
142 
143 	/* 'alias' and 'indirection' are almost the same, but not quite.
144 	'alias' always points to the primary instance of the variable, while
145 	'indirection' does the same only if we should use the primary
146 	instance's values for the node's data. This is usually the case, but
147 	when initializing a cursor (e.g., "DECLARE CURSOR c IS SELECT * FROM
148 	t WHERE id = x;"), we copy the values from the primary instance to
149 	the cursor's instance so that they are fixed for the duration of the
150 	cursor, and set 'indirection' to NULL. If we did not, the value of
151 	'x' could change between fetches and things would break horribly.
152 
153 	TODO: It would be cleaner to make 'indirection' a boolean field and
154 	always use 'alias' to refer to the primary node. */
155 
156 	sym_node_t*			indirection;	/*!< pointer to
157 							another symbol table
158 							node which contains
159 							the value for this
160 							node, NULL otherwise */
161 	sym_node_t*			alias;		/*!< pointer to
162 							another symbol table
163 							node for which this
164 							node is an alias,
165 							NULL otherwise */
166 	UT_LIST_NODE_T(sym_node_t)	col_var_list;	/*!< list of table
167 							columns or a list of
168 							input variables for an
169 							explicit cursor */
170 	ibool				copy_val;	/*!< TRUE if a column
171 							and its value should
172 							be copied to dynamic
173 							memory when fetched */
174 	ulint				field_nos[2];	/*!< if a column, in
175 							the position
176 							SYM_CLUST_FIELD_NO is
177 							the field number in the
178 							clustered index; in
179 							the position
180 							SYM_SEC_FIELD_NO
181 							the field number in the
182 							non-clustered index to
183 							use first; if not found
184 							from the index, then
185 							ULINT_UNDEFINED */
186 	ibool				resolved;	/*!< TRUE if the
187 							meaning of a variable
188 							or a column has been
189 							resolved; for literals
190 							this is always TRUE */
191 	enum sym_tab_entry		token_type;	/*!< type of the
192 							parsed token */
193 	const char*			name;		/*!< name of an id */
194 	ulint				name_len;	/*!< id name length */
195 	dict_table_t*			table;		/*!< table definition
196 							if a table id or a
197 							column id */
198 	ulint				col_no;		/*!< column number if a
199 							column */
200 	sel_buf_t*			prefetch_buf;	/*!< NULL, or a buffer
201 							for cached column
202 							values for prefetched
203 							rows */
204 	sel_node_t*			cursor_def;	/*!< cursor definition
205 							select node if a
206 							named cursor */
207 	ulint				param_type;	/*!< PARS_INPUT,
208 							PARS_OUTPUT, or
209 							PARS_NOT_PARAM if not a
210 							procedure parameter */
211 	sym_tab_t*			sym_table;	/*!< back pointer to
212 							the symbol table */
213 	UT_LIST_NODE_T(sym_node_t)	sym_list;	/*!< list of symbol
214 							nodes */
215 	sym_node_t*			like_node;	/* LIKE operator node*/
216 };
217 
218 /** Symbol table */
219 struct sym_tab_t{
220 	que_t*			query_graph;
221 					/*!< query graph generated by the
222 					parser */
223 	const char*		sql_string;
224 					/*!< SQL string to parse */
225 	size_t			string_len;
226 					/*!< SQL string length */
227 	size_t			next_char_pos;
228 					/*!< position of the next character in
229 					sql_string to give to the lexical
230 					analyzer */
231 	pars_info_t*		info;	/*!< extra information, or NULL */
232 	sym_node_list_t		sym_list;
233 					/*!< list of symbol nodes in the symbol
234 					table */
235 	UT_LIST_BASE_NODE_T(func_node_t)
236 				func_node_list;
237 					/*!< list of function nodes in the
238 					parsed query graph */
239 	mem_heap_t*		heap;	/*!< memory heap from which we can
240 					allocate space */
241 };
242 
243 #endif
244