~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_strfunc.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
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.
6
 
 
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.
11
 
 
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 */
15
 
 
16
 
 
17
 
/**
18
 
  @file
19
 
 
20
 
  @brief
21
 
  This file defines all string functions
22
 
 
23
 
  @warning
24
 
    Some string functions don't always put and end-null on a String.
25
 
    (This shouldn't be needed)
26
 
*/
27
 
 
28
 
#include <drizzled/server_includes.h>
29
 
#include <mysys/sha1.h>
30
 
#include <zlib.h>
31
 
C_MODE_START
32
 
#include <mysys/my_static.h>                    // For soundex_map
33
 
C_MODE_END
34
 
#include <drizzled/drizzled_error_messages.h>
35
 
 
36
 
String my_empty_string("",default_charset_info);
37
 
 
38
 
 
39
 
 
40
 
 
41
 
bool Item_str_func::fix_fields(THD *thd, Item **ref)
42
 
{
43
 
  bool res= Item_func::fix_fields(thd, ref);
44
 
  /*
45
 
    In Item_str_func::check_well_formed_result() we may set null_value
46
 
    flag on the same condition as in test() below.
47
 
  */
48
 
  maybe_null= (maybe_null ||
49
 
               test(thd->variables.sql_mode &
50
 
                    (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES)));
51
 
  return res;
52
 
}
53
 
 
54
 
 
55
 
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
56
 
{
57
 
  assert(fixed == 1);
58
 
  char buff[64];
59
 
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
60
 
  res= val_str(&tmp);
61
 
  if (!res)
62
 
    return 0;
63
 
  (void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
64
 
                       res->length(), res->charset(), decimal_value);
65
 
  return decimal_value;
66
 
}
67
 
 
68
 
 
69
 
double Item_str_func::val_real()
70
 
{
71
 
  assert(fixed == 1);
72
 
  int err_not_used;
73
 
  char *end_not_used, buff[64];
74
 
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
75
 
  res= val_str(&tmp);
76
 
  return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
77
 
                          &end_not_used, &err_not_used) : 0.0;
78
 
}
79
 
 
80
 
 
81
 
int64_t Item_str_func::val_int()
82
 
{
83
 
  assert(fixed == 1);
84
 
  int err;
85
 
  char buff[22];
86
 
  String *res, tmp(buff,sizeof(buff), &my_charset_bin);
87
 
  res= val_str(&tmp);
88
 
  return (res ?
89
 
          my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
90
 
                      &err) :
91
 
          (int64_t) 0);
92
 
}
93
 
 
94
 
/**
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.
98
 
*/
99
 
 
100
 
String *Item_func_concat::val_str(String *str)
101
 
{
102
 
  assert(fixed == 1);
103
 
  String *res,*res2,*use_as_buff;
104
 
  uint i;
105
 
  bool is_const= 0;
106
 
 
107
 
  null_value=0;
108
 
  if (!(res=args[0]->val_str(str)))
109
 
    goto null;
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++)
114
 
  {
115
 
    if (res->length() == 0)
116
 
    {
117
 
      if (!(res=args[i]->val_str(str)))
118
 
        goto null;
119
 
    }
120
 
    else
121
 
    {
122
 
      if (!(res2=args[i]->val_str(use_as_buff)))
123
 
        goto null;
124
 
      if (res2->length() == 0)
125
 
        continue;
126
 
      if (res->length()+res2->length() >
127
 
          current_thd->variables.max_allowed_packet)
128
 
      {
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);
133
 
        goto null;
134
 
      }
135
 
      if (!is_const && res->alloced_length() >= res->length()+res2->length())
136
 
      {                                         // Use old buffer
137
 
        res->append(*res2);
138
 
      }
139
 
      else if (str->alloced_length() >= res->length()+res2->length())
140
 
      {
141
 
        if (str == res2)
142
 
          str->replace(0,0,*res);
143
 
        else
144
 
        {
145
 
          str->copy(*res);
146
 
          str->append(*res2);
147
 
        }
148
 
        res= str;
149
 
        use_as_buff= &tmp_value;
150
 
      }
151
 
      else if (res == &tmp_value)
152
 
      {
153
 
        if (res->append(*res2))                 // Must be a blob
154
 
          goto null;
155
 
      }
156
 
      else if (res2 == &tmp_value)
157
 
      {                                         // This can happend only 1 time
158
 
        if (tmp_value.replace(0,0,*res))
159
 
          goto null;
160
 
        res= &tmp_value;
161
 
        use_as_buff=str;                        // Put next arg here
162
 
      }
163
 
      else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
164
 
               res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
165
 
      {
166
 
        /*
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
170
 
        */
171
 
        /* Chop the last characters in tmp_value that isn't in res2 */
172
 
        tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
173
 
                         res2->length());
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()),
176
 
                              *res))
177
 
          goto null;
178
 
        res= &tmp_value;
179
 
        use_as_buff=str;                        // Put next arg here
180
 
      }
181
 
      else
182
 
      {                                         // Two big const strings
183
 
        /*
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.
189
 
        */
190
 
 
191
 
        uint concat_len= res->length() + res2->length();
192
 
 
193
 
        if (tmp_value.alloced_length() < concat_len)
194
 
        {
195
 
          if (tmp_value.alloced_length() == 0)
196
 
          {
197
 
            if (tmp_value.alloc(concat_len))
198
 
              goto null;
199
 
          }
200
 
          else
201
 
          {
202
 
            uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
203
 
 
204
 
            if (tmp_value.realloc(new_len))
205
 
              goto null;
206
 
          }
207
 
        }
208
 
 
209
 
        if (tmp_value.copy(*res) || tmp_value.append(*res2))
210
 
          goto null;
211
 
 
212
 
        res= &tmp_value;
213
 
        use_as_buff=str;
214
 
      }
215
 
      is_const= 0;
216
 
    }
217
 
  }
218
 
  res->set_charset(collation.collation);
219
 
  return res;
220
 
 
221
 
null:
222
 
  null_value=1;
223
 
  return 0;
224
 
}
225
 
 
226
 
 
227
 
void Item_func_concat::fix_length_and_dec()
228
 
{
229
 
  uint64_t max_result_length= 0;
230
 
 
231
 
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
232
 
    return;
233
 
 
234
 
  for (uint i=0 ; i < arg_count ; i++)
235
 
  {
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;
240
 
    else
241
 
      max_result_length+= args[i]->max_length;
242
 
  }
243
 
 
244
 
  if (max_result_length >= MAX_BLOB_WIDTH)
245
 
  {
246
 
    max_result_length= MAX_BLOB_WIDTH;
247
 
    maybe_null= 1;
248
 
  }
249
 
  max_length= (ulong) max_result_length;
250
 
}
251
 
 
252
 
 
253
 
/**
254
 
  concat with separator. First arg is the separator
255
 
  concat_ws takes at least two arguments.
256
 
*/
257
 
 
258
 
String *Item_func_concat_ws::val_str(String *str)
259
 
{
260
 
  assert(fixed == 1);
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;
264
 
  uint i;
265
 
 
266
 
  null_value=0;
267
 
  if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
268
 
    goto null;
269
 
 
270
 
  use_as_buff= &tmp_value;
271
 
  str->length(0);                               // QQ; Should be removed
272
 
  res=str;
273
 
 
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)))
278
 
      break;
279
 
  if (i ==  arg_count)
280
 
    return &my_empty_string;
281
 
 
282
 
  for (i++; i < arg_count ; i++)
283
 
  {
284
 
    if (!(res2= args[i]->val_str(use_as_buff)))
285
 
      continue;                                 // Skip NULL
286
 
 
287
 
    if (res->length() + sep_str->length() + res2->length() >
288
 
        current_thd->variables.max_allowed_packet)
289
 
    {
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);
294
 
      goto null;
295
 
    }
296
 
    if (res->alloced_length() >=
297
 
        res->length() + sep_str->length() + res2->length())
298
 
    {                                           // Use old buffer
299
 
      res->append(*sep_str);                    // res->length() > 0 always
300
 
      res->append(*res2);
301
 
    }
302
 
    else if (str->alloced_length() >=
303
 
             res->length() + sep_str->length() + res2->length())
304
 
    {
305
 
      /* We have room in str;  We can't get any errors here */
306
 
      if (str == res2)
307
 
      {                                         // This is quote uncommon!
308
 
        str->replace(0,0,*sep_str);
309
 
        str->replace(0,0,*res);
310
 
      }
311
 
      else
312
 
      {
313
 
        str->copy(*res);
314
 
        str->append(*sep_str);
315
 
        str->append(*res2);
316
 
      }
317
 
      res=str;
318
 
      use_as_buff= &tmp_value;
319
 
    }
320
 
    else if (res == &tmp_value)
321
 
    {
322
 
      if (res->append(*sep_str) || res->append(*res2))
323
 
        goto null; // Must be a blob
324
 
    }
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))
328
 
        goto null;
329
 
      res= &tmp_value;
330
 
      use_as_buff=str;                          // Put next arg here
331
 
    }
332
 
    else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
333
 
             res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
334
 
    {
335
 
      /*
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
339
 
      */
340
 
      /* Chop the last characters in tmp_value that isn't in res2 */
341
 
      tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
342
 
                       res2->length());
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()),
345
 
                            *res) ||
346
 
          tmp_value.replace(res->length(),0, *sep_str))
347
 
        goto null;
348
 
      res= &tmp_value;
349
 
      use_as_buff=str;                  // Put next arg here
350
 
    }
351
 
    else
