Removing dependency for Cache module in MMDB build
[csql.git] / src / sql / DelStatement.cxx
blob26729a3156db676c0bb1cda7240d29f487fc5451
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 <os.h>
17 #include <Statement.h>
18 #include <Info.h>
19 DelStatement::DelStatement()
21 parsedData = NULL;
22 dbMgr = NULL;
23 table = NULL;
24 params = NULL;
25 paramValues = NULL;
26 totalParams = 0;
29 DelStatement::~DelStatement() {
30 if (table) { table->close(); table = NULL; }
31 if (totalParams) {
32 free(params);
33 params = NULL;
34 free(paramValues);
35 paramValues = NULL;
40 DbRetVal DelStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
42 if (paramPos <=0 || paramPos > totalParams) return ErrBadArg;
43 ConditionValue *value = (ConditionValue*) params[paramPos-1];
44 if (value == NULL) { printError(ErrBadArg, "Should never happen\n");
45 return ErrBadArg; }
46 table->getFieldNameAlone(value->fName,info->fldName);
47 info->type = value->type;
48 info->length = value->length;
49 info->isNull = value->isNullable;
50 return OK;
52 DbRetVal DelStatement::execute(int &rowsAffected)
54 DbRetVal rv = OK;
55 //copy param values to binded buffer
56 ConditionValue *value;
57 for (int i = 0; i < totalParams; i ++)
59 value = (ConditionValue*) params[i];
60 if (paramValues[i] == NULL)
62 continue;
63 //printError(ErrBadCall, "param values not set");
64 //return ErrBadCall;
66 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
68 rv = table->execute();
69 if (rv != OK) return rv;
70 rowsAffected = 0;
71 void *tuple;
72 while(true)
74 tuple = (char*)table->fetchNoBind(rv);
75 if (rv != OK) break;
76 if (tuple == NULL) {break;}
77 rv = table->deleteTuple();
78 if (rv != OK) break;
79 rowsAffected++;
81 table->closeScan();
82 return rv;
86 DbRetVal DelStatement::setParam(int paramNo, void *value)
88 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
89 if (NULL == value) return ErrBadArg;
90 paramValues[paramNo -1] = (char*) value;
91 return OK;
94 DbRetVal DelStatement::setShortParam(int paramNo, short value)
96 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
97 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
98 if (NULL == cValue)
100 printError(ErrSysFatal, "condition value is null. Should never happen");
101 return ErrSysFatal;
103 *(short*)cValue->value = value;
104 return OK;
107 DbRetVal DelStatement::setIntParam(int paramNo, int value)
109 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
110 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
111 if (NULL == cValue)
113 printError(ErrSysFatal, "condition value is null. Should never happen");
114 return ErrSysFatal;
116 *(int*)cValue->value = value;
117 return OK;
119 DbRetVal DelStatement::setLongParam(int paramNo, long value)
121 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
122 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
123 if (NULL == cValue)
125 printError(ErrSysFatal, "condition value is null. Should never happen");
126 return ErrSysFatal;
128 *(long*)cValue->value = value;
129 return OK;
132 DbRetVal DelStatement::setLongLongParam(int paramNo, long long value)
134 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
135 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
136 if (NULL == cValue)
138 printError(ErrSysFatal, "condition value is null. Should never happen");
139 return ErrSysFatal;
141 *(long long*)cValue->value = value;
142 return OK;
144 DbRetVal DelStatement::setByteIntParam(int paramNo, ByteInt value)
146 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
147 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
148 if (NULL == cValue)
150 printError(ErrSysFatal, "condition value is null. Should never happen");
151 return ErrSysFatal;
153 *(ByteInt*)cValue->value = value;
154 return OK;
156 DbRetVal DelStatement::setFloatParam(int paramNo, float value)
158 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
159 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
160 if (NULL == cValue)
162 printError(ErrSysFatal, "condition value is null. Should never happen");
163 return ErrSysFatal;
165 *(float*)cValue->value = value;
166 return OK;
168 DbRetVal DelStatement::setDoubleParam(int paramNo, double value)
170 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
171 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
172 if (NULL == cValue)
174 printError(ErrSysFatal, "condition value is null. Should never happen");
175 return ErrSysFatal;
177 *(double*)cValue->value = value;
178 return OK;
180 DbRetVal DelStatement::setStringParam(int paramNo, char *value)
182 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
183 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
184 if (NULL == cValue)
186 printError(ErrSysFatal, "condition value is null. Should never happen");
187 return ErrSysFatal;
189 strcpy((char*)cValue->value, value);
190 return OK;
192 DbRetVal DelStatement::setDateParam(int paramNo, Date value)
194 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
195 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
196 if (NULL == cValue)
198 printError(ErrSysFatal, "condition value is null. Should never happen");
199 return ErrSysFatal;
201 *(Date*)cValue->value = value;
202 return OK;
204 DbRetVal DelStatement::setTimeParam(int paramNo, Time value)
206 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
207 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
208 if (NULL == cValue)
210 printError(ErrSysFatal, "condition value is null. Should never happen");
211 return ErrSysFatal;
213 *(Time*)cValue->value = value;
214 return OK;
216 void* DelStatement::getParamValuePtr( int pos )
218 ConditionValue *cValue = (ConditionValue*) params [pos-1];
219 return ( (void*) cValue->value );
222 DbRetVal DelStatement::setTimeStampParam(int paramNo, TimeStamp value)
224 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
225 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
226 if (NULL == cValue)
228 printError(ErrSysFatal, "condition value is null. Should never happen");
229 return ErrSysFatal;
231 *(TimeStamp*)cValue->value = value;
232 return OK;
235 DbRetVal DelStatement::setBinaryParam(int paramNo, void *value, int length)
237 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
238 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
239 if (NULL == cValue)
241 printError(ErrSysFatal, "condition value is null. Should never happen");
242 return ErrSysFatal;
244 AllDataType::convertToBinary(cValue->value,value,typeString,cValue->length);
245 return OK;
248 DbRetVal DelStatement::resolve()
250 if (dbMgr == NULL) return ErrNoConnection;
251 //check whether the table exists
252 table = dbMgr->openTable(parsedData->getTableName());
253 if (table == NULL)
255 printError(ErrNotExists, "Unable to open the table:Table not exists");
256 return ErrNotExists;
259 table->setCondition(parsedData->getCondition());
261 DbRetVal rv = resolveForCondition();
262 if (rv != OK)
264 //TODO::free memory allocated for params
265 table->setCondition(NULL);
266 dbMgr->closeTable(table);
267 table = NULL;
269 return rv;
273 DbRetVal DelStatement::resolveForCondition()
275 //get the fieldname list and validate field names
276 ListIterator iter = parsedData->getConditionValueList().getIterator();
278 ConditionValue *value;
279 FieldInfo *fInfo = new FieldInfo();
280 int paramPos =1;
281 DbRetVal rv = OK;
282 while (iter.hasElement())
284 value = (ConditionValue*) iter.nextElement();
285 if (NULL == value)
287 delete fInfo;
288 printError(ErrSysFatal, "Should never happen.");
289 return ErrSysFatal;
291 rv = table->getFieldInfo(value->fName, fInfo);
292 if (ErrNotFound == rv)
294 delete fInfo;
295 printError(ErrSyntaxError, "Field %s does not exist in table",
296 value->fName);
297 return ErrSyntaxError;
299 value->type = fInfo->type;
300 value->length = fInfo->length;
301 value->isNullable = fInfo->isNull;
302 // for binary datatype input buffer size should be 2 times the length
303 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
304 if(value->paramNo ==1) continue; //FOR Is Null support
305 if (value->parsedString == NULL)
307 delete fInfo;
308 printError(ErrSyntaxError, "Condition value should not be NULL");
309 return ErrSyntaxError;
312 if (value->parsedString[0] == '?')
314 //if (! value->opLike) // checks if 'LIKE' operator is used
315 value->paramNo = paramPos++;
317 if (!value->paramNo) {
318 //Checking for valid integer type
319 if((value->type == typeInt) || (value->type==typeShort) || (value->type==typeByteInt) || (value->type==typeLongLong) || (value->type==typeLong)){
320 int len=strlen(value->parsedString);
321 for(int n=0;n<len;n++){
322 int p=value->parsedString[n];
323 if(!(p>=48 && p<=57 || p==45))
324 return ErrBadArg;
328 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
329 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
332 delete fInfo;
333 totalParams = paramPos -1;
334 if (0 == totalParams) return OK;
335 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
336 paramValues = (char**) malloc( totalParams * sizeof(char*));
337 memset(params, 0, totalParams * sizeof(FieldValue*));
338 memset(paramValues, 0, totalParams * sizeof(char*));
339 iter.reset();
340 while(iter.hasElement())
342 value = (ConditionValue*) iter.nextElement();
343 if (value == NULL)
345 free(params); params = NULL;
346 free(paramValues); paramValues = NULL;
347 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
348 return ErrSysFatal;
350 params[value->paramNo -1 ] = value;
352 return OK;
355 int DelStatement::getFldPos(char *name)
357 return table->getFldPos(name);