1#ifndef ITEM_STRFUNC_INCLUDED
2#define ITEM_STRFUNC_INCLUDED
3
4/*
5 Copyright (c) 2000, 2011, Oracle and/or its affiliates.
6 Copyright (c) 2009, 2015, MariaDB
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
20
21
22/* This file defines all string functions */
23
24#ifdef USE_PRAGMA_INTERFACE
25#pragma interface /* gcc class implementation */
26#endif
27
28extern size_t username_char_length;
29
30class Item_str_func :public Item_func
31{
32protected:
33 /**
34 Sets the result value of the function an empty string, using the current
35 character set. No memory is allocated.
36 @retval A pointer to the str_value member.
37 */
38 virtual String *make_empty_result()
39 {
40 /*
41 Reset string length to an empty string. We don't use str_value.set() as
42 we don't want to free and potentially have to reallocate the buffer
43 for each call.
44 */
45 str_value.length(0);
46 str_value.set_charset(collation.collation);
47 return &str_value;
48 }
49public:
50 Item_str_func(THD *thd): Item_func(thd) { decimals=NOT_FIXED_DEC; }
51 Item_str_func(THD *thd, Item *a): Item_func(thd, a) {decimals=NOT_FIXED_DEC; }
52 Item_str_func(THD *thd, Item *a, Item *b):
53 Item_func(thd, a, b) { decimals=NOT_FIXED_DEC; }
54 Item_str_func(THD *thd, Item *a, Item *b, Item *c):
55 Item_func(thd, a, b, c) { decimals=NOT_FIXED_DEC; }
56 Item_str_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
57 Item_func(thd, a, b, c, d) { decimals=NOT_FIXED_DEC; }
58 Item_str_func(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e):
59 Item_func(thd, a, b, c, d, e) { decimals=NOT_FIXED_DEC; }
60 Item_str_func(THD *thd, List<Item> &list):
61 Item_func(thd, list) { decimals=NOT_FIXED_DEC; }
62 longlong val_int();
63 double val_real();
64 my_decimal *val_decimal(my_decimal *);
65 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
66 { return get_date_from_string(ltime, fuzzydate); }
67 const Type_handler *type_handler() const { return string_type_handler(); }
68 void left_right_max_length();
69 bool fix_fields(THD *thd, Item **ref);
70 void update_null_value()
71 {
72 StringBuffer<MAX_FIELD_WIDTH> tmp;
73 (void) val_str(&tmp);
74 }
75};
76
77
78
79/*
80 Functions that return values with ASCII repertoire
81*/
82class Item_str_ascii_func :public Item_str_func
83{
84 String ascii_buf;
85public:
86 Item_str_ascii_func(THD *thd): Item_str_func(thd) {}
87 Item_str_ascii_func(THD *thd, Item *a): Item_str_func(thd, a) {}
88 Item_str_ascii_func(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
89 Item_str_ascii_func(THD *thd, Item *a, Item *b, Item *c):
90 Item_str_func(thd, a, b, c) {}
91 String *val_str(String *str)
92 {
93 return val_str_from_val_str_ascii(str, &ascii_buf);
94 }
95 String *val_str_ascii(String *)= 0;
96};
97
98
99/**
100 Functions that return a checksum or a hash of the argument,
101 or somehow else encode or decode the argument,
102 returning an ASCII-repertoire string.
103*/
104class Item_str_ascii_checksum_func: public Item_str_ascii_func
105{
106public:
107 Item_str_ascii_checksum_func(THD *thd, Item *a)
108 :Item_str_ascii_func(thd, a) { }
109 Item_str_ascii_checksum_func(THD *thd, Item *a, Item *b)
110 :Item_str_ascii_func(thd, a, b) { }
111 bool eq(const Item *item, bool binary_cmp) const
112 {
113 // Always use binary argument comparison: MD5('x') != MD5('X')
114 return Item_func::eq(item, true);
115 }
116};
117
118
119/**
120 Functions that return a checksum or a hash of the argument,
121 or somehow else encode or decode the argument,
122 returning a binary string.
123*/
124class Item_str_binary_checksum_func: public Item_str_func
125{
126public:
127 Item_str_binary_checksum_func(THD *thd, Item *a)
128 :Item_str_func(thd, a) { }
129 Item_str_binary_checksum_func(THD *thd, Item *a, Item *b)
130 :Item_str_func(thd, a, b) { }
131 bool eq(const Item *item, bool binary_cmp) const
132 {
133 /*
134 Always use binary argument comparison:
135 FROM_BASE64('test') != FROM_BASE64('TEST')
136 */
137 return Item_func::eq(item, true);
138 }
139};
140
141
142class Item_func_md5 :public Item_str_ascii_checksum_func
143{
144public:
145 Item_func_md5(THD *thd, Item *a): Item_str_ascii_checksum_func(thd, a) {}
146 String *val_str_ascii(String *);
147 void fix_length_and_dec()
148 {
149 fix_length_and_charset(32, default_charset());
150 }
151 const char *func_name() const { return "md5"; }
152 Item *get_copy(THD *thd)
153 { return get_item_copy<Item_func_md5>(thd, this); }
154};
155
156
157class Item_func_sha :public Item_str_ascii_checksum_func
158{
159public:
160 Item_func_sha(THD *thd, Item *a): Item_str_ascii_checksum_func(thd, a) {}
161 String *val_str_ascii(String *);
162 void fix_length_and_dec();
163 const char *func_name() const { return "sha"; }
164 Item *get_copy(THD *thd)
165 { return get_item_copy<Item_func_sha>(thd, this); }
166};
167
168class Item_func_sha2 :public Item_str_ascii_checksum_func
169{
170public:
171 Item_func_sha2(THD *thd, Item *a, Item *b)
172 :Item_str_ascii_checksum_func(thd, a, b) {}
173 String *val_str_ascii(String *);
174 void fix_length_and_dec();
175 const char *func_name() const { return "sha2"; }
176 Item *get_copy(THD *thd)
177 { return get_item_copy<Item_func_sha2>(thd, this); }
178};
179
180class Item_func_to_base64 :public Item_str_ascii_checksum_func
181{
182 String tmp_value;
183public:
184 Item_func_to_base64(THD *thd, Item *a)
185 :Item_str_ascii_checksum_func(thd, a) {}
186 String *val_str_ascii(String *);
187 void fix_length_and_dec();
188 const char *func_name() const { return "to_base64"; }
189 Item *get_copy(THD *thd)
190 { return get_item_copy<Item_func_to_base64>(thd, this); }
191};
192
193class Item_func_from_base64 :public Item_str_binary_checksum_func
194{
195 String tmp_value;
196public:
197 Item_func_from_base64(THD *thd, Item *a)
198 :Item_str_binary_checksum_func(thd, a) { }
199 String *val_str(String *);
200 void fix_length_and_dec();
201 const char *func_name() const { return "from_base64"; }
202 Item *get_copy(THD *thd)
203 { return get_item_copy<Item_func_from_base64>(thd, this); }
204};
205
206#include <my_crypt.h>
207
208class Item_aes_crypt :public Item_str_binary_checksum_func
209{
210 enum { AES_KEY_LENGTH = 128 };
211 void create_key(String *user_key, uchar* key);
212
213protected:
214 int what;
215 String tmp_value;
216public:
217 Item_aes_crypt(THD *thd, Item *a, Item *b)
218 :Item_str_binary_checksum_func(thd, a, b) {}
219 String *val_str(String *);
220};
221
222class Item_func_aes_encrypt :public Item_aes_crypt
223{
224public:
225 Item_func_aes_encrypt(THD *thd, Item *a, Item *b)
226 :Item_aes_crypt(thd, a, b) {}
227 void fix_length_and_dec();
228 const char *func_name() const { return "aes_encrypt"; }
229 Item *get_copy(THD *thd)
230 { return get_item_copy<Item_func_aes_encrypt>(thd, this); }
231};
232
233class Item_func_aes_decrypt :public Item_aes_crypt
234{
235public:
236 Item_func_aes_decrypt(THD *thd, Item *a, Item *b):
237 Item_aes_crypt(thd, a, b) {}
238 void fix_length_and_dec();
239 const char *func_name() const { return "aes_decrypt"; }
240 Item *get_copy(THD *thd)
241 { return get_item_copy<Item_func_aes_decrypt>(thd, this); }
242};
243
244
245class Item_func_concat :public Item_str_func
246{
247protected:
248 String tmp_value;
249 /*
250 Append a non-NULL value to the result.
251 @param [IN] thd - The current thread.
252 @param [IN/OUT] res - The current val_str() return value.
253 @param [IN] app - The value to be appended.
254 @retval - false on success, true on error
255 */
256 bool append_value(THD *thd, String *res, const String *app);
257 bool realloc_result(String *str, uint length) const;
258public:
259 Item_func_concat(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
260 Item_func_concat(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
261 String *val_str(String *);
262 void fix_length_and_dec();
263 const char *func_name() const { return "concat"; }
264 Item *get_copy(THD *thd)
265 { return get_item_copy<Item_func_concat>(thd, this); }
266};
267
268
269/*
270 This class handles the || operator in sql_mode=ORACLE.
271 Unlike the traditional MariaDB concat(), it treats NULL arguments as ''.
272*/
273class Item_func_concat_operator_oracle :public Item_func_concat
274{
275public:
276 Item_func_concat_operator_oracle(THD *thd, List<Item> &list)
277 :Item_func_concat(thd, list)
278 { }
279 Item_func_concat_operator_oracle(THD *thd, Item *a, Item *b)
280 :Item_func_concat(thd, a, b)
281 { }
282 String *val_str(String *);
283 const char *func_name() const { return "concat_operator_oracle"; }
284 Item *get_copy(THD *thd)
285 {
286 return get_item_copy<Item_func_concat_operator_oracle>(thd, this);
287 }
288};
289
290
291class Item_func_decode_histogram :public Item_str_func
292{
293public:
294 Item_func_decode_histogram(THD *thd, Item *a, Item *b):
295 Item_str_func(thd, a, b) {}
296 String *val_str(String *);
297 void fix_length_and_dec()
298 {
299 collation.set(system_charset_info);
300 max_length= MAX_BLOB_WIDTH;
301 maybe_null= 1;
302 }
303 const char *func_name() const { return "decode_histogram"; }
304 Item *get_copy(THD *thd)
305 { return get_item_copy<Item_func_decode_histogram>(thd, this); }
306};
307
308class Item_func_concat_ws :public Item_str_func
309{
310 String tmp_value;
311public:
312 Item_func_concat_ws(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
313 String *val_str(String *);
314 void fix_length_and_dec();
315 const char *func_name() const { return "concat_ws"; }
316 table_map not_null_tables() const { return 0; }
317 Item *get_copy(THD *thd)
318 { return get_item_copy<Item_func_concat_ws>(thd, this); }
319};
320
321class Item_func_reverse :public Item_str_func
322{
323 String tmp_value;
324public:
325 Item_func_reverse(THD *thd, Item *a): Item_str_func(thd, a) {}
326 String *val_str(String *);
327 void fix_length_and_dec();
328 const char *func_name() const { return "reverse"; }
329 Item *get_copy(THD *thd)
330 { return get_item_copy<Item_func_reverse>(thd, this); }
331};
332
333
334class Item_func_replace :public Item_str_func
335{
336 String tmp_value,tmp_value2;
337public:
338 Item_func_replace(THD *thd, Item *org, Item *find, Item *replace):
339 Item_str_func(thd, org, find, replace) {}
340 String *val_str(String *to) { return val_str_internal(to, NULL); };
341 void fix_length_and_dec();
342 String *val_str_internal(String *str, String *empty_string_for_null);
343 const char *func_name() const { return "replace"; }
344 Item *get_copy(THD *thd)
345 { return get_item_copy<Item_func_replace>(thd, this); }
346};
347
348
349class Item_func_replace_oracle :public Item_func_replace
350{
351 String tmp_emtpystr;
352public:
353 Item_func_replace_oracle(THD *thd, Item *org, Item *find, Item *replace):
354 Item_func_replace(thd, org, find, replace) {}
355 String *val_str(String *to) { return val_str_internal(to, &tmp_emtpystr); };
356 const char *func_name() const { return "replace_oracle"; }
357 Item *get_copy(THD *thd)
358 { return get_item_copy<Item_func_replace_oracle>(thd, this); }
359};
360
361
362class Item_func_regexp_replace :public Item_str_func
363{
364 Regexp_processor_pcre re;
365 bool append_replacement(String *str,
366 const LEX_CSTRING *source,
367 const LEX_CSTRING *replace);
368public:
369 Item_func_regexp_replace(THD *thd, Item *a, Item *b, Item *c):
370 Item_str_func(thd, a, b, c)
371 {}
372 void cleanup()
373 {
374 DBUG_ENTER("Item_func_regex::cleanup");
375 Item_str_func::cleanup();
376 re.cleanup();
377 DBUG_VOID_RETURN;
378 }
379 String *val_str(String *str);
380 bool fix_fields(THD *thd, Item **ref);
381 void fix_length_and_dec();
382 const char *func_name() const { return "regexp_replace"; }
383 Item *get_copy(THD *thd) { return 0;}
384};
385
386
387class Item_func_regexp_substr :public Item_str_func
388{
389 Regexp_processor_pcre re;
390public:
391 Item_func_regexp_substr(THD *thd, Item *a, Item *b):
392 Item_str_func(thd, a, b)
393 {}
394 void cleanup()
395 {
396 DBUG_ENTER("Item_func_regex::cleanup");
397 Item_str_func::cleanup();
398 re.cleanup();
399 DBUG_VOID_RETURN;
400 }
401 String *val_str(String *str);
402 bool fix_fields(THD *thd, Item **ref);
403 void fix_length_and_dec();
404 const char *func_name() const { return "regexp_substr"; }
405 Item *get_copy(THD *thd) { return 0; }
406};
407
408
409class Item_func_insert :public Item_str_func
410{
411 String tmp_value;
412public:
413 Item_func_insert(THD *thd, Item *org, Item *start, Item *length,
414 Item *new_str):
415 Item_str_func(thd, org, start, length, new_str) {}
416 String *val_str(String *);
417 void fix_length_and_dec();
418 const char *func_name() const { return "insert"; }
419 Item *get_copy(THD *thd)
420 { return get_item_copy<Item_func_insert>(thd, this); }
421};
422
423
424class Item_str_conv :public Item_str_func
425{
426protected:
427 uint multiply;
428 my_charset_conv_case converter;
429 String tmp_value;
430public:
431 Item_str_conv(THD *thd, Item *item): Item_str_func(thd, item) {}
432 String *val_str(String *);
433};
434
435
436class Item_func_lcase :public Item_str_conv
437{
438public:
439 Item_func_lcase(THD *thd, Item *item): Item_str_conv(thd, item) {}
440 const char *func_name() const { return "lcase"; }
441 void fix_length_and_dec();
442 Item *get_copy(THD *thd)
443 { return get_item_copy<Item_func_lcase>(thd, this); }
444};
445
446class Item_func_ucase :public Item_str_conv
447{
448public:
449 Item_func_ucase(THD *thd, Item *item): Item_str_conv(thd, item) {}
450 const char *func_name() const { return "ucase"; }
451 void fix_length_and_dec();
452 Item *get_copy(THD *thd)
453 { return get_item_copy<Item_func_ucase>(thd, this); }
454};
455
456
457class Item_func_left :public Item_str_func
458{
459 String tmp_value;
460public:
461 Item_func_left(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
462 String *val_str(String *);
463 void fix_length_and_dec();
464 const char *func_name() const { return "left"; }
465 Item *get_copy(THD *thd)
466 { return get_item_copy<Item_func_left>(thd, this); }
467};
468
469
470class Item_func_right :public Item_str_func
471{
472 String tmp_value;
473public:
474 Item_func_right(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
475 String *val_str(String *);
476 void fix_length_and_dec();
477 const char *func_name() const { return "right"; }
478 Item *get_copy(THD *thd)
479 { return get_item_copy<Item_func_right>(thd, this); }
480};
481
482
483class Item_func_substr :public Item_str_func
484{
485 String tmp_value;
486protected:
487 virtual longlong get_position() { return args[1]->val_int(); }
488public:
489 Item_func_substr(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
490 Item_func_substr(THD *thd, Item *a, Item *b, Item *c):
491 Item_str_func(thd, a, b, c) {}
492 String *val_str(String *);
493 void fix_length_and_dec();
494 const char *func_name() const { return "substr"; }
495 Item *get_copy(THD *thd)
496 { return get_item_copy<Item_func_substr>(thd, this); }
497};
498
499class Item_func_substr_oracle :public Item_func_substr
500{
501protected:
502 longlong get_position()
503 { longlong pos= args[1]->val_int(); return pos == 0 ? 1 : pos; }
504 String *make_empty_result()
505 { null_value= 1; return NULL; }
506public:
507 Item_func_substr_oracle(THD *thd, Item *a, Item *b):
508 Item_func_substr(thd, a, b) {}
509 Item_func_substr_oracle(THD *thd, Item *a, Item *b, Item *c):
510 Item_func_substr(thd, a, b, c) {}
511 void fix_length_and_dec()
512 {
513 Item_func_substr::fix_length_and_dec();
514 maybe_null= true;
515 }
516 const char *func_name() const { return "substr_oracle"; }
517 Item *get_copy(THD *thd)
518 { return get_item_copy<Item_func_substr_oracle>(thd, this); }
519};
520
521class Item_func_substr_index :public Item_str_func
522{
523 String tmp_value;
524public:
525 Item_func_substr_index(THD *thd, Item *a,Item *b,Item *c):
526 Item_str_func(thd, a, b, c) {}
527 String *val_str(String *);
528 void fix_length_and_dec();
529 const char *func_name() const { return "substring_index"; }
530 Item *get_copy(THD *thd)
531 { return get_item_copy<Item_func_substr_index>(thd, this); }
532
533};
534
535
536class Item_func_trim :public Item_str_func
537{
538protected:
539 String tmp_value;
540 String remove;
541 String *trimmed_value(String *res, uint32 offset, uint32 length)
542 {
543 if (length == 0)
544 return make_empty_result();
545
546 tmp_value.set(*res, offset, length);
547 /*
548 Make sure to return correct charset and collation:
549 TRIM(0x000000 FROM _ucs2 0x0061)
550 should set charset to "binary" rather than to "ucs2".
551 */
552 tmp_value.set_charset(collation.collation);
553 return &tmp_value;
554 }
555 String *non_trimmed_value(String *res)
556 {
557 return trimmed_value(res, 0, res->length());
558 }
559 virtual const char *func_name_ext() const { return ""; }
560public:
561 Item_func_trim(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
562 Item_func_trim(THD *thd, Item *a): Item_str_func(thd, a) {}
563 String *val_str(String *);
564 void fix_length_and_dec();
565 const char *func_name() const { return "trim"; }
566 void print(String *str, enum_query_type query_type);
567 virtual const char *mode_name() const { return "both"; }
568 Item *get_copy(THD *thd)
569 { return get_item_copy<Item_func_trim>(thd, this); }
570};
571
572
573class Item_func_trim_oracle :public Item_func_trim
574{
575protected:
576 String *make_empty_result()
577 { null_value= 1; return NULL; }
578 const char *func_name_ext() const { return "_oracle"; }
579public:
580 Item_func_trim_oracle(THD *thd, Item *a, Item *b):
581 Item_func_trim(thd, a, b) {}
582 Item_func_trim_oracle(THD *thd, Item *a): Item_func_trim(thd, a) {}
583 const char *func_name() const { return "trim_oracle"; }
584 void fix_length_and_dec()
585 {
586 Item_func_trim::fix_length_and_dec();
587 maybe_null= true;
588 }
589 Item *get_copy(THD *thd)
590 { return get_item_copy<Item_func_trim_oracle>(thd, this); }
591};
592
593
594class Item_func_ltrim :public Item_func_trim
595{
596public:
597 Item_func_ltrim(THD *thd, Item *a, Item *b): Item_func_trim(thd, a, b) {}
598 Item_func_ltrim(THD *thd, Item *a): Item_func_trim(thd, a) {}
599 String *val_str(String *);
600 const char *func_name() const { return "ltrim"; }
601 const char *mode_name() const { return "leading"; }
602 Item *get_copy(THD *thd)
603 { return get_item_copy<Item_func_ltrim>(thd, this); }
604};
605
606
607class Item_func_ltrim_oracle :public Item_func_ltrim
608{
609protected:
610 String *make_empty_result()
611 { null_value= 1; return NULL; }
612 const char *func_name_ext() const { return "_oracle"; }
613public:
614 Item_func_ltrim_oracle(THD *thd, Item *a, Item *b):
615 Item_func_ltrim(thd, a, b) {}
616 Item_func_ltrim_oracle(THD *thd, Item *a): Item_func_ltrim(thd, a) {}
617 const char *func_name() const { return "ltrim_oracle"; }
618 void fix_length_and_dec()
619 {
620 Item_func_ltrim::fix_length_and_dec();
621 maybe_null= true;
622 }
623 Item *get_copy(THD *thd)
624 { return get_item_copy<Item_func_ltrim_oracle>(thd, this); }
625};
626
627
628class Item_func_rtrim :public Item_func_trim
629{
630public:
631 Item_func_rtrim(THD *thd, Item *a, Item *b): Item_func_trim(thd, a, b) {}
632 Item_func_rtrim(THD *thd, Item *a): Item_func_trim(thd, a) {}
633 String *val_str(String *);
634 const char *func_name() const { return "rtrim"; }
635 const char *mode_name() const { return "trailing"; }
636 Item *get_copy(THD *thd)
637 { return get_item_copy<Item_func_rtrim>(thd, this); }
638};
639
640
641class Item_func_rtrim_oracle :public Item_func_rtrim
642{
643protected:
644 String *make_empty_result()
645 { null_value= 1; return NULL; }
646 const char *func_name_ext() const { return "_oracle"; }
647public:
648 Item_func_rtrim_oracle(THD *thd, Item *a, Item *b):
649 Item_func_rtrim(thd, a, b) {}
650 Item_func_rtrim_oracle(THD *thd, Item *a): Item_func_rtrim(thd, a) {}
651 const char *func_name() const { return "rtrim_oracle"; }
652 void fix_length_and_dec()
653 {
654 Item_func_rtrim::fix_length_and_dec();
655 maybe_null= true;
656 }
657 Item *get_copy(THD *thd)
658 { return get_item_copy<Item_func_rtrim_oracle>(thd, this); }
659};
660
661/*
662 Item_func_password -- new (4.1.1) PASSWORD() function implementation.
663 Returns strcat('*', octet2hex(sha1(sha1(password)))). '*' stands for new
664 password format, sha1(sha1(password) is so-called hash_stage2 value.
665 Length of returned string is always 41 byte. To find out how entire
666 authentication procedure works, see comments in password.c.
667*/
668
669class Item_func_password :public Item_str_ascii_checksum_func
670{
671public:
672 enum PW_Alg {OLD, NEW};
673private:
674 char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH+1];
675 enum PW_Alg alg;
676 bool deflt;
677public:
678 Item_func_password(THD *thd, Item *a):
679 Item_str_ascii_checksum_func(thd, a), alg(NEW), deflt(1) {}
680 Item_func_password(THD *thd, Item *a, PW_Alg al):
681 Item_str_ascii_checksum_func(thd, a), alg(al), deflt(0) {}
682 String *val_str_ascii(String *str);
683 bool fix_fields(THD *thd, Item **ref);
684 void fix_length_and_dec()
685 {
686 fix_length_and_charset((alg == 1 ?
687 SCRAMBLED_PASSWORD_CHAR_LENGTH :
688 SCRAMBLED_PASSWORD_CHAR_LENGTH_323),
689 default_charset());
690 }
691 const char *func_name() const { return ((deflt || alg == 1) ?
692 "password" : "old_password"); }
693 static char *alloc(THD *thd, const char *password, size_t pass_len,
694 enum PW_Alg al);
695 Item *get_copy(THD *thd)
696 { return get_item_copy<Item_func_password>(thd, this); }
697};
698
699
700
701class Item_func_des_encrypt :public Item_str_binary_checksum_func
702{
703 String tmp_value,tmp_arg;
704public:
705 Item_func_des_encrypt(THD *thd, Item *a)
706 :Item_str_binary_checksum_func(thd, a) {}
707 Item_func_des_encrypt(THD *thd, Item *a, Item *b)
708 :Item_str_binary_checksum_func(thd, a, b) {}
709 String *val_str(String *);
710 void fix_length_and_dec()
711 {
712 maybe_null=1;
713 /* 9 = MAX ((8- (arg_len % 8)) + 1) */
714 max_length = args[0]->max_length + 9;
715 }
716 const char *func_name() const { return "des_encrypt"; }
717 Item *get_copy(THD *thd)
718 { return get_item_copy<Item_func_des_encrypt>(thd, this); }
719};
720
721class Item_func_des_decrypt :public Item_str_binary_checksum_func
722{
723 String tmp_value;
724public:
725 Item_func_des_decrypt(THD *thd, Item *a)
726 :Item_str_binary_checksum_func(thd, a) {}
727 Item_func_des_decrypt(THD *thd, Item *a, Item *b)
728 :Item_str_binary_checksum_func(thd, a, b) {}
729 String *val_str(String *);
730 void fix_length_and_dec()
731 {
732 maybe_null=1;
733 /* 9 = MAX ((8- (arg_len % 8)) + 1) */
734 max_length= args[0]->max_length;
735 if (max_length >= 9U)
736 max_length-= 9U;
737 }
738 const char *func_name() const { return "des_decrypt"; }
739 Item *get_copy(THD *thd)
740 { return get_item_copy<Item_func_des_decrypt>(thd, this); }
741};
742
743
744/**
745 QQ: Item_func_encrypt should derive from Item_str_ascii_checksum_func.
746 However, it should be fixed to handle UCS2, UTF16, UTF32 properly first,
747 as the underlying crypt() call expects a null-terminated input string.
748*/
749class Item_func_encrypt :public Item_str_binary_checksum_func
750{
751 String tmp_value;
752
753 /* Encapsulate common constructor actions */
754 void constructor_helper()
755 {
756 collation.set(&my_charset_bin);
757 }
758public:
759 Item_func_encrypt(THD *thd, Item *a): Item_str_binary_checksum_func(thd, a)
760 {
761 constructor_helper();
762 }
763 Item_func_encrypt(THD *thd, Item *a, Item *b)
764 :Item_str_binary_checksum_func(thd, a, b)
765 {
766 constructor_helper();
767 }
768 String *val_str(String *);
769 void fix_length_and_dec() { maybe_null=1; max_length = 13; }
770 const char *func_name() const { return "encrypt"; }
771 bool check_vcol_func_processor(void *arg)
772 {
773 return FALSE;
774 }
775 Item *get_copy(THD *thd)
776 { return get_item_copy<Item_func_encrypt>(thd, this); }
777};
778
779#include "sql_crypt.h"
780
781
782class Item_func_encode :public Item_str_binary_checksum_func
783{
784private:
785 /** Whether the PRNG has already been seeded. */
786 bool seeded;
787protected:
788 SQL_CRYPT sql_crypt;
789public:
790 Item_func_encode(THD *thd, Item *a, Item *seed_arg):
791 Item_str_binary_checksum_func(thd, a, seed_arg) {}
792 String *val_str(String *);
793 void fix_length_and_dec();
794 const char *func_name() const { return "encode"; }
795 Item *get_copy(THD *thd)
796 { return get_item_copy<Item_func_encode>(thd, this); }
797protected:
798 virtual void crypto_transform(String *);
799private:
800 /** Provide a seed for the PRNG sequence. */
801 bool seed();
802};
803
804
805class Item_func_decode :public Item_func_encode
806{
807public:
808 Item_func_decode(THD *thd, Item *a, Item *seed_arg): Item_func_encode(thd, a, seed_arg) {}
809 const char *func_name() const { return "decode"; }
810 Item *get_copy(THD *thd)
811 { return get_item_copy<Item_func_decode>(thd, this); }
812protected:
813 void crypto_transform(String *);
814};
815
816
817class Item_func_sysconst :public Item_str_func
818{
819public:
820 Item_func_sysconst(THD *thd): Item_str_func(thd)
821 { collation.set(system_charset_info,DERIVATION_SYSCONST); }
822 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
823 /*
824 Used to create correct Item name in new converted item in
825 safe_charset_converter, return string representation of this function
826 call
827 */
828 virtual const char *fully_qualified_func_name() const = 0;
829 bool check_vcol_func_processor(void *arg)
830 {
831 return mark_unsupported_function(fully_qualified_func_name(), arg,
832 VCOL_SESSION_FUNC);
833 }
834 bool const_item() const;
835};
836
837
838class Item_func_database :public Item_func_sysconst
839{
840public:
841 Item_func_database(THD *thd): Item_func_sysconst(thd) {}
842 String *val_str(String *);
843 void fix_length_and_dec()
844 {
845 max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
846 maybe_null=1;
847 }
848 const char *func_name() const { return "database"; }
849 const char *fully_qualified_func_name() const { return "database()"; }
850 Item *get_copy(THD *thd)
851 { return get_item_copy<Item_func_database>(thd, this); }
852};
853
854
855class Item_func_sqlerrm :public Item_func_sysconst
856{
857public:
858 Item_func_sqlerrm(THD *thd): Item_func_sysconst(thd) {}
859 String *val_str(String *);
860 const char *func_name() const { return "SQLERRM"; }
861 const char *fully_qualified_func_name() const { return "SQLERRM"; }
862 void print(String *str, enum_query_type query_type)
863 {
864 str->append(func_name());
865 }
866 void fix_length_and_dec()
867 {
868 max_length= 512 * system_charset_info->mbmaxlen;
869 null_value= maybe_null= false;
870 }
871 Item *get_copy(THD *thd)
872 { return get_item_copy<Item_func_sqlerrm>(thd, this); }
873};
874
875
876class Item_func_user :public Item_func_sysconst
877{
878protected:
879 bool init (const char *user, const char *host);
880
881public:
882 Item_func_user(THD *thd): Item_func_sysconst(thd)
883 {
884 str_value.set("", 0, system_charset_info);
885 }
886 String *val_str(String *)
887 {
888 DBUG_ASSERT(fixed == 1);
889 return (null_value ? 0 : &str_value);
890 }
891 bool fix_fields(THD *thd, Item **ref);
892 void fix_length_and_dec()
893 {
894 max_length= (uint32) (username_char_length +
895 HOSTNAME_LENGTH + 1) * SYSTEM_CHARSET_MBMAXLEN;
896 }
897 const char *func_name() const { return "user"; }
898 const char *fully_qualified_func_name() const { return "user()"; }
899 int save_in_field(Field *field, bool no_conversions)
900 {
901 return save_str_value_in_field(field, &str_value);
902 }
903 Item *get_copy(THD *thd)
904 { return get_item_copy<Item_func_user>(thd, this); }
905};
906
907
908class Item_func_current_user :public Item_func_user
909{
910 Name_resolution_context *context;
911
912public:
913 Item_func_current_user(THD *thd, Name_resolution_context *context_arg):
914 Item_func_user(thd), context(context_arg) {}
915 bool fix_fields(THD *thd, Item **ref);
916 const char *func_name() const { return "current_user"; }
917 const char *fully_qualified_func_name() const { return "current_user()"; }
918 bool check_vcol_func_processor(void *arg)
919 {
920 context= 0;
921 return mark_unsupported_function(fully_qualified_func_name(), arg,
922 VCOL_SESSION_FUNC);
923 }
924};
925
926
927class Item_func_current_role :public Item_func_sysconst
928{
929 Name_resolution_context *context;
930
931public:
932 Item_func_current_role(THD *thd, Name_resolution_context *context_arg):
933 Item_func_sysconst(thd), context(context_arg) {}
934 bool fix_fields(THD *thd, Item **ref);
935 void fix_length_and_dec()
936 { max_length= (uint32) username_char_length * SYSTEM_CHARSET_MBMAXLEN; }
937 int save_in_field(Field *field, bool no_conversions)
938 { return save_str_value_in_field(field, &str_value); }
939 const char *func_name() const { return "current_role"; }
940 const char *fully_qualified_func_name() const { return "current_role()"; }
941 String *val_str(String *)
942 {
943 DBUG_ASSERT(fixed == 1);
944 return null_value ? NULL : &str_value;
945 }
946 bool check_vcol_func_processor(void *arg)
947 {
948
949 context= 0;
950 return mark_unsupported_function(fully_qualified_func_name(), arg,
951 VCOL_SESSION_FUNC);
952 }
953 Item *get_copy(THD *thd)
954 { return get_item_copy<Item_func_current_role>(thd, this); }
955};
956
957
958class Item_func_soundex :public Item_str_func
959{
960 String tmp_value;
961public:
962 Item_func_soundex(THD *thd, Item *a): Item_str_func(thd, a) {}
963 String *val_str(String *);
964 void fix_length_and_dec();
965 const char *func_name() const { return "soundex"; }
966 Item *get_copy(THD *thd)
967 { return get_item_copy<Item_func_soundex>(thd, this); }
968};
969
970
971class Item_func_elt :public Item_str_func
972{
973public:
974 Item_func_elt(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
975 double val_real();
976 longlong val_int();
977 String *val_str(String *str);
978 void fix_length_and_dec();
979 const char *func_name() const { return "elt"; }
980 Item *get_copy(THD *thd)
981 { return get_item_copy<Item_func_elt>(thd, this); }
982};
983
984
985class Item_func_make_set :public Item_str_func
986{
987 String tmp_str;
988
989public:
990 Item_func_make_set(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
991 String *val_str(String *str);
992 void fix_length_and_dec();
993 const char *func_name() const { return "make_set"; }
994 Item *get_copy(THD *thd)
995 { return get_item_copy<Item_func_make_set>(thd, this); }
996};
997
998
999class Item_func_format :public Item_str_ascii_func
1000{
1001 const MY_LOCALE *locale;
1002public:
1003 Item_func_format(THD *thd, Item *org, Item *dec):
1004 Item_str_ascii_func(thd, org, dec) {}
1005 Item_func_format(THD *thd, Item *org, Item *dec, Item *lang):
1006 Item_str_ascii_func(thd, org, dec, lang) {}
1007
1008 String *val_str_ascii(String *);
1009 void fix_length_and_dec();
1010 const char *func_name() const { return "format"; }
1011 Item *get_copy(THD *thd)
1012 { return get_item_copy<Item_func_format>(thd, this); }
1013};
1014
1015
1016class Item_func_char :public Item_str_func
1017{
1018public:
1019 Item_func_char(THD *thd, List<Item> &list): Item_str_func(thd, list)
1020 { collation.set(&my_charset_bin); }
1021 Item_func_char(THD *thd, List<Item> &list, CHARSET_INFO *cs):
1022 Item_str_func(thd, list)
1023 { collation.set(cs); }
1024 Item_func_char(THD *thd, Item *arg1, CHARSET_INFO *cs):
1025 Item_str_func(thd, arg1)
1026 { collation.set(cs); }
1027 String *val_str(String *);
1028 void append_char(String * str, int32 num);
1029 void fix_length_and_dec()
1030 {
1031 max_length= arg_count * 4;
1032 }
1033 const char *func_name() const { return "char"; }
1034 void print(String *str, enum_query_type query_type);
1035 Item *get_copy(THD *thd)
1036 { return get_item_copy<Item_func_char>(thd, this); }
1037};
1038
1039class Item_func_chr :public Item_func_char
1040{
1041public:
1042 Item_func_chr(THD *thd, Item *arg1, CHARSET_INFO *cs):
1043 Item_func_char(thd, arg1, cs) {}
1044 String *val_str(String *);
1045 void fix_length_and_dec()
1046 {
1047 max_length= 4;
1048 }
1049 const char *func_name() const { return "chr"; }
1050 Item *get_copy(THD *thd)
1051 { return get_item_copy<Item_func_chr>(thd, this); }
1052};
1053
1054class Item_func_repeat :public Item_str_func
1055{
1056 String tmp_value;
1057public:
1058 Item_func_repeat(THD *thd, Item *arg1, Item *arg2):
1059 Item_str_func(thd, arg1, arg2) {}
1060 String *val_str(String *);
1061 void fix_length_and_dec();
1062 const char *func_name() const { return "repeat"; }
1063 Item *get_copy(THD *thd)
1064 { return get_item_copy<Item_func_repeat>(thd, this); }
1065};
1066
1067
1068class Item_func_space :public Item_str_func
1069{
1070public:
1071 Item_func_space(THD *thd, Item *arg1): Item_str_func(thd, arg1) {}
1072 String *val_str(String *);
1073 void fix_length_and_dec();
1074 const char *func_name() const { return "space"; }
1075 Item *get_copy(THD *thd)
1076 { return get_item_copy<Item_func_space>(thd, this); }
1077};
1078
1079
1080class Item_func_binlog_gtid_pos :public Item_str_func
1081{
1082public:
1083 Item_func_binlog_gtid_pos(THD *thd, Item *arg1, Item *arg2):
1084 Item_str_func(thd, arg1, arg2) {}
1085 String *val_str(String *);
1086 void fix_length_and_dec();
1087 const char *func_name() const { return "binlog_gtid_pos"; }
1088 bool check_vcol_func_processor(void *arg)
1089 {
1090 return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
1091 }
1092 Item *get_copy(THD *thd)
1093 { return get_item_copy<Item_func_binlog_gtid_pos>(thd, this); }
1094};
1095
1096
1097class Item_func_pad: public Item_str_func
1098{
1099protected:
1100 String tmp_value, pad_str;
1101public:
1102 Item_func_pad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1103 Item_str_func(thd, arg1, arg2, arg3) {}
1104 Item_func_pad(THD *thd, Item *arg1, Item *arg2):
1105 Item_str_func(thd, arg1, arg2) {}
1106 void fix_length_and_dec();
1107};
1108
1109
1110class Item_func_rpad :public Item_func_pad
1111{
1112public:
1113 Item_func_rpad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1114 Item_func_pad(thd, arg1, arg2, arg3) {}
1115 Item_func_rpad(THD *thd, Item *arg1, Item *arg2):
1116 Item_func_pad(thd, arg1, arg2) {}
1117 String *val_str(String *);
1118 const char *func_name() const { return "rpad"; }
1119 Item *get_copy(THD *thd)
1120 { return get_item_copy<Item_func_rpad>(thd, this); }
1121};
1122
1123
1124class Item_func_rpad_oracle :public Item_func_rpad
1125{
1126 String *make_empty_result()
1127 { null_value= 1; return NULL; }
1128public:
1129 Item_func_rpad_oracle(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1130 Item_func_rpad(thd, arg1, arg2, arg3) {}
1131 Item_func_rpad_oracle(THD *thd, Item *arg1, Item *arg2):
1132 Item_func_rpad(thd, arg1, arg2) {}
1133 void fix_length_and_dec()
1134 {
1135 Item_func_rpad::fix_length_and_dec();
1136 maybe_null= true;
1137 }
1138 const char *func_name() const { return "rpad_oracle"; }
1139 Item *get_copy(THD *thd)
1140 { return get_item_copy<Item_func_rpad_oracle>(thd, this); }
1141};
1142
1143
1144class Item_func_lpad :public Item_func_pad
1145{
1146public:
1147 Item_func_lpad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1148 Item_func_pad(thd, arg1, arg2, arg3) {}
1149 Item_func_lpad(THD *thd, Item *arg1, Item *arg2):
1150 Item_func_pad(thd, arg1, arg2) {}
1151 String *val_str(String *);
1152 const char *func_name() const { return "lpad"; }
1153 Item *get_copy(THD *thd)
1154 { return get_item_copy<Item_func_lpad>(thd, this); }
1155};
1156
1157
1158class Item_func_lpad_oracle :public Item_func_lpad
1159{
1160 String *make_empty_result()
1161 { null_value= 1; return NULL; }
1162public:
1163 Item_func_lpad_oracle(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1164 Item_func_lpad(thd, arg1, arg2, arg3) {}
1165 Item_func_lpad_oracle(THD *thd, Item *arg1, Item *arg2):
1166 Item_func_lpad(thd, arg1, arg2) {}
1167 void fix_length_and_dec()
1168 {
1169 Item_func_lpad::fix_length_and_dec();
1170 maybe_null= true;
1171 }
1172 const char *func_name() const { return "lpad_oracle"; }
1173 Item *get_copy(THD *thd)
1174 { return get_item_copy<Item_func_lpad_oracle>(thd, this); }
1175};
1176
1177
1178class Item_func_conv :public Item_str_func
1179{
1180public:
1181 Item_func_conv(THD *thd, Item *a, Item *b, Item *c):
1182 Item_str_func(thd, a, b, c) {}
1183 const char *func_name() const { return "conv"; }
1184 String *val_str(String *);
1185 void fix_length_and_dec()
1186 {
1187 collation.set(default_charset());
1188 max_length=64;
1189 maybe_null= 1;
1190 }
1191 Item *get_copy(THD *thd)
1192 { return get_item_copy<Item_func_conv>(thd, this); }
1193};
1194
1195
1196class Item_func_hex :public Item_str_ascii_checksum_func
1197{
1198protected:
1199 String tmp_value;
1200 /*
1201 Calling arg[0]->type_handler() can be expensive on every row.
1202 It's a virtual method, and in case if args[0] is a complex Item,
1203 its type_handler() can call more virtual methods.
1204 So let's cache it during fix_length_and_dec().
1205 */
1206 const Type_handler *m_arg0_type_handler;
1207public:
1208 Item_func_hex(THD *thd, Item *a):
1209 Item_str_ascii_checksum_func(thd, a), m_arg0_type_handler(NULL) {}
1210 const char *func_name() const { return "hex"; }
1211 String *val_str_ascii_from_val_int(String *str);
1212 String *val_str_ascii_from_val_real(String *str);
1213 String *val_str_ascii_from_val_str(String *str);
1214 String *val_str_ascii(String *str)
1215 {
1216 DBUG_ASSERT(fixed);
1217 return m_arg0_type_handler->Item_func_hex_val_str_ascii(this, str);
1218 }
1219 void fix_length_and_dec()
1220 {
1221 collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
1222 decimals=0;
1223 fix_char_length(args[0]->max_length * 2);
1224 m_arg0_type_handler= args[0]->type_handler();
1225 }
1226 Item *get_copy(THD *thd)
1227 { return get_item_copy<Item_func_hex>(thd, this); }
1228};
1229
1230class Item_func_unhex :public Item_str_func
1231{
1232 String tmp_value;
1233public:
1234 Item_func_unhex(THD *thd, Item *a): Item_str_func(thd, a)
1235 {
1236 /* there can be bad hex strings */
1237 maybe_null= 1;
1238 }
1239 const char *func_name() const { return "unhex"; }
1240 String *val_str(String *);
1241 void fix_length_and_dec()
1242 {
1243 collation.set(&my_charset_bin);
1244 decimals=0;
1245 max_length=(1+args[0]->max_length)/2;
1246 }
1247 Item *get_copy(THD *thd)
1248 { return get_item_copy<Item_func_unhex>(thd, this); }
1249};
1250
1251
1252#ifndef DBUG_OFF
1253class Item_func_like_range :public Item_str_func
1254{
1255protected:
1256 String min_str;
1257 String max_str;
1258 const bool is_min;
1259public:
1260 Item_func_like_range(THD *thd, Item *a, Item *b, bool is_min_arg):
1261 Item_str_func(thd, a, b), is_min(is_min_arg)
1262 { maybe_null= 1; }
1263 String *val_str(String *);
1264 void fix_length_and_dec()
1265 {
1266 collation.set(args[0]->collation);
1267 decimals=0;
1268 max_length= MAX_BLOB_WIDTH;
1269 }
1270};
1271
1272
1273class Item_func_like_range_min :public Item_func_like_range
1274{
1275public:
1276 Item_func_like_range_min(THD *thd, Item *a, Item *b):
1277 Item_func_like_range(thd, a, b, true) { }
1278 const char *func_name() const { return "like_range_min"; }
1279 Item *get_copy(THD *thd)
1280 { return get_item_copy<Item_func_like_range_min>(thd, this); }
1281};
1282
1283
1284class Item_func_like_range_max :public Item_func_like_range
1285{
1286public:
1287 Item_func_like_range_max(THD *thd, Item *a, Item *b):
1288 Item_func_like_range(thd, a, b, false) { }
1289 const char *func_name() const { return "like_range_max"; }
1290 Item *get_copy(THD *thd)
1291 { return get_item_copy<Item_func_like_range_max>(thd, this); }
1292};
1293#endif
1294
1295
1296class Item_func_binary :public Item_str_func
1297{
1298public:
1299 Item_func_binary(THD *thd, Item *a): Item_str_func(thd, a) {}
1300 String *val_str(String *a)
1301 {
1302 DBUG_ASSERT(fixed == 1);
1303 String *tmp=args[0]->val_str(a);
1304 null_value=args[0]->null_value;
1305 if (tmp)
1306 tmp->set_charset(&my_charset_bin);
1307 return tmp;
1308 }
1309 void fix_length_and_dec()
1310 {
1311 collation.set(&my_charset_bin);
1312 max_length=args[0]->max_length;
1313 }
1314 void print(String *str, enum_query_type query_type);
1315 const char *func_name() const { return "cast_as_binary"; }
1316 bool need_parentheses_in_default() { return true; }
1317 Item *get_copy(THD *thd)
1318 { return get_item_copy<Item_func_binary>(thd, this); }
1319};
1320
1321
1322class Item_load_file :public Item_str_func
1323{
1324 String tmp_value;
1325public:
1326 Item_load_file(THD *thd, Item *a): Item_str_func(thd, a) {}
1327 String *val_str(String *);
1328 const char *func_name() const { return "load_file"; }
1329 void fix_length_and_dec()
1330 {
1331 collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1332 maybe_null=1;
1333 max_length=MAX_BLOB_WIDTH;
1334 }
1335 bool check_vcol_func_processor(void *arg)
1336 {
1337 return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
1338 }
1339 Item *get_copy(THD *thd)
1340 { return get_item_copy<Item_load_file>(thd, this); }
1341};
1342
1343
1344class Item_func_export_set: public Item_str_func
1345{
1346 public:
1347 Item_func_export_set(THD *thd, Item *a, Item *b, Item* c):
1348 Item_str_func(thd, a, b, c) {}
1349 Item_func_export_set(THD *thd, Item *a, Item *b, Item* c, Item* d):
1350 Item_str_func(thd, a, b, c, d) {}
1351 Item_func_export_set(THD *thd, Item *a, Item *b, Item* c, Item* d, Item* e):
1352 Item_str_func(thd, a, b, c, d, e) {}
1353 String *val_str(String *str);
1354 void fix_length_and_dec();
1355 const char *func_name() const { return "export_set"; }
1356 Item *get_copy(THD *thd)
1357 { return get_item_copy<Item_func_export_set>(thd, this); }
1358};
1359
1360
1361class Item_func_quote :public Item_str_func
1362{
1363 String tmp_value;
1364public:
1365 Item_func_quote(THD *thd, Item *a): Item_str_func(thd, a) {}
1366 const char *func_name() const { return "quote"; }
1367 String *val_str(String *);
1368 void fix_length_and_dec()
1369 {
1370 collation.set(args[0]->collation);
1371 ulonglong max_result_length= (ulonglong) args[0]->max_length * 2 +
1372 2 * collation.collation->mbmaxlen;
1373 max_length= (uint32) MY_MIN(max_result_length, MAX_BLOB_WIDTH);
1374 }
1375 Item *get_copy(THD *thd)
1376 { return get_item_copy<Item_func_quote>(thd, this); }
1377};
1378
1379class Item_func_conv_charset :public Item_str_func
1380{
1381 bool use_cached_value;
1382 String tmp_value;
1383public:
1384 bool safe;
1385 Item_func_conv_charset(THD *thd, Item *a, CHARSET_INFO *cs):
1386 Item_str_func(thd, a)
1387 {
1388 collation.set(cs, DERIVATION_IMPLICIT);
1389 use_cached_value= 0; safe= 0;
1390 }
1391 Item_func_conv_charset(THD *thd, Item *a, CHARSET_INFO *cs, bool cache_if_const):
1392 Item_str_func(thd, a)
1393 {
1394 collation.set(cs, DERIVATION_IMPLICIT);
1395 if (cache_if_const && args[0]->const_item() && !args[0]->is_expensive())
1396 {
1397 uint errors= 0;
1398 String tmp, *str= args[0]->val_str(&tmp);
1399 if (!str || str_value.copy(str->ptr(), str->length(),
1400 str->charset(), cs, &errors))
1401 null_value= 1;
1402 use_cached_value= 1;
1403 str_value.mark_as_const();
1404 safe= (errors == 0);
1405 }
1406 else
1407 {
1408 use_cached_value= 0;
1409 /*
1410 Conversion from and to "binary" is safe.
1411 Conversion to Unicode is safe.
1412 Other kind of conversions are potentially lossy.
1413 */
1414 safe= (args[0]->collation.collation == &my_charset_bin ||
1415 cs == &my_charset_bin ||
1416 (cs->state & MY_CS_UNICODE));
1417 }
1418 }
1419 String *val_str(String *);
1420 longlong val_int()
1421 {
1422 if (args[0]->result_type() == STRING_RESULT)
1423 return Item_str_func::val_int();
1424 longlong res= args[0]->val_int();
1425 if ((null_value= args[0]->null_value))
1426 return 0;
1427 return res;
1428 }
1429 double val_real()
1430 {
1431 if (args[0]->result_type() == STRING_RESULT)
1432 return Item_str_func::val_real();
1433 double res= args[0]->val_real();
1434 if ((null_value= args[0]->null_value))
1435 return 0;
1436 return res;
1437 }
1438 my_decimal *val_decimal(my_decimal *d)
1439 {
1440 if (args[0]->result_type() == STRING_RESULT)
1441 return Item_str_func::val_decimal(d);
1442 my_decimal *res= args[0]->val_decimal(d);
1443 if ((null_value= args[0]->null_value))
1444 return NULL;
1445 return res;
1446 }
1447 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
1448 {
1449 if (args[0]->result_type() == STRING_RESULT)
1450 return Item_str_func::get_date(ltime, fuzzydate);
1451 bool res= args[0]->get_date(ltime, fuzzydate);
1452 if ((null_value= args[0]->null_value))
1453 return 1;
1454 return res;
1455 }
1456 void fix_length_and_dec();
1457 const char *func_name() const { return "convert"; }
1458 void print(String *str, enum_query_type query_type);
1459 Item *get_copy(THD *thd)
1460 { return get_item_copy<Item_func_conv_charset>(thd, this); }
1461};
1462
1463class Item_func_set_collation :public Item_str_func
1464{
1465 CHARSET_INFO *m_set_collation;
1466public:
1467 Item_func_set_collation(THD *thd, Item *a, CHARSET_INFO *set_collation):
1468 Item_str_func(thd, a), m_set_collation(set_collation) {}
1469 String *val_str(String *);
1470 void fix_length_and_dec();
1471 bool eq(const Item *item, bool binary_cmp) const;
1472 const char *func_name() const { return "collate"; }
1473 enum precedence precedence() const { return COLLATE_PRECEDENCE; }
1474 enum Functype functype() const { return COLLATE_FUNC; }
1475 void print(String *str, enum_query_type query_type);
1476 Item_field *field_for_view_update()
1477 {
1478 /* this function is transparent for view updating */
1479 return args[0]->field_for_view_update();
1480 }
1481 bool need_parentheses_in_default() { return true; }
1482 Item *get_copy(THD *thd)
1483 { return get_item_copy<Item_func_set_collation>(thd, this); }
1484};
1485
1486
1487class Item_func_expr_str_metadata :public Item_str_func
1488{
1489public:
1490 Item_func_expr_str_metadata(THD *thd, Item *a): Item_str_func(thd, a) { }
1491 void fix_length_and_dec()
1492 {
1493 collation.set(system_charset_info);
1494 max_length= 64 * collation.collation->mbmaxlen; // should be enough
1495 maybe_null= 0;
1496 };
1497 table_map not_null_tables() const { return 0; }
1498 Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
1499 { return this; }
1500 bool const_item() const { return true; }
1501};
1502
1503
1504class Item_func_charset :public Item_func_expr_str_metadata
1505{
1506public:
1507 Item_func_charset(THD *thd, Item *a)
1508 :Item_func_expr_str_metadata(thd, a) { }
1509 String *val_str(String *);
1510 const char *func_name() const { return "charset"; }
1511 Item *get_copy(THD *thd)
1512 { return get_item_copy<Item_func_charset>(thd, this); }
1513};
1514
1515
1516class Item_func_collation :public Item_func_expr_str_metadata
1517{
1518public:
1519 Item_func_collation(THD *thd, Item *a)
1520 :Item_func_expr_str_metadata(thd, a) {}
1521 String *val_str(String *);
1522 const char *func_name() const { return "collation"; }
1523 Item *get_copy(THD *thd)
1524 { return get_item_copy<Item_func_collation>(thd, this); }
1525};
1526
1527
1528class Item_func_weight_string :public Item_str_func
1529{
1530 String tmp_value;
1531 uint flags;
1532 uint nweights;
1533 uint result_length;
1534public:
1535 Item_func_weight_string(THD *thd, Item *a, uint result_length_arg,
1536 uint nweights_arg, uint flags_arg):
1537 Item_str_func(thd, a)
1538 {
1539 nweights= nweights_arg;
1540 flags= flags_arg;
1541 result_length= result_length_arg;
1542 }
1543 const char *func_name() const { return "weight_string"; }
1544 String *val_str(String *);
1545 void fix_length_and_dec();
1546 bool eq(const Item *item, bool binary_cmp) const
1547 {
1548 if (!Item_str_func::eq(item, binary_cmp))
1549 return false;
1550 Item_func_weight_string *that= (Item_func_weight_string *)item;
1551 return this->flags == that->flags &&
1552 this->nweights == that->nweights &&
1553 this->result_length == that->result_length;
1554 }
1555 Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
1556 { return this; }
1557 void print(String *str, enum_query_type query_type);
1558 Item *get_copy(THD *thd)
1559 { return get_item_copy<Item_func_weight_string>(thd, this); }
1560};
1561
1562class Item_func_crc32 :public Item_long_func
1563{
1564 bool check_arguments() const
1565 { return args[0]->check_type_can_return_str(func_name()); }
1566 String value;
1567public:
1568 Item_func_crc32(THD *thd, Item *a): Item_long_func(thd, a)
1569 { unsigned_flag= 1; }
1570 const char *func_name() const { return "crc32"; }
1571 void fix_length_and_dec() { max_length=10; }
1572 longlong val_int();
1573 Item *get_copy(THD *thd)
1574 { return get_item_copy<Item_func_crc32>(thd, this); }
1575};
1576
1577class Item_func_uncompressed_length : public Item_long_func_length
1578{
1579 String value;
1580public:
1581 Item_func_uncompressed_length(THD *thd, Item *a)
1582 :Item_long_func_length(thd, a) {}
1583 const char *func_name() const{return "uncompressed_length";}
1584 void fix_length_and_dec() { max_length=10; maybe_null= true; }
1585 longlong val_int();
1586 Item *get_copy(THD *thd)
1587 { return get_item_copy<Item_func_uncompressed_length>(thd, this); }
1588};
1589
1590#ifdef HAVE_COMPRESS
1591#define ZLIB_DEPENDED_FUNCTION ;
1592#else
1593#define ZLIB_DEPENDED_FUNCTION { null_value=1; return 0; }
1594#endif
1595
1596class Item_func_compress: public Item_str_binary_checksum_func
1597{
1598 String tmp_value;
1599public:
1600 Item_func_compress(THD *thd, Item *a)
1601 :Item_str_binary_checksum_func(thd, a) {}
1602 void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;}
1603 const char *func_name() const{return "compress";}
1604 String *val_str(String *) ZLIB_DEPENDED_FUNCTION
1605 Item *get_copy(THD *thd)
1606 { return get_item_copy<Item_func_compress>(thd, this); }
1607};
1608
1609class Item_func_uncompress: public Item_str_binary_checksum_func
1610{
1611 String tmp_value;
1612public:
1613 Item_func_uncompress(THD *thd, Item *a)
1614 :Item_str_binary_checksum_func(thd, a) {}
1615 void fix_length_and_dec(){ maybe_null= 1; max_length= MAX_BLOB_WIDTH; }
1616 const char *func_name() const{return "uncompress";}
1617 String *val_str(String *) ZLIB_DEPENDED_FUNCTION
1618 Item *get_copy(THD *thd)
1619 { return get_item_copy<Item_func_uncompress>(thd, this); }
1620};
1621
1622
1623class Item_func_uuid: public Item_str_func
1624{
1625public:
1626 Item_func_uuid(THD *thd): Item_str_func(thd) {}
1627 void fix_length_and_dec()
1628 {
1629 collation.set(system_charset_info,
1630 DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
1631 fix_char_length(MY_UUID_STRING_LENGTH);
1632 }
1633 bool const_item() const { return false; }
1634 table_map used_tables() const { return RAND_TABLE_BIT; }
1635 const char *func_name() const{ return "uuid"; }
1636 String *val_str(String *);
1637 bool check_vcol_func_processor(void *arg)
1638 {
1639 return mark_unsupported_function(func_name(), "()", arg, VCOL_NON_DETERMINISTIC);
1640 }
1641 Item *get_copy(THD *thd)
1642 { return get_item_copy<Item_func_uuid>(thd, this); }
1643};
1644
1645
1646class Item_func_dyncol_create: public Item_str_func
1647{
1648protected:
1649 DYNCALL_CREATE_DEF *defs;
1650 DYNAMIC_COLUMN_VALUE *vals;
1651 uint *keys_num;
1652 LEX_STRING *keys_str;
1653 bool names, force_names;
1654 bool prepare_arguments(THD *thd, bool force_names);
1655 void print_arguments(String *str, enum_query_type query_type);
1656public:
1657 Item_func_dyncol_create(THD *thd, List<Item> &args, DYNCALL_CREATE_DEF *dfs);
1658 bool fix_fields(THD *thd, Item **ref);
1659 void fix_length_and_dec();
1660 const char *func_name() const{ return "column_create"; }
1661 String *val_str(String *);
1662 void print(String *str, enum_query_type query_type);
1663 enum Functype functype() const { return DYNCOL_FUNC; }
1664 Item *get_copy(THD *thd)
1665 { return get_item_copy<Item_func_dyncol_create>(thd, this); }
1666};
1667
1668
1669class Item_func_dyncol_add: public Item_func_dyncol_create
1670{
1671public:
1672 Item_func_dyncol_add(THD *thd, List<Item> &args_arg, DYNCALL_CREATE_DEF *dfs):
1673 Item_func_dyncol_create(thd, args_arg, dfs)
1674 {}
1675 const char *func_name() const{ return "column_add"; }
1676 String *val_str(String *);
1677 void print(String *str, enum_query_type query_type);
1678 Item *get_copy(THD *thd)
1679 { return get_item_copy<Item_func_dyncol_add>(thd, this); }
1680};
1681
1682class Item_func_dyncol_json: public Item_str_func
1683{
1684public:
1685 Item_func_dyncol_json(THD *thd, Item *str): Item_str_func(thd, str)
1686 {collation.set(DYNCOL_UTF);}
1687 const char *func_name() const{ return "column_json"; }
1688 String *val_str(String *);
1689 void fix_length_and_dec()
1690 {
1691 max_length= MAX_BLOB_WIDTH;
1692 maybe_null= 1;
1693 decimals= 0;
1694 }
1695 Item *get_copy(THD *thd)
1696 { return get_item_copy<Item_func_dyncol_json>(thd, this); }
1697};
1698
1699/*
1700 The following functions is always called from an Item_cast function
1701*/
1702
1703class Item_dyncol_get: public Item_str_func
1704{
1705public:
1706 Item_dyncol_get(THD *thd, Item *str, Item *num): Item_str_func(thd, str, num)
1707 {}
1708 void fix_length_and_dec()
1709 { maybe_null= 1;; max_length= MAX_BLOB_WIDTH; }
1710 /* Mark that collation can change between calls */
1711 bool dynamic_result() { return 1; }
1712
1713 const char *func_name() const { return "column_get"; }
1714 String *val_str(String *);
1715 longlong val_int();
1716 longlong val_int_signed_typecast()
1717 {
1718 unsigned_flag= false; // Mark that we want to have a signed value
1719 longlong value= val_int(); // val_int() can change unsigned_flag
1720 if (!null_value && unsigned_flag && value < 0)
1721 push_note_converted_to_negative_complement(current_thd);
1722 return value;
1723 }
1724 longlong val_int_unsigned_typecast()
1725 {
1726 unsigned_flag= true; // Mark that we want to have an unsigned value
1727 longlong value= val_int(); // val_int() can change unsigned_flag
1728 if (!null_value && unsigned_flag == 0 && value < 0)
1729 push_note_converted_to_positive_complement(current_thd);
1730 return value;
1731 }
1732 double val_real();
1733 my_decimal *val_decimal(my_decimal *);
1734 bool get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val, String *tmp);
1735 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
1736 void print(String *str, enum_query_type query_type);
1737 Item *get_copy(THD *thd)
1738 { return get_item_copy<Item_dyncol_get>(thd, this); }
1739};
1740
1741
1742class Item_func_dyncol_list: public Item_str_func
1743{
1744public:
1745 Item_func_dyncol_list(THD *thd, Item *str): Item_str_func(thd, str)
1746 {collation.set(DYNCOL_UTF);}
1747 void fix_length_and_dec() { maybe_null= 1; max_length= MAX_BLOB_WIDTH; };
1748 const char *func_name() const{ return "column_list"; }
1749 String *val_str(String *);
1750 Item *get_copy(THD *thd)
1751 { return get_item_copy<Item_func_dyncol_list>(thd, this); }
1752};
1753
1754/*
1755 this is used by JOIN_TAB::keep_current_rowid
1756 and stores handler::position().
1757 It has nothing to do with _rowid pseudo-column, that the parser supports.
1758*/
1759class Item_temptable_rowid :public Item_str_func
1760{
1761public:
1762 TABLE *table;
1763 Item_temptable_rowid(TABLE *table_arg);
1764 const Type_handler *type_handler() const { return &type_handler_string; }
1765 Field *create_tmp_field(bool group, TABLE *table)
1766 { return create_table_field_from_handler(table); }
1767 String *val_str(String *str);
1768 enum Functype functype() const { return TEMPTABLE_ROWID; }
1769 const char *func_name() const { return "<rowid>"; }
1770 void fix_length_and_dec();
1771 Item *get_copy(THD *thd)
1772 { return get_item_copy<Item_temptable_rowid>(thd, this); }
1773};
1774
1775#endif /* ITEM_STRFUNC_INCLUDED */
1776