00001 /****************************************************** 00002 SQL parser symbol table 00003 00004 (c) 1997 Innobase Oy 00005 00006 Created 12/15/1997 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef pars0sym_h 00010 #define pars0sym_h 00011 00012 #include "univ.i" 00013 #include "que0types.h" 00014 #include "usr0types.h" 00015 #include "dict0types.h" 00016 #include "pars0types.h" 00017 #include "row0types.h" 00018 00019 /********************************************************************** 00020 Creates a symbol table for a single stored procedure or query. */ 00021 00022 sym_tab_t* 00023 sym_tab_create( 00024 /*===========*/ 00025 /* out, own: symbol table */ 00026 mem_heap_t* heap); /* in: memory heap where to create */ 00027 /********************************************************************** 00028 Frees the memory allocated dynamically AFTER parsing phase for variables 00029 etc. in the symbol table. Does not free the mem heap where the table was 00030 originally created. Frees also SQL explicit cursor definitions. */ 00031 00032 void 00033 sym_tab_free_private( 00034 /*=================*/ 00035 sym_tab_t* sym_tab); /* in, own: symbol table */ 00036 /********************************************************************** 00037 Adds an integer literal to a symbol table. */ 00038 00039 sym_node_t* 00040 sym_tab_add_int_lit( 00041 /*================*/ 00042 /* out: symbol table node */ 00043 sym_tab_t* sym_tab, /* in: symbol table */ 00044 ulint val); /* in: integer value */ 00045 /********************************************************************** 00046 Adds an string literal to a symbol table. */ 00047 00048 sym_node_t* 00049 sym_tab_add_str_lit( 00050 /*================*/ 00051 /* out: symbol table node */ 00052 sym_tab_t* sym_tab, /* in: symbol table */ 00053 byte* str, /* in: string with no quotes around 00054 it */ 00055 ulint len); /* in: string length */ 00056 /********************************************************************** 00057 Add a bound literal to a symbol table. */ 00058 00059 sym_node_t* 00060 sym_tab_add_bound_lit( 00061 /*==================*/ 00062 /* out: symbol table node */ 00063 sym_tab_t* sym_tab, /* in: symbol table */ 00064 const char* name, /* in: name of bound literal */ 00065 ulint* lit_type); /* out: type of literal (PARS_*_LIT) */ 00066 /********************************************************************** 00067 Adds an SQL null literal to a symbol table. */ 00068 00069 sym_node_t* 00070 sym_tab_add_null_lit( 00071 /*=================*/ 00072 /* out: symbol table node */ 00073 sym_tab_t* sym_tab); /* in: symbol table */ 00074 /********************************************************************** 00075 Adds an identifier to a symbol table. */ 00076 00077 sym_node_t* 00078 sym_tab_add_id( 00079 /*===========*/ 00080 /* out: symbol table node */ 00081 sym_tab_t* sym_tab, /* in: symbol table */ 00082 byte* name, /* in: identifier name */ 00083 ulint len); /* in: identifier length */ 00084 00085 /********************************************************************** 00086 Add a bound identifier to a symbol table. */ 00087 00088 sym_node_t* 00089 sym_tab_add_bound_id( 00090 /*===========*/ 00091 /* out: symbol table node */ 00092 sym_tab_t* sym_tab, /* in: symbol table */ 00093 const char* name); /* in: name of bound id */ 00094 00095 #define SYM_CLUST_FIELD_NO 0 00096 #define SYM_SEC_FIELD_NO 1 00097 00098 struct sym_node_struct{ 00099 que_common_t common; /* node type: 00100 QUE_NODE_SYMBOL */ 00101 /* NOTE: if the data field in 'common.val' is not NULL and the symbol 00102 table node is not for a temporary column, the memory for the value has 00103 been allocated from dynamic memory and it should be freed when the 00104 symbol table is discarded */ 00105 00106 /* 'alias' and 'indirection' are almost the same, but not quite. 00107 'alias' always points to the primary instance of the variable, while 00108 'indirection' does the same only if we should use the primary 00109 instance's values for the node's data. This is usually the case, but 00110 when initializing a cursor (e.g., "DECLARE CURSOR c IS SELECT * FROM 00111 t WHERE id = x;"), we copy the values from the primary instance to 00112 the cursor's instance so that they are fixed for the duration of the 00113 cursor, and set 'indirection' to NULL. If we did not, the value of 00114 'x' could change between fetches and things would break horribly. 00115 00116 TODO: It would be cleaner to make 'indirection' a boolean field and 00117 always use 'alias' to refer to the primary node. */ 00118 00119 sym_node_t* indirection; /* pointer to 00120 another symbol table 00121 node which contains 00122 the value for this 00123 node, NULL otherwise */ 00124 sym_node_t* alias; /* pointer to 00125 another symbol table 00126 node for which this 00127 node is an alias, 00128 NULL otherwise */ 00129 UT_LIST_NODE_T(sym_node_t) col_var_list; /* list of table 00130 columns or a list of 00131 input variables for an 00132 explicit cursor */ 00133 ibool copy_val; /* TRUE if a column 00134 and its value should 00135 be copied to dynamic 00136 memory when fetched */ 00137 ulint field_nos[2]; /* if a column, in 00138 the position 00139 SYM_CLUST_FIELD_NO is 00140 the field number in the 00141 clustered index; in 00142 the position 00143 SYM_SEC_FIELD_NO 00144 the field number in the 00145 non-clustered index to 00146 use first; if not found 00147 from the index, then 00148 ULINT_UNDEFINED */ 00149 ibool resolved; /* TRUE if the 00150 meaning of a variable 00151 or a column has been 00152 resolved; for literals 00153 this is always TRUE */ 00154 ulint token_type; /* SYM_VAR, SYM_COLUMN, 00155 SYM_IMPLICIT_VAR, 00156 SYM_LIT, SYM_TABLE, 00157 SYM_CURSOR, ... */ 00158 const char* name; /* name of an id */ 00159 ulint name_len; /* id name length */ 00160 dict_table_t* table; /* table definition 00161 if a table id or a 00162 column id */ 00163 ulint col_no; /* column number if a 00164 column */ 00165 sel_buf_t* prefetch_buf; /* NULL, or a buffer 00166 for cached column 00167 values for prefetched 00168 rows */ 00169 sel_node_t* cursor_def; /* cursor definition 00170 select node if a 00171 named cursor */ 00172 ulint param_type; /* PARS_INPUT, 00173 PARS_OUTPUT, or 00174 PARS_NOT_PARAM if not a 00175 procedure parameter */ 00176 sym_tab_t* sym_table; /* back pointer to 00177 the symbol table */ 00178 UT_LIST_NODE_T(sym_node_t) sym_list; /* list of symbol 00179 nodes */ 00180 }; 00181 00182 struct sym_tab_struct{ 00183 que_t* query_graph; 00184 /* query graph generated by the 00185 parser */ 00186 const char* sql_string; 00187 /* SQL string to parse */ 00188 size_t string_len; 00189 /* SQL string length */ 00190 int next_char_pos; 00191 /* position of the next character in 00192 sql_string to give to the lexical 00193 analyzer */ 00194 pars_info_t* info; /* extra information, or NULL */ 00195 sym_node_list_t sym_list; 00196 /* list of symbol nodes in the symbol 00197 table */ 00198 UT_LIST_BASE_NODE_T(func_node_t) 00199 func_node_list; 00200 /* list of function nodes in the 00201 parsed query graph */ 00202 mem_heap_t* heap; /* memory heap from which we can 00203 allocate space */ 00204 }; 00205 00206 /* Types of a symbol table entry */ 00207 #define SYM_VAR 91 /* declared parameter or local 00208 variable of a procedure */ 00209 #define SYM_IMPLICIT_VAR 92 /* storage for a intermediate result 00210 of a calculation */ 00211 #define SYM_LIT 93 /* literal */ 00212 #define SYM_TABLE 94 /* database table name */ 00213 #define SYM_COLUMN 95 /* database table name */ 00214 #define SYM_CURSOR 96 /* named cursor */ 00215 #define SYM_PROCEDURE_NAME 97 /* stored procedure name */ 00216 #define SYM_INDEX 98 /* database index name */ 00217 #define SYM_FUNCTION 99 /* user function name */ 00218 00219 #ifndef UNIV_NONINL 00220 #include "pars0sym.ic" 00221 #endif 00222 00223 #endif
1.4.7