352
 
    {                                           // Two big const strings
353
 
      /*
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.
359
 
      */
360
 
 
361
 
      uint concat_len= res->length() + sep_str->length() + res2->length();
362
 
 
363
 
      if (tmp_value.alloced_length() < concat_len)
364
 
      {
365
 
        if (tmp_value.alloced_length() == 0)
366
 
        {
367
 
          if (tmp_value.alloc(concat_len))
368
 
            goto null;
369
 
        }
370
 
        else
371
 
        {
372
 
          uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
373
 
 
374
 
          if (tmp_value.realloc(new_len))
375
 
            goto null;
376
 
        }
377
 
      }
378
 
 
379
 
      if (tmp_value.copy(*res) ||
380
 
          tmp_value.append(*sep_str) ||
381
 
          tmp_value.append(*res2))
382
 
        goto null;
383
 
      res= &tmp_value;
384
 
      use_as_buff=str;
385
 
    }
386
 
  }
387
 
  res->set_charset(collation.collation);
388
 
  return res;
389
 
 
390
 
null:
391
 
  null_value=1;
392
 
  return 0;
393
 
}
394
 
 
395
 
 
396
 
void Item_func_concat_ws::fix_length_and_dec()
397
 
{
398
 
  uint64_t max_result_length;
399
 
 
400
 
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
401
 
    return;
402
 
 
403
 
  /*
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.
407
 
  */
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;
411
 
 
412
 
  if (max_result_length >= MAX_BLOB_WIDTH)
413
 
  {
414
 
    max_result_length= MAX_BLOB_WIDTH;
415
 
    maybe_null= 1;
416
 
  }
417
 
  max_length= (ulong) max_result_length;
418
 
}
419
 
 
420
 
 
421
 
String *Item_func_reverse::val_str(String *str)
422
 
{
423
 
  assert(fixed == 1);
424
 
  String *res = args[0]->val_str(str);
425
 
  char *ptr, *end, *tmp;
426
 
 
427
 
  if ((null_value=args[0]->null_value))
428
 
    return 0;
429
 
  /* An empty string is a special case as the string pointer may be null */
430
 
  if (!res->length())
431
 
    return &my_empty_string;
432
 
  if (tmp_value.alloced_length() < res->length() &&
433
 
      tmp_value.realloc(res->length()))
434
 
  {
435
 
    null_value= 1;
436
 
    return 0;
437
 
  }
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();
443
 
#ifdef USE_MB
444
 
  if (use_mb(res->charset()))
445
 
  {
446
 
    register uint32_t l;
447
 
    while (ptr < end)
448
 
    {
449
 
      if ((l= my_ismbchar(res->charset(),ptr,end)))
450
 
      {
451
 
        tmp-= l;
452
 
        memcpy(tmp,ptr,l);
453
 
        ptr+= l;
454
 
      }
455
 
      else
456
 
        *--tmp= *ptr++;
457
 
    }
458
 
  }
459
 
  else
460
 
#endif /* USE_MB */
461
 
  {
462
 
    while (ptr < end)
463
 
      *--tmp= *ptr++;
464
 
  }
465
 
  return &tmp_value;
466
 
}
467
 
 
468
 
 
469
 
void Item_func_reverse::fix_length_and_dec()
470
 
{
471
 
  collation.set(args[0]->collation);
472
 
  max_length = args[0]->max_length;
473
 
}
474
 
 
475
 
/**
476
 
  Replace all occurences of string2 in string1 with string3.
477
 
 
478
 
  Don't reallocate val_str() if not needed.
479
 
 
480
 
  @todo
481
 
    Fix that this works with binary strings when using USE_MB 
482
 
*/
483
 
 
484
 
String *Item_func_replace::val_str(String *str)
485
 
{
486
 
  assert(fixed == 1);
487
 
  String *res,*res2,*res3;
488
 
  int offset;
489
 
  uint from_length,to_length;
490
 
  bool alloced=0;
491
 
#ifdef USE_MB
492
 
  const char *ptr,*end,*strend,*search,*search_end;
493
 
  register uint32_t l;
494
 
  bool binary_cmp;
495
 
#endif
496
 
 
497
 
  null_value=0;
498
 
  res=args[0]->val_str(str);
499
 
  if (args[0]->null_value)
500
 
    goto null;
501
 
  res2=args[1]->val_str(&tmp_value);
502
 
  if (args[1]->null_value)
503
 
    goto null;
504
 
 
505
 
  res->set_charset(collation.collation);
506
 
 
507
 
#ifdef USE_MB
508
 
  binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
509
 
#endif
510
 
 
511
 
  if (res2->length() == 0)
512
 
    return res;
513
 
#ifndef USE_MB
514
 
  if ((offset=res->strstr(*res2)) < 0)
515
 
    return res;
516
 
#else
517
 
  offset=0;
518
 
  if (binary_cmp && (offset=res->strstr(*res2)) < 0)
519
 
    return res;
520
 
#endif
521
 
  if (!(res3=args[2]->val_str(&tmp_value2)))
522
 
    goto null;
523
 
  from_length= res2->length();
524
 
  to_length=   res3->length();
525
 
 
526
 
#ifdef USE_MB
527
 
  if (!binary_cmp)
528
 
  {
529
 
    search=res2->ptr();
530
 
    search_end=search+from_length;
531
 
redo:
532
 
    ptr=res->ptr()+offset;
533
 
    strend=res->ptr()+res->length();
534
 
    end=strend-from_length+1;
535
 
    while (ptr < end)
536
 
    {
537
 
        if (*ptr == *search)
538
 
        {
539
 
          register char *i,*j;
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)
546
 
          {
547
 
            push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
548
 
                                ER_WARN_ALLOWED_PACKET_OVERFLOWED,
549
 
                                ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
550
 
                                func_name(),
551
 
                                current_thd->variables.max_allowed_packet);
552
 
 
553
 
            goto null;
554
 
          }
555
 
          if (!alloced)
556
 
          {
557
 
            alloced=1;
558
 
            res=copy_if_not_alloced(str,res,res->length()+to_length);
559
 
          }
560
 
          res->replace((uint) offset,from_length,*res3);
561
 
          offset+=(int) to_length;
562
 
          goto redo;
563
 
        }
564
 
skip:
565
 
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
566
 
        else ++ptr;
567
 
    }
568
 
  }
569
 
  else
570
 
#endif /* USE_MB */
571
 
    do
572
 
    {
573
 
      if (res->length()-from_length + to_length >
574
 
          current_thd->variables.max_allowed_packet)
575
 
      {
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);
580
 
        goto null;
581
 
      }
582
 
      if (!alloced)
583
 
      {
584
 
        alloced=1;
585
 
        res=copy_if_not_alloced(str,res,res->length()+to_length);
586
 
      }
587
 
      res->replace((uint) offset,from_length,*res3);
588
 
      offset+=(int) to_length;
589
 
    }
590
 
    while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
591
 
  return res;
592
 
 
593
 
null:
594
 
  null_value=1;
595
 
  return 0;
596
 
}
597
 
 
598
 
 
599
 
void Item_func_replace::fix_length_and_dec()
600
 
{
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;
607
 
  }
608
 
  if (max_result_length >= MAX_BLOB_WIDTH)
609
 
  {
610
 
    max_result_length= MAX_BLOB_WIDTH;
611
 
    maybe_null= 1;
612
 
  }
613
 
  max_length= (ulong) max_result_length;
614
 
  
615
 
  if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1))
616
 
    return;
617
 
}
618
 
 
619
 
 
620
 
String *Item_func_insert::val_str(String *str)
621
 
{
622
 
  assert(fixed == 1);
623
 
  String *res,*res2;
624
 
  int64_t start, length;  /* must be int64_t to avoid truncation */
625
 
 
626
 
  null_value=0;
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();
631
 
 
632
 
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
633
 
      args[3]->null_value)
634
 
    goto null; /* purecov: inspected */
635
 
 
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();
640
 
 
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);
644
 
 
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;
650
 
 
651
 
  if ((uint64_t) (res->length() - length + res2->length()) >
652
 
      (uint64_t) current_thd->variables.max_allowed_packet)
653
 
  {
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);
658
 
    goto null;
659
 
  }
660
 
  res=copy_if_not_alloced(str,res,res->length());
661
 
  res->replace((uint32_t) start,(uint32_t) length,*res2);
662
 
  return res;
663
 
null:
664
 
  null_value=1;
665
 
  return 0;
666
 
}
667
 
 
668
 
 
669
 
void Item_func_insert::fix_length_and_dec()
670
 
{
671
 
  uint64_t max_result_length;
672
 
 
673
 
  // Handle character set for args[0] and args[3].
674
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
675
 
    return;
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)
679
 
  {
680
 
    max_result_length= MAX_BLOB_WIDTH;
681
 
    maybe_null= 1;
682
 
  }
683
 
  max_length= (ulong) max_result_length;
684
 
}
685
 
 
686
 
 
687
 
String *Item_str_conv::val_str(String *str)
688
 
{
689
 
  assert(fixed == 1);
690
 
  String *res;
691
 
  if (!(res=args[0]->val_str(str)))
692
 
  {
693
 
    null_value=1; /* purecov: inspected */
694
 
    return 0; /* purecov: inspected */
695
 
  }
696
 
  null_value=0;
697
 
  if (multiply == 1)
698
 
  {
699
 
    uint len;
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());
704
 
    res->length(len);
705
 
  }
706
 
  else
707
 
  {
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);
714
 
    res= &tmp_value;
715
 
  }
716
 
  return res;
717
 
}
718
 
 
719
 
 
720
 
void Item_func_lcase::fix_length_and_dec()
721
 
{
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;
726
 
}
727
 
 
728
 
void Item_func_ucase::fix_length_and_dec()
729
 
{
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;
734
 
}
735
 
 
736
 
 
737
 
String *Item_func_left::val_str(String *str)
738
 
{
739
 
  assert(fixed == 1);
740
 
  String *res= args[0]->val_str(str);
741
 
 
742
 
  /* must be int64_t to avoid truncation */
743
 
  int64_t length= args[1]->val_int();
744
 
  uint char_pos;
745
 
 
746
 
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
747
 
    return 0;
748
 
 
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;
752
 
 
753
 
  if ((res->length() <= (uint64_t) length) ||
754
 
      (res->length() <= (char_pos= res->charpos((int) length))))
755
 
    return res;
756
 
 
757
 
  tmp_value.set(*res, 0, char_pos);
758
 
  return &tmp_value;
759
 
}
760
 
 
761
 
 
762
 
