/
sp_instr.cc
2099 lines (1741 loc) · 55.5 KB
/
sp_instr.cc
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sp_instr.h"
#include "opt_trace.h" // class Opt_trace_start
#include "sql_array.h" // class Dynamic_array
#include "sql_audit.h" // mysql_audit_general
#include "sql_base.h" // open_and_lock_tables
#include "sql_derived.h" // mysql_handle_derived
#include "sp_head.h" // class sp_head
#include "sql_parse.h" // check_table_access
#include "sp_rcontext.h" // class sp_rcontext
#include "sql_prepare.h" // reinit_stmt_before_use
#include "transaction.h" // trans_commit_stmt, trans_rollback_stmt, ...
/*
Sufficient max length of printed destinations.
*/
static const int SP_STMT_PRINT_MAXLEN= 40;
static int cmp_rqp_locations(Rewritable_query_parameter * const *a,
Rewritable_query_parameter * const *b)
{
return (int)((*a)->pos_in_query - (*b)->pos_in_query);
}
/*
StoredRoutinesBinlogging
This paragraph applies only to statement-based binlogging. Row-based
binlogging does not need anything special like this.
Top-down overview:
1. Statements
Statements that have is_update_query(stmt) == true are written into the
binary log verbatim.
Examples:
UPDATE tbl SET tbl.x = spfunc_w_side_effects()
UPDATE tbl SET tbl.x=1 WHERE spfunc_w_side_effect_that_returns_false(tbl.y)
Statements that have is_update_query(stmt) == false (e.g. SELECTs) are not
written into binary log. Instead we catch function calls the statement
makes and write it into binary log separately (see #3).
2. PROCEDURE calls
CALL statements are not written into binary log. Instead
* Any FUNCTION invocation (in SET, IF, WHILE, OPEN CURSOR and other SP
instructions) is written into binlog separately.
* Each statement executed in SP is binlogged separately, according to rules
in #1, with the exception that we modify query string: we replace uses
of SP local variables with NAME_CONST('spvar_name', <spvar-value>) calls.
This substitution is done in subst_spvars().
3. FUNCTION calls
In sp_head::execute_function(), we check
* If this function invocation is done from a statement that is written
into the binary log.
* If there were any attempts to write events to the binary log during
function execution (grep for start_union_events and stop_union_events)
If the answers are No and Yes, we write the function call into the binary
log as "SELECT spfunc(<param1value>, <param2value>, ...)"
4. Miscellaneous issues.
4.1 User variables.
When we call mysql_bin_log.write() for an SP statement, thd->user_var_events
must hold set<{var_name, value}> pairs for all user variables used during
the statement execution.
This set is produced by tracking user variable reads during statement
execution.
For SPs, this has the following implications:
1) thd->user_var_events may contain events from several SP statements and
needs to be valid after exection of these statements was finished. In
order to achieve that, we
* Allocate user_var_events array elements on appropriate mem_root (grep
for user_var_events_alloc).
* Use is_query_in_union() to determine if user_var_event is created.
2) We need to empty thd->user_var_events after we have wrote a function
call. This is currently done by making
reset_dynamic(&thd->user_var_events);
calls in several different places. (TODO cosider moving this into
mysql_bin_log.write() function)
4.2 Auto_increment storage in binlog
As we may write two statements to binlog from one single logical statement
(case of "SELECT func1(),func2()": it is binlogged as "SELECT func1()" and
then "SELECT func2()"), we need to reset auto_increment binlog variables
after each binlogged SELECT. Otherwise, the auto_increment value of the
first SELECT would be used for the second too.
*/
/**
Replace thd->query{_length} with a string that one can write to
the binlog.
The binlog-suitable string is produced by replacing references to SP local
variables with NAME_CONST('sp_var_name', value) calls.
@param thd Current thread.
@param instr Instruction (we look for Item_splocal instances in
instr->free_list)
@param query_str Original query string
@return
- false on success.
thd->query{_length} either has been appropriately replaced or there
is no need for replacements.
- true out of memory error.
*/
static bool
subst_spvars(THD *thd, sp_instr *instr, LEX_STRING *query_str)
{
DBUG_ENTER("subst_spvars");
Dynamic_array<Rewritable_query_parameter*> rewritables(PSI_INSTRUMENT_MEM);
char *pbuf;
StringBuffer<512> qbuf;
Copy_query_with_rewrite acc(thd, query_str->str, query_str->length, &qbuf);
/* Find rewritable Items used in this statement */
for (Item *item= instr->free_list; item; item= item->next)
{
Rewritable_query_parameter *rqp= item->get_rewritable_query_parameter();
if (rqp && rqp->pos_in_query)
rewritables.append(rqp);
}
if (!rewritables.elements())
DBUG_RETURN(false);
rewritables.sort(cmp_rqp_locations);
thd->query_name_consts= (uint)rewritables.elements();
for (Rewritable_query_parameter **rqp= rewritables.front();
rqp <= rewritables.back(); rqp++)
{
if (acc.append(*rqp))
DBUG_RETURN(true);
}
if (acc.finalize())
DBUG_RETURN(true);
/*
Allocate additional space at the end of the new query string for the
query_cache_send_result_to_client function.
The query buffer layout is:
buffer :==
<statement> The input statement(s)
'\0' Terminating null char
<length> Length of following current database name 2
<db_name> Name of current database
<flags> Flags struct
*/
size_t buf_len= (qbuf.length() + 1 + QUERY_CACHE_DB_LENGTH_SIZE +
thd->db.length + QUERY_CACHE_FLAGS_SIZE + 1);
if ((pbuf= (char *) alloc_root(thd->mem_root, buf_len)))
{
char *ptr= pbuf + qbuf.length();
memcpy(pbuf, qbuf.ptr(), qbuf.length());
*ptr= 0;
int2store(ptr+1, thd->db.length);
}
else
DBUG_RETURN(true);
thd->set_query(pbuf, qbuf.length());
DBUG_RETURN(false);
}
/**
Prepare LEX and thread for execution of instruction, if requested open
and lock LEX's tables, execute instruction's core function, perform
cleanup afterwards.
@param thd thread context
@param nextp out - next instruction
@param open_tables if true then check read access to tables in LEX's table
list and open and lock them (used in instructions which
need to calculate some expression and don't execute
complete statement).
@param instr instruction for which we prepare context, and which core
function execute by calling its exec_core() method.
@param rerun_the_same_instr true in case the instruction is re-run after
a SQL statement associated with it has been
re-parsed.
@note
We are not saving/restoring some parts of THD which may need this because
we do this once for whole routine execution in sp_head::execute().
@return
0/non-0 - Success/Failure
*/
int
sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp,
bool open_tables, sp_instr* instr,
bool rerun_the_same_instr)
{
int res= 0;
DBUG_ENTER("reset_lex_and_exec_core");
/*
The flag is saved at the entry to the following substatement.
It's reset further in the common code part.
It's merged with the saved parent's value at the exit of this func.
*/
bool parent_modified_non_trans_table=
thd->transaction->stmt.modified_non_trans_table;
unsigned int parent_unsafe_rollback_flags=
thd->transaction->stmt.m_unsafe_rollback_flags;
thd->transaction->stmt.modified_non_trans_table= false;
thd->transaction->stmt.m_unsafe_rollback_flags= 0;
DBUG_ASSERT(!thd->derived_tables);
DBUG_ASSERT(thd->Item_change_list::is_empty());
/*
Use our own lex.
We should not save old value since it is saved/restored in
sp_head::execute() when we are entering/leaving routine.
*/
thd->lex= m_lex;
/*
If the instruction is re-run by a reason of metadata change, then re-use
current query id rather than set a new one. Doing this way we retain
warnings generated on running the SP instruction. If a new query id was set
it would result in clearing all accumulated warnings in
mysql_execute_command
on calling
thd->get_stmt_da()->opt_clear_warning_info(thd->query_id)
since in this case Warning_info::m_warn_id != thd->query_id.
@sa Warning_info::opt_clear()
*/
if (!rerun_the_same_instr)
thd->set_query_id(next_query_id());
if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
{
/*
This statement will enter/leave prelocked mode on its own.
Entering prelocked mode changes table list and related members
of LEX, so we'll need to restore them.
*/
if (lex_query_tables_own_last)
{
/*
We've already entered/left prelocked mode with this statement.
Attach the list of tables that need to be prelocked and mark m_lex
as having such list attached.
*/
*lex_query_tables_own_last= prelocking_tables;
m_lex->mark_as_requiring_prelocking(lex_query_tables_own_last);
}
}
reinit_stmt_before_use(thd, m_lex);
#ifndef EMBEDDED_LIBRARY
/*
If there was instruction which changed tracking state,
the result of changed tracking state send to client in OK packed.
So it changes result sent to client and probably can be different
independent on query text. So we can't cache such results.
*/
if ((thd->client_capabilities & CLIENT_SESSION_TRACK) &&
(thd->server_status & SERVER_SESSION_STATE_CHANGED))
thd->lex->safe_to_cache_query= 0;
#endif
Opt_trace_start ots(thd);
ots.init(thd, m_lex->query_tables, SQLCOM_SELECT, &m_lex->var_list,
nullptr, 0, thd->variables.character_set_client);
Json_writer_object trace_command(thd);
Json_writer_array trace_command_steps(thd, "steps");
if (open_tables)
res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables);
if (likely(!res))
{
res= instr->exec_core(thd, nextp);
DBUG_PRINT("info",("exec_core returned: %d", res));
}
/*
Call after unit->cleanup() to close open table
key read.
*/
if (open_tables)
{
m_lex->unit.cleanup();
/* Here we also commit or rollback the current statement. */
if (! thd->in_sub_stmt)
{
thd->get_stmt_da()->set_overwrite_status(true);
thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd);
thd->get_stmt_da()->set_overwrite_status(false);
}
close_thread_tables(thd);
thd_proc_info(thd, 0);
if (! thd->in_sub_stmt)
{
if (thd->transaction_rollback_request)
{
trans_rollback_implicit(thd);
thd->release_transactional_locks();
}
else if (! thd->in_multi_stmt_transaction_mode())
thd->release_transactional_locks();
else
thd->mdl_context.release_statement_locks();
}
}
//TODO: why is this here if log_slow_query is in sp_instr_stmt::execute?
delete_explain_query(m_lex);
if (m_lex->query_tables_own_last)
{
/*
We've entered and left prelocking mode when executing statement
stored in m_lex.
m_lex->query_tables(->next_global)* list now has a 'tail' - a list
of tables that are added for prelocking. (If this is the first
execution, the 'tail' was added by open_tables(), otherwise we've
attached it above in this function).
Now we'll save the 'tail', and detach it.
*/
lex_query_tables_own_last= m_lex->query_tables_own_last;
prelocking_tables= *lex_query_tables_own_last;
*lex_query_tables_own_last= nullptr;
m_lex->query_tables_last= m_lex->query_tables_own_last;
m_lex->mark_as_requiring_prelocking(nullptr);
}
thd->rollback_item_tree_changes();
/*
Update the state of the active arena if no errors on
open_tables stage.
*/
if (likely(!res) || likely(!thd->is_error()))
thd->stmt_arena->state= Query_arena::STMT_EXECUTED;
/*
Merge here with the saved parent's values
what is needed from the substatement gained
*/
thd->transaction->stmt.modified_non_trans_table |= parent_modified_non_trans_table;
thd->transaction->stmt.m_unsafe_rollback_flags |= parent_unsafe_rollback_flags;
TRANSACT_TRACKER(add_trx_state_from_thd(thd));
/*
Unlike for PS we should not call Item's destructors for newly created
items after execution of each instruction in stored routine. This is
because SP often create Item (like Item_int, Item_string etc...) when
they want to store some value in local variable, pass return value and
etc... So their life time should be longer than one instruction.
cleanup_items() is called in sp_head::execute()
*/
thd->lex->restore_set_statement_var();
DBUG_RETURN(res || thd->is_error());
}
void sp_lex_keeper::free_lex(THD *thd)
{
/*
Currently, m_lex_resp == false for sp_instr_cursor_copy_struct instructions
and in some cases for sp_instr_set instructions. For these classes
free_lex() returns control flow immediately and doesn't change m_lex.
*/
if (!m_lex_resp || !m_lex) return;
/* Prevent endless recursion. */
m_lex->sphead= nullptr;
lex_end(m_lex);
sp_lex_cursor* cursor_lex= m_lex->get_lex_for_cursor();
if (cursor_lex == nullptr)
{
delete (st_lex_local *)m_lex;
/*
In case it is not sp_lex_cursor set thd->lex to the null value
if it points to a LEX object we just deleted in order to avoid
dangling pointers problem.
*/
if (thd->lex == m_lex)
thd->lex= nullptr;
m_lex= nullptr;
m_lex_resp= false;
}
else
{
/*
sp_lex_cursor has references to items allocated on parsing a cursor
declaration statement. These items are deleted on re-parsing a failing
cursor declaration statement at the method
sp_lex_instr::cleanup_before_parsing.
Remove the reference to items that will be deleted from sp_lex_cursor
in order to avoid dangling pointers problem.
*/
cleanup_items(cursor_lex->free_list);
cursor_lex->free_list= nullptr;
}
lex_query_tables_own_last= nullptr;
}
void sp_lex_keeper::set_lex(LEX *lex)
{
m_lex= lex;
m_lex_resp= true;
m_lex->sp_lex_in_use= true;
}
int sp_lex_keeper::validate_lex_and_exec_core(THD *thd, uint *nextp,
bool open_tables,
sp_lex_instr* instr)
{
Reprepare_observer reprepare_observer;
bool rerun_the_same_instr= false;
while (true)
{
if (instr->is_invalid())
{
thd->clear_error();
free_lex(thd);
LEX *lex= instr->parse_expr(thd, thd->spcont->m_sp, m_lex);
if (!lex) return true;
/*
m_lex != nullptr in case it points to sp_lex_cursor.
*/
if (m_lex == nullptr)
set_lex(lex);
m_first_execution= true;
rerun_the_same_instr= true;
}
Reprepare_observer *stmt_reprepare_observer= nullptr;
if (!m_first_execution &&
((sql_command_flags[m_lex->sql_command] & CF_REEXECUTION_FRAGILE) ||
m_lex->sql_command == SQLCOM_END))
{
reprepare_observer.reset_reprepare_observer();
stmt_reprepare_observer= &reprepare_observer;
}
Reprepare_observer *save_reprepare_observer= thd->m_reprepare_observer;
thd->m_reprepare_observer= stmt_reprepare_observer;
bool rc= reset_lex_and_exec_core(thd, nextp, open_tables, instr,
rerun_the_same_instr);
thd->m_reprepare_observer= save_reprepare_observer;
m_first_execution= false;
if (!rc)
break;
/*
Raise the error upper level in case:
- we got an error and Reprepare_observer is not set
- a fatal error has been got
- the current execution thread has been killed
- an error different from ER_NEED_REPREPARE has been got.
*/
if (stmt_reprepare_observer == nullptr ||
thd->is_fatal_error ||
thd->killed ||
thd->get_stmt_da()->get_sql_errno() != ER_NEED_REPREPARE)
return 1;
if (!stmt_reprepare_observer->can_retry())
{
/*
Reprepare_observer sets error status in DA but Sql_condition is not
added. Please check Reprepare_observer::report_error(). Pushing
Sql_condition for ER_NEED_REPREPARE here.
*/
Diagnostics_area *da= thd->get_stmt_da();
da->push_warning(thd, da->get_sql_errno(), da->get_sqlstate(),
Sql_state_errno_level::WARN_LEVEL_ERROR, da->message());
return 1;
}
instr->invalidate();
}
return 0;
}
int sp_lex_keeper::cursor_reset_lex_and_exec_core(THD *thd, uint *nextp,
bool open_tables,
sp_lex_instr *instr)
{
Query_arena *old_arena= thd->stmt_arena;
/*
Get the Query_arena from the cursor statement LEX, which contains
the free_list of the query, so new items (if any) are stored in
the right free_list, and we can cleanup after each cursor operation,
e.g. open or cursor_copy_struct (for cursor%ROWTYPE variables).
*/
thd->stmt_arena= m_lex->query_arena();
int res= validate_lex_and_exec_core(thd, nextp, open_tables, instr);
cleanup_items(thd->stmt_arena->free_list);
thd->stmt_arena= old_arena;
return res;
}
/*
sp_instr class functions
*/
int sp_instr::exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables)
{
int result;
/*
Check whenever we have access to tables for this statement
and open and lock them before executing instructions core function.
*/
if (thd->open_temporary_tables(tables) ||
check_table_access(thd, SELECT_ACL, tables, false, UINT_MAX, false)
|| open_and_lock_tables(thd, tables, true, 0))
result= -1;
else
result= 0;
/* Prepare all derived tables/views to catch possible errors. */
if (!result)
result= mysql_handle_derived(thd->lex, DT_PREPARE) ? -1 : 0;
return result;
}
uint sp_instr::get_cont_dest() const
{
return (m_ip+1);
}
int sp_instr::exec_core(THD *thd, uint *nextp)
{
DBUG_ASSERT(0);
return 0;
}
void sp_lex_instr::get_query(String *sql_query) const
{
LEX_CSTRING expr_query= get_expr_query();
/*
the expression string must me initialized in constructor of a derived class
*/
DBUG_ASSERT(expr_query.str != null_clex_str.str &&
expr_query.length != null_clex_str.length);
/*
Leave the method in case of empty query string.
*/
if (!expr_query.length)
return;
sql_query->append(C_STRING_WITH_LEN("SELECT "));
sql_query->append(expr_query);
}
void sp_lex_instr::cleanup_before_parsing(enum_sp_type sp_type)
{
Item *current= free_list;
while (current)
{
Item *next= current->next;
current->delete_self();
current= next;
}
free_list= nullptr;
if (sp_type == SP_TYPE_TRIGGER)
/*
Some of deleted items can be referenced from the list
m_cur_trigger_stmt_items. Clean up the list content to avoid
dangling references.
*/
m_cur_trigger_stmt_items.empty();
}
/**
Set up field object for every NEW/OLD item of the trigger.
@param thd current thread
@param sp sp_head object of the trigger
*/
bool sp_lex_instr::setup_table_fields_for_trigger(
THD *thd, sp_head *sp,
SQL_I_List<Item_trigger_field> *next_trig_items_list)
{
bool result= false;
DBUG_ASSERT(sp->m_trg);
for (Item_trigger_field *trg_field= sp->m_cur_instr_trig_field_items.first;
trg_field;
trg_field= trg_field->next_trg_field)
{
trg_field->setup_field(thd, sp->m_trg->base->get_subject_table(),
&sp->m_trg->subject_table_grants);
result= trg_field->fix_fields_if_needed(thd, (Item **)0);
}
/*
Move the list of Item_trigger_field objects, that have just been
filled in on parsing the trigger's statement, into the instruction list
owned by SP instruction.
*/
if (sp->m_cur_instr_trig_field_items.elements)
{
sp->m_cur_instr_trig_field_items.save_and_clear(
&m_cur_trigger_stmt_items);
m_cur_trigger_stmt_items.first->next_trig_field_list= next_trig_items_list;
}
return result;
}
LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex)
{
String sql_query;
get_query(&sql_query);
if (sql_query.length() == 0)
{
/**
The instruction has returned zero-length query string. That means, the
re-preparation of the instruction is not possible. We should not come
here in the normal case.
*/
assert(false);
my_error(ER_UNKNOWN_ERROR, MYF(0));
return nullptr;
}
/*
Remember a pointer to the next list of Item_trigger_field objects.
The current list of Item_trigger_field objects is cleared up in the
method cleanup_before_parsing().
*/
SQL_I_List<Item_trigger_field> *saved_ptr_to_next_trg_items_list= nullptr;
if (m_cur_trigger_stmt_items.elements)
saved_ptr_to_next_trg_items_list=
m_cur_trigger_stmt_items.first->next_trig_field_list;
/*
Clean up items owned by this SP instruction.
*/
cleanup_before_parsing(sp->m_handler->type());
DBUG_ASSERT(mem_root != thd->mem_root);
/*
Back up the current free_list pointer and reset it to nullptr.
Set thd->mem_root pointing to a mem_root of SP instruction being re-parsed.
In that way any items created on parsing a statement of the current
instruction is allocated on SP instruction's mem_root and placed on its own
free_list that later assigned to the current sp_instr. We use the separate
free list for every instruction since at least at one place in the source
code (the function subst_spvars() to be accurate) we iterate along the
list sp_instr->free_list on executing of every SP instruction.
*/
Query_arena backup;
/*
A statement of SP instruction is going to be re-parsed, so reset
SP arena's state to STMT_INITIALIZED_FOR_SP as its initial state.
*/
state= STMT_INITIALIZED_FOR_SP;
thd->set_n_backup_active_arena(this, &backup);
thd->free_list= nullptr;
Parser_state parser_state;
if (parser_state.init(thd, sql_query.c_ptr(), sql_query.length()))
return nullptr;
// Create a new LEX and initialize it.
LEX *lex_saved= thd->lex;
Item **cursor_free_list= nullptr;
/*
sp_instr_lex != nullptr for cursor relating SP instructions (sp_instr_cpush,
sp_instr_cursor_copy_struct) and in some cases for sp_instr_set.
*/
if (sp_instr_lex == nullptr)
thd->lex= new (thd->mem_root) st_lex_local;
else
{
sp_lex_cursor* cursor_lex= sp_instr_lex->get_lex_for_cursor();
/*
In case sp_instr_cursor_copy_struct instruction being re-parsed
the items stored in free_list of sp_lex_cursor are not cleaned up
since the class sp_instr_cursor_copy_struct don't pass ownership of
lex object to sp_lex_keeper. So, clean up items stored in free_list of
sp_lex_cursor explicitly. For sp_instr_cpush instruction items stored
in free_list of sp_lex_cursor are cleaned up in the method free_lex()
since sp_instr_cpush owns a lex object stored in its sp_lex_keeper
data member. So, for the sp_instr_cpush instruction by the time we reach
this block cursor_lex->free_list is already empty.
*/
cleanup_items(cursor_lex->free_list);
cursor_free_list= &cursor_lex->free_list;
DBUG_ASSERT(thd->lex == sp_instr_lex);
}
lex_start(thd);
thd->lex->sphead= sp;
thd->lex->spcont= m_ctx;
sql_digest_state *parent_digest= thd->m_digest;
PSI_statement_locker *parent_locker= thd->m_statement_psi;
thd->m_digest= nullptr;
thd->m_statement_psi= nullptr;
/*
sp_head::m_tmp_query is set by parser on parsing every statement of
a stored routine. Since here we re-parse failed statement outside stored
routine context, this data member isn't set. In result, the assert
DBUG_ASSERT(sphead->m_tmp_query <= start)
is fired in the constructor of the class Query_fragment.
To fix the assert failure, reset this data member to point to beginning of
the current statement being parsed.
*/
const char *m_tmp_query_bak= sp->m_tmp_query;
sp->m_tmp_query= sql_query.c_ptr();
bool parsing_failed= parse_sql(thd, &parser_state, nullptr);
sp->m_tmp_query= m_tmp_query_bak;
thd->m_digest= parent_digest;
thd->m_statement_psi= parent_locker;
if (!parsing_failed)
{
thd->lex->set_trg_event_type_for_tables();
adjust_sql_command(thd->lex);
parsing_failed= on_after_expr_parsing(thd);
if (sp->m_handler->type() == SP_TYPE_TRIGGER)
setup_table_fields_for_trigger(thd, sp,
saved_ptr_to_next_trg_items_list);
if (cursor_free_list)
/*
Update sp_lex_cursor::free_list to point to a list of items
just created on re-parsing the cursor's statement.
*/
*cursor_free_list= thd->free_list;
else
/*
Assign the list of items created on re-parsing the statement to
the current stored routine's instruction.
*/
free_list= thd->free_list;
thd->free_list= nullptr;
}
Query_arena old;
thd->restore_active_arena(&old, &backup);
LEX *expr_lex= thd->lex;
thd->lex= lex_saved;
return parsing_failed ? nullptr : expr_lex;
}
/*
sp_instr_stmt class functions
*/
PSI_statement_info sp_instr_stmt::psi_info=
{ 0, "stmt", 0};
int
sp_instr_stmt::execute(THD *thd, uint *nextp)
{
int res;
bool save_enable_slow_log;
const CSET_STRING query_backup= thd->query_string;
Sub_statement_state backup_state;
DBUG_ENTER("sp_instr_stmt::execute");
DBUG_PRINT("info", ("command: %d", m_lex_keeper.sql_command()));
MYSQL_SET_STATEMENT_TEXT(thd->m_statement_psi, m_query.str, static_cast<uint>(m_query.length));
#if defined(ENABLED_PROFILING)
/* This s-p instr is profilable and will be captured. */
thd->profiling.set_query_source(m_query.str, m_query.length);
#endif
save_enable_slow_log= thd->enable_slow_log;
thd->store_slow_query_state(&backup_state);
if (!(res= alloc_query(thd, m_query.str, m_query.length)) &&
!(res=subst_spvars(thd, this, &m_query)))
{
/*
(the order of query cache and subst_spvars calls is irrelevant because
queries with SP vars can't be cached)
*/
general_log_write(thd, COM_QUERY, thd->query(), thd->query_length());
if (query_cache_send_result_to_client(thd, thd->query(),
thd->query_length()) <= 0)
{
thd->reset_slow_query_state();
res= m_lex_keeper.validate_lex_and_exec_core(thd, nextp, false, this);
bool log_slow= !res && thd->enable_slow_log;
/* Finalize server status flags after executing a statement. */
if (log_slow || thd->get_stmt_da()->is_eof())
thd->update_server_status();
if (thd->get_stmt_da()->is_eof())
thd->protocol->end_statement();
query_cache_end_of_result(thd);
mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_STATUS,
thd->get_stmt_da()->is_error() ?
thd->get_stmt_da()->sql_errno() : 0,
command_name[COM_QUERY].str);
if (log_slow)
log_slow_statement(thd);
/*
Restore enable_slow_log, that can be changed by a admin or call
command
*/
thd->enable_slow_log= save_enable_slow_log;
/* Add the number of rows to thd for the 'call' statistics */
thd->add_slow_query_state(&backup_state);
}
else
{
/* change statistics */
enum_sql_command save_sql_command= thd->lex->sql_command;
thd->lex->sql_command= SQLCOM_SELECT;
status_var_increment(thd->status_var.com_stat[SQLCOM_SELECT]);
thd->update_stats();
thd->lex->sql_command= save_sql_command;
*nextp= m_ip+1;
}
thd->set_query(query_backup);
thd->query_name_consts= 0;
if (likely(!thd->is_error()))
{
res= 0;
thd->get_stmt_da()->reset_diagnostics_area();
}
}
DBUG_RETURN(res || thd->is_error());
}
void
sp_instr_stmt::print(String *str)
{
size_t i, len;
/* stmt CMD "..." */
if (str->reserve(SP_STMT_PRINT_MAXLEN+SP_INSTR_UINT_MAXLEN+8))
return;
str->qs_append(STRING_WITH_LEN("stmt "));
str->qs_append((uint)m_lex_keeper.sql_command());
str->qs_append(STRING_WITH_LEN(" \""));
len= m_query.length;
/*
Print the query string (but not too much of it), just to indicate which
statement it is.
*/
if (len > SP_STMT_PRINT_MAXLEN)
len= SP_STMT_PRINT_MAXLEN-3;
/* Copy the query string and replace '\n' with ' ' in the process */
for (i= 0 ; i < len ; i++)
{
char c= m_query.str[i];
if (c == '\n')
c= ' ';
str->qs_append(c);
}
if (m_query.length > SP_STMT_PRINT_MAXLEN)
str->qs_append(STRING_WITH_LEN("...")); /* Indicate truncated string */
str->qs_append('"');
}
int
sp_instr_stmt::exec_core(THD *thd, uint *nextp)
{
MYSQL_QUERY_EXEC_START(thd->query(),
thd->thread_id,
thd->get_db(),
&thd->security_ctx->priv_user[0],
(char *)thd->security_ctx->host_or_ip,
3);
int res= mysql_execute_command(thd);
MYSQL_QUERY_EXEC_DONE(res);
*nextp= m_ip+1;
return res;
}
/*
sp_instr_set class functions
*/
PSI_statement_info sp_instr_set::psi_info=
{ 0, "set", 0};
int
sp_instr_set::execute(THD *thd, uint *nextp)
{
DBUG_ENTER("sp_instr_set::execute");
DBUG_PRINT("info", ("offset: %u", m_offset));
DBUG_RETURN(m_lex_keeper.validate_lex_and_exec_core(thd, nextp, true, this));
}
sp_rcontext *sp_instr_set::get_rcontext(THD *thd) const
{
return m_rcontext_handler->get_rcontext(thd->spcont);
}
int
sp_instr_set::exec_core(THD *thd, uint *nextp)
{
int res= get_rcontext(thd)->set_variable(thd, m_offset, &m_value);
delete_explain_query(thd->lex);
*nextp = m_ip+1;
return res;
}
void
sp_instr_set::print(String *str)
{
/* set name@offset ... */
size_t rsrv = SP_INSTR_UINT_MAXLEN+6;
sp_variable *var = m_ctx->find_variable(m_offset);
const LEX_CSTRING *prefix= m_rcontext_handler->get_name_prefix();
/* 'var' should always be non-null, but just in case... */
if (var)
rsrv+= var->name.length + prefix->length;
if (str->reserve(rsrv))
return;
str->qs_append(STRING_WITH_LEN("set "));
str->qs_append(prefix->str, prefix->length);
if (var)
{
str->qs_append(&var->name);