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.

352 lines
7.4 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************
  2. SQL parser symbol table
  3. (c) 1997 Innobase Oy
  4. Created 12/15/1997 Heikki Tuuri
  5. *******************************************************/
  6. #include "pars0sym.h"
  7. #ifdef UNIV_NONINL
  8. #include "pars0sym.ic"
  9. #endif
  10. #include "mem0mem.h"
  11. #include "data0type.h"
  12. #include "data0data.h"
  13. #include "pars0grm.h"
  14. #include "pars0pars.h"
  15. #include "que0que.h"
  16. #include "eval0eval.h"
  17. #include "row0sel.h"
  18. /**********************************************************************
  19. Creates a symbol table for a single stored procedure or query. */
  20. sym_tab_t*
  21. sym_tab_create(
  22. /*===========*/
  23. /* out, own: symbol table */
  24. mem_heap_t* heap) /* in: memory heap where to create */
  25. {
  26. sym_tab_t* sym_tab;
  27. sym_tab = mem_heap_alloc(heap, sizeof(sym_tab_t));
  28. UT_LIST_INIT(sym_tab->sym_list);
  29. UT_LIST_INIT(sym_tab->func_node_list);
  30. sym_tab->heap = heap;
  31. return(sym_tab);
  32. }
  33. /**********************************************************************
  34. Frees the memory allocated dynamically AFTER parsing phase for variables
  35. etc. in the symbol table. Does not free the mem heap where the table was
  36. originally created. Frees also SQL explicit cursor definitions. */
  37. void
  38. sym_tab_free_private(
  39. /*=================*/
  40. sym_tab_t* sym_tab) /* in, own: symbol table */
  41. {
  42. sym_node_t* sym;
  43. func_node_t* func;
  44. sym = UT_LIST_GET_FIRST(sym_tab->sym_list);
  45. while (sym) {
  46. eval_node_free_val_buf(sym);
  47. if (sym->prefetch_buf) {
  48. sel_col_prefetch_buf_free(sym->prefetch_buf);
  49. }
  50. if (sym->cursor_def) {
  51. que_graph_free_recursive(sym->cursor_def);
  52. }
  53. sym = UT_LIST_GET_NEXT(sym_list, sym);
  54. }
  55. func = UT_LIST_GET_FIRST(sym_tab->func_node_list);
  56. while (func) {
  57. eval_node_free_val_buf(func);
  58. func = UT_LIST_GET_NEXT(func_node_list, func);
  59. }
  60. }
  61. /**********************************************************************
  62. Adds an integer literal to a symbol table. */
  63. sym_node_t*
  64. sym_tab_add_int_lit(
  65. /*================*/
  66. /* out: symbol table node */
  67. sym_tab_t* sym_tab, /* in: symbol table */
  68. ulint val) /* in: integer value */
  69. {
  70. sym_node_t* node;
  71. byte* data;
  72. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  73. node->common.type = QUE_NODE_SYMBOL;
  74. node->resolved = TRUE;
  75. node->token_type = SYM_LIT;
  76. node->indirection = NULL;
  77. dtype_set(&(node->common.val.type), DATA_INT, 0, 4);
  78. data = mem_heap_alloc(sym_tab->heap, 4);
  79. mach_write_to_4(data, val);
  80. dfield_set_data(&(node->common.val), data, 4);
  81. node->common.val_buf_size = 0;
  82. node->prefetch_buf = NULL;
  83. node->cursor_def = NULL;
  84. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  85. node->sym_table = sym_tab;
  86. return(node);
  87. }
  88. /**********************************************************************
  89. Adds a string literal to a symbol table. */
  90. sym_node_t*
  91. sym_tab_add_str_lit(
  92. /*================*/
  93. /* out: symbol table node */
  94. sym_tab_t* sym_tab, /* in: symbol table */
  95. byte* str, /* in: string with no quotes around
  96. it */
  97. ulint len) /* in: string length */
  98. {
  99. sym_node_t* node;
  100. byte* data;
  101. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  102. node->common.type = QUE_NODE_SYMBOL;
  103. node->resolved = TRUE;
  104. node->token_type = SYM_LIT;
  105. node->indirection = NULL;
  106. dtype_set(&(node->common.val.type), DATA_VARCHAR, DATA_ENGLISH, 0);
  107. if (len) {
  108. data = mem_heap_alloc(sym_tab->heap, len);
  109. ut_memcpy(data, str, len);
  110. } else {
  111. data = NULL;
  112. }
  113. dfield_set_data(&(node->common.val), data, len);
  114. node->common.val_buf_size = 0;
  115. node->prefetch_buf = NULL;
  116. node->cursor_def = NULL;
  117. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  118. node->sym_table = sym_tab;
  119. return(node);
  120. }
  121. /**********************************************************************
  122. Add a bound literal to a symbol table. */
  123. sym_node_t*
  124. sym_tab_add_bound_lit(
  125. /*==================*/
  126. /* out: symbol table node */
  127. sym_tab_t* sym_tab, /* in: symbol table */
  128. const char* name, /* in: name of bound literal */
  129. ulint* lit_type) /* out: type of literal (PARS_*_LIT) */
  130. {
  131. sym_node_t* node;
  132. pars_bound_lit_t* blit;
  133. ulint len = 0;
  134. blit = pars_info_get_bound_lit(sym_tab->info, name);
  135. ut_a(blit);
  136. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  137. node->common.type = QUE_NODE_SYMBOL;
  138. node->resolved = TRUE;
  139. node->token_type = SYM_LIT;
  140. node->indirection = NULL;
  141. switch (blit->type) {
  142. case DATA_FIXBINARY:
  143. len = blit->length;
  144. *lit_type = PARS_FIXBINARY_LIT;
  145. break;
  146. case DATA_BLOB:
  147. *lit_type = PARS_BLOB_LIT;
  148. break;
  149. case DATA_VARCHAR:
  150. *lit_type = PARS_STR_LIT;
  151. break;
  152. case DATA_CHAR:
  153. ut_a(blit->length > 0);
  154. len = blit->length;
  155. *lit_type = PARS_STR_LIT;
  156. break;
  157. case DATA_INT:
  158. ut_a(blit->length > 0);
  159. ut_a(blit->length <= 8);
  160. len = blit->length;
  161. *lit_type = PARS_INT_LIT;
  162. break;
  163. default:
  164. ut_error;
  165. }
  166. dtype_set(&(node->common.val.type), blit->type, blit->prtype, len);
  167. dfield_set_data(&(node->common.val), blit->address, blit->length);
  168. node->common.val_buf_size = 0;
  169. node->prefetch_buf = NULL;
  170. node->cursor_def = NULL;
  171. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  172. node->sym_table = sym_tab;
  173. return(node);
  174. }
  175. /**********************************************************************
  176. Adds an SQL null literal to a symbol table. */
  177. sym_node_t*
  178. sym_tab_add_null_lit(
  179. /*=================*/
  180. /* out: symbol table node */
  181. sym_tab_t* sym_tab) /* in: symbol table */
  182. {
  183. sym_node_t* node;
  184. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  185. node->common.type = QUE_NODE_SYMBOL;
  186. node->resolved = TRUE;
  187. node->token_type = SYM_LIT;
  188. node->indirection = NULL;
  189. node->common.val.type.mtype = DATA_ERROR;
  190. dfield_set_null(&node->common.val);
  191. node->common.val_buf_size = 0;
  192. node->prefetch_buf = NULL;
  193. node->cursor_def = NULL;
  194. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  195. node->sym_table = sym_tab;
  196. return(node);
  197. }
  198. /**********************************************************************
  199. Adds an identifier to a symbol table. */
  200. sym_node_t*
  201. sym_tab_add_id(
  202. /*===========*/
  203. /* out: symbol table node */
  204. sym_tab_t* sym_tab, /* in: symbol table */
  205. byte* name, /* in: identifier name */
  206. ulint len) /* in: identifier length */
  207. {
  208. sym_node_t* node;
  209. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  210. node->common.type = QUE_NODE_SYMBOL;
  211. node->resolved = FALSE;
  212. node->indirection = NULL;
  213. node->name = mem_heap_strdupl(sym_tab->heap, (char*) name, len);
  214. node->name_len = len;
  215. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  216. dfield_set_null(&node->common.val);
  217. node->common.val_buf_size = 0;
  218. node->prefetch_buf = NULL;
  219. node->cursor_def = NULL;
  220. node->sym_table = sym_tab;
  221. return(node);
  222. }
  223. /**********************************************************************
  224. Add a bound identifier to a symbol table. */
  225. sym_node_t*
  226. sym_tab_add_bound_id(
  227. /*===========*/
  228. /* out: symbol table node */
  229. sym_tab_t* sym_tab, /* in: symbol table */
  230. const char* name) /* in: name of bound id */
  231. {
  232. sym_node_t* node;
  233. pars_bound_id_t* bid;
  234. bid = pars_info_get_bound_id(sym_tab->info, name);
  235. ut_a(bid);
  236. node = mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t));
  237. node->common.type = QUE_NODE_SYMBOL;
  238. node->resolved = FALSE;
  239. node->indirection = NULL;
  240. node->name = mem_heap_strdup(sym_tab->heap, bid->id);
  241. node->name_len = strlen(node->name);
  242. UT_LIST_ADD_LAST(sym_list, sym_tab->sym_list, node);
  243. dfield_set_null(&node->common.val);
  244. node->common.val_buf_size = 0;
  245. node->prefetch_buf = NULL;
  246. node->cursor_def = NULL;
  247. node->sym_table = sym_tab;
  248. return(node);
  249. }