eval0proc.c 6.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*****************************************************************************

Copyright (c) 1998, 2009, Innobase Oy. All Rights Reserved.

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 St, Fifth Floor, Boston, MA 02110-1301 USA
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

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

/**************************************************//**
@file eval/eval0proc.c
Executes SQL stored procedures and their control structures

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

#include "eval0proc.h"

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

/**********************************************************************//**
Performs an execution step of an if-statement node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
if_step(
/*====*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	if_node_t*	node;
	elsif_node_t*	elsif_node;

	ut_ad(thr);

	node = thr->run_node;
	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;
				}

				elsif_node = que_node_get_next(elsif_node);

				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);
}

/**********************************************************************//**
Performs an execution step of a while-statement node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
while_step(
/*=======*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	while_node_t*	node;

	ut_ad(thr);

	node = thr->run_node;
	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);
}

/**********************************************************************//**
Performs an execution step of an assignment statement node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
assign_step(
/*========*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	assign_node_t*	node;

	ut_ad(thr);

	node = thr->run_node;
	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);
}

/**********************************************************************//**
Performs an execution step of a for-loop node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
for_step(
/*=====*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	for_node_t*	node;
	que_node_t*	parent;
	lint		loop_var_value;

	ut_ad(thr);

	node = thr->run_node;

	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);
}

/**********************************************************************//**
Performs an execution step of an exit statement node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
exit_step(
/*======*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	exit_node_t*	node;
	que_node_t*	loop_node;

	ut_ad(thr);

	node = thr->run_node;

	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);
}

/**********************************************************************//**
Performs an execution step of a return-statement node.
@return	query thread to run next or NULL */
UNIV_INTERN
que_thr_t*
return_step(
/*========*/
	que_thr_t*	thr)	/*!< in: query thread */
{
	return_node_t*	node;
	que_node_t*	parent;

	ut_ad(thr);

	node = thr->run_node;

	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);
}