00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00038 null_value= true;
00039 return 0;
00040 }
00041
00042
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
00051
00052
00053
00054
00055
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
00072
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
00083 default:
00084 {
00085
00086
00087
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
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 }