~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_strfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <drizzled/server_includes.h>
29
29
#include <mysys/sha1.h>
30
30
#include <zlib.h>
31
 
#include <drizzled/query_id.h>
32
 
#include <uuid/uuid.h>
33
 
#include <drizzled/data_home.h>
34
 
#include <drizzled/error.h>
35
 
 
36
 
// For soundex_map
37
 
#include <mysys/my_static.h>
38
 
#include CMATH_H
39
 
 
40
 
#if defined(CMATH_NAMESPACE)
41
 
using namespace CMATH_NAMESPACE;
42
 
#endif
43
 
 
44
 
using namespace std;
 
31
C_MODE_START
 
32
#include <mysys/my_static.h>                    // For soundex_map
 
33
C_MODE_END
 
34
#include <drizzled/drizzled_error_messages.h>
45
35
 
46
36
String my_empty_string("",default_charset_info);
47
37
 
48
38
 
49
39
 
50
40
 
51
 
bool Item_str_func::fix_fields(Session *session, Item **ref)
 
41
bool Item_str_func::fix_fields(THD *thd, Item **ref)
52
42
{
53
 
  bool res= Item_func::fix_fields(session, ref);
 
43
  bool res= Item_func::fix_fields(thd, ref);
54
44
  /*
55
45
    In Item_str_func::check_well_formed_result() we may set null_value
56
46
    flag on the same condition as in test() below.
109
99
{
110
100
  assert(fixed == 1);
111
101
  String *res,*res2,*use_as_buff;
112
 
  uint32_t i;
 
102
  uint i;
113
103
  bool is_const= 0;
114
104
 
115
105
  null_value=0;
132
122
      if (res2->length() == 0)
133
123
        continue;
134
124
      if (res->length()+res2->length() >
135
 
          current_session->variables.max_allowed_packet)
 
125
          current_thd->variables.max_allowed_packet)
136
126
      {
137
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
127
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
138
128
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
139
129
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
140
 
                            current_session->variables.max_allowed_packet);
 
130
                            current_thd->variables.max_allowed_packet);
141
131
        goto null;
142
132
      }
143
133
      if (!is_const && res->alloced_length() >= res->length()+res2->length())
196
186
          more than 25% of memory will be overcommitted on average.
197
187
        */
198
188
 
199
 
        uint32_t concat_len= res->length() + res2->length();
 
189
        uint concat_len= res->length() + res2->length();
200
190
 
201
191
        if (tmp_value.alloced_length() < concat_len)
202
192
        {
207
197
          }
208
198
          else
209
199
          {
210
 
            uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
 
200
            uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
211
201
 
212
202
            if (tmp_value.realloc(new_len))
213
203
              goto null;
239
229
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
240
230
    return;
241
231
 
242
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
232
  for (uint i=0 ; i < arg_count ; i++)
243
233
  {
244
234
    if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
245
235
      max_result_length+= (args[i]->max_length /
269
259
  char tmp_str_buff[10];
270
260
  String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
271
261
         *sep_str, *res, *res2,*use_as_buff;
272
 
  uint32_t i;
 
262
  uint i;
273
263
 
274
264
  null_value=0;
275
265
  if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
293
283
      continue;                                 // Skip NULL
294
284
 
295
285
    if (res->length() + sep_str->length() + res2->length() >
296
 
        current_session->variables.max_allowed_packet)
 
286
        current_thd->variables.max_allowed_packet)
297
287
    {
298
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
288
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
299
289
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
300
290
                          ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
301
 
                          current_session->variables.max_allowed_packet);
 
291
                          current_thd->variables.max_allowed_packet);
302
292
      goto null;
303
293
    }
304
294
    if (res->alloced_length() >=
366
356
        25% of memory will be overcommitted on average.
367
357
      */
368
358
 
369
 
      uint32_t concat_len= res->length() + sep_str->length() + res2->length();
 
359
      uint concat_len= res->length() + sep_str->length() + res2->length();
370
360
 
371
361
      if (tmp_value.alloced_length() < concat_len)
372
362
      {
377
367
        }
378
368
        else
379
369
        {
380
 
          uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
 
370
          uint new_len = max(tmp_value.alloced_length() * 2, concat_len);
381
371
 
382
372
          if (tmp_value.realloc(new_len))
383
373
            goto null;
414
404
     so, (arg_count - 2) is safe here.
415
405
  */
416
406
  max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
417
 
  for (uint32_t i=1 ; i < arg_count ; i++)
 
407
  for (uint i=1 ; i < arg_count ; i++)
418
408
    max_result_length+=args[i]->max_length;
419
409
 
420
410
  if (max_result_length >= MAX_BLOB_WIDTH)
494
484
  assert(fixed == 1);
495
485
  String *res,*res2,*res3;
496
486
  int offset;
497
 
  uint32_t from_length,to_length;
 
487
  uint from_length,to_length;
498
488
  bool alloced=0;
499
489
#ifdef USE_MB
500
490
  const char *ptr,*end,*strend,*search,*search_end;
550
540
            if (*i++ != *j++) goto skip;
551
541
          offset= (int) (ptr-res->ptr());
552
542
          if (res->length()-from_length + to_length >
553
 
              current_session->variables.max_allowed_packet)
 
543
              current_thd->variables.max_allowed_packet)
554
544
          {
555
 
            push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
545
            push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
556
546
                                ER_WARN_ALLOWED_PACKET_OVERFLOWED,
557
547
                                ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
558
548
                                func_name(),
559
 
                                current_session->variables.max_allowed_packet);
 
549
                                current_thd->variables.max_allowed_packet);
560
550
 
561
551
            goto null;
562
552
          }
