Drizzled Public API Documentation

date.cc
00001 /* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 MySQL
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; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <boost/lexical_cast.hpp>
00023 
00024 #include <drizzled/field/date.h>
00025 #include <drizzled/error.h>
00026 #include <drizzled/table.h>
00027 #include <drizzled/temporal.h>
00028 #include <drizzled/session.h>
00029 #include <drizzled/time_functions.h>
00030 #include <drizzled/current_session.h>
00031 #include <drizzled/system_variables.h>
00032 
00033 #include <math.h>
00034 
00035 #include <sstream>
00036 #include <string>
00037 
00038 namespace drizzled
00039 {
00040 
00041 
00042 /****************************************************************************
00043 ** Drizzle date type stored in 3 bytes
00044 ** In number context: YYYYMMDD
00045 ****************************************************************************/
00046 
00047 /*
00048   Store string into a date field
00049 
00050   SYNOPSIS
00051     Field_date::store()
00052     from                Date string
00053     len                 Length of date field
00054     cs                  Character set (not used)
00055 
00056   RETURN
00057     0  ok
00058     1  Value was cut during conversion
00059     2  Wrong date string
00060     3  Datetime value that was cut (warning level NOTE)
00061        This is used by opt_range.cc:get_mm_leaf(). Note that there is a
00062        nearly-identical class Field_date doesn't ever return 3 from its
00063        store function.
00064 */
00065 int Field_date::store(const char *from,
00066                          uint32_t len,
00067                          const charset_info_st * const )
00068 {
00069   /* 
00070    * Try to create a DateTime from the supplied string.  Throw an error
00071    * if unable to create a valid DateTime.  A DateTime is used so that
00072    * automatic conversion from the higher-storage DateTime can be used
00073    * and matches on datetime format strings can occur.
00074    */
00075   ASSERT_COLUMN_MARKED_FOR_WRITE;
00076   DateTime temporal;
00077   if (! temporal.from_string(from, (size_t) len))
00078   {
00079     my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
00080     return 2;
00081   }
00082   /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
00083   uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
00084   int4store(ptr, int_value);
00085   return 0;
00086 }
00087 
00088 int Field_date::store(double from)
00089 {
00090   ASSERT_COLUMN_MARKED_FOR_WRITE;
00091   if (from < 0.0 || from > 99991231235959.0)
00092   {
00093     /* Convert the double to a string using stringstream */
00094     std::stringstream ss;
00095     std::string tmp;
00096     ss.precision(18); /* 18 places should be fine for error display of double input. */
00097     ss << from; ss >> tmp;
00098 
00099     my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00100     return 2;
00101   }
00102   return Field_date::store((int64_t) rint(from), false);
00103 }
00104 
00105 int Field_date::store(int64_t from, bool)
00106 {
00107   /* 
00108    * Try to create a DateTime from the supplied integer.  Throw an error
00109    * if unable to create a valid DateTime.  
00110    */
00111   ASSERT_COLUMN_MARKED_FOR_WRITE;
00112   DateTime temporal;
00113   if (! temporal.from_int64_t(from))
00114   {
00115     /* Convert the integer to a string using boost::lexical_cast */
00116     std::string tmp(boost::lexical_cast<std::string>(from)); 
00117 
00118     my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00119     return 2;
00120   }
00121 
00122   /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
00123   uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
00124   int4store(ptr, int_value);
00125 
00126   return 0;
00127 }
00128 
00129 int Field_date::store_time(type::Time &ltime,
00130                            type::timestamp_t time_type)
00131 {
00132   long tmp;
00133   int error= 0;
00134   if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
00135   {
00136     tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
00137 
00138     Session *session= getTable() ? getTable()->in_use : current_session;
00139     type::cut_t cut_error= type::VALID;
00140     if (ltime.check(tmp != 0,
00141                      (TIME_FUZZY_DATE |
00142                       (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
00143     {
00144       char buff[type::Time::MAX_STRING_LENGTH];
00145       String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
00146       ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
00147       set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
00148                            str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
00149     }
00150 
00151     error= static_cast<int>(cut_error);
00152 
00153     if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
00154         (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
00155     {
00156       char buff[type::Time::MAX_STRING_LENGTH];
00157       String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
00158       ltime.convert(str);
00159       set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00160                            ER_WARN_DATA_TRUNCATED,
00161                            str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
00162       error= 3;
00163     }
00164   }
00165   else
00166   {
00167     tmp=0;
00168     error= 1;
00169     set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
00170   }
00171 
00172   int4store(ptr,tmp);
00173 
00174   return error;
00175 }
00176 
00177 double Field_date::val_real(void) const
00178 {
00179   return (double) Field_date::val_int();
00180 }
00181 
00182 int64_t Field_date::val_int(void) const
00183 {
00184   uint32_t j;
00185 
00186   ASSERT_COLUMN_MARKED_FOR_READ;
00187 
00188   j= uint4korr(ptr);
00189 
00190   return (int64_t) j;
00191 }
00192 
00193 String *Field_date::val_str(String *val_buffer, String *) const
00194 {
00195   val_buffer->alloc(field_length);
00196   val_buffer->length(field_length);
00197   uint32_t tmp=(uint32_t) uint4korr(ptr);
00198   int32_t part;
00199   char *pos=(char*) val_buffer->ptr()+10;
00200 
00201   ASSERT_COLUMN_MARKED_FOR_READ;
00202 
00203   /* Open coded to get more speed */
00204   *pos--=0;         // End NULL
00205   part=(int32_t) (tmp % 100);
00206   *pos--= (char) ('0'+part%10);
00207   *pos--= (char) ('0'+part/10);
00208   *pos--= '-';
00209   part=(int32_t) (tmp/100%100);
00210   *pos--= (char) ('0'+part%10);
00211   *pos--= (char) ('0'+part/10);
00212   *pos--= '-';
00213   part=(int32_t) (tmp/10000);
00214   *pos--= (char) ('0'+part%10); part/=10;
00215   *pos--= (char) ('0'+part%10); part/=10;
00216   *pos--= (char) ('0'+part%10); part/=10;
00217   *pos=   (char) ('0'+part);
00218   return val_buffer;
00219 }
00220 
00221 bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
00222 {
00223   uint32_t tmp=(uint32_t) uint4korr(ptr);
00224   ltime.day=    (int) (tmp%100);
00225   ltime.month=  (int) (tmp/100%100);
00226   ltime.year=     (int) (tmp/10000);
00227   ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
00228   ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
00229 
00230   return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
00231           1 : 0);
00232 }
00233 
00234 bool Field_date::get_time(type::Time &ltime) const
00235 {
00236   return Field_date::get_date(ltime ,0);
00237 }
00238 
00239 int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00240 {
00241   uint32_t a,b;
00242   a=(uint32_t) uint4korr(a_ptr);
00243   b=(uint32_t) uint4korr(b_ptr);
00244   return (a < b) ? -1 : (a > b) ? 1 : 0;
00245 }
00246 
00247 void Field_date::sort_string(unsigned char *to,uint32_t )
00248 {
00249   to[0] = ptr[3];
00250   to[1] = ptr[2];
00251   to[2] = ptr[1];
00252   to[3] = ptr[0];
00253 }
00254 
00255 } /* namespace drizzled */