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.

251 lines
6.3 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
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. 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/eval0eval.ic
  15. SQL evaluator: evaluates simple data structures, like expressions, in
  16. a query graph
  17. Created 12/29/1997 Heikki Tuuri
  18. *******************************************************/
  19. #include "que0que.h"
  20. #include "rem0cmp.h"
  21. #include "pars0grm.h"
  22. /*****************************************************************//**
  23. Evaluates a function node. */
  24. UNIV_INTERN
  25. void
  26. eval_func(
  27. /*======*/
  28. func_node_t* func_node); /*!< in: function node */
  29. /*****************************************************************//**
  30. Allocate a buffer from global dynamic memory for a value of a que_node.
  31. NOTE that this memory must be explicitly freed when the query graph is
  32. freed. If the node already has allocated buffer, that buffer is freed
  33. here. NOTE that this is the only function where dynamic memory should be
  34. allocated for a query node val field.
  35. @return pointer to allocated buffer */
  36. UNIV_INTERN
  37. byte*
  38. eval_node_alloc_val_buf(
  39. /*====================*/
  40. que_node_t* node, /*!< in: query graph node; sets the val field
  41. data field to point to the new buffer, and
  42. len field equal to size */
  43. ulint size); /*!< in: buffer size */
  44. /*****************************************************************//**
  45. Allocates a new buffer if needed.
  46. @return pointer to buffer */
  47. UNIV_INLINE
  48. byte*
  49. eval_node_ensure_val_buf(
  50. /*=====================*/
  51. que_node_t* node, /*!< in: query graph node; sets the val field
  52. data field to point to the new buffer, and
  53. len field equal to size */
  54. ulint size) /*!< in: buffer size */
  55. {
  56. dfield_t* dfield;
  57. byte* data;
  58. dfield = que_node_get_val(node);
  59. dfield_set_len(dfield, size);
  60. data = dfield_get_data(dfield);
  61. if (!data || que_node_get_val_buf_size(node) < size) {
  62. data = eval_node_alloc_val_buf(node, size);
  63. }
  64. return(data);
  65. }
  66. /*****************************************************************//**
  67. Evaluates a symbol table symbol. */
  68. UNIV_INLINE
  69. void
  70. eval_sym(
  71. /*=====*/
  72. sym_node_t* sym_node) /*!< in: symbol table node */
  73. {
  74. ut_ad(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);
  75. if (sym_node->indirection) {
  76. /* The symbol table node is an alias for a variable or a
  77. column */
  78. dfield_copy_data(que_node_get_val(sym_node),
  79. que_node_get_val(sym_node->indirection));
  80. }
  81. }
  82. /*****************************************************************//**
  83. Evaluates an expression. */
  84. UNIV_INLINE
  85. void
  86. eval_exp(
  87. /*=====*/
  88. que_node_t* exp_node) /*!< in: expression */
  89. {
  90. if (que_node_get_type(exp_node) == QUE_NODE_SYMBOL) {
  91. eval_sym((sym_node_t*)exp_node);
  92. return;
  93. }
  94. eval_func(exp_node);
  95. }
  96. /*****************************************************************//**
  97. Sets an integer value as the value of an expression node. */
  98. UNIV_INLINE
  99. void
  100. eval_node_set_int_val(
  101. /*==================*/
  102. que_node_t* node, /*!< in: expression node */
  103. lint val) /*!< in: value to set */
  104. {
  105. dfield_t* dfield;
  106. byte* data;
  107. dfield = que_node_get_val(node);
  108. data = dfield_get_data(dfield);
  109. if (data == NULL) {
  110. data = eval_node_alloc_val_buf(node, 4);
  111. }
  112. ut_ad(dfield_get_len(dfield) == 4);
  113. mach_write_to_4(data, (ulint)val);
  114. }
  115. /*****************************************************************//**
  116. Gets an integer non-SQL null value from an expression node.
  117. @return integer value */
  118. UNIV_INLINE
  119. lint
  120. eval_node_get_int_val(
  121. /*==================*/
  122. que_node_t* node) /*!< in: expression node */
  123. {
  124. dfield_t* dfield;
  125. dfield = que_node_get_val(node);
  126. ut_ad(dfield_get_len(dfield) == 4);
  127. return((int)mach_read_from_4(dfield_get_data(dfield)));
  128. }
  129. /*****************************************************************//**
  130. Gets a iboolean value from a query node.
  131. @return iboolean value */
  132. UNIV_INLINE
  133. ibool
  134. eval_node_get_ibool_val(
  135. /*====================*/
  136. que_node_t* node) /*!< in: query graph node */
  137. {
  138. dfield_t* dfield;
  139. byte* data;
  140. dfield = que_node_get_val(node);
  141. data = dfield_get_data(dfield);
  142. ut_ad(data != NULL);
  143. return(mach_read_from_1(data));
  144. }
  145. /*****************************************************************//**
  146. Sets a iboolean value as the value of a function node. */
  147. UNIV_INLINE
  148. void
  149. eval_node_set_ibool_val(
  150. /*====================*/
  151. func_node_t* func_node, /*!< in: function node */
  152. ibool val) /*!< in: value to set */
  153. {
  154. dfield_t* dfield;
  155. byte* data;
  156. dfield = que_node_get_val(func_node);
  157. data = dfield_get_data(dfield);
  158. if (data == NULL) {
  159. /* Allocate 1 byte to hold the value */
  160. data = eval_node_alloc_val_buf(func_node, 1);
  161. }
  162. ut_ad(dfield_get_len(dfield) == 1);
  163. mach_write_to_1(data, val);
  164. }
  165. /*****************************************************************//**
  166. Copies a binary string value as the value of a query graph node. Allocates a
  167. new buffer if necessary. */
  168. UNIV_INLINE
  169. void
  170. eval_node_copy_and_alloc_val(
  171. /*=========================*/
  172. que_node_t* node, /*!< in: query graph node */
  173. const byte* str, /*!< in: binary string */
  174. ulint len) /*!< in: string length or UNIV_SQL_NULL */
  175. {
  176. byte* data;
  177. if (len == UNIV_SQL_NULL) {
  178. dfield_set_len(que_node_get_val(node), len);
  179. return;
  180. }
  181. data = eval_node_ensure_val_buf(node, len);
  182. ut_memcpy(data, str, len);
  183. }
  184. /*****************************************************************//**
  185. Copies a query node value to another node. */
  186. UNIV_INLINE
  187. void
  188. eval_node_copy_val(
  189. /*===============*/
  190. que_node_t* node1, /*!< in: node to copy to */
  191. que_node_t* node2) /*!< in: node to copy from */
  192. {
  193. dfield_t* dfield2;
  194. dfield2 = que_node_get_val(node2);
  195. eval_node_copy_and_alloc_val(node1, dfield_get_data(dfield2),
  196. dfield_get_len(dfield2));
  197. }