void Item_str_func::left_right_max_length()
763
 
{
764
 
  max_length=args[0]->max_length;
765
 
  if (args[1]->const_item())
766
 
  {
767
 
    int length=(int) args[1]->val_int()*collation.collation->mbmaxlen;
768
 
    if (length <= 0)
769
 
      max_length=0;
770
 
    else
771
 
      set_if_smaller(max_length,(uint) length);
772
 
  }
773
 
}
774
 
 
775
 
 
776
 
void Item_func_left::fix_length_and_dec()
777
 
{
778
 
  collation.set(args[0]->collation);
779
 
  left_right_max_length();
780
 
}
781
 
 
782
 
 
783
 
String *Item_func_right::val_str(String *str)
784
 
{
785
 
  assert(fixed == 1);
786
 
  String *res= args[0]->val_str(str);
787
 
  /* must be int64_t to avoid truncation */
788
 
  int64_t length= args[1]->val_int();
789
 
 
790
 
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
791
 
    return 0; /* purecov: inspected */
792
 
 
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 */
796
 
 
797
 
  if (res->length() <= (uint64_t) length)
798
 
    return res; /* purecov: inspected */
799
 
 
800
 
  uint start=res->numchars();
801
 
  if (start <= (uint) length)
802
 
    return res;
803
 
  start=res->charpos(start - (uint) length);
804
 
  tmp_value.set(*res,start,res->length()-start);
805
 
  return &tmp_value;
806
 
}
807
 
 
808
 
 
809
 
void Item_func_right::fix_length_and_dec()
810
 
{
811
 
  collation.set(args[0]->collation);
812
 
  left_right_max_length();
813
 
}
814
 
 
815
 
 
816
 
String *Item_func_substr::val_str(String *str)
817
 
{
818
 
  assert(fixed == 1);
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;
825
 
  int64_t tmp_length;
826
 
 
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 */
830
 
 
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;
835
 
 
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))
839
 
    length= INT32_MAX;
840
 
 
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;
846
 
 
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;
851
 
 
852
 
  length= res->charpos((int) length, (uint32_t) start);
853
 
  tmp_length= res->length() - start;
854
 
  length= min(length, tmp_length);
855
 
 
856
 
  if (!start && (int64_t) res->length() == length)
857
 
    return res;
858
 
  tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
859
 
  return &tmp_value;
860
 
}
861
 
 
862
 
 
863
 
void Item_func_substr::fix_length_and_dec()
864
 
{
865
 
  max_length=args[0]->max_length;
866
 
 
867
 
  collation.set(args[0]->collation);
868
 
  if (args[1]->const_item())
869
 
  {
870
 
    int32_t start= (int32_t) args[1]->val_int();
871
 
    if (start < 0)
872
 
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
873
 
    else
874
 
      max_length-= min((uint)(start - 1), max_length);
875
 
  }
876
 
  if (arg_count == 3 && args[2]->const_item())
877
 
  {
878
 
    int32_t length= (int32_t) args[2]->val_int();
879
 
    if (length <= 0)
880
 
      max_length=0; /* purecov: inspected */
881
 
    else
882
 
      set_if_smaller(max_length,(uint) length);
883
 
  }
884
 
  max_length*= collation.collation->mbmaxlen;
885
 
}
886
 
 
887
 
 
888
 
void Item_func_substr_index::fix_length_and_dec()
889
 
890
 
  max_length= args[0]->max_length;
891
 
 
892
 
  if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
893
 
    return;
894
 
}
895
 
 
896
 
 
897
 
String *Item_func_substr_index::val_str(String *str)
898
 
{
899
 
  assert(fixed == 1);
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();
903
 
  uint offset;
904
 
 
905
 
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
906
 
  {                                     // string and/or delim are null
907
 
    null_value=1;
908
 
    return 0;
909
 
  }
910
 
  null_value=0;
911
 
  uint delimiter_length= delimiter->length();
912
 
  if (!res->length() || !delimiter_length || !count)
913
 
    return &my_empty_string;            // Wrong parameters
914
 
 
915
 
  res->set_charset(collation.collation);
916
 
 
917
 
#ifdef USE_MB
918
 
  if (use_mb(res->charset()))
919
 
  {
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;
926
 
    register uint32_t l;
927
 
    for (pass=(count>0);pass<2;++pass)
928
 
    {
929
 
      while (ptr < end)
930
 
      {
931
 
        if (*ptr == *search)
932
 
        {
933
 
          register char *i,*j;
934
 
          i=(char*) ptr+1; j=(char*) search+1;
935
 
          while (j != search_end)
936
 
            if (*i++ != *j++) goto skip;
937
 
          if (pass==0) ++n;
938
 
          else if (!--c) break;
939
 
          ptr+= delimiter_length;
940
 
          continue;
941
 
        }
942
 
    skip:
943
 
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
944
 
        else ++ptr;
945
 
      } /* either not found or got total number when count<0 */
946
 
      if (pass == 0) /* count<0 */
947
 
      {
948
 
        c+=n+1;
949
 
        if (c<=0) return res; /* not found, return original string */
950
 
        ptr=res->ptr();
951
 
      }
952
 
      else
953
 
      {
954
 
        if (c) return res; /* Not found, return original string */
955
 
        if (count>0) /* return left part */
956
 
        {
957
 
          tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
958
 
        }
959
 
        else /* return right part */
960
 
        {
961
 
          ptr+= delimiter_length;
962
 
          tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
963
 
        }
964
 
      }
965
 
    }
966
 
  }
967
 
  else
968
 
#endif /* USE_MB */
969
 
  {
970
 
    if (count > 0)
971
 
    {                                   // start counting from the beginning
972
 
      for (offset=0; ; offset+= delimiter_length)
973
 
      {
974
 
        if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
975
 
          return res;                   // Didn't find, return org string
976
 
        if (!--count)
977
 
        {
978
 
          tmp_value.set(*res,0,offset);
979
 
          break;
980
 
        }
981
 
      }
982
 
    }
983
 
    else
984
 
    {
985
 
      /*
986
 
        Negative index, start counting at the end
987
 
      */
988
 
      for (offset=res->length(); offset ;)
989
 
      {
990
 
        /* 
991
 
          this call will result in finding the position pointing to one 
992
 
          address space less than where the found substring is located
993
 
          in res
994
 
        */
995
 
        if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
996
 
          return res;                   // Didn't find, return org string
997
 
        /*
998
 
          At this point, we've searched for the substring
999
 
          the number of times as supplied by the index value
1000
 
        */
1001
 
        if (!++count)
1002
 
        {
1003
 
          offset+= delimiter_length;
1004
 
          tmp_value.set(*res,offset,res->length()- offset);
1005
 
          break;
1006
 
        }
1007
 
      }
1008
 
    }
1009
 
  }
1010
 
  /*
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.
1014
 
  */
1015
 
  tmp_value.mark_as_const();
1016
 
  return (&tmp_value);
1017
 
}
1018
 
 
1019
 
/*
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.
1024
 
*/
1025
 
 
1026
 
 
1027
 
String *Item_func_ltrim::val_str(String *str)
1028
 
{
1029
 
  assert(fixed == 1);
1030
 
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1031
 
  String tmp(buff,sizeof(buff),system_charset_info);
1032
 
  String *res, *remove_str;
1033
 
  uint remove_length;
1034
 
 
1035
 
  res= args[0]->val_str(str);
1036
 
  if ((null_value=args[0]->null_value))
1037
 
    return 0;
1038
 
  remove_str= &remove;                          /* Default value. */
1039
 
  if (arg_count == 2)
1040
 
  {
1041
 
    remove_str= args[1]->val_str(&tmp);
1042
 
    if ((null_value= args[1]->null_value))
1043
 
      return 0;
1044
 
  }
1045
 
 
1046
 
  if ((remove_length= remove_str->length()) == 0 ||
1047
 
      remove_length > res->length())
1048
 
    return res;
1049
 
 
1050
 
  ptr= (char*) res->ptr();
1051
 
  end= ptr+res->length();
1052
 
  if (remove_length == 1)
1053
 
  {
1054
 
    char chr=(*remove_str)[0];
1055
 
    while (ptr != end && *ptr == chr)
1056
 
      ptr++;
1057
 
  }
1058
 
  else
1059
 
  {
1060
 
    const char *r_ptr=remove_str->ptr();
1061
 
    end-=remove_length;
1062
 
    while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
1063
 
      ptr+=remove_length;
1064
 
    end+=remove_length;
1065
 
  }
1066
 
  if (ptr == res->ptr())
1067
 
    return res;
1068
 
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1069
 
  return &tmp_value;
1070
 
}
1071
 
 
1072
 
 
1073
 
String *Item_func_rtrim::val_str(String *str)
1074
 
