1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* This file defines all string functions */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
23
class Item_str_func :public Item_func
26
Item_str_func() :Item_func() { decimals=NOT_FIXED_DEC; }
27
Item_str_func(Item *a) :Item_func(a) {decimals=NOT_FIXED_DEC; }
28
Item_str_func(Item *a,Item *b) :Item_func(a,b) { decimals=NOT_FIXED_DEC; }
29
Item_str_func(Item *a,Item *b,Item *c) :Item_func(a,b,c) { decimals=NOT_FIXED_DEC; }
30
Item_str_func(Item *a,Item *b,Item *c,Item *d) :Item_func(a,b,c,d) {decimals=NOT_FIXED_DEC; }
31
Item_str_func(Item *a,Item *b,Item *c,Item *d, Item* e) :Item_func(a,b,c,d,e) {decimals=NOT_FIXED_DEC; }
32
Item_str_func(List<Item> &list) :Item_func(list) {decimals=NOT_FIXED_DEC; }
35
my_decimal *val_decimal(my_decimal *);
36
enum Item_result result_type () const { return STRING_RESULT; }
37
void left_right_max_length();
38
bool fix_fields(THD *thd, Item **ref);
41
class Item_func_md5 :public Item_str_func
45
Item_func_md5(Item *a) :Item_str_func(a)
47
collation.set(&my_charset_bin);
49
String *val_str(String *);
50
void fix_length_and_dec();
51
const char *func_name() const { return "md5"; }
54
class Item_func_aes_encrypt :public Item_str_func
57
Item_func_aes_encrypt(Item *a, Item *b) :Item_str_func(a,b) {}
58
String *val_str(String *);
59
void fix_length_and_dec();
60
const char *func_name() const { return "aes_encrypt"; }
63
class Item_func_aes_decrypt :public Item_str_func
66
Item_func_aes_decrypt(Item *a, Item *b) :Item_str_func(a,b) {}
67
String *val_str(String *);
68
void fix_length_and_dec();
69
const char *func_name() const { return "aes_decrypt"; }
73
class Item_func_concat :public Item_str_func
77
Item_func_concat(List<Item> &list) :Item_str_func(list) {}
78
Item_func_concat(Item *a,Item *b) :Item_str_func(a,b) {}
79
String *val_str(String *);
80
void fix_length_and_dec();
81
const char *func_name() const { return "concat"; }
84
class Item_func_concat_ws :public Item_str_func
88
Item_func_concat_ws(List<Item> &list) :Item_str_func(list) {}
89
String *val_str(String *);
90
void fix_length_and_dec();
91
const char *func_name() const { return "concat_ws"; }
92
table_map not_null_tables() const { return 0; }
95
class Item_func_reverse :public Item_str_func
99
Item_func_reverse(Item *a) :Item_str_func(a) {}
100
String *val_str(String *);
101
void fix_length_and_dec();
102
const char *func_name() const { return "reverse"; }
106
class Item_func_replace :public Item_str_func
108
String tmp_value,tmp_value2;
110
Item_func_replace(Item *org,Item *find,Item *replace)
111
:Item_str_func(org,find,replace) {}
112
String *val_str(String *);
113
void fix_length_and_dec();
114
const char *func_name() const { return "replace"; }
118
class Item_func_insert :public Item_str_func
122
Item_func_insert(Item *org,Item *start,Item *length,Item *new_str)
123
:Item_str_func(org,start,length,new_str) {}
124
String *val_str(String *);
125
void fix_length_and_dec();
126
const char *func_name() const { return "insert"; }
130
class Item_str_conv :public Item_str_func
134
my_charset_conv_case converter;
137
Item_str_conv(Item *item) :Item_str_func(item) {}
138
String *val_str(String *);
142
class Item_func_lcase :public Item_str_conv
145
Item_func_lcase(Item *item) :Item_str_conv(item) {}
146
const char *func_name() const { return "lcase"; }
147
void fix_length_and_dec();
150
class Item_func_ucase :public Item_str_conv
153
Item_func_ucase(Item *item) :Item_str_conv(item) {}
154
const char *func_name() const { return "ucase"; }
155
void fix_length_and_dec();
159
class Item_func_left :public Item_str_func
163
Item_func_left(Item *a,Item *b) :Item_str_func(a,b) {}
164
String *val_str(String *);
165
void fix_length_and_dec();
166
const char *func_name() const { return "left"; }
170
class Item_func_right :public Item_str_func
174
Item_func_right(Item *a,Item *b) :Item_str_func(a,b) {}
175
String *val_str(String *);
176
void fix_length_and_dec();
177
const char *func_name() const { return "right"; }
181
class Item_func_substr :public Item_str_func
185
Item_func_substr(Item *a,Item *b) :Item_str_func(a,b) {}
186
Item_func_substr(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
187
String *val_str(String *);
188
void fix_length_and_dec();
189
const char *func_name() const { return "substr"; }
193
class Item_func_substr_index :public Item_str_func
197
Item_func_substr_index(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
198
String *val_str(String *);
199
void fix_length_and_dec();
200
const char *func_name() const { return "substring_index"; }
204
class Item_func_trim :public Item_str_func
210
Item_func_trim(Item *a,Item *b) :Item_str_func(a,b) {}
211
Item_func_trim(Item *a) :Item_str_func(a) {}
212
String *val_str(String *);
213
void fix_length_and_dec();
214
const char *func_name() const { return "trim"; }
215
virtual void print(String *str, enum_query_type query_type);
216
virtual const char *mode_name() const { return "both"; }
220
class Item_func_ltrim :public Item_func_trim
223
Item_func_ltrim(Item *a,Item *b) :Item_func_trim(a,b) {}
224
Item_func_ltrim(Item *a) :Item_func_trim(a) {}
225
String *val_str(String *);
226
const char *func_name() const { return "ltrim"; }
227
const char *mode_name() const { return "leading"; }
231
class Item_func_rtrim :public Item_func_trim
234
Item_func_rtrim(Item *a,Item *b) :Item_func_trim(a,b) {}
235
Item_func_rtrim(Item *a) :Item_func_trim(a) {}
236
String *val_str(String *);
237
const char *func_name() const { return "rtrim"; }
238
const char *mode_name() const { return "trailing"; }
243
Item_func_password -- new (4.1.1) PASSWORD() function implementation.
244
Returns strcat('*', octet2hex(sha1(sha1(password)))). '*' stands for new
245
password format, sha1(sha1(password) is so-called hash_stage2 value.
246
Length of returned string is always 41 byte. To find out how entire
247
authentication procedure works, see comments in password.c.
250
class Item_func_password :public Item_str_func
252
char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH+1];
254
Item_func_password(Item *a) :Item_str_func(a) {}
255
String *val_str(String *str);
256
void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH; }
257
const char *func_name() const { return "password"; }
258
static char *alloc(THD *thd, const char *password);
263
Item_func_old_password -- PASSWORD() implementation used in MySQL 3.21 - 4.0
264
compatibility mode. This item is created in sql_yacc.yy when
265
'old_passwords' session variable is set, and to handle OLD_PASSWORD()
269
class Item_func_old_password :public Item_str_func
271
char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1];
273
Item_func_old_password(Item *a) :Item_str_func(a) {}
274
String *val_str(String *str);
275
void fix_length_and_dec() { max_length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323; }
276
const char *func_name() const { return "old_password"; }
277
static char *alloc(THD *thd, const char *password);
281
class Item_func_des_encrypt :public Item_str_func
285
String *val_str(String *);
286
void fix_length_and_dec()
287
{ maybe_null=1; max_length = args[0]->max_length+8; }
288
const char *func_name() const { return "des_encrypt"; }
291
class Item_func_des_decrypt :public Item_str_func
295
String *val_str(String *);
296
void fix_length_and_dec() { maybe_null=1; max_length = args[0]->max_length; }
297
const char *func_name() const { return "des_decrypt"; }
300
class Item_func_encrypt :public Item_str_func
304
/* Encapsulate common constructor actions */
305
void constructor_helper()
307
collation.set(&my_charset_bin);
310
Item_func_encrypt(Item *a) :Item_str_func(a)
312
constructor_helper();
314
Item_func_encrypt(Item *a, Item *b): Item_str_func(a,b)
316
constructor_helper();
318
String *val_str(String *);
319
void fix_length_and_dec() { maybe_null=1; max_length = 13; }
320
const char *func_name() const { return "encrypt"; }
323
#include "sql_crypt.h"
326
class Item_func_encode :public Item_str_func
329
Item_func_encode(Item *a, Item *seed):
330
Item_str_func(a, seed) {}
331
String *val_str(String *);
332
void fix_length_and_dec();
333
const char *func_name() const { return "encode"; }
337
class Item_func_decode :public Item_func_encode
340
Item_func_decode(Item *a, Item *seed): Item_func_encode(a, seed) {}
341
String *val_str(String *);
342
const char *func_name() const { return "decode"; }
346
class Item_func_sysconst :public Item_str_func
350
{ collation.set(system_charset_info,DERIVATION_SYSCONST); }
351
Item *safe_charset_converter(CHARSET_INFO *tocs);
353
Used to create correct Item name in new converted item in
354
safe_charset_converter, return string representation of this function
357
virtual const char *fully_qualified_func_name() const = 0;
361
class Item_func_database :public Item_func_sysconst
364
Item_func_database() :Item_func_sysconst() {}
365
String *val_str(String *);
366
void fix_length_and_dec()
368
max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
371
const char *func_name() const { return "database"; }
372
const char *fully_qualified_func_name() const { return "database()"; }
376
class Item_func_user :public Item_func_sysconst
379
bool init (const char *user, const char *host);
384
str_value.set("", 0, system_charset_info);
386
String *val_str(String *)
388
DBUG_ASSERT(fixed == 1);
389
return (null_value ? 0 : &str_value);
391
bool fix_fields(THD *thd, Item **ref);
392
void fix_length_and_dec()
394
max_length= (USERNAME_CHAR_LENGTH + HOSTNAME_LENGTH + 1) *
395
system_charset_info->mbmaxlen;
397
const char *func_name() const { return "user"; }
398
const char *fully_qualified_func_name() const { return "user()"; }
399
int save_in_field(Field *field, bool no_conversions)
401
return save_str_value_in_field(field, &str_value);
406
class Item_func_current_user :public Item_func_user
408
Name_resolution_context *context;
411
Item_func_current_user(Name_resolution_context *context_arg)
412
: context(context_arg) {}
413
bool fix_fields(THD *thd, Item **ref);
414
const char *func_name() const { return "current_user"; }
415
const char *fully_qualified_func_name() const { return "current_user()"; }
419
class Item_func_soundex :public Item_str_func
423
Item_func_soundex(Item *a) :Item_str_func(a) {}
424
String *val_str(String *);
425
void fix_length_and_dec();
426
const char *func_name() const { return "soundex"; }
430
class Item_func_elt :public Item_str_func
433
Item_func_elt(List<Item> &list) :Item_str_func(list) {}
436
String *val_str(String *str);
437
void fix_length_and_dec();
438
const char *func_name() const { return "elt"; }
442
class Item_func_make_set :public Item_str_func
448
Item_func_make_set(Item *a,List<Item> &list) :Item_str_func(list),item(a) {}
449
String *val_str(String *str);
450
bool fix_fields(THD *thd, Item **ref)
452
DBUG_ASSERT(fixed == 0);
453
return ((!item->fixed && item->fix_fields(thd, &item)) ||
454
item->check_cols(1) ||
455
Item_func::fix_fields(thd, ref));
457
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
458
void fix_length_and_dec();
459
void update_used_tables();
460
const char *func_name() const { return "make_set"; }
462
bool walk(Item_processor processor, bool walk_subquery, uchar *arg)
464
return item->walk(processor, walk_subquery, arg) ||
465
Item_str_func::walk(processor, walk_subquery, arg);
467
Item *transform(Item_transformer transformer, uchar *arg);
468
virtual void print(String *str, enum_query_type query_type);
472
class Item_func_format :public Item_str_func
476
Item_func_format(Item *org, Item *dec);
477
String *val_str(String *);
478
void fix_length_and_dec();
479
const char *func_name() const { return "format"; }
480
virtual void print(String *str, enum_query_type query_type);
484
class Item_func_char :public Item_str_func
487
Item_func_char(List<Item> &list) :Item_str_func(list)
488
{ collation.set(&my_charset_bin); }
489
Item_func_char(List<Item> &list, CHARSET_INFO *cs) :Item_str_func(list)
490
{ collation.set(cs); }
491
String *val_str(String *);
492
void fix_length_and_dec()
494
max_length= arg_count * 4;
496
const char *func_name() const { return "char"; }
500
class Item_func_repeat :public Item_str_func
504
Item_func_repeat(Item *arg1,Item *arg2) :Item_str_func(arg1,arg2) {}
505
String *val_str(String *);
506
void fix_length_and_dec();
507
const char *func_name() const { return "repeat"; }
511
class Item_func_rpad :public Item_str_func
513
String tmp_value, rpad_str;
515
Item_func_rpad(Item *arg1,Item *arg2,Item *arg3)
516
:Item_str_func(arg1,arg2,arg3) {}
517
String *val_str(String *);
518
void fix_length_and_dec();
519
const char *func_name() const { return "rpad"; }
523
class Item_func_lpad :public Item_str_func
525
String tmp_value, lpad_str;
527
Item_func_lpad(Item *arg1,Item *arg2,Item *arg3)
528
:Item_str_func(arg1,arg2,arg3) {}
529
String *val_str(String *);
530
void fix_length_and_dec();
531
const char *func_name() const { return "lpad"; }
535
class Item_func_conv :public Item_str_func
538
Item_func_conv(Item *a,Item *b,Item *c) :Item_str_func(a,b,c) {}
539
const char *func_name() const { return "conv"; }
540
String *val_str(String *);
541
void fix_length_and_dec()
543
collation.set(default_charset());
550
class Item_func_hex :public Item_str_func
554
Item_func_hex(Item *a) :Item_str_func(a) {}
555
const char *func_name() const { return "hex"; }
556
String *val_str(String *);
557
void fix_length_and_dec()
559
collation.set(default_charset());
561
max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
565
class Item_func_unhex :public Item_str_func
569
Item_func_unhex(Item *a) :Item_str_func(a)
571
/* there can be bad hex strings */
574
const char *func_name() const { return "unhex"; }
575
String *val_str(String *);
576
void fix_length_and_dec()
578
collation.set(&my_charset_bin);
580
max_length=(1+args[0]->max_length)/2;
585
class Item_func_binary :public Item_str_func
588
Item_func_binary(Item *a) :Item_str_func(a) {}
589
String *val_str(String *a)
591
DBUG_ASSERT(fixed == 1);
592
String *tmp=args[0]->val_str(a);
593
null_value=args[0]->null_value;
595
tmp->set_charset(&my_charset_bin);
598
void fix_length_and_dec()
600
collation.set(&my_charset_bin);
601
max_length=args[0]->max_length;
603
virtual void print(String *str, enum_query_type query_type);
604
const char *func_name() const { return "cast_as_binary"; }
608
class Item_load_file :public Item_str_func
612
Item_load_file(Item *a) :Item_str_func(a) {}
613
String *val_str(String *);
614
const char *func_name() const { return "load_file"; }
615
void fix_length_and_dec()
617
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
619
max_length=MAX_BLOB_WIDTH;
624
class Item_func_export_set: public Item_str_func
627
Item_func_export_set(Item *a,Item *b,Item* c) :Item_str_func(a,b,c) {}
628
Item_func_export_set(Item *a,Item *b,Item* c,Item* d) :Item_str_func(a,b,c,d) {}
629
Item_func_export_set(Item *a,Item *b,Item* c,Item* d,Item* e) :Item_str_func(a,b,c,d,e) {}
630
String *val_str(String *str);
631
void fix_length_and_dec();
632
const char *func_name() const { return "export_set"; }
635
class Item_func_quote :public Item_str_func
639
Item_func_quote(Item *a) :Item_str_func(a) {}
640
const char *func_name() const { return "quote"; }
641
String *val_str(String *);
642
void fix_length_and_dec()
644
collation.set(args[0]->collation);
645
max_length= args[0]->max_length * 2 + 2;
649
class Item_func_conv_charset :public Item_str_func
651
bool use_cached_value;
654
CHARSET_INFO *conv_charset; // keep it public
655
Item_func_conv_charset(Item *a, CHARSET_INFO *cs) :Item_str_func(a)
656
{ conv_charset= cs; use_cached_value= 0; safe= 0; }
657
Item_func_conv_charset(Item *a, CHARSET_INFO *cs, bool cache_if_const)
660
DBUG_ASSERT(args[0]->fixed);
662
if (cache_if_const && args[0]->const_item())
665
String tmp, *str= args[0]->val_str(&tmp);
666
if (!str || str_value.copy(str->ptr(), str->length(),
667
str->charset(), conv_charset, &errors))
670
str_value.mark_as_const();
677
Conversion from and to "binary" is safe.
678
Conversion to Unicode is safe.
679
Other kind of conversions are potentially lossy.
681
safe= (args[0]->collation.collation == &my_charset_bin ||
682
cs == &my_charset_bin ||
683
(cs->state & MY_CS_UNICODE));
686
String *val_str(String *);
687
void fix_length_and_dec();
688
const char *func_name() const { return "convert"; }
689
virtual void print(String *str, enum_query_type query_type);
692
class Item_func_set_collation :public Item_str_func
695
Item_func_set_collation(Item *a, Item *b) :Item_str_func(a,b) {};
696
String *val_str(String *);
697
void fix_length_and_dec();
698
bool eq(const Item *item, bool binary_cmp) const;
699
const char *func_name() const { return "collate"; }
700
enum Functype functype() const { return COLLATE_FUNC; }
701
virtual void print(String *str, enum_query_type query_type);
702
Item_field *filed_for_view_update()
704
/* this function is transparent for view updating */
705
return args[0]->filed_for_view_update();
709
class Item_func_charset :public Item_str_func
712
Item_func_charset(Item *a) :Item_str_func(a) {}
713
String *val_str(String *);
714
const char *func_name() const { return "charset"; }
715
void fix_length_and_dec()
717
collation.set(system_charset_info);
718
max_length= 64 * collation.collation->mbmaxlen; // should be enough
721
table_map not_null_tables() const { return 0; }
724
class Item_func_collation :public Item_str_func
727
Item_func_collation(Item *a) :Item_str_func(a) {}
728
String *val_str(String *);
729
const char *func_name() const { return "collation"; }
730
void fix_length_and_dec()
732
collation.set(system_charset_info);
733
max_length= 64 * collation.collation->mbmaxlen; // should be enough
736
table_map not_null_tables() const { return 0; }
740
class Item_func_weight_string :public Item_str_func
746
Item_func_weight_string(Item *a, uint nweights_arg, uint flags_arg)
747
:Item_str_func(a) { nweights= nweights_arg; flags= flags_arg; }
748
const char *func_name() const { return "weight_string"; }
749
String *val_str(String *);
750
void fix_length_and_dec();
754
class Item_func_crc32 :public Item_int_func
758
Item_func_crc32(Item *a) :Item_int_func(a) { unsigned_flag= 1; }
759
const char *func_name() const { return "crc32"; }
760
void fix_length_and_dec() { max_length=10; }
764
class Item_func_uncompressed_length : public Item_int_func
768
Item_func_uncompressed_length(Item *a):Item_int_func(a){}
769
const char *func_name() const{return "uncompressed_length";}
770
void fix_length_and_dec() { max_length=10; }
775
#define ZLIB_DEPENDED_FUNCTION ;
777
#define ZLIB_DEPENDED_FUNCTION { null_value=1; return 0; }
780
class Item_func_compress: public Item_str_func
784
Item_func_compress(Item *a):Item_str_func(a){}
785
void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;}
786
const char *func_name() const{return "compress";}
787
String *val_str(String *) ZLIB_DEPENDED_FUNCTION
790
class Item_func_uncompress: public Item_str_func
794
Item_func_uncompress(Item *a): Item_str_func(a){}
795
void fix_length_and_dec(){ maybe_null= 1; max_length= MAX_BLOB_WIDTH; }
796
const char *func_name() const{return "uncompress";}
797
String *val_str(String *) ZLIB_DEPENDED_FUNCTION
800
#define UUID_LENGTH (8+1+4+1+4+1+4+1+12)
801
class Item_func_uuid: public Item_str_func
804
Item_func_uuid(): Item_str_func() {}
805
void fix_length_and_dec() {
806
collation.set(system_charset_info);
808
NOTE! uuid() should be changed to use 'ascii'
809
charset when hex(), format(), md5(), etc, and implicit
810
number-to-string conversion will use 'ascii'
812
max_length= UUID_LENGTH * system_charset_info->mbmaxlen;
814
const char *func_name() const{ return "uuid"; }
815
String *val_str(String *);