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/function/time/to_days.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/temporal.h>
00025
00026 namespace drizzled
00027 {
00028
00029
00030
00031
00032
00033 int64_t Item_func_to_days::val_int()
00034 {
00035 assert(fixed);
00036
00037
00038 if (args[0]->null_value)
00039 {
00040 null_value= true;
00041 return false;
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 DateTime temporal;
00055 Item_result arg0_result_type= args[0]->result_type();
00056
00057 switch (arg0_result_type)
00058 {
00059 case REAL_RESULT:
00060 case DECIMAL_RESULT:
00061
00062
00063
00064
00065
00066
00067
00068
00069 case STRING_RESULT:
00070 {
00071 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00072 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00073 String *res= args[0]->val_str(&tmp);
00074
00075 if (! res)
00076 {
00077
00078
00079
00080
00081
00082 return false;
00083 }
00084
00085 if (res != &tmp)
00086 {
00087 tmp.copy(*res);
00088 }
00089
00090 if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
00091 {
00092
00093
00094
00095
00096 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00097 return 0;
00098 }
00099 }
00100 break;
00101 case INT_RESULT:
00102 if (temporal.from_int64_t(args[0]->val_int()))
00103 break;
00104
00105 default:
00106 {
00107
00108
00109
00110
00111 null_value= true;
00112 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00113 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00114 String *res;
00115
00116 res= args[0]->val_str(&tmp);
00117
00118 if (! res)
00119 {
00120
00121
00122
00123
00124
00125 return false;
00126 }
00127
00128 if (res != &tmp)
00129 {
00130 tmp.copy(*res);
00131 }
00132
00133 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00134 return 0;
00135 }
00136 }
00137 int64_t julian_day_number;
00138 temporal.to_julian_day_number(&julian_day_number);
00139 return julian_day_number;
00140 }
00141
00142 int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
00143 {
00144 assert(fixed);
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 DateTime temporal;
00157 Item_result arg0_result_type= args[0]->result_type();
00158
00159 switch (arg0_result_type)
00160 {
00161 case REAL_RESULT:
00162 case DECIMAL_RESULT:
00163
00164
00165
00166
00167
00168
00169
00170
00171 case STRING_RESULT:
00172 {
00173 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00174 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00175 String *res= args[0]->val_str(&tmp);
00176
00177 if (! res)
00178 {
00179
00180
00181
00182
00183
00184 return 0;
00185 }
00186
00187 if (res != &tmp)
00188 {
00189 tmp.copy(*res);
00190 }
00191
00192 if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
00193 {
00194
00195
00196
00197
00198 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00199 return 0;
00200 }
00201 }
00202 break;
00203 case INT_RESULT:
00204 if (temporal.from_int64_t(args[0]->val_int()))
00205 break;
00206
00207 default:
00208 {
00209
00210
00211
00212
00213 null_value= true;
00214 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00215 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00216 String *res;
00217
00218 res= args[0]->val_str(&tmp);
00219
00220 if (! res)
00221 {
00222
00223
00224
00225
00226
00227 return 0;
00228 }
00229
00230 if (res != &tmp)
00231 {
00232 tmp.copy(*res);
00233 }
00234
00235 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00236 return 0;
00237 }
00238 }
00239
00240 if (null_value == true)
00241 {
00242
00243 return INT64_MIN;
00244 }
00245
00246 int64_t julian_day_number;
00247 temporal.to_julian_day_number(&julian_day_number);
00248
00249 if (args[0]->field_type() == DRIZZLE_TYPE_DATE)
00250 {
00251
00252 return julian_day_number;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 if (!left_endp && ! (
00267 temporal.hours()
00268 || temporal.minutes()
00269 || temporal.seconds()
00270 || temporal.useconds()
00271 || temporal.nseconds()
00272 )
00273 )
00274 ;
00275 else
00276 *incl_endp= true;
00277 return julian_day_number;
00278 }
00279
00280 }