~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_strfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-07-28 02:47:38 UTC
  • mto: (212.5.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 219.
  • Revision ID: monty@inaugust.com-20080728024738-366ikqnoqv7d8g6l
Fixed the includes in places to make the myisam header file move work.

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