| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1998, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work 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 either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 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 |
#include <stddef.h> |
| 37 |
|
| 38 |
/** Performs an execution step of an if-statement node. |
| 39 |
@return query thread to run next or NULL */ |
| 40 |
que_thr_t *if_step(que_thr_t *thr) /*!< in: query thread */ |
| 41 |
{ |
| 42 |
if_node_t *node; |
| 43 |
elsif_node_t *elsif_node; |
| 44 |
|
| 45 |
ut_ad(thr); |
| 46 |
|
| 47 |
node = static_cast<if_node_t *>(thr->run_node); |
| 48 |
ut_ad(que_node_get_type(node) == QUE_NODE_IF); |
| 49 |
|
| 50 |
if (thr->prev_node == que_node_get_parent(node)) { |
| 51 |
/* Evaluate the condition */ |
| 52 |
|
| 53 |
eval_exp(node->cond); |
| 54 |
|
| 55 |
if (eval_node_get_bool_val(node->cond)) { |
| 56 |
/* The condition evaluated to true: start execution |
| 57 |
from the first statement in the statement list */ |
| 58 |
|
| 59 |
thr->run_node = node->stat_list; |
| 60 |
|
| 61 |
} else if (node->else_part) { |
| 62 |
thr->run_node = node->else_part; |
| 63 |
|
| 64 |
} else if (node->elsif_list) { |
| 65 |
elsif_node = node->elsif_list; |
| 66 |
|
| 67 |
for (;;) { |
| 68 |
eval_exp(elsif_node->cond); |
| 69 |
|
| 70 |
if (eval_node_get_bool_val(elsif_node->cond)) { |
| 71 |
/* The condition evaluated to true: |
| 72 |
start execution from the first |
| 73 |
statement in the statement list */ |
| 74 |
|
| 75 |
thr->run_node = elsif_node->stat_list; |
| 76 |
|
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
elsif_node = static_cast<elsif_node_t *>(que_node_get_next(elsif_node)); |
| 81 |
|
| 82 |
if (elsif_node == nullptr) { |
| 83 |
thr->run_node = nullptr; |
| 84 |
|
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
} else { |
| 89 |
thr->run_node = nullptr; |
| 90 |
} |
| 91 |
} else { |
| 92 |
/* Move to the next statement */ |
| 93 |
ut_ad(que_node_get_next(thr->prev_node) == nullptr); |
| 94 |
|
| 95 |
thr->run_node = nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
if (thr->run_node == nullptr) { |
| 99 |
thr->run_node = que_node_get_parent(node); |
| 100 |
} |
| 101 |
|
| 102 |
return (thr); |
| 103 |
} |
| 104 |
|
| 105 |
/** Performs an execution step of a while-statement node. |
| 106 |
@return query thread to run next or NULL */ |
| 107 |
que_thr_t *while_step(que_thr_t *thr) /*!< in: query thread */ |
| 108 |
{ |
| 109 |
while_node_t *node; |
| 110 |
|
| 111 |
ut_ad(thr); |
| 112 |
|
| 113 |
node = static_cast<while_node_t *>(thr->run_node); |
| 114 |
ut_ad(que_node_get_type(node) == QUE_NODE_WHILE); |
| 115 |
|
| 116 |
ut_ad((thr->prev_node == que_node_get_parent(node)) || |
| 117 |
(que_node_get_next(thr->prev_node) == nullptr)); |
| 118 |
|
| 119 |
/* Evaluate the condition */ |
| 120 |
|
| 121 |
eval_exp(node->cond); |
| 122 |
|
| 123 |
if (eval_node_get_bool_val(node->cond)) { |
| 124 |
/* The condition evaluated to true: start execution |
| 125 |
from the first statement in the statement list */ |
| 126 |
|
| 127 |
thr->run_node = node->stat_list; |
| 128 |
} else { |
| 129 |
thr->run_node = que_node_get_parent(node); |
| 130 |
} |
| 131 |
|
| 132 |
return (thr); |
| 133 |
} |
| 134 |
|
| 135 |
/** Performs an execution step of an assignment statement node. |
| 136 |
@return query thread to run next or NULL */ |
| 137 |
que_thr_t *assign_step(que_thr_t *thr) /*!< in: query thread */ |
| 138 |
{ |
| 139 |
assign_node_t *node; |
| 140 |
|
| 141 |
ut_ad(thr); |
| 142 |
|
| 143 |
node = static_cast<assign_node_t *>(thr->run_node); |
| 144 |
ut_ad(que_node_get_type(node) == QUE_NODE_ASSIGNMENT); |
| 145 |
|
| 146 |
/* Evaluate the value to assign */ |
| 147 |
|
| 148 |
eval_exp(node->val); |
| 149 |
|
| 150 |
eval_node_copy_val(node->var->alias, node->val); |
| 151 |
|
| 152 |
thr->run_node = que_node_get_parent(node); |
| 153 |
|
| 154 |
return (thr); |
| 155 |
} |
| 156 |
|
| 157 |
/** Performs an execution step of a for-loop node. |
| 158 |
@return query thread to run next or NULL */ |
| 159 |
que_thr_t *for_step(que_thr_t *thr) /*!< in: query thread */ |
| 160 |
{ |
| 161 |
for_node_t *node; |
| 162 |
que_node_t *parent; |
| 163 |
lint loop_var_value; |
| 164 |
|
| 165 |
ut_ad(thr); |
| 166 |
|
| 167 |
node = static_cast<for_node_t *>(thr->run_node); |
| 168 |
|
| 169 |
ut_ad(que_node_get_type(node) == QUE_NODE_FOR); |
| 170 |
|
| 171 |
parent = que_node_get_parent(node); |
| 172 |
|
| 173 |
if (thr->prev_node != parent) { |
| 174 |
/* Move to the next statement */ |
| 175 |
thr->run_node = que_node_get_next(thr->prev_node); |
| 176 |
|
| 177 |
if (thr->run_node != nullptr) { |
| 178 |
return (thr); |
| 179 |
} |
| 180 |
|
| 181 |
/* Increment the value of loop_var */ |
| 182 |
|
| 183 |
loop_var_value = 1 + eval_node_get_int_val(node->loop_var); |
| 184 |
} else { |
| 185 |
/* Initialize the loop */ |
| 186 |
|
| 187 |
eval_exp(node->loop_start_limit); |
| 188 |
eval_exp(node->loop_end_limit); |
| 189 |
|
| 190 |
loop_var_value = eval_node_get_int_val(node->loop_start_limit); |
| 191 |
|
| 192 |
node->loop_end_value = (int)eval_node_get_int_val(node->loop_end_limit); |
| 193 |
} |
| 194 |
|
| 195 |
/* Check if we should do another loop */ |
| 196 |
|
| 197 |
if (loop_var_value > node->loop_end_value) { |
| 198 |
/* Enough loops done */ |
| 199 |
|
| 200 |
thr->run_node = parent; |
| 201 |
} else { |
| 202 |
eval_node_set_int_val(node->loop_var, loop_var_value); |
| 203 |
|
| 204 |
thr->run_node = node->stat_list; |
| 205 |
} |
| 206 |
|
| 207 |
return (thr); |
| 208 |
} |
| 209 |
|
| 210 |
/** Performs an execution step of an exit statement node. |
| 211 |
@return query thread to run next or NULL */ |
| 212 |
que_thr_t *exit_step(que_thr_t *thr) /*!< in: query thread */ |
| 213 |
{ |
| 214 |
exit_node_t *node; |
| 215 |
que_node_t *loop_node; |
| 216 |
|
| 217 |
ut_ad(thr); |
| 218 |
|
| 219 |
node = static_cast<exit_node_t *>(thr->run_node); |
| 220 |
|
| 221 |
ut_ad(que_node_get_type(node) == QUE_NODE_EXIT); |
| 222 |
|
| 223 |
/* Loops exit by setting thr->run_node as the loop node's parent, so |
| 224 |
find our containing loop node and get its parent. */ |
| 225 |
|
| 226 |
loop_node = que_node_get_containing_loop_node(node); |
| 227 |
|
| 228 |
/* If someone uses an EXIT statement outside of a loop, this will |
| 229 |
trigger. */ |
| 230 |
ut_a(loop_node); |
| 231 |
|
| 232 |
thr->run_node = que_node_get_parent(loop_node); |
| 233 |
|
| 234 |
return (thr); |
| 235 |
} |
| 236 |
|
| 237 |
/** Performs an execution step of a return-statement node. |
| 238 |
@return query thread to run next or NULL */ |
| 239 |
que_thr_t *return_step(que_thr_t *thr) /*!< in: query thread */ |
| 240 |
{ |
| 241 |
return_node_t *node; |
| 242 |
que_node_t *parent; |
| 243 |
|
| 244 |
ut_ad(thr); |
| 245 |
|
| 246 |
node = static_cast<return_node_t *>(thr->run_node); |
| 247 |
|
| 248 |
ut_ad(que_node_get_type(node) == QUE_NODE_RETURN); |
| 249 |
|
| 250 |
parent = node; |
| 251 |
|
| 252 |
while (que_node_get_type(parent) != QUE_NODE_PROC) { |
| 253 |
parent = que_node_get_parent(parent); |
| 254 |
} |
| 255 |
|
| 256 |
ut_a(parent); |
| 257 |
|
| 258 |
thr->run_node = que_node_get_parent(parent); |
| 259 |
|
| 260 |
return (thr); |
| 261 |
} |