32 #include "my_global.h"
41 #include "sql_class.h"
45 #include "des_key_file.h"
48 #include "crypt_genhash_impl.h"
56 #include "../mysys/my_static.h"
73 DBUG_ASSERT(fixed == 1);
75 if (!(collation.collation->state & MY_CS_NONASCII))
77 String *res= val_str_ascii(str);
79 res->set_charset(collation.collation);
83 DBUG_ASSERT(str != str2);
86 String *res= val_str_ascii(str);
90 if ((null_value= str2->copy(res->ptr(), res->length(),
91 &my_charset_latin1, collation.collation,
98 bool Item_str_func::fix_fields(THD *thd,
Item **ref)
100 bool res= Item_func::fix_fields(thd, ref);
105 maybe_null= (maybe_null || thd->is_strict_mode());
112 DBUG_ASSERT(fixed == 1);
114 String *res, tmp(buff,
sizeof(buff), &my_charset_bin);
118 (void)str2my_decimal(E_DEC_FATAL_ERROR, (
char*) res->ptr(),
119 res->length(), res->charset(), decimal_value);
120 return decimal_value;
124 double Item_str_func::val_real()
126 DBUG_ASSERT(fixed == 1);
128 char *end_not_used, buff[64];
129 String *res, tmp(buff,
sizeof(buff), &my_charset_bin);
131 return res ? my_strntod(res->charset(), (
char*) res->ptr(), res->length(),
132 &end_not_used, &err_not_used) : 0.0;
136 longlong Item_str_func::val_int()
138 DBUG_ASSERT(fixed == 1);
141 String *res, tmp(buff,
sizeof(buff), &my_charset_bin);
144 my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
152 DBUG_ASSERT(fixed == 1);
153 String * sptr= args[0]->val_str(str);
154 str->set_charset(&my_charset_bin);
157 uchar digest[MD5_HASH_SIZE];
160 compute_md5_hash((
char *) digest, (
const char *) sptr->ptr(), sptr->length());
166 array_to_hex((
char *) str->ptr(), digest, MD5_HASH_SIZE);
167 str->length((uint) 32);
175 void Item_func_md5::fix_length_and_dec()
182 args[0]->collation.set(
183 get_charset_by_csname(args[0]->collation.collation->csname,
184 MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
185 fix_length_and_charset(32, default_charset());
191 DBUG_ASSERT(fixed == 1);
192 String * sptr= args[0]->val_str(str);
193 str->set_charset(&my_charset_bin);
197 uint8 digest[SHA1_HASH_SIZE];
200 if (!(str->alloc(SHA1_HASH_SIZE * 2)))
202 array_to_hex((
char *) str->ptr(), digest, SHA1_HASH_SIZE);
203 str->length((uint) SHA1_HASH_SIZE*2);
212 void Item_func_sha::fix_length_and_dec()
219 args[0]->collation.set(
220 get_charset_by_csname(args[0]->collation.collation->csname,
221 MY_CS_BINSORT,MYF(0)), DERIVATION_COERCIBLE);
223 fix_length_and_charset(SHA1_HASH_SIZE * 2, default_charset());
228 DBUG_ASSERT(fixed == 1);
229 #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
230 unsigned char digest_buf[SHA512_DIGEST_LENGTH];
232 unsigned char *input_ptr;
234 uint digest_length= 0;
236 str->set_charset(&my_charset_bin);
238 input_string= args[0]->val_str(str);
239 if (input_string == NULL)
245 null_value= args[0]->null_value;
249 input_ptr= (
unsigned char *) input_string->ptr();
250 input_len= input_string->length();
252 switch ((uint) args[1]->val_int()) {
253 #ifndef OPENSSL_NO_SHA512
255 digest_length= SHA512_DIGEST_LENGTH;
256 (void) SHA512(input_ptr, input_len, digest_buf);
259 digest_length= SHA384_DIGEST_LENGTH;
260 (void) SHA384(input_ptr, input_len, digest_buf);
263 #ifndef OPENSSL_NO_SHA256
265 digest_length= SHA224_DIGEST_LENGTH;
266 (void) SHA224(input_ptr, input_len, digest_buf);
270 digest_length= SHA256_DIGEST_LENGTH;
271 (void) SHA256(input_ptr, input_len, digest_buf);
275 if (!args[1]->const_item())
276 push_warning_printf(current_thd,
277 Sql_condition::WARN_LEVEL_WARN,
278 ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
279 ER(ER_WRONG_PARAMETERS_TO_NATIVE_FCT),
"sha2");
288 str->
realloc((uint) digest_length*2 + 1);
291 array_to_hex((
char *) str->ptr(), digest_buf, digest_length);
294 str->length((uint) digest_length*2);
300 push_warning_printf(current_thd,
301 Sql_condition::WARN_LEVEL_WARN,
303 ER(ER_FEATURE_DISABLED),
304 "sha2",
"--with-ssl");
311 void Item_func_sha2::fix_length_and_dec()
316 #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
317 int sha_variant= args[1]->const_item() ? args[1]->val_int() : 512;
319 switch (sha_variant) {
320 #ifndef OPENSSL_NO_SHA512
322 fix_length_and_charset(SHA512_DIGEST_LENGTH * 2, default_charset());
325 fix_length_and_charset(SHA384_DIGEST_LENGTH * 2, default_charset());
328 #ifndef OPENSSL_NO_SHA256
331 fix_length_and_charset(SHA256_DIGEST_LENGTH * 2, default_charset());
334 fix_length_and_charset(SHA224_DIGEST_LENGTH * 2, default_charset());
338 push_warning_printf(current_thd,
339 Sql_condition::WARN_LEVEL_WARN,
340 ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
341 ER(ER_WRONG_PARAMETERS_TO_NATIVE_FCT),
"sha2");
350 args[0]->collation.set(
351 get_charset_by_csname(
352 args[0]->collation.collation->csname,
355 DERIVATION_COERCIBLE);
357 push_warning_printf(current_thd,
358 Sql_condition::WARN_LEVEL_WARN,
360 ER(ER_FEATURE_DISABLED),
361 "sha2",
"--with-ssl");
369 DBUG_ASSERT(fixed == 1);
371 String tmp_key_value(key_buff,
sizeof(key_buff), system_charset_info);
372 String *sptr= args[0]->val_str(str);
373 String *key= args[1]->val_str(&tmp_key_value);
378 aes_length=my_aes_get_size(sptr->length());
380 if (!str_value.alloc(aes_length))
383 if (my_aes_encrypt(sptr->ptr(),sptr->length(), (
char*) str_value.ptr(),
384 key->ptr(), key->length()) == aes_length)
387 str_value.length((uint) aes_length);
397 void Item_func_aes_encrypt::fix_length_and_dec()
399 max_length=my_aes_get_size(args[0]->max_length);
405 DBUG_ASSERT(fixed == 1);
407 String tmp_key_value(key_buff,
sizeof(key_buff), system_charset_info);
409 DBUG_ENTER(
"Item_func_aes_decrypt::val_str");
411 sptr= args[0]->val_str(str);
412 key= args[1]->val_str(&tmp_key_value);
416 if (!str_value.alloc(sptr->length()))
420 length=my_aes_decrypt(sptr->ptr(), sptr->length(),
421 (
char*) str_value.ptr(),
422 key->ptr(), key->length());
425 str_value.length((uint) length);
426 DBUG_RETURN(&str_value);
436 void Item_func_aes_decrypt::fix_length_and_dec()
438 max_length=args[0]->max_length;
443 void Item_func_to_base64::fix_length_and_dec()
445 maybe_null= args[0]->maybe_null;
446 collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
447 if (args[0]->max_length > (uint) base64_encode_max_arg_length())
450 fix_char_length_ulonglong((ulonglong) base64_encode_max_arg_length());
454 int length= base64_needed_encoded_length((
int) args[0]->max_length);
455 DBUG_ASSERT(length > 0);
456 fix_char_length_ulonglong((ulonglong) length - 1);
463 String *res= args[0]->val_str(str);
464 bool too_long=
false;
467 res->length() > (uint) base64_encode_max_arg_length() ||
469 ((uint) (length= base64_needed_encoded_length((
int) res->length())) >
470 current_thd->variables.max_allowed_packet)) ||
471 tmp_value.alloc((uint) length))
476 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
477 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
478 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
479 current_thd->variables.max_allowed_packet);
483 base64_encode(res->ptr(), (int) res->length(), (
char*) tmp_value.ptr());
484 DBUG_ASSERT(length > 0);
485 tmp_value.length((uint) length - 1);
491 void Item_func_from_base64::fix_length_and_dec()
493 if (args[0]->max_length > (uint) base64_decode_max_arg_length())
495 fix_char_length_ulonglong((ulonglong) base64_decode_max_arg_length());
499 int length= base64_needed_decoded_length((
int) args[0]->max_length);
500 fix_char_length_ulonglong((ulonglong) length);
508 String *res= args[0]->val_str_ascii(str);
509 bool too_long=
false;
514 res->length() > (uint) base64_decode_max_arg_length() ||
516 ((uint) (length= base64_needed_decoded_length((
int) res->length())) >
517 current_thd->variables.max_allowed_packet)) ||
518 tmp_value.alloc((uint) length) ||
519 (length= base64_decode(res->ptr(), (int) res->length(),
520 (
char *) tmp_value.ptr(), &end_ptr, 0)) < 0 ||
521 end_ptr < res->ptr() + res->length())
526 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
527 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
528 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
529 current_thd->variables.max_allowed_packet);
533 tmp_value.length((uint) length);
547 DBUG_ASSERT(fixed == 1);
548 String *res,*res2,*use_as_buff;
553 if (!(res=args[0]->
val_str(str)))
555 use_as_buff= &tmp_value;
557 is_const= args[0]->const_item() || !args[0]->used_tables();
558 for (i=1 ; i < arg_count ; i++)
560 if (res->length() == 0)
562 if (!(res=args[i]->
val_str(str)))
569 is_const= args[
i]->const_item() || !args[
i]->used_tables();
573 if (!(res2=args[i]->
val_str(use_as_buff)))
575 if (res2->length() == 0)
577 if (res->length()+res2->length() >
578 current_thd->variables.max_allowed_packet)
580 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
581 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
582 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
583 current_thd->variables.max_allowed_packet);
586 if (!is_const && res->alloced_length() >= res->length()+res2->length())
590 else if (str->alloced_length() >= res->length()+res2->length())
592 if (str->ptr() == res2->ptr())
593 str->replace(0,0,*res);
600 use_as_buff= &tmp_value;
602 else if (res == &tmp_value)
604 if (res->append(*res2))
607 else if (res2 == &tmp_value)
609 if (tmp_value.replace(0,0,*res))
614 else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
615 res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
623 tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
626 if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
642 uint concat_len= res->length() + res2->length();
644 if (tmp_value.alloced_length() < concat_len)
646 if (tmp_value.alloced_length() == 0)
648 if (tmp_value.alloc(concat_len))
653 uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
655 if (tmp_value.
realloc(new_len))
660 if (tmp_value.copy(*res) || tmp_value.append(*res2))
669 res->set_charset(collation.collation);
678 void Item_func_concat::fix_length_and_dec()
680 ulonglong char_length= 0;
682 if (agg_arg_charsets_for_string_result(collation, args, arg_count))
685 for (uint
i=0 ;
i < arg_count ;
i++)
686 char_length+= args[
i]->max_char_length();
688 fix_char_length_ulonglong(char_length);
704 DBUG_ASSERT(fixed == 1);
705 #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
706 uint
code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
708 struct st_des_keyblock keyblock;
709 struct st_des_keyschedule keyschedule;
710 const char *append_str=
"********";
711 uint key_number, res_length, tail;
712 String *res= args[0]->val_str(str);
714 if ((null_value= args[0]->null_value))
716 if ((res_length=res->length()) == 0)
722 keyschedule= des_keyschedule[key_number=des_default_key];
725 else if (args[1]->result_type() == INT_RESULT)
727 key_number= (uint) args[1]->val_int();
731 keyschedule= des_keyschedule[key_number];
736 String *keystr=args[1]->val_str(&tmp_value);
742 memset(&ivec, 0,
sizeof(ivec));
743 EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
744 (uchar*) keystr->ptr(), (int) keystr->length(),
745 1, (uchar*) &keyblock,ivec);
746 DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
747 DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
748 DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
760 tail= 8 - (res_length % 8);
764 tmp_arg.append(res->ptr(), res->length());
765 code= ER_OUT_OF_RESOURCES;
766 if (tmp_arg.append(append_str, tail) || tmp_value.alloc(res_length+1))
768 tmp_arg[res_length-1]=tail;
769 tmp_value.
realloc(res_length+1);
770 tmp_value.length(res_length+1);
771 tmp_value.set_charset(&my_charset_bin);
772 tmp_value[0]=(char) (128 | key_number);
774 memset(&ivec, 0,
sizeof(ivec));
775 DES_ede3_cbc_encrypt((
const uchar*) (tmp_arg.ptr()),
776 (uchar*) (tmp_value.ptr()+1),
785 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
789 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
790 ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED),
791 "des_encrypt",
"--with-ssl");
800 DBUG_ASSERT(fixed == 1);
801 #if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
802 uint
code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
804 struct st_des_keyblock keyblock;
805 struct st_des_keyschedule keyschedule;
806 String *res= args[0]->val_str(str);
809 if ((null_value= args[0]->null_value))
811 length= res->length();
812 if (length < 9 || (length % 8) != 1 || !((*res)[0] & 128))
817 uint key_number=(uint) (*res)[0] & 127;
819 if (!(current_thd->security_ctx->master_access & SUPER_ACL) ||
824 keyschedule= des_keyschedule[key_number];
830 String *keystr=args[1]->val_str(&tmp_value);
834 memset(&ivec, 0,
sizeof(ivec));
835 EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
836 (uchar*) keystr->ptr(),(int) keystr->length(),
837 1,(uchar*) &keyblock,ivec);
839 DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
840 DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
841 DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
843 code= ER_OUT_OF_RESOURCES;
844 if (tmp_value.alloc(length-1))
847 memset(&ivec, 0,
sizeof(ivec));
848 DES_ede3_cbc_encrypt((
const uchar*) res->ptr()+1,
849 (uchar*) (tmp_value.ptr()),
856 if ((tail=(uint) (uchar) tmp_value[length-2]) > 8)
858 tmp_value.length(length-1-tail);
859 tmp_value.set_charset(&my_charset_bin);
863 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
868 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
869 ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED),
870 "des_decrypt",
"--with-ssl");
884 DBUG_ASSERT(fixed == 1);
885 char tmp_str_buff[10];
886 String tmp_sep_str(tmp_str_buff,
sizeof(tmp_str_buff),default_charset_info),
887 *sep_str, *res, *res2,*use_as_buff;
892 if (!(sep_str= args[0]->
val_str(&tmp_sep_str)))
895 use_as_buff= &tmp_value;
901 for (i=1; i < arg_count; i++)
902 if ((res= args[i]->
val_str(str)))
904 is_const= args[
i]->const_item() || !args[
i]->used_tables();
911 for (i++; i < arg_count ; i++)
913 if (!(res2= args[i]->
val_str(use_as_buff)))
916 if (res->length() + sep_str->length() + res2->length() >
917 current_thd->variables.max_allowed_packet)
919 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
920 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
921 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
922 current_thd->variables.max_allowed_packet);
925 if (!is_const && res->alloced_length() >=
926 res->length() + sep_str->length() + res2->length())
928 res->append(*sep_str);
931 else if (str->alloced_length() >=
932 res->length() + sep_str->length() + res2->length())
935 if (str->ptr() == res2->ptr())
937 str->replace(0,0,*sep_str);
938 str->replace(0,0,*res);
943 str->append(*sep_str);
947 use_as_buff= &tmp_value;
949 else if (res == &tmp_value)
951 if (res->append(*sep_str) || res->append(*res2))
954 else if (res2 == &tmp_value)
956 if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
961 else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
962 res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
970 tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
973 if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
975 tmp_value.replace(res->length(),0, *sep_str))
990 uint concat_len= res->length() + sep_str->length() + res2->length();
992 if (tmp_value.alloced_length() < concat_len)
994 if (tmp_value.alloced_length() == 0)
996 if (tmp_value.alloc(concat_len))
1001 uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
1003 if (tmp_value.
realloc(new_len))
1008 if (tmp_value.copy(*res) ||
1009 tmp_value.append(*sep_str) ||
1010 tmp_value.append(*res2))
1016 res->set_charset(collation.collation);
1025 void Item_func_concat_ws::fix_length_and_dec()
1027 ulonglong char_length;
1029 if (agg_arg_charsets_for_string_result(collation, args, arg_count))
1037 char_length= (ulonglong) args[0]->max_char_length() * (arg_count - 2);
1038 for (uint
i=1 ;
i < arg_count ;
i++)
1039 char_length+= args[
i]->max_char_length();
1041 fix_char_length_ulonglong(char_length);
1047 DBUG_ASSERT(fixed == 1);
1048 String *res = args[0]->val_str(str);
1049 char *ptr, *end, *tmp;
1051 if ((null_value=args[0]->null_value))
1056 if (tmp_value.alloced_length() < res->length() &&
1057 tmp_value.
realloc(res->length()))
1062 tmp_value.length(res->length());
1063 tmp_value.set_charset(res->charset());
1064 ptr= (
char *) res->ptr();
1065 end= ptr + res->length();
1066 tmp= (
char *) tmp_value.ptr() + tmp_value.length();
1068 if (use_mb(res->charset()))
1073 if ((l= my_ismbchar(res->charset(),ptr,end)))
1093 void Item_func_reverse::fix_length_and_dec()
1095 agg_arg_charsets_for_string_result(collation, args, 1);
1096 DBUG_ASSERT(collation.collation != NULL);
1097 fix_char_length(args[0]->max_char_length());
1111 DBUG_ASSERT(fixed == 1);
1114 uint from_length,to_length;
1117 const char *ptr,*end,*strend,*search,*search_end;
1123 res=args[0]->val_str(str);
1124 if (args[0]->null_value)
1126 res2=args[1]->val_str(&tmp_value);
1127 if (args[1]->null_value)
1130 res->set_charset(collation.collation);
1133 binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
1136 if (res2->length() == 0)
1139 if ((offset=res->strstr(*res2)) < 0)
1143 if (binary_cmp && (offset=res->strstr(*res2)) < 0)
1146 if (!(res3=args[2]->
val_str(&tmp_value2)))
1148 from_length= res2->length();
1149 to_length= res3->length();
1155 search_end=search+from_length;
1157 DBUG_ASSERT(res->ptr() || !
offset);
1159 strend=res->ptr()+res->length();
1165 end= strend ? strend - from_length + 1 : NULL;
1168 if (*ptr == *search)
1170 register char *
i,*j;
1171 i=(
char*) ptr+1; j=(
char*) search+1;
1172 while (j != search_end)
1173 if (*i++ != *j++)
goto skip;
1174 offset= (int) (ptr-res->ptr());
1175 if (res->length()-from_length + to_length >
1176 current_thd->variables.max_allowed_packet)
1178 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
1179 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1180 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1182 current_thd->variables.max_allowed_packet);
1189 res=copy_if_not_alloced(str,res,res->length()+to_length);
1191 res->replace((uint) offset,from_length,*res3);
1192 offset+=(int) to_length;
1196 if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
1204 if (res->length()-from_length + to_length >
1205 current_thd->variables.max_allowed_packet)
1207 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
1208 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1209 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
1210 current_thd->variables.max_allowed_packet);
1216 res=copy_if_not_alloced(str,res,res->length()+to_length);
1218 res->replace((uint) offset,from_length,*res3);
1219 offset+=(int) to_length;
1221 while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
1230 void Item_func_replace::fix_length_and_dec()
1232 ulonglong char_length= (ulonglong) args[0]->max_char_length();
1233 int diff=(int) (args[2]->max_char_length() - args[1]->max_char_length());
1234 if (diff > 0 && args[1]->max_char_length())
1236 ulonglong max_substrs= char_length / args[1]->max_char_length();
1237 char_length+= max_substrs * (uint) diff;
1240 if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 3))
1242 fix_char_length_ulonglong(char_length);
1248 DBUG_ASSERT(fixed == 1);
1250 longlong start, length;
1253 res=args[0]->val_str(str);
1254 res2=args[3]->val_str(&tmp_value);
1255 start= args[1]->val_int() - 1;
1256 length= args[2]->val_int();
1258 if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
1259 args[3]->null_value)
1262 if ((start < 0) || (start > res->length()))
1264 if ((length < 0) || (length > res->length()))
1265 length= res->length();
1275 if (collation.collation == &my_charset_bin)
1277 res->set_charset(&my_charset_bin);
1278 res2->set_charset(&my_charset_bin);
1282 start= res->charpos((
int) start);
1283 length= res->charpos((
int) length, (uint32) start);
1286 if (start > res->length())
1288 if (length > res->length() - start)
1289 length= res->length() - start;
1291 if ((ulonglong) (res->length() - length + res2->length()) >
1292 (ulonglong) current_thd->variables.max_allowed_packet)
1294 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
1295 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1296 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1297 func_name(), current_thd->variables.max_allowed_packet);
1300 res=copy_if_not_alloced(str,res,res->length());
1301 res->replace((uint32) start,(uint32) length,*res2);
1309 void Item_func_insert::fix_length_and_dec()
1311 ulonglong char_length;
1314 if (agg_arg_charsets_for_string_result(collation, args, 2, 3))
1316 char_length= ((ulonglong) args[0]->max_char_length() +
1317 (ulonglong) args[3]->max_char_length());
1318 fix_char_length_ulonglong(char_length);
1324 DBUG_ASSERT(fixed == 1);
1326 if (!(res=args[0]->val_str(str)))
1335 res= copy_if_not_alloced(str,res,res->length());
1336 len= converter(collation.collation, (
char*) res->ptr(), res->length(),
1337 (
char*) res->ptr(), res->length());
1338 DBUG_ASSERT(len <= res->length());
1343 uint len= res->length() * multiply;
1344 tmp_value.alloc(len);
1345 tmp_value.set_charset(collation.collation);
1346 len= converter(collation.collation, (
char*) res->ptr(), res->length(),
1347 (
char*) tmp_value.ptr(), len);
1348 tmp_value.length(len);
1355 void Item_func_lcase::fix_length_and_dec()
1357 agg_arg_charsets_for_string_result(collation, args, 1);
1358 DBUG_ASSERT(collation.collation != NULL);
1359 multiply= collation.collation->casedn_multiply;
1360 converter= collation.collation->cset->casedn;
1361 fix_char_length_ulonglong((ulonglong) args[0]->max_char_length() * multiply);
1364 void Item_func_ucase::fix_length_and_dec()
1366 agg_arg_charsets_for_string_result(collation, args, 1);
1367 DBUG_ASSERT(collation.collation != NULL);
1368 multiply= collation.collation->caseup_multiply;
1369 converter= collation.collation->cset->caseup;
1370 fix_char_length_ulonglong((ulonglong) args[0]->max_char_length() * multiply);
1376 DBUG_ASSERT(fixed == 1);
1377 String *res= args[0]->val_str(str);
1380 longlong length= args[1]->val_int();
1383 if ((null_value=(args[0]->null_value || args[1]->null_value)))
1387 if ((length <= 0) && (!args[1]->unsigned_flag))
1389 if ((res->length() <= (ulonglong) length) ||
1390 (res->length() <= (char_pos= res->charpos((
int) length))))
1393 tmp_value.set(*res, 0, char_pos);
1398 void Item_str_func::left_right_max_length()
1400 uint32 char_length= args[0]->max_char_length();
1401 if (args[1]->const_item())
1403 int length= (int) args[1]->val_int();
1404 if (args[1]->null_value)
1410 set_if_smaller(char_length, (uint) length);
1414 fix_char_length(char_length);
1418 void Item_func_left::fix_length_and_dec()
1420 agg_arg_charsets_for_string_result(collation, args, 1);
1421 DBUG_ASSERT(collation.collation != NULL);
1422 left_right_max_length();
1428 DBUG_ASSERT(fixed == 1);
1429 String *res= args[0]->val_str(str);
1431 longlong length= args[1]->val_int();
1433 if ((null_value=(args[0]->null_value || args[1]->null_value)))
1437 if ((length <= 0) && (!args[1]->unsigned_flag))
1440 if (res->length() <= (ulonglong) length)
1443 uint start=res->numchars();
1444 if (start <= (uint) length)
1446 start=res->charpos(start - (uint) length);
1447 tmp_value.set(*res,start,res->length()-start);
1452 void Item_func_right::fix_length_and_dec()
1454 agg_arg_charsets_for_string_result(collation, args, 1);
1455 DBUG_ASSERT(collation.collation != NULL);
1456 left_right_max_length();
1462 DBUG_ASSERT(fixed == 1);
1463 String *res = args[0]->val_str(str);
1465 longlong start= args[1]->val_int();
1468 longlong length= arg_count == 3 ? args[2]->val_int() : INT_MAX32;
1469 longlong tmp_length;
1471 if ((null_value=(args[0]->null_value || args[1]->null_value ||
1472 (arg_count == 3 && args[2]->null_value))))
1476 if ((arg_count == 3) && (length <= 0) &&
1477 (length == 0 || !args[2]->unsigned_flag))
1482 if ((length <= 0) || (length > INT_MAX32))
1487 if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
1488 (args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
1491 start= ((start < 0) ? res->numchars() + start : start - 1);
1492 start= res->charpos((
int) start);
1493 if ((start < 0) || ((uint) start + 1 > res->length()))
1496 length= res->charpos((
int) length, (uint32) start);
1497 tmp_length= res->length() - start;
1498 length= min(length, tmp_length);
1500 if (!start && (longlong) res->length() == length)
1502 tmp_value.set(*res, (uint32) start, (uint32) length);
1507 void Item_func_substr::fix_length_and_dec()
1509 max_length=args[0]->max_length;
1511 agg_arg_charsets_for_string_result(collation, args, 1);
1512 DBUG_ASSERT(collation.collation != NULL);
1513 if (args[1]->const_item())
1515 int32 start= (int32) args[1]->val_int();
1516 if (args[1]->null_value)
1519 max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
1521 max_length-= min((uint)(start - 1), max_length);
1523 if (arg_count == 3 && args[2]->const_item())
1525 int32 length= (int32) args[2]->val_int();
1526 if (args[2]->null_value)
1531 set_if_smaller(max_length,(uint) length);
1535 max_length*= collation.collation->mbmaxlen;
1539 void Item_func_substr_index::fix_length_and_dec()
1541 if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 2))
1543 fix_char_length(args[0]->max_char_length());
1549 DBUG_ASSERT(fixed == 1);
1550 char buff[MAX_FIELD_WIDTH];
1551 String tmp(buff,
sizeof(buff),system_charset_info);
1552 String *res= args[0]->val_str(str);
1553 String *delimiter= args[1]->val_str(&tmp);
1554 int32 count= (int32) args[2]->val_int();
1557 if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
1563 uint delimiter_length= delimiter->length();
1564 if (!res->length() || !delimiter_length || !count)
1567 res->set_charset(collation.collation);
1570 if (use_mb(res->charset()))
1572 const char *ptr= res->ptr();
1573 const char *strend= ptr+res->length();
1574 const char *end= strend-delimiter_length+1;
1575 const char *search= delimiter->ptr();
1576 const char *search_end= search+delimiter_length;
1577 int32
n=0,c=count,pass;
1579 for (pass=(count>0);pass<2;++pass)
1583 if (*ptr == *search)
1585 register char *
i,*j;
1586 i=(
char*) ptr+1; j=(
char*) search+1;
1587 while (j != search_end)
1588 if (*i++ != *j++)
goto skip;
1590 else if (!--c)
break;
1591 ptr+= delimiter_length;
1595 if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
1601 if (c<=0)
return res;
1609 tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
1613 ptr+= delimiter_length;
1614 tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
1624 for (offset=0; ; offset+= delimiter_length)
1626 if ((offset= res->strstr(*delimiter, offset)) < 0)
1630 tmp_value.set(*res,0,offset);
1640 for (offset=res->length();
offset; )
1647 if ((offset= res->strrstr(*delimiter, offset)) < 0)
1655 offset+= delimiter_length;
1656 tmp_value.set(*res,offset,res->length()-
offset);
1664 return (&tmp_value);
1677 DBUG_ASSERT(fixed == 1);
1678 char buff[MAX_FIELD_WIDTH], *ptr, *end;
1679 String tmp(buff,
sizeof(buff),system_charset_info);
1680 String *res, *remove_str;
1682 LINT_INIT(remove_length);
1684 res= args[0]->val_str(str);
1685 if ((null_value=args[0]->null_value))
1687 remove_str= &
remove;
1690 remove_str= args[1]->val_str(&tmp);
1691 if ((null_value= args[1]->null_value))
1695 if ((remove_length= remove_str->length()) == 0 ||
1696 remove_length > res->length())
1699 ptr= (
char*) res->ptr();
1700 end= ptr+res->length();
1701 if (remove_length == 1)
1703 char chr=(*remove_str)[0];
1704 while (ptr != end && *ptr == chr)
1709 const char *r_ptr=remove_str->ptr();
1711 while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
1715 if (ptr == res->ptr())
1717 tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1724 DBUG_ASSERT(fixed == 1);
1725 char buff[MAX_FIELD_WIDTH], *ptr, *end;
1726 String tmp(buff,
sizeof(buff), system_charset_info);
1727 String *res, *remove_str;
1729 LINT_INIT(remove_length);
1731 res= args[0]->val_str(str);
1732 if ((null_value=args[0]->null_value))
1734 remove_str= &
remove;
1737 remove_str= args[1]->val_str(&tmp);
1738 if ((null_value= args[1]->null_value))
1742 if ((remove_length= remove_str->length()) == 0 ||
1743 remove_length > res->length())
1746 ptr= (
char*) res->ptr();
1747 end= ptr+res->length();
1752 if (remove_length == 1)
1754 char chr=(*remove_str)[0];
1756 if (use_mb(res->charset()))
1760 if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
1766 while (ptr != end && end[-1] == chr)
1771 const char *r_ptr=remove_str->ptr();
1773 if (use_mb(res->charset()))
1776 while (ptr + remove_length < end)
1778 if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1781 if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1791 while (ptr + remove_length <= end &&
1792 !memcmp(end-remove_length, r_ptr, remove_length))
1796 if (end == res->ptr()+res->length())
1798 tmp_value.set(*res,0,(uint) (end-res->ptr()));
1805 DBUG_ASSERT(fixed == 1);
1806 char buff[MAX_FIELD_WIDTH], *ptr, *end;
1808 String tmp(buff,
sizeof(buff), system_charset_info);
1809 String *res, *remove_str;
1811 LINT_INIT(remove_length);
1813 res= args[0]->val_str(str);
1814 if ((null_value=args[0]->null_value))
1816 remove_str= &
remove;
1819 remove_str= args[1]->val_str(&tmp);
1820 if ((null_value= args[1]->null_value))
1824 if ((remove_length= remove_str->length()) == 0 ||
1825 remove_length > res->length())
1828 ptr= (
char*) res->ptr();
1829 end= ptr+res->length();
1830 r_ptr= remove_str->ptr();
1831 while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
1834 if (use_mb(res->charset()))
1839 while (ptr + remove_length < end)
1841 if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1844 if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1855 while (ptr + remove_length <= end &&
1856 !memcmp(end-remove_length,r_ptr,remove_length))
1859 if (ptr == res->ptr() && end == ptr+res->length())
1861 tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1865 void Item_func_trim::fix_length_and_dec()
1869 agg_arg_charsets_for_string_result(collation, args, 1);
1870 DBUG_ASSERT(collation.collation != NULL);
1871 remove.set_charset(collation.collation);
1872 remove.set_ascii(
" ",1);
1878 if (agg_arg_charsets_for_string_result_with_comparison(collation,
1882 fix_char_length(args[0]->max_char_length());
1892 str->append(Item_func_trim::func_name());
1894 str->append(mode_name());
1896 args[1]->
print(str, query_type);
1897 str->append(STRING_WITH_LEN(
" from "));
1898 args[0]->
print(str, query_type);
1915 static int calculate_password(
String *str,
char *buffer)
1918 if (str->length() == 0)
1922 THD *thd= current_thd;
1923 int old_passwords= 0;
1925 old_passwords= thd->variables.old_passwords;
1927 #if defined(HAVE_OPENSSL)
1928 if (old_passwords == 2)
1930 my_make_scrambled_password(buffer, str->ptr(),
1932 buffer_len= (int) strlen(buffer) + 1;
1936 if (old_passwords == 0)
1938 my_make_scrambled_password_sha1(buffer, str->ptr(),
1940 buffer_len= SCRAMBLED_PASSWORD_CHAR_LENGTH;
1943 if (old_passwords == 1)
1945 my_make_scrambled_password_323(buffer, str->ptr(),
1947 buffer_len= SCRAMBLED_PASSWORD_CHAR_LENGTH_323;
1953 void Item_func_password::fix_length_and_dec()
1957 if (args[0]->const_item())
1960 String *res= args[0]->val_str(&str);
1961 if (!args[0]->null_value)
1963 m_hashed_password_buffer_len=
1964 calculate_password(res, m_hashed_password_buffer);
1965 fix_length_and_charset(m_hashed_password_buffer_len, default_charset());
1966 m_recalculate_password=
false;
1971 m_recalculate_password=
true;
1972 fix_length_and_charset(CRYPT_MAX_PASSWORD_SIZE, default_charset());
1977 DBUG_ASSERT(fixed == 1);
1979 String *res= args[0]->val_str(str);
1981 if (args[0]->null_value)
1985 check_password_policy(res);
1988 if (args[0]->null_value)
1991 if (m_recalculate_password)
1992 m_hashed_password_buffer_len= calculate_password(res,
1993 m_hashed_password_buffer);
1995 if (m_hashed_password_buffer_len == 0)
1998 str->set(m_hashed_password_buffer, m_hashed_password_buffer_len,
2004 char *Item_func_password::
2005 create_password_hash_buffer(THD *thd,
const char *password,
size_t pass_len)
2007 String *password_str=
new (thd->mem_root)
String(password, thd->variables.
2008 character_set_client);
2009 check_password_policy(password_str);
2012 if (thd->variables.old_passwords == 0)
2015 buff= (
char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH + 1);
2016 my_make_scrambled_password_sha1(buff, password, pass_len);
2018 #if defined(HAVE_OPENSSL)
2022 buff= (
char *) thd->alloc(CRYPT_MAX_PASSWORD_SIZE + 1);
2023 my_make_scrambled_password(buff, password, pass_len);
2031 String *Item_func_old_password::val_str_ascii(
String *str)
2035 DBUG_ASSERT(fixed == 1);
2037 res= args[0]->val_str(str);
2039 if ((null_value= args[0]->null_value))
2043 check_password_policy(res);
2048 if (res->length() == 0)
2051 my_make_scrambled_password_323(tmp_value, res->ptr(), res->length());
2052 str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, &my_charset_latin1);
2056 char *Item_func_old_password::alloc(THD *thd,
const char *password,
2059 char *buff= (
char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
2062 String *password_str=
new (thd->mem_root)
String(password, thd->variables.
2063 character_set_client);
2064 check_password_policy(password_str);
2065 my_make_scrambled_password_323(buff, password, pass_len);
2071 #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
2075 DBUG_ASSERT(fixed == 1);
2076 String *res =args[0]->val_str(str);
2079 char salt[3],*salt_ptr;
2080 if ((null_value=args[0]->null_value))
2082 if (res->length() == 0)
2086 time_t timestamp=current_thd->query_start();
2087 salt[0] = bin_to_ascii( (ulong) timestamp & 0x3f);
2088 salt[1] = bin_to_ascii(( (ulong) timestamp >> 5) & 0x3f);
2094 String *salt_str=args[1]->val_str(&tmp_value);
2095 if ((null_value= (args[1]->null_value || salt_str->length() < 2)))
2097 salt_ptr= salt_str->c_ptr_safe();
2100 char *tmp= crypt(res->c_ptr_safe(),salt_ptr);
2107 str->set(tmp, (uint) strlen(tmp), &my_charset_bin);
2117 bool Item_func_encode::seed()
2121 String *key, tmp(buf,
sizeof(buf), system_charset_info);
2123 if (!(key= args[1]->val_str(&tmp)))
2126 hash_password(rand_nr, key->ptr(), key->length());
2127 sql_crypt.init(rand_nr);
2132 void Item_func_encode::fix_length_and_dec()
2134 max_length=args[0]->max_length;
2135 maybe_null=args[0]->maybe_null || args[1]->maybe_null;
2136 collation.set(&my_charset_bin);
2138 seeded= args[1]->const_item() &&
2139 (args[1]->result_type() == STRING_RESULT) && !seed();
2145 DBUG_ASSERT(fixed == 1);
2147 if (!(res=args[0]->val_str(str)))
2153 if (!seeded && seed())
2160 res= copy_if_not_alloced(str, res, res->length());
2161 crypto_transform(res);
2167 void Item_func_encode::crypto_transform(
String *res)
2169 sql_crypt.encode((
char*) res->ptr(),res->length());
2170 res->set_charset(&my_charset_bin);
2173 void Item_func_decode::crypto_transform(
String *res)
2175 sql_crypt.decode((
char*) res->ptr(),res->length());
2183 String tmp, cstr, *ostr= val_str(&tmp);
2186 Item *null_item=
new Item_null(fully_qualified_func_name());
2187 null_item->collation.set (tocs);
2190 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
2193 cstr.ptr(), cstr.length(),
2195 collation.derivation)))
2199 conv->str_value.copy();
2200 conv->str_value.mark_as_const();
2207 DBUG_ASSERT(fixed == 1);
2208 THD *thd= current_thd;
2209 if (thd->db == NULL)
2215 str->copy(thd->db, thd->db_length, system_charset_info);
2227 DBUG_ASSERT(fixed == 1);
2233 size_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
2235 if (str_value.alloc((uint) res_length))
2241 res_length=cs->cset->snprintf(cs, (
char*)str_value.ptr(), (uint) res_length,
2242 "%s@%s", user, host);
2243 str_value.length((uint) res_length);
2244 str_value.mark_as_const();
2250 bool Item_func_user::fix_fields(THD *thd,
Item **ref)
2252 return (Item_func_sysconst::fix_fields(thd, ref) ||
2253 init(thd->main_security_ctx.user,
2254 thd->main_security_ctx.host_or_ip));
2258 bool Item_func_current_user::fix_fields(THD *thd,
Item **ref)
2260 if (Item_func_sysconst::fix_fields(thd, ref))
2263 Security_context *ctx=
2264 #ifndef NO_EMBEDDED_ACCESS_CHECKS
2265 (context->security_ctx
2266 ? context->security_ctx : thd->security_ctx);
2270 return init(ctx->priv_user, ctx->priv_host);
2274 void Item_func_soundex::fix_length_and_dec()
2276 uint32 char_length= args[0]->max_char_length();
2277 agg_arg_charsets_for_string_result(collation, args, 1);
2278 DBUG_ASSERT(collation.collation != NULL);
2279 set_if_bigger(char_length, 4);
2280 fix_char_length(char_length);
2281 tmp_value.set_charset(collation.collation);
2291 static int soundex_toupper(
int ch)
2293 return (ch >=
'a' && ch <=
'z') ? ch -
'a' +
'A' : ch;
2297 static char get_scode(
int wc)
2299 int ch= soundex_toupper(wc);
2300 if (ch < 'A' || ch >
'Z')
2305 return(soundex_map[ch-
'A']);
2309 static bool my_uni_isalpha(
int wc)
2317 return (wc >=
'a' && wc <=
'z') ||
2318 (wc >=
'A' && wc <=
'Z') ||
2325 DBUG_ASSERT(fixed == 1);
2326 String *res =args[0]->val_str(str);
2333 if ((null_value= args[0]->null_value))
2336 if (tmp_value.alloc(max(res->length(), 4 * cs->mbminlen)))
2338 char *
to= (
char *) tmp_value.ptr();
2339 char *to_end= to + tmp_value.alloced_length();
2340 char *from= (
char *) res->ptr(), *end= from + res->length();
2344 if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
2347 if (rc == 1 && cs->ctype)
2350 if (my_isalpha(cs, *from))
2352 last_ch= get_scode(*from);
2353 *to++= soundex_toupper(*from++);
2361 if (my_uni_isalpha(wc))
2364 wc= soundex_toupper(wc);
2365 last_ch= get_scode(wc);
2366 if ((rc= cs->cset->wc_mb(cs, wc, (uchar*) to, (uchar*) to_end)) <= 0)
2382 for (nchars= 1 ; ; )
2384 if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
2387 if (rc == 1 && cs->ctype)
2389 if (!my_isalpha(cs, *from++))
2395 if (!my_uni_isalpha(wc))
2400 if ((ch !=
'0') && (ch != last_ch))
2403 if ((rc= cs->cset->wc_mb(cs, (my_wc_t) ch,
2404 (uchar*) to, (uchar*) to_end)) <= 0)
2419 uint nbytes= (4 - nchars) * cs->mbminlen;
2420 cs->cset->fill(cs, to, nbytes,
'0');
2424 tmp_value.length((uint) (to-tmp_value.ptr()));
2440 DBUG_ASSERT(arg_count == 3);
2441 String tmp, *locale_name= args[2]->val_str_ascii(&tmp);
2444 !(lc= my_locale_by_name(locale_name->c_ptr_safe())))
2446 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
2448 ER(ER_UNKNOWN_LOCALE),
2449 locale_name ? locale_name->c_ptr_safe() :
"NULL");
2450 lc= &my_locale_en_US;
2455 void Item_func_format::fix_length_and_dec()
2457 uint32 char_length= args[0]->max_char_length();
2458 uint32 max_sep_count= (char_length / 3) + (decimals ? 1 : 0) + 1;
2459 collation.set(default_charset());
2460 fix_char_length(char_length + max_sep_count + decimals);
2462 locale= args[2]->basic_const_item() ? get_locale(args[2]) : NULL;
2464 locale= &my_locale_en_US;
2482 DBUG_ASSERT(fixed == 1);
2484 dec= (int) args[1]->val_int();
2485 if (args[1]->null_value)
2491 lc= locale ? locale : get_locale(args[2]);
2493 dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
2494 dec_length= dec ? dec+1 : 0;
2497 if (args[0]->result_type() == DECIMAL_RESULT ||
2498 args[0]->result_type() == INT_RESULT)
2501 res= args[0]->val_decimal(&dec_val);
2502 if ((null_value=args[0]->null_value))
2504 my_decimal_round(E_DEC_FATAL_ERROR, res, dec,
false, &rnd_dec);
2505 my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
2506 str_length= str->length();
2510 double nr= args[0]->val_real();
2511 if ((null_value=args[0]->null_value))
2513 nr= my_double_round(nr, (longlong) dec, FALSE, FALSE);
2514 str->set_real(nr, dec, &my_charset_numeric);
2515 if (isnan(nr) || my_isinf(nr))
2517 str_length=str->length();
2520 if (lc->grouping[0] > 0 &&
2521 str_length >= dec_length + 1 + lc->grouping[0])
2524 char buf[2 * FLOATING_POINT_BUFFER];
2526 const char *grouping= lc->grouping;
2527 char sign_length= *str->ptr() ==
'-' ? 1 : 0;
2528 const char *src= str->ptr() + str_length - dec_length - 1;
2529 const char *src_begin= str->ptr() + sign_length;
2530 char *dst= buf +
sizeof(
buf);
2536 *dst= lc->decimal_point;
2537 memcpy(dst + 1, src + 2, dec);
2541 for (count= *grouping; src >= src_begin; count--)
2550 *--dst= lc->thousand_sep;
2555 DBUG_ASSERT(dst > buf);
2560 *--dst= *str->ptr();
2563 str->copy(dst, buf +
sizeof(buf) - dst, &my_charset_latin1);
2565 else if (dec_length && lc->decimal_point !=
'.')
2571 DBUG_ASSERT(dec_length <= str_length);
2572 ((
char*) str->ptr())[str_length - dec_length]= lc->decimal_point;
2580 str->append(STRING_WITH_LEN(
"format("));
2581 args[0]->
print(str, query_type);
2583 args[1]->
print(str, query_type);
2587 args[2]->
print(str,query_type);
2592 void Item_func_elt::fix_length_and_dec()
2594 uint32 char_length= 0;
2597 if (agg_arg_charsets_for_string_result(collation, args + 1, arg_count - 1))
2600 for (uint i= 1 ; i < arg_count ; i++)
2602 set_if_bigger(char_length, args[i]->max_char_length());
2603 set_if_bigger(decimals,args[i]->decimals);
2605 fix_char_length(char_length);
2610 double Item_func_elt::val_real()
2612 DBUG_ASSERT(fixed == 1);
2615 if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
2617 double result= args[tmp]->val_real();
2618 null_value= args[tmp]->null_value;
2623 longlong Item_func_elt::val_int()
2625 DBUG_ASSERT(fixed == 1);
2628 if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
2631 longlong result= args[tmp]->val_int();
2632 null_value= args[tmp]->null_value;
2639 DBUG_ASSERT(fixed == 1);
2642 if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
2645 String *result= args[tmp]->val_str(str);
2647 result->set_charset(collation.collation);
2648 null_value= args[tmp]->null_value;
2657 item->split_sum_func2(thd, ref_pointer_array, fields, &item, TRUE);
2662 void Item_func_make_set::fix_length_and_dec()
2664 uint32 char_length= arg_count - 1;
2666 if (agg_arg_charsets_for_string_result(collation, args, arg_count))
2669 for (uint i=0 ; i < arg_count ; i++)
2670 char_length+= args[i]->max_char_length();
2671 fix_char_length(char_length);
2675 with_sum_func= with_sum_func || item->with_sum_func;
2679 void Item_func_make_set::update_used_tables()
2681 Item_func::update_used_tables();
2682 item->update_used_tables();
2685 with_subselect= item->has_subquery();
2686 with_stored_program= item->has_stored_program();
2692 DBUG_ASSERT(fixed == 1);
2698 bits=item->val_int();
2699 if ((null_value=item->null_value))
2703 bits &= ((ulonglong) 1 << arg_count)-1;
2705 for (; bits; bits >>= 1, ptr++)
2709 String *res= (*ptr)->val_str(str);
2719 if (tmp_str.copy(*res))
2726 if (result != &tmp_str)
2728 if (tmp_str.alloc((result != NULL ? result->length() : 0) +
2729 res->length() + 1) ||
2730 tmp_str.copy(*result))
2734 if (tmp_str.append(STRING_WITH_LEN(
","), &my_charset_bin) ||
2735 tmp_str.append(*res))
2749 DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
2761 if (item != new_item)
2762 current_thd->change_item_tree(&item, new_item);
2769 str->append(STRING_WITH_LEN(
"make_set("));
2770 item->print(str, query_type);
2774 print_args(str, 0, query_type);
2782 DBUG_ASSERT(fixed == 1);
2784 str->set_charset(collation.collation);
2785 for (uint i=0 ; i < arg_count ; i++)
2787 int32 num=(int32) args[i]->val_int();
2788 if (!args[i]->null_value)
2791 if (num & 0xFF000000L)
2793 mi_int4store(tmp, num);
2794 str->append(tmp, 4, &my_charset_bin);
2796 else if (num & 0xFF0000L)
2798 mi_int3store(tmp, num);
2799 str->append(tmp, 3, &my_charset_bin);
2801 else if (num & 0xFF00L)
2803 mi_int2store(tmp, num);
2804 str->append(tmp, 2, &my_charset_bin);
2809 str->append(tmp, 1, &my_charset_bin);
2814 return check_well_formed_result(str);
2821 if (res->alloced_length() < length)
2823 if (str->alloced_length() >= length)
2825 (void) str->copy(*res);
2826 str->length(length);
2829 if (tmp_value->alloc(length))
2831 (void) tmp_value->copy(*res);
2832 tmp_value->length(length);
2835 res->length(length);
2840 void Item_func_repeat::fix_length_and_dec()
2842 agg_arg_charsets_for_string_result(collation, args, 1);
2843 DBUG_ASSERT(collation.collation != NULL);
2844 if (args[1]->const_item())
2847 longlong count= args[1]->val_int();
2848 if (args[1]->null_value)
2853 if (count > INT_MAX32)
2856 ulonglong char_length= (ulonglong) args[0]->max_char_length() * count;
2857 fix_char_length_ulonglong(char_length);
2862 max_length= MAX_BLOB_WIDTH;
2873 DBUG_ASSERT(fixed == 1);
2874 uint length,tot_length;
2877 longlong count= args[1]->val_int();
2878 String *res= args[0]->val_str(str);
2880 if (args[0]->null_value || args[1]->null_value)
2884 if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
2889 if ((ulonglong) count > INT_MAX32)
2893 length=res->length();
2895 if (length > current_thd->variables.max_allowed_packet / (uint) count)
2897 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
2898 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2899 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2900 func_name(), current_thd->variables.max_allowed_packet);
2903 tot_length= length*(uint) count;
2904 if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
2907 to=(
char*) res->ptr()+length;
2910 memcpy(to,res->ptr(),length);
2922 void Item_func_space::fix_length_and_dec()
2924 collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
2925 if (args[0]->const_item())
2928 longlong count= args[0]->val_int();
2929 if (args[0]->null_value)
2935 if (count > INT_MAX32)
2937 fix_char_length_ulonglong(count);
2942 max_length= MAX_BLOB_WIDTH;
2950 longlong count= args[0]->val_int();
2953 if (args[0]->null_value)
2957 if (count <= 0 && (count == 0 || !args[0]->unsigned_flag))
2963 if ((ulonglong) count > INT_MAX32)
2967 tot_length= (uint) count * cs->mbminlen;
2968 if (tot_length > current_thd->variables.max_allowed_packet)
2970 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
2971 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2972 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2974 current_thd->variables.max_allowed_packet);
2978 if (str->alloc(tot_length))
2980 str->length(tot_length);
2981 str->set_charset(cs);
2982 cs->cset->fill(cs, (
char*) str->ptr(), tot_length,
' ');
2991 void Item_func_rpad::fix_length_and_dec()
2994 if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2))
2996 if (args[1]->const_item())
2998 ulonglong char_length= (ulonglong) args[1]->val_int();
2999 if (args[1]->null_value)
3001 DBUG_ASSERT(collation.collation->mbmaxlen > 0);
3004 if (char_length > INT_MAX32)
3005 char_length= INT_MAX32;
3006 fix_char_length_ulonglong(char_length);
3011 max_length= MAX_BLOB_WIDTH;
3018 DBUG_ASSERT(fixed == 1);
3019 uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
3021 const char *ptr_pad;
3023 longlong count= args[1]->val_int();
3024 longlong byte_count;
3025 String *res= args[0]->val_str(str);
3026 String *rpad= args[2]->val_str(&rpad_str);
3028 if (!res || args[1]->null_value || !rpad ||
3029 ((count < 0) && !args[1]->unsigned_flag))
3034 if ((ulonglong) count > INT_MAX32)
3044 if (collation.collation == &my_charset_bin)
3046 res->set_charset(&my_charset_bin);
3047 rpad->set_charset(&my_charset_bin);
3050 if (count <= (res_char_length= res->numchars()))
3052 res->length(res->charpos((
int) count));
3055 pad_char_length= rpad->numchars();
3057 byte_count= count * collation.collation->mbmaxlen;
3058 if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
3060 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3061 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3062 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3063 func_name(), current_thd->variables.max_allowed_packet);
3066 if (args[2]->null_value || !pad_char_length)
3068 res_byte_length= res->length();
3069 if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
3072 to= (
char*) res->ptr()+res_byte_length;
3073 ptr_pad=rpad->ptr();
3074 pad_byte_length= rpad->length();
3075 count-= res_char_length;
3076 for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
3078 memcpy(to,ptr_pad,pad_byte_length);
3079 to+= pad_byte_length;
3083 pad_byte_length= rpad->charpos((
int) count);
3084 memcpy(to,ptr_pad,(
size_t) pad_byte_length);
3085 to+= pad_byte_length;
3087 res->length((uint) (to- (
char*) res->ptr()));
3096 void Item_func_lpad::fix_length_and_dec()
3099 if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2))
3102 if (args[1]->const_item())
3104 ulonglong char_length= (ulonglong) args[1]->val_int();
3105 if (args[1]->null_value)
3107 DBUG_ASSERT(collation.collation->mbmaxlen > 0);
3110 if (char_length > INT_MAX32)
3111 char_length= INT_MAX32;
3112 fix_char_length_ulonglong(char_length);
3117 max_length= MAX_BLOB_WIDTH;
3124 DBUG_ASSERT(fixed == 1);
3125 uint32 res_char_length,pad_char_length;
3127 longlong count= args[1]->val_int();
3128 longlong byte_count;
3129 String *res= args[0]->val_str(&tmp_value);
3130 String *pad= args[2]->val_str(&lpad_str);
3132 if (!res || args[1]->null_value || !pad ||
3133 ((count < 0) && !args[1]->unsigned_flag))
3138 if ((ulonglong) count > INT_MAX32)
3149 if (collation.collation == &my_charset_bin)
3151 res->set_charset(&my_charset_bin);
3152 pad->set_charset(&my_charset_bin);
3155 res_char_length= res->numchars();
3157 if (count <= res_char_length)
3159 res->length(res->charpos((
int) count));
3163 pad_char_length= pad->numchars();
3164 byte_count= count * collation.collation->mbmaxlen;
3166 if ((ulonglong) byte_count > current_thd->variables.max_allowed_packet)
3168 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3169 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3170 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3171 func_name(), current_thd->variables.max_allowed_packet);
3175 if (args[2]->null_value || !pad_char_length ||
3176 str->alloc((uint32) byte_count))
3180 str->set_charset(collation.collation);
3181 count-= res_char_length;
3182 while (count >= pad_char_length)
3185 count-= pad_char_length;
3188 str->append(pad->ptr(), pad->charpos((
int) count), collation.collation);
3202 DBUG_ASSERT(fixed == 1);
3203 String *res= args[0]->val_str(str);
3204 char *endptr,ans[65],*ptr;
3206 int from_base= (int) args[1]->val_int();
3207 int to_base= (int) args[2]->val_int();
3210 if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
3211 abs(to_base) > 36 || abs(to_base) < 2 ||
3212 abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
3218 unsigned_flag= !(from_base < 0);
3220 if (args[0]->field_type() == MYSQL_TYPE_BIT)
3227 dec= args[0]->val_int();
3232 dec= my_strntoll(res->charset(), res->ptr(), res->length(),
3233 -from_base, &endptr, &err);
3235 dec= (longlong) my_strntoull(res->charset(), res->ptr(), res->length(),
3236 from_base, &endptr, &err);
3239 if (!(ptr= longlong2str(dec, ans, to_base)) ||
3240 str->copy(ans, (uint32) (ptr - ans), default_charset()))
3251 DBUG_ASSERT(fixed == 1);
3252 if (use_cached_value)
3253 return null_value ? 0 : &str_value;
3254 String *arg= args[0]->val_str(str);
3261 null_value= tmp_value.copy(arg->ptr(), arg->length(), arg->charset(),
3262 conv_charset, &dummy_errors);
3263 return null_value ? 0 : check_well_formed_result(&tmp_value);
3266 void Item_func_conv_charset::fix_length_and_dec()
3268 collation.set(conv_charset, DERIVATION_IMPLICIT);
3269 fix_char_length(args[0]->max_char_length());
3274 str->append(STRING_WITH_LEN(
"convert("));
3275 args[0]->
print(str, query_type);
3276 str->append(STRING_WITH_LEN(
" using "));
3277 str->append(conv_charset->csname);
3283 DBUG_ASSERT(fixed == 1);
3284 str=args[0]->val_str(str);
3285 if ((null_value=args[0]->null_value))
3287 str->set_charset(collation.collation);
3291 void Item_func_set_collation::fix_length_and_dec()
3294 const char *colname;
3295 String tmp, *str= args[1]->val_str(&tmp);
3296 colname= str->c_ptr();
3297 if (colname == binary_keyword)
3298 set_collation= get_charset_by_csname(args[0]->collation.collation->csname,
3299 MY_CS_BINSORT,MYF(0));
3302 if (!(set_collation= mysqld_collation_get_by_name(colname)))
3306 if (!set_collation ||
3307 !my_charset_same(args[0]->collation.collation,set_collation))
3309 my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
3310 colname, args[0]->collation.collation->csname);
3313 collation.set(set_collation, DERIVATION_EXPLICIT,
3314 args[0]->collation.repertoire);
3315 max_length= args[0]->max_length;
3324 if (item->type() != FUNC_ITEM)
3327 if (arg_count != item_func->arg_count ||
3328 functype() != item_func->functype())
3331 if (collation.collation != item_func_sc->collation.collation)
3333 for (uint i=0; i < arg_count ; i++)
3334 if (!args[i]->
eq(item_func_sc->args[i], binary_cmp))
3343 args[0]->
print(str, query_type);
3344 str->append(STRING_WITH_LEN(
" collate "));
3345 DBUG_ASSERT(args[1]->basic_const_item() &&
3346 args[1]->
type() == Item::STRING_ITEM);
3347 args[1]->str_value.print(str);
3353 DBUG_ASSERT(fixed == 1);
3356 const CHARSET_INFO *cs= args[0]->charset_for_protocol();
3358 str->copy(cs->csname, (uint) strlen(cs->csname),
3359 &my_charset_latin1, collation.collation, &dummy_errors);
3365 DBUG_ASSERT(fixed == 1);
3367 const CHARSET_INFO *cs= args[0]->charset_for_protocol();
3370 str->copy(cs->name, (uint) strlen(cs->name),
3371 &my_charset_latin1, collation.collation, &dummy_errors);
3376 void Item_func_weight_string::fix_length_and_dec()
3379 collation.set(&my_charset_bin, args[0]->collation.derivation);
3380 flags= my_strxfrm_flag_normalize(
flags, cs->levels_for_order);
3381 field= args[0]->type() == FIELD_ITEM && args[0]->is_temporal() ?
3388 max_length= field ? field->pack_length() :
3389 result_length ? result_length :
3390 cs->mbmaxlen * max(args[0]->max_length, nweights);
3400 uint tmp_length, frm_length;
3401 DBUG_ASSERT(fixed == 1);
3403 if (args[0]->result_type() != STRING_RESULT ||
3404 !(res= args[0]->val_str(str)))
3412 tmp_length= field ? field->pack_length() :
3413 result_length ? result_length :
3414 cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
3415 max(res->length(), nweights));
3417 if(tmp_length > current_thd->variables.max_allowed_packet)
3419 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3420 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3421 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
3422 current_thd->variables.max_allowed_packet);
3426 if (tmp_value.alloc(tmp_length))
3431 frm_length= field->pack_length();
3432 field->
make_sort_key((uchar *) tmp_value.ptr(), tmp_length);
3435 frm_length= cs->coll->strnxfrm(cs,
3436 (uchar *) tmp_value.ptr(), tmp_length,
3437 nweights ? nweights : tmp_length,
3438 (
const uchar *) res->ptr(), res->length(),
3440 tmp_value.length(frm_length);
3453 DBUG_ASSERT(fixed == 1);
3454 if (args[0]->result_type() != STRING_RESULT)
3459 if (args[0]->result_type() == REAL_RESULT ||
3460 args[0]->result_type() == DECIMAL_RESULT)
3462 double val= args[0]->val_real();
3463 if ((val <= (
double) LONGLONG_MIN) ||
3464 (val >= (
double) (ulonglong) ULONGLONG_MAX))
3467 dec= (ulonglong) (val + (val > 0 ? 0.5 : -0.5));
3470 dec= (ulonglong) args[0]->val_int();
3472 if ((null_value= args[0]->null_value))
3475 if (!(ptr= longlong2str(dec, ans, 16)) ||
3476 str->copy(ans,(uint32) (ptr - ans),
3477 &my_charset_numeric))
3483 res= args[0]->val_str(str);
3484 if (!res || tmp_value.alloc(res->length()*2+1))
3490 tmp_value.length(res->length()*2);
3491 tmp_value.set_charset(&my_charset_latin1);
3493 octet2hex((
char*) tmp_value.ptr(), res->ptr(), res->length());
3501 const char *from, *end;
3505 DBUG_ASSERT(fixed == 1);
3507 res= args[0]->val_str(str);
3508 if (!res || tmp_value.alloc(length= (1+res->length())/2))
3516 tmp_value.length(length);
3517 to= (
char*) tmp_value.ptr();
3518 if (res->length() % 2)
3521 *to++= hex_char= hexchar_to_int(*from++);
3522 if ((null_value= (hex_char == -1)))
3525 for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
3528 *to= (hex_char= hexchar_to_int(from[0])) << 4;
3529 if ((null_value= (hex_char == -1)))
3531 *to|= hex_char= hexchar_to_int(from[1]);
3532 if ((null_value= (hex_char == -1)))
3542 DBUG_ASSERT(fixed == 1);
3543 longlong nbytes= args[1]->val_int();
3544 String *res= args[0]->val_str(str);
3545 size_t min_len, max_len;
3548 if (!res || args[0]->null_value || args[1]->null_value ||
3549 nbytes < 0 || nbytes > MAX_BLOB_WIDTH ||
3550 min_str.alloc(nbytes) || max_str.alloc(nbytes))
3554 if (cs->coll->like_range(cs, res->ptr(), res->length(),
3555 '\\',
'_',
'%', nbytes,
3556 (
char*) min_str.ptr(), (
char*) max_str.ptr(),
3557 &min_len, &max_len))
3560 min_str.set_charset(collation.collation);
3561 max_str.set_charset(collation.collation);
3562 min_str.length(min_len);
3563 max_str.length(max_len);
3565 return is_min ? &min_str : &max_str;
3578 if (item->type() != FUNC_ITEM ||
3579 functype() != ((
Item_func*)item)->functype())
3583 if (cast_length != cast->cast_length ||
3584 cast_cs != cast->cast_cs)
3587 if (!args[0]->
eq(cast->args[0], binary_cmp))
3595 str->append(STRING_WITH_LEN(
"cast("));
3596 args[0]->
print(str, query_type);
3597 str->append(STRING_WITH_LEN(
" as char"));
3598 if (cast_length >= 0)
3602 str->append(STRING_WITH_LEN(
" charset "));
3603 str->append(cast_cs->csname);
3611 DBUG_ASSERT(fixed == 1);
3615 if (cast_length >= 0 &&
3616 ((
unsigned) cast_length) > current_thd->variables.max_allowed_packet)
3618 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3619 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3620 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3621 cast_cs == &my_charset_bin ?
3622 "cast_as_binary" : func_name(),
3623 current_thd->variables.max_allowed_packet);
3628 if (!charset_conversion)
3630 if (!(res= args[0]->val_str(str)))
3640 if (!(res= args[0]->val_str(str)) ||
3641 tmp_value.copy(res->ptr(), res->length(), from_cs,
3642 cast_cs, &dummy_errors))
3650 res->set_charset(cast_cs);
3657 if (cast_length >= 0)
3659 if (res->length() > (length= (uint32) res->charpos(cast_length)))
3662 my_snprintf(char_type,
sizeof(char_type),
"%s(%lu)",
3663 cast_cs == &my_charset_bin ?
"BINARY" :
"CHAR",
3666 if (!res->alloced_length())
3672 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3673 ER_TRUNCATED_WRONG_VALUE,
3674 ER(ER_TRUNCATED_WRONG_VALUE), char_type,
3676 res->length((uint) length);
3678 else if (cast_cs == &my_charset_bin && res->length() < (uint) cast_length)
3680 if (res->alloced_length() < (uint) cast_length)
3682 str_value.alloc(cast_length);
3683 str_value.copy(*res);
3686 memset(const_cast<char*>(res->ptr() + res->length()), 0,
3687 cast_length - res->length());
3688 res->length(cast_length);
3696 void Item_char_typecast::fix_length_and_dec()
3704 from_cs= args[0]->collation.repertoire == MY_REPERTOIRE_ASCII &&
3705 my_charset_is_ascii_based(cast_cs) &&
3706 my_charset_is_ascii_based(args[0]->collation.collation) ?
3707 cast_cs : args[0]->collation.collation;
3710 collation.set(cast_cs, DERIVATION_IMPLICIT);
3711 fix_char_length(cast_length >= 0 ? cast_length :
3712 cast_cs == &my_charset_bin ? args[0]->max_length :
3713 args[0]->max_char_length());
3722 charset_conversion= (cast_cs->mbmaxlen > 1) ||
3723 (!my_charset_same(from_cs, cast_cs) &&
3724 from_cs != &my_charset_bin &&
3725 cast_cs != &my_charset_bin);
3731 str->append(STRING_WITH_LEN(
"cast("));
3732 args[0]->
print(str, query_type);
3733 str->append(STRING_WITH_LEN(
" as binary)"));
3741 DBUG_ASSERT(fixed == 1);
3745 char path[FN_REFLEN];
3746 DBUG_ENTER(
"load_file");
3748 if (!(file_name= args[0]->val_str(str))
3749 #ifndef NO_EMBEDDED_ACCESS_CHECKS
3750 || !(current_thd->security_ctx->master_access & FILE_ACL)
3755 (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home,
"",
3756 MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
3759 if (!is_secure_file_path(path))
3765 if (!(stat_info.st_mode & S_IROTH))
3770 if (stat_info.st_size > (
long) current_thd->variables.max_allowed_packet)
3772 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3773 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3774 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3775 func_name(), current_thd->variables.max_allowed_packet);
3778 if (tmp_value.alloc(stat_info.st_size))
3781 file_name->ptr(), O_RDONLY, MYF(0))) < 0)
3783 if (
mysql_file_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size,
3789 tmp_value.length(stat_info.st_size);
3792 DBUG_RETURN(&tmp_value);
3802 DBUG_ASSERT(fixed == 1);
3803 String yes_buf, no_buf, sep_buf;
3804 const ulonglong the_set = (ulonglong) args[0]->val_int();
3805 const String *yes= args[1]->val_str(&yes_buf);
3806 const String *no= args[2]->val_str(&no_buf);
3809 uint num_set_values = 64;
3811 str->set_charset(collation.collation);
3814 if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
3825 num_set_values = (uint) args[4]->val_int();
3826 if (num_set_values > 64)
3828 if (args[4]->null_value)
3835 if (!(sep = args[3]->val_str(&sep_buf)))
3845 sep_buf.copy(STRING_WITH_LEN(
","), &my_charset_bin,
3846 collation.collation, &errors);
3855 const ulong max_allowed_packet= current_thd->variables.max_allowed_packet;
3856 const uint num_separators= num_set_values > 0 ? num_set_values - 1 : 0;
3857 const ulonglong max_total_length=
3858 num_set_values * max(yes->length(), no->length()) +
3859 num_separators * sep->length();
3861 if (unlikely(max_total_length > max_allowed_packet))
3863 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3864 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3865 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3866 func_name(), max_allowed_packet);
3873 for (ix= 0, mask=0x1; ix < num_set_values; ++ix, mask = (mask << 1))
3879 if (ix != num_separators)
3885 void Item_func_export_set::fix_length_and_dec()
3887 uint32 length= max(args[1]->max_char_length(), args[2]->max_char_length());
3888 uint32 sep_length= (arg_count > 3 ? args[3]->max_char_length() : 1);
3890 if (agg_arg_charsets_for_string_result(collation,
3891 args + 1, min(4
U, arg_count) - 1))
3893 fix_char_length(length * 64 + sep_length * 63);
3897 #define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
3920 DBUG_ASSERT(fixed == 1);
3926 static uchar escmask[32]=
3928 0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
3929 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
3930 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3931 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3934 char *from, *
to, *end, *start;
3935 String *arg= args[0]->val_str(str);
3936 uint arg_length, new_length;
3940 str->copy(STRING_WITH_LEN(
"NULL"), collation.collation);
3945 arg_length= arg->length();
3947 if (collation.collation->mbmaxlen == 1)
3949 new_length= arg_length + 2;
3950 for (from= (
char*) arg->ptr(), end= from + arg_length; from < end; from++)
3951 new_length+= get_esc_bit(escmask, (uchar) *from);
3955 new_length= (arg_length * 2) +
3956 (2 * collation.collation->mbmaxlen);
3959 if (tmp_value.alloc(new_length))
3962 if (collation.collation->mbmaxlen > 1)
3967 to= (
char*) tmp_value.ptr();
3968 to_end= (uchar*) to + new_length;
3971 if ((mblen= cs->cset->wc_mb(cs,
'\'', (uchar *) to, to_end)) <= 0)
3975 for (start= (
char*) arg->ptr(), end= start + arg_length; start < end; )
3979 if ((mblen= cs->cset->mb_wc(cs, &wc, (uchar*) start, (uchar*) end)) <= 0)
3983 case 0: escape= 1; wc=
'0';
break;
3984 case '\032': escape= 1; wc=
'Z';
break;
3985 case '\'': escape= 1;
break;
3986 case '\\': escape= 1;
break;
3987 default: escape= 0;
break;
3991 if ((mblen= cs->cset->wc_mb(cs,
'\\', (uchar*) to, to_end)) <= 0)
3995 if ((mblen= cs->cset->wc_mb(cs, wc, (uchar*) to, to_end)) <= 0)
4001 if ((mblen= cs->cset->wc_mb(cs,
'\'', (uchar *) to, to_end)) <= 0)
4004 new_length= to - tmp_value.ptr();
4011 to= (
char*) tmp_value.ptr() + new_length - 1;
4013 for (start= (
char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
4041 tmp_value.length(new_length);
4042 tmp_value.set_charset(collation.collation);
4051 longlong Item_func_uncompressed_length::val_int()
4053 DBUG_ASSERT(fixed == 1);
4054 String *res= args[0]->val_str(&value);
4061 if (res->is_empty())
return 0;
4067 if (res->length() <= 4)
4069 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
4070 ER_ZLIB_Z_DATA_ERROR,
4071 ER(ER_ZLIB_Z_DATA_ERROR));
4084 return uint4korr(res->ptr()) & 0x3FFFFFFF;
4087 longlong Item_func_crc32::val_int()
4089 DBUG_ASSERT(fixed == 1);
4090 String *res=args[0]->val_str(&value);
4097 return (longlong) crc32(0L, (uchar*)res->ptr(), res->length());
4100 #ifdef HAVE_COMPRESS
4105 int err= Z_OK, code;
4109 char *tmp, *last_char;
4110 DBUG_ASSERT(fixed == 1);
4112 if (!(res= args[0]->val_str(str)))
4118 if (res->is_empty())
return res;
4129 new_size= res->length() + res->length() / 5 + 12;
4132 if (((uint32) (new_size+5) <= res->length()) ||
4133 buffer.realloc((uint32) new_size + 4 + 1))
4139 body= ((Byte*)buffer.ptr()) + 4;
4142 if ((err= compress(body, &new_size,
4143 (
const Bytef*)res->ptr(), res->length())) != Z_OK)
4145 code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
4146 push_warning(current_thd,Sql_condition::WARN_LEVEL_WARN,code,ER(code));
4151 tmp= (
char*)buffer.ptr();
4152 int4store(tmp, res->length() & 0x3FFFFFFF);
4155 last_char= ((
char*)body)+new_size-1;
4156 if (*last_char ==
' ')
4162 buffer.length((uint32)new_size + 4);
4169 DBUG_ASSERT(fixed == 1);
4170 String *res= args[0]->val_str(str);
4178 if (res->is_empty())
4182 if (res->length() <= 4)
4184 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
4185 ER_ZLIB_Z_DATA_ERROR,
4186 ER(ER_ZLIB_Z_DATA_ERROR));
4191 new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
4192 if (new_size > current_thd->variables.max_allowed_packet)
4194 push_warning_printf(current_thd,Sql_condition::WARN_LEVEL_WARN,
4195 ER_TOO_BIG_FOR_UNCOMPRESS,
4196 ER(ER_TOO_BIG_FOR_UNCOMPRESS),
4197 static_cast<int>(current_thd->variables.
4198 max_allowed_packet));
4201 if (buffer.realloc((uint32)new_size))
4204 if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
4205 ((
const Bytef*)res->ptr())+4,res->length())) == Z_OK)
4207 buffer.length((uint32) new_size);
4211 code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
4212 ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
4213 push_warning(current_thd,Sql_condition::WARN_LEVEL_WARN,code,ER(code));
4230 static uint nanoseq;
4231 static ulonglong uuid_time=0;
4232 static char clock_seq_and_node_str[]=
"-0000-000000000000";
4238 #define UUID_TIME_OFFSET ((ulonglong) 141427 * 24 * 60 * 60 * \
4241 #define UUID_VERSION 0x1000
4242 #define UUID_VARIANT 0x8000
4244 static void tohex(
char *to, uint from, uint len)
4249 *--to= _dig_vec_lower[from & 15];
4254 static void set_clock_seq_str()
4256 uint16 clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
4257 tohex(clock_seq_and_node_str+1, clock_seq, 4);
4263 DBUG_ASSERT(fixed == 1);
4265 THD *thd= current_thd;
4270 ulong tmp=sql_rnd_with_mutex();
4273 if (my_gethwaddr(mac))
4282 randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
4283 for (i=0; i < (int)
sizeof(mac); i++)
4284 mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
4287 s=clock_seq_and_node_str+
sizeof(clock_seq_and_node_str)-1;
4288 for (i=
sizeof(mac)-1 ; i>=0 ; i--)
4290 *--s=_dig_vec_lower[mac[
i] & 15];
4291 *--s=_dig_vec_lower[mac[
i] >> 4];
4293 randominit(&uuid_rand, tmp + (ulong) server_start_time,
4294 tmp + (ulong) thd->status_var.bytes_sent);
4295 set_clock_seq_str();
4300 if (likely(tv > uuid_time))
4309 DBUG_ASSERT((tv > uuid_time) && (nanoseq > 0));
4313 ulong delta= min<ulong>(nanoseq, (ulong) (tv - uuid_time -1));
4320 if (unlikely(tv == uuid_time))
4333 if (likely(++nanoseq))
4337 if (unlikely(tv <= uuid_time))
4349 set_clock_seq_str();
4352 DBUG_PRINT(
"uuid",(
"making new numberspace"));
4359 uint32 time_low= (uint32) (tv & 0xFFFFFFFF);
4360 uint16 time_mid= (uint16) ((tv >> 32) & 0xFFFF);
4361 uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);
4364 str->length(UUID_LENGTH);
4365 str->set_charset(system_charset_info);
4366 s=(
char *) str->ptr();
4368 tohex(s, time_low, 8);
4369 tohex(s+9, time_mid, 4);
4370 tohex(s+14, time_hi_and_version, 4);
4371 strmov(s+18, clock_seq_and_node_str);
4376 void Item_func_gtid_subtract::fix_length_and_dec()
4378 maybe_null= args[0]->maybe_null || args[1]->maybe_null;
4379 collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
4387 fix_char_length_ulonglong(args[0]->max_length +
4388 max<ulonglong>(args[1]->max_length -
4393 String *Item_func_gtid_subtract::val_str_ascii(
String *str)
4395 DBUG_ENTER(
"Item_func_gtid_subtract::val_str_ascii");
4397 const char *charp1, *charp2;
4398 enum_return_status status;
4404 if ((str1= args[0]->val_str_ascii(&buf1)) != NULL &&
4405 (charp1= str1->c_ptr_safe()) != NULL &&
4406 (str2= args[1]->val_str_ascii(&buf2)) != NULL &&
4407 (charp2= str2->c_ptr_safe()) != NULL &&
4408 !args[0]->null_value && !args[1]->null_value)
4412 Gtid_set set1(&sid_map, charp1, &status);
4413 if (status == RETURN_STATUS_OK)
4415 Gtid_set set2(&sid_map, charp2, &status);
4418 if (status == RETURN_STATUS_OK &&
4419 set1.remove_gtid_set(&set2) == 0 &&
4420 !str->
realloc((length= set1.get_string_length()) + 1))
4423 set1.to_string((
char *)str->ptr());
4424 str->length(length);