Drizzled Public API Documentation

typecast.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <drizzled/charset.h>
00023 #include <drizzled/field.h>
00024 #include <drizzled/function/str/strfunc.h>
00025 #include <drizzled/temporal.h>
00026 
00027 namespace drizzled {
00028 
00029 class Item_typecast : public Item_str_func
00030 {
00031 public:
00032   using Item_func::tmp_table_field;
00033 
00034   Item_typecast(Item *a) :Item_str_func(a) {}
00035   String *val_str(String *a)
00036   {
00037     assert(fixed == 1);
00038     String *tmp=args[0]->val_str(a);
00039     null_value=args[0]->null_value;
00040     if (tmp)
00041       tmp->set_charset(collation.collation);
00042     return tmp;
00043   }
00044   void fix_length_and_dec()
00045   {
00046     collation.set(&my_charset_bin);
00047     max_length=args[0]->max_length;
00048   }
00049   virtual const char* cast_type() const= 0;
00050   virtual void print(String *str);
00051 };
00052 
00053 class Item_typecast_maybe_null :public Item_typecast
00054 {
00055 public:
00056   Item_typecast_maybe_null(Item *a) :Item_typecast(a) {}
00057   void fix_length_and_dec()
00058   {
00059     collation.set(&my_charset_bin);
00060     max_length=args[0]->max_length;
00061     maybe_null= 1;
00062   }
00063 };
00064 
00065 class Item_char_typecast :public Item_typecast
00066 {
00067   int cast_length;
00068   const charset_info_st *cast_cs, *from_cs;
00069   bool charset_conversion;
00070   String tmp_value;
00071 public:
00072   using Item_func::tmp_table_field;
00073 
00074   Item_char_typecast(Item *a, int length_arg, const charset_info_st * const cs_arg)
00075     :Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
00076   enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
00077   bool eq(const Item *item, bool binary_cmp) const;
00078   const char *func_name() const { return "cast_as_char"; }
00079   const char* cast_type() const { return "char"; }
00080   String *val_str(String *a);
00081   void fix_length_and_dec();
00082   virtual void print(String *str);
00083 };
00084 
00085 class Item_date_typecast :public Item_typecast_maybe_null
00086 {
00087 public:
00088   using Item_func::tmp_table_field;
00089 
00090   Item_date_typecast(Item *a) :
00091     Item_typecast_maybe_null(a)
00092   {}
00093 
00094   const char *func_name() const { return "cast_as_date"; }
00095   String *val_str(String *str);
00096   bool get_date(type::Time &ltime, uint32_t fuzzy_date);
00097   bool get_time(type::Time &ltime);
00098   const char *cast_type() const { return "date"; }
00099   enum_field_types field_type() const { return DRIZZLE_TYPE_DATE; }
00100 
00101   Field *tmp_table_field(Table *table)
00102   {
00103     return tmp_table_field_from_field_type(table, 0);
00104   }
00105 
00106   void fix_length_and_dec()
00107   {
00108     collation.set(&my_charset_bin);
00109     max_length= 10;
00110     maybe_null= 1;
00111   }
00112 
00113   bool result_as_int64_t() { return true; }
00114   int64_t val_int();
00115   double val_real() { return (double) val_int(); }
00116 
00117   type::Decimal *val_decimal(type::Decimal *decimal_value)
00118   {
00119     assert(fixed == 1);
00120     return  val_decimal_from_date(decimal_value);
00121   }
00122 
00123   int save_in_field(Field *field, bool )
00124   {
00125     return save_date_in_field(field);
00126   }
00127 };
00128 
00129 class Item_datetime_typecast :public Item_typecast_maybe_null
00130 {
00131 public:
00132   using Item_func::tmp_table_field;
00133 
00134   Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
00135   const char *func_name() const { return "cast_as_datetime"; }
00136   String *val_str(String *str);
00137   const char *cast_type() const { return "datetime"; }
00138   enum_field_types field_type() const { return DRIZZLE_TYPE_DATETIME; }
00139 
00140   Field *tmp_table_field(Table *table)
00141   {
00142     return tmp_table_field_from_field_type(table, 0);
00143   }
00144 
00145   void fix_length_and_dec()
00146   {
00147     collation.set(&my_charset_bin);
00148     maybe_null= 1;
00149     max_length= DateTime::MAX_STRING_LENGTH * MY_CHARSET_BIN_MB_MAXLEN;
00150     decimals= DATETIME_DEC;
00151   }
00152 
00153   bool result_as_int64_t()
00154   {
00155     return true;
00156   }
00157 
00158   int64_t val_int();
00159   double val_real()
00160   {
00161     return val_real_from_decimal();
00162   }
00163 
00164   double val()
00165   {
00166     return (double) val_int();
00167   }
00168 
00169   type::Decimal *val_decimal(type::Decimal *decimal_value)
00170   {
00171     assert(fixed == 1);
00172     return  val_decimal_from_date(decimal_value);
00173   }
00174   int save_in_field(Field *field, bool )
00175   {
00176     return save_date_in_field(field);
00177   }
00178 };
00179 
00180 } /* namespace drizzled */
00181