~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_create.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 17:07:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705170746-8aq11u9fuwtfwy85
Removed my_alarm. Made my_lock only do the non-alarm version. Moved my_lock to MyISAM where it belongs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  Functions to create an item. Used by sql_yac.yy
21
21
*/
22
22
 
23
 
#include <drizzled/server_includes.h>
 
23
#include "mysql_priv.h"
 
24
#include "item_create.h"
24
25
 
25
26
/*
26
27
=============================================================================
404
405
  virtual ~Create_func_cot() {}
405
406
};
406
407
 
 
408
 
 
409
class Create_func_crc32 : public Create_func_arg1
 
410
{
 
411
public:
 
412
  virtual Item *create(THD *thd, Item *arg1);
 
413
 
 
414
  static Create_func_crc32 s_singleton;
 
415
 
 
416
protected:
 
417
  Create_func_crc32() {}
 
418
  virtual ~Create_func_crc32() {}
 
419
};
 
420
 
 
421
 
407
422
class Create_func_date_format : public Create_func_arg2
408
423
{
409
424
public:
521
536
};
522
537
 
523
538
 
 
539
class Create_func_encode : public Create_func_arg2
 
540
{
 
541
public:
 
542
  virtual Item *create(THD *thd, Item *arg1, Item *arg2);
 
543
 
 
544
  static Create_func_encode s_singleton;
 
545
 
 
546
protected:
 
547
  Create_func_encode() {}
 
548
  virtual ~Create_func_encode() {}
 
549
};
 
550
 
 
551
 
 
552
class Create_func_encrypt : public Create_native_func
 
553
{
 
554
public:
 
555
  virtual Item *create_native(THD *thd, LEX_STRING name, List<Item> *item_list);
 
556
 
 
557
  static Create_func_encrypt s_singleton;
 
558
 
 
559
protected:
 
560
  Create_func_encrypt() {}
 
561
  virtual ~Create_func_encrypt() {}
 
562
};
 
563
 
 
564
 
524
565
class Create_func_exp : public Create_func_arg1
525
566
{
526
567
public:
923
964
  virtual ~Create_func_master_pos_wait() {}
924
965
};
925
966
 
 
967
 
 
968
class Create_func_md5 : public Create_func_arg1
 
969
{
 
970
public:
 
971
  virtual Item *create(THD *thd, Item *arg1);
 
972
 
 
973
  static Create_func_md5 s_singleton;
 
974
 
 
975
protected:
 
976
  Create_func_md5() {}
 
977
  virtual ~Create_func_md5() {}
 
978
};
 
979
 
 
980
 
926
981
class Create_func_monthname : public Create_func_arg1
927
982
{
928
983
public:
1365
1420
};
1366
1421
 
1367
1422
 
 
1423
class Create_func_uuid_short : public Create_func_arg0
 
1424
{
 
1425
public:
 
1426
  virtual Item *create(THD *thd);
 
1427
 
 
1428
  static Create_func_uuid_short s_singleton;
 
1429
 
 
1430
protected:
 
1431
  Create_func_uuid_short() {}
 
1432
  virtual ~Create_func_uuid_short() {}
 
1433
};
 
1434
 
 
1435
 
1368
1436
class Create_func_version : public Create_func_arg0
1369
1437
{
1370
1438
public:
1447
1515
}
1448
1516
 
1449
1517
 
 
1518
#ifdef HAVE_DLOPEN
1450
1519
Create_udf_func Create_udf_func::s_singleton;
1451
1520
 
1452
1521
Item*
1453
1522
Create_udf_func::create(THD *thd, LEX_STRING name, List<Item> *item_list)
1454
1523
{
1455
1524
  udf_func *udf= find_udf(name.str, name.length);
1456
 
  assert(udf);
 
1525
  DBUG_ASSERT(udf);
1457
1526
  return create(thd, udf, item_list);
1458
1527
}
1459
1528
 
1461
1530
Item*
1462
1531
Create_udf_func::create(THD *thd, udf_func *udf, List<Item> *item_list)
1463
1532
{
1464
 
  Item_func *func= NULL;
 
1533
  Item *func= NULL;
1465
1534
  int arg_count= 0;
1466
1535
 
1467
1536
  if (item_list != NULL)
1469
1538
 
1470
1539
  thd->lex->set_stmt_unsafe();
1471
1540
 
1472
 
  func= udf->create_func(thd->mem_root);
1473
 
 
1474
 
  func->set_arguments(*item_list);
1475
 
 
 
1541
  DBUG_ASSERT(   (udf->type == UDFTYPE_FUNCTION)
 
1542
              || (udf->type == UDFTYPE_AGGREGATE));
 
1543
 
 
1544
  switch(udf->returns) {
 
1545
  case STRING_RESULT:
 
1546
  {
 
1547
    if (udf->type == UDFTYPE_FUNCTION)
 
1548
    {
 
1549
      if (arg_count)
 
1550
        func= new (thd->mem_root) Item_func_udf_str(udf, *item_list);
 
1551
      else
 
1552
        func= new (thd->mem_root) Item_func_udf_str(udf);
 
1553
    }
 
1554
    else
 
1555
    {
 
1556
      if (arg_count)
 
1557
        func= new (thd->mem_root) Item_sum_udf_str(udf, *item_list);
 
1558
      else
 
1559
        func= new (thd->mem_root) Item_sum_udf_str(udf);
 
1560
    }
 
1561
    break;
 
1562
  }
 
1563
  case REAL_RESULT:
 
1564
  {
 
1565
    if (udf->type == UDFTYPE_FUNCTION)
 
1566
    {
 
1567
      if (arg_count)
 
1568
        func= new (thd->mem_root) Item_func_udf_float(udf, *item_list);
 
1569
      else
 
1570
        func= new (thd->mem_root) Item_func_udf_float(udf);
 
1571
    }
 
1572
    else
 
1573
    {
 
1574
      if (arg_count)
 
1575
        func= new (thd->mem_root) Item_sum_udf_float(udf, *item_list);
 
1576
      else
 
1577
        func= new (thd->mem_root) Item_sum_udf_float(udf);
 
1578
    }
 
1579
    break;
 
1580
  }
 
1581
  case INT_RESULT:
 
1582
  {
 
1583
    if (udf->type == UDFTYPE_FUNCTION)
 
1584
    {
 
1585
      if (arg_count)
 
1586
        func= new (thd->mem_root) Item_func_udf_int(udf, *item_list);
 
1587
      else
 
1588
        func= new (thd->mem_root) Item_func_udf_int(udf);
 
1589
    }
 
1590
    else
 
1591
    {
 
1592
      if (arg_count)
 
1593
        func= new (thd->mem_root) Item_sum_udf_int(udf, *item_list);
 
1594
      else
 
1595
        func= new (thd->mem_root) Item_sum_udf_int(udf);
 
1596
    }
 
1597
    break;
 
1598
  }
 
1599
  case DECIMAL_RESULT:
 
1600
  {
 
1601
    if (udf->type == UDFTYPE_FUNCTION)
 
1602
    {
 
1603
      if (arg_count)
 
1604
        func= new (thd->mem_root) Item_func_udf_decimal(udf, *item_list);
 
1605
      else
 
1606
        func= new (thd->mem_root) Item_func_udf_decimal(udf);
 
1607
    }
 
1608
    else
 
1609
    {
 
1610
      if (arg_count)
 
1611
        func= new (thd->mem_root) Item_sum_udf_decimal(udf, *item_list);
 
1612
      else
 
1613
        func= new (thd->mem_root) Item_sum_udf_decimal(udf);
 
1614
    }
 
1615
    break;
 
1616
  }
 
1617
  default:
 
1618
  {
 
1619
    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "UDF return type");
 
1620
  }
 
1621
  }
1476
1622
  return func;
1477
1623
}
 
1624
#endif
1478
1625
 
1479
1626
 
1480
1627
Item*
1679
1826
Item*
1680
1827
Create_func_bin::create(THD *thd, Item *arg1)
1681
1828
{
1682
 
  Item *i10= new (thd->mem_root) Item_int((int32_t) 10,2);
1683
 
  Item *i2= new (thd->mem_root) Item_int((int32_t) 2,1);
 
1829
  Item *i10= new (thd->mem_root) Item_int((int32) 10,2);
 
1830
  Item *i2= new (thd->mem_root) Item_int((int32) 2,1);
1684
1831
  return new (thd->mem_root) Item_func_conv(arg1, i10, i2);
1685
1832
}
1686
1833
 
1810
1957
  return new (thd->mem_root) Item_func_div(i1, i2);
1811
1958
}
1812
1959
 
 
1960
 
 
1961
Create_func_crc32 Create_func_crc32::s_singleton;
 
1962
 
 
1963
Item*
 
1964
Create_func_crc32::create(THD *thd, Item *arg1)
 
1965
{
 
1966
  return new (thd->mem_root) Item_func_crc32(arg1);
 
1967
}
 
1968
 
 
1969
 
1813
1970
Create_func_date_format Create_func_date_format::s_singleton;
1814
1971
 
1815
1972
Item*
1867
2024
}
1868
2025
 
1869
2026
 
 
2027
Create_func_decode Create_func_decode::s_singleton;
 
2028
 
 
2029
Item*
 
2030
Create_func_decode::create(THD *thd, Item *arg1, Item *arg2)
 
2031
{
 
2032
  return new (thd->mem_root) Item_func_decode(arg1, arg2);
 
2033
}
 
2034
 
 
2035
 
1870
2036
Create_func_degrees Create_func_degrees::s_singleton;
1871
2037
 
1872
2038
Item*
1898
2064
}
1899
2065
 
1900
2066
 
 
2067
Create_func_encode Create_func_encode::s_singleton;
 
2068
 
 
2069
Item*
 
2070
Create_func_encode::create(THD *thd, Item *arg1, Item *arg2)
 
2071
{
 
2072
  return new (thd->mem_root) Item_func_encode(arg1, arg2);
 
2073
}
 
2074
 
 
2075
 
 
2076
Create_func_encrypt Create_func_encrypt::s_singleton;
 
2077
 
 
2078
Item*
 
2079
Create_func_encrypt::create_native(THD *thd, LEX_STRING name,
 
2080
                                   List<Item> *item_list)
 
2081
{
 
2082
  Item *func= NULL;
 
2083
  int arg_count= 0;
 
2084
 
 
2085
  if (item_list != NULL)
 
2086
    arg_count= item_list->elements;
 
2087
 
 
2088
  switch (arg_count) {
 
2089
  case 1:
 
2090
  {
 
2091
    Item *param_1= item_list->pop();
 
2092
    func= new (thd->mem_root) Item_func_encrypt(param_1);
 
2093
    break;
 
2094
  }
 
2095
  case 2:
 
2096
  {
 
2097
    Item *param_1= item_list->pop();
 
2098
    Item *param_2= item_list->pop();
 
2099
    func= new (thd->mem_root) Item_func_encrypt(param_1, param_2);
 
2100
    break;
 
2101
  }
 
2102
  default:
 
2103
  {
 
2104
    my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name.str);
 
2105
    break;
 
2106
  }
 
2107
  }
 
2108
 
 
2109
  return func;
 
2110
}
 
2111
 
 
2112
 
1901
2113
Create_func_exp Create_func_exp::s_singleton;
1902
2114
 
1903
2115
Item*
2417
2629
}
2418
2630
 
2419
2631
 
 
2632
Create_func_md5 Create_func_md5::s_singleton;
 
2633
 
 
2634
Item*
 
2635
Create_func_md5::create(THD *thd, Item *arg1)
 
2636
{
 
2637
  return new (thd->mem_root) Item_func_md5(arg1);
 
2638
}
 
2639
 
 
2640
 
2420
2641
Create_func_monthname Create_func_monthname::s_singleton;
2421
2642
 
2422
2643
Item*
2440
2661
Item*
2441
2662
Create_func_oct::create(THD *thd, Item *arg1)
2442
2663
{
2443
 
  Item *i10= new (thd->mem_root) Item_int((int32_t) 10,2);
2444
 
  Item *i8= new (thd->mem_root) Item_int((int32_t) 8,1);
 
2664
  Item *i10= new (thd->mem_root) Item_int((int32) 10,2);
 
2665
  Item *i8= new (thd->mem_root) Item_int((int32) 8,1);
2445
2666
  return new (thd->mem_root) Item_func_conv(arg1, i10, i8);
2446
2667
}
2447
2668
 
2648
2869
    The parsed item tree should not depend on
2649
2870
    <code>thd->variables.collation_connection</code>.
2650
2871
  */