579
569
    do
580
570
    {
581
571
      if (res->length()-from_length + to_length >
582
 
          current_session->variables.max_allowed_packet)
 
572
          current_thd->variables.max_allowed_packet)
583
573
      {
584
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
574
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
585
575
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
586
576
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
587
 
                            current_session->variables.max_allowed_packet);
 
577
                            current_thd->variables.max_allowed_packet);
588
578
        goto null;
589
579
      }
590
580
      if (!alloced)
657
647
    length= res->length() - start;
658
648
 
659
649
  if ((uint64_t) (res->length() - length + res2->length()) >
660
 
      (uint64_t) current_session->variables.max_allowed_packet)
 
650
      (uint64_t) current_thd->variables.max_allowed_packet)
661
651
  {
662
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
652
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
663
653
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
664
654
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
665
 
                        func_name(), current_session->variables.max_allowed_packet);
 
655
                        func_name(), current_thd->variables.max_allowed_packet);
666
656
    goto null;
667
657
  }
668
658
  res=copy_if_not_alloced(str,res,res->length());
704
694
  null_value=0;
705
695
  if (multiply == 1)
706
696
  {
707
 
    uint32_t len;
 
697
    uint len;
708
698
    res= copy_if_not_alloced(str,res,res->length());
709
699
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
710
700
                                        (char*) res->ptr(), res->length());
713
703
  }
714
704
  else
715
705
  {
716
 
    uint32_t len= res->length() * multiply;
 
706
    uint len= res->length() * multiply;
717
707
    tmp_value.alloc(len);
718
708
    tmp_value.set_charset(collation.collation);
719
709
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
749
739
 
750
740
  /* must be int64_t to avoid truncation */
751
741
  int64_t length= args[1]->val_int();
752
 
  uint32_t char_pos;
 
742
  uint char_pos;
753
743
 
754
744
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
755
745
    return 0;
805
795
  if (res->length() <= (uint64_t) length)
806
796
    return res; /* purecov: inspected */
807
797
 
808
 
  uint32_t start=res->numchars();
 
798
  uint start=res->numchars();
809
799
  if (start <= (uint) length)
810
800
    return res;
811
801
  start=res->charpos(start - (uint) length);
859
849
 
860
850
  length= res->charpos((int) length, (uint32_t) start);
861
851
  tmp_length= res->length() - start;
862
 
  length= cmin(length, tmp_length);
 
852
  length= min(length, tmp_length);
863
853
 
864
854
  if (!start && (int64_t) res->length() == length)
865
855
    return res;
879
869
    if (start < 0)
880
870
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
881
871
    else
882
 
      max_length-= cmin((uint)(start - 1), max_length);
 
872
      max_length-= min((uint)(start - 1), max_length);
883
873
  }
884
874
  if (arg_count == 3 && args[2]->const_item())
