1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
15 ***************************************************************************/
17 #include <Statement.h>
19 DelStatement::DelStatement()
29 DelStatement::~DelStatement() {
30 if (table
) { table
->close(); table
= 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");
46 table
->getFieldNameAlone(value
->fName
,info
->fldName
);
47 info
->type
= value
->type
;
48 info
->length
= value
->length
;
49 info
->isNull
= value
->isNullable
;
52 DbRetVal
DelStatement::execute(int &rowsAffected
)
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
)
63 //printError(ErrBadCall, "param values not set");
66 AllDataType::copyVal(value
->value
, paramValues
[i
], value
->type
, value
->length
);
68 rv
= table
->execute();
69 if (rv
!= OK
) return rv
;
74 tuple
= (char*)table
->fetchNoBind(rv
);
76 if (tuple
== NULL
) {break;}
77 rv
= table
->deleteTuple();
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
;
94 DbRetVal
DelStatement::setShortParam(int paramNo
, short value
)
96 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
97 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
100 printError(ErrSysFatal
, "condition value is null. Should never happen");
103 *(short*)cValue
->value
= value
;
107 DbRetVal
DelStatement::setIntParam(int paramNo
, int value
)
109 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
110 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
113 printError(ErrSysFatal
, "condition value is null. Should never happen");
116 *(int*)cValue
->value
= value
;
119 DbRetVal
DelStatement::setLongParam(int paramNo
, long value
)
121 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
122 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
125 printError(ErrSysFatal
, "condition value is null. Should never happen");
128 *(long*)cValue
->value
= value
;
132 DbRetVal
DelStatement::setLongLongParam(int paramNo
, long long value
)
134 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
135 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
138 printError(ErrSysFatal
, "condition value is null. Should never happen");
141 *(long long*)cValue
->value
= value
;
144 DbRetVal
DelStatement::setByteIntParam(int paramNo
, ByteInt value
)
146 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
147 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
150 printError(ErrSysFatal
, "condition value is null. Should never happen");
153 *(ByteInt
*)cValue
->value
= value
;
156 DbRetVal
DelStatement::setFloatParam(int paramNo
, float value
)
158 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
159 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
162 printError(ErrSysFatal
, "condition value is null. Should never happen");
165 *(float*)cValue
->value
= value
;
168 DbRetVal
DelStatement::setDoubleParam(int paramNo
, double value
)
170 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
171 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
174 printError(ErrSysFatal
, "condition value is null. Should never happen");
177 *(double*)cValue
->value
= value
;
180 DbRetVal
DelStatement::setStringParam(int paramNo
, char *value
)
182 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
183 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
186 printError(ErrSysFatal
, "condition value is null. Should never happen");
189 strcpy((char*)cValue
->value
, value
);
192 DbRetVal
DelStatement::setDateParam(int paramNo
, Date value
)
194 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
195 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
198 printError(ErrSysFatal
, "condition value is null. Should never happen");
201 *(Date
*)cValue
->value
= value
;
204 DbRetVal
DelStatement::setTimeParam(int paramNo
, Time value
)
206 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
207 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
210 printError(ErrSysFatal
, "condition value is null. Should never happen");
213 *(Time
*)cValue
->value
= value
;
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];
228 printError(ErrSysFatal
, "condition value is null. Should never happen");
231 *(TimeStamp
*)cValue
->value
= value
;
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];
241 printError(ErrSysFatal
, "condition value is null. Should never happen");
244 AllDataType::convertToBinary(cValue
->value
,value
,typeString
,cValue
->length
);
248 DbRetVal
DelStatement::resolve()
250 if (dbMgr
== NULL
) return ErrNoConnection
;
251 //check whether the table exists
252 table
= dbMgr
->openTable(parsedData
->getTableName());
255 printError(ErrNotExists
, "Unable to open the table:Table not exists");
259 table
->setCondition(parsedData
->getCondition());
261 DbRetVal rv
= resolveForCondition();
264 //TODO::free memory allocated for params
265 table
->setCondition(NULL
);
266 dbMgr
->closeTable(table
);
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();
282 while (iter
.hasElement())
284 value
= (ConditionValue
*) iter
.nextElement();
288 printError(ErrSysFatal
, "Should never happen.");
291 char tName
[IDENTIFIER_LENGTH
];
292 Table::getTableNameAlone(value
->fName
,tName
);
293 if( strcmp(tName
,"")!=0 && strcmp(parsedData
->getTableName(),tName
)!=0 )
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
)
303 printError(ErrSyntaxError
, "Field %s does not exist in table",
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
)
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)) {
337 /* Checking for char data type 8kb(8000) */
338 if(value
->type
==typeString
){
339 int len
=strlen(value
->parsedString
);
341 printError(ErrBadRange
, "Char DataType length should be less than 8kb(8000).");
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
);
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*));
362 while(iter
.hasElement())
364 value
= (ConditionValue
*) iter
.nextElement();
367 free(params
); params
= NULL
;
368 free(paramValues
); paramValues
= NULL
;
369 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
372 params
[value
->paramNo
-1 ] = value
;
377 int DelStatement::getFldPos(char *name
)
379 return table
->getFldPos(name
);