{
1075
 
  assert(fixed == 1);
1076
 
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1077
 
  String tmp(buff, sizeof(buff), system_charset_info);
1078
 
  String *res, *remove_str;
1079
 
  uint remove_length;
1080
 
 
1081
 
  res= args[0]->val_str(str);
1082
 
  if ((null_value=args[0]->null_value))
1083
 
    return 0;
1084
 
  remove_str= &remove;                          /* Default value. */
1085
 
  if (arg_count == 2)
1086
 
  {
1087
 
    remove_str= args[1]->val_str(&tmp);
1088
 
    if ((null_value= args[1]->null_value))
1089
 
      return 0;
1090
 
  }
1091
 
 
1092
 
  if ((remove_length= remove_str->length()) == 0 ||
1093
 
      remove_length > res->length())
1094
 
    return res;
1095
 
 
1096
 
  ptr= (char*) res->ptr();
1097
 
  end= ptr+res->length();
1098
 
#ifdef USE_MB
1099
 
  char *p=ptr;
1100
 
  register uint32_t l;
1101
 
#endif
1102
 
  if (remove_length == 1)
1103
 
  {
1104
 
    char chr=(*remove_str)[0];
1105
 
#ifdef USE_MB
1106
 
    if (use_mb(res->charset()))
1107
 
    {
1108
 
      while (ptr < end)
1109
 
      {
1110
 
        if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
1111
 
        else ++ptr;
1112
 
      }
1113
 
      ptr=p;
1114
 
    }
1115
 
#endif
1116
 
    while (ptr != end  && end[-1] == chr)
1117
 
      end--;
1118
 
  }
1119
 
  else
1120
 
  {
1121
 
    const char *r_ptr=remove_str->ptr();
1122
 
#ifdef USE_MB
1123
 
    if (use_mb(res->charset()))
1124
 
    {
1125
 
  loop:
1126
 
      while (ptr + remove_length < end)
1127
 
      {
1128
 
        if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1129
 
        else ++ptr;
1130
 
      }
1131
 
      if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1132
 
      {
1133
 
        end-=remove_length;
1134
 
        ptr=p;
1135
 
        goto loop;
1136
 
      }
1137
 
    }
1138
 
    else
1139
 
#endif /* USE_MB */
1140
 
    {
1141
 
      while (ptr + remove_length <= end &&
1142
 
             !memcmp(end-remove_length, r_ptr, remove_length))
1143
 
        end-=remove_length;
1144
 
    }
1145
 
  }
1146
 
  if (end == res->ptr()+res->length())
1147
 
    return res;
1148
 
  tmp_value.set(*res,0,(uint) (end-res->ptr()));
1149
 
  return &tmp_value;
1150
 
}
1151
 
 
1152
 
 
1153
 
String *Item_func_trim::val_str(String *str)
1154
 
{
1155
 
  assert(fixed == 1);
1156
 
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1157
 
  const char *r_ptr;
1158
 
  String tmp(buff, sizeof(buff), system_charset_info);
1159
 
  String *res, *remove_str;
1160
 
  uint remove_length;
1161
 
 
1162
 
  res= args[0]->val_str(str);
1163
 
  if ((null_value=args[0]->null_value))
1164
 
    return 0;
1165
 
  remove_str= &remove;                          /* Default value. */
1166
 
  if (arg_count == 2)
1167
 
  {
1168
 
    remove_str= args[1]->val_str(&tmp);
1169
 
    if ((null_value= args[1]->null_value))
1170
 
      return 0;
1171
 
  }
1172
 
 
1173
 
  if ((remove_length= remove_str->length()) == 0 ||
1174
 
      remove_length > res->length())
1175
 
    return res;
1176
 
 
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))
1181
 
    ptr+=remove_length;
1182
 
#ifdef USE_MB
1183
 
  if (use_mb(res->charset()))
1184
 
  {
1185
 
    char *p=ptr;
1186
 
    register uint32_t l;
1187
 
 loop:
1188
 
    while (ptr + remove_length < end)
1189
 
    {
1190
 
      if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
1191
 
      else ++ptr;
1192
 
    }
1193
 
    if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
1194
 
    {
1195
 
      end-=remove_length;
1196
 
      ptr=p;
1197
 
      goto loop;
1198
 
    }
1199
 
    ptr=p;
1200
 
  }
1201
 
  else
1202
 
#endif /* USE_MB */
1203
 
  {
1204
 
    while (ptr + remove_length <= end &&
1205
 
           !memcmp(end-remove_length,r_ptr,remove_length))
1206
 
      end-=remove_length;
1207
 
  }
1208
 
  if (ptr == res->ptr() && end == ptr+res->length())
1209
 
    return res;
1210
 
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
1211
 
  return &tmp_value;
1212
 
}
1213
 
 
1214
 
void Item_func_trim::fix_length_and_dec()
1215
 
{
1216
 
  max_length= args[0]->max_length;
1217
 
  if (arg_count == 1)
1218
 
  {
1219
 
    collation.set(args[0]->collation);
1220
 
    remove.set_charset(collation.collation);
1221
 
    remove.set_ascii(" ",1);
1222
 
  }
1223
 
  else
1224
 
  {
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))
1228
 
      return;
1229
 
  }
1230
 
}
1231
 
 
1232
 
void Item_func_trim::print(String *str, enum_query_type query_type)
1233
 
{
1234
 
  if (arg_count == 1)
1235
 
  {
1236
 
    Item_func::print(str, query_type);
1237
 
    return;
1238
 
  }
1239
 
  str->append(Item_func_trim::func_name());
1240
 
  str->append('(');
1241
 
  str->append(mode_name());
1242
 
  str->append(' ');
1243
 
  args[1]->print(str, query_type);
1244
 
  str->append(STRING_WITH_LEN(" from "));
1245
 
  args[0]->print(str, query_type);
1246
 
  str->append(')');
1247
 
}
1248
 
 
1249
 
 
1250
 
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
1251
 
{
1252
 
  Item_string *conv;
1253
 
  uint conv_errors;
1254
 
  String tmp, cstr, *ostr= val_str(&tmp);
1255
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1256
 
  if (conv_errors ||
1257
 
      !(conv= new Item_static_string_func(fully_qualified_func_name(),
1258
 
                                          cstr.ptr(), cstr.length(),
1259
 
                                          cstr.charset(),
1260
 
                                          collation.derivation)))
1261
 
  {
1262
 
    return NULL;
1263
 
  }
1264
 
  conv->str_value.copy();
1265
 
  conv->str_value.mark_as_const();
1266
 
  return conv;
1267
 
}
1268
 
 
1269
 
 
1270
 
String *Item_func_database::val_str(String *str)
1271
 
{
1272
 
  assert(fixed == 1);
1273
 
  THD *thd= current_thd;
1274
 
  if (thd->db == NULL)
1275
 
  {
1276
 
    null_value= 1;
1277
 
    return 0;
1278
 
  }
1279
 
  else
1280
 
    str->copy(thd->db, thd->db_length, system_charset_info);
1281
 
  return str;
1282
 
}
1283
 
 
1284
 
 
1285
 
/**
1286
 
  @todo
1287
 
  make USER() replicate properly (currently it is replicated to "")
1288
 
*/
1289
 
bool Item_func_user::init(const char *user, const char *host)
1290
 
{
1291
 
  assert(fixed == 1);
1292
 
 
1293
 
  // For system threads (e.g. replication SQL thread) user may be empty
1294
 
  if (user)
1295
 
  {
1296
 
    const CHARSET_INFO * const cs= str_value.charset();
1297
 
    uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1298
 
 
1299
 
    if (str_value.alloc(res_length))
1300
 
    {
1301
 
      null_value=1;
1302
 
      return true;
1303
 
    }
1304
 
 
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();
1309
 
  }
1310
 
  return false;
1311
 
}
1312
 
 
1313
 
 
1314
 
bool Item_func_user::fix_fields(THD *thd, Item **ref)
1315
 
{
1316
 
  return (Item_func_sysconst::fix_fields(thd, ref) ||
1317
 
          init(thd->main_security_ctx.user,
1318
 
               thd->main_security_ctx.ip));
1319
 
}
1320
 
 
1321
 
 
1322
 
bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
1323
 
{
1324
 
  if (Item_func_sysconst::fix_fields(thd, ref))
1325
 
    return true;
1326
 
 
1327
 
  Security_context *ctx=
1328
 
                         thd->security_ctx;
1329
 
  return init(ctx->user, ctx->ip);
1330
 
}
1331
 
 
1332
 
 
1333
 
/**
1334
 
  Change a number to format '3,333,333,333.000'.
1335
 
 
1336
 
  This should be 'internationalized' sometimes.
1337
 
*/
1338
 
 
1339
 
const int FORMAT_MAX_DECIMALS= 30;
1340
 
 
1341
 
Item_func_format::Item_func_format(Item *org, Item *dec)
1342
 
: Item_str_func(org, dec)
1343
 
{
1344
 
}
1345
 
 
1346
 
void Item_func_format::fix_length_and_dec()
1347
 
{
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);
1352
 
}
1353
 
 
1354
 
 
1355
 
/**
1356
 
  @todo
1357
 
  This needs to be fixed for multi-byte character set where numbers
1358
 
  are stored in more than one byte
1359
 
*/
1360
 
 
1361
 
String *Item_func_format::val_str(String *str)
1362
 
{
1363
 
  uint32_t length;
1364
 
  uint32_t str_length;
1365
 
  /* Number of decimal digits */
1366
 
  int dec;
1367
 
  /* Number of characters used to represent the decimals, including '.' */
1368
 
  uint32_t dec_length;
1369
 
  int diff;
1370
 
  assert(fixed == 1);
1371
 
 
1372
 
  dec= (int) args[1]->val_int();
1373
 
  if (args[1]->null_value)
1374
 
  {
1375
 
    null_value=1;
1376
 
    return NULL;
1377
 
  }
1378
 
 
1379
 
  dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
1380
 
  dec_length= dec ? dec+1 : 0;
1381
 
  null_value=0;
1382
 
 
1383
 
  if (args[0]->result_type() == DECIMAL_RESULT ||
1384
 
      args[0]->result_type() == INT_RESULT)
1385
 
  {
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();
1393
 
    if (rnd_dec.sign())
1394
 
      str_length--;
1395
 
  }
1396
 
  else
1397
 
  {
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());
1404
 
    if (isnan(nr))
1405
 
      return str;
1406
 
    str_length=str->length();
1407
 
    if (nr < 0)
1408
 
      str_length--;                             // Don't count sign
1409
 
  }
1410
 
  /* We need this test to handle 'nan' values */
1411
 
  if (str_length >= dec_length+4)
1412
 
  {
1413
 
    char *tmp,*pos;
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--)
1419
 
      pos[0]= pos[-diff];
1420
 
    while (diff)
