~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/eval0eval.ic

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (c) 1997, 2009, Innobase Oy. All Rights Reserved.
4
 
 
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.
8
 
 
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.
12
 
 
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., 59 Temple
15
 
Place, Suite 330, Boston, MA 02111-1307 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file include/eval0eval.ic
21
 
SQL evaluator: evaluates simple data structures, like expressions, in
22
 
a query graph
23
 
 
24
 
Created 12/29/1997 Heikki Tuuri
25
 
*******************************************************/
26
 
 
27
 
#include "que0que.h"
28
 
#include "rem0cmp.h"
29
 
#ifndef PARS0GRM_H
30
 
# define PARS0GRM_H
31
 
# include "pars0grm.h"
32
 
#endif
33
 
 
34
 
/*****************************************************************//**
35
 
Evaluates a function node. */
36
 
UNIV_INTERN
37
 
void
38
 
eval_func(
39
 
/*======*/
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 */
48
 
UNIV_INTERN
49
 
byte*
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 */
56
 
 
57
 
 
58
 
/*****************************************************************//**
59
 
Allocates a new buffer if needed.
60
 
@return pointer to buffer */
61
 
UNIV_INLINE
62
 
byte*
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 */
69
 
{
70
 
        dfield_t*       dfield;
71
 
        byte*           data;
72
 
 
73
 
        dfield = que_node_get_val(node);
74
 
        dfield_set_len(dfield, size);
75
 
 
76
 
        data = dfield_get_data(dfield);
77
 
 
78
 
        if (!data || que_node_get_val_buf_size(node) < size) {
79
 
 
80
 
                data = eval_node_alloc_val_buf(node, size);
81
 
        }
82
 
 
83
 
        return(data);
84
 
}
85
 
 
86
 
/*****************************************************************//**
87
 
Evaluates a symbol table symbol. */
88
 
UNIV_INLINE
89
 
void
90
 
eval_sym(
91
 
/*=====*/
92
 
        sym_node_t*     sym_node)       /*!< in: symbol table node */
93
 
{
94
 
 
95
 
        ut_ad(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);
96
 
 
97
 
        if (sym_node->indirection) {
98
 
                /* The symbol table node is an alias for a variable or a
99
 
                column */
100
 
 
101
 
                dfield_copy_data(que_node_get_val(sym_node),
102
 
                                 que_node_get_val(sym_node->indirection));
103
 
        }
104
 
}
105
 
 
106
 
/*****************************************************************//**
107
 
Evaluates an expression. */
108
 
UNIV_INLINE
109
 
void
110
 
eval_exp(
111
 
/*=====*/
112
 
        que_node_t*     exp_node)       /*!< in: expression */
113
 
{
114
 
        if (que_node_get_type(exp_node) == QUE_NODE_SYMBOL) {
115
 
 
116
 
                eval_sym((sym_node_t*)exp_node);
117
 
 
118
 
                return;
119
 
        }
120
 
 
121
 
        eval_func(exp_node);
122
 
}
123
 
 
124
 
/*****************************************************************//**
125
 
Sets an integer value as the value of an expression node. */
126
 
UNIV_INLINE
127
 
void
128
 
eval_node_set_int_val(
129
 
/*==================*/
130
 
        que_node_t*     node,   /*!< in: expression node */
131
 
        lint            val)    /*!< in: value to set */
132
 
{
133
 
        dfield_t*       dfield;
134
 
        byte*           data;
135
 
 
136
 
        dfield = que_node_get_val(node);
137
 
 
138
 
        data = dfield_get_data(dfield);
139
 
 
140
 
        if (data == NULL) {
141
 
                data = eval_node_alloc_val_buf(node, 4);
142
 
        }
143
 
 
144
 
        ut_ad(dfield_get_len(dfield) == 4);
145
 
 
146
 
        mach_write_to_4(data, (ulint)val);
147
 
}
148
 
 
149
 
/*****************************************************************//**
150
 
Gets an integer non-SQL null value from an expression node.
151
 
@return integer value */
152
 
UNIV_INLINE
153
 
lint
154
 
eval_node_get_int_val(
155
 
/*==================*/
156
 
        que_node_t*     node)   /*!< in: expression node */
157
 
{
158
 
        dfield_t*       dfield;
159
 
 
160
 
        dfield = que_node_get_val(node);
161
 
 
162
 
        ut_ad(dfield_get_len(dfield) == 4);
163
 
 
164
 
        return((int)mach_read_from_4(dfield_get_data(dfield)));
165
 
}
166
 
 
167
 
/*****************************************************************//**
168
 
Gets a iboolean value from a query node.
169
 
@return iboolean value */
170
 
UNIV_INLINE
171
 
ibool
172
 
eval_node_get_ibool_val(
173
 
/*====================*/
174
 
        que_node_t*     node)   /*!< in: query graph node */
175
 
{
176
 
        dfield_t*       dfield;
177
 
        byte*           data;
178
 
 
179
 
        dfield = que_node_get_val(node);
180
 
 
181
 
        data = dfield_get_data(dfield);
182
 
 
183
 
        ut_ad(data != NULL);
184
 
 
185
 
        return(mach_read_from_1(data));
186
 
}
187
 
 
188
 
/*****************************************************************//**
189
 
Sets a iboolean value as the value of a function node. */
190
 
UNIV_INLINE
191
 
void
192
 
eval_node_set_ibool_val(
193
 
/*====================*/
194
 
        func_node_t*    func_node,      /*!< in: function node */
195
 
        ibool           val)            /*!< in: value to set */
196
 
{
197
 
        dfield_t*       dfield;
198
 
        byte*           data;
199
 
 
200
 
        dfield = que_node_get_val(func_node);
201
 
 
202
 
        data = dfield_get_data(dfield);
203
 
 
204
 
        if (data == NULL) {
205
 
                /* Allocate 1 byte to hold the value */
206
 
 
207
 
                data = eval_node_alloc_val_buf(func_node, 1);
208
 
        }
209
 
 
210
 
        ut_ad(dfield_get_len(dfield) == 1);
211
 
 
212
 
        mach_write_to_1(data, val);
213
 
}
214
 
 
215
 
/*****************************************************************//**
216
 
Copies a binary string value as the value of a query graph node. Allocates a
217
 
new buffer if necessary. */
218
 
UNIV_INLINE
219
 
void
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 */
225
 
{
226
 
        byte*           data;
227
 
 
228
 
        if (len == UNIV_SQL_NULL) {
229
 
                dfield_set_len(que_node_get_val(node), len);
230
 
 
231
 
                return;
232
 
        }
233
 
 
234
 
        data = eval_node_ensure_val_buf(node, len);
235
 
 
236
 
        ut_memcpy(data, str, len);
237
 
}
238
 
 
239
 
/*****************************************************************//**
240
 
Copies a query node value to another node. */
241
 
UNIV_INLINE
242
 
void
243
 
eval_node_copy_val(
244
 
/*===============*/
245
 
        que_node_t*     node1,  /*!< in: node to copy to */
246
 
        que_node_t*     node2)  /*!< in: node to copy from */
247
 
{
248
 
        dfield_t*       dfield2;
249
 
 
250
 
        dfield2 = que_node_get_val(node2);
251
 
 
252
 
        eval_node_copy_and_alloc_val(node1, dfield_get_data(dfield2),
253
 
                                     dfield_get_len(dfield2));
254
 
}