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>
32
#include <mysys/my_static.h> // For soundex_map
34
#include <drizzled/drizzled_error_messages.h>
36
String my_empty_string("",default_charset_info);
41
bool Item_str_func::fix_fields(THD *thd, Item **ref)
43
bool res= Item_func::fix_fields(thd, ref);
45
In Item_str_func::check_well_formed_result() we may set null_value
46
flag on the same condition as in test() below.
48
maybe_null= (maybe_null ||
49
test(thd->variables.sql_mode &
50
(MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES)));
55
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
59
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
63
(void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
64
res->length(), res->charset(), decimal_value);
69
double Item_str_func::val_real()
73
char *end_not_used, buff[64];
74
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
76
return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
77
&end_not_used, &err_not_used) : 0.0;
81
int64_t Item_str_func::val_int()
86
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
89
my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
95
Concatenate args with the following premises:
96
If only one arg (which is ok), return value of arg;
97
Don't reallocate val_str() if not absolute necessary.
100
String *Item_func_concat::val_str(String *str)
103
String *res,*res2,*use_as_buff;
108
if (!(res=args[0]->val_str(str)))
110
use_as_buff= &tmp_value;
111
/* Item_subselect in --ps-protocol mode will state it as a non-const */
112
is_const= args[0]->const_item() || !args[0]->used_tables();
113
for (i=1 ; i < arg_count ; i++)
115
if (res->length() == 0)
117
if (!(res=args[i]->val_str(str)))
122
if (!(res2=args[i]->val_str(use_as_buff)))
124
if (res2->length() == 0)
126
if (res->length()+res2->length() >
127
current_thd->variables.max_allowed_packet)
129
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
130
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
131
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
132
current_thd->variables.max_allowed_packet);
135
if (!is_const && res->alloced_length() >= res->length()+res2->length())
139
else if (str->alloced_length() >= res->length()+res2->length())
142
str->replace(0,0,*res);
149
use_as_buff= &tmp_value;
151
else if (res == &tmp_value)
153
if (res->append(*res2)) // Must be a blob
156
else if (res2 == &tmp_value)
157
{ // This can happend only 1 time
158
if (tmp_value.replace(0,0,*res))
161
use_as_buff=str; // Put next arg here
163
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
164
res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
167
This happens really seldom:
168
In this case res2 is sub string of tmp_value. We will
169
now work in place in tmp_value to set it to res | res2
171
/* Chop the last characters in tmp_value that isn't in res2 */
172
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
174
/* Place res2 at start of tmp_value, remove chars before res2 */
175
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
179
use_as_buff=str; // Put next arg here
182
{ // Two big const strings
184
NOTE: We should be prudent in the initial allocation unit -- the
185
size of the arguments is a function of data distribution, which
186
can be any. Instead of overcommitting at the first row, we grow
187
the allocated amount by the factor of 2. This ensures that no
188
more than 25% of memory will be overcommitted on average.
191
uint concat_len= res->length() + res2->length();
193
if (tmp_value.alloced_length() < concat_len)
195
if (tmp_value.alloced_length() == 0)
197
if (tmp_value.alloc(concat_len))
202
uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
204
if (tmp_value.realloc(new_len))
209
if (tmp_value.copy(*res) || tmp_value.append(*res2))
218
res->set_charset(collation.collation);
227
void Item_func_concat::fix_length_and_dec()
229
uint64_t max_result_length= 0;
231
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
234
for (uint i=0 ; i < arg_count ; i++)
236
if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
237
max_result_length+= (args[i]->max_length /
238
args[i]->collation.collation->mbmaxlen) *
239
collation.collation->mbmaxlen;
241
max_result_length+= args[i]->max_length;
244
if (max_result_length >= MAX_BLOB_WIDTH)
246
max_result_length= MAX_BLOB_WIDTH;
249
max_length= (ulong) max_result_length;
254
concat with separator. First arg is the separator
255
concat_ws takes at least two arguments.
258
String *Item_func_concat_ws::val_str(String *str)
261
char tmp_str_buff[10];
262
String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
263
*sep_str, *res, *res2,*use_as_buff;
267
if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
270
use_as_buff= &tmp_value;
271
str->length(0); // QQ; Should be removed
274
// Skip until non-null argument is found.
275
// If not, return the empty string
276
for (i=1; i < arg_count; i++)
277
if ((res= args[i]->val_str(str)))
280
return &my_empty_string;
282
for (i++; i < arg_count ; i++)
284
if (!(res2= args[i]->val_str(use_as_buff)))
285
continue; // Skip NULL
287
if (res->length() + sep_str->length() + res2->length() >
288
current_thd->variables.max_allowed_packet)
290
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
291
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
292
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
293
current_thd->variables.max_allowed_packet);
296
if (res->alloced_length() >=
297
res->length() + sep_str->length() + res2->length())
299
res->append(*sep_str); // res->length() > 0 always
302
else if (str->alloced_length() >=
303
res->length() + sep_str->length() + res2->length())
305
/* We have room in str; We can't get any errors here */
307
{ // This is quote uncommon!
308
str->replace(0,0,*sep_str);
309
str->replace(0,0,*res);
314
str->append(*sep_str);
318
use_as_buff= &tmp_value;
320
else if (res == &tmp_value)
322
if (res->append(*sep_str) || res->append(*res2))
323
goto null; // Must be a blob
325
else if (res2 == &tmp_value)
326
{ // This can happend only 1 time
327
if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
330
use_as_buff=str; // Put next arg here
332
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
333
res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
336
This happens really seldom:
337
In this case res2 is sub string of tmp_value. We will
338
now work in place in tmp_value to set it to res | sep_str | res2
340
/* Chop the last characters in tmp_value that isn't in res2 */
341
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
343
/* Place res2 at start of tmp_value, remove chars before res2 */
344
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
346
tmp_value.replace(res->length(),0, *sep_str))
349
use_as_buff=str; // Put next arg here
352
{ // Two big const strings
354
NOTE: We should be prudent in the initial allocation unit -- the
355
size of the arguments is a function of data distribution, which can
356
be any. Instead of overcommitting at the first row, we grow the
357
allocated amount by the factor of 2. This ensures that no more than
358
25% of memory will be overcommitted on average.
361
uint concat_len= res->length() + sep_str->length() + res2->length();
363
if (tmp_value.alloced_length() < concat_len)
365
if (tmp_value.alloced_length() == 0)
367
if (tmp_value.alloc(concat_len))
372
uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
374
if (tmp_value.realloc(new_len))
379
if (tmp_value.copy(*res) ||
380
tmp_value.append(*sep_str) ||
381
tmp_value.append(*res2))
387
res->set_charset(collation.collation);
396
void Item_func_concat_ws::fix_length_and_dec()
398
uint64_t max_result_length;
400
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
404
arg_count cannot be less than 2,
405
it is done on parser level in sql_yacc.yy
406
so, (arg_count - 2) is safe here.
408
max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
409
for (uint i=1 ; i < arg_count ; i++)
410
max_result_length+=args[i]->max_length;
412
if (max_result_length >= MAX_BLOB_WIDTH)
414
max_result_length= MAX_BLOB_WIDTH;
417
max_length= (ulong) max_result_length;
421
String *Item_func_reverse::val_str(String *str)
424
String *res = args[0]->val_str(str);
425
char *ptr, *end, *tmp;
427
if ((null_value=args[0]->null_value))
429
/* An empty string is a special case as the string pointer may be null */
431
return &my_empty_string;
432
if (tmp_value.alloced_length() < res->length() &&
433
tmp_value.realloc(res->length()))
438
tmp_value.length(res->length());
439
tmp_value.set_charset(res->charset());
440
ptr= (char *) res->ptr();
441
end= ptr + res->length();
442
tmp= (char *) tmp_value.ptr() + tmp_value.length();
444
if (use_mb(res->charset()))
449
if ((l= my_ismbchar(res->charset(),ptr,end)))
469
void Item_func_reverse::fix_length_and_dec()
471
collation.set(args[0]->collation);
472
max_length = args[0]->max_length;
476
Replace all occurences of string2 in string1 with string3.
478
Don't reallocate val_str() if not needed.
481
Fix that this works with binary strings when using USE_MB
484
String *Item_func_replace::val_str(String *str)
487
String *res,*res2,*res3;
489
uint from_length,to_length;
492
const char *ptr,*end,*strend,*search,*search_end;
498
res=args[0]->val_str(str);
499
if (args[0]->null_value)
501
res2=args[1]->val_str(&tmp_value);
502
if (args[1]->null_value)
505
res->set_charset(collation.collation);
508
binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
511
if (res2->length() == 0)
514
if ((offset=res->strstr(*res2)) < 0)
518
if (binary_cmp && (offset=res->strstr(*res2)) < 0)
521
if (!(res3=args[2]->val_str(&tmp_value2)))
523
from_length= res2->length();
524
to_length= res3->length();
530
search_end=search+from_length;
532
ptr=res->ptr()+offset;
533
strend=res->ptr()+res->length();
534
end=strend-from_length+1;
540
i=(char*) ptr+1; j=(char*) search+1;
541
while (j != search_end)
542
if (*i++ != *j++) goto skip;
543
offset= (int) (ptr-res->ptr());
544
if (res->length()-from_length + to_length >
545
current_thd->variables.max_allowed_packet)
547
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
548
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
549
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
551
current_thd->variables.max_allowed_packet);
558
res=copy_if_not_alloced(str,res,res->length()+to_length);
560
res->replace((uint) offset,from_length,*res3);
561
offset+=(int) to_length;
565
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
573
if (res->length()-from_length + to_length >
574
current_thd->variables.max_allowed_packet)
576
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
577
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
578
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
579
current_thd->variables.max_allowed_packet);
585
res=copy_if_not_alloced(str,res,res->length()+to_length);
587
res->replace((uint) offset,from_length,*res3);
588
offset+=(int) to_length;
590
while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
599
void Item_func_replace::fix_length_and_dec()
601
uint64_t max_result_length= args[0]->max_length;
602
int diff=(int) (args[2]->max_length - args[1]->max_length);
603
if (diff > 0 && args[1]->max_length)
604
{ // Calculate of maxreplaces
605
uint64_t max_substrs= max_result_length/args[1]->max_length;
606
max_result_length+= max_substrs * (uint) diff;
608
if (max_result_length >= MAX_BLOB_WIDTH)
610
max_result_length= MAX_BLOB_WIDTH;
613
max_length= (ulong) max_result_length;
615
if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1))
620
String *Item_func_insert::val_str(String *str)
624
int64_t start, length; /* must be int64_t to avoid truncation */
627
res=args[0]->val_str(str);
628
res2=args[3]->val_str(&tmp_value);
629
start= args[1]->val_int() - 1;
630
length= args[2]->val_int();
632
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
634
goto null; /* purecov: inspected */
636
if ((start < 0) || (start > res->length()))
637
return res; // Wrong param; skip insert
638
if ((length < 0) || (length > res->length()))
639
length= res->length();
641
/* start and length are now sufficiently valid to pass to charpos function */
642
start= res->charpos((int) start);
643
length= res->charpos((int) length, (uint32_t) start);
645
/* Re-testing with corrected params */
646
if (start > res->length())
647
return res; /* purecov: inspected */ // Wrong param; skip insert
648
if (length > res->length() - start)
649
length= res->length() - start;
651
if ((uint64_t) (res->length() - length + res2->length()) >
652
(uint64_t) current_thd->variables.max_allowed_packet)
654
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
655
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
656
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
657
func_name(), current_thd->variables.max_allowed_packet);
660
res=copy_if_not_alloced(str,res,res->length());
661
res->replace((uint32_t) start,(uint32_t) length,*res2);
669
void Item_func_insert::fix_length_and_dec()
671
uint64_t max_result_length;
673
// Handle character set for args[0] and args[3].
674
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
676
max_result_length= ((uint64_t) args[0]->max_length+
677
(uint64_t) args[3]->max_length);
678
if (max_result_length >= MAX_BLOB_WIDTH)
680
max_result_length= MAX_BLOB_WIDTH;
683
max_length= (ulong) max_result_length;
687
String *Item_str_conv::val_str(String *str)
691
if (!(res=args[0]->val_str(str)))
693
null_value=1; /* purecov: inspected */
694
return 0; /* purecov: inspected */
700
res= copy_if_not_alloced(str,res,res->length());
701
len= converter(collation.collation, (char*) res->ptr(), res->length(),
702
(char*) res->ptr(), res->length());
703
assert(len <= res->length());
708
uint len= res->length() * multiply;
709
tmp_value.alloc(len);
710
tmp_value.set_charset(collation.collation);
711
len= converter(collation.collation, (char*) res->ptr(), res->length(),
712
(char*) tmp_value.ptr(), len);
713
tmp_value.length(len);
720
void Item_func_lcase::fix_length_and_dec()
722
collation.set(args[0]->collation);
723
multiply= collation.collation->casedn_multiply;
724
converter= collation.collation->cset->casedn;
725
max_length= args[0]->max_length * multiply;
728
void Item_func_ucase::fix_length_and_dec()
730
collation.set(args[0]->collation);
731
multiply= collation.collation->caseup_multiply;
732
converter= collation.collation->cset->caseup;
733
max_length= args[0]->max_length * multiply;
737
String *Item_func_left::val_str(String *str)
740
String *res= args[0]->val_str(str);
742
/* must be int64_t to avoid truncation */
743
int64_t length= args[1]->val_int();
746
if ((null_value=(args[0]->null_value || args[1]->null_value)))
749
/* if "unsigned_flag" is set, we have a *huge* positive number. */
750
if ((length <= 0) && (!args[1]->unsigned_flag))
751
return &my_empty_string;
753
if ((res->length() <= (uint64_t) length) ||
754
(res->length() <= (char_pos= res->charpos((int) length))))
757
tmp_value.set(*res, 0, char_pos);
762
void Item_str_func::left_right_max_length()
764
max_length=args[0]->max_length;
765
if (args[1]->const_item())
767
int length=(int) args[1]->val_int()*collation.collation->mbmaxlen;
771
set_if_smaller(max_length,(uint) length);
776
void Item_func_left::fix_length_and_dec()
778
collation.set(args[0]->collation);
779
left_right_max_length();
783
String *Item_func_right::val_str(String *str)
786
String *res= args[0]->val_str(str);
787
/* must be int64_t to avoid truncation */
788
int64_t length= args[1]->val_int();
790
if ((null_value=(args[0]->null_value || args[1]->null_value)))
791
return 0; /* purecov: inspected */
793
/* if "unsigned_flag" is set, we have a *huge* positive number. */
794
if ((length <= 0) && (!args[1]->unsigned_flag))
795
return &my_empty_string; /* purecov: inspected */
797
if (res->length() <= (uint64_t) length)
798
return res; /* purecov: inspected */
800
uint start=res->numchars();
801
if (start <= (uint) length)
803
start=res->charpos(start - (uint) length);
804
tmp_value.set(*res,start,res->length()-start);
809
void Item_func_right::fix_length_and_dec()
811
collation.set(args[0]->collation);
812
left_right_max_length();
816
String *Item_func_substr::val_str(String *str)
819
String *res = args[0]->val_str(str);
820
/* must be int64_t to avoid truncation */
821
int64_t start= args[1]->val_int();
822
/* Assumes that the maximum length of a String is < INT32_MAX. */
823
/* Limit so that code sees out-of-bound value properly. */
824
int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
827
if ((null_value=(args[0]->null_value || args[1]->null_value ||
828
(arg_count == 3 && args[2]->null_value))))
829
return 0; /* purecov: inspected */
831
/* Negative or zero length, will return empty string. */
832
if ((arg_count == 3) && (length <= 0) &&
833
(length == 0 || !args[2]->unsigned_flag))
834
return &my_empty_string;
836
/* Assumes that the maximum length of a String is < INT32_MAX. */
837
/* Set here so that rest of code sees out-of-bound value as such. */
838
if ((length <= 0) || (length > INT32_MAX))
841
/* if "unsigned_flag" is set, we have a *huge* positive number. */
842
/* Assumes that the maximum length of a String is < INT32_MAX. */
843
if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
844
(args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
845
return &my_empty_string;
847
start= ((start < 0) ? res->numchars() + start : start - 1);
848
start= res->charpos((int) start);
849
if ((start < 0) || ((uint) start + 1 > res->length()))
850
return &my_empty_string;
852
length= res->charpos((int) length, (uint32_t) start);
853
tmp_length= res->length() - start;
854
length= min(length, tmp_length);
856
if (!start && (int64_t) res->length() == length)
858
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
863
void Item_func_substr::fix_length_and_dec()
865
max_length=args[0]->max_length;
867
collation.set(args[0]->collation);
868
if (args[1]->const_item())
870
int32_t start= (int32_t) args[1]->val_int();
872
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
874
max_length-= min((uint)(start - 1), max_length);
876
if (arg_count == 3 && args[2]->const_item())
878
int32_t length= (int32_t) args[2]->val_int();
880
max_length=0; /* purecov: inspected */
882
set_if_smaller(max_length,(uint) length);
884
max_length*= collation.collation->mbmaxlen;
888
void Item_func_substr_index::fix_length_and_dec()
890
max_length= args[0]->max_length;
892
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
897
String *Item_func_substr_index::val_str(String *str)
900
String *res= args[0]->val_str(str);
901
String *delimiter= args[1]->val_str(&tmp_value);
902
int32_t count= (int32_t) args[2]->val_int();
905
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
906
{ // string and/or delim are null
911
uint delimiter_length= delimiter->length();
912
if (!res->length() || !delimiter_length || !count)
913
return &my_empty_string; // Wrong parameters
915
res->set_charset(collation.collation);
918
if (use_mb(res->charset()))
920
const char *ptr= res->ptr();
921
const char *strend= ptr+res->length();
922
const char *end= strend-delimiter_length+1;
923
const char *search= delimiter->ptr();
924
const char *search_end= search+delimiter_length;
925
int32_t n=0,c=count,pass;
927
for (pass=(count>0);pass<2;++pass)
934
i=(char*) ptr+1; j=(char*) search+1;
935
while (j != search_end)
936
if (*i++ != *j++) goto skip;
938
else if (!--c) break;
939
ptr+= delimiter_length;
943
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
945
} /* either not found or got total number when count<0 */
946
if (pass == 0) /* count<0 */
949
if (c<=0) return res; /* not found, return original string */
954
if (c) return res; /* Not found, return original string */
955
if (count>0) /* return left part */
957
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
959
else /* return right part */
961
ptr+= delimiter_length;
962
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
971
{ // start counting from the beginning
972
for (offset=0; ; offset+= delimiter_length)
974
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
975
return res; // Didn't find, return org string
978
tmp_value.set(*res,0,offset);
986
Negative index, start counting at the end
988
for (offset=res->length(); offset ;)
991
this call will result in finding the position pointing to one
992
address space less than where the found substring is located
995
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
996
return res; // Didn't find, return org string
998
At this point, we've searched for the substring
999
the number of times as supplied by the index value
1003
offset+= delimiter_length;
1004
tmp_value.set(*res,offset,res->length()- offset);
1011
We always mark tmp_value as const so that if val_str() is called again
1012
on this object, we don't disrupt the contents of tmp_value when it was
1013
derived from another String.
1015
tmp_value.mark_as_const();
1016
return (&tmp_value);
1020
** The trim functions are extension to ANSI SQL because they trim substrings
1021
** They ltrim() and rtrim() functions are optimized for 1 byte strings
1022
** They also return the original string if possible, else they return
1023
** a substring that points at the original string.
1027
String *Item_func_ltrim::val_str(String *str)
1030
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1031
String tmp(buff,sizeof(buff),system_charset_info);
1032
String *res, *remove_str;
1035
res= args[0]->val_str(str);
1036
if ((null_value=args[0]->null_value))
1038
remove_str= &remove; /* Default value. */
1041
remove_str= args[1]->val_str(&tmp);
1042
if ((null_value= args[1]->null_value))
1046
if ((remove_length= remove_str->length()) == 0 ||
1047
remove_length > res->length())
1050
ptr= (char*) res->ptr();
1051
end= ptr+res->length();
1052
if (remove_length == 1)
1054
char chr=(*remove_str)[0];
1055
while (ptr != end && *ptr == chr)
1060
const char *r_ptr=remove_str->ptr();
1062
while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
1066
if (ptr == res->ptr())
1068
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1073
String *Item_func_rtrim::val_str(String *str)
1076
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1077
String tmp(buff, sizeof(buff), system_charset_info);
1078
String *res, *remove_str;
1081
res= args[0]->val_str(str);
1082
if ((null_value=args[0]->null_value))
1084
remove_str= &remove; /* Default value. */
1087
remove_str= args[1]->val_str(&tmp);
1088
if ((null_value= args[1]->null_value))
1092
if ((remove_length= remove_str->length()) == 0 ||
1093
remove_length > res->length())
1096
ptr= (char*) res->ptr();
1097
end= ptr+res->length();
1100
register uint32_t l;
1102
if (remove_length == 1)
1104
char chr=(*remove_str)[0];
1106
if (use_mb(res->charset()))
1110
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
1116
while (ptr != end && end[-1] == chr)
1121
const char *r_ptr=remove_str->ptr();
1123
if (use_mb(res->charset()))
1126
while (ptr + remove_length < end)
1128
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1131
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1141
while (ptr + remove_length <= end &&
1142
!memcmp(end-remove_length, r_ptr, remove_length))
1146
if (end == res->ptr()+res->length())
1148
tmp_value.set(*res,0,(uint) (end-res->ptr()));
1153
String *Item_func_trim::val_str(String *str)
1156
char buff[MAX_FIELD_WIDTH], *ptr, *end;
1158
String tmp(buff, sizeof(buff), system_charset_info);
1159
String *res, *remove_str;
1162
res= args[0]->val_str(str);
1163
if ((null_value=args[0]->null_value))
1165
remove_str= &remove; /* Default value. */
1168
remove_str= args[1]->val_str(&tmp);
1169
if ((null_value= args[1]->null_value))
1173
if ((remove_length= remove_str->length()) == 0 ||
1174
remove_length > res->length())
1177
ptr= (char*) res->ptr();
1178
end= ptr+res->length();
1179
r_ptr= remove_str->ptr();
1180
while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
1183
if (use_mb(res->charset()))
1186
register uint32_t l;
1188
while (ptr + remove_length < end)
1190
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1193
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1204
while (ptr + remove_length <= end &&
1205
!memcmp(end-remove_length,r_ptr,remove_length))
1208
if (ptr == res->ptr() && end == ptr+res->length())
1210
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1214
void Item_func_trim::fix_length_and_dec()
1216
max_length= args[0]->max_length;
1219
collation.set(args[0]->collation);
1220
remove.set_charset(collation.collation);
1221
remove.set_ascii(" ",1);
1225
// Handle character set for args[1] and args[0].
1226
// Note that we pass args[1] as the first item, and args[0] as the second.
1227
if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1))
1232
void Item_func_trim::print(String *str, enum_query_type query_type)
1236
Item_func::print(str, query_type);
1239
str->append(Item_func_trim::func_name());
1241
str->append(mode_name());
1243
args[1]->print(str, query_type);
1244
str->append(STRING_WITH_LEN(" from "));
1245
args[0]->print(str, query_type);
1250
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
1254
String tmp, cstr, *ostr= val_str(&tmp);
1255
cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1257
!(conv= new Item_static_string_func(fully_qualified_func_name(),
1258
cstr.ptr(), cstr.length(),
1260
collation.derivation)))
1264
conv->str_value.copy();
1265
conv->str_value.mark_as_const();
1270
String *Item_func_database::val_str(String *str)
1273
THD *thd= current_thd;
1274
if (thd->db == NULL)
1280
str->copy(thd->db, thd->db_length, system_charset_info);
1287
make USER() replicate properly (currently it is replicated to "")
1289
bool Item_func_user::init(const char *user, const char *host)
1293
// For system threads (e.g. replication SQL thread) user may be empty
1296
const CHARSET_INFO * const cs= str_value.charset();
1297
uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1299
if (str_value.alloc(res_length))
1305
res_length=cs->cset->snprintf(cs, (char*)str_value.ptr(), res_length,
1306
"%s@%s", user, host);
1307
str_value.length(res_length);
1308
str_value.mark_as_const();
1314
bool Item_func_user::fix_fields(THD *thd, Item **ref)
1316
return (Item_func_sysconst::fix_fields(thd, ref) ||
1317
init(thd->main_security_ctx.user,
1318
thd->main_security_ctx.ip));
1322
bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
1324
if (Item_func_sysconst::fix_fields(thd, ref))
1327
Security_context *ctx=
1329
return init(ctx->user, ctx->ip);
1334
Change a number to format '3,333,333,333.000'.
1336
This should be 'internationalized' sometimes.
1339
const int FORMAT_MAX_DECIMALS= 30;
1341
Item_func_format::Item_func_format(Item *org, Item *dec)
1342
: Item_str_func(org, dec)
1346
void Item_func_format::fix_length_and_dec()
1348
collation.set(default_charset());
1349
uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1350
max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1351
collation.collation->mbmaxlen);
1357
This needs to be fixed for multi-byte character set where numbers
1358
are stored in more than one byte
1361
String *Item_func_format::val_str(String *str)
1364
uint32_t str_length;
1365
/* Number of decimal digits */
1367
/* Number of characters used to represent the decimals, including '.' */
1368
uint32_t dec_length;
1372
dec= (int) args[1]->val_int();
1373
if (args[1]->null_value)
1379
dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
1380
dec_length= dec ? dec+1 : 0;
1383
if (args[0]->result_type() == DECIMAL_RESULT ||
1384
args[0]->result_type() == INT_RESULT)
1386
my_decimal dec_val, rnd_dec, *res;
1387
res= args[0]->val_decimal(&dec_val);
1388
if ((null_value=args[0]->null_value))
1389
return 0; /* purecov: inspected */
1390
my_decimal_round(E_DEC_FATAL_ERROR, res, dec, false, &rnd_dec);
1391
my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
1392
str_length= str->length();
1398
double nr= args[0]->val_real();
1399
if ((null_value=args[0]->null_value))
1400
return 0; /* purecov: inspected */
1401
nr= my_double_round(nr, (int64_t) dec, false, false);
1402
/* Here default_charset() is right as this is not an automatic conversion */
1403
str->set_real(nr, dec, default_charset());
1406
str_length=str->length();
1408
str_length--; // Don't count sign
1410
/* We need this test to handle 'nan' values */
1411
if (str_length >= dec_length+4)
1414
length= str->length()+(diff=((int)(str_length- dec_length-1))/3);
1415
str= copy_if_not_alloced(&tmp_str,str,length);
1416
str->length(length);
1417
tmp= (char*) str->ptr()+length - dec_length-1;
1418
for (pos= (char*) str->ptr()+length-1; pos != tmp; pos--)
1422
*pos= *(pos - diff);
1424
*pos= *(pos - diff);
1426
*pos= *(pos - diff);
1437
void Item_func_format::print(String *str, enum_query_type query_type)
1439
str->append(STRING_WITH_LEN("format("));
1440
args[0]->print(str, query_type);
1442
args[1]->print(str, query_type);
1446
void Item_func_elt::fix_length_and_dec()
1451
if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1454
for (uint i= 1 ; i < arg_count ; i++)
1456
set_if_bigger(max_length,args[i]->max_length);
1457
set_if_bigger(decimals,args[i]->decimals);
1459
maybe_null=1; // NULL if wrong first arg
1463
double Item_func_elt::val_real()
1468
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1470
double result= args[tmp]->val_real();
1471
null_value= args[tmp]->null_value;
1476
int64_t Item_func_elt::val_int()
1481
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1484
int64_t result= args[tmp]->val_int();
1485
null_value= args[tmp]->null_value;
1490
String *Item_func_elt::val_str(String *str)
1495
if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1498
String *result= args[tmp]->val_str(str);
1500
result->set_charset(collation.collation);
1501
null_value= args[tmp]->null_value;
1506
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
1509
item->split_sum_func2(thd, ref_pointer_array, fields, &item, true);
1510
Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
1514
void Item_func_make_set::fix_length_and_dec()
1516
max_length=arg_count-1;
1518
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1521
for (uint i=0 ; i < arg_count ; i++)
1522
max_length+=args[i]->max_length;
1524
used_tables_cache|= item->used_tables();
1525
not_null_tables_cache&= item->not_null_tables();
1526
const_item_cache&= item->const_item();
1527
with_sum_func= with_sum_func || item->with_sum_func;
1531
void Item_func_make_set::update_used_tables()
1533
Item_func::update_used_tables();
1534
item->update_used_tables();
1535
used_tables_cache|=item->used_tables();
1536
const_item_cache&=item->const_item();
1540
String *Item_func_make_set::val_str(String *str)
1546
String *result=&my_empty_string;
1548
bits=item->val_int();
1549
if ((null_value=item->null_value))
1553
bits &= ((uint64_t) 1 << arg_count)-1;
1555
for (; bits; bits >>= 1, ptr++)
1559
String *res= (*ptr)->val_str(str);
1560
if (res) // Skip nulls
1566
result=res; // Use original string
1569
if (tmp_str.copy(*res)) // Don't use 'str'
1570
return &my_empty_string;
1576
if (result != &tmp_str)
1577
{ // Copy data to tmp_str
1578
if (tmp_str.alloc(result->length()+res->length()+1) ||
1579
tmp_str.copy(*result))
1580
return &my_empty_string;
1583
if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
1584
return &my_empty_string;
1593
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
1595
Item *new_item= item->transform(transformer, arg);
1600
THD::change_item_tree() should be called only if the tree was
1601
really transformed, i.e. when a new item has been created.
1602
Otherwise we'll be allocating a lot of unnecessary memory for
1603
change records at each execution.
1605
if (item != new_item)
1606
current_thd->change_item_tree(&item, new_item);
1607
return Item_str_func::transform(transformer, arg);
1611
void Item_func_make_set::print(String *str, enum_query_type query_type)
1613
str->append(STRING_WITH_LEN("make_set("));
1614
item->print(str, query_type);
1618
print_args(str, 0, query_type);
1624
String *Item_func_char::val_str(String *str)
1628
str->set_charset(collation.collation);
1629
for (uint i=0 ; i < arg_count ; i++)
1631
int32_t num=(int32_t) args[i]->val_int();
1632
if (!args[i]->null_value)
1634
char char_num= (char) num;
1635
if (num&0xFF000000L) {
1636
str->append((char)(num>>24));
1638
} else if (num&0xFF0000L) {
1639
b2: str->append((char)(num>>16));
1641
} else if (num&0xFF00L) {
1642
b1: str->append((char)(num>>8));
1644
str->append(&char_num, 1);
1647
str->realloc(str->length()); // Add end 0 (for Purify)
1648
return check_well_formed_result(str);
1652
inline String* alloc_buffer(String *res,String *str,String *tmp_value,
1655
if (res->alloced_length() < length)
1657
if (str->alloced_length() >= length)
1659
(void) str->copy(*res);
1660
str->length(length);
1663
if (tmp_value->alloc(length))
1665
(void) tmp_value->copy(*res);
1666
tmp_value->length(length);
1669
res->length(length);
1674
void Item_func_repeat::fix_length_and_dec()
1676
collation.set(args[0]->collation);
1677
if (args[1]->const_item())
1679
/* must be int64_t to avoid truncation */
1680
int64_t count= args[1]->val_int();
1682
/* Assumes that the maximum length of a String is < INT32_MAX. */
1683
/* Set here so that rest of code sees out-of-bound value as such. */
1684
if (count > INT32_MAX)
1687
uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
1688
if (max_result_length >= MAX_BLOB_WIDTH)
1690
max_result_length= MAX_BLOB_WIDTH;
1693
max_length= (ulong) max_result_length;
1697
max_length= MAX_BLOB_WIDTH;
1703
Item_func_repeat::str is carefully written to avoid reallocs
1704
as much as possible at the cost of a local buffer
1707
String *Item_func_repeat::val_str(String *str)
1710
uint length,tot_length;
1712
/* must be int64_t to avoid truncation */
1713
int64_t count= args[1]->val_int();
1714
String *res= args[0]->val_str(str);
1716
if (args[0]->null_value || args[1]->null_value)
1717
goto err; // string and/or delim are null
1720
if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
1721
return &my_empty_string;
1723
/* Assumes that the maximum length of a String is < INT32_MAX. */
1724
/* Bounds check on count: If this is triggered, we will error. */
1725
if ((uint64_t) count > INT32_MAX)
1727
if (count == 1) // To avoid reallocs
1729
length=res->length();
1730
// Safe length check
1731
if (length > current_thd->variables.max_allowed_packet / (uint) count)
1733
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1734
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1735
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1736
func_name(), current_thd->variables.max_allowed_packet);
1739
tot_length= length*(uint) count;
1740
if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
1743
to=(char*) res->ptr()+length;
1746
memcpy(to,res->ptr(),length);
1757
void Item_func_rpad::fix_length_and_dec()
1759
// Handle character set for args[0] and args[2].
1760
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1762
if (args[1]->const_item())
1766
if (collation.collation->mbmaxlen > 0)
1768
uint64_t temp= (uint64_t) args[1]->val_int();
1770
/* Assumes that the maximum length of a String is < INT32_MAX. */
1771
/* Set here so that rest of code sees out-of-bound value as such. */
1772
if (temp > INT32_MAX)
1775
length= temp * collation.collation->mbmaxlen;
1778
if (length >= MAX_BLOB_WIDTH)
1780
length= MAX_BLOB_WIDTH;
1783
max_length= (ulong) length;
1787
max_length= MAX_BLOB_WIDTH;
1793
String *Item_func_rpad::val_str(String *str)
1796
uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1798
const char *ptr_pad;
1799
/* must be int64_t to avoid truncation */
1800
int64_t count= args[1]->val_int();
1802
String *res= args[0]->val_str(str);
1803
String *rpad= args[2]->val_str(&rpad_str);
1805
if (!res || args[1]->null_value || !rpad ||
1806
((count < 0) && !args[1]->unsigned_flag))
1809
/* Assumes that the maximum length of a String is < INT32_MAX. */
1810
/* Set here so that rest of code sees out-of-bound value as such. */
1811
if ((uint64_t) count > INT32_MAX)
1813
if (count <= (res_char_length= res->numchars()))
1814
{ // String to pad is big enough
1815
res->length(res->charpos((int) count)); // Shorten result if longer
1818
pad_char_length= rpad->numchars();
1820
byte_count= count * collation.collation->mbmaxlen;
1821
if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1823
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1824
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1825
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1826
func_name(), current_thd->variables.max_allowed_packet);
1829
if (args[2]->null_value || !pad_char_length)
1831
res_byte_length= res->length(); /* Must be done before alloc_buffer */
1832
if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
1835
to= (char*) res->ptr()+res_byte_length;
1836
ptr_pad=rpad->ptr();
1837
pad_byte_length= rpad->length();
1838
count-= res_char_length;
1839
for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
1841
memcpy(to,ptr_pad,pad_byte_length);
1842
to+= pad_byte_length;
1846
pad_byte_length= rpad->charpos((int) count);
1847
memcpy(to,ptr_pad,(size_t) pad_byte_length);
1848
to+= pad_byte_length;
1850
res->length(to- (char*) res->ptr());
1859
void Item_func_lpad::fix_length_and_dec()
1861
// Handle character set for args[0] and args[2].
1862
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1865
if (args[1]->const_item())
1869
if (collation.collation->mbmaxlen > 0)
1871
uint64_t temp= (uint64_t) args[1]->val_int();
1873
/* Assumes that the maximum length of a String is < INT32_MAX. */
1874
/* Set here so that rest of code sees out-of-bound value as such. */
1875
if (temp > INT32_MAX)
1878
length= temp * collation.collation->mbmaxlen;
1881
if (length >= MAX_BLOB_WIDTH)
1883
length= MAX_BLOB_WIDTH;
1886
max_length= (ulong) length;
1890
max_length= MAX_BLOB_WIDTH;
1896
String *Item_func_lpad::val_str(String *str)
1899
uint32_t res_char_length,pad_char_length;
1900
/* must be int64_t to avoid truncation */
1901
int64_t count= args[1]->val_int();
1903
String *res= args[0]->val_str(&tmp_value);
1904
String *pad= args[2]->val_str(&lpad_str);
1906
if (!res || args[1]->null_value || !pad ||
1907
((count < 0) && !args[1]->unsigned_flag))
1910
/* Assumes that the maximum length of a String is < INT32_MAX. */
1911
/* Set here so that rest of code sees out-of-bound value as such. */
1912
if ((uint64_t) count > INT32_MAX)
1915
res_char_length= res->numchars();
1917
if (count <= res_char_length)
1919
res->length(res->charpos((int) count));
1923
pad_char_length= pad->numchars();
1924
byte_count= count * collation.collation->mbmaxlen;
1926
if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1928
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1929
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1930
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1931
func_name(), current_thd->variables.max_allowed_packet);
1935
if (args[2]->null_value || !pad_char_length ||
1936
str->alloc((uint32_t) byte_count))
1940
str->set_charset(collation.collation);
1941
count-= res_char_length;
1942
while (count >= pad_char_length)
1945
count-= pad_char_length;
1948
str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
1960
String *Item_func_conv::val_str(String *str)
1963
String *res= args[0]->val_str(str);
1964
char *endptr,ans[65],*ptr;
1966
int from_base= (int) args[1]->val_int();
1967
int to_base= (int) args[2]->val_int();
1970
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
1971
abs(to_base) > 36 || abs(to_base) < 2 ||
1972
abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
1978
unsigned_flag= !(from_base < 0);
1981
dec= my_strntoll(res->charset(), res->ptr(), res->length(),
1982
-from_base, &endptr, &err);
1984
dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
1985
from_base, &endptr, &err);
1987
ptr= int64_t2str(dec, ans, to_base);
1988
if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
1989
return &my_empty_string;
1994
String *Item_func_conv_charset::val_str(String *str)
1997
if (use_cached_value)
1998
return null_value ? 0 : &str_value;
1999
String *arg= args[0]->val_str(str);
2006
null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
2007
conv_charset, &dummy_errors);
2008
return null_value ? 0 : check_well_formed_result(&str_value);
2011
void Item_func_conv_charset::fix_length_and_dec()
2013
collation.set(conv_charset, DERIVATION_IMPLICIT);
2014
max_length = args[0]->max_length*conv_charset->mbmaxlen;
2017
void Item_func_conv_charset::print(String *str, enum_query_type query_type)
2019
str->append(STRING_WITH_LEN("convert("));
2020
args[0]->print(str, query_type);
2021
str->append(STRING_WITH_LEN(" using "));
2022
str->append(conv_charset->csname);
2026
String *Item_func_set_collation::val_str(String *str)
2029
str=args[0]->val_str(str);
2030
if ((null_value=args[0]->null_value))
2032
str->set_charset(collation.collation);
2036
void Item_func_set_collation::fix_length_and_dec()
2038
const CHARSET_INFO *set_collation;
2039
const char *colname;
2040
String tmp, *str= args[1]->val_str(&tmp);
2041
colname= str->c_ptr();
2042
if (colname == binary_keyword)
2043
set_collation= get_charset_by_csname(args[0]->collation.collation->csname,
2044
MY_CS_BINSORT,MYF(0));
2047
if (!(set_collation= get_charset_by_name(colname,MYF(0))))
2049
my_error(ER_UNKNOWN_COLLATION, MYF(0), colname);
2054
if (!set_collation ||
2055
!my_charset_same(args[0]->collation.collation,set_collation))
2057
my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
2058
colname, args[0]->collation.collation->csname);
2061
collation.set(set_collation, DERIVATION_EXPLICIT,
2062
args[0]->collation.repertoire);
2063
max_length= args[0]->max_length;
2067
bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const
2069
/* Assume we don't have rtti */
2072
if (item->type() != FUNC_ITEM)
2074
Item_func *item_func=(Item_func*) item;
2075
if (arg_count != item_func->arg_count ||
2076
functype() != item_func->functype())
2078
Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2079
if (collation.collation != item_func_sc->collation.collation)
2081
for (uint i=0; i < arg_count ; i++)
2082
if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2088
void Item_func_set_collation::print(String *str, enum_query_type query_type)
2091
args[0]->print(str, query_type);
2092
str->append(STRING_WITH_LEN(" collate "));
2093
assert(args[1]->basic_const_item() &&
2094
args[1]->type() == Item::STRING_ITEM);
2095
args[1]->str_value.print(str);
2099
String *Item_func_charset::val_str(String *str)
2104
const CHARSET_INFO * const cs= args[0]->collation.collation;
2106
str->copy(cs->csname, strlen(cs->csname),
2107
&my_charset_latin1, collation.collation, &dummy_errors);
2111
String *Item_func_collation::val_str(String *str)
2115
const CHARSET_INFO * const cs= args[0]->collation.collation;
2118
str->copy(cs->name, strlen(cs->name),
2119
&my_charset_latin1, collation.collation, &dummy_errors);
2124
void Item_func_weight_string::fix_length_and_dec()
2126
const CHARSET_INFO * const cs= args[0]->collation.collation;
2127
collation.set(&my_charset_bin, args[0]->collation.derivation);
2128
flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2129
max_length= cs->mbmaxlen * max(args[0]->max_length, nweights);
2134
/* Return a weight_string according to collation */
2135
String *Item_func_weight_string::val_str(String *str)
2138
const CHARSET_INFO * const cs= args[0]->collation.collation;
2139
uint tmp_length, frm_length;
2142
if (args[0]->result_type() != STRING_RESULT ||
2143
!(res= args[0]->val_str(str)))
2146
tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2147
max(res->length(), nweights));
2149
if (tmp_value.alloc(tmp_length))
2152
frm_length= cs->coll->strnxfrm(cs,
2153
(uchar*) tmp_value.ptr(), tmp_length,
2154
nweights ? nweights : tmp_length,
2155
(const uchar*) res->ptr(), res->length(),
2157
tmp_value.length(frm_length);
2167
String *Item_func_hex::val_str(String *str)
2171
if (args[0]->result_type() != STRING_RESULT)
2175
/* Return hex of unsigned int64_t value */
2176
if (args[0]->result_type() == REAL_RESULT ||
2177
args[0]->result_type() == DECIMAL_RESULT)
2179
double val= args[0]->val_real();
2180
if ((val <= (double) INT64_MIN) ||
2181
(val >= (double) (uint64_t) UINT64_MAX))
2184
dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
2187
dec= (uint64_t) args[0]->val_int();
2189
if ((null_value= args[0]->null_value))
2191
ptr= int64_t2str(dec,ans,16);
2192
if (str->copy(ans,(uint32_t) (ptr-ans),default_charset()))
2193
return &my_empty_string; // End of memory
2197
/* Convert given string to a hex string, character by character */
2198
res= args[0]->val_str(str);
2199
if (!res || tmp_value.alloc(res->length()*2+1))
2205
tmp_value.length(res->length()*2);
2207
octet2hex((char*) tmp_value.ptr(), res->ptr(), res->length());
2211
/** Convert given hex string to a binary string. */
2213
String *Item_func_unhex::val_str(String *str)
2215
const char *from, *end;
2221
res= args[0]->val_str(str);
2222
if (!res || tmp_value.alloc(length= (1+res->length())/2))
2230
tmp_value.length(length);
2231
to= (char*) tmp_value.ptr();
2232
if (res->length() % 2)
2235
*to++= hex_char= hexchar_to_int(*from++);
2236
if ((null_value= (hex_char == -1)))
2239
for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
2242
*to= (hex_char= hexchar_to_int(from[0])) << 4;
2243
if ((null_value= (hex_char == -1)))
2245
*to|= hex_char= hexchar_to_int(from[1]);
2246
if ((null_value= (hex_char == -1)))
2253
void Item_func_binary::print(String *str, enum_query_type query_type)
2255
str->append(STRING_WITH_LEN("cast("));
2256
args[0]->print(str, query_type);
2257
str->append(STRING_WITH_LEN(" as binary)"));
2261
String *Item_load_file::val_str(String *str)
2266
struct stat stat_info;
2267
char path[FN_REFLEN];
2269
if (!(file_name= args[0]->val_str(str)))
2272
(void) fn_format(path, file_name->c_ptr(), mysql_real_data_home, "",
2273
MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
2275
/* Read only allowed from within dir specified by secure_file_priv */
2276
if (opt_secure_file_priv &&
2277
strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
2280
if (stat(path, &stat_info))
2283
if (!(stat_info.st_mode & S_IROTH))
2285
/* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
2288
if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2290
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2291
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2292
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2293
func_name(), current_thd->variables.max_allowed_packet);
2296
if (tmp_value.alloc(stat_info.st_size))
2298
if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2300
if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2302
my_close(file, MYF(0));
2305
tmp_value.length(stat_info.st_size);
2306
my_close(file, MYF(0));
2316
String* Item_func_export_set::val_str(String* str)
2319
uint64_t the_set = (uint64_t) args[0]->val_int();
2320
String yes_buf, *yes;
2321
yes = args[1]->val_str(&yes_buf);
2323
no = args[2]->val_str(&no_buf);
2324
String *sep = NULL, sep_buf ;
2326
uint num_set_values = 64;
2327
uint64_t mask = 0x1;
2329
str->set_charset(collation.collation);
2331
/* Check if some argument is a NULL value */
2332
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
2338
Arg count can only be 3, 4 or 5 here. This is guaranteed from the
2339
grammar for EXPORT_SET()
2343
num_set_values = (uint) args[4]->val_int();
2344
if (num_set_values > 64)
2346
if (args[4]->null_value)
2353
if (!(sep = args[3]->val_str(&sep_buf))) // Only true if NULL
2361
/* errors is not checked - assume "," can always be converted */
2363
sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2368
assert(0); // cannot happen
2372
for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
2378
if (i != num_set_values - 1)
2384
void Item_func_export_set::fix_length_and_dec()
2386
uint length=max(args[1]->max_length,args[2]->max_length);
2387
uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
2388
max_length=length*64+sep_length*63;
2390
if (agg_arg_charsets(collation, args+1, min((uint)4,arg_count)-1,
2391
MY_COLL_ALLOW_CONV, 1))
2396
#define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
2399
QUOTE() function returns argument string in single quotes suitable for
2400
using in a SQL statement.
2402
Adds a \\ before all characters that needs to be escaped in a SQL string.
2403
We also escape '^Z' (END-OF-FILE in windows) to avoid probelms when
2404
running commands from a file in windows.
2406
This function is very useful when you want to generate SQL statements.
2409
QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).
2417
String *Item_func_quote::val_str(String *str)
2421
Bit mask that has 1 for set for the position of the following characters:
2425
static uchar escmask[32]=
2427
0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2428
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2429
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2430
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2433
char *from, *to, *end, *start;
2434
String *arg= args[0]->val_str(str);
2435
uint arg_length, new_length;
2436
if (!arg) // Null argument
2438
/* Return the string 'NULL' */
2439
str->copy(STRING_WITH_LEN("NULL"), collation.collation);
2444
arg_length= arg->length();
2445
new_length= arg_length+2; /* for beginning and ending ' signs */
2447
for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2448
new_length+= get_esc_bit(escmask, (uchar) *from);
2450
if (tmp_value.alloc(new_length))
2454
We replace characters from the end to the beginning
2456
to= (char*) tmp_value.ptr() + new_length - 1;
2458
for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
2461
We can't use the bitmask here as we want to replace \O and ^Z with 0
2484
tmp_value.length(new_length);
2485
tmp_value.set_charset(collation.collation);
2494
int64_t Item_func_uncompressed_length::val_int()
2497
String *res= args[0]->val_str(&value);
2501
return 0; /* purecov: inspected */
2504
if (res->is_empty()) return 0;
2507
res->ptr() using is safe because we have tested that string is not empty,
2508
res->c_ptr() is not used because:
2509
- we do not need \0 terminated string to get first 4 bytes
2510
- c_ptr() tests simbol after string end (uninitialiozed memory) which
2513
return uint4korr(res->ptr()) & 0x3FFFFFFF;
2516
#ifdef HAVE_COMPRESS
2519
String *Item_func_compress::val_str(String *str)
2521
int err= Z_OK, code;
2525
char *tmp, *last_char;
2528
if (!(res= args[0]->val_str(str)))
2534
if (res->is_empty()) return res;
2537
Citation from zlib.h (comment for compress function):
2539
Compresses the source buffer into the destination buffer. sourceLen is
2540
the byte length of the source buffer. Upon entry, destLen is the total
2541
size of the destination buffer, which must be at least 0.1% larger than
2542
sourceLen plus 12 bytes.
2543
We assume here that the buffer can't grow more than .25 %.
2545
new_size= res->length() + res->length() / 5 + 12;
2547
// Check new_size overflow: new_size <= res->length()
2548
if (((uint32_t) (new_size+5) <= res->length()) ||
2549
buffer.realloc((uint32_t) new_size + 4 + 1))
2555
body= ((Byte*)buffer.ptr()) + 4;
2557
// As far as we have checked res->is_empty() we can use ptr()
2558
if ((err= compress(body, &new_size,
2559
(const Bytef*)res->ptr(), res->length())) != Z_OK)
2561
code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
2562
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
2567
tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
2568
int4store(tmp, res->length() & 0x3FFFFFFF);
2570
/* This is to ensure that things works for CHAR fields, which trim ' ': */
2571
last_char= ((char*)body)+new_size-1;
2572
if (*last_char == ' ')
2578
buffer.length((uint32_t)new_size + 4);
2583
String *Item_func_uncompress::val_str(String *str)
2586
String *res= args[0]->val_str(str);
2594
if (res->is_empty())
2597
/* If length is less than 4 bytes, data is corrupt */
2598
if (res->length() <= 4)
2600
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2601
ER_ZLIB_Z_DATA_ERROR,
2602
ER(ER_ZLIB_Z_DATA_ERROR));
2606
/* Size of uncompressed data is stored as first 4 bytes of field */
2607
new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
2608
if (new_size > current_thd->variables.max_allowed_packet)
2610
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2611
ER_TOO_BIG_FOR_UNCOMPRESS,
2612
ER(ER_TOO_BIG_FOR_UNCOMPRESS),
2613
current_thd->variables.max_allowed_packet);
2616
if (buffer.realloc((uint32_t)new_size))
2619
if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
2620
((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
2622
buffer.length((uint32_t) new_size);
2626
code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
2627
((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
2628
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
2638
DCE 1.1: Remote Procedure Call,
2639
Open Group Technical Standard Document Number C706, October 1997,
2640
(supersedes C309 DCE: Remote Procedure Call 8/1994,
2641
which was basis for ISO/IEC 11578:1996 specification)
2644
static struct rand_struct uuid_rand;
2645
static uint nanoseq;
2646
static uint64_t uuid_time=0;
2647
static char clock_seq_and_node_str[]="-0000-000000000000";
2650
number of 100-nanosecond intervals between
2651
1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
2653
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
2655
#define UUID_VERSION 0x1000
2656
#define UUID_VARIANT 0x8000
2658
static void tohex(char *to, uint from, uint len)
2663
*--to= _dig_vec_lower[from & 15];
2668
static void set_clock_seq_str()
2670
uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2671
tohex(clock_seq_and_node_str+1, clock_seq, 4);
2675
String *Item_func_uuid::val_str(String *str)
2679
THD *thd= current_thd;
2681
pthread_mutex_lock(&LOCK_uuid_generator);
2682
if (! uuid_time) /* first UUID() call. initializing data */
2684
ulong tmp= sql_rnd();
2687
if (my_gethwaddr(mac))
2689
/* purecov: begin inspected */
2691
generating random "hardware addr"
2692
and because specs explicitly specify that it should NOT correlate
2693
with a clock_seq value (initialized random below), we use a separate
2696
randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
2697
for (i=0; i < (int)sizeof(mac); i++)
2698
mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
2701
s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2702
for (i=sizeof(mac)-1 ; i>=0 ; i--)
2704
*--s=_dig_vec_lower[mac[i] & 15];
2705
*--s=_dig_vec_lower[mac[i] >> 4];
2707
randominit(&uuid_rand, tmp + (ulong) server_start_time,
2708
tmp + (ulong) thd->status_var.bytes_sent);
2709
set_clock_seq_str();
2712
uint64_t tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
2713
if (unlikely(tv < uuid_time))
2714
set_clock_seq_str();
2715
else if (unlikely(tv == uuid_time))
2717
/* special protection from low-res system clocks */
2728
assert(tv > uuid_time);
2731
pthread_mutex_unlock(&LOCK_uuid_generator);
2733
uint32_t time_low= (uint32_t) (tv & 0xFFFFFFFF);
2734
uint16_t time_mid= (uint16_t) ((tv >> 32) & 0xFFFF);
2735
uint16_t time_hi_and_version= (uint16_t) ((tv >> 48) | UUID_VERSION);
2737
str->realloc(UUID_LENGTH+1);
2738
str->length(UUID_LENGTH);
2739
str->set_charset(system_charset_info);
2740
s=(char *) str->ptr();
2742
tohex(s, time_low, 8);
2743
tohex(s+9, time_mid, 4);
2744
tohex(s+14, time_hi_and_version, 4);
2745
stpcpy(s+18, clock_seq_and_node_str);