1 /*****************************************************************************
2 
3 Copyright (c) 1998, 2014, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file eval/eval0proc.cc
29 Executes SQL stored procedures and their control structures
30 
31 Created 1/20/1998 Heikki Tuuri
32 *******************************************************/
33 
34 #include "eval0proc.h"
35 
36 #ifdef UNIV_NONINL
37 #include "eval0proc.ic"
38 #endif
39 
40 /**********************************************************************//**
41 Performs an execution step of an if-statement node.
42 @return query thread to run next or NULL */
43 que_thr_t*
if_step(que_thr_t * thr)44 if_step(
45 /*====*/
46 	que_thr_t*	thr)	/*!< in: query thread */
47 {
48 	if_node_t*	node;
49 	elsif_node_t*	elsif_node;
50 
51 	ut_ad(thr);
52 
53 	node = static_cast<if_node_t*>(thr->run_node);
54 	ut_ad(que_node_get_type(node) == QUE_NODE_IF);
55 
56 	if (thr->prev_node == que_node_get_parent(node)) {
57 
58 		/* Evaluate the condition */
59 
60 		eval_exp(node->cond);
61 
62 		if (eval_node_get_ibool_val(node->cond)) {
63 
64 			/* The condition evaluated to TRUE: start execution
65 			from the first statement in the statement list */
66 
67 			thr->run_node = node->stat_list;
68 
69 		} else if (node->else_part) {
70 			thr->run_node = node->else_part;
71 
72 		} else if (node->elsif_list) {
73 			elsif_node = node->elsif_list;
74 
75 			for (;;) {
76 				eval_exp(elsif_node->cond);
77 
78 				if (eval_node_get_ibool_val(
79 					    elsif_node->cond)) {
80 
81 					/* The condition evaluated to TRUE:
82 					start execution from the first
83 					statement in the statement list */
84 
85 					thr->run_node = elsif_node->stat_list;
86 
87 					break;
88 				}
89 
90 				elsif_node = static_cast<elsif_node_t*>(
91 					que_node_get_next(elsif_node));
92 
93 				if (elsif_node == NULL) {
94 					thr->run_node = NULL;
95 
96 					break;
97 				}
98 			}
99 		} else {
100 			thr->run_node = NULL;
101 		}
102 	} else {
103 		/* Move to the next statement */
104 		ut_ad(que_node_get_next(thr->prev_node) == NULL);
105 
106 		thr->run_node = NULL;
107 	}
108 
109 	if (thr->run_node == NULL) {
110 		thr->run_node = que_node_get_parent(node);
111 	}
112 
113 	return(thr);
114 }
115 
116 /**********************************************************************//**
117 Performs an execution step of a while-statement node.
118 @return query thread to run next or NULL */
119 que_thr_t*
while_step(que_thr_t * thr)120 while_step(
121 /*=======*/
122 	que_thr_t*	thr)	/*!< in: query thread */
123 {
124 	while_node_t*	node;
125 
126 	ut_ad(thr);
127 
128 	node = static_cast<while_node_t*>(thr->run_node);
129 	ut_ad(que_node_get_type(node) == QUE_NODE_WHILE);
130 
131 	ut_ad((thr->prev_node == que_node_get_parent(node))
132 	      || (que_node_get_next(thr->prev_node) == NULL));
133 
134 	/* Evaluate the condition */
135 
136 	eval_exp(node->cond);
137 
138 	if (eval_node_get_ibool_val(node->cond)) {
139 
140 		/* The condition evaluated to TRUE: start execution
141 		from the first statement in the statement list */
142 
143 		thr->run_node = node->stat_list;
144 	} else {
145 		thr->run_node = que_node_get_parent(node);
146 	}
147 
148 	return(thr);
149 }
150 
151 /**********************************************************************//**
152 Performs an execution step of an assignment statement node.
153 @return query thread to run next or NULL */
154 que_thr_t*
assign_step(que_thr_t * thr)155 assign_step(
156 /*========*/
157 	que_thr_t*	thr)	/*!< in: query thread */
158 {
159 	assign_node_t*	node;
160 
161 	ut_ad(thr);
162 
163 	node = static_cast<assign_node_t*>(thr->run_node);
164 	ut_ad(que_node_get_type(node) == QUE_NODE_ASSIGNMENT);
165 
166 	/* Evaluate the value to assign */
167 
168 	eval_exp(node->val);
169 
170 	eval_node_copy_val(node->var->alias, node->val);
171 
172 	thr->run_node = que_node_get_parent(node);
173 
174 	return(thr);
175 }
176 
177 /**********************************************************************//**
178 Performs an execution step of a for-loop node.
179 @return query thread to run next or NULL */
180 que_thr_t*
for_step(que_thr_t * thr)181 for_step(
182 /*=====*/
183 	que_thr_t*	thr)	/*!< in: query thread */
184 {
185 	for_node_t*	node;
186 	que_node_t*	parent;
187 	lint		loop_var_value;
188 
189 	ut_ad(thr);
190 
191 	node = static_cast<for_node_t*>(thr->run_node);
192 
193 	ut_ad(que_node_get_type(node) == QUE_NODE_FOR);
194 
195 	parent = que_node_get_parent(node);
196 
197 	if (thr->prev_node != parent) {
198 
199 		/* Move to the next statement */
200 		thr->run_node = que_node_get_next(thr->prev_node);
201 
202 		if (thr->run_node != NULL) {
203 
204 			return(thr);
205 		}
206 
207 		/* Increment the value of loop_var */
208 
209 		loop_var_value = 1 + eval_node_get_int_val(node->loop_var);
210 	} else {
211 		/* Initialize the loop */
212 
213 		eval_exp(node->loop_start_limit);
214 		eval_exp(node->loop_end_limit);
215 
216 		loop_var_value = eval_node_get_int_val(node->loop_start_limit);
217 
218 		node->loop_end_value
219                   = (int) eval_node_get_int_val(node->loop_end_limit);
220 	}
221 
222 	/* Check if we should do another loop */
223 
224 	if (loop_var_value > node->loop_end_value) {
225 
226 		/* Enough loops done */
227 
228 		thr->run_node = parent;
229 	} else {
230 		eval_node_set_int_val(node->loop_var, loop_var_value);
231 
232 		thr->run_node = node->stat_list;
233 	}
234 
235 	return(thr);
236 }
237 
238 /**********************************************************************//**
239 Performs an execution step of an exit statement node.
240 @return query thread to run next or NULL */
241 que_thr_t*
exit_step(que_thr_t * thr)242 exit_step(
243 /*======*/
244 	que_thr_t*	thr)	/*!< in: query thread */
245 {
246 	exit_node_t*	node;
247 	que_node_t*	loop_node;
248 
249 	ut_ad(thr);
250 
251 	node = static_cast<exit_node_t*>(thr->run_node);
252 
253 	ut_ad(que_node_get_type(node) == QUE_NODE_EXIT);
254 
255 	/* Loops exit by setting thr->run_node as the loop node's parent, so
256 	find our containing loop node and get its parent. */
257 
258 	loop_node = que_node_get_containing_loop_node(node);
259 
260 	/* If someone uses an EXIT statement outside of a loop, this will
261 	trigger. */
262 	ut_a(loop_node);
263 
264 	thr->run_node = que_node_get_parent(loop_node);
265 
266 	return(thr);
267 }
268 
269 /**********************************************************************//**
270 Performs an execution step of a return-statement node.
271 @return query thread to run next or NULL */
272 que_thr_t*
return_step(que_thr_t * thr)273 return_step(
274 /*========*/
275 	que_thr_t*	thr)	/*!< in: query thread */
276 {
277 	return_node_t*	node;
278 	que_node_t*	parent;
279 
280 	ut_ad(thr);
281 
282 	node = static_cast<return_node_t*>(thr->run_node);
283 
284 	ut_ad(que_node_get_type(node) == QUE_NODE_RETURN);
285 
286 	parent = node;
287 
288 	while (que_node_get_type(parent) != QUE_NODE_PROC) {
289 
290 		parent = que_node_get_parent(parent);
291 	}
292 
293 	ut_a(parent);
294 
295 	thr->run_node = que_node_get_parent(parent);
296 
297 	return(thr);
298 }
299