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.

244 lines
8.1 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1997, 2009, Innobase Oy. 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., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 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. 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. Adds an SQL null literal to a symbol table.
  75. @return symbol table node */
  76. UNIV_INTERN
  77. sym_node_t*
  78. sym_tab_add_null_lit(
  79. /*=================*/
  80. sym_tab_t* sym_tab); /*!< in: symbol table */
  81. /******************************************************************//**
  82. Adds an identifier to a symbol table.
  83. @return symbol table node */
  84. UNIV_INTERN
  85. sym_node_t*
  86. sym_tab_add_id(
  87. /*===========*/
  88. sym_tab_t* sym_tab, /*!< in: symbol table */
  89. byte* name, /*!< in: identifier name */
  90. ulint len); /*!< in: identifier length */
  91. /******************************************************************//**
  92. Add a bound identifier to a symbol table.
  93. @return symbol table node */
  94. UNIV_INTERN
  95. sym_node_t*
  96. sym_tab_add_bound_id(
  97. /*===========*/
  98. sym_tab_t* sym_tab, /*!< in: symbol table */
  99. const char* name); /*!< in: name of bound id */
  100. /** Index of sym_node_struct::field_nos corresponding to the clustered index */
  101. #define SYM_CLUST_FIELD_NO 0
  102. /** Index of sym_node_struct::field_nos corresponding to a secondary index */
  103. #define SYM_SEC_FIELD_NO 1
  104. /** Types of a symbol table node */
  105. enum sym_tab_entry {
  106. SYM_VAR = 91, /*!< declared parameter or local
  107. variable of a procedure */
  108. SYM_IMPLICIT_VAR, /*!< storage for a intermediate result
  109. of a calculation */
  110. SYM_LIT, /*!< literal */
  111. SYM_TABLE, /*!< database table name */
  112. SYM_COLUMN, /*!< database table name */
  113. SYM_CURSOR, /*!< named cursor */
  114. SYM_PROCEDURE_NAME, /*!< stored procedure name */
  115. SYM_INDEX, /*!< database index name */
  116. SYM_FUNCTION /*!< user function name */
  117. };
  118. /** Symbol table node */
  119. struct sym_node_struct{
  120. que_common_t common; /*!< node type:
  121. QUE_NODE_SYMBOL */
  122. /* NOTE: if the data field in 'common.val' is not NULL and the symbol
  123. table node is not for a temporary column, the memory for the value has
  124. been allocated from dynamic memory and it should be freed when the
  125. symbol table is discarded */
  126. /* 'alias' and 'indirection' are almost the same, but not quite.
  127. 'alias' always points to the primary instance of the variable, while
  128. 'indirection' does the same only if we should use the primary
  129. instance's values for the node's data. This is usually the case, but
  130. when initializing a cursor (e.g., "DECLARE CURSOR c IS SELECT * FROM
  131. t WHERE id = x;"), we copy the values from the primary instance to
  132. the cursor's instance so that they are fixed for the duration of the
  133. cursor, and set 'indirection' to NULL. If we did not, the value of
  134. 'x' could change between fetches and things would break horribly.
  135. TODO: It would be cleaner to make 'indirection' a boolean field and
  136. always use 'alias' to refer to the primary node. */
  137. sym_node_t* indirection; /*!< pointer to
  138. another symbol table
  139. node which contains
  140. the value for this
  141. node, NULL otherwise */
  142. sym_node_t* alias; /*!< pointer to
  143. another symbol table
  144. node for which this
  145. node is an alias,
  146. NULL otherwise */
  147. UT_LIST_NODE_T(sym_node_t) col_var_list; /*!< list of table
  148. columns or a list of
  149. input variables for an
  150. explicit cursor */
  151. ibool copy_val; /*!< TRUE if a column
  152. and its value should
  153. be copied to dynamic
  154. memory when fetched */
  155. ulint field_nos[2]; /*!< if a column, in
  156. the position
  157. SYM_CLUST_FIELD_NO is
  158. the field number in the
  159. clustered index; in
  160. the position
  161. SYM_SEC_FIELD_NO
  162. the field number in the
  163. non-clustered index to
  164. use first; if not found
  165. from the index, then
  166. ULINT_UNDEFINED */
  167. ibool resolved; /*!< TRUE if the
  168. meaning of a variable
  169. or a column has been
  170. resolved; for literals
  171. this is always TRUE */
  172. enum sym_tab_entry token_type; /*!< type of the
  173. parsed token */
  174. const char* name; /*!< name of an id */
  175. ulint name_len; /*!< id name length */
  176. dict_table_t* table; /*!< table definition
  177. if a table id or a
  178. column id */
  179. ulint col_no; /*!< column number if a
  180. column */
  181. sel_buf_t* prefetch_buf; /*!< NULL, or a buffer
  182. for cached column
  183. values for prefetched
  184. rows */
  185. sel_node_t* cursor_def; /*!< cursor definition
  186. select node if a
  187. named cursor */
  188. ulint param_type; /*!< PARS_INPUT,
  189. PARS_OUTPUT, or
  190. PARS_NOT_PARAM if not a
  191. procedure parameter */
  192. sym_tab_t* sym_table; /*!< back pointer to
  193. the symbol table */
  194. UT_LIST_NODE_T(sym_node_t) sym_list; /*!< list of symbol
  195. nodes */
  196. };
  197. /** Symbol table */
  198. struct sym_tab_struct{
  199. que_t* query_graph;
  200. /*!< query graph generated by the
  201. parser */
  202. const char* sql_string;
  203. /*!< SQL string to parse */
  204. size_t string_len;
  205. /*!< SQL string length */
  206. int next_char_pos;
  207. /*!< position of the next character in
  208. sql_string to give to the lexical
  209. analyzer */
  210. pars_info_t* info; /*!< extra information, or NULL */
  211. sym_node_list_t sym_list;
  212. /*!< list of symbol nodes in the symbol
  213. table */
  214. UT_LIST_BASE_NODE_T(func_node_t)
  215. func_node_list;
  216. /*!< list of function nodes in the
  217. parsed query graph */
  218. mem_heap_t* heap; /*!< memory heap from which we can
  219. allocate space */
  220. };
  221. #ifndef UNIV_NONINL
  222. #include "pars0sym.ic"
  223. #endif
  224. #endif