table->closeScan is called instead of close
[csql.git] / src / sql / DelStatement.cxx
blob40cc4fb060227599ef714ffd806fc2c5304cdfbe
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include "Statement.h"
17 #include <Info.h>
18 DelStatement::DelStatement()
20 parsedData = NULL;
21 dbMgr = NULL;
22 table = NULL;
23 params = NULL;
24 paramValues = NULL;
25 totalParams = 0;
28 DelStatement::~DelStatement() {
29 if (table) {
30 table->setCondition(NULL);
31 if (dbMgr) dbMgr->closeTable(table);
33 if (totalParams) {
34 free(params);
35 params = NULL;
36 free(paramValues);
37 paramValues = NULL;
42 DbRetVal DelStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
44 if (paramPos <=0 || paramPos > totalParams) return ErrBadArg;
45 ConditionValue *value = (ConditionValue*) params[paramPos-1];
46 if (value == NULL) { printError(ErrBadArg, "Should never happen\n");
47 return ErrBadArg; }
48 table->getFieldNameAlone(value->fName,info->fldName);
49 info->type = value->type;
50 info->length = value->length;
51 info->isNull = value->isNullable;
52 return OK;
54 DbRetVal DelStatement::execute(int &rowsAffected)
56 DbRetVal rv = OK;
57 //copy param values to binded buffer
58 ConditionValue *value;
59 for (int i = 0; i < totalParams; i ++)
61 value = (ConditionValue*) params[i];
62 if (paramValues[i] == NULL)
64 continue;
65 //printError(ErrBadCall, "param values not set");
66 //return ErrBadCall;
68 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
70 rv = table->execute();
71 if (rv != OK) return rv;
72 rowsAffected = 0;
73 void *tuple;
74 while(true)
76 tuple = (char*)table->fetchNoBind(rv);
77 if (rv != OK) break;
78 if (tuple == NULL) {break;}
79 rv = table->deleteTuple();
80 if (rv != OK) break;
81 rowsAffected++;
83 table->close();
84 return rv;
88 DbRetVal DelStatement::setParam(int paramNo, void *value)
90 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
91 if (NULL == value) return ErrBadArg;
92 paramValues[paramNo -1] = (char*) value;
93 return OK;
96 DbRetVal DelStatement::setShortParam(int paramNo, short value)
98 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
99 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
100 if (NULL == cValue)
102 printError(ErrSysFatal, "condition value is null. Should never happen");
103 return ErrSysFatal;
105 *(short*)cValue->value = value;
106 return OK;
109 DbRetVal DelStatement::setIntParam(int paramNo, int value)
111 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
112 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
113 if (NULL == cValue)
115 printError(ErrSysFatal, "condition value is null. Should never happen");
116 return ErrSysFatal;
118 *(int*)cValue->value = value;
119 return OK;
121 DbRetVal DelStatement::setLongParam(int paramNo, long value)
123 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
124 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
125 if (NULL == cValue)
127 printError(ErrSysFatal, "condition value is null. Should never happen");
128 return ErrSysFatal;
130 *(long*)cValue->value = value;
131 return OK;
134 DbRetVal DelStatement::setLongLongParam(int paramNo, long long value)
136 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
137 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
138 if (NULL == cValue)
140 printError(ErrSysFatal, "condition value is null. Should never happen");
141 return ErrSysFatal;
143 *(long long*)cValue->value = value;
144 return OK;
146 DbRetVal DelStatement::setByteIntParam(int paramNo, ByteInt value)
148 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
149 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
150 if (NULL == cValue)
152 printError(ErrSysFatal, "condition value is null. Should never happen");
153 return ErrSysFatal;
155 *(ByteInt*)cValue->value = value;
156 return OK;
158 DbRetVal DelStatement::setFloatParam(int paramNo, float value)
160 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
161 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
162 if (NULL == cValue)
164 printError(ErrSysFatal, "condition value is null. Should never happen");
165 return ErrSysFatal;
167 *(float*)cValue->value = value;
168 return OK;
170 DbRetVal DelStatement::setDoubleParam(int paramNo, double value)
172 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
173 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
174 if (NULL == cValue)
176 printError(ErrSysFatal, "condition value is null. Should never happen");
177 return ErrSysFatal;
179 *(double*)cValue->value = value;
180 return OK;
182 DbRetVal DelStatement::setStringParam(int paramNo, char *value)
184 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
185 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
186 if (NULL == cValue)
188 printError(ErrSysFatal, "condition value is null. Should never happen");
189 return ErrSysFatal;
191 strcpy((char*)cValue->value, value);
192 return OK;
194 DbRetVal DelStatement::setDateParam(int paramNo, Date value)
196 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
197 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
198 if (NULL == cValue)
200 printError(ErrSysFatal, "condition value is null. Should never happen");
201 return ErrSysFatal;
203 *(Date*)cValue->value = value;
204 return OK;
206 DbRetVal DelStatement::setTimeParam(int paramNo, Time value)
208 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
209 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
210 if (NULL == cValue)
212 printError(ErrSysFatal, "condition value is null. Should never happen");
213 return ErrSysFatal;
215 *(Time*)cValue->value = value;
216 return OK;
218 void* DelStatement::getParamValuePtr( int pos )
220 ConditionValue *cValue = (ConditionValue*) params [pos-1];
221 return ( (void*) cValue->value );
224 DbRetVal DelStatement::setTimeStampParam(int paramNo, TimeStamp value)
226 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
227 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
228 if (NULL == cValue)
230 printError(ErrSysFatal, "condition value is null. Should never happen");
231 return ErrSysFatal;
233 *(TimeStamp*)cValue->value = value;
234 return OK;
237 DbRetVal DelStatement::setBinaryParam(int paramNo, void *value)
239 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
240 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
241 if (NULL == cValue)
243 printError(ErrSysFatal, "condition value is null. Should never happen");
244 return ErrSysFatal;
246 AllDataType::convertToBinary(cValue->value,value,typeString,cValue->length);
247 return OK;
250 DbRetVal DelStatement::resolve()
252 if (dbMgr == NULL) return ErrNoConnection;
253 //check whether the table exists
254 table = dbMgr->openTable(parsedData->getTableName());
255 if (table == NULL)
257 printError(ErrNotExists, "Unable to open the table:Table not exists");
258 return ErrNotExists;
261 table->setCondition(parsedData->getCondition());
263 DbRetVal rv = resolveForCondition();
264 if (rv != OK)
266 //TODO::free memory allocated for params
267 table->setCondition(NULL);
268 dbMgr->closeTable(table);
269 table = NULL;
271 return rv;
275 DbRetVal DelStatement::resolveForCondition()
277 //get the fieldname list and validate field names
278 ListIterator iter = parsedData->getConditionValueList().getIterator();
280 ConditionValue *value;
281 FieldInfo *fInfo = new FieldInfo();
282 int paramPos =1;
283 DbRetVal rv = OK;
284 while (iter.hasElement())
286 value = (ConditionValue*) iter.nextElement();
287 if (NULL == value)
289 delete fInfo;
290 printError(ErrSysFatal, "Should never happen.");
291 return ErrSysFatal;
293 rv = table->getFieldInfo(value->fName, fInfo);
294 if (ErrNotFound == rv)
296 delete fInfo;
297 printError(ErrSyntaxError, "Field %s does not exist in table",
298 value->fName);
299 return ErrSyntaxError;
301 value->type = fInfo->type;
302 value->length = fInfo->length;
303 value->isNullable = fInfo->isNull;
304 // for binary datatype input buffer size should be 2 times the length
305 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
306 if (value->parsedString == NULL)
308 delete fInfo;
309 printError(ErrSyntaxError, "Condition value should not be NULL");
310 return ErrSyntaxError;
313 if (value->parsedString[0] == '?')
315 if (! value->opLike) // checks if 'LIKE' operator is used
316 value->paramNo = paramPos++;
318 if (!value->paramNo) {
319 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
320 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
323 delete fInfo;
324 totalParams = paramPos -1;
325 if (0 == totalParams) return OK;
326 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
327 paramValues = (char**) malloc( totalParams * sizeof(char*));
328 memset(params, 0, totalParams * sizeof(FieldValue*));
329 memset(paramValues, 0, totalParams * sizeof(char*));
330 iter.reset();
331 while(iter.hasElement())
333 value = (ConditionValue*) iter.nextElement();
334 if (value == NULL)
336 free(params); params = NULL;
337 free(paramValues); paramValues = NULL;
338 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
339 return ErrSysFatal;
341 params[value->paramNo -1 ] = value;
343 return OK;
346 int DelStatement::getFldPos(char *name)
348 return table->getFldPos(name);