~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/create.cc

  • Committer: Olaf van der Spek
  • Date: 2011-04-16 13:00:30 UTC
  • mto: (2280.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 2281.
  • Revision ID: olafvdspek@gmail.com-20110416130030-53mitypb2aenxpkb
Prune

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 "config.h"
 
23
#include <config.h>
24
24
#include <drizzled/item/create.h>
25
25
#include <drizzled/item/func.h>
26
26
#include <drizzled/error.h>
 
27
#include <drizzled/system_variables.h>
27
28
 
28
 
#include "drizzled/function_container.h"
 
29
#include <drizzled/function_container.h>
29
30
 
30
31
#include <drizzled/function/str/binary.h>
31
32
#include <drizzled/function/str/concat.h>
83
84
#include <drizzled/function/math/tan.h>
84
85
#include <drizzled/function/units.h>
85
86
 
 
87
#include <drizzled/function/cast/boolean.h>
 
88
#include <drizzled/function/cast/signed.h>
 
89
#include <drizzled/function/cast/time.h>
 
90
#include <drizzled/function/cast/unsigned.h>
 
91
 
86
92
using namespace std;
87
93
 
88
 
namespace drizzled
89
 
{
90
 
 
91
 
class Item;
92
 
 
 
94
namespace drizzled {
93
95
 
94
96
/*
95
97
=============================================================================
1048
1050
  if (params)
1049
1051
  {
1050
1052
    Item *param;
1051
 
    List_iterator<Item> it(*params);
 
1053
    List<Item>::iterator it(params->begin());
1052
1054
    while ((param= it++))
1053
1055
    {
1054
1056
      if (! param->is_autogenerated_name)
1065
1067
Item*
1066
1068
Create_udf_func::create(Session *session, LEX_STRING name, List<Item> *item_list)
1067
1069
{
1068
 
  const plugin::Function *udf= plugin::Function::get(name.str, name.length);
 
1070
  const plugin::Function *udf= plugin::Function::get(std::string(name.str, name.length));
1069
1071
  assert(udf);
1070
1072
  return create(session, udf, item_list);
1071
1073
}
1079
1081
  int arg_count= 0;
1080
1082
 
1081
1083
  if (item_list != NULL)
1082
 
    arg_count= item_list->elements;
 
1084
    arg_count= item_list->size();
1083
1085
 
1084
1086
  func= (*udf)(session->mem_root);
1085
1087
 
1115
1117
  int arg_count= 0;
1116
1118
 
1117
1119
  if (item_list != NULL)
1118
 
    arg_count= item_list->elements;
 
1120
    arg_count= item_list->size();
1119
1121
 
1120
1122
  if (arg_count != 0)
1121
1123
  {
1133
1135
  int arg_count= 0;
1134
1136
 
1135
1137
  if (item_list)
1136
 
    arg_count= item_list->elements;
 
1138
    arg_count= item_list->size();
1137
1139
 
1138
1140
  if (arg_count != 1)
1139
1141
  {
1159
1161
  int arg_count= 0;
1160
1162
 
1161
1163
  if (item_list)
1162
 
    arg_count= item_list->elements;
 
1164
    arg_count= item_list->size();
1163
1165
 
1164
1166
  if (arg_count != 2)
1165
1167
  {
1187
1189
  int arg_count= 0;
1188
1190
 
1189
1191
  if (item_list)
1190
 
    arg_count= item_list->elements;
 
1192
    arg_count= item_list->size();
1191
1193
 
1192
1194
  if (arg_count != 3)
1193
1195
  {
1229
1231
  int arg_count= 0;
1230
1232
 
1231
1233
  if (item_list != NULL)
1232
 
    arg_count= item_list->elements;
 
1234
    arg_count= item_list->size();
1233
1235
 
1234
1236
  if (arg_count < 1)
1235
1237
  {
1250
1252
  int arg_count= 0;
1251
1253
 
1252
1254
  if (item_list != NULL)
1253
 
    arg_count= item_list->elements;
 
1255
    arg_count= item_list->size();
1254
1256
 
1255
1257
  /* "WS" stands for "With Separator": this function takes 2+ arguments */
1256
1258
  if (arg_count < 2)
1357
1359
  int arg_count= 0;
1358
1360
 
1359
1361
  if (item_list != NULL)
1360
 
    arg_count= item_list->elements;
 
1362
    arg_count= item_list->size();
1361
1363
 
1362
1364
  switch (arg_count) {
1363
1365
  case 3:
1409
1411
  int arg_count= 0;
1410
1412
 
1411
1413
  if (item_list != NULL)
1412
 
    arg_count= item_list->elements;
 
1414
    arg_count= item_list->size();
1413
1415
 
1414
1416
  if (arg_count < 2)
1415
1417
  {
1457
1459
  int arg_count= 0;
1458
1460
 
1459
1461
  if (item_list != NULL)
1460
 
    arg_count= item_list->elements;
 
1462
    arg_count= item_list->size();
1461
1463
 
1462
1464
  switch (arg_count) {
1463
1465
  case 1:
1494
1496
  int arg_count= 0;
1495
1497
 
1496
1498
  if (item_list != NULL)
1497
 
    arg_count= item_list->elements;
 
1499
    arg_count= item_list->size();
1498
1500
 
1499
1501
  if (arg_count < 2)
1500
1502
  {
1551
1553
  int arg_count= 0;
1552
1554
 
1553
1555
  if (item_list != NULL)
1554
 
    arg_count= item_list->elements;
 
1556
    arg_count= item_list->size();
1555
1557
 
1556
1558
  switch (arg_count) {
1557
1559
  case 0:
1594
1596
  int arg_count= 0;
1595
1597
 
1596
1598
  if (item_list != NULL)
1597
 
    arg_count= item_list->elements;
 
1599
    arg_count= item_list->size();
1598
1600
 
1599
1601
  if (arg_count < 2)
1600
1602
  {
1624
1626
  int arg_count= 0;
1625
1627
 
1626
1628
  if (item_list != NULL)
1627
 
    arg_count= item_list->elements;
 
1629
    arg_count= item_list->size();
1628
1630
 
1629
1631
  switch (arg_count) {
1630
1632
  case 2:
1690
1692
  int arg_count= 0;
1691
1693
 
1692
1694
  if (item_list != NULL)
1693
 
    arg_count= item_list->elements;
 
1695
    arg_count= item_list->size();
1694
1696
 
1695
1697
  if (arg_count < 2)
1696
1698
  {
1776
1778
  int arg_count= 0;
1777
1779
 
1778
1780
  if (item_list != NULL)
1779
 
    arg_count= item_list->elements;
 
1781
    arg_count= item_list->size();
1780
1782
 
1781
1783
  switch (arg_count) {
1782
1784
  case 1:
1849
1851
    The parsed item tree should not depend on
1850
1852
    <code>session->variables.collation_connection</code>.
1851
1853
  */
1852
 
  const CHARSET_INFO * const cs= session->variables.getCollation();
 
1854
  const charset_info_st * const cs= session->variables.getCollation();
1853
1855
  Item *sp;
1854
1856
 
1855
1857
  if (cs->mbminlen > 1)
1919
1921
  int arg_count= 0;
1920
1922
 
1921
1923
  if (item_list != NULL)
1922
 
    arg_count= item_list->elements;
 
1924
    arg_count= item_list->size();
1923
1925
 
1924
1926
  switch (arg_count) {
1925
1927
  case 0:
2042
2044
  {
2043
2045
    func_name.assign(func->name.str, func->name.length);
2044
2046
 
2045
 
    FunctionContainer::getMap()[func_name]= func;
 
2047
    FunctionContainer::getMap()[func_name]= func->builder;
2046
2048
  }
2047
2049
 
2048
2050
  return 0;
2052
2054
Create_func *
2053
2055
find_native_function_builder(LEX_STRING name)
2054
2056
{
2055
 
  Native_func_registry *func;
2056
2057
  Create_func *builder= NULL;
2057
2058
 
2058
2059
  string func_name(name.str, name.length);
2059
2060
 
2060
 
  NativeFunctionsMap::iterator func_iter=
 
2061
  FunctionContainer::Map::iterator func_iter=
2061
2062
    FunctionContainer::getMap().find(func_name);
2062
2063
 
2063
2064
  if (func_iter != FunctionContainer::getMap().end())
2064
2065
  {
2065
 
    func= (*func_iter).second;
2066
 
    builder= func->builder;
 
2066
    builder= (*func_iter).second;
2067
2067
  }
2068
2068
 
2069
2069
  return builder;
2071
2071
 
2072
2072
 
2073
2073
Item*
2074
 
create_func_char_cast(Session *session, Item *a, int len, const CHARSET_INFO * const cs)
 
2074
create_func_char_cast(Session *session, Item *a, int len, const charset_info_st * const cs)
2075
2075
{
2076
 
  const CHARSET_INFO * const real_cs= (cs ? cs : session->variables.getCollation());
 
2076
  const charset_info_st * const real_cs= (cs ? cs : session->variables.getCollation());
2077
2077
  return new (session->mem_root) Item_char_typecast(a, len, real_cs);
2078
2078
}
2079
2079
 
2081
2081
Item *
2082
2082
create_func_cast(Session *session, Item *a, Cast_target cast_type,
2083
2083
                 const char *c_len, const char *c_dec,
2084
 
                 const CHARSET_INFO * const cs)
 
2084
                 const charset_info_st * const cs)
2085
2085
{
2086
 
  Item *res;
 
2086
  Item *res= NULL;
2087
2087
  uint32_t len;
2088
2088
  uint32_t dec;
2089
2089
 
2090
2090
  switch (cast_type) {
 
2091
  case ITEM_CAST_SIGNED:
 
2092
    res= new (session->mem_root) function::cast::Signed(a);
 
2093
    break;
 
2094
 
 
2095
  case ITEM_CAST_UNSIGNED:
 
2096
    res= new (session->mem_root) function::cast::Unsigned(a);
 
2097
    break;
 
2098
 
2091
2099
  case ITEM_CAST_BINARY:
2092
2100
    res= new (session->mem_root) Item_func_binary(a);
2093
2101
    break;
 
2102
 
 
2103
  case ITEM_CAST_BOOLEAN:
 
2104
    res= new (session->mem_root) function::cast::Boolean(a);
 
2105
    break;
 
2106
 
 
2107
  case ITEM_CAST_TIME:
 
2108
    res= new (session->mem_root) function::cast::Time(a);
 
2109
    break;
 
2110
 
2094
2111
  case ITEM_CAST_DATE:
2095
2112
    res= new (session->mem_root) Item_date_typecast(a);
2096
2113
    break;
 
2114
 
2097
2115
  case ITEM_CAST_DATETIME:
2098
2116
    res= new (session->mem_root) Item_datetime_typecast(a);
2099
2117
    break;
 
2118
 
2100
2119
  case ITEM_CAST_DECIMAL:
2101
 
  {
2102
 
    len= c_len ? atoi(c_len) : 0;
2103
 
    dec= c_dec ? atoi(c_dec) : 0;
2104
 
    my_decimal_trim(&len, &dec);
2105
 
    if (len < dec)
2106
 
    {
2107
 
      my_error(ER_M_BIGGER_THAN_D, MYF(0), "");
2108
 
      return 0;
2109
 
    }
2110
 
    if (len > DECIMAL_MAX_PRECISION)
2111
 
    {
2112
 
      my_error(ER_TOO_BIG_PRECISION, MYF(0), len, a->name,
2113
 
               DECIMAL_MAX_PRECISION);
2114
 
      return 0;
2115
 
    }
2116
 
    if (dec > DECIMAL_MAX_SCALE)
2117
 
    {
2118
 
      my_error(ER_TOO_BIG_SCALE, MYF(0), dec, a->name,
2119
 
               DECIMAL_MAX_SCALE);
2120
 
      return 0;
2121
 
    }
2122
 
    res= new (session->mem_root) Item_decimal_typecast(a, len, dec);
2123
 
    break;
2124
 
  }
 
2120
    {
 
2121
      len= c_len ? atoi(c_len) : 0;
 
2122
      dec= c_dec ? atoi(c_dec) : 0;
 
2123
      class_decimal_trim(&len, &dec);
 
2124
      if (len < dec)
 
2125
      {
 
2126
        my_error(ER_M_BIGGER_THAN_D, MYF(0), "");
 
2127
        return 0;
 
2128
      }
 
2129
      if (len > DECIMAL_MAX_PRECISION)
 
2130
      {
 
2131
        my_error(ER_TOO_BIG_PRECISION, MYF(0), len, a->name,
 
2132
                 DECIMAL_MAX_PRECISION);
 
2133
        return 0;
 
2134
      }
 
2135
      if (dec > DECIMAL_MAX_SCALE)
 
2136
      {
 
2137
        my_error(ER_TOO_BIG_SCALE, MYF(0), dec, a->name,
 
2138
                 DECIMAL_MAX_SCALE);
 
2139
        return 0;
 
2140
      }
 
2141
      res= new (session->mem_root) Item_decimal_typecast(a, len, dec);
 
2142
      break;
 
2143
    }
2125
2144
  case ITEM_CAST_CHAR:
2126
 
  {
2127
 
    len= c_len ? atoi(c_len) : -1;
2128
 
    res= create_func_char_cast(session, a, len, cs);
2129
 
    break;
2130
 
  }
2131
 
  default:
2132
 
  {
2133
 
    assert(0);
2134
 
    res= 0;
2135
 
    break;
2136
 
  }
2137
 
  }
 
2145
    {
 
2146
      len= c_len ? atoi(c_len) : -1;
 
2147
      res= create_func_char_cast(session, a, len, cs);
 
2148
      break;
 
2149
    }
 
2150
  }
 
2151
 
2138
2152
  return res;
2139
2153
}
2140
2154