2651
 
  const CHARSET_INFO * const cs= thd->variables.collation_connection;
 
2872
  CHARSET_INFO *cs= thd->variables.collation_connection;
2652
2873
  Item *sp;
2653
2874
 
2654
2875
  if (cs->mbminlen > 1)
2655
2876
  {
2656
 
    uint32_t dummy_errors;
 
2877
    uint dummy_errors;
2657
2878
    sp= new (thd->mem_root) Item_string("", 0, cs, DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
2658
 
    sp->str_value.copy(" ", 1, &my_charset_utf8_general_ci, cs, &dummy_errors);
 
2879
    sp->str_value.copy(" ", 1, &my_charset_latin1, cs, &dummy_errors);
2659
2880
  }
2660
2881
  else
2661
2882
  {
2819
3040
}
2820
3041
 
2821
3042
 
 
3043
Create_func_uuid_short Create_func_uuid_short::s_singleton;
 
3044
 
 
3045
Item*
 
3046
Create_func_uuid_short::create(THD *thd)
 
3047
{
 
3048
  thd->lex->set_stmt_unsafe();
 
3049
  return new (thd->mem_root) Item_func_uuid_short();
 
3050
}
 
3051
 
 
3052
 
2822
3053
Create_func_version Create_func_version::s_singleton;
2823
3054
 
2824
3055
Item*
2927
3158
  { { C_STRING_WITH_LEN("CONV") }, BUILDER(Create_func_conv)},
2928
3159
  { { C_STRING_WITH_LEN("COS") }, BUILDER(Create_func_cos)},
2929
3160
  { { C_STRING_WITH_LEN("COT") }, BUILDER(Create_func_cot)},
 
3161
  { { C_STRING_WITH_LEN("CRC32") }, BUILDER(Create_func_crc32)},
2930
3162
  { { C_STRING_WITH_LEN("DATEDIFF") }, BUILDER(Create_func_datediff)},
2931
3163
  { { C_STRING_WITH_LEN("DATE_FORMAT") }, BUILDER(Create_func_date_format)},
2932
3164
  { { C_STRING_WITH_LEN("DAYNAME") }, BUILDER(Create_func_dayname)},
2933
3165
  { { C_STRING_WITH_LEN("DAYOFMONTH") }, BUILDER(Create_func_dayofmonth)},
2934
3166
  { { C_STRING_WITH_LEN("DAYOFWEEK") }, BUILDER(Create_func_dayofweek)},
2935
3167
  { { C_STRING_WITH_LEN("DAYOFYEAR") }, BUILDER(Create_func_dayofyear)},
 
3168
  { { C_STRING_WITH_LEN("DECODE") }, BUILDER(Create_func_decode)},
2936
3169
  { { C_STRING_WITH_LEN("DEGREES") }, BUILDER(Create_func_degrees)},
2937
3170
  { { C_STRING_WITH_LEN("ELT") }, BUILDER(Create_func_elt)},
 
3171
  { { C_STRING_WITH_LEN("ENCODE") }, BUILDER(Create_func_encode)},
 
3172
  { { C_STRING_WITH_LEN("ENCRYPT") }, BUILDER(Create_func_encrypt)},
2938
3173
  { { C_STRING_WITH_LEN("EXP") }, BUILDER(Create_func_exp)},
2939
3174
  { { C_STRING_WITH_LEN("EXPORT_SET") }, BUILDER(Create_func_export_set)},
2940
3175
  { { C_STRING_WITH_LEN("FIELD") }, BUILDER(Create_func_field)},
2967
3202
  { { C_STRING_WITH_LEN("MAKETIME") }, BUILDER(Create_func_maketime)},
2968
3203
  { { C_STRING_WITH_LEN("MAKE_SET") }, BUILDER(Create_func_make_set)},
2969
3204
  { { C_STRING_WITH_LEN("MASTER_POS_WAIT") }, BUILDER(Create_func_master_pos_wait)},
 
3205
  { { C_STRING_WITH_LEN("MD5") }, BUILDER(Create_func_md5)},
2970
3206
  { { C_STRING_WITH_LEN("MONTHNAME") }, BUILDER(Create_func_monthname)},
2971
3207
  { { C_STRING_WITH_LEN("NULLIF") }, BUILDER(Create_func_nullif)},
2972
3208
  { { C_STRING_WITH_LEN("OCT") }, BUILDER(Create_func_oct)},
3003
3239
  { { C_STRING_WITH_LEN("UNIX_TIMESTAMP") }, BUILDER(Create_func_unix_timestamp)},
3004
3240
  { { C_STRING_WITH_LEN("UPPER") }, BUILDER(Create_func_ucase)},
3005
3241
  { { C_STRING_WITH_LEN("UUID") }, BUILDER(Create_func_uuid)},
 
3242
  { { C_STRING_WITH_LEN("UUID_SHORT") }, BUILDER(Create_func_uuid_short)},
3006
3243
  { { C_STRING_WITH_LEN("VERSION") }, BUILDER(Create_func_version)},
3007
3244
  { { C_STRING_WITH_LEN("WEEKDAY") }, BUILDER(Create_func_weekday)},
3008
3245
  { { C_STRING_WITH_LEN("WEEKOFYEAR") }, BUILDER(Create_func_weekofyear)},
3013
3250
 
3014
3251
static HASH native_functions_hash;
3015
3252
 
3016
 
extern "C" unsigned char*
3017
 
get_native_fct_hash_key(const unsigned char *buff, size_t *length,
3018
 
                        bool /* unused */)
 
3253
extern "C" uchar*
 
3254
get_native_fct_hash_key(const uchar *buff, size_t *length,
 
3255
                        my_bool /* unused */)
3019
3256
{
3020
3257
  Native_func_registry *func= (Native_func_registry*) buff;
3021
3258
  *length= func->name.length;
3022
 
  return (unsigned char*) func->name.str;
 
3259
  return (uchar*) func->name.str;
3023
3260
}
3024
3261
 
3025
3262
/*
3032
3269
{
3033
3270
  Native_func_registry *func;
3034
3271
 
 
3272
  DBUG_ENTER("item_create_init");
 
3273
 
3035
3274
  if (hash_init(& native_functions_hash,
3036
3275
                system_charset_info,
3037
3276
                array_elements(func_array),
3040
3279
                (hash_get_key) get_native_fct_hash_key,
3041
3280
                NULL,                          /* Nothing to free */
3042
3281
                MYF(0)))
