ExtCast.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/ExtCast.cpp#2 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/calculator/SqlString.h"
00025 #include "fennel/calculator/ExtendedInstructionTable.h"
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00029 void
00030 castExactToStrA(
00031     RegisterRef<char*>* result,
00032     RegisterRef<int64_t>* src)
00033 {
00034     assert(StandardTypeDescriptor::isTextArray(result->type()));
00035 
00036     if (src->isNull()) {
00037         result->toNull();
00038         result->length(0);
00039     } else {
00040         result->length(
00041             SqlStrCastFromExact<1,1>(
00042                 result->pointer(),
00043                 result->storage(),
00044                 src->value(),
00045                 (result->type() == STANDARD_TYPE_CHAR ? true : false)));
00046     }
00047 }
00048 
00049 void
00050 castExactToStrA(
00051     RegisterRef<char*>* result,
00052     RegisterRef<int64_t>* src,
00053     RegisterRef<int32_t>* precision,
00054     RegisterRef<int32_t>* scale)
00055 {
00056     assert(StandardTypeDescriptor::isTextArray(result->type()));
00057 
00058     if (src->isNull()) {
00059         result->toNull();
00060         result->length(0);
00061     } else {
00062          result->length(
00063              SqlStrCastFromExact<1,1>(
00064                  result->pointer(),
00065                  result->storage(),
00066                  src->value(),
00067                  precision->value(),
00068                  scale->value(),
00069                  (result->type() == STANDARD_TYPE_CHAR ? true : false)));
00070     }
00071 }
00072 
00073 void
00074 castApproxToStrA(
00075     RegisterRef<char*>* result,
00076     RegisterRef<float>* src)
00077 {
00078     assert(StandardTypeDescriptor::isTextArray(result->type()));
00079 
00080     if (src->isNull()) {
00081         result->toNull();
00082         result->length(0);
00083     } else {
00084         result->length(
00085             SqlStrCastFromApprox<1,1>(
00086                 result->pointer(),
00087                 result->storage(),
00088                 src->value(),
00089                 true,
00090                 (result->type() == STANDARD_TYPE_CHAR ? true : false)));
00091     }
00092 }
00093 
00094 void
00095 castApproxToStrA(
00096     RegisterRef<char*>* result,
00097     RegisterRef<double>* src)
00098 {
00099     assert(StandardTypeDescriptor::isTextArray(result->type()));
00100 
00101     if (src->isNull()) {
00102         result->toNull();
00103         result->length(0);
00104     } else {
00105         result->length(
00106             SqlStrCastFromApprox<1,1>(
00107                 result->pointer(),
00108                 result->storage(),
00109                 src->value(),
00110                 false,
00111                 (result->type() == STANDARD_TYPE_CHAR ? true : false)));
00112     }
00113 }
00114 
00115 void
00116 castStrToExactA(
00117     RegisterRef<int64_t>* result,
00118     RegisterRef<char*>* src)
00119 {
00120     assert(StandardTypeDescriptor::isTextArray(src->type()));
00121 
00122     if (src->isNull()) {
00123         result->toNull();
00124     } else {
00125         result->value(
00126             SqlStrCastToExact<1,1>(
00127                 src->pointer(),
00128                 src->stringLength()));
00129     }
00130 }
00131 
00132 
00133 void
00134 castStrToExactA(
00135     RegisterRef<int64_t>* result,
00136     RegisterRef<char*>* src,
00137     RegisterRef<int32_t>* precision,
00138     RegisterRef<int32_t>* scale)
00139 {
00140     assert(StandardTypeDescriptor::isTextArray(src->type()));
00141 
00142     if (src->isNull()) {
00143         result->toNull();
00144     } else {
00145         result->value(
00146             SqlStrCastToExact<1,1>(
00147                 src->pointer(),
00148                 src->stringLength(),
00149                 precision->value(),
00150                 scale->value()));
00151     }
00152 }
00153 
00154 void
00155 castStrToApproxA(
00156     RegisterRef<double>* result,
00157     RegisterRef<char*>* src)
00158 {
00159     assert(StandardTypeDescriptor::isTextArray(src->type()));
00160 
00161     if (src->isNull()) {
00162         result->toNull();
00163     } else {
00164         result->value(
00165             SqlStrCastToApprox<1,1>(
00166                 src->pointer(),
00167                 src->stringLength()));
00168     }
00169 }
00170 
00171 void
00172 castBooleanToStrA(
00173     RegisterRef<char*>* result,
00174     RegisterRef<bool>* src)
00175 {
00176     assert(StandardTypeDescriptor::isTextArray(result->type()));
00177 
00178     if (src->isNull()) {
00179         result->toNull();
00180         result->length(0);
00181     } else {
00182         result->length(
00183             SqlStrCastFromBoolean<1,1>(
00184                 result->pointer(),
00185                 result->storage(),
00186                 src->value(),
00187                 (result->type() == STANDARD_TYPE_CHAR ? true : false)));
00188     }
00189 }
00190 
00191 void
00192 castStrToBooleanA(
00193     RegisterRef<bool>* result,
00194     RegisterRef<char*>* src)
00195 {
00196     assert(StandardTypeDescriptor::isTextArray(src->type()));
00197 
00198     if (src->isNull()) {
00199         result->toNull();
00200     } else {
00201         result->value(
00202             SqlStrCastToBoolean<1,1>(
00203                 src->pointer(),
00204                 src->stringLength()));
00205     }
00206 }
00207 
00208 
00209 // TODO: cases where the result is smaller than the input could
00210 // probably benefit from operating by reference instead of by value
00211 void
00212 castStrToVarCharA(
00213     RegisterRef<char*>* result,
00214     RegisterRef<char*>* src)
00215 {
00216     assert(StandardTypeDescriptor::isArray(result->type()));
00217     assert(StandardTypeDescriptor::isArray(src->type()));
00218 
00219     if (src->isNull()) {
00220         result->toNull();
00221         result->length(0);
00222     } else {
00223         int rightTruncWarning = 0;
00224         result->length(
00225             SqlStrCastToVarChar<1,1>(
00226                 result->pointer(),
00227                 result->storage(),
00228                 src->pointer(),
00229                 src->stringLength(),
00230                 &rightTruncWarning));
00231         if (rightTruncWarning) {
00232             // TODO: throw 22001 as a warning
00233 //            throw "22001";
00234         }
00235     }
00236 }
00237 
00238 // TODO: cases where the result is smaller than the input could
00239 // probably benefit from operating by reference instead of by value
00240 void
00241 castStrToCharA(
00242     RegisterRef<char*>* result,
00243     RegisterRef<char*>* src)
00244 {
00245     assert(StandardTypeDescriptor::isArray(result->type()));
00246     assert(StandardTypeDescriptor::isArray(src->type()));
00247 
00248     if (src->isNull()) {
00249         result->toNull();
00250         result->length(0);
00251     } else {
00252         int rightTruncWarning = 0;
00253         result->length(
00254             SqlStrCastToChar<1,1>(
00255                 result->pointer(),
00256                 result->storage(),
00257                 src->pointer(),
00258                 src->stringLength(),
00259                 &rightTruncWarning));
00260 
00261         if (rightTruncWarning) {
00262             // TODO: throw 22001 as a warning
00263 //            throw "22001";
00264         }
00265     }
00266 }
00267 
00268 
00269 // TODO: cases where the result is smaller than the input could
00270 // probably benefit from operating by reference instead of by value
00271 void
00272 castStrToVarBinaryA(
00273     RegisterRef<char*>* result,
00274     RegisterRef<char*>* src)
00275 {
00276     assert(StandardTypeDescriptor::isArray(result->type()));
00277     assert(StandardTypeDescriptor::isArray(src->type()));
00278 
00279     if (src->isNull()) {
00280         result->toNull();
00281         result->length(0);
00282     } else {
00283         int rightTruncWarning = 0;
00284         result->length(
00285             SqlStrCastToVarChar<1,1>(
00286                 result->pointer(),
00287                 result->storage(),
00288                 src->pointer(),
00289                 src->stringLength(),
00290                 &rightTruncWarning,
00291                 0));
00292         if (rightTruncWarning) {
00293             // TODO: throw 22001 as a warning
00294 //            throw "22001";
00295         }
00296     }
00297 }
00298 
00299 // TODO: cases where the result is smaller than the input could
00300 // probably benefit from operating by reference instead of by value
00301 void
00302 castStrToBinaryA(
00303     RegisterRef<char*>* result,
00304     RegisterRef<char*>* src)
00305 {
00306     assert(StandardTypeDescriptor::isArray(result->type()));
00307     assert(StandardTypeDescriptor::isArray(src->type()));
00308 
00309     if (src->isNull()) {
00310         result->toNull();
00311         result->length(0);
00312     } else {
00313         int rightTruncWarning = 0;
00314         result->length(
00315             SqlStrCastToChar<1,1>(
00316                 result->pointer(),
00317                 result->storage(),
00318                 src->pointer(),
00319                 src->stringLength(),
00320                 &rightTruncWarning,
00321                 0));
00322 
00323         if (rightTruncWarning) {
00324             // TODO: throw 22001 as a warning
00325 //            throw "22001";
00326         }
00327     }
00328 }
00329 
00330 
00331 void
00332 ExtCastRegister(ExtendedInstructionTable* eit)
00333 {
00334     assert(eit != NULL);
00335 
00336     vector<StandardTypeDescriptorOrdinal> params_1bo_1c;
00337     params_1bo_1c.push_back(STANDARD_TYPE_BOOL);
00338     params_1bo_1c.push_back(STANDARD_TYPE_CHAR);
00339 
00340     vector<StandardTypeDescriptorOrdinal> params_1bo_1vc;
00341     params_1bo_1vc.push_back(STANDARD_TYPE_BOOL);
00342     params_1bo_1vc.push_back(STANDARD_TYPE_VARCHAR);
00343 
00344     vector<StandardTypeDescriptorOrdinal> params_1s8_1c;
00345     params_1s8_1c.push_back(STANDARD_TYPE_INT_64);
00346     params_1s8_1c.push_back(STANDARD_TYPE_CHAR);
00347 
00348     vector<StandardTypeDescriptorOrdinal> params_1s8_1vc;
00349     params_1s8_1vc.push_back(STANDARD_TYPE_INT_64);
00350     params_1s8_1vc.push_back(STANDARD_TYPE_VARCHAR);
00351 
00352     vector<StandardTypeDescriptorOrdinal> params_1s8_1c_PS;
00353     params_1s8_1c_PS.push_back(STANDARD_TYPE_INT_64);
00354     params_1s8_1c_PS.push_back(STANDARD_TYPE_CHAR);
00355     params_1s8_1c_PS.push_back(STANDARD_TYPE_INT_32);
00356     params_1s8_1c_PS.push_back(STANDARD_TYPE_INT_32);
00357 
00358     vector<StandardTypeDescriptorOrdinal> params_1s8_1vc_PS;
00359     params_1s8_1vc_PS.push_back(STANDARD_TYPE_INT_64);
00360     params_1s8_1vc_PS.push_back(STANDARD_TYPE_VARCHAR);
00361     params_1s8_1vc_PS.push_back(STANDARD_TYPE_INT_32);
00362     params_1s8_1vc_PS.push_back(STANDARD_TYPE_INT_32);
00363 
00364     vector<StandardTypeDescriptorOrdinal> params_1d_1c;
00365     params_1d_1c.push_back(STANDARD_TYPE_DOUBLE);
00366     params_1d_1c.push_back(STANDARD_TYPE_CHAR);
00367 
00368     vector<StandardTypeDescriptorOrdinal> params_1d_1vc;
00369     params_1d_1vc.push_back(STANDARD_TYPE_DOUBLE);
00370     params_1d_1vc.push_back(STANDARD_TYPE_VARCHAR);
00371 
00372     vector<StandardTypeDescriptorOrdinal> params_1c_1bo;
00373     params_1c_1bo.push_back(STANDARD_TYPE_CHAR);
00374     params_1c_1bo.push_back(STANDARD_TYPE_BOOL);
00375 
00376     vector<StandardTypeDescriptorOrdinal> params_1vc_1bo;
00377     params_1vc_1bo.push_back(STANDARD_TYPE_VARCHAR);
00378     params_1vc_1bo.push_back(STANDARD_TYPE_BOOL);
00379 
00380     vector<StandardTypeDescriptorOrdinal> params_1c_1s8;
00381     params_1c_1s8.push_back(STANDARD_TYPE_CHAR);
00382     params_1c_1s8.push_back(STANDARD_TYPE_INT_64);
00383 
00384     vector<StandardTypeDescriptorOrdinal> params_1vc_1s8;
00385     params_1vc_1s8.push_back(STANDARD_TYPE_VARCHAR);
00386     params_1vc_1s8.push_back(STANDARD_TYPE_INT_64);
00387 
00388     vector<StandardTypeDescriptorOrdinal> params_1c_1s8_PS;
00389     params_1c_1s8_PS.push_back(STANDARD_TYPE_CHAR);
00390     params_1c_1s8_PS.push_back(STANDARD_TYPE_INT_64);
00391     params_1c_1s8_PS.push_back(STANDARD_TYPE_INT_32);
00392     params_1c_1s8_PS.push_back(STANDARD_TYPE_INT_32);
00393 
00394     vector<StandardTypeDescriptorOrdinal> params_1vc_1s8_PS;
00395     params_1vc_1s8_PS.push_back(STANDARD_TYPE_VARCHAR);
00396     params_1vc_1s8_PS.push_back(STANDARD_TYPE_INT_64);
00397     params_1vc_1s8_PS.push_back(STANDARD_TYPE_INT_32);
00398     params_1vc_1s8_PS.push_back(STANDARD_TYPE_INT_32);
00399 
00400     vector<StandardTypeDescriptorOrdinal> params_1c_1d;
00401     params_1c_1d.push_back(STANDARD_TYPE_CHAR);
00402     params_1c_1d.push_back(STANDARD_TYPE_DOUBLE);
00403 
00404     vector<StandardTypeDescriptorOrdinal> params_1vc_1d;
00405     params_1vc_1d.push_back(STANDARD_TYPE_VARCHAR);
00406     params_1vc_1d.push_back(STANDARD_TYPE_DOUBLE);
00407 
00408     vector<StandardTypeDescriptorOrdinal> params_1c_1r;
00409     params_1c_1r.push_back(STANDARD_TYPE_CHAR);
00410     params_1c_1r.push_back(STANDARD_TYPE_REAL);
00411 
00412     vector<StandardTypeDescriptorOrdinal> params_1vc_1r;
00413     params_1vc_1r.push_back(STANDARD_TYPE_VARCHAR);
00414     params_1vc_1r.push_back(STANDARD_TYPE_REAL);
00415 
00416     vector<StandardTypeDescriptorOrdinal> params_1vc_1c;
00417     params_1vc_1c.push_back(STANDARD_TYPE_VARCHAR);
00418     params_1vc_1c.push_back(STANDARD_TYPE_CHAR);
00419 
00420     vector<StandardTypeDescriptorOrdinal> params_1c_1vc;
00421     params_1c_1vc.push_back(STANDARD_TYPE_CHAR);
00422     params_1c_1vc.push_back(STANDARD_TYPE_VARCHAR);
00423 
00424     vector<StandardTypeDescriptorOrdinal> params_1vc_1vc;
00425     params_1vc_1vc.push_back(STANDARD_TYPE_VARCHAR);
00426     params_1vc_1vc.push_back(STANDARD_TYPE_VARCHAR);
00427 
00428     vector<StandardTypeDescriptorOrdinal> params_1c_1c;
00429     params_1c_1c.push_back(STANDARD_TYPE_CHAR);
00430     params_1c_1c.push_back(STANDARD_TYPE_CHAR);
00431 
00432     vector<StandardTypeDescriptorOrdinal> params_1vb_1b;
00433     params_1vb_1b.push_back(STANDARD_TYPE_VARBINARY);
00434     params_1vb_1b.push_back(STANDARD_TYPE_BINARY);
00435 
00436     vector<StandardTypeDescriptorOrdinal> params_1b_1vb;
00437     params_1b_1vb.push_back(STANDARD_TYPE_BINARY);
00438     params_1b_1vb.push_back(STANDARD_TYPE_VARBINARY);
00439 
00440     vector<StandardTypeDescriptorOrdinal> params_1vb_1vb;
00441     params_1vb_1vb.push_back(STANDARD_TYPE_VARBINARY);
00442     params_1vb_1vb.push_back(STANDARD_TYPE_VARBINARY);
00443 
00444     vector<StandardTypeDescriptorOrdinal> params_1b_1b;
00445     params_1b_1b.push_back(STANDARD_TYPE_BINARY);
00446     params_1b_1b.push_back(STANDARD_TYPE_BINARY);
00447 
00448     eit->add(
00449         "castA", params_1bo_1c,
00450         (ExtendedInstruction2<bool, char*>*) NULL,
00451         &castStrToBooleanA);
00452     eit->add(
00453         "castA", params_1bo_1vc,
00454         (ExtendedInstruction2<bool, char*>*) NULL,
00455         &castStrToBooleanA);
00456 
00457     eit->add(
00458         "castA", params_1s8_1c,
00459         (ExtendedInstruction2<int64_t, char*>*) NULL,
00460         &castStrToExactA);
00461     eit->add(
00462         "castA", params_1s8_1vc,
00463         (ExtendedInstruction2<int64_t, char*>*) NULL,
00464         &castStrToExactA);
00465 
00466     eit->add(
00467         "castA", params_1s8_1c_PS,
00468         (ExtendedInstruction4<int64_t, char*, int32_t, int32_t>*) NULL,
00469         &castStrToExactA);
00470     eit->add(
00471         "castA", params_1s8_1vc_PS,
00472         (ExtendedInstruction4<int64_t, char*, int32_t, int32_t>*) NULL,
00473         &castStrToExactA);
00474 
00475     eit->add(
00476         "castA", params_1d_1c,
00477         (ExtendedInstruction2<double, char*>*) NULL,
00478         &castStrToApproxA);
00479     eit->add(
00480         "castA", params_1d_1vc,
00481         (ExtendedInstruction2<double, char*>*) NULL,
00482         &castStrToApproxA);
00483 
00484     eit->add(
00485         "castA", params_1c_1bo,
00486         (ExtendedInstruction2<char*, bool>*) NULL,
00487         &castBooleanToStrA);
00488     eit->add(
00489         "castA", params_1vc_1bo,
00490         (ExtendedInstruction2<char*, bool>*) NULL,
00491         &castBooleanToStrA);
00492 
00493     eit->add(
00494         "castA", params_1c_1s8,
00495         (ExtendedInstruction2<char*, int64_t>*) NULL,
00496         &castExactToStrA);
00497     eit->add(
00498         "castA", params_1vc_1s8,
00499         (ExtendedInstruction2<char*, int64_t>*) NULL,
00500         &castExactToStrA);
00501 
00502     eit->add(
00503         "castA", params_1c_1s8_PS,
00504         (ExtendedInstruction4<char*, int64_t, int32_t, int32_t>*) NULL,
00505         &castExactToStrA);
00506     eit->add(
00507         "castA", params_1vc_1s8_PS,
00508         (ExtendedInstruction4<char*, int64_t, int32_t, int32_t>*) NULL,
00509         &castExactToStrA);
00510 
00511     eit->add(
00512         "castA", params_1c_1d,
00513         (ExtendedInstruction2<char*, double>*) NULL,
00514         &castApproxToStrA);
00515     eit->add(
00516         "castA", params_1vc_1d,
00517         (ExtendedInstruction2<char*, double>*) NULL,
00518         &castApproxToStrA);
00519 
00520     eit->add(
00521         "castA", params_1c_1r,
00522         (ExtendedInstruction2<char*, float>*) NULL,
00523         &castApproxToStrA);
00524     eit->add(
00525         "castA", params_1vc_1r,
00526         (ExtendedInstruction2<char*, float>*) NULL,
00527         &castApproxToStrA);
00528 
00529     eit->add(
00530         "castA", params_1c_1vc,
00531         (ExtendedInstruction2<char*, char*>*) NULL,
00532         &castStrToCharA);
00533     eit->add(
00534         "castA", params_1c_1c,
00535         (ExtendedInstruction2<char*, char*>*) NULL,
00536         &castStrToCharA);
00537 
00538     eit->add(
00539         "castA", params_1vc_1c,
00540         (ExtendedInstruction2<char*, char*>*) NULL,
00541         &castStrToVarCharA);
00542     eit->add(
00543         "castA", params_1vc_1vc,
00544         (ExtendedInstruction2<char*, char*>*) NULL,
00545         &castStrToVarCharA);
00546 
00547     eit->add(
00548         "castA", params_1b_1vb,
00549         (ExtendedInstruction2<char*, char*>*) NULL,
00550         &castStrToBinaryA);
00551     eit->add(
00552         "castA", params_1b_1b,
00553         (ExtendedInstruction2<char*, char*>*) NULL,
00554         &castStrToBinaryA);
00555 
00556     eit->add(
00557         "castA", params_1vb_1b,
00558         (ExtendedInstruction2<char*, char*>*) NULL,
00559         &castStrToVarBinaryA);
00560     eit->add(
00561         "castA", params_1vb_1vb,
00562         (ExtendedInstruction2<char*, char*>*) NULL,
00563         &castStrToVarBinaryA);
00564 }
00565 
00566 
00567 FENNEL_END_NAMESPACE
00568 
00569 // End ExtCast.cpp

Generated on Mon Jun 22 04:00:17 2009 for Fennel by  doxygen 1.5.1