00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/calculator/ExtendedInstructionTable.h"
00025 #include "fennel/calculator/SqlDate.h"
00026
00027 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/calculator/ExtDateTime.cpp#2 $");
00028
00029 using namespace boost::local_time;
00030
00031 void
00032 CastDateToStrA(
00033 RegisterRef<char*>* result,
00034 RegisterRef<int64_t>* date)
00035 {
00036 assert(date->type() == STANDARD_TYPE_INT_64);
00037 assert(StandardTypeDescriptor::isTextArray(result->type()));
00038
00039 if (date->isNull()) {
00040 result->toNull();
00041 result->length(0);
00042 } else {
00043
00044 int64_t v = date->value() * 1000;
00045 int len = SqlDateToStr<1,1,SQLDATE>(
00046 result->pointer(), result->storage(), v,
00047 (result->type() == STANDARD_TYPE_CHAR ? true : false));
00048 result->length(len);
00049 }
00050 }
00051
00052 void
00053 CastTimeToStrA(
00054 RegisterRef<char*>* result,
00055 RegisterRef<int64_t>* time)
00056 {
00057 assert(time->type() == STANDARD_TYPE_INT_64);
00058 assert(StandardTypeDescriptor::isTextArray(result->type()));
00059
00060 if (time->isNull()) {
00061 result->toNull();
00062 result->length(0);
00063 } else {
00064 int64_t v = time->value() * 1000;
00065 int len = SqlDateToStr<1,1,SQLTIME>(
00066 result->pointer(), result->storage(), v,
00067 (result->type() == STANDARD_TYPE_CHAR ? true : false));
00068 result->length(len);
00069 }
00070 }
00071
00072 void
00073 CastTimestampToStrA(
00074 RegisterRef<char*>* result,
00075 RegisterRef<int64_t>* tstamp)
00076 {
00077 assert(tstamp->type() == STANDARD_TYPE_INT_64);
00078 assert(StandardTypeDescriptor::isTextArray(result->type()));
00079
00080 if (tstamp->isNull()) {
00081 result->toNull();
00082 result->length(0);
00083 } else {
00084 int64_t v = tstamp->value() * 1000;
00085 int len = SqlDateToStr<1,1,SQLTIMESTAMP>(
00086 result->pointer(), result->storage(), v,
00087 (result->type() == STANDARD_TYPE_CHAR ? true : false));
00088 result->length(len);
00089 }
00090 }
00091
00092 void
00093 CastStrAToDate(
00094 RegisterRef<int64_t>* result,
00095 RegisterRef<char*>* dateStr)
00096 {
00097 assert(result->type() == STANDARD_TYPE_INT_64);
00098 assert(StandardTypeDescriptor::isTextArray(dateStr->type()));
00099
00100 if (dateStr->isNull()) {
00101 result->toNull();
00102 } else {
00103 result->value(
00104 SqlStrToDate<1,1,SQLDATE>(
00105 dateStr->pointer(),
00106 dateStr->stringLength()));
00107 }
00108 }
00109
00110 void
00111 CastStrAToTime(
00112 RegisterRef<int64_t>* result,
00113 RegisterRef<char*>* timeStr)
00114 {
00115 assert(result->type() == STANDARD_TYPE_INT_64);
00116 assert(StandardTypeDescriptor::isTextArray(timeStr->type()));
00117
00118 if (timeStr->isNull()) {
00119 result->toNull();
00120 } else {
00121 result->value(
00122 SqlStrToDate<1,1,SQLTIME>(
00123 timeStr->pointer(),
00124 timeStr->stringLength()));
00125 }
00126 }
00127
00128 void
00129 CastStrAToTimestamp(
00130 RegisterRef<int64_t>* result,
00131 RegisterRef<char*>* timestampStr)
00132 {
00133 assert(result->type() == STANDARD_TYPE_INT_64);
00134 assert(StandardTypeDescriptor::isTextArray(timestampStr->type()));
00135
00136 if (timestampStr->isNull()) {
00137 result->toNull();
00138 } else {
00139 result->value(
00140 SqlStrToDate<1,1,SQLTIMESTAMP>(
00141 timestampStr->pointer(),
00142 timestampStr->stringLength()));
00143 }
00144 }
00145
00146
00147 void CastDateTimeToInt64(
00148 RegisterRef<int64_t>* result,
00149 RegisterRef<int64_t>* dtime)
00150 {
00151 assert(result->type() == STANDARD_TYPE_INT_64);
00152 assert(dtime->type() == STANDARD_TYPE_INT_64);
00153
00154 if (dtime->isNull()) {
00155 result->toNull();
00156 } else {
00157 result->value(dtime->value());
00158 }
00159 }
00160
00161 void CurrentTime(RegisterRef<int64_t>* result)
00162 {
00163 assert (result->type() == STANDARD_TYPE_INT_64);
00164 result->value(UniversalTime());
00165 }
00166
00167 void CurrentTimestamp(RegisterRef<int64_t>* result)
00168 {
00169 assert (result->type() == STANDARD_TYPE_INT_64);
00170 result->value(UniversalTimestamp());
00171 }
00172
00173 void CurrentTime(
00174 RegisterRef<int64_t>* result,
00175 RegisterRef<int32_t>* precision)
00176 {
00177 assert (result->type() == STANDARD_TYPE_INT_64);
00178 assert (precision->type() == STANDARD_TYPE_INT_32);
00179
00180
00181 result->value(UniversalTime());
00182 }
00183
00184 void CurrentTimestamp(
00185 RegisterRef<int64_t>* result,
00186 RegisterRef<int32_t>* precision)
00187 {
00188 assert (result->type() == STANDARD_TYPE_INT_64);
00189 assert (precision->type() == STANDARD_TYPE_INT_32);
00190
00191
00192 result->value(UniversalTimestamp());
00193 }
00194
00195 void LocalTime(
00196 RegisterRef<int64_t>* result,
00197 RegisterRef<char *>* tz)
00198 {
00199 assert (result->type() == STANDARD_TYPE_INT_64);
00200 assert (tz->type() == STANDARD_TYPE_CHAR);
00201
00202 time_zone_ptr tzPtr(
00203 new posix_time_zone(
00204 string(tz->pointer(), tz->stringLength())));
00205
00206 result->value(LocalTime(tzPtr));
00207 }
00208
00209 void LocalTimestamp(
00210 RegisterRef<int64_t>* result,
00211 RegisterRef<char *>* tz)
00212 {
00213 assert (result->type() == STANDARD_TYPE_INT_64);
00214 assert (tz->type() == STANDARD_TYPE_CHAR);
00215
00216 time_zone_ptr tzPtr(
00217 new posix_time_zone(
00218 string(tz->pointer(), tz->stringLength())));
00219
00220 result->value(LocalTimestamp(tzPtr));
00221 }
00222
00223 void LocalTime(
00224 RegisterRef<int64_t>* result,
00225 RegisterRef<char *>* tz,
00226 RegisterRef<int32_t>* precision)
00227 {
00228 assert (result->type() == STANDARD_TYPE_INT_64);
00229 assert (tz->type() == STANDARD_TYPE_CHAR);
00230 assert (precision->type() == STANDARD_TYPE_INT_32);
00231
00232 time_zone_ptr tzPtr(
00233 new posix_time_zone(
00234 string(tz->pointer(), tz->stringLength())));
00235
00236
00237 result->value(LocalTime(tzPtr));
00238 }
00239
00240 void LocalTimestamp(
00241 RegisterRef<int64_t>* result,
00242 RegisterRef<char *>* tz,
00243 RegisterRef<int32_t>* precision)
00244 {
00245 assert (result->type() == STANDARD_TYPE_INT_64);
00246 assert (tz->type() == STANDARD_TYPE_CHAR);
00247 assert (precision->type() == STANDARD_TYPE_INT_32);
00248
00249 time_zone_ptr tzPtr(
00250 new posix_time_zone(
00251 string(tz->pointer(), tz->stringLength())));
00252
00253
00254 result->value(LocalTimestamp(tzPtr));
00255 }
00256
00257
00258 void
00259 ExtDateTimeRegister(ExtendedInstructionTable* eit)
00260 {
00261 assert(eit != NULL);
00262
00263 vector<StandardTypeDescriptorOrdinal> params_V_I64;
00264 params_V_I64.push_back(STANDARD_TYPE_VARCHAR);
00265 params_V_I64.push_back(STANDARD_TYPE_INT_64);
00266
00267 vector<StandardTypeDescriptorOrdinal> params_C_I64;
00268 params_C_I64.push_back(STANDARD_TYPE_CHAR);
00269 params_C_I64.push_back(STANDARD_TYPE_INT_64);
00270
00271 vector<StandardTypeDescriptorOrdinal> params_I64_V;
00272 params_I64_V.push_back(STANDARD_TYPE_INT_64);
00273 params_I64_V.push_back(STANDARD_TYPE_VARCHAR);
00274
00275 vector<StandardTypeDescriptorOrdinal> params_I64_C;
00276 params_I64_C.push_back(STANDARD_TYPE_INT_64);
00277 params_I64_C.push_back(STANDARD_TYPE_CHAR);
00278
00279 vector<StandardTypeDescriptorOrdinal> params_I64_C_I32;
00280 params_I64_C_I32.push_back(STANDARD_TYPE_INT_64);
00281 params_I64_C_I32.push_back(STANDARD_TYPE_CHAR);
00282 params_I64_C_I32.push_back(STANDARD_TYPE_INT_32);
00283
00284 vector<StandardTypeDescriptorOrdinal> params_I64_I64;
00285 params_I64_I64.push_back(STANDARD_TYPE_INT_64);
00286 params_I64_I64.push_back(STANDARD_TYPE_INT_64);
00287
00288 vector<StandardTypeDescriptorOrdinal> params_I64;
00289 params_I64.push_back(STANDARD_TYPE_INT_64);
00290
00291 vector<StandardTypeDescriptorOrdinal> params_I64_I32;
00292 params_I64_I32.push_back(STANDARD_TYPE_INT_64);
00293 params_I64_I32.push_back(STANDARD_TYPE_INT_32);
00294
00295
00296 eit->add(
00297 "CastDateToStrA", params_V_I64,
00298 (ExtendedInstruction2<char*, int64_t>*) NULL,
00299 &CastDateToStrA);
00300
00301 eit->add(
00302 "CastDateToStrA", params_C_I64,
00303 (ExtendedInstruction2<char*, int64_t>*) NULL,
00304 &CastDateToStrA);
00305
00306 eit->add(
00307 "CastTimeToStrA", params_V_I64,
00308 (ExtendedInstruction2<char*, int64_t>*) NULL,
00309 &CastTimeToStrA);
00310
00311 eit->add(
00312 "CastTimeToStrA", params_C_I64,
00313 (ExtendedInstruction2<char*, int64_t>*) NULL,
00314 &CastTimeToStrA);
00315
00316 eit->add(
00317 "CastTimestampToStrA", params_V_I64,
00318 (ExtendedInstruction2<char*, int64_t>*) NULL,
00319 &CastTimestampToStrA);
00320
00321 eit->add(
00322 "CastTimestampToStrA", params_C_I64,
00323 (ExtendedInstruction2<char*, int64_t>*) NULL,
00324 &CastTimestampToStrA);
00325
00326
00327 eit->add(
00328 "CastStrAToDate", params_I64_V,
00329 (ExtendedInstruction2<int64_t, char*>*) NULL,
00330 &CastStrAToDate);
00331
00332 eit->add(
00333 "CastStrAToDate", params_I64_C,
00334 (ExtendedInstruction2<int64_t, char*>*) NULL,
00335 &CastStrAToDate);
00336
00337 eit->add(
00338 "CastStrAToTime", params_I64_V,
00339 (ExtendedInstruction2<int64_t, char*>*) NULL,
00340 &CastStrAToTime);
00341
00342 eit->add(
00343 "CastStrAToTime", params_I64_C,
00344 (ExtendedInstruction2<int64_t, char*>*) NULL,
00345 &CastStrAToTime);
00346
00347 eit->add(
00348 "CastStrAToTimestamp", params_I64_V,
00349 (ExtendedInstruction2<int64_t, char*>*) NULL,
00350 &CastStrAToTimestamp);
00351
00352 eit->add(
00353 "CastStrAToTimestamp", params_I64_C,
00354 (ExtendedInstruction2<int64_t, char*>*) NULL,
00355 &CastStrAToTimestamp);
00356
00357
00358 eit->add(
00359 "CastDateTimeToInt64", params_I64_I64,
00360 (ExtendedInstruction2<int64_t, int64_t>*) NULL,
00361 &CastDateTimeToInt64);
00362
00363 eit->add(
00364 "LocalTime2", params_I64_C,
00365 (ExtendedInstruction2<int64_t, char *>*) NULL,
00366 &LocalTime);
00367
00368 eit->add(
00369 "LocalTimestamp2", params_I64_C,
00370 (ExtendedInstruction2<int64_t, char *>*) NULL,
00371 &LocalTimestamp);
00372
00373 eit->add(
00374 "LocalTime3", params_I64_C_I32,
00375 (ExtendedInstruction3<int64_t, char *, int32_t>*) NULL,
00376 &LocalTime);
00377
00378 eit->add(
00379 "LocalTimestamp3", params_I64_C_I32,
00380 (ExtendedInstruction3<int64_t, char *, int32_t>*) NULL,
00381 &LocalTimestamp);
00382
00383 eit->add(
00384 "CurrentTime1", params_I64,
00385 (ExtendedInstruction1<int64_t>*) NULL,
00386 &CurrentTime);
00387
00388 eit->add(
00389 "CurrentTimestamp1", params_I64,
00390 (ExtendedInstruction1<int64_t>*) NULL,
00391 &CurrentTimestamp);
00392
00393 eit->add(
00394 "CurrentTime2", params_I64_I32,
00395 (ExtendedInstruction2<int64_t, int32_t>*) NULL,
00396 &CurrentTime);
00397
00398 eit->add(
00399 "CurrentTimestamp2", params_I64_I32,
00400 (ExtendedInstruction2<int64_t, int32_t>*) NULL,
00401 &CurrentTimestamp);
00402
00403 }
00404
00405 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/calculator/ExtDateTime.cpp#2 $");
00406
00407
00408