eval0proc.cc 6.7 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1 2
/*****************************************************************************

3
Copyright (c) 1998, 2011, Oracle and/or its affiliates. All Rights Reserved.
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4 5 6 7 8 9 10 11 12 13

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
14 15
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
Vadim Tkachenko's avatar
Vadim Tkachenko committed
16 17 18

*****************************************************************************/

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
19
/**************************************************//**
20
@file eval/eval0proc.cc
21 22 23 24 25 26 27 28 29 30 31
Executes SQL stored procedures and their control structures

Created 1/20/1998 Heikki Tuuri
*******************************************************/

#include "eval0proc.h"

#ifdef UNIV_NONINL
#include "eval0proc.ic"
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
32 33 34
/**********************************************************************//**
Performs an execution step of an if-statement node.
@return	query thread to run next or NULL */
35 36 37 38
UNIV_INTERN
que_thr_t*
if_step(
/*====*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
39
	que_thr_t*	thr)	/*!< in: query thread */
40 41 42 43 44 45
{
	if_node_t*	node;
	elsif_node_t*	elsif_node;

	ut_ad(thr);

46
	node = static_cast<if_node_t*>(thr->run_node);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	ut_ad(que_node_get_type(node) == QUE_NODE_IF);

	if (thr->prev_node == que_node_get_parent(node)) {

		/* Evaluate the condition */

		eval_exp(node->cond);

		if (eval_node_get_ibool_val(node->cond)) {

			/* The condition evaluated to TRUE: start execution
			from the first statement in the statement list */

			thr->run_node = node->stat_list;

		} else if (node->else_part) {
			thr->run_node = node->else_part;

		} else if (node->elsif_list) {
			elsif_node = node->elsif_list;

			for (;;) {
				eval_exp(elsif_node->cond);

				if (eval_node_get_ibool_val(
					    elsif_node->cond)) {

					/* The condition evaluated to TRUE:
					start execution from the first
					statement in the statement list */

					thr->run_node = elsif_node->stat_list;

					break;
				}

83 84
				elsif_node = static_cast<elsif_node_t*>(
					que_node_get_next(elsif_node));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

				if (elsif_node == NULL) {
					thr->run_node = NULL;

					break;
				}
			}
		} else {
			thr->run_node = NULL;
		}
	} else {
		/* Move to the next statement */
		ut_ad(que_node_get_next(thr->prev_node) == NULL);

		thr->run_node = NULL;
	}

	if (thr->run_node == NULL) {
		thr->run_node = que_node_get_parent(node);
	}

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
109 110 111
/**********************************************************************//**
Performs an execution step of a while-statement node.
@return	query thread to run next or NULL */
112 113 114 115
UNIV_INTERN
que_thr_t*
while_step(
/*=======*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
116
	que_thr_t*	thr)	/*!< in: query thread */
117 118 119 120 121
{
	while_node_t*	node;

	ut_ad(thr);

122
	node = static_cast<while_node_t*>(thr->run_node);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	ut_ad(que_node_get_type(node) == QUE_NODE_WHILE);

	ut_ad((thr->prev_node == que_node_get_parent(node))
	      || (que_node_get_next(thr->prev_node) == NULL));

	/* Evaluate the condition */

	eval_exp(node->cond);

	if (eval_node_get_ibool_val(node->cond)) {

		/* The condition evaluated to TRUE: start execution
		from the first statement in the statement list */

		thr->run_node = node->stat_list;
	} else {
		thr->run_node = que_node_get_parent(node);
	}

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
145 146 147
/**********************************************************************//**
Performs an execution step of an assignment statement node.
@return	query thread to run next or NULL */
148 149 150 151
UNIV_INTERN
que_thr_t*
assign_step(
/*========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
152
	que_thr_t*	thr)	/*!< in: query thread */
153 154 155 156 157
{
	assign_node_t*	node;

	ut_ad(thr);

158
	node = static_cast<assign_node_t*>(thr->run_node);
159 160 161 162 163 164 165 166 167 168 169 170 171
	ut_ad(que_node_get_type(node) == QUE_NODE_ASSIGNMENT);

	/* Evaluate the value to assign */

	eval_exp(node->val);

	eval_node_copy_val(node->var->alias, node->val);

	thr->run_node = que_node_get_parent(node);

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
172 173 174
/**********************************************************************//**
Performs an execution step of a for-loop node.
@return	query thread to run next or NULL */
175 176 177 178
UNIV_INTERN
que_thr_t*
for_step(
/*=====*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
179
	que_thr_t*	thr)	/*!< in: query thread */
180 181 182 183 184 185 186
{
	for_node_t*	node;
	que_node_t*	parent;
	lint		loop_var_value;

	ut_ad(thr);

187
	node = static_cast<for_node_t*>(thr->run_node);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	ut_ad(que_node_get_type(node) == QUE_NODE_FOR);

	parent = que_node_get_parent(node);

	if (thr->prev_node != parent) {

		/* Move to the next statement */
		thr->run_node = que_node_get_next(thr->prev_node);

		if (thr->run_node != NULL) {

			return(thr);
		}

		/* Increment the value of loop_var */

		loop_var_value = 1 + eval_node_get_int_val(node->loop_var);
	} else {
		/* Initialize the loop */

		eval_exp(node->loop_start_limit);
		eval_exp(node->loop_end_limit);

		loop_var_value = eval_node_get_int_val(node->loop_start_limit);

		node->loop_end_value
                  = (int) eval_node_get_int_val(node->loop_end_limit);
	}

	/* Check if we should do another loop */

	if (loop_var_value > node->loop_end_value) {

		/* Enough loops done */

		thr->run_node = parent;
	} else {
		eval_node_set_int_val(node->loop_var, loop_var_value);

		thr->run_node = node->stat_list;
	}

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
234 235 236
/**********************************************************************//**
Performs an execution step of an exit statement node.
@return	query thread to run next or NULL */
237 238 239 240
UNIV_INTERN
que_thr_t*
exit_step(
/*======*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
241
	que_thr_t*	thr)	/*!< in: query thread */
242 243 244 245 246 247
{
	exit_node_t*	node;
	que_node_t*	loop_node;

	ut_ad(thr);

248
	node = static_cast<exit_node_t*>(thr->run_node);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

	ut_ad(que_node_get_type(node) == QUE_NODE_EXIT);

	/* Loops exit by setting thr->run_node as the loop node's parent, so
	find our containing loop node and get its parent. */

	loop_node = que_node_get_containing_loop_node(node);

	/* If someone uses an EXIT statement outside of a loop, this will
	trigger. */
	ut_a(loop_node);

	thr->run_node = que_node_get_parent(loop_node);

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
266 267 268
/**********************************************************************//**
Performs an execution step of a return-statement node.
@return	query thread to run next or NULL */
269 270 271 272
UNIV_INTERN
que_thr_t*
return_step(
/*========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
273
	que_thr_t*	thr)	/*!< in: query thread */
274 275 276 277 278 279
{
	return_node_t*	node;
	que_node_t*	parent;

	ut_ad(thr);

280
	node = static_cast<return_node_t*>(thr->run_node);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

	ut_ad(que_node_get_type(node) == QUE_NODE_RETURN);

	parent = node;

	while (que_node_get_type(parent) != QUE_NODE_PROC) {

		parent = que_node_get_parent(parent);
	}

	ut_a(parent);

	thr->run_node = que_node_get_parent(parent);

	return(thr);
}