00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <drizzled/function/str/repeat.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/function/str/alloc_buffer.h>
00025 #include <drizzled/session.h>
00026
00027 namespace drizzled
00028 {
00029
00030 void Item_func_repeat::fix_length_and_dec()
00031 {
00032 collation.set(args[0]->collation);
00033 if (args[1]->const_item())
00034 {
00035
00036 int64_t count= args[1]->val_int();
00037
00038
00039
00040 if (count > INT32_MAX)
00041 count= INT32_MAX;
00042
00043 uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
00044 if (max_result_length >= MAX_BLOB_WIDTH)
00045 {
00046 max_result_length= MAX_BLOB_WIDTH;
00047 maybe_null= 1;
00048 }
00049 max_length= (ulong) max_result_length;
00050 }
00051 else
00052 {
00053 max_length= MAX_BLOB_WIDTH;
00054 maybe_null= 1;
00055 }
00056 }
00057
00063 String *Item_func_repeat::val_str(String *str)
00064 {
00065 assert(fixed == 1);
00066 uint32_t length,tot_length;
00067 char *to;
00068
00069 int64_t count= args[1]->val_int();
00070 String *res= args[0]->val_str(str);
00071
00072 if (args[0]->null_value || args[1]->null_value)
00073 goto err;
00074 null_value= 0;
00075
00076 if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
00077 return &my_empty_string;
00078
00079
00080
00081 if ((uint64_t) count > INT32_MAX)
00082 count= INT32_MAX;
00083 if (count == 1)
00084 return res;
00085 length=res->length();
00086
00087 if (length > session.variables.max_allowed_packet / (uint) count)
00088 {
00089 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
00090 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
00091 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
00092 func_name(), session.variables.max_allowed_packet);
00093 goto err;
00094 }
00095 tot_length= length*(uint) count;
00096 if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
00097 goto err;
00098
00099 to=(char*) res->ptr()+length;
00100 while (--count)
00101 {
00102 memcpy(to,res->ptr(),length);
00103 to+=length;
00104 }
00105 return (res);
00106
00107 err:
00108 null_value=1;
00109 return 0;
00110 }
00111
00112 }