3043
 
    return(1);
 
3282
    DBUG_RETURN(1);
3044
3283
 
3045
3284
  for (func= func_array; func->builder != NULL; func++)
3046
3285
  {
3047
 
    if (my_hash_insert(& native_functions_hash, (unsigned char*) func))
3048
 
      return(1);
3049
 
  }
3050
 
 
3051
 
  return(0);
 
3286
    if (my_hash_insert(& native_functions_hash, (uchar*) func))
 
3287
      DBUG_RETURN(1);
 
3288
  }
 
3289
 
 
3290
#ifndef DBUG_OFF
 
3291
  for (uint i=0 ; i < native_functions_hash.records ; i++)
 
3292
  {
 
3293
    func= (Native_func_registry*) hash_element(& native_functions_hash, i);
 
3294
    DBUG_PRINT("info", ("native function: %s  length: %u",
 
3295
                        func->name.str, (uint) func->name.length));
 
3296
  }
 
3297
#endif
 
3298
 
 
3299
  DBUG_RETURN(0);
3052
3300
}
3053
3301
 
3054
3302
/*
3059
3307
 
3060
3308
void item_create_cleanup()
3061
3309
{
 
3310
  DBUG_ENTER("item_create_cleanup");
3062
3311
  hash_free(& native_functions_hash);
3063
 
  return;
 
3312
  DBUG_VOID_RETURN;
3064
3313
}
3065
3314
 
3066
3315
Create_func *
3067
 
find_native_function_builder(THD *thd __attribute__((unused)),
3068
 
                             LEX_STRING name)
 
3316
find_native_function_builder(THD *thd, LEX_STRING name)
3069
3317
{
3070
3318
  Native_func_registry *func;
3071
3319
  Create_func *builder= NULL;
3072
3320
 
3073
3321
  /* Thread safe */
