src/sql/SelStatement.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
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   ***************************************************************************/
00016 #include "Statement.h"
00017 
00018 SelStatement::SelStatement()
00019 {
00020     parsedData = NULL; 
00021     dbMgr = NULL; 
00022     table = NULL;
00023     params = NULL;
00024     paramValues = NULL;
00025     totalParams = 0;
00026     bindFields = NULL;
00027     bindFieldValues = NULL;
00028     totalFields = 0;
00029 }
00030 
00031 SelStatement::~SelStatement()
00032 {
00033     if (table) {
00034         table->setCondition(NULL);
00035         if (dbMgr) dbMgr->closeTable(table);
00036     }
00037     if (totalParams) {
00038         free(params);
00039         params =  NULL;
00040         free(paramValues);
00041         paramValues = NULL;
00042     }
00043     if (totalFields)
00044     {
00045         free(bindFields);
00046         bindFields =  NULL;
00047         free(bindFieldValues);
00048         bindFieldValues = NULL;
00049 
00050     }
00051 }
00052 DbRetVal SelStatement::getParamFldInfo(int paramNo, FieldInfo *&info)
00053 {
00054     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00055     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00056     if (NULL == cValue)
00057     {
00058         printError(ErrSysFatal, "condition value is null. Should never happen");
00059         return ErrSysFatal;
00060     }
00061     info->type = cValue->type;
00062     info->length = cValue->length;
00063     return OK;
00064 }
00065 DbRetVal SelStatement::execute(int &rowsAffected)
00066 {
00067     DbRetVal rv = OK;
00068     //copy param values to binded buffer
00069     ConditionValue *value;
00070     for (int i = 0; i < totalParams; i ++)
00071     {
00072         value = (ConditionValue*) params[i];
00073         if (paramValues[i] == NULL) 
00074         {
00075             continue;
00076             //printError(ErrBadCall, "param values not set");
00077             //return ErrBadCall;
00078         }
00079         AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
00080     }
00081     rv = table->execute();
00082     return rv;
00083 }
00084 
00085 DbRetVal SelStatement::setParam(int paramNo, void *value)
00086 {
00087     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00088     if (NULL == value) return ErrBadArg;
00089     paramValues[paramNo -1] = (char*) value; 
00090     return OK;
00091 }
00092 
00093 DbRetVal SelStatement::setShortParam(int paramNo, short value)
00094 {
00095     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00096     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00097     if (NULL == cValue)
00098     {
00099         printError(ErrSysFatal, "field value is null. Should never happen");
00100         return ErrSysFatal;
00101     }
00102     *(short*)cValue->value = value; 
00103     return OK;
00104 }
00105 
00106 DbRetVal SelStatement::setIntParam(int paramNo, int value)
00107 {
00108     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00109     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00110     if (NULL == cValue)
00111     {
00112         printError(ErrSysFatal, "condition value is null. Should never happen");
00113         return ErrSysFatal;
00114     }
00115     *(int*)cValue->value = value; 
00116     return OK;
00117 }
00118 DbRetVal SelStatement::setLongParam(int paramNo, long value)
00119 {
00120     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00121     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00122     if (NULL == cValue)
00123     {
00124         printError(ErrSysFatal, "condition value is null. Should never happen");
00125         return ErrSysFatal;
00126     }
00127     *(long*)cValue->value = value; 
00128     return OK;
00129 }
00130 
00131 DbRetVal SelStatement::setLongLongParam(int paramNo, long long value)
00132 {
00133     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00134     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00135     if (NULL == cValue)
00136     {
00137         printError(ErrSysFatal, "condition value is null. Should never happen");
00138         return ErrSysFatal;
00139     }
00140     *(long long*)cValue->value = value; 
00141     return OK;
00142 }
00143 DbRetVal SelStatement::setByteIntParam(int paramNo, ByteInt value)
00144 {
00145     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00146     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00147     if (NULL == cValue)
00148     {
00149         printError(ErrSysFatal, "condition value is null. Should never happen");
00150         return ErrSysFatal;
00151     }
00152     *(ByteInt*)cValue->value = value; 
00153     return OK;
00154 }
00155 DbRetVal SelStatement::setFloatParam(int paramNo, float value)
00156 {
00157     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00158     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00159     if (NULL == cValue)
00160     {
00161         printError(ErrSysFatal, "condition value is null. Should never happen");
00162         return ErrSysFatal;
00163     }
00164     *(float*)cValue->value = value; 
00165     return OK;
00166 }
00167 DbRetVal SelStatement::setDoubleParam(int paramNo, double value)
00168 {
00169     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00170     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00171     if (NULL == cValue)
00172     {
00173         printError(ErrSysFatal, "condition value is null. Should never happen");
00174         return ErrSysFatal;
00175     }
00176     *(double*)cValue->value = value; 
00177     return OK;
00178 }
00179 DbRetVal SelStatement::setStringParam(int paramNo, char *value)
00180 {
00181     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00182     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00183     if (NULL == cValue)
00184     {
00185         printError(ErrSysFatal, "condition value is null. Should never happen");
00186         return ErrSysFatal;
00187     }
00188     strcpy((char*)cValue->value, value);
00189     return OK;
00190 }
00191 DbRetVal SelStatement::setDateParam(int paramNo, Date value)
00192 {
00193     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00194     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00195     if (NULL == cValue)
00196     {
00197         printError(ErrSysFatal, "condition value is null. Should never happen");
00198         return ErrSysFatal;
00199     }
00200     *(Date*)cValue->value = value; 
00201     return OK;
00202 }
00203 DbRetVal SelStatement::setTimeParam(int paramNo, Time value)
00204 {
00205     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00206     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00207     if (NULL == cValue)
00208     {
00209         printError(ErrSysFatal, "condition value is null. Should never happen");
00210         return ErrSysFatal;
00211     }
00212     *(Time*)cValue->value = value; 
00213     return OK;
00214 }
00215 DbRetVal SelStatement::setTimeStampParam(int paramNo, TimeStamp value)
00216 {
00217     if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00218     ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
00219     if (NULL == cValue)
00220     {
00221         printError(ErrSysFatal, "condition value is null. Should never happen");
00222         return ErrSysFatal;
00223     }
00224     *(TimeStamp*)cValue->value = value;
00225     return OK;
00226 }
00227 
00228 DbRetVal SelStatement::setBindField(int colNo, void *value)
00229 {
00230     if (colNo <=0) return ErrBadArg;
00231     //TODO: check the upper limit
00232     //if (colNo > table->getFieldNameList().size()) return ErrBadArg;
00233     if (NULL == value) return ErrBadArg;
00234     bindFieldValues[colNo -1] = (char*) value; 
00235     return OK;
00236 }
00237 
00238 DbRetVal SelStatement::resolve()
00239 {
00240     if (dbMgr == NULL) return ErrNoConnection;
00241     //check whether the table exists
00242     table = dbMgr->openTable(parsedData->getTableName());
00243     if (table == NULL) 
00244     {
00245         printError(ErrNotExists, "Unable to open the table:Table not exists");
00246         return ErrNotExists;
00247     }
00248     //get the fieldname list and validate field names
00249     ListIterator iter = parsedData->getFieldNameList().getIterator();
00250     FieldName *name = NULL;
00251     FieldInfo *fInfo = new FieldInfo();
00252     DbRetVal rv = OK;
00253     while (iter.hasElement())
00254     {
00255         name = (FieldName*)iter.nextElement();
00256         if (NULL == name) 
00257         {
00258             dbMgr->closeTable(table);
00259             table = NULL;
00260             delete fInfo;
00261             printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
00262             return ErrSysFatal;
00263         }
00264         if ('*' == name->fldName[0]) 
00265         {
00266             iter.reset();
00267             while (iter.hasElement())
00268                 delete (FieldName *) iter.nextElement();
00269             rv = resolveStar();
00270             if (rv != OK)
00271             { 
00272                 dbMgr->closeTable(table);
00273                 table = NULL;
00274                 delete fInfo; 
00275                 return rv; 
00276             }
00277             //as soon as it encounters *, it breaks the loop negleting other field names
00278             //as they all are deleted during resolveStar method.
00279             break;
00280         } else {
00281             rv = table->getFieldInfo(name->fldName, fInfo);
00282             if (ErrNotFound == rv)
00283             {
00284                 dbMgr->closeTable(table);
00285                 table = NULL;
00286                 delete fInfo;
00287                 printError(ErrSyntaxError, "Field %s does not exist in table", 
00288                                         name->fldName);
00289                 return ErrSyntaxError;
00290             }
00291             FieldValue *newVal = new FieldValue();
00292             newVal->parsedString = NULL;
00293             newVal->paramNo = 0;
00294             newVal->type = fInfo->type;
00295             newVal->length = fInfo->length;
00296             newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
00297             parsedData->insertFieldValue(newVal);
00298             table->bindFld(name->fldName, newVal->value);
00299         }
00300     }
00301     delete fInfo;
00302 
00303     rv = setBindFieldAndValues();
00304     if (rv != OK) 
00305     {
00306         dbMgr->closeTable(table);
00307         table = NULL;
00308         return rv;
00309     }
00310 
00311     table->setCondition(parsedData->getCondition());
00312 
00313     rv = resolveForCondition();
00314     if (rv != OK) 
00315     {
00316         //TODO::free memory allocated for params
00317         table->setCondition(NULL);
00318         dbMgr->closeTable(table);
00319         table = NULL;
00320     }
00321     return rv;
00322 }
00323 DbRetVal SelStatement::resolveStar()
00324 {
00325     DbRetVal rv = OK;
00326     parsedData->clearFieldNameList();
00327     List fNameList = table->getFieldNameList();
00328     ListIterator fNameIter = fNameList.getIterator();
00329     FieldValue *newVal = NULL;
00330     //fNameList.resetIter(); //do not remove this.
00331     FieldInfo *fInfo = new FieldInfo();
00332     for (int i = 0; i < fNameList.size() ; i++)
00333     {
00334         char *fName = ((Identifier*)(fNameIter.nextElement()))->name;
00335         rv = table->getFieldInfo(fName, fInfo);
00336         if (ErrNotFound == rv)
00337         {
00338             delete fInfo;
00339             fNameList.reset();
00340             printError(ErrSysFatal, "Should never happen.");
00341             return ErrSysFatal;
00342         }
00343         newVal = new FieldValue();
00344         newVal->parsedString = NULL;
00345         newVal->paramNo = 0;
00346         newVal->type = fInfo->type;
00347         newVal->length = fInfo->length;
00348         newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
00349         parsedData->insertFieldValue(newVal);
00350         parsedData->insertField(fName);
00351         table->bindFld(fName, newVal->value);
00352     }
00353     fNameIter.reset();
00354     while (fNameIter.hasElement())
00355         delete (Identifier *) fNameIter.nextElement();
00356     fNameList.reset();
00357     delete fInfo;
00358     return OK;
00359 }
00360 
00361 DbRetVal SelStatement::setBindFieldAndValues()
00362 {
00363     totalFields = parsedData->getFieldNameList().size();
00364     bindFields = (FieldValue**) malloc ( totalFields * sizeof(FieldValue*));
00365     bindFieldValues = (char**) malloc( totalFields * sizeof(char*));
00366     memset(bindFields, 0, totalFields * sizeof(FieldValue*));
00367     memset(bindFieldValues, 0, totalFields * sizeof(char*));
00368     ListIterator valIter = parsedData->getFieldValueList().getIterator();
00369     int colNo =0;
00370     FieldValue *value = NULL;
00371     valIter.reset();
00372     while(valIter.hasElement())
00373     {
00374         value = (FieldValue*) valIter.nextElement();
00375         if (value == NULL)
00376         {
00377             free(bindFields); bindFields = NULL;
00378             free(bindFieldValues); bindFieldValues = NULL;
00379             printError(ErrSysFatal, "Should never happen. value NULL after iteration");
00380             return ErrSysFatal;
00381         }
00382         bindFields[colNo++ ] = value;
00383     }
00384     return OK;
00385 }
00386 
00387 
00388 DbRetVal SelStatement::resolveForCondition()
00389 {
00390     //get the fieldname list and validate field names
00391     ListIterator iter = parsedData->getConditionValueList().getIterator();
00392 
00393     ConditionValue *value;
00394     FieldInfo *fInfo = new FieldInfo();
00395     int paramPos =1;
00396     DbRetVal rv = OK;
00397     while (iter.hasElement())
00398     {
00399         value = (ConditionValue*) iter.nextElement();
00400         if (NULL == value) 
00401         {
00402             delete fInfo;
00403             printError(ErrSysFatal, "Should never happen.");
00404             return ErrSysFatal;
00405         }
00406         rv = table->getFieldInfo(value->fName, fInfo);
00407         if (ErrNotFound == rv)
00408         {
00409             delete fInfo;
00410             printError(ErrSyntaxError, "Field %s does not exist in table", 
00411                                         value->fName);
00412             return ErrSyntaxError;
00413         }
00414         value->type = fInfo->type;
00415         value->length = fInfo->length;
00416         value->value = AllDataType::alloc(fInfo->type, fInfo->length);
00417         //table->bindFld(name->fldName, value->value);
00418         if (value->parsedString == NULL)
00419         {
00420             delete fInfo;
00421             printError(ErrSyntaxError, "Condition value should not be NULL"); 
00422             return ErrSyntaxError;
00423         }
00424         if (value->parsedString[0] == '?')
00425         {
00426             value->paramNo = paramPos++;
00427         }
00428         if (!value->paramNo) 
00429             AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
00430     }
00431     delete fInfo;
00432     totalParams = paramPos -1;
00433     if (0 == totalParams) return OK;
00434     params = (void**) malloc ( totalParams * sizeof(FieldValue*));
00435     paramValues = (char**) malloc( totalParams * sizeof(char*));
00436     memset(params, 0, totalParams * sizeof(FieldValue*));
00437     memset(paramValues, 0, totalParams * sizeof(char*));
00438     iter.reset();
00439     while(iter.hasElement())
00440     {
00441         value = (ConditionValue*) iter.nextElement();
00442         if (value == NULL) 
00443         {
00444             free(params); params = NULL;
00445             free(paramValues); paramValues = NULL;
00446             printError(ErrSysFatal, "Should never happen. value NULL after iteration");
00447             return ErrSysFatal;
00448         }
00449         params[value->paramNo -1 ] = value;
00450     }
00451     return OK;
00452 }
00453 
00454 void* SelStatement::fetch()
00455 {
00456     void *tuple = table->fetch();
00457     if (NULL == tuple) return NULL;
00458     //copy values to binded buffer
00459     FieldValue *value;
00460     for (int i = 0; i < totalFields; i++)
00461     {
00462         value = bindFields[i];
00463         if (bindFieldValues[i] == NULL) 
00464         {
00465             printError(ErrBadCall, "Fields are not binded properly. Should never happen");
00466             return NULL;
00467         }
00468         AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
00469     }
00470     return tuple;
00471 }
00472 
00473 void* SelStatement::fetch(DbRetVal &rv)
00474 {
00475     void *tuple = table->fetch(rv);
00476     if (NULL == tuple) return NULL;
00477     //copy values to binded buffer
00478     FieldValue *value;
00479     for (int i = 0; i < totalFields; i++)
00480     {
00481         value = bindFields[i];
00482         if (bindFieldValues[i] == NULL) 
00483         {
00484             printError(ErrBadCall, "Fields are not binded properly. Should never happen");
00485             return NULL;
00486         }
00487         AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
00488     }
00489     return tuple;
00490 }
00491 
00492 DbRetVal SelStatement::close()
00493 {
00494     return table->close();
00495 }
00496 void* SelStatement::getParamValuePtr( int pos )
00497 {
00498     ConditionValue *p = (ConditionValue*) params [pos-1];
00499     return ( (void*) p->value );
00500 }
00501 
00502 char* SelStatement::getFieldName ( int pos )
00503 {
00504     //TODO::if not yet prepared return error
00505     //TODO::check the upper limit for projpos
00506     ListIterator iter = parsedData->getFieldNameList().getIterator();
00507     int position =0;
00508     while (iter.hasElement())
00509     {
00510         if (position == pos) {
00511               FieldName *name = (FieldName*) iter.nextElement();
00512               if (NULL == name)
00513               {
00514                   printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
00515                   return (char*) 0;
00516               }
00517               return name->fldName;
00518       }
00519         position++;
00520     }
00521     return (char*) 0;
00522 }
00523 
00524 DataType SelStatement::getFieldType( int pos )
00525 {
00526     FieldValue *v = bindFields[pos];
00527     return ( (DataType) v->type );
00528 }
00529 
00530 int SelStatement::getFieldLength( int pos )
00531 {
00532     FieldValue *v = bindFields[pos];
00533     return ( (int) v->type );
00534 }
00535 
00536 void* SelStatement::fetchAndPrint(bool SQL)
00537 {
00538     void *tuple = table->fetch();
00539     if (NULL == tuple) return NULL;
00540     FieldValue *value;
00541     bool nullValueSet;
00542     char stmt[128];
00543     if (SQL) {
00544         sprintf(stmt, "INSERT INTO %s VALUES(", table->getName());
00545         printf("%s", stmt);
00546     }
00547     for (int i = 0; i < totalFields; i++)
00548     {
00549         value = bindFields[i];
00550         nullValueSet = table->isFldNull(i+1);
00551         if (nullValueSet) 
00552             if (SQL) { 
00553                 if (i==0) 
00554                     printf("NULL"); 
00555                 else
00556                     printf(", NULL"); 
00557             }
00558             else printf("NULL\t");
00559         else  {
00560             if (SQL) {
00561                 switch(value->type)
00562                 {
00563                     case typeString:
00564                     case typeDate:
00565                     case typeTime:
00566                     case typeTimeStamp:
00567                     {
00568                         if (i==0) 
00569                             printf(" '"); 
00570                         else
00571                             printf(", '");
00572                         break;
00573                     }
00574                     default:
00575                     {
00576                         if (i!=0) 
00577                            printf(",");
00578                     }
00579                 }
00580             }
00581             AllDataType::printVal(value->value, value->type, value->length);
00582             if (SQL) {
00583                 switch(value->type)
00584                 {
00585                     case typeString:
00586                     case typeDate:
00587                     case typeTime:
00588                     case typeTimeStamp:
00589                         printf("'");
00590                 }
00591             } else printf("\t");
00592         }
00593     }
00594     if (SQL) printf(");\n");
00595     return tuple;
00596 }
00597 
00598 void* SelStatement::next()
00599 {
00600     return( table->fetch() );
00601 }
00602 
00603 void* SelStatement::getFieldValuePtr( int pos )
00604 {
00605     FieldValue *v = bindFields[pos];
00606     return ( (void*) v->value );
00607 }
00608 
00609 int SelStatement::noOfProjFields()
00610 {
00611     return totalFields;
00612 }
00613 
00614 DbRetVal SelStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
00615 {
00616     //TODO::if not yet prepared return error
00617     //TODO::check the upper limit for projpos
00618     ListIterator iter = parsedData->getFieldNameList().getIterator();
00619     FieldName *name = NULL;
00620     DbRetVal rv = OK;
00621     int position =0;
00622     while (iter.hasElement())
00623     {
00624         name = (FieldName*)iter.nextElement();
00625         if (NULL == name) 
00626         {
00627             printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
00628             return ErrSysFatal;
00629         }
00630         if (position == projpos) break;
00631         position++;
00632     }
00633 
00634     rv = table->getFieldInfo(name->fldName, fInfo);
00635 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7