00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "Statement.h"
00017 #include <Info.h>
00018
00019 InsStatement::InsStatement()
00020 {
00021 parsedData = NULL;
00022 dbMgr = NULL;
00023 table = NULL;
00024 params = NULL;
00025 paramValues = NULL;
00026 totalParams = 0;
00027 }
00028
00029 InsStatement::~InsStatement()
00030 {
00031 if (table) {
00032 table->setCondition(NULL);
00033 if (dbMgr) dbMgr->closeTable(table);
00034 }
00035 if (totalParams) {
00036 free(params);
00037 params = NULL;
00038 free(paramValues);
00039 paramValues = NULL;
00040 }
00041 }
00042
00043 DbRetVal InsStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
00044 {
00045 if (paramPos >totalParams ) return ErrBadCall;
00046 FieldValue *value = (FieldValue*)params[paramPos-1];
00047 if (value == NULL)
00048 {
00049 printError(ErrSysFatal, "param ptr is null: should never happen\n");
00050 return ErrBadArg;
00051 }
00052 info->type = value->type;
00053 info->length = value->length;
00054 return OK;
00055 }
00056
00057 DbRetVal InsStatement::execute(int &rowsAffected)
00058 {
00059 DbRetVal rv = OK;
00060
00061 FieldValue *value;
00062 for (int i = 0; i < totalParams; i ++)
00063 {
00064 value = (FieldValue*) params[i];
00065 if (paramValues[i] == NULL)
00066 {
00067
00068 continue;
00069
00070 }
00071 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
00072 }
00073 rv = table->insertTuple();
00074 if (rv ==OK) rowsAffected = 1;
00075 return rv;
00076 }
00077
00078 DbRetVal InsStatement::setParam(int paramNo, void *value)
00079 {
00080 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00081 if (NULL == value) return ErrBadArg;
00082 paramValues[paramNo -1] = (char*) value;
00083 return OK;
00084 }
00085
00086 DbRetVal InsStatement::setShortParam(int paramNo, short value)
00087 {
00088 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00089 FieldValue *fValue = (FieldValue*) params [paramNo-1];
00090 if (NULL == fValue)
00091 {
00092 printError(ErrSysFatal, "field value is null. Should never happen");
00093 return ErrSysFatal;
00094 }
00095 *(short*)fValue->value = value;
00096 return OK;
00097 }
00098
00099 DbRetVal InsStatement::setIntParam(int paramNo, int value)
00100 {
00101 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00102 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00103 if (NULL == cValue)
00104 {
00105 printError(ErrSysFatal, "condition value is null. Should never happen");
00106 return ErrSysFatal;
00107 }
00108
00109 *(int*)cValue->value = value;
00110 return OK;
00111 }
00112 DbRetVal InsStatement::setLongParam(int paramNo, long value)
00113 {
00114 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00115 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00116 if (NULL == cValue)
00117 {
00118 printError(ErrSysFatal, "condition value is null. Should never happen");
00119 return ErrSysFatal;
00120 }
00121 *(long*)cValue->value = value;
00122 return OK;
00123 }
00124
00125 DbRetVal InsStatement::setLongLongParam(int paramNo, long long value)
00126 {
00127 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00128 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00129 if (NULL == cValue)
00130 {
00131 printError(ErrSysFatal, "condition value is null. Should never happen");
00132 return ErrSysFatal;
00133 }
00134 *(long long*)cValue->value = value;
00135 return OK;
00136 }
00137 DbRetVal InsStatement::setByteIntParam(int paramNo, ByteInt value)
00138 {
00139 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00140 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00141 if (NULL == cValue)
00142 {
00143 printError(ErrSysFatal, "condition value is null. Should never happen");
00144 return ErrSysFatal;
00145 }
00146 *(ByteInt*)cValue->value = value;
00147 return OK;
00148 }
00149 DbRetVal InsStatement::setFloatParam(int paramNo, float value)
00150 {
00151 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00152 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00153 if (NULL == cValue)
00154 {
00155 printError(ErrSysFatal, "condition value is null. Should never happen");
00156 return ErrSysFatal;
00157 }
00158 *(float*)cValue->value = value;
00159 return OK;
00160 }
00161 DbRetVal InsStatement::setDoubleParam(int paramNo, double value)
00162 {
00163 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00164 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00165 if (NULL == cValue)
00166 {
00167 printError(ErrSysFatal, "condition value is null. Should never happen");
00168 return ErrSysFatal;
00169 }
00170 *(double*)cValue->value = value;
00171 return OK;
00172 }
00173 DbRetVal InsStatement::setStringParam(int paramNo, char *value)
00174 {
00175 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00176 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00177 if (NULL == cValue)
00178 {
00179 printError(ErrSysFatal, "condition value is null. Should never happen");
00180 return ErrSysFatal;
00181 }
00182
00183
00184 strcpy((char*)cValue->value, value);
00185 return OK;
00186 }
00187 DbRetVal InsStatement::setDateParam(int paramNo, Date value)
00188 {
00189 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00190 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00191 if (NULL == cValue)
00192 {
00193 printError(ErrSysFatal, "condition value is null. Should never happen");
00194 return ErrSysFatal;
00195 }
00196 *(Date*)cValue->value = value;
00197 return OK;
00198 }
00199 DbRetVal InsStatement::setTimeParam(int paramNo, Time value)
00200 {
00201 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00202 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00203 if (NULL == cValue)
00204 {
00205 printError(ErrSysFatal, "condition value is null. Should never happen");
00206 return ErrSysFatal;
00207 }
00208 *(Time*)cValue->value = value;
00209 return OK;
00210 }
00211 void* InsStatement::getParamValuePtr( int pos )
00212 {
00213 FieldValue *p = (FieldValue*) params [pos-1];
00214 return ( (void*) p->value );
00215 }
00216
00217 DbRetVal InsStatement::setTimeStampParam(int paramNo, TimeStamp value)
00218 {
00219 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00220 FieldValue *cValue = (FieldValue*) params [paramNo-1];
00221 if (NULL == cValue)
00222 {
00223 printError(ErrSysFatal, "condition value is null. Should never happen");
00224 return ErrSysFatal;
00225 }
00226 *(TimeStamp*)cValue->value = value;
00227 return OK;
00228 }
00229
00230 DbRetVal InsStatement::resolve()
00231 {
00232 if (dbMgr == NULL) return ErrNoConnection;
00233
00234 table = dbMgr->openTable(parsedData->getTableName());
00235 if (table == NULL)
00236 {
00237 printError(ErrNotExists, "Unable to open the table:Table not exists");
00238 return ErrNotExists;
00239 }
00240
00241 List fieldNameList;
00242
00243 if( 0 == parsedData->getFieldNameList().size() )
00244 fieldNameList = table->getFieldNameList();
00245 else
00246 fieldNameList = parsedData->getFieldNameList();
00247
00248
00249
00250 if ( fieldNameList.size() !=
00251 parsedData->getFieldValueList().size())
00252 {
00253 dbMgr->closeTable(table);
00254 table = NULL;
00255 printError(ErrSyntaxError, "Field Name list and field values do not match");
00256 return ErrSyntaxError;
00257 }
00258
00259
00260 ListIterator iter = fieldNameList.getIterator();
00261 ListIterator valIter = parsedData->getFieldValueList().getIterator();
00262 FieldName *name; FieldValue *value;
00263 FieldInfo *fInfo = new FieldInfo();
00264 int paramPos =1;
00265 DbRetVal rv = OK;
00266 while (iter.hasElement())
00267 {
00268 name = (FieldName*)iter.nextElement();
00269 value = (FieldValue*) valIter.nextElement();
00270 if (NULL == name || NULL == value)
00271 {
00272 dbMgr->closeTable(table);
00273 table = NULL;
00274 delete fInfo;
00275 printError(ErrSyntaxError, "Field Name list and field values do not match");
00276 return ErrSyntaxError;
00277 }
00278 rv = table->getFieldInfo(name->fldName, fInfo);
00279 if (ErrNotFound == rv)
00280 {
00281 dbMgr->closeTable(table);
00282 table = NULL;
00283 delete fInfo;
00284 printError(ErrSyntaxError, "Field %s does not exist in table",
00285 name->fldName);
00286 return ErrSyntaxError;
00287 }
00288 value->type = fInfo->type;
00289 value->length = fInfo->length;
00290 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
00291 if (value->parsedString == NULL) continue;
00292 table->bindFld(name->fldName, value->value);
00293 if (value->parsedString[0] == '?')
00294 {
00295 value->paramNo = paramPos++;
00296 }
00297 if (!value->paramNo)
00298 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
00299 }
00300 delete fInfo;
00301 totalParams = paramPos -1;
00302 if (0 == totalParams) {
00303 if( 0 == parsedData->getFieldNameList().size() ) {
00304 iter.reset();
00305 while(iter.hasElement())
00306 delete (Identifier *) iter.nextElement();
00307 fieldNameList.reset();
00308 }
00309 return OK;
00310 }
00311 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
00312 paramValues = (char**) malloc( totalParams * sizeof(char*));
00313 memset(params, 0, totalParams * sizeof(FieldValue*));
00314 memset(paramValues, 0, totalParams * sizeof(char*));
00315 valIter.reset();
00316 while(valIter.hasElement())
00317 {
00318 value = (FieldValue*) valIter.nextElement();
00319 if (value == NULL)
00320 {
00321 dbMgr->closeTable(table);
00322 table = NULL;
00323 free(params); params = NULL;
00324 free(paramValues); paramValues = NULL;
00325 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
00326 return ErrSysFatal;
00327 }
00328 if (value->paramNo == 0) continue;
00329 params[value->paramNo -1 ] = value;
00330 }
00331
00332 if( 0 == parsedData->getFieldNameList().size() ) {
00333 iter.reset();
00334 while(iter.hasElement())
00335 delete (Identifier *) iter.nextElement();
00336 fieldNameList.reset();
00337 }
00338 return OK;
00339 }
00340