1421
 
    {
1422
 
      *pos= *(pos - diff);
1423
 
      pos--;
1424
 
      *pos= *(pos - diff);
1425
 
      pos--;
1426
 
      *pos= *(pos - diff);
1427
 
      pos--;
1428
 
      pos[0]=',';
1429
 
      pos--;
1430
 
      diff--;
1431
 
    }
1432
 
  }
1433
 
  return str;
1434
 
}
1435
 
 
1436
 
 
1437
 
void Item_func_format::print(String *str, enum_query_type query_type)
1438
 
{
1439
 
  str->append(STRING_WITH_LEN("format("));
1440
 
  args[0]->print(str, query_type);
1441
 
  str->append(',');
1442
 
  args[1]->print(str, query_type);
1443
 
  str->append(')');
1444
 
}
1445
 
 
1446
 
void Item_func_elt::fix_length_and_dec()
1447
 
{
1448
 
  max_length=0;
1449
 
  decimals=0;
1450
 
 
1451
 
  if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1452
 
    return;
1453
 
 
1454
 
  for (uint i= 1 ; i < arg_count ; i++)
1455
 
  {
1456
 
    set_if_bigger(max_length,args[i]->max_length);
1457
 
    set_if_bigger(decimals,args[i]->decimals);
1458
 
  }
1459
 
  maybe_null=1;                                 // NULL if wrong first arg
1460
 
}
1461
 
 
1462
 
 
1463
 
double Item_func_elt::val_real()
1464
 
{
1465
 
  assert(fixed == 1);
1466
 
  uint tmp;
1467
 
  null_value=1;
1468
 
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1469
 
    return 0.0;
1470
 
  double result= args[tmp]->val_real();
1471
 
  null_value= args[tmp]->null_value;
1472
 
  return result;
1473
 
}
1474
 
 
1475
 
 
1476
 
int64_t Item_func_elt::val_int()
1477
 
{
1478
 
  assert(fixed == 1);
1479
 
  uint tmp;
1480
 
  null_value=1;
1481
 
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1482
 
    return 0;
1483
 
 
1484
 
  int64_t result= args[tmp]->val_int();
1485
 
  null_value= args[tmp]->null_value;
1486
 
  return result;
1487
 
}
1488
 
 
1489
 
 
1490
 
String *Item_func_elt::val_str(String *str)
1491
 
{
1492
 
  assert(fixed == 1);
1493
 
  uint tmp;
1494
 
  null_value=1;
1495
 
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1496
 
    return NULL;
1497
 
 
1498
 
  String *result= args[tmp]->val_str(str);
1499
 
  if (result)
1500
 
    result->set_charset(collation.collation);
1501
 
  null_value= args[tmp]->null_value;
1502
 
  return result;
1503
 
}
1504
 
 
1505
 
 
1506
 
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
1507
 
                                        List<Item> &fields)
1508
 
{
1509
 
  item->split_sum_func2(thd, ref_pointer_array, fields, &item, true);
1510
 
  Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
1511
 
}
1512
 
 
1513
 
 
1514
 
void Item_func_make_set::fix_length_and_dec()
1515
 
{
1516
 
  max_length=arg_count-1;
1517
 
 
1518
 
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1519
 
    return;
1520
 
  
1521
 
  for (uint i=0 ; i < arg_count ; i++)
1522
 
    max_length+=args[i]->max_length;
1523
 
 
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;
1528
 
}
1529
 
 
1530
 
 
1531
 
void Item_func_make_set::update_used_tables()
1532
 
{
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();
1537
 
}
1538
 
 
1539
 
 
1540
 
String *Item_func_make_set::val_str(String *str)
1541
 
{
1542
 
  assert(fixed == 1);
1543
 
  uint64_t bits;
1544
 
  bool first_found=0;
1545
 
  Item **ptr=args;
1546
 
  String *result=&my_empty_string;
1547
 
 
1548
 
  bits=item->val_int();
1549
 
  if ((null_value=item->null_value))
1550
 
    return NULL;
1551
 
 
1552
 
  if (arg_count < 64)
1553
 
    bits &= ((uint64_t) 1 << arg_count)-1;
1554
 
 
1555
 
  for (; bits; bits >>= 1, ptr++)
1556
 
  {
1557
 
    if (bits & 1)
1558
 
    {
1559
 
      String *res= (*ptr)->val_str(str);
1560
 
      if (res)                                  // Skip nulls
1561
 
      {
1562
 
        if (!first_found)
1563
 
        {                                       // First argument
1564
 
          first_found=1;
1565
 
          if (res != str)
1566
 
            result=res;                         // Use original string
1567
 
          else
1568
 
          {
1569
 
            if (tmp_str.copy(*res))             // Don't use 'str'
1570
 
              return &my_empty_string;
1571
 
            result= &tmp_str;
1572
 
          }
1573
 
        }
1574
 
        else
1575
 
        {
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;
1581
 
            result= &tmp_str;
1582
 
          }
1583
 
          if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
1584
 
            return &my_empty_string;
1585
 
        }
1586
 
      }
1587
 
    }
1588
 
  }
1589
 
  return result;
1590
 
}
1591
 
 
1592
 
 
1593
 
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
1594
 
{
1595
 
  Item *new_item= item->transform(transformer, arg);
1596
 
  if (!new_item)
1597
 
    return 0;
1598
 
 
1599
 
  /*
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.
1604
 
  */
1605
 
  if (item != new_item)
1606
 
    current_thd->change_item_tree(&item, new_item);
1607
 
  return Item_str_func::transform(transformer, arg);
1608
 
}
1609
 
 
1610
 
 
1611
 
void Item_func_make_set::print(String *str, enum_query_type query_type)
1612
 
{
1613
 
  str->append(STRING_WITH_LEN("make_set("));
1614
 
  item->print(str, query_type);
1615
 
  if (arg_count)
1616
 
  {
1617
 
    str->append(',');
1618
 
    print_args(str, 0, query_type);
1619
 
  }
1620
 
  str->append(')');
1621
 
}
1622
 
 
1623
 
 
1624
 
String *Item_func_char::val_str(String *str)
1625
 
{
1626
 
  assert(fixed == 1);
1627
 
  str->length(0);
1628
 
  str->set_charset(collation.collation);
1629
 
  for (uint i=0 ; i < arg_count ; i++)
1630
 
  {
1631
 
    int32_t num=(int32_t) args[i]->val_int();
1632
 
    if (!args[i]->null_value)
1633
 
    {
1634
 
      char char_num= (char) num;
1635
 
      if (num&0xFF000000L) {
1636
 
        str->append((char)(num>>24));
1637
 
        goto b2;
1638
 
      } else if (num&0xFF0000L) {
1639
 
    b2:        str->append((char)(num>>16));
1640
 
        goto b1;
1641
 
      } else if (num&0xFF00L) {
1642
 
    b1:        str->append((char)(num>>8));
1643
 
      }
1644
 
      str->append(&char_num, 1);
1645
 
    }
1646
 
  }
1647
 
  str->realloc(str->length());                  // Add end 0 (for Purify)
1648
 
  return check_well_formed_result(str);
1649
 
}
1650
 
 
1651
 
 
1652
 
inline String* alloc_buffer(String *res,String *str,String *tmp_value,
1653
 
                            ulong length)
1654
 
{
1655
 
  if (res->alloced_length() < length)
1656
 
  {
1657
 
    if (str->alloced_length() >= length)
1658
 
    {
1659
 
      (void) str->copy(*res);
1660
 
      str->length(length);
1661
 
      return str;
1662
 
    }
1663
 
    if (tmp_value->alloc(length))
1664
 
      return 0;
1665
 
    (void) tmp_value->copy(*res);
1666
 
    tmp_value->length(length);
1667
 
    return tmp_value;
1668
 
  }
1669
 
  res->length(length);
1670
 
  return res;
1671
 
}
1672
 
 
1673
 
 
1674
 
void Item_func_repeat::fix_length_and_dec()
1675
 
{
1676
 
  collation.set(args[0]->collation);
1677
 
  if (args[1]->const_item())
1678
 
  {
1679
 
    /* must be int64_t to avoid truncation */
1680
 
    int64_t count= args[1]->val_int();
1681
 
 
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)
1685
 
      count= INT32_MAX;
1686
 
 
1687
 
    uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
1688
 
    if (max_result_length >= MAX_BLOB_WIDTH)
1689
 
    {
1690
 
      max_result_length= MAX_BLOB_WIDTH;
1691
 
      maybe_null= 1;
1692
 
    }
1693
 
    max_length= (ulong) max_result_length;
1694
 
  }
1695
 
  else
1696
 
  {
1697
 
    max_length= MAX_BLOB_WIDTH;
1698
 
    maybe_null= 1;
1699
 
  }
1700
 
}
1701
 
 
1702
 
/**
1703
 
  Item_func_repeat::str is carefully written to avoid reallocs
1704
 
  as much as possible at the cost of a local buffer
1705
 
*/
1706
 
 
1707
 
String *Item_func_repeat::val_str(String *str)
1708
 
{
1709
 
  assert(fixed == 1);
1710
 
  uint length,tot_length;
1711
 
  char *to;
1712
 
  /* must be int64_t to avoid truncation */
1713
 
  int64_t count= args[1]->val_int();
1714
 
  String *res= args[0]->val_str(str);
1715
 
 
1716
 
  if (args[0]->null_value || args[1]->null_value)
1717
 
    goto err;                           // string and/or delim are null
1718
 
  null_value= 0;
1719
 
 
1720
 
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
1721
 
    return &my_empty_string;
1722
 
 
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)
1726
 
    count= INT32_MAX;
1727
 
  if (count == 1)                       // To avoid reallocs
1728
 
    return res;
1729
 
  length=res->length();
1730
 
  // Safe length check
1731
 
  if (length > current_thd->variables.max_allowed_packet / (uint) count)
1732
 
  {
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);
1737
 
    goto err;
1738
 
  }
1739
 
  tot_length= length*(uint) count;
1740
 
  if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
1741
 
    goto err;
1742
 
 
1743
 
  to=(char*) res->ptr()+length;
