1
/*****************************************************************************
3
Copyright (C) 1997, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/eval0eval.ic
21
SQL evaluator: evaluates simple data structures, like expressions, in
24
Created 12/29/1997 Heikki Tuuri
25
*******************************************************/
31
# include "pars0grm.hh"
34
/*****************************************************************//**
35
Evaluates a function node. */
40
func_node_t* func_node); /*!< in: function node */
41
/*****************************************************************//**
42
Allocate a buffer from global dynamic memory for a value of a que_node.
43
NOTE that this memory must be explicitly freed when the query graph is
44
freed. If the node already has allocated buffer, that buffer is freed
45
here. NOTE that this is the only function where dynamic memory should be
46
allocated for a query node val field.
47
@return pointer to allocated buffer */
50
eval_node_alloc_val_buf(
51
/*====================*/
52
que_node_t* node, /*!< in: query graph node; sets the val field
53
data field to point to the new buffer, and
54
len field equal to size */
55
ulint size); /*!< in: buffer size */
58
/*****************************************************************//**
59
Allocates a new buffer if needed.
60
@return pointer to buffer */
63
eval_node_ensure_val_buf(
64
/*=====================*/
65
que_node_t* node, /*!< in: query graph node; sets the val field
66
data field to point to the new buffer, and
67
len field equal to size */
68
ulint size) /*!< in: buffer size */
73
dfield = que_node_get_val(node);
74
dfield_set_len(dfield, size);
76
data = static_cast<unsigned char *>(dfield_get_data(dfield));
78
if (!data || que_node_get_val_buf_size(node) < size) {
80
data = eval_node_alloc_val_buf(node, size);
86
/*****************************************************************//**
87
Evaluates a symbol table symbol. */
92
sym_node_t* sym_node) /*!< in: symbol table node */
95
ut_ad(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);
97
if (sym_node->indirection) {
98
/* The symbol table node is an alias for a variable or a
101
dfield_copy_data(que_node_get_val(sym_node),
102
que_node_get_val(sym_node->indirection));
106
/*****************************************************************//**
107
Evaluates an expression. */
112
que_node_t* exp_node) /*!< in: expression */
114
if (que_node_get_type(exp_node) == QUE_NODE_SYMBOL) {
116
eval_sym((sym_node_t*)exp_node);
121
eval_func(static_cast<func_node_t *>(exp_node));
124
/*****************************************************************//**
125
Sets an integer value as the value of an expression node. */
128
eval_node_set_int_val(
129
/*==================*/
130
que_node_t* node, /*!< in: expression node */
131
lint val) /*!< in: value to set */
136
dfield = que_node_get_val(node);
138
data = static_cast<byte *>(dfield_get_data(dfield));
141
data = eval_node_alloc_val_buf(node, 4);
144
ut_ad(dfield_get_len(dfield) == 4);
146
mach_write_to_4(data, (ulint)val);
149
/*****************************************************************//**
150
Gets an integer non-SQL null value from an expression node.
151
@return integer value */
154
eval_node_get_int_val(
155
/*==================*/
156
que_node_t* node) /*!< in: expression node */
160
dfield = que_node_get_val(node);
162
ut_ad(dfield_get_len(dfield) == 4);
164
return((int)mach_read_from_4(static_cast<const unsigned char *>(dfield_get_data(dfield))));
167
/*****************************************************************//**
168
Gets a iboolean value from a query node.
169
@return iboolean value */
172
eval_node_get_ibool_val(
173
/*====================*/
174
que_node_t* node) /*!< in: query graph node */
179
dfield = que_node_get_val(node);
181
data = static_cast<byte *>(dfield_get_data(dfield));
185
return(mach_read_from_1(data));
188
/*****************************************************************//**
189
Sets a iboolean value as the value of a function node. */
192
eval_node_set_ibool_val(
193
/*====================*/
194
func_node_t* func_node, /*!< in: function node */
195
ibool val) /*!< in: value to set */
200
dfield = que_node_get_val(func_node);
202
data = static_cast<byte *>(dfield_get_data(dfield));
205
/* Allocate 1 byte to hold the value */
207
data = eval_node_alloc_val_buf(func_node, 1);
210
ut_ad(dfield_get_len(dfield) == 1);
212
mach_write_to_1(data, val);
215
/*****************************************************************//**
216
Copies a binary string value as the value of a query graph node. Allocates a
217
new buffer if necessary. */
220
eval_node_copy_and_alloc_val(
221
/*=========================*/
222
que_node_t* node, /*!< in: query graph node */
223
const byte* str, /*!< in: binary string */
224
ulint len) /*!< in: string length or UNIV_SQL_NULL */
228
if (len == UNIV_SQL_NULL) {
229
dfield_set_len(que_node_get_val(node), len);
234
data = eval_node_ensure_val_buf(node, len);
236
ut_memcpy(data, str, len);
239
/*****************************************************************//**
240
Copies a query node value to another node. */
245
que_node_t* node1, /*!< in: node to copy to */
246
que_node_t* node2) /*!< in: node to copy from */
250
dfield2 = que_node_get_val(node2);
252
eval_node_copy_and_alloc_val(node1,
253
static_cast<unsigned char *>(dfield_get_data(dfield2)),
254
dfield_get_len(dfield2));