885
875
  {
908
898
  String *res= args[0]->val_str(str);
909
899
  String *delimiter= args[1]->val_str(&tmp_value);
910
900
  int32_t count= (int32_t) args[2]->val_int();
911
 
  uint32_t offset;
 
901
  uint offset;
912
902
 
913
903
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
914
904
  {                                     // string and/or delim are null
916
906
    return 0;
917
907
  }
918
908
  null_value=0;
919
 
  uint32_t delimiter_length= delimiter->length();
 
909
  uint delimiter_length= delimiter->length();
920
910
  if (!res->length() || !delimiter_length || !count)
921
911
    return &my_empty_string;            // Wrong parameters
922
912
 
1038
1028
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1039
1029
  String tmp(buff,sizeof(buff),system_charset_info);
1040
1030
  String *res, *remove_str;
1041
 
  uint32_t remove_length;
 
1031
  uint remove_length;
1042
1032
 
1043
1033
  res= args[0]->val_str(str);
1044
1034
  if ((null_value=args[0]->null_value))
1084
1074
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
1085
1075
  String tmp(buff, sizeof(buff), system_charset_info);
1086
1076
  String *res, *remove_str;
1087
 
  uint32_t remove_length;
 
1077
  uint remove_length;
1088
1078
 
1089
1079
  res= args[0]->val_str(str);
1090
1080
  if ((null_value=args[0]->null_value))
1165
1155
  const char *r_ptr;
1166
1156
  String tmp(buff, sizeof(buff), system_charset_info);
1167
1157
  String *res, *remove_str;
1168
 
  uint32_t remove_length;
 
1158
  uint remove_length;
1169
1159
 
1170
1160
  res= args[0]->val_str(str);
1171
1161
  if ((null_value=args[0]->null_value))
1258
1248
Item *Item_func_sysconst::safe_charset_converter(const CHARSET_INFO * const tocs)
1259
1249
{
1260
1250
  Item_string *conv;
1261
 
  uint32_t conv_errors;
 
1251
  uint conv_errors;
1262
1252
  String tmp, cstr, *ostr= val_str(&tmp);
1263
1253
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1264
1254
  if (conv_errors ||
1278
1268
String *Item_func_database::val_str(String *str)
1279
1269
{
1280
1270
  assert(fixed == 1);
1281
 
  Session *session= current_session;
1282
 
  if (session->db == NULL)
 
1271
  THD *thd= current_thd;
 
1272
  if (thd->db == NULL)
1283
1273
  {
1284
1274
    null_value= 1;
1285
1275
    return 0;
1286
1276
  }
1287
1277
  else
1288
 
    str->copy(session->db, session->db_length, system_charset_info);
 
1278
    str->copy(thd->db, thd->db_length, system_charset_info);
1289
1279
  return str;
1290
1280
}
1291
1281
 
1302
1292
  if (user)
1303
1293
  {
1304
1294
    const CHARSET_INFO * const cs= str_value.charset();
1305
 
    uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
 
1295
    uint res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
1306
1296
 
1307
1297
    if (str_value.alloc(res_length))
1308
1298
    {
1319
1309
}
1320
1310
 
1321
1311
 
1322
 
bool Item_func_user::fix_fields(Session *session, Item **ref)
 
1312
bool Item_func_user::fix_fields(THD *thd, Item **ref)
1323
1313
{
1324
 
  return (Item_func_sysconst::fix_fields(session, ref) ||
1325
 
          init(session->main_security_ctx.user,
1326
 
               session->main_security_ctx.ip));
 
1314
  return (Item_func_sysconst::fix_fields(thd, ref) ||
 
1315
          init(thd->main_security_ctx.user,
 
1316
               thd->main_security_ctx.ip));
1327
1317
}
1328
1318
 
1329
1319
 
1330
 
bool Item_func_current_user::fix_fields(Session *session, Item **ref)
 
1320
bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
1331
1321
{
1332
 
  if (Item_func_sysconst::fix_fields(session, ref))
 
1322
  if (Item_func_sysconst::fix_fields(thd, ref))
1333
1323
    return true;
1334
1324
 
1335
1325
  Security_context *ctx=
1336
 
                         session->security_ctx;
 
1326
                         thd->security_ctx;
1337
1327
  return init(ctx->user, ctx->ip);
1338
1328
}
1339
1329
 
1354
1344
void Item_func_format::fix_length_and_dec()
1355
1345
{
1356
1346
  collation.set(default_charset());
1357
 
  uint32_t char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
 
1347
  uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
1358
1348
  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
1359
1349
               collation.collation->mbmaxlen);
1360
1350
}
1459
1449
  if (agg_arg_charsets(collation, args+1, arg_count-1, MY_COLL_ALLOW_CONV, 1))
1460
1450
    return;
1461
1451
 
1462
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
1452
  for (uint i= 1 ; i < arg_count ; i++)
1463
1453
  {
1464
1454
    set_if_bigger(max_length,args[i]->max_length);
1465
1455
    set_if_bigger(decimals,args[i]->decimals);
1471
1461
double Item_func_elt::val_real()
1472
1462
{
1473
1463
  assert(fixed == 1);
1474
 
  uint32_t tmp;
 
1464
  uint tmp;
1475
1465
  null_value=1;
1476
1466
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1477
1467
    return 0.0;
1484
1474
int64_t Item_func_elt::val_int()
1485
1475
{
1486
1476
  assert(fixed == 1);
1487
 
  uint32_t tmp;
 
1477
  uint tmp;
1488
1478
  null_value=1;
1489
1479
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1490
1480
    return 0;
1498
1488
String *Item_func_elt::val_str(String *str)
1499
1489
{
1500
1490
  assert(fixed == 1);
1501
 
  uint32_t tmp;
 
1491
  uint tmp;
1502
1492
  null_value=1;
1503
1493
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
1504
1494
    return NULL;
1511
1501
}
1512
1502
 
1513
1503
 
1514
 
void Item_func_make_set::split_sum_func(Session *session, Item **ref_pointer_array,
 
1504
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
1515
1505
                                        List<Item> &fields)
1516
1506
{
1517
 
  item->split_sum_func2(session, ref_pointer_array, fields, &item, true);
1518
 
  Item_str_func::split_sum_func(session, ref_pointer_array, fields);
 
1507
  item->split_sum_func2(thd, ref_pointer_array, fields, &item, true);
 
1508
  Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
1519
1509
}
1520
1510
 
1521
1511
 
1526
1516
  if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
1527
1517
    return;
1528
1518
  
1529
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1519
  for (uint i=0 ; i < arg_count ; i++)
1530
1520
    max_length+=args[i]->max_length;
1531
1521
 
1532
1522
  used_tables_cache|=     item->used_tables();
1598
1588
}
1599
1589
 
1600
1590
 
1601
 
Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
 
1591
Item *Item_func_make_set::transform(Item_transformer transformer, uchar *arg)
1602
1592
{
1603
1593
  Item *new_item= item->transform(transformer, arg);
1604
1594
  if (!new_item)
1605
1595
    return 0;
1606
1596
 
1607
1597
  /*
1608
 
    Session::change_item_tree() should be called only if the tree was
 
1598
    THD::change_item_tree() should be called only if the tree was
1609
1599
    really transformed, i.e. when a new item has been created.
1610
1600
    Otherwise we'll be allocating a lot of unnecessary memory for
1611
1601
    change records at each execution.
1612
1602
  */
1613
1603
  if (item != new_item)
1614
 
    current_session->change_item_tree(&item, new_item);
 
1604
    current_thd->change_item_tree(&item, new_item);
1615
1605
  return Item_str_func::transform(transformer, arg);
1616
1606
}
1617
1607
 
1634
1624
  assert(fixed == 1);
1635
1625
  str->length(0);
1636
1626
  str->set_charset(collation.collation);
1637
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1627
  for (uint i=0 ; i < arg_count ; i++)
1638
1628
  {
1639
1629
    int32_t num=(int32_t) args[i]->val_int();
1640
1630
    if (!args[i]->null_value)
1715
1705
String *Item_func_repeat::val_str(String *str)
1716
1706
{
1717
1707
  assert(fixed == 1);
1718
 
  uint32_t length,tot_length;
 
1708
  uint length,tot_length;
1719
1709
  char *to;
1720
1710
  /* must be int64_t to avoid truncation */
1721
1711
  int64_t count= args[1]->val_int();
1736
1726
    return res;
1737
1727
  length=res->length();
1738
1728
  // Safe length check
1739
 
  if (length > current_session->variables.max_allowed_packet / (uint) count)
 
1729
  if (length > current_thd->variables.max_allowed_packet / (uint) count)
1740
1730
  {
1741
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1731
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1742
1732
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1743
1733
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1744
 
                        func_name(), current_session->variables.max_allowed_packet);
 
1734
                        func_name(), current_thd->variables.max_allowed_packet);
1745
1735
    goto err;
1746
1736
  }
1747
1737
  tot_length= length*(uint) count;
1826
1816
  pad_char_length= rpad->numchars();
1827
1817
 
1828
1818
  byte_count= count * collation.collation->mbmaxlen;
1829
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
 
1819
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1830
1820
  {
1831
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1821
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1832
1822
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1833
1823
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1834
 
                        func_name(), current_session->variables.max_allowed_packet);
 
1824
                        func_name(), current_thd->variables.max_allowed_packet);
1835
1825
    goto err;
1836
1826
  }
1837
1827
  if (args[2]->null_value || !pad_char_length)
1931
1921
  pad_char_length= pad->numchars();
1932
1922
  byte_count= count * collation.collation->mbmaxlen;
1933
1923
  
1934
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
 
1924
  if ((uint64_t) byte_count > current_thd->variables.max_allowed_packet)
1935
1925
  {
1936
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1926
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1937
1927
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1938
1928
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1939
 
                        func_name(), current_session->variables.max_allowed_packet);
 
1929
                        func_name(), current_thd->variables.max_allowed_packet);
1940
1930
    goto err;
1941
1931
  }
1942
1932
 
2005
1995
  if (use_cached_value)
2006
1996
    return null_value ? 0 : &str_value;
2007
1997
  String *arg= args[0]->val_str(str);
2008
 
  uint32_t dummy_errors;
 
1998
  uint dummy_errors;
2009
1999
  if (!arg)
2010
2000
  {
2011
2001
    null_value=1;
2086
2076
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
2087
2077
  if (collation.collation != item_func_sc->collation.collation)
2088
2078
    return 0;
2089
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2079
  for (uint i=0; i < arg_count ; i++)
2090
2080
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
2091
2081
      return 0;
2092
2082
  return 1;
2107
2097
String *Item_func_charset::val_str(String *str)
2108
2098
{
2109
2099
  assert(fixed == 1);
2110
 
  uint32_t dummy_errors;
 
2100
  uint dummy_errors;
2111
2101
 
2112
2102
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2113
2103
  null_value= 0;
2119
2109
String *Item_func_collation::val_str(String *str)
2120
2110
{
2121
2111
  assert(fixed == 1);
2122
 
  uint32_t dummy_errors;
 
2112
  uint dummy_errors;
2123
2113
  const CHARSET_INFO * const cs= args[0]->collation.collation; 
2124
2114
 
2125
2115
  null_value= 0;
2134
2124
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2135
2125
  collation.set(&my_charset_bin, args[0]->collation.derivation);
2136
2126
  flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
2137
 
  max_length= cs->mbmaxlen * cmax(args[0]->max_length, nweights);
 
2127
  max_length= cs->mbmaxlen * max(args[0]->max_length, nweights);
2138
2128
  maybe_null= 1;
2139
2129
}
2140
2130
 
2144
2134
{
2145
2135
  String *res;
2146
2136
  const CHARSET_INFO * const cs= args[0]->collation.collation;
2147
 
  uint32_t tmp_length, frm_length;
 
2137
  uint tmp_length, frm_length;
2148
2138
  assert(fixed == 1);
2149
2139
 
2150
2140
  if (args[0]->result_type() != STRING_RESULT ||
2152
2142
    goto nl;
2153
2143
  
2154
2144
  tmp_length= cs->coll->strnxfrmlen(cs, cs->mbmaxlen *
2155
 
                                        cmax(res->length(), nweights));
 
2145
                                        max(res->length(), nweights));
2156
2146
 
2157
2147
  if (tmp_value.alloc(tmp_length))
2158
2148
    goto nl;
2159
2149
 
2160
2150
  frm_length= cs->coll->strnxfrm(cs,
2161
 
                                 (unsigned char*) tmp_value.ptr(), tmp_length,
 
2151
                                 (uchar*) tmp_value.ptr(), tmp_length,
2162
2152
                                 nweights ? nweights : tmp_length,
2163
 
                                 (const unsigned char*) res->ptr(), res->length(),
 
2153
                                 (const uchar*) res->ptr(), res->length(),
2164
2154
                                 flags);
2165
2155
  tmp_value.length(frm_length);
2166
2156
  null_value= 0;
2223
2213
  const char *from, *end;
2224
2214
  char *to;
2225
2215
  String *res;
2226
 
  uint32_t length;
 
2216
  uint length;
2227
2217
  assert(fixed == 1);
2228
2218
 
2229
2219
  res= args[0]->val_str(str);
2293
2283
    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
2294
2284
    goto err;
2295
2285
  }
2296
 
  if (stat_info.st_size > (long) current_session->variables.max_allowed_packet)
 
2286
  if (stat_info.st_size > (long) current_thd->variables.max_allowed_packet)
2297
2287
  {
2298
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2288
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2299
2289
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
2300
2290
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
2301
 
                        func_name(), current_session->variables.max_allowed_packet);
 
2291
                        func_name(), current_thd->variables.max_allowed_packet);
2302
2292
    goto err;
2303
2293
  }
2304
2294
  if (tmp_value.alloc(stat_info.st_size))
2305
2295
    goto err;
2306
2296
  if ((file = my_open(file_name->c_ptr(), O_RDONLY, MYF(0))) < 0)
2307
2297
    goto err;
2308
 
  if (my_read(file, (unsigned char*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
 
2298
  if (my_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size, MYF(MY_NABP)))
2309
2299
  {
2310
2300
    my_close(file, MYF(0));
2311
2301
    goto err;
2331
2321
  no = args[2]->val_str(&no_buf);
2332
2322
  String *sep = NULL, sep_buf ;
2333
2323
 
2334
 
  uint32_t num_set_values = 64;
 
2324
  uint num_set_values = 64;
2335
2325
  uint64_t mask = 0x1;
2336
2326
  str->length(0);
2337
2327
  str->set_charset(collation.collation);
2367
2357
  case 3:
2368
2358
    {
2369
2359
      /* errors is not checked - assume "," can always be converted */
2370
 
      uint32_t errors;
 
2360
      uint errors;
2371
2361
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
2372
2362
      sep = &sep_buf;
2373
2363
    }
2377
2367
  }
2378
2368
  null_value=0;
2379
2369
 
2380
 
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
 
2370
  for (uint i = 0; i < num_set_values; i++, mask = (mask << 1))
2381
2371
  {
2382
2372
    if (the_set & mask)
2383
2373
      str->append(*yes);
2391
2381
 
2392
2382
void Item_func_export_set::fix_length_and_dec()
2393
2383
{
2394
 
  uint32_t length=cmax(args[1]->max_length,args[2]->max_length);
2395
 
  uint32_t sep_length=(arg_count > 3 ? args[3]->max_length : 1);
 
2384
  uint length=max(args[1]->max_length,args[2]->max_length);
 
2385
  uint sep_length=(arg_count > 3 ? args[3]->max_length : 1);
2396
2386
  max_length=length*64+sep_length*63;
2397
2387
 
2398
 
  if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
 
2388
  if (agg_arg_charsets(collation, args+1, min((uint)4,arg_count)-1,
2399
2389
                       MY_COLL_ALLOW_CONV, 1))
2400
2390
    return;
2401
2391
}
2430
2420
    0, \, ' and ^Z
2431
2421
  */
2432
2422
 
2433
 
  static unsigned char escmask[32]=
 
2423
  static uchar escmask[32]=
2434
2424
  {
2435
2425
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
2436
2426
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
2440
2430
 
2441
2431
  char *from, *to, *end, *start;
2442
2432
  String *arg= args[0]->val_str(str);
2443
 
  uint32_t arg_length, new_length;
 
2433
  uint arg_length, new_length;
2444
2434
  if (!arg)                                     // Null argument
2445
2435
  {
2446
2436
    /* Return the string 'NULL' */
2453
2443
  new_length= arg_length+2; /* for beginning and ending ' signs */
2454
2444
 
2455
2445
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
2456
 
    new_length+= get_esc_bit(escmask, (unsigned char) *from);
 
2446
    new_length+= get_esc_bit(escmask, (uchar) *from);
2457
2447
 
2458
2448
  if (tmp_value.alloc(new_length))
2459
2449
    goto null;
2499
2489
  return 0;
2500
2490
}
2501
2491
 
2502
 
 
 
2492
int64_t Item_func_uncompressed_length::val_int()
 
2493
{
 
2494
  assert(fixed == 1);
 
2495
  String *res= args[0]->val_str(&value);
 
2496
  if (!res)
 
2497
  {
 
2498
    null_value=1;
 
2499
    return 0; /* purecov: inspected */
 
2500
  }
 
2501
  null_value=0;
 
2502
  if (res->is_empty()) return 0;
 
2503
 
 
2504
  /*
 
2505
    res->ptr() using is safe because we have tested that string is not empty,
 
2506
    res->c_ptr() is not used because:
 
2507
      - we do not need \0 terminated string to get first 4 bytes
 
2508
      - c_ptr() tests simbol after string end (uninitialiozed memory) which
 
2509
        confuse valgrind
 
2510
  */
 
2511
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2512
}
 
2513
 
 
2514
#ifdef HAVE_COMPRESS
 
2515
#include "zlib.h"
 
2516
 
 
2517
String *Item_func_compress::val_str(String *str)
 
2518
{
 
2519
  int err= Z_OK, code;
 
2520
  ulong new_size;
 
2521
  String *res;
 
2522
  Byte *body;
 
2523
  char *tmp, *last_char;
 
2524
  assert(fixed == 1);
 
2525
 
 
2526
  if (!(res= args[0]->val_str(str)))
 
2527
  {
 
2528
    null_value= 1;
 
2529
    return 0;
 
2530
  }
 
2531
  null_value= 0;
 
2532
  if (res->is_empty()) return res;
 
2533
 
 
2534
  /*
 
2535
    Citation from zlib.h (comment for compress function):
 
2536
 
 
2537
    Compresses the source buffer into the destination buffer.  sourceLen is
 
2538
    the byte length of the source buffer. Upon entry, destLen is the total
 
2539
    size of the destination buffer, which must be at least 0.1% larger than
 
2540
    sourceLen plus 12 bytes.
 
2541
    We assume here that the buffer can't grow more than .25 %.
 
2542
  */
 
2543
  new_size= res->length() + res->length() / 5 + 12;
 
2544
 
 
2545
  // Check new_size overflow: new_size <= res->length()
 
2546
  if (((uint32_t) (new_size+5) <= res->length()) || 
 
2547
      buffer.realloc((uint32_t) new_size + 4 + 1))
 
2548
  {
 
2549
    null_value= 1;
 
2550
    return 0;
 
2551
  }
 
2552
 
 
2553
  body= ((Byte*)buffer.ptr()) + 4;
 
2554
 
 
2555
  // As far as we have checked res->is_empty() we can use ptr()
 
2556
  if ((err= compress(body, &new_size,
 
2557
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
 
2558
  {
 
2559
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
 
2560
    push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
 
2561
    null_value= 1;
 
2562
    return 0;
 
2563
  }
 
2564
 
 
2565
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
 
2566
  int4store(tmp, res->length() & 0x3FFFFFFF);
 
2567
 
 
2568
  /* This is to ensure that things works for CHAR fields, which trim ' ': */
 
2569
  last_char= ((char*)body)+new_size-1;
 
2570
  if (*last_char == ' ')
 
2571
  {
 
2572
    *++last_char= '.';
 
2573
    new_size++;
 
2574
  }
 
2575
 
 
2576
  buffer.length((uint32_t)new_size + 4);
 
2577
  return &buffer;
 
2578
}
 
2579
 
 
2580
 
 
2581
String *Item_func_uncompress::val_str(String *str)
 
2582
{
 
2583
  assert(fixed == 1);
 
2584
  String *res= args[0]->val_str(str);
 
2585
  ulong new_size;
 
2586
  int err;
 
2587
  uint code;
 
2588
 
 
2589
  if (!res)
 
2590
    goto err;
 
2591
  null_value= 0;
 
2592
  if (res->is_empty())
 
2593
    return res;
 
2594
 
 
2595
  /* If length is less than 4 bytes, data is corrupt */
 
2596
  if (res->length() <= 4)
 
2597
  {
 
2598
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2599
                        ER_ZLIB_Z_DATA_ERROR,
 
2600
                        ER(ER_ZLIB_Z_DATA_ERROR));
 
2601
    goto err;
 
2602
  }
 
2603
 
 
2604
  /* Size of uncompressed data is stored as first 4 bytes of field */
 
2605
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
 
2606
  if (new_size > current_thd->variables.max_allowed_packet)
 
2607
  {
 
2608
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2609
                        ER_TOO_BIG_FOR_UNCOMPRESS,
 
2610
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
 
2611
                        current_thd->variables.max_allowed_packet);
 
2612
    goto err;
 
2613
  }
 
2614
  if (buffer.realloc((uint32_t)new_size))
 
2615
    goto err;
 
2616
 
 
2617
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
 
2618
                       ((const Bytef*)res->ptr())+4,res->length())) == Z_OK)
 
2619
  {
 
2620
    buffer.length((uint32_t) new_size);
 
2621
    return &buffer;
 
2622
  }
 
2623
 
 
2624
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
 
2625
         ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
 
2626
  push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
 
2627
 
 
2628
err:
 
2629
  null_value= 1;
 
2630
  return 0;
 
2631
}
 
2632
#endif
 
2633
 
 
2634
/*
 
2635
  UUID, as in
 
2636
    DCE 1.1: Remote Procedure Call,
 
2637
    Open Group Technical Standard Document Number C706, October 1997,
 
2638
    (supersedes C309 DCE: Remote Procedure Call 8/1994,
 
2639
    which was basis for ISO/IEC 11578:1996 specification)
 
2640
*/
 
2641
 
 
2642
static struct rand_struct uuid_rand;
 
2643
static uint nanoseq;
 
2644
static uint64_t uuid_time=0;
 
2645
static char clock_seq_and_node_str[]="-0000-000000000000";
 
2646
 
 
2647
/**
 
2648
  number of 100-nanosecond intervals between
 
2649
  1582-10-15 00:00:00.00 and 1970-01-01 00:00:00.00.
 
2650
*/
 
2651
#define UUID_TIME_OFFSET ((uint64_t) 141427 * 24 * 60 * 60 * 1000 * 10 )
 
2652
 
 
2653
#define UUID_VERSION      0x1000
 
2654
#define UUID_VARIANT      0x8000
 
2655
 
 
2656
static void tohex(char *to, uint from, uint len)
 
2657
{
 
2658
  to+= len;
 
2659
  while (len--)
 
2660
  {
 
2661
    *--to= _dig_vec_lower[from & 15];
 
2662
    from >>= 4;
 
2663
  }
 
2664
}
 
2665
 
 
2666
static void set_clock_seq_str()
 
2667
{
 
2668
  uint16_t clock_seq= ((uint)(my_rnd(&uuid_rand)*16383)) | UUID_VARIANT;
 
2669
  tohex(clock_seq_and_node_str+1, clock_seq, 4);
 
2670
  nanoseq= 0;
 
2671
}
2503
2672
 
2504
2673
String *Item_func_uuid::val_str(String *str)
2505
2674
{
2506
 
  uuid_t uu;
2507
 
  char *uuid_string;
2508
 
 
2509
 
  /* 36 characters for uuid string +1 for NULL */
2510
 
  str->realloc(36+1);
2511
 
  str->length(36);
 
2675
  assert(fixed == 1);
 
2676
  char *s;
 
2677
  THD *thd= current_thd;
 
2678
 
 
2679
  pthread_mutex_lock(&LOCK_uuid_generator);
 
2680
  if (! uuid_time) /* first UUID() call. initializing data */
 
2681
  {
 
2682
    ulong tmp= sql_rnd();
 
2683
    uchar mac[6];
 
2684
    int i;
 
2685
    if (my_gethwaddr(mac))
 
2686
    {
 
2687
      /* purecov: begin inspected */
 
2688
      /*
 
2689
        generating random "hardware addr"
 
2690
        and because specs explicitly specify that it should NOT correlate
 
2691
        with a clock_seq value (initialized random below), we use a separate
 
2692
        randominit() here
 
2693
      */
 
2694
      randominit(&uuid_rand, tmp + (ulong) thd, tmp + (ulong)global_query_id);
 
2695
      for (i=0; i < (int)sizeof(mac); i++)
 
2696
        mac[i]=(uchar)(my_rnd(&uuid_rand)*255);
 
2697
      /* purecov: end */    
 
2698
    }
 
2699
    s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1;
 
2700
    for (i=sizeof(mac)-1 ; i>=0 ; i--)
 
2701
    {
 
2702
      *--s=_dig_vec_lower[mac[i] & 15];
 
2703
      *--s=_dig_vec_lower[mac[i] >> 4];
 
2704
    }
 
2705
    randominit(&uuid_rand, tmp + (ulong) server_start_time,
 
2706
               tmp + (ulong) thd->status_var.bytes_sent);
 
2707
    set_clock_seq_str();
 
2708
  }
 
2709
 
 
2710
  uint64_t tv=my_getsystime() + UUID_TIME_OFFSET + nanoseq;
 
2711
  if (unlikely(tv < uuid_time))
 
2712
    set_clock_seq_str();
 
2713
  else if (unlikely(tv == uuid_time))
 
2714
  {
 
2715
    /* special protection from low-res system clocks */
 
2716
    nanoseq++;
 
2717
    tv++;
 
2718
  }
 
2719
  else
 
2720
  {
 
2721
    if (nanoseq)
 
2722
    {
 
2723
      tv-=nanoseq;
 
2724
      nanoseq=0;
 
2725
    }
 
2726
    assert(tv > uuid_time);
 
2727
  }
 
2728
  uuid_time=tv;
 
2729
  pthread_mutex_unlock(&LOCK_uuid_generator);
 
2730
 
 
2731
  uint32_t time_low=            (uint32_t) (tv & 0xFFFFFFFF);
 
2732
  uint16_t time_mid=            (uint16_t) ((tv >> 32) & 0xFFFF);
 
2733
  uint16_t time_hi_and_version= (uint16_t) ((tv >> 48) | UUID_VERSION);
 
2734
 
 
2735
  str->realloc(UUID_LENGTH+1);
 
2736
  str->length(UUID_LENGTH);
2512
2737
  str->set_charset(system_charset_info);
2513
 
  uuid_string= (char *) str->ptr();
2514
 
  uuid_generate_time(uu);
2515
 
  uuid_unparse(uu, uuid_string);
2516
 
 
 
2738
  s=(char *) str->ptr();
 
2739
  s[8]=s[13]='-';
 
2740
  tohex(s, time_low, 8);
 
2741
  tohex(s+9, time_mid, 4);
 
2742
  tohex(s+14, time_hi_and_version, 4);
 
2743
  stpcpy(s+18, clock_seq_and_node_str);
2517
2744
  return str;
2518
2745
}