changing close to closescan
[csql.git] / src / sql / DelStatement.cxx
blobc353b8bd467d570090409054f43f6b2c1313c75f
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) { table->close(); table = NULL; }
30 if (totalParams) {
31 free(params);
32 params = NULL;
33 free(paramValues);
34 paramValues = NULL;
39 DbRetVal DelStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
41 if (paramPos <=0 || paramPos > totalParams) return ErrBadArg;
42 ConditionValue *value = (ConditionValue*) params[paramPos-1];
43 if (value == NULL) { printError(ErrBadArg, "Should never happen\n");
44 return ErrBadArg; }
45 table->getFieldNameAlone(value->fName,info->fldName);
46 info->type = value->type;
47 info->length = value->length;
48 info->isNull = value->isNullable;
49 return OK;
51 DbRetVal DelStatement::execute(int &rowsAffected)
53 DbRetVal rv = OK;
54 //copy param values to binded buffer
55 ConditionValue *value;
56 for (int i = 0; i < totalParams; i ++)
58 value = (ConditionValue*) params[i];
59 if (paramValues[i] == NULL)
61 continue;
62 //printError(ErrBadCall, "param values not set");
63 //return ErrBadCall;
65 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
67 rv = table->execute();
68 if (rv != OK) return rv;
69 rowsAffected = 0;
70 void *tuple;
71 while(true)
73 tuple = (char*)table->fetchNoBind(rv);
74 if (rv != OK) break;
75 if (tuple == NULL) {break;}
76 rv = table->deleteTuple();
77 if (rv != OK) break;
78 rowsAffected++;
80 table->closeScan();
81 return rv;
85 DbRetVal DelStatement::setParam(int paramNo, void *value)
87 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
88 if (NULL == value) return ErrBadArg;
89 paramValues[paramNo -1] = (char*) value;
90 return OK;
93 DbRetVal DelStatement::setShortParam(int paramNo, short value)
95 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
96 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
97 if (NULL == cValue)
99 printError(ErrSysFatal, "condition value is null. Should never happen");
100 return ErrSysFatal;
102 *(short*)cValue->value = value;
103 return OK;
106 DbRetVal DelStatement::setIntParam(int paramNo, int value)
108 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
109 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
110 if (NULL == cValue)
112 printError(ErrSysFatal, "condition value is null. Should never happen");
113 return ErrSysFatal;
115 *(int*)cValue->value = value;
116 return OK;
118 DbRetVal DelStatement::setLongParam(int paramNo, long value)
120 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
121 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
122 if (NULL == cValue)
124 printError(ErrSysFatal, "condition value is null. Should never happen");
125 return ErrSysFatal;
127 *(long*)cValue->value = value;
128 return OK;
131 DbRetVal DelStatement::setLongLongParam(int paramNo, long long value)
133 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
134 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
135 if (NULL == cValue)
137 printError(ErrSysFatal, "condition value is null. Should never happen");
138 return ErrSysFatal;
140 *(long long*)cValue->value = value;
141 return OK;
143 DbRetVal DelStatement::setByteIntParam(int paramNo, ByteInt value)
145 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
146 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
147 if (NULL == cValue)
149 printError(ErrSysFatal, "condition value is null. Should never happen");
150 return ErrSysFatal;
152 *(ByteInt*)cValue->value = value;
153 return OK;
155 DbRetVal DelStatement::setFloatParam(int paramNo, float value)
157 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
158 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
159 if (NULL == cValue)
161 printError(ErrSysFatal, "condition value is null. Should never happen");
162 return ErrSysFatal;
164 *(float*)cValue->value = value;
165 return OK;
167 DbRetVal DelStatement::setDoubleParam(int paramNo, double value)
169 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
170 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
171 if (NULL == cValue)
173 printError(ErrSysFatal, "condition value is null. Should never happen");
174 return ErrSysFatal;
176 *(double*)cValue->value = value;
177 return OK;
179 DbRetVal DelStatement::setStringParam(int paramNo, char *value)
181 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
182 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
183 if (NULL == cValue)
185 printError(ErrSysFatal, "condition value is null. Should never happen");
186 return ErrSysFatal;
188 strcpy((char*)cValue->value, value);
189 return OK;
191 DbRetVal DelStatement::setDateParam(int paramNo, Date value)
193 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
194 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
195 if (NULL == cValue)
197 printError(ErrSysFatal, "condition value is null. Should never happen");
198 return ErrSysFatal;
200 *(Date*)cValue->value = value;
201 return OK;
203 DbRetVal DelStatement::setTimeParam(int paramNo, Time value)
205 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
206 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
207 if (NULL == cValue)
209 printError(ErrSysFatal, "condition value is null. Should never happen");
210 return ErrSysFatal;
212 *(Time*)cValue->value = value;
213 return OK;
215 void* DelStatement::getParamValuePtr( int pos )
217 ConditionValue *cValue = (ConditionValue*) params [pos-1];
218 return ( (void*) cValue->value );
221 DbRetVal DelStatement::setTimeStampParam(int paramNo, TimeStamp value)
223 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
224 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
225 if (NULL == cValue)
227 printError(ErrSysFatal, "condition value is null. Should never happen");
228 return ErrSysFatal;
230 *(TimeStamp*)cValue->value = value;
231 return OK;
234 DbRetVal DelStatement::setBinaryParam(int paramNo, void *value)
236 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
237 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
238 if (NULL == cValue)
240 printError(ErrSysFatal, "condition value is null. Should never happen");
241 return ErrSysFatal;
243 AllDataType::convertToBinary(cValue->value,value,typeString,cValue->length);
244 return OK;
247 DbRetVal DelStatement::resolve()
249 if (dbMgr == NULL) return ErrNoConnection;
250 //check whether the table exists
251 table = dbMgr->openTable(parsedData->getTableName());
252 if (table == NULL)
254 printError(ErrNotExists, "Unable to open the table:Table not exists");
255 return ErrNotExists;
258 table->setCondition(parsedData->getCondition());
260 DbRetVal rv = resolveForCondition();
261 if (rv != OK)
263 //TODO::free memory allocated for params
264 table->setCondition(NULL);
265 dbMgr->closeTable(table);
266 table = NULL;
268 return rv;
272 DbRetVal DelStatement::resolveForCondition()
274 //get the fieldname list and validate field names
275 ListIterator iter = parsedData->getConditionValueList().getIterator();
277 ConditionValue *value;
278 FieldInfo *fInfo = new FieldInfo();
279 int paramPos =1;
280 DbRetVal rv = OK;
281 while (iter.hasElement())
283 value = (ConditionValue*) iter.nextElement();
284 if (NULL == value)
286 delete fInfo;
287 printError(ErrSysFatal, "Should never happen.");
288 return ErrSysFatal;
290 rv = table->getFieldInfo(value->fName, fInfo);
291 if (ErrNotFound == rv)
293 delete fInfo;
294 printError(ErrSyntaxError, "Field %s does not exist in table",
295 value->fName);
296 return ErrSyntaxError;
298 value->type = fInfo->type;
299 value->length = fInfo->length;
300 value->isNullable = fInfo->isNull;
301 // for binary datatype input buffer size should be 2 times the length
302 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
303 if (value->parsedString == NULL)
305 delete fInfo;
306 printError(ErrSyntaxError, "Condition value should not be NULL");
307 return ErrSyntaxError;
310 if (value->parsedString[0] == '?')
312 if (! value->opLike) // checks if 'LIKE' operator is used
313 value->paramNo = paramPos++;
315 if (!value->paramNo) {
316 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
317 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
320 delete fInfo;
321 totalParams = paramPos -1;
322 if (0 == totalParams) return OK;
323 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
324 paramValues = (char**) malloc( totalParams * sizeof(char*));
325 memset(params, 0, totalParams * sizeof(FieldValue*));
326 memset(paramValues, 0, totalParams * sizeof(char*));
327 iter.reset();
328 while(iter.hasElement())
330 value = (ConditionValue*) iter.nextElement();
331 if (value == NULL)
333 free(params); params = NULL;
334 free(paramValues); paramValues = NULL;
335 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
336 return ErrSysFatal;
338 params[value->paramNo -1 ] = value;
340 return OK;
343 int DelStatement::getFldPos(char *name)
345 return table->getFldPos(name);