1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
/* Copyright (c) 2002, 2010, Oracle and/or its affiliates. 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <my_global.h>
#include "sql_priv.h"
#include "unireg.h"
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation
#endif
#include "sp_pcontext.h"
#include "sp_head.h"
bool sp_condition_value::equals(const sp_condition_value *cv) const
{
DBUG_ASSERT(cv);
/*
The following test disallows duplicate handlers,
including user defined exceptions with the same WHEN clause:
DECLARE
a EXCEPTION;
b EXCEPTION;
BEGIN
RAUSE a;
EXCEPTION
WHEN a THEN RETURN 'a0';
WHEN a THEN RETURN 'a1';
END
*/
if (this == cv)
return true;
/*
The test below considers two conditions of the same type as equal
(except for the user defined exceptions) to avoid declaring duplicate
handlers.
All user defined conditions have type==SQLSTATE
with the same SQL state and error code.
It's OK to have multiple user defined conditions:
DECLARE
a EXCEPTION;
b EXCEPTION;
BEGIN
RAISE a;
EXCEPTION
WHEN a THEN RETURN 'a';
WHEN b THEN RETURN 'b';
END;
*/
if (type != cv->type || m_is_user_defined || cv->m_is_user_defined)
return false;
switch (type)
{
case sp_condition_value::ERROR_CODE:
return (get_sql_errno() == cv->get_sql_errno());
case sp_condition_value::SQLSTATE:
return Sql_state::eq(cv);
default:
return true;
}
}
void sp_pcontext::init(uint var_offset,
uint cursor_offset,
int num_case_expressions)
{
m_var_offset= var_offset;
m_cursor_offset= cursor_offset;
m_num_case_exprs= num_case_expressions;
m_labels.empty();
m_goto_labels.empty();
}
sp_pcontext::sp_pcontext()
: Sql_alloc(),
m_max_var_index(0), m_max_cursor_index(0),
m_parent(NULL), m_pboundary(0),
m_scope(REGULAR_SCOPE)
{
init(0, 0, 0);
}
sp_pcontext::sp_pcontext(sp_pcontext *prev, sp_pcontext::enum_scope scope)
: Sql_alloc(),
m_max_var_index(0), m_max_cursor_index(0),
m_parent(prev), m_pboundary(0),
m_scope(scope)
{
init(prev->m_var_offset + prev->m_max_var_index,
prev->current_cursor_count(),
prev->get_num_case_exprs());
}
sp_pcontext::~sp_pcontext()
{
for (size_t i= 0; i < m_children.elements(); ++i)
delete m_children.at(i);
}
sp_pcontext *sp_pcontext::push_context(THD *thd, sp_pcontext::enum_scope scope)
{
sp_pcontext *child= new (thd->mem_root) sp_pcontext(this, scope);
if (child)
m_children.append(child);
return child;
}
bool cmp_labels(sp_label *a, sp_label *b)
{
return (my_strcasecmp(system_charset_info, a->name.str, b->name.str) == 0
&& a->type == b->type);
}
sp_pcontext *sp_pcontext::pop_context()
{
m_parent->m_max_var_index+= m_max_var_index;
uint submax= max_cursor_index();
if (submax > m_parent->m_max_cursor_index)
m_parent->m_max_cursor_index= submax;
if (m_num_case_exprs > m_parent->m_num_case_exprs)
m_parent->m_num_case_exprs= m_num_case_exprs;
/*
** Push unresolved goto label to parent context
*/
sp_label *label;
List_iterator_fast<sp_label> li(m_goto_labels);
while ((label= li++))
{
if (label->ip == 0)
{
m_parent->m_goto_labels.add_unique(label, &cmp_labels);
}
}
return m_parent;
}
uint sp_pcontext::diff_handlers(const sp_pcontext *ctx, bool exclusive) const
{
uint n= 0;
const sp_pcontext *pctx= this;
const sp_pcontext *last_ctx= NULL;
while (pctx && pctx != ctx)
{
n+= pctx->m_handlers.elements();
last_ctx= pctx;
pctx= pctx->parent_context();
}
if (pctx)
return (exclusive && last_ctx ? n - last_ctx->m_handlers.elements() : n);
return 0; // Didn't find ctx
}
uint sp_pcontext::diff_cursors(const sp_pcontext *ctx, bool exclusive) const
{
uint n= 0;
const sp_pcontext *pctx= this;
const sp_pcontext *last_ctx= NULL;
while (pctx && pctx != ctx)
{
n+= pctx->m_cursors.elements();
last_ctx= pctx;
pctx= pctx->parent_context();
}
if (pctx)
return (exclusive && last_ctx ? n - last_ctx->m_cursors.elements() : n);
return 0; // Didn't find ctx
}
sp_variable *sp_pcontext::find_variable(const LEX_CSTRING *name,
bool current_scope_only) const
{
uint i= m_vars.elements() - m_pboundary;
while (i--)
{
sp_variable *p= m_vars.at(i);
if (my_strnncoll(system_charset_info,
(const uchar *)name->str, name->length,
(const uchar *)p->name.str, p->name.length) == 0)
{
return p;
}
}
return (!current_scope_only && m_parent) ?
m_parent->find_variable(name, false) :
NULL;
}
/*
Find a variable by its run-time offset.
If the variable with a desired run-time offset is not found in this
context frame, it's recursively searched on parent context frames.
Note, context frames can have holes:
CREATE PROCEDURE p1() AS
x0 INT:=100;
CURSOR cur(p0 INT, p1 INT) IS SELECT p0, p1;
x1 INT:=101;
BEGIN
...
END;
The variables (x0 and x1) and the cursor parameters (p0 and p1)
reside in separate parse context frames.
The variables reside on the top level parse context frame:
- x0 has frame offset 0 and run-time offset 0
- x1 has frame offset 1 and run-time offset 3
The cursor parameters reside on the second level parse context frame:
- p0 has frame offset 0 and run-time offset 1
- p1 has frame offset 1 and run-time offset 2
Run-time offsets on a frame can have holes, but offsets monotonocally grow,
so run-time offsets of all variables are not greater than the run-time offset
of the very last variable in this frame.
*/
sp_variable *sp_pcontext::find_variable(uint offset) const
{
if (m_var_offset <= offset &&
m_vars.elements() &&
offset <= get_last_context_variable()->offset)
{
for (uint i= 0; i < m_vars.elements(); i++)
{
if (m_vars.at(i)->offset == offset)
return m_vars.at(i); // This frame
}
}
return m_parent ?
m_parent->find_variable(offset) : // Some previous frame
NULL; // Index out of bounds
}
sp_variable *sp_pcontext::add_variable(THD *thd, const LEX_CSTRING *name)
{
sp_variable *p=
new (thd->mem_root) sp_variable(name, m_var_offset + m_max_var_index);
if (!p)
return NULL;
++m_max_var_index;
return m_vars.append(p) ? NULL : p;
}
sp_label *sp_pcontext::push_label(THD *thd, const LEX_CSTRING *name, uint ip,
sp_label::enum_type type,
List<sp_label> *list)
{
sp_label *label=
new (thd->mem_root) sp_label(name, ip, type, this);
if (!label)
return NULL;
list->push_front(label, thd->mem_root);
return label;
}
sp_label *sp_pcontext::find_goto_label(const LEX_CSTRING *name, bool recusive)
{
List_iterator_fast<sp_label> li(m_goto_labels);
sp_label *lab;
while ((lab= li++))
{
if (my_strcasecmp(system_charset_info, name->str, lab->name.str) == 0)
return lab;
}
if (!recusive)
return NULL;
/*
Note about exception handlers.
See SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003),
section 13.1 <compound statement>,
syntax rule 4.
In short, a DECLARE HANDLER block can not refer
to labels from the parent context, as they are out of scope.
*/
if (m_scope == HANDLER_SCOPE && m_parent)
{
if (m_parent->m_parent)
{
// Skip the parent context
return m_parent->m_parent->find_goto_label(name);
}
}
return m_parent && (m_scope == REGULAR_SCOPE) ?
m_parent->find_goto_label(name) :
NULL;
}
sp_label *sp_pcontext::find_label(const LEX_CSTRING *name)
{
List_iterator_fast<sp_label> li(m_labels);
sp_label *lab;
while ((lab= li++))
{
if (my_strcasecmp(system_charset_info, name->str, lab->name.str) == 0)
return lab;
}
/*
Note about exception handlers.
See SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003),
section 13.1 <compound statement>,
syntax rule 4.
In short, a DECLARE HANDLER block can not refer
to labels from the parent context, as they are out of scope.
*/
return (m_parent && (m_scope == REGULAR_SCOPE)) ?
m_parent->find_label(name) :
NULL;
}
sp_label *sp_pcontext::find_label_current_loop_start()
{
List_iterator_fast<sp_label> li(m_labels);
sp_label *lab;
while ((lab= li++))
{
if (lab->type == sp_label::ITERATION)
return lab;
}
// See a comment in sp_pcontext::find_label()
return (m_parent && (m_scope == REGULAR_SCOPE)) ?
m_parent->find_label_current_loop_start() :
NULL;
}
bool sp_pcontext::add_condition(THD *thd,
const LEX_CSTRING *name,
sp_condition_value *value)
{
sp_condition *p= new (thd->mem_root) sp_condition(name, value);
if (p == NULL)
return true;
return m_conditions.append(p);
}
sp_condition_value *sp_pcontext::find_condition(const LEX_CSTRING *name,
bool current_scope_only) const
{
uint i= m_conditions.elements();
while (i--)
{
sp_condition *p= m_conditions.at(i);
if (p->eq_name(name))
{
return p->value;
}
}
return (!current_scope_only && m_parent) ?
m_parent->find_condition(name, false) :
NULL;
}
static sp_condition_value
// Warnings
cond_no_data_found(ER_SP_FETCH_NO_DATA, "01000"),
// Errors
cond_invalid_cursor(ER_SP_CURSOR_NOT_OPEN, "24000"),
cond_dup_val_on_index(ER_DUP_ENTRY, "23000"),
cond_too_many_rows(ER_TOO_MANY_ROWS, "42000");
static sp_condition sp_predefined_conditions[]=
{
// Warnings
sp_condition(C_STRING_WITH_LEN("NO_DATA_FOUND"), &cond_no_data_found),
// Errors
sp_condition(C_STRING_WITH_LEN("INVALID_CURSOR"), &cond_invalid_cursor),
sp_condition(C_STRING_WITH_LEN("DUP_VAL_ON_INDEX"), &cond_dup_val_on_index),
sp_condition(C_STRING_WITH_LEN("TOO_MANY_ROWS"), &cond_too_many_rows)
};
sp_condition_value *
sp_pcontext::find_predefined_condition(const LEX_CSTRING *name) const
{
for (uint i= 0; i < array_elements(sp_predefined_conditions) ; i++)
{
if (sp_predefined_conditions[i].eq_name(name))
return sp_predefined_conditions[i].value;
}
return NULL;
}
sp_handler *sp_pcontext::add_handler(THD *thd,
sp_handler::enum_type type)
{
sp_handler *h= new (thd->mem_root) sp_handler(type);
if (!h)
return NULL;
return m_handlers.append(h) ? NULL : h;
}
bool sp_pcontext::check_duplicate_handler(
const sp_condition_value *cond_value) const
{
for (size_t i= 0; i < m_handlers.elements(); ++i)
{
sp_handler *h= m_handlers.at(i);
List_iterator_fast<sp_condition_value> li(h->condition_values);
sp_condition_value *cv;
while ((cv= li++))
{
if (cond_value->equals(cv))
return true;
}
}
return false;
}
bool sp_condition_value::matches(const Sql_condition_identity &value,
const sp_condition_value *found_cv) const
{
bool user_value_matched= !value.get_user_condition_value() ||
this == value.get_user_condition_value();
switch (type)
{
case sp_condition_value::ERROR_CODE:
return user_value_matched &&
value.get_sql_errno() == get_sql_errno() &&
(!found_cv || found_cv->type > sp_condition_value::ERROR_CODE);
case sp_condition_value::SQLSTATE:
return user_value_matched &&
Sql_state::eq(&value) &&
(!found_cv || found_cv->type > sp_condition_value::SQLSTATE);
case sp_condition_value::WARNING:
return user_value_matched &&
(value.Sql_state::is_warning() ||
value.get_level() == Sql_condition::WARN_LEVEL_WARN) &&
!found_cv;
case sp_condition_value::NOT_FOUND:
return user_value_matched &&
value.Sql_state::is_not_found() &&
!found_cv;
case sp_condition_value::EXCEPTION:
/*
In sql_mode=ORACLE this construct should catch both errors and warnings:
EXCEPTION
WHEN OTHERS THEN ...;
E.g. NO_DATA_FOUND is more like a warning than an error,
and it should be caught.
We don't check user_value_matched here.
"WHEN OTHERS" catches all user defined exception.
*/
return (((current_thd->variables.sql_mode & MODE_ORACLE) ||
(value.Sql_state::is_exception() &&
value.get_level() == Sql_condition::WARN_LEVEL_ERROR)) &&
!found_cv);
}
return false;
}
sp_handler*
sp_pcontext::find_handler(const Sql_condition_identity &value) const
{
sp_handler *found_handler= NULL;
sp_condition_value *found_cv= NULL;
for (size_t i= 0; i < m_handlers.elements(); ++i)
{
sp_handler *h= m_handlers.at(i);
List_iterator_fast<sp_condition_value> li(h->condition_values);
sp_condition_value *cv;
while ((cv= li++))
{
if (cv->matches(value, found_cv))
{
found_cv= cv;
found_handler= h;
}
}
}
if (found_handler)
return found_handler;
// There is no appropriate handler in this parsing context. We need to look up
// in parent contexts. There might be two cases here:
//
// 1. The current context has REGULAR_SCOPE. That means, it's a simple
// BEGIN..END block:
// ...
// BEGIN
// ... # We're here.
// END
// ...
// In this case we simply call find_handler() on parent's context recursively.
//
// 2. The current context has HANDLER_SCOPE. That means, we're inside an
// SQL-handler block:
// ...
// DECLARE ... HANDLER FOR ...
// BEGIN
// ... # We're here.
// END
// ...
// In this case we can not just call parent's find_handler(), because
// parent's handler don't catch conditions from this scope. Instead, we should
// try to find first parent context (we might have nested handler
// declarations), which has REGULAR_SCOPE (i.e. which is regular BEGIN..END
// block).
const sp_pcontext *p= this;
while (p && p->m_scope == HANDLER_SCOPE)
p= p->m_parent;
if (!p || !p->m_parent)
return NULL;
return p->m_parent->find_handler(value);
}
bool sp_pcontext::add_cursor(const LEX_CSTRING *name, sp_pcontext *param_ctx,
sp_lex_cursor *lex)
{
if (m_cursors.elements() == m_max_cursor_index)
++m_max_cursor_index;
return m_cursors.append(sp_pcursor(name, param_ctx, lex));
}
const sp_pcursor *sp_pcontext::find_cursor(const LEX_CSTRING *name,
uint *poff,
bool current_scope_only) const
{
uint i= m_cursors.elements();
while (i--)
{
LEX_CSTRING n= m_cursors.at(i);
if (my_strnncoll(system_charset_info,
(const uchar *) name->str, name->length,
(const uchar *) n.str, n.length) == 0)
{
*poff= m_cursor_offset + i;
return &m_cursors.at(i);
}
}
return (!current_scope_only && m_parent) ?
m_parent->find_cursor(name, poff, false) :
NULL;
}
void sp_pcontext::retrieve_field_definitions(
List<Spvar_definition> *field_def_lst) const
{
/* Put local/context fields in the result list. */
size_t next_child= 0;
for (size_t i= 0; i < m_vars.elements(); ++i)
{
sp_variable *var_def= m_vars.at(i);
/*
The context can have holes in run-time offsets,
the missing offsets reside on the children contexts in such cases.
Example:
CREATE PROCEDURE p1() AS
x0 INT:=100; -- context 0, position 0, run-time 0
CURSOR cur(
p0 INT, -- context 1, position 0, run-time 1
p1 INT -- context 1, position 1, run-time 2
) IS SELECT p0, p1;
x1 INT:=101; -- context 0, position 1, run-time 3
BEGIN
...
END;
See more comments in sp_pcontext::find_variable().
We must retrieve the definitions in the order of their run-time offsets.
Check that there are children that should go before the current variable.
*/
for ( ; next_child < m_children.elements(); next_child++)
{
sp_pcontext *child= m_children.at(next_child);
if (!child->context_var_count() ||
child->get_context_variable(0)->offset > var_def->offset)
break;
/*
All variables on the embedded context (that fills holes of the parent)
should have the run-time offset strictly less than var_def.
*/
DBUG_ASSERT(child->get_context_variable(0)->offset < var_def->offset);
DBUG_ASSERT(child->get_last_context_variable()->offset < var_def->offset);
child->retrieve_field_definitions(field_def_lst);
}
field_def_lst->push_back(&var_def->field_def);
}
/* Put the fields of the remaining enclosed contexts in the result list. */
for (size_t i= next_child; i < m_children.elements(); ++i)
m_children.at(i)->retrieve_field_definitions(field_def_lst);
}
const sp_pcursor *sp_pcontext::find_cursor(uint offset) const
{
if (m_cursor_offset <= offset &&
offset < m_cursor_offset + m_cursors.elements())
{
return &m_cursors.at(offset - m_cursor_offset); // This frame
}
return m_parent ?
m_parent->find_cursor(offset) : // Some previous frame
NULL; // Index out of bounds
}
bool sp_pcursor::check_param_count_with_error(uint param_count) const
{
if (param_count != (m_param_context ?
m_param_context->context_var_count() : 0))
{
my_error(ER_WRONG_PARAMCOUNT_TO_CURSOR, MYF(0), LEX_CSTRING::str);
return true;
}
return false;
}
const Spvar_definition *
sp_variable::find_row_field(const LEX_CSTRING *var_name,
const LEX_CSTRING *field_name,
uint *row_field_offset)
{
if (!field_def.is_row())
{
my_printf_error(ER_UNKNOWN_ERROR,
"'%s' is not a row variable", MYF(0), var_name->str);
return NULL;
}
const Spvar_definition *def;
if ((def= field_def.find_row_field_by_name(field_name, row_field_offset)))
return def;
my_error(ER_ROW_VARIABLE_DOES_NOT_HAVE_FIELD, MYF(0),
var_name->str, field_name->str);
return NULL;
}
|