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 UpdStatement::UpdStatement()
00020 {
00021 parsedData = NULL;
00022 dbMgr = NULL;
00023 table = NULL;
00024 params = NULL;
00025 paramValues = NULL;
00026 totalParams = 0;
00027 totalAssignParams =0;
00028 }
00029
00030 UpdStatement::~UpdStatement() {
00031 if (totalParams) {
00032
00033 free(params);
00034 params = NULL;
00035 free(paramValues);
00036 paramValues = NULL;
00037 }
00038 if (table) {
00039 table->setCondition(NULL);
00040 if (dbMgr) dbMgr->closeTable(table);
00041 }
00042 }
00043 void* UpdStatement::getParamValuePtr( int pos )
00044 {
00045 ConditionValue *cValue;
00046 UpdateFieldValue *uValue;
00047 if (pos <= totalAssignParams) {
00048 uValue = (UpdateFieldValue*) params[pos-1];
00049 return( (void*) uValue->value );
00050 } else {
00051 cValue = (ConditionValue*) params[pos-1];
00052 return( (void*) cValue->value );
00053 }
00054 }
00055
00056 DbRetVal UpdStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
00057 {
00058 if (paramPos <=0 || paramPos > totalParams) return ErrBadArg;
00059 if (NULL == params[paramPos-1])
00060 {
00061 printError(ErrSysFatal, "param not set. Should never happen");
00062 return ErrSysFatal;
00063 }
00064
00065 ConditionValue *cValue;
00066 UpdateFieldValue *uValue;
00067 if (paramPos <= totalAssignParams) {
00068 uValue = (UpdateFieldValue*) params[paramPos-1];
00069 info->type = uValue->type;
00070 info->length = uValue->length;
00071 } else {
00072 cValue = (ConditionValue*) params[paramPos-1];
00073 info->type = cValue->type;
00074 info->length = cValue->length;
00075 }
00076 return OK;
00077
00078 }
00079 DbRetVal UpdStatement::execute(int &rowsAffected)
00080 {
00081 DbRetVal rv = OK;
00082
00083 ConditionValue *cValue;
00084 UpdateFieldValue *uValue;
00085
00086 for (int i = 0; i < totalParams; i ++)
00087 {
00088 if (i < totalAssignParams) {
00089 uValue = (UpdateFieldValue*) params[i];
00090 if (paramValues[i] == NULL)
00091 {
00092 continue;
00093
00094
00095 }
00096 AllDataType::copyVal(uValue->value, paramValues[i], uValue->type, uValue->length);
00097 } else {
00098 cValue = (ConditionValue*) params[i];
00099 if (paramValues[i] == NULL)
00100 {
00101 continue;
00102
00103
00104 }
00105 AllDataType::copyVal(cValue->value, paramValues[i], cValue->type, cValue->length);
00106 }
00107 }
00108 rv = table->execute();
00109 if (rv != OK) return rv;
00110 rowsAffected = 0;
00111 void *tuple;
00112 while(true)
00113 {
00114 tuple = (char*)table->fetchNoBind(rv);
00115 if (rv != OK) break;
00116 if (tuple == NULL) {break;}
00117 rv = table->updateTuple();
00118 if (rv != OK) break;
00119 rowsAffected++;
00120 }
00121 table->close();
00122 return rv;
00123 }
00124
00125
00126 DbRetVal UpdStatement::setParam(int paramNo, void *value)
00127 {
00128 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00129 if (NULL == value) return ErrBadArg;
00130 paramValues[paramNo -1] = (char*) value;
00131 return OK;
00132 }
00133
00134 DbRetVal UpdStatement::setShortParam(int paramNo, short value)
00135 {
00136 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00137 if (NULL == params[paramNo-1])
00138 {
00139 printError(ErrSysFatal, "param not set. Should never happen");
00140 return ErrSysFatal;
00141 }
00142
00143 ConditionValue *cValue;
00144 UpdateFieldValue *uValue;
00145 if (paramNo <= totalAssignParams) {
00146 uValue = (UpdateFieldValue*) params[paramNo-1];
00147 *(short*)uValue->value = value;
00148 } else {
00149 cValue = (ConditionValue*) params[paramNo-1];
00150 *(short*)cValue->value = value;
00151 }
00152 return OK;
00153 }
00154
00155
00156 DbRetVal UpdStatement::setIntParam(int paramNo, int value)
00157 {
00158 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00159 if (NULL == params[paramNo-1])
00160 {
00161 printError(ErrSysFatal, "param not set. Should never happen");
00162 return ErrSysFatal;
00163 }
00164 ConditionValue *cValue;
00165 UpdateFieldValue *uValue;
00166 if (paramNo <= totalAssignParams) {
00167 uValue = (UpdateFieldValue*) params[paramNo-1];
00168 *(int*)uValue->value = value;
00169 } else {
00170 cValue = (ConditionValue*) params[paramNo-1];
00171 *(int*)cValue->value = value;
00172 }
00173 return OK;
00174 }
00175
00176 DbRetVal UpdStatement::setLongParam(int paramNo, long value)
00177 {
00178 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00179 if (NULL == params[paramNo-1])
00180 {
00181 printError(ErrSysFatal, "param not set. Should never happen");
00182 return ErrSysFatal;
00183 }
00184
00185 ConditionValue *cValue;
00186 UpdateFieldValue *uValue;
00187 if (paramNo <= totalAssignParams) {
00188 uValue = (UpdateFieldValue*) params[paramNo-1];
00189 *(long*)uValue->value = value;
00190 } else {
00191 cValue = (ConditionValue*) params[paramNo-1];
00192 *(long*)cValue->value = value;
00193 }
00194 return OK;
00195 }
00196
00197 DbRetVal UpdStatement::setLongLongParam(int paramNo, long long value)
00198 {
00199 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00200 if (NULL == params[paramNo-1])
00201 {
00202 printError(ErrSysFatal, "param not set. Should never happen");
00203 return ErrSysFatal;
00204 }
00205
00206 ConditionValue *cValue;
00207 UpdateFieldValue *uValue;
00208 if (paramNo <= totalAssignParams) {
00209 uValue = (UpdateFieldValue*) params[paramNo-1];
00210 *(long long*)uValue->value = value;
00211 } else {
00212 cValue = (ConditionValue*) params[paramNo-1];
00213 *(long long*)cValue->value = value;
00214 }
00215 return OK;
00216 }
00217
00218 DbRetVal UpdStatement::setByteIntParam(int paramNo, ByteInt value)
00219 {
00220 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00221 if (NULL == params[paramNo-1])
00222 {
00223 printError(ErrSysFatal, "param not set. Should never happen");
00224 return ErrSysFatal;
00225 }
00226
00227 ConditionValue *cValue;
00228 UpdateFieldValue *uValue;
00229 if (paramNo <= totalAssignParams) {
00230 uValue = (UpdateFieldValue*) params[paramNo-1];
00231 *(ByteInt*)uValue->value = value;
00232 } else {
00233 cValue = (ConditionValue*) params[paramNo-1];
00234 *(ByteInt*)cValue->value = value;
00235 }
00236 return OK;
00237 }
00238
00239 DbRetVal UpdStatement::setFloatParam(int paramNo, float value)
00240 {
00241 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00242 if (NULL == params[paramNo-1])
00243 {
00244 printError(ErrSysFatal, "param not set. Should never happen");
00245 return ErrSysFatal;
00246 }
00247
00248 ConditionValue *cValue;
00249 UpdateFieldValue *uValue;
00250 if (paramNo <= totalAssignParams) {
00251 uValue = (UpdateFieldValue*) params[paramNo-1];
00252 *(float*)uValue->value = value;
00253 } else {
00254 cValue = (ConditionValue*) params[paramNo-1];
00255 *(float*)cValue->value = value;
00256 }
00257 return OK;
00258 }
00259
00260
00261 DbRetVal UpdStatement::setDoubleParam(int paramNo, double value)
00262 {
00263 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00264 if (NULL == params[paramNo-1])
00265 {
00266 printError(ErrSysFatal, "param not set. Should never happen");
00267 return ErrSysFatal;
00268 }
00269
00270 ConditionValue *cValue;
00271 UpdateFieldValue *uValue;
00272 if (paramNo <= totalAssignParams) {
00273 uValue = (UpdateFieldValue*) params[paramNo-1];
00274 *(double*)uValue->value = value;
00275 } else {
00276 cValue = (ConditionValue*) params[paramNo-1];
00277 *(double*)cValue->value = value;
00278 }
00279 return OK;
00280 }
00281
00282 DbRetVal UpdStatement::setStringParam(int paramNo, char *value)
00283 {
00284 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00285 if (NULL == params[paramNo-1])
00286 {
00287 printError(ErrSysFatal, "param not set. Should never happen");
00288 return ErrSysFatal;
00289 }
00290
00291 ConditionValue *cValue;
00292 UpdateFieldValue *uValue;
00293 if (paramNo <= totalAssignParams) {
00294 uValue = (UpdateFieldValue*) params[paramNo-1];
00295 strcpy((char*)uValue->value, value);
00296 } else {
00297 cValue = (ConditionValue*) params[paramNo-1];
00298 strcpy((char*)cValue->value, value);
00299 }
00300 return OK;
00301 }
00302
00303
00304 DbRetVal UpdStatement::setDateParam(int paramNo, Date value)
00305 {
00306 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00307 if (NULL == params[paramNo-1])
00308 {
00309 printError(ErrSysFatal, "param not set. Should never happen");
00310 return ErrSysFatal;
00311 }
00312
00313 ConditionValue *cValue;
00314 UpdateFieldValue *uValue;
00315 if (paramNo <= totalAssignParams) {
00316 uValue = (UpdateFieldValue*) params[paramNo-1];
00317 *(Date*)uValue->value = value;
00318 } else {
00319 cValue = (ConditionValue*) params[paramNo-1];
00320 *(Date*)cValue->value = value;
00321 }
00322 return OK;
00323 }
00324
00325 DbRetVal UpdStatement::setTimeParam(int paramNo, Time value)
00326 {
00327 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00328 if (NULL == params[paramNo-1])
00329 {
00330 printError(ErrSysFatal, "param not set. Should never happen");
00331 return ErrSysFatal;
00332 }
00333
00334 ConditionValue *cValue;
00335 UpdateFieldValue *uValue;
00336 if (paramNo <= totalAssignParams) {
00337 uValue = (UpdateFieldValue*) params[paramNo-1];
00338 *(Time*)uValue->value = value;
00339 } else {
00340 cValue = (ConditionValue*) params[paramNo-1];
00341 *(Time*)cValue->value = value;
00342 }
00343 return OK;
00344 }
00345
00346
00347 DbRetVal UpdStatement::setTimeStampParam(int paramNo, TimeStamp value)
00348 {
00349 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
00350 if (NULL == params[paramNo-1])
00351 {
00352 printError(ErrSysFatal, "param not set. Should never happen");
00353 return ErrSysFatal;
00354 }
00355
00356 ConditionValue *cValue;
00357 UpdateFieldValue *uValue;
00358 if (paramNo <= totalAssignParams) {
00359 uValue = (UpdateFieldValue*) params[paramNo-1];
00360 *(TimeStamp*)uValue->value = value;
00361 } else {
00362 cValue = (ConditionValue*) params[paramNo-1];
00363 *(TimeStamp*)cValue->value = value;
00364 }
00365 return OK;
00366 }
00367
00368 DbRetVal UpdStatement::resolve()
00369 {
00370 if (dbMgr == NULL) return ErrNoConnection;
00371
00372 table = dbMgr->openTable(parsedData->getTableName());
00373 if (table == NULL)
00374 {
00375 printError(ErrNotExists, "Unable to open the table:Table not exists");
00376 return ErrNotExists;
00377 }
00378
00379 table->setCondition(parsedData->getCondition());
00380
00381 DbRetVal rv = resolveForAssignment();
00382 if (rv != OK)
00383 {
00384
00385 table->setCondition(NULL);
00386 dbMgr->closeTable(table);
00387 table = NULL;
00388 }
00389 return rv;
00390 }
00391
00392 DbRetVal UpdStatement::resolveForAssignment()
00393 {
00394
00395 ListIterator iter = parsedData->getUpdateFieldValueList().getIterator();
00396
00397 UpdateFieldValue *value;
00398 FieldInfo *fInfo = new FieldInfo();
00399 int paramPos =1;
00400 DbRetVal rv = OK;
00401 while (iter.hasElement())
00402 {
00403 value = (UpdateFieldValue*) iter.nextElement();
00404 if (NULL == value)
00405 {
00406 delete fInfo;
00407 printError(ErrSysFatal, "Should never happen.");
00408 return ErrSysFatal;
00409 }
00410 rv = table->getFieldInfo(value->fldName, fInfo);
00411 if (ErrNotFound == rv)
00412 {
00413 delete fInfo;
00414 printError(ErrSyntaxError, "Field %s does not exist in table",
00415 value->fldName);
00416 return ErrSyntaxError;
00417 }
00418 value->type = fInfo->type;
00419 value->length = fInfo->length;
00420 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
00421 table->bindFld(value->fldName, value->value);
00422 if (value->parsedString == NULL)
00423 {
00424 if (fInfo->isNull) { delete fInfo; return ErrNullViolation; }
00425 table->markFldNull(value->fldName);
00426 continue;
00427 }
00428 if (value->parsedString[0] == '?')
00429 {
00430 value->paramNo = paramPos++;
00431 }
00432 if (!value->paramNo)
00433 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, value->length);
00434 }
00435 totalAssignParams = paramPos -1;
00436
00437
00438
00439 ListIterator cIter = parsedData->getConditionValueList().getIterator();
00440 ConditionValue *cValue = NULL;
00441 while (cIter.hasElement())
00442 {
00443 cValue = (ConditionValue*) cIter.nextElement();
00444 if (NULL == cValue)
00445 {
00446 delete fInfo;
00447 printError(ErrSysFatal, "Should never happen.");
00448 return ErrSysFatal;
00449 }
00450 rv = table->getFieldInfo(cValue->fName, fInfo);
00451 if (ErrNotFound == rv)
00452 {
00453 delete fInfo;
00454 printError(ErrSyntaxError, "Field %s does not exist in table",
00455 cValue->fName);
00456 return ErrSyntaxError;
00457 }
00458 cValue->type = fInfo->type;
00459 cValue->length = fInfo->length;
00460 cValue->value = AllDataType::alloc(fInfo->type, fInfo->length);
00461 if (cValue->parsedString == NULL)
00462 {
00463 delete fInfo;
00464 printError(ErrSyntaxError, "Condition value should not be NULL");
00465 return ErrSyntaxError;
00466 }
00467
00468 if (cValue->parsedString[0] == '?')
00469 {
00470 cValue->paramNo = paramPos++;
00471 }
00472 if (!cValue->paramNo)
00473 AllDataType::strToValue(cValue->value, cValue->parsedString, fInfo->type, fInfo->length);
00474 }
00475
00476
00477 delete fInfo;
00478 totalParams = paramPos -1;
00479 if (0 == totalParams) return OK;
00480 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
00481 paramValues = (char**) malloc( totalParams * sizeof(char*));
00482
00483 memset(params, 0, totalParams * sizeof(FieldValue*));
00484 memset(paramValues, 0, totalParams * sizeof(char*));
00485
00486 iter.reset();
00487 while(iter.hasElement())
00488 {
00489 value = (UpdateFieldValue*) iter.nextElement();
00490 if (value == NULL)
00491 {
00492 free(params); params = NULL;
00493 free(paramValues); paramValues = NULL;
00494 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
00495 return ErrSysFatal;
00496 }
00497 if (0 == value->paramNo) continue;
00498 params[value->paramNo -1 ] = value;
00499 }
00500
00501 cIter.reset();
00502 while(cIter.hasElement())
00503 {
00504 cValue = (ConditionValue*) cIter.nextElement();
00505 if (cValue == NULL)
00506 {
00507 free(params); params = NULL;
00508 free(paramValues); paramValues = NULL;
00509 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
00510 return ErrSysFatal;
00511 }
00512 if (0 == cValue->paramNo) continue;
00513 params[cValue->paramNo -1 ] = cValue;
00514 }
00515 return OK;
00516 }