You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
8.7 KiB

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