1744
 
  while (--count)
1745
 
  {
1746
 
    memcpy(to,res->ptr(),length);
1747
 
    to+=length;
1748
 
  }
1749
 
  return (res);
1750
 
 
1751
 
err:
1752
 
  null_value=1;
1753
 
  return 0;
1754
 
}
1755
 
 
1756
 
 
1757
 
void Item_func_rpad::fix_length_and_dec()
1758
 
{
1759
 
  // Handle character set for args[0] and args[2].
1760
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1761
 
    return;
1762
 
  if (args[1]->const_item())
1763
 
  {
1764
 
    uint64_t length= 0;
1765
 
 
1766
 
    if (collation.collation->mbmaxlen > 0)
1767
 
    {
1768
 
      uint64_t temp= (uint64_t) args[1]->val_int();
1769
 
 
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)
1773
 
        temp = INT32_MAX;
1774
 
 
1775
 
      length= temp * collation.collation->mbmaxlen;
1776
 
    }
1777
 
 
1778
 
    if (length >= MAX_BLOB_WIDTH)
1779
 
    {
1780
 
      length= MAX_BLOB_WIDTH;
1781
 
      maybe_null= 1;
1782
 
    }
1783
 
    max_length= (ulong) length;
1784
 
  }
1785
 
  else
1786
 
  {
1787
 
    max_length= MAX_BLOB_WIDTH;
1788
 
    maybe_null= 1;
1789
 
  }
1790
 
}
1791
 
 
1792
 
 
1793
 
String *Item_func_rpad::val_str(String *str)
1794
 
{
1795
 
  assert(fixed == 1);
1796
 
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
1797
 
  char *to;
1798
 
  const char *ptr_pad;
1799
 
  /* must be int64_t to avoid truncation */
1800
 
  int64_t count= args[1]->val_int();
1801
 
  int64_t byte_count;
1802
 
  String *res= args[0]->val_str(str);
1803
 
  String *rpad= args[2]->val_str(&rpad_str);
1804
 
 
1805
 
  if (!res || args[1]->null_value || !rpad || 
1806
 
      ((count < 0) && !args[1]->unsigned_flag))
1807
 
    goto err;
1808
 
  null_value=0;
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)
1812
 
    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
1816
 
    return (res);
1817
 
  }
1818
 
  pad_char_length= rpad->numchars();
1819
 
 
1820
 
  byte_count= count * collation.collation->mbmaxlen;
1821
 
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1822
 
  {
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);
1827
 
    goto err;
1828
 
  }
1829
 
  if (args[2]->null_value || !pad_char_length)
1830
 
    goto err;
1831
 
  res_byte_length= res->length();       /* Must be done before alloc_buffer */
1832
 
  if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
1833
 
    goto err;
1834
 
 
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)
1840
 
  {
1841
 
    memcpy(to,ptr_pad,pad_byte_length);
1842
 
    to+= pad_byte_length;
1843
 
  }
1844
 
  if (count)
1845
 
  {
1846
 
    pad_byte_length= rpad->charpos((int) count);
1847
 
    memcpy(to,ptr_pad,(size_t) pad_byte_length);
1848
 
    to+= pad_byte_length;
1849
 
  }
1850
 
  res->length(to- (char*) res->ptr());
1851
 
  return (res);
1852
 
 
1853
 
 err:
1854
 
  null_value=1;
1855
 
  return 0;
1856
 
}
1857
 
 
1858
 
 
1859
 
void Item_func_lpad::fix_length_and_dec()
1860
 
{
1861
 
  // Handle character set for args[0] and args[2].
1862
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
1863
 
    return;
1864
 
  
1865
 
  if (args[1]->const_item())
1866
 
  {
1867
 
    uint64_t length= 0;
1868
 
 
1869
 
    if (collation.collation->mbmaxlen > 0)
1870
 
    {
1871
 
      uint64_t temp= (uint64_t) args[1]->val_int();
1872
 
 
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)
1876
 
        temp= INT32_MAX;
1877
 
 
1878
 
      length= temp * collation.collation->mbmaxlen;
1879
 
    }
1880
 
 
1881
 
    if (length >= MAX_BLOB_WIDTH)
1882
 
    {
1883
 
      length= MAX_BLOB_WIDTH;
1884
 
      maybe_null= 1;
1885
 
    }
1886
 
    max_length= (ulong) length;
1887
 
  }
1888
 
  else
1889
 
  {
1890
 
    max_length= MAX_BLOB_WIDTH;
1891
 
    maybe_null= 1;
1892
 
  }
1893
 
}
1894
 
 
1895
 
 
1896
 
String *Item_func_lpad::val_str(String *str)
1897
 
{
1898
 
  assert(fixed == 1);
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();
1902
 
  int64_t byte_count;
1903
 
  String *res= args[0]->val_str(&tmp_value);
1904
 
  String *pad= args[2]->val_str(&lpad_str);
1905
 
 
1906
 
  if (!res || args[1]->null_value || !pad ||  
1907
 
      ((count < 0) && !args[1]->unsigned_flag))
1908
 
    goto err;  
1909
 
  null_value=0;
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)
1913
 
    count= INT32_MAX;
1914
 
 
1915
 
  res_char_length= res->numchars();
1916
 
 
1917
 
  if (count <= res_char_length)
1918
 
  {
1919
 
    res->length(res->charpos((int) count));
1920
 
    return res;
1921
 
  }
1922
 
  
1923
 
  pad_char_length= pad->numchars();
1924
 
  byte_count= count * collation.collation->mbmaxlen;
1925
 
  
1926
 
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1927
 
  {
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);
1932
 
    goto err;
1933
 
  }
1934
 
 
1935
 
  if (args[2]->null_value || !pad_char_length ||
1936
 
      str->alloc((uint32_t) byte_count))
1937
 
    goto err;
1938
 
  
1939
 
  str->length(0);
1940
 
  str->set_charset(collation.collation);
1941
 
  count-= res_char_length;
1942
 
  while (count >= pad_char_length)
1943
 
  {
1944
 
    str->append(*pad);
1945
 
    count-= pad_char_length;
1946
 
  }
1947
 
  if (count > 0)
1948
 
    str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
1949
 
 
1950
 
  str->append(*res);
1951
 
  null_value= 0;
1952
 
  return str;
1953
 
 
1954
 
err:
1955
 
  null_value= 1;
1956
 
  return 0;
1957
 
}
1958
 
 
1959
 
 
1960
 
String *Item_func_conv::val_str(String *str)
1961
 
{
1962
 
  assert(fixed == 1);
1963
 
  String *res= args[0]->val_str(str);
1964
 
  char *endptr,ans[65],*ptr;
1965
 
  int64_t dec;
1966
 
  int from_base= (int) args[1]->val_int();
1967
 
  int to_base= (int) args[2]->val_int();
1968
 
  int err;
1969
 
 
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()))
1973
 
  {
1974
 
    null_value= 1;
1975
 
    return NULL;
1976
 
  }
1977
 
  null_value= 0;
1978
 
  unsigned_flag= !(from_base < 0);
1979
 
 
1980
 
  if (from_base < 0)
1981
 
    dec= my_strntoll(res->charset(), res->ptr(), res->length(),
1982
 
                     -from_base, &endptr, &err);
1983
 
  else
1984
 
    dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
1985
 
                                 from_base, &endptr, &err);
1986
 
 
1987
 
  ptr= int64_t2str(dec, ans, to_base);
1988
 
  if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
1989
 
    return &my_empty_string;
1990
 
  return str;
1991
 
}
1992
 
 
1993
 
 
1994
 
String *Item_func_conv_charset::val_str(String *str)
1995
 
{
1996
 
  assert(fixed == 1);
1997
 
  if (use_cached_value)
1998
 
    return null_value ? 0 : &str_value;
1999
 
  String *arg= args[0]->val_str(str);
2000
 
  uint dummy_errors;
2001
 
  if (!arg)
2002
 
  {
2003
 
    null_value=1;
2004
 
    return 0;
2005
 
  }
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);
2009
 
}
2010
 
 
2011
 
void Item_func_conv_charset::fix_length_and_dec()
2012
 
{
2013
 
  collation.set(conv_charset, DERIVATION_IMPLICIT);
2014
 
  max_length = args[0]->max_length*conv_charset->mbmaxlen;
2015
 
}
2016
 
 
2017
 
void Item_func_conv_charset::print(String *str, enum_query_type query_type)
2018
 
{
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);
2023
 
  str->append(')');
2024
 
}
2025
 
 
2026
 
String *Item_func_set_collation::val_str(String *str)
2027
 
{
2028
 
  assert(fixed == 1);
2029
 
  str=args[0]->val_str(str);
2030
 
  if ((null_value=args[0]->null_value))
2031
 
    return 0;
2032
 
  str->set_charset(collation.collation);
2033
 
  return str;
2034
 
}
2035
 
 
2036
 
void Item_func_set_collation::fix_length_and_dec()
2037
 
{
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));
2045
 
  else
2046
 
  {
2047
 
    if (!(set_collation= get_charset_by_name(colname,MYF(0))))
2048
 
    {
2049
 
      my_error(ER_UNKNOWN_COLLATION, MYF(0), colname);
2050
 
      return;
2051
 
    }
2052
 
  }
2053
 
 
2054
 
  if (!set_collation || 
2055
 
      !my_charset_same(args[0]->collation.collation,set_collation))
2056
 
  {
2057
 
    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
2058
 
             colname, args[0]->collation.collation->csname);
2059
 
    return;
2060
 
  }
2061
 
  collation.set(set_collation, DERIVATION_EXPLICIT,
2062
 
                args[0]->collation.repertoire);
2063
 
  max_length= args[0]->max_length;
2064
 
}
2065
 
 
2066
 
 
2067
 
bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const
2068
 
