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_info.h>
00023 #include <drizzled/field.h>
00024 #include <drizzled/function/str/strfunc.h>
00025 #include <drizzled/temporal.h>
00026 
00027 namespace drizzled
00028 {
00029 
00030 class Item_typecast :public Item_str_func
00031 {
00032 public:
00033   using Item_func::tmp_table_field;
00034 
00035   Item_typecast(Item *a) :Item_str_func(a) {}
00036   String *val_str(String *a)
00037   {
00038     assert(fixed == 1);
00039     String *tmp=args[0]->val_str(a);
00040     null_value=args[0]->null_value;
00041     if (tmp)
00042       tmp->set_charset(collation.collation);
00043     return tmp;
00044   }
00045   void fix_length_and_dec()
00046   {
00047     collation.set(&my_charset_bin);
00048     max_length=args[0]->max_length;
00049   }
00050   virtual const char* cast_type() const= 0;
00051   virtual void print(String *str);
00052 };
00053 
00054 class Item_typecast_maybe_null :public Item_typecast
00055 {
00056 public:
00057   Item_typecast_maybe_null(Item *a) :Item_typecast(a) {}
00058   void fix_length_and_dec()
00059   {
00060     collation.set(&my_charset_bin);
00061     max_length=args[0]->max_length;
00062     maybe_null= 1;
00063   }
00064 };
00065 
00066 class Item_char_typecast :public Item_typecast
00067 {
00068   int cast_length;
00069   const CHARSET_INFO *cast_cs, *from_cs;
00070   bool charset_conversion;
00071   String tmp_value;
00072 public:
00073   using Item_func::tmp_table_field;
00074 
00075   Item_char_typecast(Item *a, int length_arg, const CHARSET_INFO * const cs_arg)
00076     :Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
00077   enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
00078   bool eq(const Item *item, bool binary_cmp) const;
00079   const char *func_name() const { return "cast_as_char"; }
00080   const char* cast_type() const { return "char"; }
00081   String *val_str(String *a);
00082   void fix_length_and_dec();
00083   virtual void print(String *str);
00084 };
00085 
00086 class Item_date_typecast :public Item_typecast_maybe_null
00087 {
00088 public:
00089   using Item_func::tmp_table_field;
00090 
00091   Item_date_typecast(Item *a) :
00092     Item_typecast_maybe_null(a)
00093   {}
00094 
00095   const char *func_name() const { return "cast_as_date"; }
00096   String *val_str(String *str);
00097   bool get_date(type::Time &ltime, uint32_t fuzzy_date);
00098   bool get_time(type::Time &ltime);
00099   const char *cast_type() const { return "date"; }
00100   enum_field_types field_type() const { return DRIZZLE_TYPE_DATE; }
00101 
00102   Field *tmp_table_field(Table *table)
00103   {
00104     return tmp_table_field_from_field_type(table, 0);
00105   }
00106 
00107   void fix_length_and_dec()
00108   {
00109     collation.set(&my_charset_bin);
00110     max_length= 10;
00111     maybe_null= 1;
00112   }
00113 
00114   bool result_as_int64_t() { return true; }
00115   int64_t val_int();
00116   double val_real() { return (double) val_int(); }
00117 
00118   type::Decimal *val_decimal(type::Decimal *decimal_value)
00119   {
00120     assert(fixed == 1);
00121     return  val_decimal_from_date(decimal_value);
00122   }
00123 
00124   int save_in_field(Field *field, bool )
00125   {
00126     return save_date_in_field(field);
00127   }
00128 };
00129 
00130 class Item_datetime_typecast :public Item_typecast_maybe_null
00131 {
00132 public:
00133   using Item_func::tmp_table_field;
00134 
00135   Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
00136   const char *func_name() const { return "cast_as_datetime"; }
00137   String *val_str(String *str);
00138   const char *cast_type() const { return "datetime"; }
00139   enum_field_types field_type() const { return DRIZZLE_TYPE_DATETIME; }
00140 
00141   Field *tmp_table_field(Table *table)
00142   {
00143     return tmp_table_field_from_field_type(table, 0);
00144   }
00145 
00146   void fix_length_and_dec()
00147   {
00148     collation.set(&my_charset_bin);
00149     maybe_null= 1;
00150     max_length= DateTime::MAX_STRING_LENGTH * MY_CHARSET_BIN_MB_MAXLEN;
00151     decimals= DATETIME_DEC;
00152   }
00153 
00154   bool result_as_int64_t()
00155   {
00156     return true;
00157   }
00158 
00159   int64_t val_int();
00160   double val_real()
00161   {
00162     return val_real_from_decimal();
00163   }
00164 
00165   double val()
00166   {
00167     return (double) val_int();
00168   }
00169 
00170   type::Decimal *val_decimal(type::Decimal *decimal_value)
00171   {
00172     assert(fixed == 1);
00173     return  val_decimal_from_date(decimal_value);
00174   }
00175   int save_in_field(Field *field, bool )
00176   {
00177     return save_date_in_field(field);
00178   }
00179 };
00180 
00181 } /* namespace drizzled */
00182