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