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 ***************************************************************************/
16 #include "Statement.h"
18 DelStatement::DelStatement()
28 DelStatement::~DelStatement() {
30 table
->setCondition(NULL
);
31 if (dbMgr
) dbMgr
->closeTable(table
);
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");
48 info
->type
= value
->type
;
49 info
->length
= value
->length
;
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
)
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 memcpy(cValue
->value
, value
, 2 * 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 rv
= table
->getFieldInfo(value
->fName
, fInfo
);
292 if (ErrNotFound
== rv
)
295 printError(ErrSyntaxError
, "Field %s does not exist in table",
297 return ErrSyntaxError
;
299 value
->type
= fInfo
->type
;
300 value
->length
= fInfo
->length
;
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
)
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
);
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*));
328 while(iter
.hasElement())
330 value
= (ConditionValue
*) iter
.nextElement();
333 free(params
); params
= NULL
;
334 free(paramValues
); paramValues
= NULL
;
335 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
338 params
[value
->paramNo
-1 ] = value
;