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])
267 while (iter
.hasElement())
268 delete (FieldName
*) iter
.nextElement();
272 dbMgr
->closeTable(table
);
277 //as soon as it encounters *, it breaks the loop negleting other field names
278 //as they all are deleted during resolveStar method.
281 rv
= table
->getFieldInfo(name
->fldName
, fInfo
);
282 if (ErrNotFound
== rv
)
284 dbMgr
->closeTable(table
);
287 printError(ErrSyntaxError
, "Field %s does not exist in table",
289 return ErrSyntaxError
;
291 FieldValue
*newVal
= new FieldValue();
292 newVal
->parsedString
= NULL
;
294 newVal
->type
= fInfo
->type
;
295 newVal
->length
= fInfo
->length
;
296 newVal
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
297 parsedData
->insertFieldValue(newVal
);
298 table
->bindFld(name
->fldName
, newVal
->value
);
303 rv
= setBindFieldAndValues();
306 dbMgr
->closeTable(table
);
311 table
->setCondition(parsedData
->getCondition());
313 rv
= resolveForCondition();
316 //TODO::free memory allocated for params
317 table
->setCondition(NULL
);
318 dbMgr
->closeTable(table
);
323 DbRetVal
SelStatement::resolveStar()
326 parsedData
->clearFieldNameList();
327 List fNameList
= table
->getFieldNameList();
328 ListIterator fNameIter
= fNameList
.getIterator();
329 FieldValue
*newVal
= NULL
;
330 //fNameList.resetIter(); //do not remove this.
331 FieldInfo
*fInfo
= new FieldInfo();
332 for (int i
= 0; i
< fNameList
.size() ; i
++)
334 char *fName
= ((Identifier
*)(fNameIter
.nextElement()))->name
;
335 rv
= table
->getFieldInfo(fName
, fInfo
);
336 if (ErrNotFound
== rv
)
340 printError(ErrSysFatal
, "Should never happen.");
343 newVal
= new FieldValue();
344 newVal
->parsedString
= NULL
;
346 newVal
->type
= fInfo
->type
;
347 newVal
->length
= fInfo
->length
;
348 newVal
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
349 parsedData
->insertFieldValue(newVal
);
350 parsedData
->insertField(fName
);
351 table
->bindFld(fName
, newVal
->value
);
354 while (fNameIter
.hasElement())
355 delete (Identifier
*) fNameIter
.nextElement();
361 DbRetVal
SelStatement::setBindFieldAndValues()
363 totalFields
= parsedData
->getFieldNameList().size();
364 bindFields
= (FieldValue
**) malloc ( totalFields
* sizeof(FieldValue
*));
365 bindFieldValues
= (char**) malloc( totalFields
* sizeof(char*));
366 memset(bindFields
, 0, totalFields
* sizeof(FieldValue
*));
367 memset(bindFieldValues
, 0, totalFields
* sizeof(char*));
368 ListIterator valIter
= parsedData
->getFieldValueList().getIterator();
370 FieldValue
*value
= NULL
;
372 while(valIter
.hasElement())
374 value
= (FieldValue
*) valIter
.nextElement();
377 free(bindFields
); bindFields
= NULL
;
378 free(bindFieldValues
); bindFieldValues
= NULL
;
379 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
382 bindFields
[colNo
++ ] = value
;
388 DbRetVal
SelStatement::resolveForCondition()
390 //get the fieldname list and validate field names
391 ListIterator iter
= parsedData
->getConditionValueList().getIterator();
393 ConditionValue
*value
;
394 FieldInfo
*fInfo
= new FieldInfo();
397 while (iter
.hasElement())
399 value
= (ConditionValue
*) iter
.nextElement();
403 printError(ErrSysFatal
, "Should never happen.");
406 rv
= table
->getFieldInfo(value
->fName
, fInfo
);
407 if (ErrNotFound
== rv
)
410 printError(ErrSyntaxError
, "Field %s does not exist in table",
412 return ErrSyntaxError
;
414 value
->type
= fInfo
->type
;
415 value
->length
= fInfo
->length
;
416 value
->value
= AllDataType::alloc(fInfo
->type
, fInfo
->length
);
417 //table->bindFld(name->fldName, value->value);
418 if (value
->parsedString
== NULL
)
421 printError(ErrSyntaxError
, "Condition value should not be NULL");
422 return ErrSyntaxError
;
424 if (value
->parsedString
[0] == '?')
426 value
->paramNo
= paramPos
++;
429 AllDataType::strToValue(value
->value
, value
->parsedString
, fInfo
->type
);
432 totalParams
= paramPos
-1;
433 if (0 == totalParams
) return OK
;
434 params
= (void**) malloc ( totalParams
* sizeof(FieldValue
*));
435 paramValues
= (char**) malloc( totalParams
* sizeof(char*));
436 memset(params
, 0, totalParams
* sizeof(FieldValue
*));
437 memset(paramValues
, 0, totalParams
* sizeof(char*));
439 while(iter
.hasElement())
441 value
= (ConditionValue
*) iter
.nextElement();
444 free(params
); params
= NULL
;
445 free(paramValues
); paramValues
= NULL
;
446 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
449 params
[value
->paramNo
-1 ] = value
;
454 void* SelStatement::fetch()
456 void *tuple
= table
->fetch();
457 if (NULL
== tuple
) return NULL
;
458 //copy values to binded buffer
460 for (int i
= 0; i
< totalFields
; i
++)
462 value
= bindFields
[i
];
463 if (bindFieldValues
[i
] == NULL
)
465 printError(ErrBadCall
, "Fields are not binded properly. Should never happen");
468 AllDataType::copyVal(bindFieldValues
[i
], value
->value
, value
->type
, value
->length
);
473 void* SelStatement::fetch(DbRetVal
&rv
)
475 void *tuple
= table
->fetch(rv
);
476 if (NULL
== tuple
) return NULL
;
477 //copy values to binded buffer
479 for (int i
= 0; i
< totalFields
; i
++)
481 value
= bindFields
[i
];
482 if (bindFieldValues
[i
] == NULL
)
484 printError(ErrBadCall
, "Fields are not binded properly. Should never happen");
487 AllDataType::copyVal(bindFieldValues
[i
], value
->value
, value
->type
, value
->length
);
492 DbRetVal
SelStatement::close()
494 return table
->close();
496 void* SelStatement::getParamValuePtr( int pos
)
498 ConditionValue
*p
= (ConditionValue
*) params
[pos
-1];
499 return ( (void*) p
->value
);
502 char* SelStatement::getFieldName ( int pos
)
504 //TODO::if not yet prepared return error
505 //TODO::check the upper limit for projpos
506 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
508 while (iter
.hasElement())
510 if (position
== pos
) {
511 FieldName
*name
= (FieldName
*) iter
.nextElement();
514 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
517 return name
->fldName
;
524 DataType
SelStatement::getFieldType( int pos
)
526 FieldValue
*v
= bindFields
[pos
];
527 return ( (DataType
) v
->type
);
530 int SelStatement::getFieldLength( int pos
)
532 FieldValue
*v
= bindFields
[pos
];
533 return ( (int) v
->type
);
536 void* SelStatement::fetchAndPrint(bool SQL
)
538 void *tuple
= table
->fetch();
539 if (NULL
== tuple
) return NULL
;
544 sprintf(stmt
, "INSERT INTO %s VALUES(", table
->getName());
547 for (int i
= 0; i
< totalFields
; i
++)
549 value
= bindFields
[i
];
550 nullValueSet
= table
->isFldNull(i
+1);
558 else printf("NULL\t");
581 AllDataType::printVal(value
->value
, value
->type
, value
->length
);
594 if (SQL
) printf(");\n");
598 void* SelStatement::next()
600 return( table
->fetch() );
603 void* SelStatement::getFieldValuePtr( int pos
)
605 FieldValue
*v
= bindFields
[pos
];
606 return ( (void*) v
->value
);
609 int SelStatement::noOfProjFields()
614 DbRetVal
SelStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
616 //TODO::if not yet prepared return error
617 //TODO::check the upper limit for projpos
618 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
619 FieldName
*name
= NULL
;
622 while (iter
.hasElement())
624 name
= (FieldName
*)iter
.nextElement();
627 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
630 if (position
== projpos
) break;
634 rv
= table
->getFieldInfo(name
->fldName
, fInfo
);