code reorg and moving files to csql base directory
[csql.git] / src / sql / DelStatement.cxx
blob46b5b72ebb2a7915694a3ff7d583677b40397b25
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 char tName[IDENTIFIER_LENGTH];
292 Table::getTableNameAlone(value->fName,tName);
293 if( strcmp(tName,"")!=0 && strcmp(parsedData->getTableName(),tName)!=0 )
295 delete fInfo;
296 printError(ErrSyntaxError, "Field %s does not exist in table %s", value->fName,parsedData->getTableName());
297 return ErrSyntaxError;
299 rv = table->getFieldInfo(value->fName, fInfo);
300 if (ErrNotFound == rv)
302 delete fInfo;
303 printError(ErrSyntaxError, "Field %s does not exist in table",
304 value->fName);
305 return ErrSyntaxError;
307 value->type = fInfo->type;
308 value->length = fInfo->length;
309 value->isNullable = fInfo->isNull;
310 // for binary datatype input buffer size should be 2 times the length
311 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
312 if(value->paramNo ==1) continue; //FOR Is Null support
313 if (value->parsedString == NULL)
315 delete fInfo;
316 printError(ErrSyntaxError, "Condition value should not be NULL");
317 return ErrSyntaxError;
320 if (value->parsedString[0] == '?')
322 //if (! value->opLike) // checks if 'LIKE' operator is used
323 value->paramNo = paramPos++;
325 if (!value->paramNo) {
326 //Checking for valid integer type
327 if((value->type == typeInt) || (value->type==typeShort) || (value->type==typeByteInt) || (value->type==typeLongLong) || (value->type==typeLong)){
328 int len=strlen(value->parsedString);
329 for(int n=0;n<len;n++){
330 int p=value->parsedString[n];
331 if(!(p>=48 && p<=57 || p==45)) {
332 delete fInfo;
333 return ErrBadArg;
337 /* Checking for char data type 8kb(8000) */
338 if(value->type==typeString){
339 int len=strlen(value->parsedString);
340 if(len > 8000){
341 printError(ErrBadRange, "Char DataType length should be less than 8kb(8000).");
342 delete fInfo;
343 return ErrBadRange;
346 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
347 rv = AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
348 if (rv != OK) {
349 delete fInfo;
350 return ErrBadArg;
354 delete fInfo;
355 totalParams = paramPos -1;
356 if (0 == totalParams) return OK;
357 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
358 paramValues = (char**) malloc( totalParams * sizeof(char*));
359 memset(params, 0, totalParams * sizeof(FieldValue*));
360 memset(paramValues, 0, totalParams * sizeof(char*));
361 iter.reset();
362 while(iter.hasElement())
364 value = (ConditionValue*) iter.nextElement();
365 if (value == NULL)
367 free(params); params = NULL;
368 free(paramValues); paramValues = NULL;
369 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
370 return ErrSysFatal;
372 params[value->paramNo -1 ] = value;
374 return OK;
377 int DelStatement::getFldPos(char *name)
379 return table->getFldPos(name);