destructor is made to call table close which will delete the table handle
[csql.git] / src / sql / SelStatement.cxx
blobe7482db8ef49229b6008dbce2130e5b3d9f3721a
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include "Statement.h"
17 #include <TableImpl.h>
19 SelStatement::SelStatement()
21 parsedData = NULL;
22 dbMgr = NULL;
23 table = NULL;
24 params = NULL;
25 paramValues = NULL;
26 totalParams = 0;
27 bindFields = NULL;
28 bindFieldValues = NULL;
29 totalFields = 0;
30 isPointReturned = false;
31 handleAggWithTbl=false;
34 SelStatement::~SelStatement()
36 if (table) { table->close(); table = NULL; }
37 if (totalParams) {
38 free(params);
39 params = NULL;
40 free(paramValues);
41 paramValues = NULL;
43 if (totalFields)
45 free(bindFields);
46 bindFields = NULL;
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];
56 if (NULL == cValue)
58 printError(ErrSysFatal, "condition value is null. Should never happen");
59 return ErrSysFatal;
61 table->getFieldNameAlone(cValue->fName,info->fldName);
62 info->type = cValue->type;
63 info->length = cValue->length;
64 info->isNull = cValue->isNullable;
65 return OK;
67 DbRetVal SelStatement::execute(int &rowsAffected)
69 DbRetVal rv = OK;
70 //copy param values to binded buffer
71 ConditionValue *value;
72 for (int i = 0; i < totalParams; i ++)
74 value = (ConditionValue*) params[i];
75 if (paramValues[i] == NULL)
77 continue;
78 //printError(ErrBadCall, "param values not set");
79 //return ErrBadCall;
81 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
84 rv = table->execute();
85 //table->printPlan(0);
86 return rv;
89 DbRetVal SelStatement::setParam(int paramNo, void *value)
91 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
92 if (NULL == value) return ErrBadArg;
93 paramValues[paramNo -1] = (char*) value;
94 return OK;
97 DbRetVal SelStatement::setShortParam(int paramNo, short value)
99 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
100 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
101 if (NULL == cValue)
103 printError(ErrSysFatal, "field value is null. Should never happen");
104 return ErrSysFatal;
106 *(short*)cValue->value = value;
107 return OK;
110 DbRetVal SelStatement::setIntParam(int paramNo, int value)
112 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
113 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
114 if (NULL == cValue)
116 printError(ErrSysFatal, "condition value is null. Should never happen");
117 return ErrSysFatal;
119 *(int*)cValue->value = value;
120 return OK;
122 DbRetVal SelStatement::setLongParam(int paramNo, long value)
124 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
125 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
126 if (NULL == cValue)
128 printError(ErrSysFatal, "condition value is null. Should never happen");
129 return ErrSysFatal;
131 *(long*)cValue->value = value;
132 return OK;
135 DbRetVal SelStatement::setLongLongParam(int paramNo, long long value)
137 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
138 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
139 if (NULL == cValue)
141 printError(ErrSysFatal, "condition value is null. Should never happen");
142 return ErrSysFatal;
144 *(long long*)cValue->value = value;
145 return OK;
147 DbRetVal SelStatement::setByteIntParam(int paramNo, ByteInt value)
149 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
150 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
151 if (NULL == cValue)
153 printError(ErrSysFatal, "condition value is null. Should never happen");
154 return ErrSysFatal;
156 *(ByteInt*)cValue->value = value;
157 return OK;
159 DbRetVal SelStatement::setFloatParam(int paramNo, float value)
161 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
162 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
163 if (NULL == cValue)
165 printError(ErrSysFatal, "condition value is null. Should never happen");
166 return ErrSysFatal;
168 *(float*)cValue->value = value;
169 return OK;
171 DbRetVal SelStatement::setDoubleParam(int paramNo, double value)
173 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
174 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
175 if (NULL == cValue)
177 printError(ErrSysFatal, "condition value is null. Should never happen");
178 return ErrSysFatal;
180 *(double*)cValue->value = value;
181 return OK;
183 DbRetVal SelStatement::setStringParam(int paramNo, char *value)
185 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
186 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
187 if (NULL == cValue)
189 printError(ErrSysFatal, "condition value is null. Should never happen");
190 return ErrSysFatal;
192 strcpy((char*)cValue->value, value);
193 return OK;
195 DbRetVal SelStatement::setDateParam(int paramNo, Date value)
197 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
198 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
199 if (NULL == cValue)
201 printError(ErrSysFatal, "condition value is null. Should never happen");
202 return ErrSysFatal;
204 *(Date*)cValue->value = value;
205 return OK;
207 DbRetVal SelStatement::setTimeParam(int paramNo, Time value)
209 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
210 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
211 if (NULL == cValue)
213 printError(ErrSysFatal, "condition value is null. Should never happen");
214 return ErrSysFatal;
216 *(Time*)cValue->value = value;
217 return OK;
219 DbRetVal SelStatement::setTimeStampParam(int paramNo, TimeStamp value)
221 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
222 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
223 if (NULL == cValue)
225 printError(ErrSysFatal, "condition value is null. Should never happen");
226 return ErrSysFatal;
228 *(TimeStamp*)cValue->value = value;
229 return OK;
232 DbRetVal SelStatement::setBinaryParam(int paramNo, void *value)
234 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
235 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
236 if (NULL == cValue)
238 printError(ErrSysFatal, "condition value is null. Should never happen");
239 return ErrSysFatal;
241 AllDataType::convertToBinary(cValue->value,value,typeString,cValue->length);
242 return OK;
245 DbRetVal SelStatement::setBindField(int colNo, void *value)
247 if (colNo <=0) return ErrBadArg;
248 //TODO: check the upper limit
249 //if (colNo > table->getFieldNameList().size()) return ErrBadArg;
250 if (NULL == value) return ErrBadArg;
251 bindFieldValues[colNo -1] = (char*) value;
252 return OK;
254 DbRetVal SelStatement::openTables()
256 if (dbMgr == NULL) return ErrNoConnection;
257 JoinTableImpl *jHdl = NULL;
258 Table *tHdl = NULL, *prevHdl = NULL;
259 bool joinInvolved = false;
260 //check whether all the table exists
261 ListIterator titer = parsedData->getTableNameList().getIterator();
262 while (titer.hasElement())
264 TableName *t = (TableName*)titer.nextElement();
265 tHdl = dbMgr->openTable(t->tblName);
266 if ( NULL == tHdl )
268 printError(ErrNotExists,
269 "Unable to open the table:Table not exists");
270 return ErrNotExists;
272 if (NULL != prevHdl)
274 joinInvolved = true;
275 jHdl = new JoinTableImpl();
276 jHdl->setTable(prevHdl, tHdl);
277 prevHdl = jHdl;
278 continue;
280 prevHdl = tHdl;
282 if (joinInvolved) table = jHdl; else table = tHdl;
283 return OK;
285 DbRetVal SelStatement::resolve()
287 DbRetVal rv = openTables();
288 if (rv != OK) return rv;
289 //get the fieldname list and validate field names
290 ListIterator iter = parsedData->getFieldNameList().getIterator();
291 FieldName *name = NULL;
292 FieldInfo *fInfo = new FieldInfo();
293 List bindFldList;
294 bool isSingleTableNoGrp = false;
295 if(parsedData->getTableNameList().size() == 1 &&
296 parsedData->getGroupFieldNameList().size() == 0)
298 isSingleTableNoGrp = true;
300 AggTableImpl *aggTable = NULL;
301 while (iter.hasElement())
303 name = (FieldName*)iter.nextElement();
304 if (NULL == name)
306 dbMgr->closeTable(table);
307 table = NULL;
308 delete fInfo;
309 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
310 return ErrSysFatal;
312 bool isBindFld=false;
313 if ('*' == name->fldName[0] && name->aType == AGG_UNKNOWN)
315 rv = resolveStar();
316 if (rv != OK)
318 dbMgr->closeTable(table);
319 table = NULL;
320 delete fInfo;
321 return rv;
323 if (parsedData->getGroupFieldNameList().size()!= 0
324 && !isSingleTableNoGrp)
326 if (!aggTable)
327 aggTable = new AggTableImpl();
328 aggTable->setTable(table);
330 //as soon as it encounters *, it breaks the loop negleting other field names
331 //as they all are deleted during resolveStar method.
332 break;
333 }else {
334 if ('*' == name->fldName[0] && name->aType != AGG_COUNT) {return ErrSyntaxError;}
335 rv = table->getFieldInfo(name->fldName, fInfo);
336 if (ErrNotFound == rv || ErrNotExists == rv)
338 dbMgr->closeTable(table);
339 table = NULL;
340 delete fInfo;
341 printError(ErrSyntaxError, "Field %s does not exist in table",
342 name->fldName);
343 return ErrSyntaxError;
345 FieldValue *newVal = new FieldValue();
346 strcpy(newVal->fldName,name->fldName);
347 newVal->parsedString = NULL;
348 newVal->paramNo = 0;
349 newVal->type = fInfo->type;
350 newVal->length = fInfo->length;
351 newVal->isNullable = fInfo->isNull;
352 FieldName *bFldName=NULL;
353 ListIterator it = bindFldList.getIterator();
354 while (it.hasElement())
356 bFldName = (FieldName*)it.nextElement();
357 if(0==strcmp(bFldName->fldName,name->fldName) &&
358 name->aType == AGG_UNKNOWN)
360 newVal->value=table->getBindFldAddr(name->fldName);
361 newVal->isAllocVal=false;
362 isBindFld=true;
363 break;
366 if (!isBindFld) {
367 if(newVal->type == typeBinary)
368 newVal->value = AllDataType::alloc(fInfo->type,
369 2 * fInfo->length);
370 else newVal->value = AllDataType::alloc(fInfo->type,
371 fInfo->length);
372 newVal->isAllocVal=true;
374 if (name->aType ==AGG_UNKNOWN &&
375 parsedData->getGroupFieldNameList().size()== 0)
376 table->bindFld(name->fldName, newVal->value);
377 else if (!isSingleTableNoGrp)
379 if (!aggTable) {
380 aggTable = new AggTableImpl();
381 aggTable->setTable(table);
383 aggTable->bindFld(name->fldName, name->aType, newVal->value);
385 if (name->aType !=AGG_UNKNOWN && isSingleTableNoGrp)
386 handleAggWithTbl= true;
387 parsedData->insertFieldValue(newVal);
389 if (!isBindFld) bindFldList.append(name);
391 bindFldList.reset();
392 rv = setBindFieldAndValues();
393 if (rv != OK)
395 delete fInfo;
396 dbMgr->closeTable(table);
397 table = NULL;
398 return rv;
401 table->setCondition(parsedData->getCondition());
403 rv = resolveForCondition();
404 if (rv != OK)
406 delete fInfo;
407 //TODO::free memory allocated for params
408 table->setCondition(NULL);
409 dbMgr->closeTable(table);
410 table = NULL;
411 return rv;
413 rv = resolveGroupFld(aggTable);
414 if (rv != OK)
416 delete fInfo;
417 //TODO::free memory allocated for params
418 if (table)
420 table->setCondition(NULL);
421 dbMgr->closeTable(table);
423 table = NULL;
424 return rv;
426 delete fInfo;
427 return rv;
429 DbRetVal SelStatement::resolveGroupFld(AggTableImpl *aggTable)
431 if (!aggTable) {
432 return OK;
434 ListIterator giter = parsedData->getGroupFieldNameList().getIterator();
435 FieldName *name = NULL;
436 DbRetVal rv = OK;
437 FieldInfo *fInfo = new FieldInfo();
438 if (giter.hasElement())
440 name = (FieldName*)giter.nextElement();
441 rv = table->getFieldInfo(name->fldName, fInfo);
442 if (ErrNotFound == rv || ErrNotExists == rv)
444 dbMgr->closeTable(table);
445 table = NULL;
446 delete fInfo;
447 delete aggTable;
448 printError(ErrSyntaxError, "Field %s does not exist in table",
449 name->fldName);
450 return ErrSyntaxError;
452 FieldValue *newVal = new FieldValue();
453 strcpy(newVal->fldName,name->fldName);
454 newVal->parsedString = NULL;
455 newVal->paramNo = 0;
456 newVal->type = fInfo->type;
457 newVal->isNullable = fInfo->isNull;
458 newVal->length = fInfo->length;
459 if (newVal->type == typeBinary)
460 newVal->value = AllDataType::alloc(fInfo->type, 2 * fInfo->length);
461 else newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
462 newVal->isAllocVal=true;
463 parsedData->insertFieldValue(newVal);
464 aggTable->setGroup(name->fldName, newVal->value);
466 delete fInfo;
467 if (giter.hasElement())
469 table= aggTable;
470 printError(ErrSyntaxError, "Only one field allowed in group\n");
471 return ErrSyntaxError;
473 table = aggTable;
474 return OK;
476 DbRetVal SelStatement::resolveStar()
478 DbRetVal rv = OK;
479 parsedData->clearFieldNameList();
481 List fNameList = table->getFieldNameList();
482 ListIterator fNameIter = fNameList.getIterator();
483 FieldValue *newVal = NULL;
484 //fNameList.resetIter(); //do not remove this.
485 FieldInfo *fInfo = new FieldInfo();
486 for (int i = 0; i < fNameList.size() ; i++)
488 char *fName = ((Identifier*)(fNameIter.nextElement()))->name;
489 rv = table->getFieldInfo(fName, fInfo);
490 if (ErrNotFound == rv || ErrNotExists == rv)
492 delete fInfo;
493 fNameList.reset();
494 printError(ErrSysFatal, "Should never happen.");
495 return ErrSysFatal;
497 newVal = new FieldValue();
498 strcpy(newVal->fldName,fName);
499 newVal->parsedString = NULL;
500 newVal->paramNo = 0;
501 newVal->type = fInfo->type;
502 newVal->length = fInfo->length;
503 // for binary datatype input buffer size should be 2 times the length
504 if(newVal->type == typeBinary)
505 newVal->value = AllDataType::alloc(fInfo->type, 2 * fInfo->length);
506 else newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
507 newVal->isAllocVal=true;
508 parsedData->insertFieldValue(newVal);
509 parsedData->insertField(fName);
510 table->bindFld(fName, newVal->value);
512 fNameIter.reset();
513 while (fNameIter.hasElement())
514 delete (Identifier *) fNameIter.nextElement();
515 fNameList.reset();
516 delete fInfo;
517 return OK;
520 DbRetVal SelStatement::setBindFieldAndValues()
522 totalFields = parsedData->getFieldNameList().size();
523 bindFields = (FieldValue**) malloc ( totalFields * sizeof(FieldValue*));
524 bindFieldValues = (char**) malloc( totalFields * sizeof(char*));
525 memset(bindFields, 0, totalFields * sizeof(FieldValue*));
526 memset(bindFieldValues, 0, totalFields * sizeof(char*));
527 ListIterator valIter = parsedData->getFieldValueList().getIterator();
528 int colNo =0;
529 FieldValue *value = NULL;
530 valIter.reset();
531 while(valIter.hasElement())
533 value = (FieldValue*) valIter.nextElement();
534 if (value == NULL)
536 free(bindFields); bindFields = NULL;
537 free(bindFieldValues); bindFieldValues = NULL;
538 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
539 return ErrSysFatal;
541 bindFields[colNo++ ] = value;
543 return OK;
547 DbRetVal SelStatement::resolveForCondition()
549 //get the fieldname list and validate field names
550 ListIterator iter = parsedData->getConditionValueList().getIterator();
552 ConditionValue *value;
553 FieldInfo *fInfo = new FieldInfo();
554 int paramPos =1;
555 DbRetVal rv = OK;
556 while (iter.hasElement())
558 value = (ConditionValue*) iter.nextElement();
559 if (NULL == value)
561 delete fInfo;
562 printError(ErrSysFatal, "Should never happen.");
563 return ErrSysFatal;
565 rv = table->getFieldInfo(value->fName, fInfo);
566 if (ErrNotFound == rv || ErrNotExists == rv)
568 delete fInfo;
569 printError(ErrSyntaxError, "Field %s does not exist in table",
570 value->fName);
571 return ErrSyntaxError;
573 value->type = fInfo->type;
574 value->length = fInfo->length;
575 value->isNullable = fInfo->isNull;
576 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
577 //table->bindFld(name->fldName, value->value);
578 if (value->parsedString == NULL)
580 delete fInfo;
581 printError(ErrSyntaxError, "Condition value should not be NULL");
582 return ErrSyntaxError;
584 if (value->parsedString[0] == '?')
586 if(!value->opLike) // checks if 'LIKE' operator is used
587 value->paramNo = paramPos++;
589 if (!value->paramNo) {
590 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
591 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
594 delete fInfo;
595 totalParams = paramPos -1;
596 if (0 == totalParams) return OK;
597 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
598 paramValues = (char**) malloc( totalParams * sizeof(char*));
599 memset(params, 0, totalParams * sizeof(FieldValue*));
600 memset(paramValues, 0, totalParams * sizeof(char*));
601 iter.reset();
602 while(iter.hasElement())
604 value = (ConditionValue*) iter.nextElement();
605 if (value == NULL)
607 free(params); params = NULL;
608 free(paramValues); paramValues = NULL;
609 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
610 return ErrSysFatal;
612 params[value->paramNo -1 ] = value;
614 return OK;
616 void* SelStatement::handleSingleTableAggWithoutGroup()
618 if (isPointReturned) return NULL;
619 TableImpl *tblImpl = (TableImpl*)table;
620 ListIterator iter = parsedData->getFieldNameList().getIterator();
621 int i=0;
622 DbRetVal rv = OK;
623 FieldName *name;
624 FieldValue *fVal = NULL;
625 while (iter.hasElement())
627 name = (FieldName*) iter.nextElement();
628 fVal = bindFields[i];
630 //rv = tblImpl->fetchAgg(name, (int) name->aType, fVal->value);
631 rv = tblImpl->fetchAgg(name->fldName, name->aType, fVal->value);
632 if (OK != rv) return NULL;
633 i++;
634 tblImpl->closeScan();
635 tblImpl->execute();
637 isPointReturned = true;
638 return fVal;
640 void* SelStatement::fetch()
642 if(handleAggWithTbl)
644 return handleSingleTableAggWithoutGroup();
646 void *tuple = table->fetch();
647 if (NULL == tuple) return NULL;
648 //copy values to binded buffer
649 FieldValue *value;
650 for (int i = 0; i < totalFields; i++)
652 value = bindFields[i];
653 if (bindFieldValues[i] == NULL)
655 printError(ErrBadCall, "Fields are not binded properly. Should never happen");
656 return NULL;
658 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
660 return tuple;
663 void* SelStatement::fetch(DbRetVal &rv)
665 if(handleAggWithTbl)
667 return handleSingleTableAggWithoutGroup();
669 void *tuple = table->fetch(rv);
670 if (NULL == tuple) return NULL;
671 //copy values to binded buffer
672 FieldValue *value;
673 for (int i = 0; i < totalFields; i++)
675 value = bindFields[i];
676 if (bindFieldValues[i] == NULL)
678 printError(ErrBadCall, "Fields are not binded properly. Should never happen %d", i);
679 return NULL;
681 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
683 return tuple;
686 DbRetVal SelStatement::close()
688 isPointReturned = false;
689 return table->closeScan();
692 DbRetVal SelStatement::freeScan()
694 if (table) return table->close();
695 else return OK;
698 void* SelStatement::getParamValuePtr( int pos )
700 ConditionValue *p = (ConditionValue*) params [pos-1];
701 return ( (void*) p->value );
704 char* SelStatement::getFieldName ( int pos )
706 //TODO::if not yet prepared return error
707 //TODO::check the upper limit for projpos
708 ListIterator iter = parsedData->getFieldNameList().getIterator();
709 int position =0;
710 while (iter.hasElement())
712 if (position == pos) {
713 FieldName *name = (FieldName*) iter.nextElement();
714 if (NULL == name)
716 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
717 return (char*) 0;
719 return name->fldName;
721 position++;
723 return (char*) 0;
726 DataType SelStatement::getFieldType( int pos )
728 FieldValue *v = bindFields[pos];
729 return ( (DataType) v->type );
732 int SelStatement::getFieldLength( int pos )
734 FieldValue *v = bindFields[pos];
735 return ( (int) v->type );
738 void* SelStatement::fetchAndPrint(bool SQL)
740 void *tuple = NULL;
741 if(handleAggWithTbl)
743 tuple = handleSingleTableAggWithoutGroup();
744 }else {
745 tuple = table->fetch();
747 if (NULL == tuple) return NULL;
748 FieldValue *value;
749 bool nullValueSet;
750 char stmt[128];
751 if (SQL) {
752 sprintf(stmt, "INSERT INTO %s VALUES(", table->getName());
753 printf("%s", stmt);
755 for (int i = 0; i < totalFields; i++)
757 value = bindFields[i];
758 nullValueSet = table->isFldNull(value->fldName);
759 if (nullValueSet)
760 if (SQL) {
761 if (i==0)
762 printf("NULL");
763 else
764 printf(", NULL");
766 else printf("NULL\t");
767 else {
768 if (SQL) {
769 switch(value->type)
771 case typeString:
772 case typeBinary:
773 case typeDate:
774 case typeTime:
775 case typeTimeStamp:
777 if (i==0)
778 printf(" '");
779 else
780 printf(", '");
781 break;
783 default:
785 if (i!=0)
786 printf(",");
790 AllDataType::printVal(value->value, value->type, value->length);
791 if (SQL) {
792 switch(value->type)
794 case typeString:
795 case typeBinary:
796 case typeDate:
797 case typeTime:
798 case typeTimeStamp:
799 printf("'");
801 } else printf("\t");
804 if (SQL) printf(");\n");
805 return tuple;
808 void* SelStatement::next()
810 if(handleAggWithTbl)
812 return handleSingleTableAggWithoutGroup();
814 return( table->fetch() );
818 void* SelStatement::getFieldValuePtr( int pos )
820 FieldValue *v = bindFields[pos];
821 return ( (void*) v->value );
824 int SelStatement::noOfProjFields()
826 return totalFields;
829 DbRetVal SelStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
831 //TODO::if not yet prepared return error
832 //TODO::check the upper limit for projpos
833 //TODO::validate if projpos is less than size of the list
834 ListIterator iter = parsedData->getFieldNameList().getIterator();
835 FieldName *name = NULL;
836 DbRetVal rv = OK;
837 int position =0;
838 while (iter.hasElement())
840 name = (FieldName*)iter.nextElement();
841 if (NULL == name)
843 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
844 return ErrSysFatal;
846 if (position == (projpos-1)) break;
847 position++;
850 rv = table->getFieldInfo(name->fldName, fInfo);
851 if (OK == rv)
853 //get back the qualified name(tablename.fldname)
854 char qualName[IDENTIFIER_LENGTH];
855 strcpy(qualName, name->fldName);
856 switch(name->aType)
858 case AGG_COUNT:
859 sprintf(fInfo->fldName, "COUNT(%s)", qualName);
860 break;
861 case AGG_MIN:
862 sprintf(fInfo->fldName, "MIN(%s)", qualName);
863 break;
864 case AGG_MAX:
865 sprintf(fInfo->fldName, "MAX(%s)", qualName);
866 break;
867 case AGG_SUM:
868 sprintf(fInfo->fldName, "SUM(%s)", qualName);
869 break;
870 case AGG_AVG:
871 sprintf(fInfo->fldName, "AVG(%s)", qualName);
872 break;
873 default:
874 strcpy(fInfo->fldName, qualName);
875 break;
878 return rv;
880 int SelStatement::getFldPos(char *name)
882 return table->getFldPos(name);