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 SelStatement::SelStatement()
27 bindFieldValues
= NULL
;
31 SelStatement::~SelStatement()
34 table
->setCondition(NULL
);
35 if (dbMgr
) dbMgr
->closeTable(table
);
47 free(bindFieldValues
);
48 bindFieldValues
= NULL
;
52 DbRetVal
SelStatement::getParamFldInfo(int paramNo
, FieldInfo
*&info
)
54 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
55 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
58 printError(ErrSysFatal
, "condition value is null. Should never happen");
61 info
->type
= cValue
->type
;
62 info
->length
= cValue
->length
;
65 DbRetVal
SelStatement::execute(int &rowsAffected
)
68 //copy param values to binded buffer
69 ConditionValue
*value
;
70 for (int i
= 0; i
< totalParams
; i
++)
72 value
= (ConditionValue
*) params
[i
];
73 if (paramValues
[i
] == NULL
)
76 //printError(ErrBadCall, "param values not set");
79 AllDataType::copyVal(value
->value
, paramValues
[i
], value
->type
, value
->length
);
81 rv
= table
->execute();
85 DbRetVal
SelStatement::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
SelStatement::setShortParam(int paramNo
, short value
)
95 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
96 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
99 printError(ErrSysFatal
, "field value is null. Should never happen");
102 *(short*)cValue
->value
= value
;
106 DbRetVal
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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
SelStatement::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 DbRetVal
SelStatement::setTimeStampParam(int paramNo
, TimeStamp value
)
217 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
218 ConditionValue
*cValue
= (ConditionValue
*) params
[paramNo
-1];
221 printError(ErrSysFatal
, "condition value is null. Should never happen");
224 *(TimeStamp
*)cValue
->value
= value
;
228 DbRetVal
SelStatement::setBindField(int colNo
, void *value
)
230 if (colNo
<=0) return ErrBadArg
;
231 //TODO: check the upper limit
232 //if (colNo > table->getFieldNameList().size()) return ErrBadArg;
233 if (NULL
== value
) return ErrBadArg
;
234 bindFieldValues
[colNo
-1] = (char*) value
;
238 DbRetVal
SelStatement::resolve()
240 if (dbMgr
== NULL
) return ErrNoConnection
;
241 //check whether the table exists
242 table
= dbMgr
->openTable(parsedData
->getTableName());
245 printError(ErrNotExists
, "Unable to open the table:Table not exists");
248 //get the fieldname list and validate field names
249 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
250 FieldName
*name
= NULL
;
251 FieldInfo
*fInfo
= new FieldInfo();
253 while (iter
.hasElement())
255 name
= (FieldName
*)iter
.nextElement();
258 dbMgr
->closeTable(table
);
261 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
264 if ('*' == name
->fldName
[0])
269 dbMgr
->closeTable(table
);
274 //as soon as it encounters *, it breaks the loop negleting other field names
275 //as they all are deleted during resolveStar method.
278 rv
= table
->getFieldInfo(name
->fldName
, fInfo
);
279 if (ErrNotFound
== rv
)
281 dbMgr
->closeTable(table
);
284 printError(ErrSyntaxError
, "Field %s does not exist in table",
286 return ErrSyntaxError
;
288 FieldValue
*newVal
= new FieldValue();
289 newVal
->parsedString
= NULL
;
291 newVal
->type
= fInfo
->type
;
292 newVal
->length
= fInfo
->length
;
293 newVal
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
294 parsedData
->insertFieldValue(newVal
);
295 table
->bindFld(name
->fldName
, newVal
->value
);
300 rv
= setBindFieldAndValues();
303 dbMgr
->closeTable(table
);
308 table
->setCondition(parsedData
->getCondition());
310 rv
= resolveForCondition();
313 //TODO::free memory allocated for params
314 table
->setCondition(NULL
);
315 dbMgr
->closeTable(table
);
320 DbRetVal
SelStatement::resolveStar()
323 parsedData
->clearFieldNameList();
324 List fNameList
= table
->getFieldNameList();
325 ListIterator fNameIter
= fNameList
.getIterator();
326 FieldValue
*newVal
= NULL
;
327 //fNameList.resetIter(); //do not remove this.
328 FieldInfo
*fInfo
= new FieldInfo();
329 for (int i
= 0; i
< fNameList
.size() ; i
++)
331 char *fName
= ((Identifier
*)(fNameIter
.nextElement()))->name
;
332 rv
= table
->getFieldInfo(fName
, fInfo
);
333 if (ErrNotFound
== rv
)
337 printError(ErrSysFatal
, "Should never happen.");
340 newVal
= new FieldValue();
341 newVal
->parsedString
= NULL
;
343 newVal
->type
= fInfo
->type
;
344 newVal
->length
= fInfo
->length
;
345 newVal
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
346 parsedData
->insertFieldValue(newVal
);
347 parsedData
->insertField(fName
);
348 table
->bindFld(fName
, newVal
->value
);
354 DbRetVal
SelStatement::setBindFieldAndValues()
356 totalFields
= parsedData
->getFieldNameList().size();
357 bindFields
= (FieldValue
**) malloc ( totalFields
* sizeof(FieldValue
*));
358 bindFieldValues
= (char**) malloc( totalFields
* sizeof(char*));
359 memset(bindFields
, 0, totalFields
* sizeof(FieldValue
*));
360 memset(bindFieldValues
, 0, totalFields
* sizeof(char*));
361 ListIterator valIter
= parsedData
->getFieldValueList().getIterator();
363 FieldValue
*value
= NULL
;
365 while(valIter
.hasElement())
367 value
= (FieldValue
*) valIter
.nextElement();
370 free(bindFields
); bindFields
= NULL
;
371 free(bindFieldValues
); bindFieldValues
= NULL
;
372 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
375 bindFields
[colNo
++ ] = value
;
381 DbRetVal
SelStatement::resolveForCondition()
383 //get the fieldname list and validate field names
384 ListIterator iter
= parsedData
->getConditionValueList().getIterator();
386 ConditionValue
*value
;
387 FieldInfo
*fInfo
= new FieldInfo();
390 while (iter
.hasElement())
392 value
= (ConditionValue
*) iter
.nextElement();
396 printError(ErrSysFatal
, "Should never happen.");
399 rv
= table
->getFieldInfo(value
->fName
, fInfo
);
400 if (ErrNotFound
== rv
)
403 printError(ErrSyntaxError
, "Field %s does not exist in table",
405 return ErrSyntaxError
;
407 value
->type
= fInfo
->type
;
408 value
->length
= fInfo
->length
;
409 value
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
410 //table->bindFld(name->fldName, value->value);
411 if (value
->parsedString
== NULL
)
414 printError(ErrSyntaxError
, "Condition value should not be NULL");
415 return ErrSyntaxError
;
417 if (value
->parsedString
[0] == '?')
419 value
->paramNo
= paramPos
++;
422 AllDataType::strToValue(value
->value
, value
->parsedString
, fInfo
->type
);
425 totalParams
= paramPos
-1;
426 if (0 == totalParams
) return OK
;
427 params
= (void**) malloc ( totalParams
* sizeof(FieldValue
*));
428 paramValues
= (char**) malloc( totalParams
* sizeof(char*));
429 memset(params
, 0, totalParams
* sizeof(FieldValue
*));
430 memset(paramValues
, 0, totalParams
* sizeof(char*));
432 while(iter
.hasElement())
434 value
= (ConditionValue
*) iter
.nextElement();
437 free(params
); params
= NULL
;
438 free(paramValues
); paramValues
= NULL
;
439 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
442 params
[value
->paramNo
-1 ] = value
;
447 void* SelStatement::fetch()
449 void *tuple
= table
->fetch();
450 if (NULL
== tuple
) return NULL
;
451 //copy values to binded buffer
453 for (int i
= 0; i
< totalFields
; i
++)
455 value
= bindFields
[i
];
456 if (bindFieldValues
[i
] == NULL
)
458 printError(ErrBadCall
, "Fields are not binded properly. Should never happen");
461 AllDataType::copyVal(bindFieldValues
[i
], value
->value
, value
->type
, value
->length
);
466 DbRetVal
SelStatement::close()
468 return table
->close();
471 void* SelStatement::fetchAndPrint()
473 void *tuple
= table
->fetch();
474 if (NULL
== tuple
) return NULL
;
477 for (int i
= 0; i
< totalFields
; i
++)
479 value
= bindFields
[i
];
480 nullValueSet
= table
->isFldNull(i
+1);
484 AllDataType::printVal(value
->value
, value
->type
, value
->length
);
489 void* SelStatement::next()
491 return( table
->fetch() );
494 void* SelStatement::getFieldValuePtr( int pos
)
496 FieldValue
*v
= bindFields
[pos
];
497 return ( (void*) v
->value
);
500 int SelStatement::noOfProjFields()
505 DbRetVal
SelStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
507 //TODO::if not yet prepared return error
508 //TODO::check the upper limit for projpos
509 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
510 FieldName
*name
= NULL
;
513 while (iter
.hasElement())
515 name
= (FieldName
*)iter
.nextElement();
518 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
521 if (position
== projpos
) break;
525 rv
= table
->getFieldInfo(name
->fldName
, fInfo
);