/
sql_lex.cc
10449 lines (9077 loc) · 298 KB
/
sql_lex.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
/* Copyright (c) 2000, 2014, Oracle and/or its affiliates.
Copyright (c) 2009, 2019, MariaDB Corporation
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-1335 USA */
/* A lexical scanner on a temporary buffer with a yacc interface */
#define MYSQL_LEX 1
#include "mariadb.h"
#include "sql_priv.h"
#include "sql_class.h" // sql_lex.h: SQLCOM_END
#include "sql_lex.h"
#include "sql_parse.h" // add_to_list
#include "item_create.h"
#include <m_ctype.h>
#include <hash.h>
#include "sp_head.h"
#include "sp.h"
#include "sql_select.h"
#include "sql_cte.h"
#include "sql_signal.h"
#include "sql_truncate.h" // Sql_cmd_truncate_table
#include "sql_admin.h" // Sql_cmd_analyze/Check..._table
#include "sql_partition.h"
#include "sql_partition_admin.h" // Sql_cmd_alter_table_*_part
#include "event_parse_data.h"
void LEX::parse_error(uint err_number)
{
thd->parse_error(err_number);
}
/**
LEX_STRING constant for null-string to be used in parser and other places.
*/
const LEX_STRING empty_lex_str= {(char *) "", 0};
const LEX_CSTRING null_clex_str= {NULL, 0};
const LEX_CSTRING empty_clex_str= {"", 0};
const LEX_CSTRING star_clex_str= {"*", 1};
const LEX_CSTRING param_clex_str= {"?", 1};
/**
@note The order of the elements of this array must correspond to
the order of elements in enum_binlog_stmt_unsafe.
*/
const int
Query_tables_list::binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT] =
{
ER_BINLOG_UNSAFE_LIMIT,
ER_BINLOG_UNSAFE_INSERT_DELAYED,
ER_BINLOG_UNSAFE_SYSTEM_TABLE,
ER_BINLOG_UNSAFE_AUTOINC_COLUMNS,
ER_BINLOG_UNSAFE_UDF,
ER_BINLOG_UNSAFE_SYSTEM_VARIABLE,
ER_BINLOG_UNSAFE_SYSTEM_FUNCTION,
ER_BINLOG_UNSAFE_NONTRANS_AFTER_TRANS,
ER_BINLOG_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
ER_BINLOG_UNSAFE_MIXED_STATEMENT,
ER_BINLOG_UNSAFE_INSERT_IGNORE_SELECT,
ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE,
ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT,
ER_BINLOG_UNSAFE_REPLACE_SELECT,
ER_BINLOG_UNSAFE_CREATE_IGNORE_SELECT,
ER_BINLOG_UNSAFE_CREATE_REPLACE_SELECT,
ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC,
ER_BINLOG_UNSAFE_UPDATE_IGNORE,
ER_BINLOG_UNSAFE_INSERT_TWO_KEYS,
ER_BINLOG_UNSAFE_AUTOINC_NOT_FIRST
};
/* Longest standard keyword name */
#define TOCK_NAME_LENGTH 24
/*
The following data is based on the latin1 character set, and is only
used when comparing keywords
*/
static uchar to_upper_lex[]=
{
0, 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, 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,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,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
};
/*
Names of the index hints (for error messages). Keep in sync with
index_hint_type
*/
const char * index_hint_type_name[] =
{
"IGNORE INDEX",
"USE INDEX",
"FORCE INDEX"
};
inline int lex_casecmp(const char *s, const char *t, uint len)
{
while (len-- != 0 &&
to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
return (int) len+1;
}
#include <lex_hash.h>
void lex_init(void)
{
uint i;
DBUG_ENTER("lex_init");
for (i=0 ; i < array_elements(symbols) ; i++)
symbols[i].length=(uchar) strlen(symbols[i].name);
for (i=0 ; i < array_elements(sql_functions) ; i++)
sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
DBUG_VOID_RETURN;
}
void lex_free(void)
{ // Call this when daemon ends
DBUG_ENTER("lex_free");
DBUG_VOID_RETURN;
}
/**
Initialize lex object for use in fix_fields and parsing.
SYNOPSIS
init_lex_with_single_table()
@param thd The thread object
@param table The table object
@return Operation status
@retval TRUE An error occurred, memory allocation error
@retval FALSE Ok
DESCRIPTION
This function is used to initialize a lex object on the
stack for use by fix_fields and for parsing. In order to
work properly it also needs to initialize the
Name_resolution_context object of the lexer.
Finally it needs to set a couple of variables to ensure
proper functioning of fix_fields.
*/
int
init_lex_with_single_table(THD *thd, TABLE *table, LEX *lex)
{
TABLE_LIST *table_list;
Table_ident *table_ident;
SELECT_LEX *select_lex= lex->first_select_lex();
Name_resolution_context *context= &select_lex->context;
/*
We will call the parser to create a part_info struct based on the
partition string stored in the frm file.
We will use a local lex object for this purpose. However we also
need to set the Name_resolution_object for this lex object. We
do this by using add_table_to_list where we add the table that
we're working with to the Name_resolution_context.
*/
thd->lex= lex;
lex_start(thd);
context->init();
if (unlikely((!(table_ident= new Table_ident(thd,
&table->s->db,
&table->s->table_name,
TRUE)))) ||
(unlikely(!(table_list= select_lex->add_table_to_list(thd,
table_ident,
NULL,
0)))))
return TRUE;
context->resolve_in_table_list_only(table_list);
lex->use_only_table_context= TRUE;
lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_VCOL_EXPR;
select_lex->cur_pos_in_select_list= UNDEF_POS;
table->map= 1; //To ensure correct calculation of const item
table_list->table= table;
table_list->cacheable_table= false;
return FALSE;
}
/**
End use of local lex with single table
SYNOPSIS
end_lex_with_single_table()
@param thd The thread object
@param table The table object
@param old_lex The real lex object connected to THD
DESCRIPTION
This function restores the real lex object after calling
init_lex_with_single_table and also restores some table
variables temporarily set.
*/
void
end_lex_with_single_table(THD *thd, TABLE *table, LEX *old_lex)
{
LEX *lex= thd->lex;
table->map= 0;
table->get_fields_in_item_tree= FALSE;
lex_end(lex);
thd->lex= old_lex;
}
void
st_parsing_options::reset()
{
allows_variable= TRUE;
}
/**
Perform initialization of Lex_input_stream instance.
Basically, a buffer for pre-processed query. This buffer should be large
enough to keep multi-statement query. The allocation is done once in
Lex_input_stream::init() in order to prevent memory pollution when
the server is processing large multi-statement queries.
*/
bool Lex_input_stream::init(THD *thd,
char* buff,
size_t length)
{
DBUG_EXECUTE_IF("bug42064_simulate_oom",
DBUG_SET("+d,simulate_out_of_memory"););
m_cpp_buf= (char*) thd->alloc(length + 1);
DBUG_EXECUTE_IF("bug42064_simulate_oom",
DBUG_SET("-d,bug42064_simulate_oom"););
if (m_cpp_buf == NULL)
return true;
m_thd= thd;
reset(buff, length);
return false;
}
/**
Prepare Lex_input_stream instance state for use for handling next SQL statement.
It should be called between two statements in a multi-statement query.
The operation resets the input stream to the beginning-of-parse state,
but does not reallocate m_cpp_buf.
*/
void
Lex_input_stream::reset(char *buffer, size_t length)
{
yylineno= 1;
lookahead_token= -1;
lookahead_yylval= NULL;
m_ptr= buffer;
m_tok_start= NULL;
m_tok_end= NULL;
m_end_of_query= buffer + length;
m_tok_start_prev= NULL;
m_buf= buffer;
m_buf_length= length;
m_echo= TRUE;
m_cpp_tok_start= NULL;
m_cpp_tok_start_prev= NULL;
m_cpp_tok_end= NULL;
m_body_utf8= NULL;
m_cpp_utf8_processed_ptr= NULL;
next_state= MY_LEX_START;
found_semicolon= NULL;
ignore_space= MY_TEST(m_thd->variables.sql_mode & MODE_IGNORE_SPACE);
stmt_prepare_mode= FALSE;
multi_statements= TRUE;
in_comment=NO_COMMENT;
m_underscore_cs= NULL;
m_cpp_ptr= m_cpp_buf;
}
/**
The operation is called from the parser in order to
1) designate the intention to have utf8 body;
1) Indicate to the lexer that we will need a utf8 representation of this
statement;
2) Determine the beginning of the body.
@param thd Thread context.
@param begin_ptr Pointer to the start of the body in the pre-processed
buffer.
*/
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
{
DBUG_ASSERT(begin_ptr);
DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
size_t body_utf8_length= get_body_utf8_maximum_length(thd);
m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
m_body_utf8_ptr= m_body_utf8;
*m_body_utf8_ptr= 0;
m_cpp_utf8_processed_ptr= begin_ptr;
}
size_t Lex_input_stream::get_body_utf8_maximum_length(THD *thd)
{
/*
String literals can grow during escaping:
1a. Character string '<TAB>' can grow to '\t', 3 bytes to 4 bytes growth.
1b. Character string '1000 times <TAB>' grows from
1002 to 2002 bytes (including quotes), which gives a little bit
less than 2 times growth.
"2" should be a reasonable multiplier that safely covers escaping needs.
*/
return (m_buf_length / thd->variables.character_set_client->mbminlen) *
my_charset_utf8_bin.mbmaxlen * 2/*for escaping*/;
}
/**
@brief The operation appends unprocessed part of pre-processed buffer till
the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.
The idea is that some tokens in the pre-processed buffer (like character
set introducers) should be skipped.
Example:
CPP buffer: SELECT 'str1', _latin1 'str2';
m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
In order to skip "_latin1", the following call should be made:
body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)
@param ptr Pointer in the pre-processed buffer, which specifies the
end of the chunk, which should be appended to the utf8
body.
@param end_ptr Pointer in the pre-processed buffer, to which
m_cpp_utf8_processed_ptr will be set in the end of the
operation.
*/
void Lex_input_stream::body_utf8_append(const char *ptr,
const char *end_ptr)
{
DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
if (!m_body_utf8)
return;
if (m_cpp_utf8_processed_ptr >= ptr)
return;
size_t bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
m_body_utf8_ptr += bytes_to_copy;
*m_body_utf8_ptr= 0;
m_cpp_utf8_processed_ptr= end_ptr;
}
/**
The operation appends unprocessed part of the pre-processed buffer till
the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.
@param ptr Pointer in the pre-processed buffer, which specifies the end
of the chunk, which should be appended to the utf8 body.
*/
void Lex_input_stream::body_utf8_append(const char *ptr)
{
body_utf8_append(ptr, ptr);
}
/**
The operation converts the specified text literal to the utf8 and appends
the result to the utf8-body.
@param thd Thread context.
@param txt Text literal.
@param txt_cs Character set of the text literal.
@param end_ptr Pointer in the pre-processed buffer, to which
m_cpp_utf8_processed_ptr will be set in the end of the
operation.
*/
void
Lex_input_stream::body_utf8_append_ident(THD *thd,
const Lex_string_with_metadata_st *txt,
const char *end_ptr)
{
if (!m_cpp_utf8_processed_ptr)
return;
LEX_CSTRING utf_txt;
thd->make_text_string_sys(&utf_txt, txt); // QQ: check return value?
/* NOTE: utf_txt.length is in bytes, not in symbols. */
memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
m_body_utf8_ptr += utf_txt.length;
*m_body_utf8_ptr= 0;
m_cpp_utf8_processed_ptr= end_ptr;
}
extern "C" {
/**
Escape a character. Consequently puts "escape" and "wc" characters into
the destination utf8 string.
@param cs - the character set (utf8)
@param escape - the escape character (backslash, single quote, double quote)
@param wc - the character to be escaped
@param str - the destination string
@param end - the end of the destination string
@returns - a code according to the wc_mb() convension.
*/
int my_wc_mb_utf8_with_escape(CHARSET_INFO *cs, my_wc_t escape, my_wc_t wc,
uchar *str, uchar *end)
{
DBUG_ASSERT(escape > 0);
if (str + 1 >= end)
return MY_CS_TOOSMALL2; // Not enough space, need at least two bytes.
*str= (uchar)escape;
int cnvres= my_charset_utf8_handler.wc_mb(cs, wc, str + 1, end);
if (cnvres > 0)
return cnvres + 1; // The character was normally put
if (cnvres == MY_CS_ILUNI)
return MY_CS_ILUNI; // Could not encode "wc" (e.g. non-BMP character)
DBUG_ASSERT(cnvres <= MY_CS_TOOSMALL);
return cnvres - 1; // Not enough space
}
/**
Optionally escape a character.
If "escape" is non-zero, then both "escape" and "wc" are put to
the destination string. Otherwise, only "wc" is put.
@param cs - the character set (utf8)
@param wc - the character to be optionally escaped
@param escape - the escape character, or 0
@param ewc - the escaped replacement of "wc" (e.g. 't' for '\t')
@param str - the destination string
@param end - the end of the destination string
@returns - a code according to the wc_mb() conversion.
*/
int my_wc_mb_utf8_opt_escape(CHARSET_INFO *cs,
my_wc_t wc, my_wc_t escape, my_wc_t ewc,
uchar *str, uchar *end)
{
return escape ? my_wc_mb_utf8_with_escape(cs, escape, ewc, str, end) :
my_charset_utf8_handler.wc_mb(cs, wc, str, end);
}
/**
Encode a character with optional backlash escaping and quote escaping.
Quote marks are escaped using another quote mark.
Additionally, if "escape" is non-zero, then special characters are
also escaped using "escape".
Otherwise (if "escape" is zero, e.g. in case of MODE_NO_BACKSLASH_ESCAPES),
then special characters are not escaped and handled as normal characters.
@param cs - the character set (utf8)
@param wc - the character to be encoded
@param str - the destination string
@param end - the end of the destination string
@param sep - the string delimiter (e.g. ' or ")
@param escape - the escape character (backslash, or 0)
@returns - a code according to the wc_mb() convension.
*/
int my_wc_mb_utf8_escape(CHARSET_INFO *cs, my_wc_t wc, uchar *str, uchar *end,
my_wc_t sep, my_wc_t escape)
{
DBUG_ASSERT(escape == 0 || escape == '\\');
DBUG_ASSERT(sep == '"' || sep == '\'');
switch (wc) {
case 0: return my_wc_mb_utf8_opt_escape(cs, wc, escape, '0', str, end);
case '\t': return my_wc_mb_utf8_opt_escape(cs, wc, escape, 't', str, end);
case '\r': return my_wc_mb_utf8_opt_escape(cs, wc, escape, 'r', str, end);
case '\n': return my_wc_mb_utf8_opt_escape(cs, wc, escape, 'n', str, end);
case '\032': return my_wc_mb_utf8_opt_escape(cs, wc, escape, 'Z', str, end);
case '\'':
case '\"':
if (wc == sep)
return my_wc_mb_utf8_with_escape(cs, wc, wc, str, end);
}
return my_charset_utf8_handler.wc_mb(cs, wc, str, end); // No escaping needed
}
/** wc_mb() compatible routines for all sql_mode and delimiter combinations */
int my_wc_mb_utf8_escape_single_quote_and_backslash(CHARSET_INFO *cs,
my_wc_t wc,
uchar *str, uchar *end)
{
return my_wc_mb_utf8_escape(cs, wc, str, end, '\'', '\\');
}
int my_wc_mb_utf8_escape_double_quote_and_backslash(CHARSET_INFO *cs,
my_wc_t wc,
uchar *str, uchar *end)
{
return my_wc_mb_utf8_escape(cs, wc, str, end, '"', '\\');
}
int my_wc_mb_utf8_escape_single_quote(CHARSET_INFO *cs, my_wc_t wc,
uchar *str, uchar *end)
{
return my_wc_mb_utf8_escape(cs, wc, str, end, '\'', 0);
}
int my_wc_mb_utf8_escape_double_quote(CHARSET_INFO *cs, my_wc_t wc,
uchar *str, uchar *end)
{
return my_wc_mb_utf8_escape(cs, wc, str, end, '"', 0);
}
}; // End of extern "C"
/**
Get an escaping function, depending on the current sql_mode and the
string separator.
*/
my_charset_conv_wc_mb
Lex_input_stream::get_escape_func(THD *thd, my_wc_t sep) const
{
return thd->backslash_escapes() ?
(sep == '"' ? my_wc_mb_utf8_escape_double_quote_and_backslash:
my_wc_mb_utf8_escape_single_quote_and_backslash) :
(sep == '"' ? my_wc_mb_utf8_escape_double_quote:
my_wc_mb_utf8_escape_single_quote);
}
/**
Append a text literal to the end of m_body_utf8.
The string is escaped according to the current sql_mode and the
string delimiter (e.g. ' or ").
@param thd - current THD
@param txt - the string to be appended to m_body_utf8.
Note, the string must be already unescaped.
@param cs - the character set of the string
@param end_ptr - m_cpp_utf8_processed_ptr will be set to this value
(see body_utf8_append_ident for details)
@param sep - the string delimiter (single or double quote)
*/
void Lex_input_stream::body_utf8_append_escape(THD *thd,
const LEX_CSTRING *txt,
CHARSET_INFO *cs,
const char *end_ptr,
my_wc_t sep)
{
DBUG_ASSERT(sep == '\'' || sep == '"');
if (!m_cpp_utf8_processed_ptr)
return;
uint errors;
/**
We previously alloced m_body_utf8 to be able to store the query with all
strings properly escaped. See get_body_utf8_maximum_length().
So here we have guaranteedly enough space to append any string literal
with escaping. Passing txt->length*2 as "available space" is always safe.
For better safety purposes we could calculate get_body_utf8_maximum_length()
every time we append a string, but this would affect performance negatively,
so let's check that we don't get beyond the allocated buffer in
debug build only.
*/
DBUG_ASSERT(m_body_utf8 + get_body_utf8_maximum_length(thd) >=
m_body_utf8_ptr + txt->length * 2);
uint32 cnv_length= my_convert_using_func(m_body_utf8_ptr, txt->length * 2,
&my_charset_utf8_general_ci,
get_escape_func(thd, sep),
txt->str, txt->length,
cs, cs->cset->mb_wc,
&errors);
m_body_utf8_ptr+= cnv_length;
*m_body_utf8_ptr= 0;
m_cpp_utf8_processed_ptr= end_ptr;
}
void Lex_input_stream::add_digest_token(uint token, LEX_YYSTYPE yylval)
{
if (m_digest != NULL)
{
m_digest= digest_add_token(m_digest, token, yylval);
}
}
void Lex_input_stream::reduce_digest_token(uint token_left, uint token_right)
{
if (m_digest != NULL)
{
m_digest= digest_reduce_token(m_digest, token_left, token_right);
}
}
/**
lex starting operations for builtin select collected together
*/
void SELECT_LEX::lex_start(LEX *plex)
{
SELECT_LEX_UNIT *unit= &plex->unit;
/* 'parent_lex' is used in init_query() so it must be before it. */
parent_lex= plex;
init_query();
master= unit;
prev= &unit->slave;
link_next= slave= next= 0;
link_prev= (st_select_lex_node**)&(plex->all_selects_list);
DBUG_ASSERT(!group_list_ptrs);
select_number= 1;
in_sum_expr=0;
ftfunc_list_alloc.empty();
ftfunc_list= &ftfunc_list_alloc;
group_list.empty();
order_list.empty();
gorder_list.empty();
}
void lex_start(THD *thd)
{
DBUG_ENTER("lex_start");
thd->lex->start(thd);
DBUG_VOID_RETURN;
}
/*
This is called before every query that is to be parsed.
Because of this, it's critical to not do too much things here.
(We already do too much here)
*/
void LEX::start(THD *thd_arg)
{
DBUG_ENTER("LEX::start");
DBUG_PRINT("info", ("This: %p thd_arg->lex: %p", this, thd_arg->lex));
thd= unit.thd= thd_arg;
stmt_lex= this; // default, should be rewritten for VIEWs And CTEs
DBUG_ASSERT(!explain);
builtin_select.lex_start(this);
lex_options= 0;
context_stack.empty();
//empty select_stack
select_stack_top= 0;
unit.init_query();
current_select_number= 0;
curr_with_clause= 0;
with_clauses_list= 0;
with_clauses_list_last_next= &with_clauses_list;
clone_spec_offset= 0;
create_view= NULL;
field_list.empty();
value_list.empty();
update_list.empty();
set_var_list.empty();
param_list.empty();
view_list.empty();
with_column_list.empty();
with_persistent_for_clause= FALSE;
column_list= NULL;
index_list= NULL;
prepared_stmt.lex_start();
auxiliary_table_list.empty();
unit.next= unit.master= unit.link_next= unit.return_to= 0;
unit.prev= unit.link_prev= 0;
unit.slave= current_select= all_selects_list= &builtin_select;
sql_cache= LEX::SQL_CACHE_UNSPECIFIED;
describe= 0;
analyze_stmt= 0;
explain_json= false;
context_analysis_only= 0;
derived_tables= 0;
safe_to_cache_query= 1;
parsing_options.reset();
empty_field_list_on_rset= 0;
part_info= 0;
m_sql_cmd= NULL;
duplicates= DUP_ERROR;
ignore= 0;
spname= NULL;
spcont= NULL;
proc_list.first= 0;
escape_used= FALSE;
default_used= FALSE;
query_tables= 0;
reset_query_tables_list(FALSE);
expr_allows_subselect= TRUE;
selects_allow_into= FALSE;
selects_allow_procedure= FALSE;
use_only_table_context= FALSE;
parse_vcol_expr= FALSE;
check_exists= FALSE;
create_info.lex_start();
verbose= 0;
name= null_clex_str;
event_parse_data= NULL;
profile_options= PROFILE_NONE;
nest_level= 0;
builtin_select.nest_level_base= &unit;
allow_sum_func.clear_all();
in_sum_func= NULL;
used_tables= 0;
table_type= TABLE_TYPE_UNKNOWN;
reset_slave_info.all= false;
limit_rows_examined= 0;
limit_rows_examined_cnt= ULONGLONG_MAX;
var_list.empty();
stmt_var_list.empty();
proc_list.elements=0;
save_group_list.empty();
save_order_list.empty();
win_ref= NULL;
win_frame= NULL;
frame_top_bound= NULL;
frame_bottom_bound= NULL;
win_spec= NULL;
vers_conditions.empty();
period_conditions.empty();
is_lex_started= TRUE;
next_is_main= FALSE;
next_is_down= FALSE;
wild= 0;
exchange= 0;
DBUG_VOID_RETURN;
}
void lex_end(LEX *lex)
{
DBUG_ENTER("lex_end");
DBUG_PRINT("enter", ("lex: %p", lex));
lex_end_stage1(lex);
lex_end_stage2(lex);
DBUG_VOID_RETURN;
}
void lex_end_stage1(LEX *lex)
{
DBUG_ENTER("lex_end_stage1");
/* release used plugins */
if (lex->plugins.elements) /* No function call and no mutex if no plugins. */
{
plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer,
lex->plugins.elements);
}
reset_dynamic(&lex->plugins);
if (lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_PREPARE)
{
/*
Don't delete lex->sphead, it'll be needed for EXECUTE.
Note that of all statements that populate lex->sphead
only SQLCOM_COMPOUND can be PREPAREd
*/
DBUG_ASSERT(lex->sphead == 0 || lex->sql_command == SQLCOM_COMPOUND);
}
else
{
delete lex->sphead;
lex->sphead= NULL;
}
DBUG_VOID_RETURN;
}
/*
MASTER INFO parameters (or state) is normally cleared towards the end
of a statement. But in case of PS, the state needs to be preserved during
its lifetime and should only be cleared on PS close or deallocation.
*/
void lex_end_stage2(LEX *lex)
{
DBUG_ENTER("lex_end_stage2");
/* Reset LEX_MASTER_INFO */
lex->mi.reset(lex->sql_command == SQLCOM_CHANGE_MASTER);
delete_dynamic(&lex->delete_gtid_domain);
DBUG_VOID_RETURN;
}
Yacc_state::~Yacc_state()
{
if (yacc_yyss)
{
my_free(yacc_yyss);
my_free(yacc_yyvs);
}
}
int Lex_input_stream::find_keyword(Lex_ident_cli_st *kwd,
uint len, bool function)
{
const char *tok= m_tok_start;
SYMBOL *symbol= get_hash_symbol(tok, len, function);
if (symbol)
{
kwd->set_keyword(tok, len);
DBUG_ASSERT(tok >= get_buf());
DBUG_ASSERT(tok < get_end_of_query());
if (m_thd->variables.sql_mode & MODE_ORACLE)
{
switch (symbol->tok) {
case BEGIN_MARIADB_SYM: return BEGIN_ORACLE_SYM;
case BLOB_MARIADB_SYM: return BLOB_ORACLE_SYM;
case BODY_MARIADB_SYM: return BODY_ORACLE_SYM;
case CLOB_MARIADB_SYM: return CLOB_ORACLE_SYM;
case CONTINUE_MARIADB_SYM: return CONTINUE_ORACLE_SYM;
case DECLARE_MARIADB_SYM: return DECLARE_ORACLE_SYM;
case DECODE_MARIADB_SYM: return DECODE_ORACLE_SYM;
case ELSEIF_MARIADB_SYM: return ELSEIF_ORACLE_SYM;
case ELSIF_MARIADB_SYM: return ELSIF_ORACLE_SYM;
case EXCEPTION_MARIADB_SYM: return EXCEPTION_ORACLE_SYM;
case EXIT_MARIADB_SYM: return EXIT_ORACLE_SYM;
case GOTO_MARIADB_SYM: return GOTO_ORACLE_SYM;
case NUMBER_MARIADB_SYM: return NUMBER_ORACLE_SYM;
case OTHERS_MARIADB_SYM: return OTHERS_ORACLE_SYM;
case PACKAGE_MARIADB_SYM: return PACKAGE_ORACLE_SYM;
case RAISE_MARIADB_SYM: return RAISE_ORACLE_SYM;
case RAW_MARIADB_SYM: return RAW_ORACLE_SYM;
case RETURN_MARIADB_SYM: return RETURN_ORACLE_SYM;
case ROWTYPE_MARIADB_SYM: return ROWTYPE_ORACLE_SYM;
case VARCHAR2_MARIADB_SYM: return VARCHAR2_ORACLE_SYM;
}
}
if ((symbol->tok == NOT_SYM) &&
(m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE))
return NOT2_SYM;
if ((symbol->tok == OR2_SYM) &&
(m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT))
{
return (m_thd->variables.sql_mode & MODE_ORACLE) ?
ORACLE_CONCAT_SYM : MYSQL_CONCAT_SYM;
}
return symbol->tok;
}
return 0;
}
/*
Check if name is a keyword
SYNOPSIS
is_keyword()
name checked name (must not be empty)
len length of checked name
RETURN VALUES
0 name is a keyword
1 name isn't a keyword
*/
bool is_keyword(const char *name, uint len)
{
DBUG_ASSERT(len != 0);
return get_hash_symbol(name,len,0)!=0;
}
/**
Check if name is a sql function
@param name checked name
@return is this a native function or not
@retval 0 name is a function
@retval 1 name isn't a function
*/
bool is_lex_native_function(const LEX_CSTRING *name)
{
DBUG_ASSERT(name != NULL);
return (get_hash_symbol(name->str, (uint) name->length, 1) != 0);
}
bool is_native_function(THD *thd, const LEX_CSTRING *name)
{
if (find_native_function_builder(thd, name))
return true;
if (is_lex_native_function(name))
return true;
return false;
}
bool is_native_function_with_warn(THD *thd, const LEX_CSTRING *name)
{
if (!is_native_function(thd, name))
return false;
/*
This warning will be printed when
[1] A client query is parsed,
[2] A stored function is loaded by db_load_routine.
Printing the warning for [2] is intentional, to cover the
following scenario:
- A user define a SF 'foo' using MySQL 5.N
- An application uses select foo(), and works.
- MySQL 5.{N+1} defines a new native function 'foo', as
part of a new feature.
- MySQL 5.{N+1} documentation is updated, and should mention
that there is a potential incompatible change in case of
existing stored function named 'foo'.
- The user deploys 5.{N+1}. At this point, 'select foo()'
means something different, and the user code is most likely
broken (it's only safe if the code is 'select db.foo()').
With a warning printed when the SF is loaded (which has to
occur before the call), the warning will provide a hint
explaining the root cause of a later failure of 'select foo()'.
With no warning printed, the user code will fail with no
apparent reason.
Printing a warning each time db_load_routine is executed for
an ambiguous function is annoying, since that can happen a lot,
but in practice should not happen unless there *are* name
collisions.
If a collision exists, it should not be silenced but fixed.
*/
push_warning_printf(thd,
Sql_condition::WARN_LEVEL_NOTE,
ER_NATIVE_FCT_NAME_COLLISION,
ER_THD(thd, ER_NATIVE_FCT_NAME_COLLISION),
name->str);
return true;
}
/* make a copy of token before ptr and set yytoklen */
LEX_CSTRING Lex_input_stream::get_token(uint skip, uint length)
{
LEX_CSTRING tmp;
yyUnget(); // ptr points now after last token char
tmp.length= length;
tmp.str= m_thd->strmake(m_tok_start + skip, tmp.length);
m_cpp_text_start= m_cpp_tok_start + skip;