22 #include <drizzled/function/str/concat.h>
23 #include <drizzled/error.h>
24 #include <drizzled/session.h>
25 #include <drizzled/system_variables.h>
36 String *res,*res2,*use_as_buff;
41 if (!(res=args[0]->val_str(str)))
43 use_as_buff= &tmp_value;
45 is_const= args[0]->const_item() || !args[0]->used_tables();
46 for (i=1 ; i < arg_count ; i++)
48 if (res->length() == 0)
50 if (!(res=args[i]->val_str(str)))
55 if (!(res2=args[i]->val_str(use_as_buff)))
57 if (res2->length() == 0)
59 if (res->length()+res2->length() >
62 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
63 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
64 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
68 if (!is_const && res->alloced_length() >= res->length()+res2->length())
72 else if (str->alloced_length() >= res->length()+res2->length())
75 str->replace(0,0,*res);
82 use_as_buff= &tmp_value;
84 else if (res == &tmp_value)
88 else if (res2 == &tmp_value)
90 tmp_value.replace(0,0,*res);
94 else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
95 res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
103 tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
106 tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()), *res);
120 size_t concat_len= res->length() + res2->length();
122 if (tmp_value.alloced_length() < concat_len)
124 if (tmp_value.alloced_length() == 0)
126 tmp_value.alloc(concat_len);
130 uint32_t new_len= max(tmp_value.alloced_length() * 2, concat_len);
132 tmp_value.realloc(new_len);
136 tmp_value.copy(*res);
137 tmp_value.append(*res2);
145 res->set_charset(collation.collation);
154 void Item_func_concat::fix_length_and_dec()
156 uint64_t max_result_length= 0;
158 if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
161 for (uint32_t i=0 ; i < arg_count ; i++)
163 if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
164 max_result_length+= (args[i]->max_length /
165 args[i]->collation.collation->mbmaxlen) *
166 collation.collation->mbmaxlen;
168 max_result_length+= args[i]->max_length;
171 if (max_result_length >= MAX_BLOB_WIDTH)
173 max_result_length= MAX_BLOB_WIDTH;
176 max_length= (ulong) max_result_length;
188 char tmp_str_buff[10];
189 String tmp_sep_str(tmp_str_buff,
sizeof(tmp_str_buff),default_charset_info),
190 *sep_str, *res, *res2,*use_as_buff;
194 if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
197 use_as_buff= &tmp_value;
203 for (i=1; i < arg_count; i++)
204 if ((res= args[i]->val_str(str)))
207 return &my_empty_string;
209 for (i++; i < arg_count ; i++)
211 if (!(res2= args[i]->val_str(use_as_buff)))
214 if (res->length() + sep_str->length() + res2->length() >
217 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
218 ER_WARN_ALLOWED_PACKET_OVERFLOWED,
219 ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
223 if (res->alloced_length() >=
224 res->length() + sep_str->length() + res2->length())
226 res->append(*sep_str);
229 else if (str->alloced_length() >=
230 res->length() + sep_str->length() + res2->length())
235 str->replace(0,0,*sep_str);
236 str->replace(0,0,*res);
241 str->append(*sep_str);
245 use_as_buff= &tmp_value;
247 else if (res == &tmp_value)
249 res->append(*sep_str);
252 else if (res2 == &tmp_value)
254 tmp_value.replace(0,0,*sep_str);
255 tmp_value.replace(0,0,*res);
259 else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
260 res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
268 tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
271 tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()), *res);
272 tmp_value.replace(res->length(),0, *sep_str);
286 size_t concat_len= res->length() + sep_str->length() + res2->length();
288 if (tmp_value.alloced_length() < concat_len)
290 if (tmp_value.alloced_length() == 0)
292 tmp_value.alloc(concat_len);
296 uint32_t new_len= max(tmp_value.alloced_length() * 2, concat_len);
298 tmp_value.realloc(new_len);
302 tmp_value.copy(*res);
303 tmp_value.append(*sep_str);
304 tmp_value.append(*res2);
309 res->set_charset(collation.collation);
318 void Item_func_concat_ws::fix_length_and_dec()
320 uint64_t max_result_length;
322 if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
330 max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
331 for (uint32_t i=1 ; i < arg_count ; i++)
332 max_result_length+=args[i]->max_length;
334 if (max_result_length >= MAX_BLOB_WIDTH)
336 max_result_length= MAX_BLOB_WIDTH;
339 max_length= (ulong) max_result_length;
drizzle_system_variables & variables