3074
3322
  func= (Native_func_registry*) hash_search(& native_functions_hash,
3075
 
                                            (unsigned char*) name.str,
 
3323
                                            (uchar*) name.str,
3076
3324
                                             name.length);
3077
3325
 
3078
3326
  if (func)
3085
3333
 
3086
3334
 
3087
3335
Item*
3088
 
create_func_char_cast(THD *thd, Item *a, int len, const CHARSET_INFO * const cs)
 
3336
create_func_char_cast(THD *thd, Item *a, int len, CHARSET_INFO *cs)
3089
3337
{
3090
 
  const CHARSET_INFO * const real_cs= (cs ? cs : thd->variables.collation_connection);
 
3338
  CHARSET_INFO *real_cs= (cs ? cs : thd->variables.collation_connection);
3091
3339
  return new (thd->mem_root) Item_char_typecast(a, len, real_cs);
3092
3340
}
3093
3341
 
3095
3343
Item *
3096
3344
create_func_cast(THD *thd, Item *a, Cast_target cast_type,
3097
3345
                 const char *c_len, const char *c_dec,
3098
 
                 const CHARSET_INFO * const cs)
 
3346
                 CHARSET_INFO *cs)
3099
3347
{
3100
3348
  Item *res;
3101
 
  uint32_t len;
3102
 
  uint32_t dec;
 
3349
  ulong len;
 
3350
  uint dec;
3103
3351
 
3104
3352
  switch (cast_type) {
3105
3353
  case ITEM_CAST_BINARY:
3153
3401
  }
3154
3402
  default:
3155
3403
  {
3156
 
    assert(0);
 
3404
    DBUG_ASSERT(0);
3157
3405
    res= 0;
3158
3406
    break;
3159
3407
  }