Drizzled Public API Documentation

year.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 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 #include <config.h>
00021 
00022 #include <drizzled/temporal.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/function/time/year.h>
00025 
00026 namespace drizzled
00027 {
00028 
00029 int64_t Item_func_year::val_int()
00030 {
00031   assert(fixed);
00032 
00033   if (args[0]->is_null())
00034   {
00035     /* For NULL argument, we return a NULL result */
00036     null_value= true;
00037     return 0;
00038   }
00039 
00040   /* Grab the first argument as a DateTime object */
00041   DateTime temporal;
00042   Item_result arg0_result_type= args[0]->result_type();
00043   
00044   switch (arg0_result_type)
00045   {
00046     case DECIMAL_RESULT: 
00047       /* 
00048        * For doubles supplied, interpret the arg as a string, 
00049        * so intentionally fall-through here...
00050        * This allows us to accept double parameters like 
00051        * 19971231235959.01 and interpret it the way MySQL does:
00052        * as a TIMESTAMP-like thing with a microsecond component.
00053        * Ugh, but need to keep backwards-compat.
00054        */
00055     case STRING_RESULT:
00056       {
00057         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00058         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00059         String *res= args[0]->val_str(&tmp);
00060 
00061         if (res && (res != &tmp))
00062         {
00063           tmp.copy(*res);
00064         }
00065 
00066         if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
00067         {
00068           /* 
00069           * Could not interpret the function argument as a temporal value, 
00070           * so throw an error and return 0
00071           */
00072           my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00073           return 0;
00074         }
00075       }
00076       break;
00077     case INT_RESULT:
00078       if (temporal.from_int64_t(args[0]->val_int()))
00079         break;
00080       /* Intentionally fall-through on invalid conversion from integer */
00081     default:
00082       {
00083         /* 
00084         * Could not interpret the function argument as a temporal value, 
00085         * so throw an error and return 0
00086         */
00087         null_value= true;
00088         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00089         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00090         String *res;
00091 
00092         res= args[0]->val_str(&tmp);
00093 
00094         if (res && (res != &tmp))
00095         {
00096           tmp.copy(*res);
00097         }
00098 
00099         my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00100         return 0;
00101       }
00102   }
00103   return (int64_t) temporal.years();
00104 }
00105 
00106 int64_t Item_func_year::val_int_endpoint(bool left_endp, bool *incl_endp)
00107 {
00108   assert(fixed == 1);
00109 
00110   if (args[0]->is_null())
00111   {
00112     /* got NULL, leave the incl_endp intact */
00113     return INT64_MIN;
00114   }
00115 
00116   /* Grab the first argument as a DateTime object */
00117   DateTime temporal;
00118   
00119   if (! temporal.from_int64_t(args[0]->val_int()))
00120   {
00121     /* got bad DateTime, leave the incl_endp intact */
00122     return INT64_MIN;
00123   }
00124 
00125   /*
00126     Handle the special but practically useful case of datetime values that
00127     point to year bound ("strictly less" comparison stays intact) :
00128 
00129       col < '2007-01-01 00:00:00'  -> YEAR(col) <  2007
00130 
00131     which is different from the general case ("strictly less" changes to
00132     "less or equal"):
00133 
00134       col < '2007-09-15 23:00:00'  -> YEAR(col) <= 2007
00135   */
00136   if (!left_endp && temporal.days() == 1 && temporal.months() == 1 &&
00137       ! (temporal.hours() || temporal.minutes() || temporal.seconds() || temporal.useconds()))
00138     ; /* do nothing */
00139   else
00140     *incl_endp= true;
00141   return (int64_t) temporal.years();
00142 }
00143 
00144 } /* namespace drizzled */