{
2069
 
  /* Assume we don't have rtti */
2070
 
  if (this == item)
2071
 
    return 1;
2072
 
  if (item->type() != FUNC_ITEM)
2073
 
    return 0;
2074
 
  Item_func *item_func=(Item_func*) item;
2075
 
  if (arg_count != item_func->arg_count ||
2076
 
      functype() != item_func->functype())
2077
 
    return 0;
2078
 
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2079
 
  if (collation.collation != item_func_sc->collation.collation)
2080
 
    return 0;
2081
 
  for (uint i=0; i < arg_count ; i++)
2082
 
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2083
 
      return 0;
2084
 
  return 1;
2085
 
}
2086
 
 
2087
 
 
2088
 
void Item_func_set_collation::print(String *str, enum_query_type query_type)
2089
 
{
2090
 
  str->append('(');
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);
2096
 
  str->append(')');
2097
 
}
2098
 
 
2099
 
String *Item_func_charset::val_str(String *str)
2100
 
{
2101
 
  assert(fixed == 1);
2102
 
  uint dummy_errors;
2103
 
 
2104
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2105
 
  null_value= 0;
2106
 
  str->copy(cs->csname, strlen(cs->csname),
2107
 
            &my_charset_latin1, collation.collation, &dummy_errors);
2108
 
  return str;
2109
 
}
2110
 
 
2111
 
String *Item_func_collation::val_str(String *str)
2112
 
{
2113
 
  assert(fixed == 1);
2114
 
  uint dummy_errors;
2115
 
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2116
 
 
2117
 
  null_value= 0;
2118
 
  str->copy(cs->name, strlen(cs->name),
2119
 
            &my_charset_latin1, collation.collation, &dummy_errors);
2120
 
  return str;
2121
 
}
2122
 
 
2123
 
 
2124
 
void Item_func_weight_string::fix_length_and_dec()
2125
 
{
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);
2130
 
  maybe_null= 1;
2131
 
}
2132
 
 
2133
 
 
2134
 
/* Return a weight_string according to collation */
2135
 
String *Item_func_weight_string::val_str(String *str)
2136
 
{
2137
 
  String *res;
2138
 
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2139
 
  uint tmp_length, frm_length;
2140
 
  assert(fixed == 1);
2141
 
 
2142
 
  if (args[0]->result_type() != STRING_RESULT ||
2143
 
      !(res= args[0]->val_str(str)))
2144
 
    goto nl;
2145
 
  
2146
 
  tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2147
 
                                        max(res->length(), nweights));
2148
 
 
2149
 
  if (tmp_value.alloc(tmp_length))
2150
 
    goto nl;
2151
 
 
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(),
2156
 
                                 flags);
2157
 
  tmp_value.length(frm_length);
2158
 
  null_value= 0;
2159
 
  return &tmp_value;
2160
 
 
2161
 
nl:
2162
 
  null_value= 1;
2163
 
  return 0;
2164
 
}
2165
 
 
2166
 
 
2167
 
String *Item_func_hex::val_str(String *str)
2168
 
{
2169
 
  String *res;
2170
 
  assert(fixed == 1);
2171
 
  if (args[0]->result_type() != STRING_RESULT)
2172
 
  {
2173
 
    uint64_t dec;
2174
 
    char ans[65],*ptr;
2175
 
    /* Return hex of unsigned int64_t value */
2176
 
    if (args[0]->result_type() == REAL_RESULT ||
2177
 
        args[0]->result_type() == DECIMAL_RESULT)
2178
 
    {
2179
 
      double val= args[0]->val_real();
2180
 
      if ((val <= (double) INT64_MIN) || 
2181
 
          (val >= (double) (uint64_t) UINT64_MAX))
2182
 
        dec=  ~(int64_t) 0;
2183
 
      else
2184
 
        dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
2185
 
    }
2186
 
    else
2187
 
      dec= (uint64_t) args[0]->val_int();
2188
 
 
2189
 
    if ((null_value= args[0]->null_value))
2190
 
      return 0;
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
2194
 
    return str;
2195
 
  }
2196
 
 
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))
2200
 
  {
2201
 
    null_value=1;
2202
 
    return 0;
2203
 
  }
2204
 
  null_value=0;
2205
 
  tmp_value.length(res->length()*2);
2206
 
 
2207
 
  octet2hex((char*) tmp_value.ptr(), res->ptr(), res->length());
2208
 
  return &tmp_value;
2209
 
}
2210
 
 
2211
 
  /** Convert given hex string to a binary string. */
2212
 
 
2213
 
String *Item_func_unhex::val_str(String *str)
2214
 
{
2215
 
  const char *from, *end;
2216
 
  char *to;
2217
 
  String *res;
2218
 
  uint length;
2219
 
  assert(fixed == 1);
2220
 
 
2221
 
  res= args[0]->val_str(str);
2222
 
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
2223
 
  {
2224
 
    null_value=1;
2225
 
    return 0;
2226
 
  }
2227
 
 
2228
 
  from= res->ptr();
2229
 
  null_value= 0;
2230
 
  tmp_value.length(length);
2231
 
  to= (char*) tmp_value.ptr();
2232
 
  if (res->length() % 2)
2233
 
  {
2234
 
    int hex_char;
2235
 
    *to++= hex_char= hexchar_to_int(*from++);
2236
 
    if ((null_value= (hex_char == -1)))
2237
 
      return 0;
2238
 
  }
2239
 
  for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
2240
 
  {
2241
 
    int hex_char;
2242
 
    *to= (hex_char= hexchar_to_int(from[0])) << 4;
2243
 
    if ((null_value= (hex_char == -1)))
2244
 
      return 0;
2245
 
    *to|= hex_char= hexchar_to_int(from[1]);
2246
 
    if ((null_value= (hex_char == -1)))
2247
 
      return 0;
2248
 
  }
2249
 
  return &tmp_value;
2250
 
}
2251
 
 
2252
 
 
2253
 
void Item_func_binary::print(String *str, enum_query_type query_type)
2254
 
{
2255
 
  str->append(STRING_WITH_LEN("cast("));
2256
 
  args[0]->print(str, query_type);
2257
 
  str->append(STRING_WITH_LEN(" as binary)"));
2258
 
}
2259
 
 
2260
 
 
2261
 
String *Item_load_file::val_str(String *str)
2262
 
{
2263
 
  assert(fixed == 1);
2264
 
  String *file_name;
2265
 
  File file;
2266
 
  struct stat stat_info;
2267
 
  char path[FN_REFLEN];
2268
 
 
2269
 
  if (!(file_name= args[0]->val_str(str)))
2270
 
    goto err;
2271
 
 
2272
 
  (void) fn_format(path, file_name->c_ptr(), mysql_real_data_home, "",
2273
 
                   MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
2274
 
 
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)))
2278
 
    goto err;
2279
 
 
2280
 
  if (stat(path, &stat_info))
2281
 
    goto err;
2282
 
 
2283
 
  if (!(stat_info.st_mode & S_IROTH))
2284
 
  {
2285
 
    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
2286
 
    goto err;
2287
 
  }
2288
 
  if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2289
 
  {
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);
2294
 
    goto err;
2295
 
  }
2296
 
  if (tmp_value.alloc(stat_info.st_size))
2297
 
    goto err;
2298
 
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2299
 
    goto err;
2300
 
  if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2301
 
  {
2302
 
    my_close(file, MYF(0));
2303
 
    goto err;
2304
 
  }
2305
 
  tmp_value.length(stat_info.st_size);
2306
 
  my_close(file, MYF(0));
2307
 
  null_value = 0;
2308
 
  return(&tmp_value);
2309
 
 
2310
 
err:
2311
 
  null_value = 1;
2312
 
  return(0);
2313
 
}
2314
 
 
2315
 
 
2316
 
String* Item_func_export_set::val_str(String* str)
2317
 
{
2318
 
  assert(fixed == 1);
2319
 
  uint64_t the_set = (uint64_t) args[0]->val_int();
2320
 
  String yes_buf, *yes;
2321
 
  yes = args[1]->val_str(&yes_buf);
2322
 
  String no_buf, *no;
2323
 
  no = args[2]->val_str(&no_buf);
2324
 
  String *sep = NULL, sep_buf ;
2325
 
 
2326
 
  uint num_set_values = 64;
2327
 
  uint64_t mask = 0x1;
2328
 
  str->length(0);
2329
 
  str->set_charset(collation.collation);
2330
 
 
2331
 
  /* Check if some argument is a NULL value */
2332
 
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
2333
 
  {
2334
 
    null_value=1;
2335
 
    return 0;
2336
 
  }
2337
 
  /*
2338
 
    Arg count can only be 3, 4 or 5 here. This is guaranteed from the
2339
 
    grammar for EXPORT_SET()
2340
 
  */
2341
 
  switch(arg_count) {
2342
 
  case 5:
2343
 
    num_set_values = (uint) args[4]->val_int();
2344
 
    if (num_set_values > 64)
2345
 
      num_set_values=64;
2346
 
    if (args[4]->null_value)
2347
 
    {
2348
 
      null_value=1;
2349
 
      return 0;
2350
 
    }
2351
 
    /* Fall through */
2352
 
  case 4:
2353
 
    if (!(sep = args[3]->val_str(&sep_buf)))    // Only true if NULL
2354
 
    {
2355
 
      null_value=1;
2356
 
      return 0;
2357
 
    }
2358
 
    break;
2359
 
  case 3:
2360
 
    {
2361
 
      /* errors is not checked - assume "," can always be converted */
2362
 
      uint errors;
2363
 
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2364
 
      sep = &sep_buf;
2365
 
    }
2366
 
    break;
2367
 
  default:
2368
 
    assert(0); // cannot happen
2369
 
  }
2370
 
  null_value=0;
2371
 
 
2372
 
  for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
2373
 
  {
2374
 
    if (the_set & mask)
2375
 
      str->append(*yes);
2376
 
    else
2377
 
      str->append(*no);
2378
 
    if (i != num_set_values - 1)
2379
 
      str->append(*sep);
2380
 
  }
2381
 
  return str;
2382
 
}
2383
 
 
2384
 
