Drizzled Public API Documentation

month.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/session.h>
00025 #include <drizzled/function/time/month.h>
00026 #include <drizzled/typelib.h>
00027 
00028 namespace drizzled
00029 {
00030 
00031 int64_t Item_func_month::val_int()
00032 {
00033   assert(fixed);
00034 
00035   if (args[0]->is_null())
00036   {
00037     /* For NULL argument, we return a NULL result */
00038     null_value= true;
00039     return 0;
00040   }
00041 
00042   /* Grab the first argument as a DateTime object */
00043   DateTime temporal;
00044   Item_result arg0_result_type= args[0]->result_type();
00045   
00046   switch (arg0_result_type)
00047   {
00048     case DECIMAL_RESULT: 
00049       /* 
00050        * For doubles supplied, interpret the arg as a string, 
00051        * so intentionally fall-through here...
00052        * This allows us to accept double parameters like 
00053        * 19971231235959.01 and interpret it the way MySQL does:
00054        * as a TIMESTAMP-like thing with a microsecond component.
00055        * Ugh, but need to keep backwards-compat.
00056        */
00057     case STRING_RESULT:
00058       {
00059         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00060         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00061         String *res= args[0]->val_str(&tmp);
00062 
00063         if (res && (res != &tmp))
00064         {
00065           tmp.copy(*res);
00066         }
00067 
00068         if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
00069         {
00070           /* 
00071           * Could not interpret the function argument as a temporal value, 
00072           * so throw an error and return 0
00073           */
00074           my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00075           return 0;
00076         }
00077       }
00078       break;
00079     case INT_RESULT:
00080       if (temporal.from_int64_t(args[0]->val_int()))
00081         break;
00082       /* Intentionally fall-through on invalid conversion from integer */
00083     default:
00084       {
00085         /* 
00086         * Could not interpret the function argument as a temporal value, 
00087         * so throw an error and return 0
00088         */
00089         null_value= true;
00090         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00091         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00092         String *res;
00093 
00094         res= args[0]->val_str(&tmp);
00095 
00096         if (res && (res != &tmp))
00097         {
00098           tmp.copy(*res);
00099         }
00100 
00101         my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00102         return 0;
00103       }
00104   }
00105   return (int64_t) temporal.months();
00106 }
00107 
00108 String* Item_func_monthname::val_str(String* str)
00109 {
00110   assert(fixed);
00111 
00112   if (args[0]->is_null())
00113   {
00114     /* For NULL argument, we return a NULL result */
00115     null_value= true;
00116     return (String *) 0;
00117   }
00118   const char *month_name;
00119   uint32_t   month= (uint) val_int();
00120 
00121   if (null_value || !month)
00122   {
00123     null_value=1;
00124     return (String*) 0;
00125   }
00126   null_value=0;
00127   month_name= getSession().variables.lc_time_names->month_names->type_names[month-1];
00128   str->set(month_name, strlen(month_name), system_charset_info);
00129   return str;
00130 }
00131 
00132 } /* namespace drizzled */