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