void Item_func_export_set::fix_length_and_dec()
2385
 
{
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;
2389
 
 
2390
 
  if (agg_arg_charsets(collation, args+1, min((uint)4,arg_count)-1,
2391
 
                       MY_COLL_ALLOW_CONV, 1))
2392
 
    return;
2393
 
}
2394
 
 
2395
 
 
2396
 
#define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))
2397
 
 
2398
 
/**
2399
 
  QUOTE() function returns argument string in single quotes suitable for
2400
 
  using in a SQL statement.
2401
 
 
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.
2405
 
 
2406
 
  This function is very useful when you want to generate SQL statements.
2407
 
 
2408
 
  @note
2409
 
    QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).
2410
 
 
2411
 
  @retval
2412
 
    str    Quoted string
2413
 
  @retval
2414
 
    NULL           Out of memory.
2415
 
*/
2416
 
 
2417
 
String *Item_func_quote::val_str(String *str)
2418
 
{
2419
 
  assert(fixed == 1);
2420
 
  /*
2421
 
    Bit mask that has 1 for set for the position of the following characters:
2422
 
    0, \, ' and ^Z
2423
 
  */
2424
 
 
2425
 
  static uchar escmask[32]=
2426
 
  {
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
2431
 
  };
2432
 
 
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
2437
 
  {
2438
 
    /* Return the string 'NULL' */
2439
 
    str->copy(STRING_WITH_LEN("NULL"), collation.collation);
2440
 
    null_value= 0;
2441
 
    return str;
2442
 
  }
2443
 
 
2444
 
  arg_length= arg->length();
2445
 
  new_length= arg_length+2; /* for beginning and ending ' signs */
2446
 
 
2447
 
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2448
 
    new_length+= get_esc_bit(escmask, (uchar) *from);
2449
 
 
2450
 
  if (tmp_value.alloc(new_length))
2451
 
    goto null;
2452
 
 
2453
 
  /*
2454
 
    We replace characters from the end to the beginning
2455
 
  */
2456
 
  to= (char*) tmp_value.ptr() + new_length - 1;
2457
 
  *to--= '\'';
2458
 
  for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
2459
 
  {
2460
 
    /*
2461
 
      We can't use the bitmask here as we want to replace \O and ^Z with 0
2462
 
      and Z
2463
 
    */
2464
 
    switch (*end)  {
2465
 
    case 0:
2466
 
      *to--= '0';
2467
 
      *to=   '\\';
2468
 
      break;
2469
 
    case '\032':
2470
 
      *to--= 'Z';
2471
 
      *to=   '\\';
2472
 
      break;
2473
 
    case '\'':
2474
 
    case '\\':
2475
 
      *to--= *end;
2476
 
      *to=   '\\';
2477
 
      break;
2478
 
    default:
2479
 
      *to= *end;
2480
 
      break;
2481
 
    }
2482
 
  }
2483
 
  *to= '\'';
2484
 
  tmp_value.length(new_length);
2485
 
  tmp_value.set_charset(collation.collation);
2486
 
  null_value= 0;
2487
 
  return &tmp_value;
2488
 
 
2489
 
null:
2490
 
  null_value= 1;
2491
 
  return 0;
2492
 
}
2493
 
 
2494
 
int64_t Item_func_uncompressed_length::val_int()
2495
 
{
2496
 
  assert(fixed == 1);
2497
 
  String *res= args[0]->val_str(&value);
2498
 
  if (!res)
2499
 
  {
2500
 
    null_value=1;
2501
 
    return 0; /* purecov: inspected */
2502
 
  }
2503
 
  null_value=0;
2504
 
  if (res->is_empty()) return 0;
2505
 
 
2506
 
  /*
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
2511
 
        confuse valgrind
2512
 
  */
2513
 
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
2514
 
}
2515
 
 
2516
 
#ifdef HAVE_COMPRESS
2517
 
#include "zlib.h"
2518
 
 
2519
 
String *Item_func_compress::val_str(String *str)
2520
 
{
2521
 
  int err= Z_OK, code;
2522
 
  ulong new_size;
2523
 
  String *res;
2524
 
  Byte *body;
2525
 
  char *tmp, *last_char;
2526
 
  assert(fixed == 1);
2527
 
 
2528
 
  if (!(res= args[0]->val_str(str)))
2529
 
  {
2530
 
    null_value= 1;
2531
 
    return 0;
2532
 
  }
2533
 
  null_value= 0;
2534
 
  if (res->is_empty()) return res;
2535
 
 
2536
 
  /*
2537
 
    Citation from zlib.h (comment for compress function):
2538
 
 
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 %.
2544
 
  */
2545
 
  new_size= res->length() + res->length() / 5 + 12;
2546
 
 
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))
2550
 
  {
2551
 
    null_value= 1;
2552
 
    return 0;
2553
 
  }
2554
 
 
2555
 
  body= ((Byte*)buffer.ptr()) + 4;
2556
 
 
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)
2560
 
  {
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));
2563
 
    null_value= 1;
2564
 
    return 0;
2565
 
  }
2566
 
 
2567
 
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
2568
 
  int4store(tmp, res->length() & 0x3FFFFFFF);
2569
 
 
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 == ' ')
2573
 
  {
2574
 
    *++last_char= '.';
2575
 
    new_size++;
2576
 
  }
2577
 
 
2578
 
  buffer.length((uint32_t)new_size + 4);
2579
 
  return &buffer;
2580
 
}
2581
 
 
2582
 
 
2583
 
String *Item_func_uncompress::val_str(String *str)
2584
 
{
2585
 
  assert(fixed == 1);
2586
 
  String *res= args[0]->val_str(str);
2587
 
  ulong new_size;
2588
 
  int err;
2589
 
  uint code;
2590
 
 
2591
 
  if (!res)
2592
 
    goto err;
2593
 
  null_value= 0;
2594
 
  if (res->is_empty())
2595
 
    return res;
2596
 
 
2597
 
  /* If length is less than 4 bytes, data is corrupt */
2598
 
  if (res->length() <= 4)
2599
 
  {
2600
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2601
 
                        ER_ZLIB_Z_DATA_ERROR,
2602
 
                        ER(ER_ZLIB_Z_DATA_ERROR));
2603
 
    goto err;
2604
 
  }
2605
 
 
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)
2609
 
  {
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);
2614
 
    goto err;
2615
 
  }
2616
 
  if (buffer.realloc((uint32_t)new_size))
2617
 
    goto err;
2618
 
 
2619
 
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
2620
 
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
2621
 
  {
2622
 
    buffer.length((uint32_t) new_size);
2623
 
    return &buffer;
2624
 
  }
2625
 
 
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));
2629
 
 
2630
 
err:
2631
 
  null_value= 1;
2632
 
  return 0;
2633
 
}
2634
 
#endif
2635
 
 
2636
 
/*
2637
 
  UUID, as in
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)
2642
 
*/
2643
 
 
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";
2648
 
 
2649
 
/**
2650
 
  number of 100-nanosecond intervals between
2651
 
  1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
2652
 
*/
2653
 
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
2654
 
 
2655
 
#define UUID_VERSION      0x1000
2656
 
#define UUID_VARIANT      0x8000
2657
 
 
2658
 
static void tohex(char *to, uint from, uint len)
2659
 
{
2660
 
  to+= len;
2661
 
  while (len--)
2662
 
  {
2663
 
    *--to= _dig_vec_lower[from & 15];
2664
 
    from >>= 4;
2665
 
  }
2666
 
}
2667
 
 
2668
 
static void set_clock_seq_str()
2669
 
{
2670
 
  uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
2671
 
  tohex(clock_seq_and_node_str+1, clock_seq, 4);
2672
 
  nanoseq= 0;
2673
 
}
2674
 
 
2675
 
String *Item_func_uuid::val_str(String *str)
2676
 
{
2677
 
  assert(fixed == 1);
2678
 
  char *s;
2679
 
  THD *thd= current_thd;
2680
 
 
2681
 
  pthread_mutex_lock(&LOCK_uuid_generator);
2682
 
  if (! uuid_time) /* first UUID() call. initializing data */
2683
 
  {
2684
 
    ulong tmp= sql_rnd();
2685
 
    uchar mac[6];
2686
 
    int i;
2687
 
    if (my_gethwaddr(mac))
2688
 
    {
2689
 
      /* purecov: begin inspected */
2690
 
      /*
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
2694
 
        randominit() here
2695
 
      */
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);
2699
 
      /* purecov: end */    
2700
 
    }
2701
 
    s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
2702
 
    for (i=sizeof(mac)-1 ; i>=0 ; i--)
2703
 
    {
2704
 
      *--s=_dig_vec_lower[mac[i] & 15];
2705
 
      *--s=_dig_vec_lower[mac[i] >> 4];
2706
 
    }
2707
 
    randominit(&uuid_rand, tmp + (ulong) server_start_time,
2708
 
               tmp + (ulong) thd->status_var.bytes_sent);
2709
 
    set_clock_seq_str();
2710
 
  }
2711
 
 
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))
2716
 
  {
2717
 
    /* special protection from low-res system clocks */
2718
 
    nanoseq++;
2719
 
    tv++;
2720
 
  }
2721
 
  else
2722
 
  {
2723
 
    if (nanoseq)
2724
 
    {
2725
 
      tv-=nanoseq;
2726
 
      nanoseq=0;
2727
 
    }
2728
 
    assert(tv > uuid_time);
2729
 
  }
2730
 
  uuid_time=tv;
2731
 
  pthread_mutex_unlock(&LOCK_uuid_generator);
2732
 
 
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);
2736
 
 
2737
 
  str->realloc(UUID_LENGTH+1);
2738
 
  str->length(UUID_LENGTH);
2739
 
  str->set_charset(system_charset_info);
2740
 
  s=(char *) str->ptr();
2741
 
  s[8]=s[13]='-';
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);
2746
 
  return str;
2747
 
}