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() {
29 if (table
) { table
->close(); table
= 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");
45 table
->getFieldNameAlone(value
->fName
,info
->fldName
);
46 info
->type
= value
->type
;
47 info
->length
= value
->length
;
48 info
->isNull
= value
->isNullable
;
51 DbRetVal
DelStatement::execute(int &rowsAffected
)
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
)
62 //printError(ErrBadCall, "param values not set");
65 AllDataType::copyVal(value
->value
, paramValues
[i
], value
->type
, value
->length
);
67 rv
= table
->execute();
68 if (rv
!= OK
) return rv
;
73 tuple
= (char*)table
->fetchNoBind(rv
);
75 if (tuple
== NULL
) {break;}
76 rv
= table
->deleteTuple();
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
;
93 DbRetVal
DelStatement::setShortParam(int paramNo
, short value
)
95 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
96 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
99 printError(ErrSysFatal
, "condition value is null. Should never happen");
102 *(short*)cValue
->value
= value
;
106 DbRetVal
DelStatement::setIntParam(int paramNo
, int value
)
108 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
109 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
112 printError(ErrSysFatal
, "condition value is null. Should never happen");
115 *(int*)cValue
->value
= value
;
118 DbRetVal
DelStatement::setLongParam(int paramNo
, long value
)
120 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
121 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
124 printError(ErrSysFatal
, "condition value is null. Should never happen");
127 *(long*)cValue
->value
= value
;
131 DbRetVal
DelStatement::setLongLongParam(int paramNo
, long long value
)
133 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
134 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
137 printError(ErrSysFatal
, "condition value is null. Should never happen");
140 *(long long*)cValue
->value
= value
;
143 DbRetVal
DelStatement::setByteIntParam(int paramNo
, ByteInt value
)
145 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
146 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
149 printError(ErrSysFatal
, "condition value is null. Should never happen");
152 *(ByteInt
*)cValue
->value
= value
;
155 DbRetVal
DelStatement::setFloatParam(int paramNo
, float value
)
157 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
158 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
161 printError(ErrSysFatal
, "condition value is null. Should never happen");
164 *(float*)cValue
->value
= value
;
167 DbRetVal
DelStatement::setDoubleParam(int paramNo
, double value
)
169 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
170 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
173 printError(ErrSysFatal
, "condition value is null. Should never happen");
176 *(double*)cValue
->value
= value
;
179 DbRetVal
DelStatement::setStringParam(int paramNo
, char *value
)
181 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
182 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
185 printError(ErrSysFatal
, "condition value is null. Should never happen");
188 strcpy((char*)cValue
->value
, value
);
191 DbRetVal
DelStatement::setDateParam(int paramNo
, Date value
)
193 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
194 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
197 printError(ErrSysFatal
, "condition value is null. Should never happen");
200 *(Date
*)cValue
->value
= value
;
203 DbRetVal
DelStatement::setTimeParam(int paramNo
, Time value
)
205 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
206 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
209 printError(ErrSysFatal
, "condition value is null. Should never happen");
212 *(Time
*)cValue
->value
= value
;
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];
227 printError(ErrSysFatal
, "condition value is null. Should never happen");
230 *(TimeStamp
*)cValue
->value
= value
;
234 DbRetVal
DelStatement::setBinaryParam(int paramNo
, void *value
)
236 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
237 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
240 printError(ErrSysFatal
, "condition value is null. Should never happen");
243 AllDataType::convertToBinary(cValue
->value
,value
,typeString
,cValue
->length
);
247 DbRetVal
DelStatement::resolve()
249 if (dbMgr
== NULL
) return ErrNoConnection
;
250 //check whether the table exists
251 table
= dbMgr
->openTable(parsedData
->getTableName());
254 printError(ErrNotExists
, "Unable to open the table:Table not exists");
258 table
->setCondition(parsedData
->getCondition());
260 DbRetVal rv
= resolveForCondition();
263 //TODO::free memory allocated for params
264 table
->setCondition(NULL
);
265 dbMgr
->closeTable(table
);
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();
281 while (iter
.hasElement())
283 value
= (ConditionValue
*) iter
.nextElement();
287 printError(ErrSysFatal
, "Should never happen.");
290 rv
= table
->getFieldInfo(value
->fName
, fInfo
);
291 if (ErrNotFound
== rv
)
294 printError(ErrSyntaxError
, "Field %s does not exist in table",
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
)
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
;
343 int DelStatement::getFldPos(char *name
)
345 return table
->getFldPos(name
);