1
/* Copyright (C) 2000-2006 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 */
21
This file defines all string functions
24
Some string functions don't always put and end-null on a String.
25
(This shouldn't be needed)
28
#include <drizzled/server_includes.h>
29
#include <mysys/sha1.h>
36
#include <mysys/my_static.h> // For soundex_map
42
#include <drizzled/drizzled_error_messages.h>
44
String my_empty_string("",default_charset_info);
49
bool Item_str_func::fix_fields(THD *thd, Item **ref)
51
bool res= Item_func::fix_fields(thd, ref);
53
In Item_str_func::check_well_formed_result() we may set null_value
54
flag on the same condition as in test() below.
56
maybe_null= (maybe_null || true);
61
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
65
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
69
(void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
70
res->length(), res->charset(), decimal_value);
75
double Item_str_func::val_real()
79
char *end_not_used, buff[64];
80
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
82
return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
83
&end_not_used, &err_not_used) : 0.0;
87
int64_t Item_str_func::val_int()
92
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
95
my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
101
Concatenate args with the following premises:
102
If only one arg (which is ok), return value of arg;
103
Don't reallocate val_str() if not absolute necessary.
106
String *Item_func_concat::val_str(String *str)
109
String *res,*res2,*use_as_buff;
114
if (!(res=args[0]->val_str(str)))
116
use_as_buff= &tmp_value;
117
/* Item_subselect in --ps-protocol mode will state it as a non-const */
118
is_const= args[0]->const_item() || !args[0]->used_tables();
119
for (i=1 ; i < arg_count ; i++)
121
if (res->length() == 0)
123
if (!(res=args[i]->val_str(str)))
128
if (!(res2=args[i]->val_str(use_as_buff)))
130
if (res2->length() == 0)
132
if (res->length()+res2->length() >
133
current_thd->variables.max_allowed_packet)
135
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
136
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
137
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
138
current_thd->variables.max_allowed_packet);
141
if (!is_const && res->alloced_length() >= res->length()+res2->length())
145
else if (str->alloced_length() >= res->length()+res2->length())
148
str->replace(0,0,*res);
155
use_as_buff= &tmp_value;
157
else if (res == &tmp_value)
159
if (res->append(*res2)) // Must be a blob
162
else if (res2 == &tmp_value)
163
{ // This can happend only 1 time
164
if (tmp_value.replace(0,0,*res))
167
use_as_buff=str; // Put next arg here
169
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
170
res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
173
This happens really seldom:
174
In this case res2 is sub string of tmp_value. We will
175
now work in place in tmp_value to set it to res | res2
177
/* Chop the last characters in tmp_value that isn't in res2 */
178
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
180
/* Place res2 at start of tmp_value, remove chars before res2 */
181
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
185
use_as_buff=str; // Put next arg here
188
{ // Two big const strings
190
NOTE: We should be prudent in the initial allocation unit -- the
191
size of the arguments is a function of data distribution, which
192
can be any. Instead of overcommitting at the first row, we grow
193
the allocated amount by the factor of 2. This ensures that no
194
more than 25% of memory will be overcommitted on average.
197
uint32_t concat_len= res->length() + res2->length();
199
if (tmp_value.alloced_length() < concat_len)
201
if (tmp_value.alloced_length() == 0)
203
if (tmp_value.alloc(concat_len))
208
uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
210
if (tmp_value.realloc(new_len))
215
if (tmp_value.copy(*res) || tmp_value.append(*res2))
224
res->set_charset(collation.collation);
233
void Item_func_concat::fix_length_and_dec()
235
uint64_t max_result_length= 0;
237
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
240
for (uint32_t i=0 ; i < arg_count ; i++)
242
if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
243
max_result_length+= (args[i]->max_length /
244
args[i]->collation.collation->mbmaxlen) *
245
collation.collation->mbmaxlen;
247
max_result_length+= args[i]->max_length;
250
if (max_result_length >= MAX_BLOB_WIDTH)
252
max_result_length= MAX_BLOB_WIDTH;
255
max_length= (ulong) max_result_length;
260
concat with separator. First arg is the separator
261
concat_ws takes at least two arguments.
264
String *Item_func_concat_ws::val_str(String *str)
267
char tmp_str_buff[10];
268
String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
269
*sep_str, *res, *res2,*use_as_buff;
273
if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
276
use_as_buff= &tmp_value;
277
str->length(0); // QQ; Should be removed
280
// Skip until non-null argument is found.
281
// If not, return the empty string
282
for (i=1; i < arg_count; i++)
283
if ((res= args[i]->val_str(str)))
286
return &my_empty_string;
288
for (i++; i < arg_count ; i++)
290
if (!(res2= args[i]->val_str(use_as_buff)))
291
continue; // Skip NULL
293
if (res->length() + sep_str->length() + res2->length() >
294
current_thd->variables.max_allowed_packet)
296
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
297
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
298
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
299
current_thd->variables.max_allowed_packet);
302
if (res->alloced_length() >=
303
res->length() + sep_str->length() + res2->length())
305
res->append(*sep_str); // res->length() > 0 always
308
else if (str->alloced_length() >=
309
res->length() + sep_str->length() + res2->length())
311
/* We have room in str; We can't get any errors here */
313
{ // This is quote uncommon!
314
str->replace(0,0,*sep_str);
315
str->replace(0,0,*res);
320
str->append(*sep_str);
324
use_as_buff= &tmp_value;
326
else if (res == &tmp_value)
328
if (res->append(*sep_str) || res->append(*res2))
329
goto null; // Must be a blob
331
else if (res2 == &tmp_value)
332
{ // This can happend only 1 time
333
if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
336
use_as_buff=str; // Put next arg here
338
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
339
res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
342
This happens really seldom:
343
In this case res2 is sub string of tmp_value. We will
344
now work in place in tmp_value to set it to res | sep_str | res2
346
/* Chop the last characters in tmp_value that isn't in res2 */
347
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
349
/* Place res2 at start of tmp_value, remove chars before res2 */
350
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
352
tmp_value.replace(res->length(),0, *sep_str))
355
use_as_buff=str; // Put next arg here
358
{ // Two big const strings
360
NOTE: We should be prudent in the initial allocation unit -- the
361
size of the arguments is a function of data distribution, which can
362
be any. Instead of overcommitting at the first row, we grow the
363
allocated amount by the factor of 2. This ensures that no more than
364
25% of memory will be overcommitted on average.
367
uint32_t concat_len= res->length() + sep_str->length() + res2->length();
369
if (tmp_value.alloced_length() < concat_len)
371
if (tmp_value.alloced_length() == 0)
373
if (tmp_value.alloc(concat_len))
378
uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
380
if (tmp_value.realloc(new_len))
385
if (tmp_value.copy(*res) ||
386
tmp_value.append(*sep_str) ||
387
tmp_value.append(*res2))
393
res->set_charset(collation.collation);
402
void Item_func_concat_ws::fix_length_and_dec()
404
uint64_t max_result_length;
406
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
410
arg_count cannot be less than 2,
411
it is done on parser level in sql_yacc.yy
412
so, (arg_count - 2) is safe here.
414
max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
415
for (uint32_t i=1 ; i < arg_count ; i++)
416
max_result_length+=args[i]->max_length;
418
if (max_result_length >= MAX_BLOB_WIDTH)
420
max_result_length= MAX_BLOB_WIDTH;
423
max_length= (ulong) max_result_length;
427
String *Item_func_reverse::val_str(String *str)
430
String *res = args[0]->val_str(str);
431
char *ptr, *end, *tmp;
433
if ((null_value=args[0]->null_value))
435
/* An empty string is a special case as the string pointer may be null */
437
return &my_empty_string;
438
if (tmp_value.alloced_length() < res->length() &&
439
tmp_value.realloc(res->length()))
444
tmp_value.length(res->length());
445
tmp_value.set_charset(res->charset());
446
ptr= (char *) res->ptr();
447
end= ptr + res->length();
448
tmp= (char *) tmp_value.ptr() + tmp_value.length();
450
if (use_mb(res->charset()))
455
if ((l= my_ismbchar(res->charset(),ptr,end)))
475
void Item_func_reverse::fix_length_and_dec()
477
collation.set(args[0]->collation);
478
max_length = args[0]->max_length;
482
Replace all occurences of string2 in string1 with string3.
484
Don't reallocate val_str() if not needed.
487
Fix that this works with binary strings when using USE_MB
490
String *Item_func_replace::val_str(String *str)
493
String *res,*res2,*res3;
495
uint32_t from_length,to_length;
498
const char *ptr,*end,*strend,*search,*search_end;
504
res=args[0]->val_str(str);
505
if (args[0]->null_value)
507
res2=args[1]->val_str(&tmp_value);
508
if (args[1]->null_value)
511
res->set_charset(collation.collation);
514
binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
517
if (res2->length() == 0)
520
if ((offset=res->strstr(*res2)) < 0)
524
if (binary_cmp && (offset=res->strstr(*res2)) < 0)
527
if (!(res3=args[2]->val_str(&tmp_value2)))
529
from_length= res2->length();
530
to_length= res3->length();
536
search_end=search+from_length;
538
ptr=res->ptr()+offset;
539
strend=res->ptr()+res->length();
540
end=strend-from_length+1;
546
i=(char*) ptr+1; j=(char*) search+1;
547
while (j != search_end)
548
if (*i++ != *j++) goto skip;
549
offset= (int) (ptr-res->ptr());
550
if (res->length()-from_length + to_length >
551
current_thd->variables.max_allowed_packet)
553
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
554
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
555
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
557
current_thd->variables.max_allowed_packet);
564
res=copy_if_not_alloced(str,res,res->length()+to_length);
566
res->replace((uint) offset,from_length,*res3);
567
offset+=(int) to_length;
571
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
579
if (res->length()-from_length + to_length >
580
current_thd->variables.max_allowed_packet)
582
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
583
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
584
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
585
current_thd->variables.max_allowed_packet);
591
res=copy_if_not_alloced(str,res,res->length()+to_length);
593
res->replace((uint) offset,from_length,*res3);
594
offset+=(int) to_length;
596
while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
605
void Item_func_replace::fix_length_and_dec()
607
uint64_t max_result_length= args[0]->max_length;
608
int diff=(int) (args[2]->max_length - args[1]->max_length);
609
if (diff > 0 && args[1]->max_length)
610
{ // Calculate of maxreplaces
611
uint64_t max_substrs= max_result_length/args[1]->max_length;
612
max_result_length+= max_substrs * (uint) diff;
614
if (max_result_length >= MAX_BLOB_WIDTH)
616
max_result_length= MAX_BLOB_WIDTH;
619
max_length= (ulong) max_result_length;
621
if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1))
626
String *Item_func_insert::val_str(String *str)
630
int64_t start, length; /* must be int64_t to avoid truncation */
633
res=args[0]->val_str(str);
634
res2=args[3]->val_str(&tmp_value);
635
start= args[1]->val_int() - 1;
636
length= args[2]->val_int();
638
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
640
goto null; /* purecov: inspected */
642
if ((start < 0) || (start > res->length()))
643
return res; // Wrong param; skip insert
644
if ((length < 0) || (length > res->length()))
645
length= res->length();
647
/* start and length are now sufficiently valid to pass to charpos function */
648
start= res->charpos((int) start);
649
length= res->charpos((int) length, (uint32_t) start);
651
/* Re-testing with corrected params */
652
if (start > res->length())
653
return res; /* purecov: inspected */ // Wrong param; skip insert
654
if (length > res->length() - start)
655
length= res->length() - start;
657
if ((uint64_t) (res->length() - length + res2->length()) >
658
(uint64_t) current_thd->variables.max_allowed_packet)
660
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
661
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
662
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
663
func_name(), current_thd->variables.max_allowed_packet);
666
res=copy_if_not_alloced(str,res,res->length());
667
res->replace((uint32_t) start,(uint32_t) length,*res2);
675
void Item_func_insert::fix_length_and_dec()
677
uint64_t max_result_length;
679
// Handle character set for args[0] and args[3].
680
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
682
max_result_length= ((uint64_t) args[0]->max_length+
683
(uint64_t) args[3]->max_length);
684
if (max_result_length >= MAX_BLOB_WIDTH)
686
max_result_length= MAX_BLOB_WIDTH;
689
max_length= (ulong) max_result_length;
693
String *Item_str_conv::val_str(String *str)
697
if (!(res=args[0]->val_str(str)))
699
null_value=1; /* purecov: inspected */
700
return 0; /* purecov: inspected */
706
res= copy_if_not_alloced(str,res,res->length());
707
len= converter(collation.collation, (char*) res->ptr(), res->length(),
708
(char*) res->ptr(), res->length());
709
assert(len <= res->length());
714
uint32_t len= res->length() * multiply;
715
tmp_value.alloc(len);
716
tmp_value.set_charset(collation.collation);
717
len= converter(collation.collation, (char*) res->ptr(), res->length(),
718
(char*) tmp_value.ptr(), len);
719
tmp_value.length(len);
726
void Item_func_lcase::fix_length_and_dec()
728
collation.set(args[0]->collation);
729
multiply= collation.collation->casedn_multiply;
730
converter= collation.collation->cset->casedn;
731
max_length= args[0]->max_length * multiply;
734
void Item_func_ucase::fix_length_and_dec()
736
collation.set(args[0]->collation);
737
multiply= collation.collation->caseup_multiply;
738
converter= collation.collation->cset->caseup;
739
max_length= args[0]->max_length * multiply;
743
String *Item_func_left::val_str(String *str)
746
String *res= args[0]->val_str(str);
748
/* must be int64_t to avoid truncation */
749
int64_t length= args[1]->val_int();
752
if ((null_value=(args[0]->null_value || args[1]->null_value)))
755
/* if "unsigned_flag" is set, we have a *huge* positive number. */
756
if ((length <= 0) && (!args[1]->unsigned_flag))
757
return &my_empty_string;
759
if ((res->length() <= (uint64_t) length) ||
760
(res->length() <= (char_pos= res->charpos((int) length))))
763
tmp_value.set(*res, 0, char_pos);
768
void Item_str_func::left_right_max_length()
770
max_length=args[0]->max_length;
771
if (args[1]->const_item())
773
int length=(int) args[1]->val_int()*collation.collation->mbmaxlen;
777
set_if_smaller(max_length,(uint) length);
782
void Item_func_left::fix_length_and_dec()
784
collation.set(args[0]->collation);
785
left_right_max_length();
789
String *Item_func_right::val_str(String *str)
792
String *res= args[0]->val_str(str);
793
/* must be int64_t to avoid truncation */
794
int64_t length= args[1]->val_int();
796
if ((null_value=(args[0]->null_value || args[1]->null_value)))
797
return 0; /* purecov: inspected */
799
/* if "unsigned_flag" is set, we have a *huge* positive number. */
800
if ((length <= 0) && (!args[1]->unsigned_flag))
801
return &my_empty_string; /* purecov: inspected */
803
if (res->length() <= (uint64_t) length)
804
return res; /* purecov: inspected */
806
uint32_t start=res->numchars();
807
if (start <= (uint) length)
809
start=res->charpos(start - (uint) length);
810
tmp_value.set(*res,start,res->length()-start);
815
void Item_func_right::fix_length_and_dec()
817
collation.set(args[0]->collation);
818
left_right_max_length();
822
String *Item_func_substr::val_str(String *str)
825
String *res = args[0]->val_str(str);
826
/* must be int64_t to avoid truncation */
827
int64_t start= args[1]->val_int();
828
/* Assumes that the maximum length of a String is < INT32_MAX. */
829
/* Limit so that code sees out-of-bound value properly. */
830
int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
833
if ((null_value=(args[0]->null_value || args[1]->null_value ||
834
(arg_count == 3 && args[2]->null_value))))
835
return 0; /* purecov: inspected */
837
/* Negative or zero length, will return empty string. */
838
if ((arg_count == 3) && (length <= 0) &&
839
(length == 0 || !args[2]->unsigned_flag))
840
return &my_empty_string;
842
/* Assumes that the maximum length of a String is < INT32_MAX. */
843
/* Set here so that rest of code sees out-of-bound value as such. */
844
if ((length <= 0) || (length > INT32_MAX))
847
/* if "unsigned_flag" is set, we have a *huge* positive number. */
848
/* Assumes that the maximum length of a String is < INT32_MAX. */
849
if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
850
(args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
851
return &my_empty_string;
853
start= ((start < 0) ? res->numchars() + start : start - 1);
854
start= res->charpos((int) start);
855
if ((start < 0) || ((uint) start + 1 > res->length()))
856
return &my_empty_string;
858
length= res->charpos((int) length, (uint32_t) start);
859
tmp_length= res->length() - start;
860
length= cmin(length, tmp_length);
862
if (!start && (int64_t) res->length() == length)
864
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
869
void Item_func_substr::fix_length_and_dec()
871
max_length=args[0]->max_length;
873
collation.set(args[0]->collation);
874
if (args[1]->const_item())
876
int32_t start= (int32_t) args[1]->val_int();
878
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
880
max_length-= cmin((uint)(start - 1), max_length);
882
if (arg_count == 3 && args[2]->const_item())
884
int32_t length= (int32_t) args[2]->val_int();
886
max_length=0; /* purecov: inspected */
888
set_if_smaller(max_length,(uint) length);
890
max_length*= collation.collation->mbmaxlen;
894
void Item_func_substr_index::fix_length_and_dec()
896
max_length= args[0]->max_length;
898
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
903
String *Item_func_substr_index::val_str(String *str)
906
String *res= args[0]->val_str(str);
907
String *delimiter= args[1]->val_str(&tmp_value);
908
int32_t count= (int32_t) args[2]->val_int();
911
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
912
{ // string and/or delim are null
917
uint32_t delimiter_length= delimiter->length();
918
if (!res->length() || !delimiter_length || !count)
919
return &my_empty_string; // Wrong parameters
921
res->set_charset(collation.collation);
924
if (use_mb(res->charset()))
926
const char *ptr= res->ptr();
927
const char *strend= ptr+res->length();
928
const char *end= strend-delimiter_length+1;
929
const char *search= delimiter->ptr();
930
const char *search_end= search+delimiter_length;
931
int32_t n=0,c=count,pass;
933
for (pass=(count>0);pass<2;++pass)
940
i=(char*) ptr+1; j=(char*) search+1;
941
while (j != search_end)
942
if (*i++ != *j++) goto skip;
944
else if (!--c) break;
945
ptr+= delimiter_length;
949
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
951
} /* either not found or got total number when count<0 */
952
if (pass == 0) /* count<0 */
955
if (c<=0) return res; /* not found, return original string */
960
if (c) return res; /* Not found, return original string */
961
if (count>0) /* return left part */
963
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
965
else /* return right part */
967
ptr+= delimiter_length;
968
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
977
{ // start counting from the beginning
978
for (offset=0; ; offset+= delimiter_length)
980
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
981
return res; // Didn't find, return org string
984
tmp_value.set(*res,0,offset);
992
Negative index, start counting at the end
994
for (offset=res->length(); offset ;)
997
this call will result in finding the position pointing to one
998
address space less than where the found substring is located
1001
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
1002
return res; // Didn't find, return org string
1004
At this point, we've searched for the substring
1005
the number of times as supplied by the index value
1009
offset+= delimiter_length;
1010
tmp_value.set(*res,offset,res->length()- offset);
1017
We always mark tmp_value as const so that if val_str() is called again
1018
on this object, we don't disrupt the contents of tmp_value when it was
1019
derived from another String.
1021
tmp_value.mark_as_const();
1022
return (&tmp_value);
1026
** The trim functions are extension to ANSI SQL because they trim substrings
1027
** They ltrim() and rtrim() functions are optimized for 1 byte strings
1028
** They also return the original string if possible, else they return
1029
** a substring that points at the original string.
1033
String *Item_func_ltrim::val_str(String *str)
1036
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1037
String tmp(buff,sizeof(buff),system_charset_info);
1038
String *res, *remove_str;
1039
uint32_t remove_length;
1041
res= args[0]->val_str(str);
1042
if ((null_value=args[0]->null_value))
1044
remove_str= &remove; /* Default value. */
1047
remove_str= args[1]->val_str(&tmp);
1048
if ((null_value= args[1]->null_value))
1052
if ((remove_length= remove_str->length()) == 0 ||
1053
remove_length > res->length())
1056
ptr= (char*) res->ptr();
1057
end= ptr+res->length();
1058
if (remove_length == 1)
1060
char chr=(*remove_str)[0];
1061
while (ptr != end && *ptr == chr)
1066
const char *r_ptr=remove_str->ptr();
1068
while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
1072
if (ptr == res->ptr())
1074
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1079
String *Item_func_rtrim::val_str(String *str)
1082
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1083
String tmp(buff, sizeof(buff), system_charset_info);
1084
String *res, *remove_str;
1085
uint32_t remove_length;
1087
res= args[0]->val_str(str);
1088
if ((null_value=args[0]->null_value))
1090
remove_str= &remove; /* Default value. */
1093
remove_str= args[1]->val_str(&tmp);
1094
if ((null_value= args[1]->null_value))
1098
if ((remove_length= remove_str->length()) == 0 ||
1099
remove_length > res->length())
1102
ptr= (char*) res->ptr();
1103
end= ptr+res->length();
1106
register uint32_t l;
1108
if (remove_length == 1)
1110
char chr=(*remove_str)[0];
1112
if (use_mb(res->charset()))
1116
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
1122
while (ptr != end && end[-1] == chr)
1127
const char *r_ptr=remove_str->ptr();
1129
if (use_mb(res->charset()))
1132
while (ptr + remove_length < end)
1134
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1137
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1147
while (ptr + remove_length <= end &&
1148
!memcmp(end-remove_length, r_ptr, remove_length))
1152
if (end == res->ptr()+res->length())
1154
tmp_value.set(*res,0,(uint) (end-res->ptr()));
1159
String *Item_func_trim::val_str(String *str)
1162
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1164
String tmp(buff, sizeof(buff), system_charset_info);
1165
String *res, *remove_str;
1166
uint32_t remove_length;
1168
res= args[0]->val_str(str);
1169
if ((null_value=args[0]->null_value))
1171
remove_str= &remove; /* Default value. */
1174
remove_str= args[1]->val_str(&tmp);
1175
if ((null_value= args[1]->null_value))
1179
if ((remove_length= remove_str->length()) == 0 ||
1180
remove_length > res->length())
1183
ptr= (char*) res->ptr();
1184
end= ptr+res->length();
1185
r_ptr= remove_str->ptr();
1186
while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
1189
if (use_mb(res->charset()))
1192
register uint32_t l;
1194
while (ptr + remove_length < end)
1196
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1199
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1210
while (ptr + remove_length <= end &&
1211
!memcmp(end-remove_length,r_ptr,remove_length))
1214
if (ptr == res->ptr() && end == ptr+res->length())
1216
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1220
void Item_func_trim::fix_length_and_dec()
1222
max_length= args[0]->max_length;
1225
collation.set(args[0]->collation);
1226
remove.set_charset(collation.collation);
1227
remove.set_ascii(" ",1);
1231
// Handle character set for args[1] and args[0].
1232
// Note that we pass args[1] as the first item, and args[0] as the second.
1233
if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1))
1238
void Item_func_trim::print(String *str, enum_query_type query_type)
1242
Item_func::print(str, query_type);
1245
str->append(Item_func_trim::func_name());
1247
str->append(mode_name());
1249
args[1]->print(str, query_type);
1250
str->append(STRING_WITH_LEN(" from "));
1251
args[0]->print(str, query_type);
1256
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
1259
uint32_t conv_errors;
1260
String tmp, cstr, *ostr= val_str(&tmp);
1261
cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1263
!(conv= new Item_static_string_func(fully_qualified_func_name(),
1264
cstr.ptr(), cstr.length(),
1266
collation.derivation)))
1270
conv->str_value.copy();
1271
conv->str_value.mark_as_const();
1276
String *Item_func_database::val_str(String *str)
1279
THD *thd= current_thd;
1280
if (thd->db == NULL)
1286
str->copy(thd->db, thd->db_length, system_charset_info);
1293
make USER() replicate properly (currently it is replicated to "")
1295
bool Item_func_user::init(const char *user, const char *host)
1299
// For system threads (e.g. replication SQL thread) user may be empty
1302
const CHARSET_INFO * const cs= str_value.charset();
1303
uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1305
if (str_value.alloc(res_length))
1311
res_length=cs->cset->snprintf(cs, (char*)str_value.ptr(), res_length,
1312
"%s@%s", user, host);
1313
str_value.length(res_length);
1314
str_value.mark_as_const();
1320
bool Item_func_user::fix_fields(THD *thd, Item **ref)
1322
return (Item_func_sysconst::fix_fields(thd, ref) ||
1323
init(thd->main_security_ctx.user,
1324
thd->main_security_ctx.ip));
1328
bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
1330
if (Item_func_sysconst::fix_fields(thd, ref))
1333
Security_context *ctx=
1335
return init(ctx->user, ctx->ip);
1340
Change a number to format '3,333,333,333.000'.
1342
This should be 'internationalized' sometimes.
1345
const int FORMAT_MAX_DECIMALS= 30;
1347
Item_func_format::Item_func_format(Item *org, Item *dec)
1348
: Item_str_func(org, dec)
1352
void Item_func_format::fix_length_and_dec()
1354
collation.set(default_charset());
1355
uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1356
max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1357
collation.collation->mbmaxlen);
1363
This needs to be fixed for multi-byte character set where numbers
1364
are stored in more than one byte
1367
String *Item_func_format::val_str(String *str)
1370
uint32_t str_length;
1371
/* Number of decimal digits */
1373
/* Number of characters used to represent the decimals, including '.' */
1374
uint32_t dec_length;
1378
dec= (int) args[1]->val_int();
1379
if (args[1]->null_value)
1385
dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
1386
dec_length= dec ? dec+1 : 0;
1389
if (args[0]->result_type() == DECIMAL_RESULT ||
1390
args[0]->result_type() == INT_RESULT)
1392
my_decimal dec_val, rnd_dec, *res;
1393
res= args[0]->val_decimal(&dec_val);
1394
if ((null_value=args[0]->null_value))
1395
return 0; /* purecov: inspected */
1396
my_decimal_round(E_DEC_FATAL_ERROR, res, dec, false, &rnd_dec);
1397
my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
1398
str_length= str->length();
1404
double nr= args[0]->val_real();
1405
if ((null_value=args[0]->null_value))
1406
return 0; /* purecov: inspected */
1407
nr= my_double_round(nr, (int64_t) dec, false, false);
1408
/* Here default_charset() is right as this is not an automatic conversion */
1409
str->set_real(nr, dec, default_charset());
1412
str_length=str->length();
1414
str_length--; // Don't count sign
1416
/* We need this test to handle 'nan' values */
1417
if (str_length >= dec_length+4)
1420
length= str->length()+(diff=((int)(str_length- dec_length-1))/3);
1421
str= copy_if_not_alloced(&tmp_str,str,length);
1422
str->length(length);
1423
tmp= (char*) str->ptr()+length - dec_length-1;
1424
for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--)
1428
*pos= *(pos - diff);
1430
*pos= *(pos - diff);
1432
*pos= *(pos - diff);
1443
void Item_func_format::print(String *str, enum_query_type query_type)
1445
str->append(STRING_WITH_LEN("format("));
1446
args[0]->print(str, query_type);
1448
args[1]->print(str, query_type);
1452
void Item_func_elt::fix_length_and_dec()
1457
if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1460
for (uint32_t i= 1 ; i < arg_count ; i++)
1462
set_if_bigger(max_length,args[i]->max_length);
1463
set_if_bigger(decimals,args[i]->decimals);
1465
maybe_null=1; // NULL if wrong first arg
1469
double Item_func_elt::val_real()
1474
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1476
double result= args[tmp]->val_real();
1477
null_value= args[tmp]->null_value;
1482
int64_t Item_func_elt::val_int()
1487
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1490
int64_t result= args[tmp]->val_int();
1491
null_value= args[tmp]->null_value;
1496
String *Item_func_elt::val_str(String *str)
1501
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1504
String *result= args[tmp]->val_str(str);
1506
result->set_charset(collation.collation);
1507
null_value= args[tmp]->null_value;
1512
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
1515
item->split_sum_func2(thd, ref_pointer_array, fields, &item, true);
1516
Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
1520
void Item_func_make_set::fix_length_and_dec()
1522
max_length=arg_count-1;
1524
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1527
for (uint32_t i=0 ; i < arg_count ; i++)
1528
max_length+=args[i]->max_length;
1530
used_tables_cache|= item->used_tables();
1531
not_null_tables_cache&= item->not_null_tables();
1532
const_item_cache&= item->const_item();
1533
with_sum_func= with_sum_func || item->with_sum_func;
1537
void Item_func_make_set::update_used_tables()
1539
Item_func::update_used_tables();
1540
item->update_used_tables();
1541
used_tables_cache|=item->used_tables();
1542
const_item_cache&=item->const_item();
1546
String *Item_func_make_set::val_str(String *str)
1552
String *result=&my_empty_string;
1554
bits=item->val_int();
1555
if ((null_value=item->null_value))
1559
bits &= ((uint64_t) 1 << arg_count)-1;
1561
for (; bits; bits >>= 1, ptr++)
1565
String *res= (*ptr)->val_str(str);
1566
if (res) // Skip nulls
1572
result=res; // Use original string
1575
if (tmp_str.copy(*res)) // Don't use 'str'
1576
return &my_empty_string;
1582
if (result != &tmp_str)
1583
{ // Copy data to tmp_str
1584
if (tmp_str.alloc(result->length()+res->length()+1) ||
1585
tmp_str.copy(*result))
1586
return &my_empty_string;
1589
if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
1590
return &my_empty_string;
1599
Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
1601
Item *new_item= item->transform(transformer, arg);
1606
THD::change_item_tree() should be called only if the tree was
1607
really transformed, i.e. when a new item has been created.
1608
Otherwise we'll be allocating a lot of unnecessary memory for
1609
change records at each execution.
1611
if (item != new_item)
1612
current_thd->change_item_tree(&item, new_item);
1613
return Item_str_func::transform(transformer, arg);
1617
void Item_func_make_set::print(String *str, enum_query_type query_type)
1619
str->append(STRING_WITH_LEN("make_set("));
1620
item->print(str, query_type);
1624
print_args(str, 0, query_type);
1630
String *Item_func_char::val_str(String *str)
1634
str->set_charset(collation.collation);
1635
for (uint32_t i=0 ; i < arg_count ; i++)
1637
int32_t num=(int32_t) args[i]->val_int();
1638
if (!args[i]->null_value)
1640
char char_num= (char) num;
1641
if (num&0xFF000000L) {
1642
str->append((char)(num>>24));
1644
} else if (num&0xFF0000L) {
1645
b2: str->append((char)(num>>16));
1647
} else if (num&0xFF00L) {
1648
b1: str->append((char)(num>>8));
1650
str->append(&char_num, 1);
1653
str->realloc(str->length()); // Add end 0 (for Purify)
1654
return check_well_formed_result(str);
1658
inline String* alloc_buffer(String *res,String *str,String *tmp_value,
1661
if (res->alloced_length() < length)
1663
if (str->alloced_length() >= length)
1665
(void) str->copy(*res);
1666
str->length(length);
1669
if (tmp_value->alloc(length))
1671
(void) tmp_value->copy(*res);
1672
tmp_value->length(length);
1675
res->length(length);
1680
void Item_func_repeat::fix_length_and_dec()
1682
collation.set(args[0]->collation);
1683
if (args[1]->const_item())
1685
/* must be int64_t to avoid truncation */
1686
int64_t count= args[1]->val_int();
1688
/* Assumes that the maximum length of a String is < INT32_MAX. */
1689
/* Set here so that rest of code sees out-of-bound value as such. */
1690
if (count > INT32_MAX)
1693
uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
1694
if (max_result_length >= MAX_BLOB_WIDTH)
1696
max_result_length= MAX_BLOB_WIDTH;
1699
max_length= (ulong) max_result_length;
1703
max_length= MAX_BLOB_WIDTH;
1709
Item_func_repeat::str is carefully written to avoid reallocs
1710
as much as possible at the cost of a local buffer
1713
String *Item_func_repeat::val_str(String *str)
1716
uint32_t length,tot_length;
1718
/* must be int64_t to avoid truncation */
1719
int64_t count= args[1]->val_int();
1720
String *res= args[0]->val_str(str);
1722
if (args[0]->null_value || args[1]->null_value)
1723
goto err; // string and/or delim are null
1726
if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
1727
return &my_empty_string;
1729
/* Assumes that the maximum length of a String is < INT32_MAX. */
1730
/* Bounds check on count: If this is triggered, we will error. */
1731
if ((uint64_t) count > INT32_MAX)
1733
if (count == 1) // To avoid reallocs
1735
length=res->length();
1736
// Safe length check
1737
if (length > current_thd->variables.max_allowed_packet / (uint) count)
1739
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1740
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1741
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1742
func_name(), current_thd->variables.max_allowed_packet);
1745
tot_length= length*(uint) count;
1746
if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
1749
to=(char*) res->ptr()+length;
1752
memcpy(to,res->ptr(),length);
1763
void Item_func_rpad::fix_length_and_dec()
1765
// Handle character set for args[0] and args[2].
1766
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1768
if (args[1]->const_item())
1772
if (collation.collation->mbmaxlen > 0)
1774
uint64_t temp= (uint64_t) args[1]->val_int();
1776
/* Assumes that the maximum length of a String is < INT32_MAX. */
1777
/* Set here so that rest of code sees out-of-bound value as such. */
1778
if (temp > INT32_MAX)
1781
length= temp * collation.collation->mbmaxlen;
1784
if (length >= MAX_BLOB_WIDTH)
1786
length= MAX_BLOB_WIDTH;
1789
max_length= (ulong) length;
1793
max_length= MAX_BLOB_WIDTH;
1799
String *Item_func_rpad::val_str(String *str)
1802
uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1804
const char *ptr_pad;
1805
/* must be int64_t to avoid truncation */
1806
int64_t count= args[1]->val_int();
1808
String *res= args[0]->val_str(str);
1809
String *rpad= args[2]->val_str(&rpad_str);
1811
if (!res || args[1]->null_value || !rpad ||
1812
((count < 0) && !args[1]->unsigned_flag))
1815
/* Assumes that the maximum length of a String is < INT32_MAX. */
1816
/* Set here so that rest of code sees out-of-bound value as such. */
1817
if ((uint64_t) count > INT32_MAX)
1819
if (count <= (res_char_length= res->numchars()))
1820
{ // String to pad is big enough
1821
res->length(res->charpos((int) count)); // Shorten result if longer
1824
pad_char_length= rpad->numchars();
1826
byte_count= count * collation.collation->mbmaxlen;
1827
if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1829
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1830
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1831
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1832
func_name(), current_thd->variables.max_allowed_packet);
1835
if (args[2]->null_value || !pad_char_length)
1837
res_byte_length= res->length(); /* Must be done before alloc_buffer */
1838
if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
1841
to= (char*) res->ptr()+res_byte_length;
1842
ptr_pad=rpad->ptr();
1843
pad_byte_length= rpad->length();
1844
count-= res_char_length;
1845
for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
1847
memcpy(to,ptr_pad,pad_byte_length);
1848
to+= pad_byte_length;
1852
pad_byte_length= rpad->charpos((int) count);
1853
memcpy(to,ptr_pad,(size_t) pad_byte_length);
1854
to+= pad_byte_length;
1856
res->length(to- (char*) res->ptr());
1865
void Item_func_lpad::fix_length_and_dec()
1867
// Handle character set for args[0] and args[2].
1868
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1871
if (args[1]->const_item())
1875
if (collation.collation->mbmaxlen > 0)
1877
uint64_t temp= (uint64_t) args[1]->val_int();
1879
/* Assumes that the maximum length of a String is < INT32_MAX. */
1880
/* Set here so that rest of code sees out-of-bound value as such. */
1881
if (temp > INT32_MAX)
1884
length= temp * collation.collation->mbmaxlen;
1887
if (length >= MAX_BLOB_WIDTH)
1889
length= MAX_BLOB_WIDTH;
1892
max_length= (ulong) length;
1896
max_length= MAX_BLOB_WIDTH;
1902
String *Item_func_lpad::val_str(String *str)
1905
uint32_t res_char_length,pad_char_length;
1906
/* must be int64_t to avoid truncation */
1907
int64_t count= args[1]->val_int();
1909
String *res= args[0]->val_str(&tmp_value);
1910
String *pad= args[2]->val_str(&lpad_str);
1912
if (!res || args[1]->null_value || !pad ||
1913
((count < 0) && !args[1]->unsigned_flag))
1916
/* Assumes that the maximum length of a String is < INT32_MAX. */
1917
/* Set here so that rest of code sees out-of-bound value as such. */
1918
if ((uint64_t) count > INT32_MAX)
1921
res_char_length= res->numchars();
1923
if (count <= res_char_length)
1925
res->length(res->charpos((int) count));
1929
pad_char_length= pad->numchars();
1930
byte_count= count * collation.collation->mbmaxlen;
1932
if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1934
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1935
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1936
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1937
func_name(), current_thd->variables.max_allowed_packet);
1941
if (args[2]->null_value || !pad_char_length ||
1942
str->alloc((uint32_t) byte_count))
1946
str->set_charset(collation.collation);
1947
count-= res_char_length;
1948
while (count >= pad_char_length)
1951
count-= pad_char_length;
1954
str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
1966
String *Item_func_conv::val_str(String *str)
1969
String *res= args[0]->val_str(str);
1970
char *endptr,ans[65],*ptr;
1972
int from_base= (int) args[1]->val_int();
1973
int to_base= (int) args[2]->val_int();
1976
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
1977
abs(to_base) > 36 || abs(to_base) < 2 ||
1978
abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
1984
unsigned_flag= !(from_base < 0);
1987
dec= my_strntoll(res->charset(), res->ptr(), res->length(),
1988
-from_base, &endptr, &err);
1990
dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
1991
from_base, &endptr, &err);
1993
ptr= int64_t2str(dec, ans, to_base);
1994
if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
1995
return &my_empty_string;
2000
String *Item_func_conv_charset::val_str(String *str)
2003
if (use_cached_value)
2004
return null_value ? 0 : &str_value;
2005
String *arg= args[0]->val_str(str);
2006
uint32_t dummy_errors;
2012
null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
2013
conv_charset, &dummy_errors);
2014
return null_value ? 0 : check_well_formed_result(&str_value);
2017
void Item_func_conv_charset::fix_length_and_dec()
2019
collation.set(conv_charset, DERIVATION_IMPLICIT);
2020
max_length = args[0]->max_length*conv_charset->mbmaxlen;
2023
void Item_func_conv_charset::print(String *str, enum_query_type query_type)
2025
str->append(STRING_WITH_LEN("convert("));
2026
args[0]->print(str, query_type);
2027
str->append(STRING_WITH_LEN(" using "));
2028
str->append(conv_charset->csname);
2032
String *Item_func_set_collation::val_str(String *str)
2035
str=args[0]->val_str(str);
2036
if ((null_value=args[0]->null_value))
2038
str->set_charset(collation.collation);
2042
void Item_func_set_collation::fix_length_and_dec()
2044
const CHARSET_INFO *set_collation;
2045
const char *colname;
2046
String tmp, *str= args[1]->val_str(&tmp);
2047
colname= str->c_ptr();
2048
if (colname == binary_keyword)
2049
set_collation= get_charset_by_csname(args[0]->collation.collation->csname,
2050
MY_CS_BINSORT,MYF(0));
2053
if (!(set_collation= get_charset_by_name(colname,MYF(0))))
2055
my_error(ER_UNKNOWN_COLLATION, MYF(0), colname);
2060
if (!set_collation ||
2061
!my_charset_same(args[0]->collation.collation,set_collation))
2063
my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
2064
colname, args[0]->collation.collation->csname);
2067
collation.set(set_collation, DERIVATION_EXPLICIT,
2068
args[0]->collation.repertoire);
2069
max_length= args[0]->max_length;
2073
bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const
2075
/* Assume we don't have rtti */
2078
if (item->type() != FUNC_ITEM)
2080
Item_func *item_func=(Item_func*) item;
2081
if (arg_count != item_func->arg_count ||
2082
functype() != item_func->functype())
2084
Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2085
if (collation.collation != item_func_sc->collation.collation)
2087
for (uint32_t i=0; i < arg_count ; i++)
2088
if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2094
void Item_func_set_collation::print(String *str, enum_query_type query_type)
2097
args[0]->print(str, query_type);
2098
str->append(STRING_WITH_LEN(" collate "));
2099
assert(args[1]->basic_const_item() &&
2100
args[1]->type() == Item::STRING_ITEM);
2101
args[1]->str_value.print(str);
2105
String *Item_func_charset::val_str(String *str)
2108
uint32_t dummy_errors;
2110
const CHARSET_INFO * const cs= args[0]->collation.collation;
2112
str->copy(cs->csname, strlen(cs->csname),
2113
&my_charset_utf8_general_ci, collation.collation, &dummy_errors);
2117
String *Item_func_collation::val_str(String *str)
2120
uint32_t dummy_errors;
2121
const CHARSET_INFO * const cs= args[0]->collation.collation;
2124
str->copy(cs->name, strlen(cs->name),
2125
&my_charset_utf8_general_ci, collation.collation, &dummy_errors);
2130
void Item_func_weight_string::fix_length_and_dec()
2132
const CHARSET_INFO * const cs= args[0]->collation.collation;
2133
collation.set(&my_charset_bin, args[0]->collation.derivation);
2134
flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2135
max_length= cs->mbmaxlen * cmax(args[0]->max_length, nweights);
2140
/* Return a weight_string according to collation */
2141
String *Item_func_weight_string::val_str(String *str)
2144
const CHARSET_INFO * const cs= args[0]->collation.collation;
2145
uint32_t tmp_length, frm_length;
2148
if (args[0]->result_type() != STRING_RESULT ||
2149
!(res= args[0]->val_str(str)))
2152
tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2153
cmax(res->length(), nweights));
2155
if (tmp_value.alloc(tmp_length))
2158
frm_length= cs->coll->strnxfrm(cs,
2159
(unsigned char*) tmp_value.ptr(), tmp_length,
2160
nweights ? nweights : tmp_length,
2161
(const unsigned char*) res->ptr(), res->length(),
2163
tmp_value.length(frm_length);
2173
String *Item_func_hex::val_str(String *str)
2177
if (args[0]->result_type() != STRING_RESULT)
2181
/* Return hex of unsigned int64_t value */
2182
if (args[0]->result_type() == REAL_RESULT ||
2183
args[0]->result_type() == DECIMAL_RESULT)
2185
double val= args[0]->val_real();
2186
if ((val <= (double) INT64_MIN) ||
2187
(val >= (double) (uint64_t) UINT64_MAX))
2190
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
2193
dec= (uint64_t) args[0]->val_int();
2195
if ((null_value= args[0]->null_value))
2197
ptr= int64_t2str(dec,ans,16);
2198
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
2199
return &my_empty_string; // End of memory
2203
/* Convert given string to a hex string, character by character */
2204
res= args[0]->val_str(str);
2205
if (!res || tmp_value.alloc(res->length()*2+1))
2211
tmp_value.length(res->length()*2);
2213
octet2hex((char*) tmp_value.ptr(), res->ptr(), res->length());
2217
/** Convert given hex string to a binary string. */
2219
String *Item_func_unhex::val_str(String *str)
2221
const char *from, *end;
2227
res= args[0]->val_str(str);
2228
if (!res || tmp_value.alloc(length= (1+res->length())/2))
2236
tmp_value.length(length);
2237
to= (char*) tmp_value.ptr();
2238
if (res->length() % 2)
2241
*to++= hex_char= hexchar_to_int(*from++);
2242
if ((null_value= (hex_char == -1)))
2245
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
2248
*to= (hex_char= hexchar_to_int(from[0])) << 4;
2249
if ((null_value= (hex_char == -1)))
2251
*to|= hex_char= hexchar_to_int(from[1]);
2252
if ((null_value= (hex_char == -1)))
2259
void Item_func_binary::print(String *str, enum_query_type query_type)
2261
str->append(STRING_WITH_LEN("cast("));
2262
args[0]->print(str, query_type);
2263
str->append(STRING_WITH_LEN(" as binary)"));
2267
String *Item_load_file::val_str(String *str)
2272
struct stat stat_info;
2273
char path[FN_REFLEN];
2275
if (!(file_name= args[0]->val_str(str)))
2278
(void) fn_format(path, file_name->c_ptr(), mysql_real_data_home, "",
2279
MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
2281
/* Read only allowed from within dir specified by secure_file_priv */
2282
if (opt_secure_file_priv &&
2283
strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
2286
if (stat(path, &stat_info))
2289
if (!(stat_info.st_mode & S_IROTH))
2291
/* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
2294
if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2296
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2297
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2298
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2299
func_name(), current_thd->variables.max_allowed_packet);
2302
if (tmp_value.alloc(stat_info.st_size))
2304
if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2306
if (my_read(file, (unsigned char*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2308
my_close(file, MYF(0));
2311
tmp_value.length(stat_info.st_size);
2312
my_close(file, MYF(0));
2322
String* Item_func_export_set::val_str(String* str)
2325
uint64_t the_set = (uint64_t) args[0]->val_int();
2326
String yes_buf, *yes;
2327
yes = args[1]->val_str(&yes_buf);
2329
no = args[2]->val_str(&no_buf);
2330
String *sep = NULL, sep_buf ;
2332
uint32_t num_set_values = 64;
2333
uint64_t mask = 0x1;
2335
str->set_charset(collation.collation);
2337
/* Check if some argument is a NULL value */
2338
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
2344
Arg count can only be 3, 4 or 5 here. This is guaranteed from the
2345
grammar for EXPORT_SET()
2349
num_set_values = (uint) args[4]->val_int();
2350
if (num_set_values > 64)
2352
if (args[4]->null_value)
2359
if (!(sep = args[3]->val_str(&sep_buf))) // Only true if NULL
2367
/* errors is not checked - assume "," can always be converted */
2369
sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2374
assert(0); // cannot happen
2378
for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
2384
if (i != num_set_values - 1)
2390
void Item_func_export_set::fix_length_and_dec()
2392
uint32_t length=cmax(args[1]->max_length,args[2]->max_length);
2393
uint32_t sep_length=(arg_count > 3 ? args[3]->max_length : 1);
2394
max_length=length*64+sep_length*63;
2396
if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
2397
MY_COLL_ALLOW_CONV, 1))
2402
#define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
2405
QUOTE() function returns argument string in single quotes suitable for
2406
using in a SQL statement.
2408
Adds a \\ before all characters that needs to be escaped in a SQL string.
2409
We also escape '^Z' (END-OF-FILE in windows) to avoid probelms when
2410
running commands from a file in windows.
2412
This function is very useful when you want to generate SQL statements.
2415
QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).
2423
String *Item_func_quote::val_str(String *str)
2427
Bit mask that has 1 for set for the position of the following characters:
2431
static unsigned char escmask[32]=
2433
0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2434
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2435
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2436
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2439
char *from, *to, *end, *start;
2440
String *arg= args[0]->val_str(str);
2441
uint32_t arg_length, new_length;
2442
if (!arg) // Null argument
2444
/* Return the string 'NULL' */
2445
str->copy(STRING_WITH_LEN("NULL"), collation.collation);
2450
arg_length= arg->length();
2451
new_length= arg_length+2; /* for beginning and ending ' signs */
2453
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2454
new_length+= get_esc_bit(escmask, (unsigned char) *from);
2456
if (tmp_value.alloc(new_length))
2460
We replace characters from the end to the beginning
2462
to= (char*) tmp_value.ptr() + new_length - 1;
2464
for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
2467
We can't use the bitmask here as we want to replace \O and ^Z with 0
2490
tmp_value.length(new_length);
2491
tmp_value.set_charset(collation.collation);
2502
DCE 1.1: Remote Procedure Call,
2503
Open Group Technical Standard Document Number C706, October 1997,
2504
(supersedes C309 DCE: Remote Procedure Call 8/1994,
2505
which was basis for ISO/IEC 11578:1996 specification)
2508
static struct rand_struct uuid_rand;
2509
static uint32_t nanoseq;
2510
static uint64_t uuid_time=0;
2511
static char clock_seq_and_node_str[]="-0000-000000000000";
2514
number of 100-nanosecond intervals between
2515
1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
2517
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
2519
#define UUID_VERSION 0x1000
2520
#define UUID_VARIANT 0x8000
2522
static void tohex(char *to, uint32_t from, uint32_t len)
2527
*--to= _dig_vec_lower[from & 15];
2532
static void set_clock_seq_str()
2534
uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2535
tohex(clock_seq_and_node_str+1, clock_seq, 4);
2539
String *Item_func_uuid::val_str(String *str)
2543
THD *thd= current_thd;
2545
pthread_mutex_lock(&LOCK_uuid_generator);
2546
if (! uuid_time) /* first UUID() call. initializing data */
2548
ulong tmp= sql_rnd();
2549
unsigned char mac[6];
2551
if (my_gethwaddr(mac))
2553
/* purecov: begin inspected */
2555
generating random "hardware addr"
2556
and because specs explicitly specify that it should NOT correlate
2557
with a clock_seq value (initialized random below), we use a separate
2560
randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
2561
for (i=0; i < (int)sizeof(mac); i++)
2562
mac[i]=(unsigned char)(my_rnd(&uuid_rand)*255);
2565
s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2566
for (i=sizeof(mac)-1 ; i>=0 ; i--)
2568
*--s=_dig_vec_lower[mac[i] & 15];
2569
*--s=_dig_vec_lower[mac[i] >> 4];
2571
randominit(&uuid_rand, tmp + (ulong) server_start_time,
2572
tmp + (ulong) thd->status_var.bytes_sent);
2573
set_clock_seq_str();
2576
uint64_t tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
2577
if (unlikely(tv < uuid_time))
2578
set_clock_seq_str();
2579
else if (unlikely(tv == uuid_time))
2581
/* special protection from low-res system clocks */
2592
assert(tv > uuid_time);
2595
pthread_mutex_unlock(&LOCK_uuid_generator);
2597
uint32_t time_low= (uint32_t) (tv & 0xFFFFFFFF);
2598
uint16_t time_mid= (uint16_t) ((tv >> 32) & 0xFFFF);
2599
uint16_t time_hi_and_version= (uint16_t) ((tv >> 48) | UUID_VERSION);
2601
str->realloc(UUID_LENGTH+1);
2602
str->length(UUID_LENGTH);
2603
str->set_charset(system_charset_info);
2604
s=(char *) str->ptr();
2606
tohex(s, time_low, 8);
2607
tohex(s+9, time_mid, 4);
2608
tohex(s+14, time_hi_and_version, 4);
2609
my_stpcpy(s+18, clock_seq_and_node_str);