freeScan function removed as it is not required since table closeScan will delete...
[csql.git] / src / sql / SelStatement.cxx
blob8332a46ebcc0be0f4841dc1d26f95d1732d76818
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 close();
37 if (table) { table->close(); table = NULL; }
38 if (totalParams) {
39 free(params);
40 params = NULL;
41 free(paramValues);
42 paramValues = NULL;
44 if (totalFields)
46 free(bindFields);
47 bindFields = NULL;
48 free(bindFieldValues);
49 bindFieldValues = NULL;
53 DbRetVal SelStatement::getParamFldInfo(int paramNo, FieldInfo *&info)
55 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
56 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
57 if (NULL == cValue)
59 printError(ErrSysFatal, "condition value is null. Should never happen");
60 return ErrSysFatal;
62 table->getFieldNameAlone(cValue->fName,info->fldName);
63 info->type = cValue->type;
64 info->length = cValue->length;
65 info->isNull = cValue->isNullable;
66 return OK;
68 DbRetVal SelStatement::execute(int &rowsAffected)
70 DbRetVal rv = OK;
71 //copy param values to binded buffer
72 ConditionValue *value;
73 for (int i = 0; i < totalParams; i ++)
75 value = (ConditionValue*) params[i];
76 if (paramValues[i] == NULL)
78 continue;
79 //printError(ErrBadCall, "param values not set");
80 //return ErrBadCall;
82 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
85 rv = table->execute();
86 //table->printPlan(0);
87 return rv;
90 DbRetVal SelStatement::setParam(int paramNo, void *value)
92 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
93 if (NULL == value) return ErrBadArg;
94 paramValues[paramNo -1] = (char*) value;
95 return OK;
98 DbRetVal SelStatement::setShortParam(int paramNo, short value)
100 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
101 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
102 if (NULL == cValue)
104 printError(ErrSysFatal, "field value is null. Should never happen");
105 return ErrSysFatal;
107 *(short*)cValue->value = value;
108 return OK;
111 DbRetVal SelStatement::setIntParam(int paramNo, int value)
113 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
114 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
115 if (NULL == cValue)
117 printError(ErrSysFatal, "condition value is null. Should never happen");
118 return ErrSysFatal;
120 *(int*)cValue->value = value;
121 return OK;
123 DbRetVal SelStatement::setLongParam(int paramNo, long value)
125 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
126 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
127 if (NULL == cValue)
129 printError(ErrSysFatal, "condition value is null. Should never happen");
130 return ErrSysFatal;
132 *(long*)cValue->value = value;
133 return OK;
136 DbRetVal SelStatement::setLongLongParam(int paramNo, long long value)
138 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
139 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
140 if (NULL == cValue)
142 printError(ErrSysFatal, "condition value is null. Should never happen");
143 return ErrSysFatal;
145 *(long long*)cValue->value = value;
146 return OK;
148 DbRetVal SelStatement::setByteIntParam(int paramNo, ByteInt value)
150 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
151 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
152 if (NULL == cValue)
154 printError(ErrSysFatal, "condition value is null. Should never happen");
155 return ErrSysFatal;
157 *(ByteInt*)cValue->value = value;
158 return OK;
160 DbRetVal SelStatement::setFloatParam(int paramNo, float value)
162 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
163 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
164 if (NULL == cValue)
166 printError(ErrSysFatal, "condition value is null. Should never happen");
167 return ErrSysFatal;
169 *(float*)cValue->value = value;
170 return OK;
172 DbRetVal SelStatement::setDoubleParam(int paramNo, double value)
174 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
175 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
176 if (NULL == cValue)
178 printError(ErrSysFatal, "condition value is null. Should never happen");
179 return ErrSysFatal;
181 *(double*)cValue->value = value;
182 return OK;
184 DbRetVal SelStatement::setStringParam(int paramNo, char *value)
186 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
187 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
188 if (NULL == cValue)
190 printError(ErrSysFatal, "condition value is null. Should never happen");
191 return ErrSysFatal;
193 strcpy((char*)cValue->value, value);
194 return OK;
196 DbRetVal SelStatement::setDateParam(int paramNo, Date value)
198 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
199 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
200 if (NULL == cValue)
202 printError(ErrSysFatal, "condition value is null. Should never happen");
203 return ErrSysFatal;
205 *(Date*)cValue->value = value;
206 return OK;
208 DbRetVal SelStatement::setTimeParam(int paramNo, Time value)
210 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
211 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
212 if (NULL == cValue)
214 printError(ErrSysFatal, "condition value is null. Should never happen");
215 return ErrSysFatal;
217 *(Time*)cValue->value = value;
218 return OK;
220 DbRetVal SelStatement::setTimeStampParam(int paramNo, TimeStamp value)
222 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
223 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
224 if (NULL == cValue)
226 printError(ErrSysFatal, "condition value is null. Should never happen");
227 return ErrSysFatal;
229 *(TimeStamp*)cValue->value = value;
230 return OK;
233 DbRetVal SelStatement::setBinaryParam(int paramNo, void *value)
235 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
236 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
237 if (NULL == cValue)
239 printError(ErrSysFatal, "condition value is null. Should never happen");
240 return ErrSysFatal;
242 AllDataType::convertToBinary(cValue->value,value,typeString,cValue->length);
243 return OK;
246 DbRetVal SelStatement::setBindField(int colNo, void *value)
248 if (colNo <=0) return ErrBadArg;
249 //TODO: check the upper limit
250 //if (colNo > table->getFieldNameList().size()) return ErrBadArg;
251 if (NULL == value) return ErrBadArg;
252 bindFieldValues[colNo -1] = (char*) value;
253 return OK;
255 DbRetVal SelStatement::openTables()
257 if (dbMgr == NULL) return ErrNoConnection;
258 JoinTableImpl *jHdl = NULL;
259 Table *tHdl = NULL, *prevHdl = NULL;
260 bool joinInvolved = false;
261 //check whether all the table exists
262 ListIterator titer = parsedData->getTableNameList().getIterator();
263 while (titer.hasElement())
265 TableName *t = (TableName*)titer.nextElement();
266 tHdl = dbMgr->openTable(t->tblName);
267 if ( NULL == tHdl )
269 printError(ErrNotExists,
270 "Unable to open the table:Table not exists");
271 return ErrNotExists;
273 if (NULL != prevHdl)
275 joinInvolved = true;
276 jHdl = new JoinTableImpl();
277 jHdl->setTable(prevHdl, tHdl);
278 prevHdl = jHdl;
279 continue;
281 prevHdl = tHdl;
283 if (joinInvolved) table = jHdl; else table = tHdl;
284 return OK;
286 DbRetVal SelStatement::resolve()
288 DbRetVal rv = openTables();
289 if (rv != OK) return rv;
290 //get the fieldname list and validate field names
291 ListIterator iter = parsedData->getFieldNameList().getIterator();
292 FieldName *name = NULL;
293 FieldInfo *fInfo = new FieldInfo();
294 List bindFldList;
295 bool isSingleTableNoGrp = false;
296 if(parsedData->getTableNameList().size() == 1 &&
297 parsedData->getGroupFieldNameList().size() == 0)
299 isSingleTableNoGrp = true;
301 AggTableImpl *aggTable = NULL;
302 while (iter.hasElement())
304 name = (FieldName*)iter.nextElement();
305 if (NULL == name)
307 dbMgr->closeTable(table);
308 table = NULL;
309 delete fInfo;
310 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
311 return ErrSysFatal;
313 bool isBindFld=false;
314 if ('*' == name->fldName[0] && name->aType == AGG_UNKNOWN)
316 rv = resolveStar();
317 if (rv != OK)
319 dbMgr->closeTable(table);
320 table = NULL;
321 delete fInfo;
322 return rv;
324 if (parsedData->getGroupFieldNameList().size()!= 0
325 && !isSingleTableNoGrp)
327 if (!aggTable)
328 aggTable = new AggTableImpl();
329 aggTable->setTable(table);
331 //as soon as it encounters *, it breaks the loop negleting other field names
332 //as they all are deleted during resolveStar method.
333 break;
334 }else {
335 if ('*' == name->fldName[0] && name->aType != AGG_COUNT) {return ErrSyntaxError;}
336 rv = table->getFieldInfo(name->fldName, fInfo);
337 if (ErrNotFound == rv || ErrNotExists == rv)
339 dbMgr->closeTable(table);
340 table = NULL;
341 delete fInfo;
342 printError(ErrSyntaxError, "Field %s does not exist in table",
343 name->fldName);
344 return ErrSyntaxError;
346 FieldValue *newVal = new FieldValue();
347 strcpy(newVal->fldName,name->fldName);
348 newVal->parsedString = NULL;
349 newVal->paramNo = 0;
350 newVal->type = fInfo->type;
351 newVal->length = fInfo->length;
352 newVal->isNullable = fInfo->isNull;
353 FieldName *bFldName=NULL;
354 ListIterator it = bindFldList.getIterator();
355 while (it.hasElement())
357 bFldName = (FieldName*)it.nextElement();
358 if(0==strcmp(bFldName->fldName,name->fldName) &&
359 name->aType == AGG_UNKNOWN)
361 newVal->value=table->getBindFldAddr(name->fldName);
362 newVal->isAllocVal=false;
363 isBindFld=true;
364 break;
367 if (!isBindFld) {
368 if(newVal->type == typeBinary)
369 newVal->value = AllDataType::alloc(fInfo->type,
370 2 * fInfo->length);
371 else newVal->value = AllDataType::alloc(fInfo->type,
372 fInfo->length);
373 newVal->isAllocVal=true;
375 if (name->aType ==AGG_UNKNOWN &&
376 parsedData->getGroupFieldNameList().size()== 0)
377 table->bindFld(name->fldName, newVal->value);
378 else if (!isSingleTableNoGrp)
380 if (!aggTable) {
381 aggTable = new AggTableImpl();
382 aggTable->setTable(table);
384 aggTable->bindFld(name->fldName, name->aType, newVal->value);
386 if (name->aType !=AGG_UNKNOWN && isSingleTableNoGrp)
387 handleAggWithTbl= true;
388 parsedData->insertFieldValue(newVal);
390 if (!isBindFld) bindFldList.append(name);
392 bindFldList.reset();
393 rv = setBindFieldAndValues();
394 if (rv != OK)
396 delete fInfo;
397 dbMgr->closeTable(table);
398 table = NULL;
399 return rv;
402 table->setCondition(parsedData->getCondition());
404 rv = resolveForCondition();
405 if (rv != OK)
407 delete fInfo;
408 //TODO::free memory allocated for params
409 table->setCondition(NULL);
410 dbMgr->closeTable(table);
411 table = NULL;
412 return rv;
414 rv = resolveGroupFld(aggTable);
415 if (rv != OK)
417 delete fInfo;
418 //TODO::free memory allocated for params
419 if (table)
421 table->setCondition(NULL);
422 dbMgr->closeTable(table);
424 table = NULL;
425 return rv;
427 delete fInfo;
428 return rv;
430 DbRetVal SelStatement::resolveGroupFld(AggTableImpl *aggTable)
432 if (!aggTable) {
433 return OK;
435 ListIterator giter = parsedData->getGroupFieldNameList().getIterator();
436 FieldName *name = NULL;
437 DbRetVal rv = OK;
438 FieldInfo *fInfo = new FieldInfo();
439 if (giter.hasElement())
441 name = (FieldName*)giter.nextElement();
442 rv = table->getFieldInfo(name->fldName, fInfo);
443 if (ErrNotFound == rv || ErrNotExists == rv)
445 dbMgr->closeTable(table);
446 table = NULL;
447 delete fInfo;
448 delete aggTable;
449 printError(ErrSyntaxError, "Field %s does not exist in table",
450 name->fldName);
451 return ErrSyntaxError;
453 FieldValue *newVal = new FieldValue();
454 strcpy(newVal->fldName,name->fldName);
455 newVal->parsedString = NULL;
456 newVal->paramNo = 0;
457 newVal->type = fInfo->type;
458 newVal->isNullable = fInfo->isNull;
459 newVal->length = fInfo->length;
460 if (newVal->type == typeBinary)
461 newVal->value = AllDataType::alloc(fInfo->type, 2 * fInfo->length);
462 else newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
463 newVal->isAllocVal=true;
464 parsedData->insertFieldValue(newVal);
465 aggTable->setGroup(name->fldName, newVal->value);
467 delete fInfo;
468 if (giter.hasElement())
470 table= aggTable;
471 printError(ErrSyntaxError, "Only one field allowed in group\n");
472 return ErrSyntaxError;
474 table = aggTable;
475 return OK;
477 DbRetVal SelStatement::resolveStar()
479 DbRetVal rv = OK;
480 parsedData->clearFieldNameList();
482 List fNameList = table->getFieldNameList();
483 ListIterator fNameIter = fNameList.getIterator();
484 FieldValue *newVal = NULL;
485 //fNameList.resetIter(); //do not remove this.
486 FieldInfo *fInfo = new FieldInfo();
487 for (int i = 0; i < fNameList.size() ; i++)
489 char *fName = ((Identifier*)(fNameIter.nextElement()))->name;
490 rv = table->getFieldInfo(fName, fInfo);
491 if (ErrNotFound == rv || ErrNotExists == rv)
493 delete fInfo;
494 fNameList.reset();
495 printError(ErrSysFatal, "Should never happen.");
496 return ErrSysFatal;
498 newVal = new FieldValue();
499 strcpy(newVal->fldName,fName);
500 newVal->parsedString = NULL;
501 newVal->paramNo = 0;
502 newVal->type = fInfo->type;
503 newVal->length = fInfo->length;
504 // for binary datatype input buffer size should be 2 times the length
505 if(newVal->type == typeBinary)
506 newVal->value = AllDataType::alloc(fInfo->type, 2 * fInfo->length);
507 else newVal->value = AllDataType::alloc(fInfo->type, fInfo->length);
508 newVal->isAllocVal=true;
509 parsedData->insertFieldValue(newVal);
510 parsedData->insertField(fName);
511 table->bindFld(fName, newVal->value);
513 fNameIter.reset();
514 while (fNameIter.hasElement())
515 delete (Identifier *) fNameIter.nextElement();
516 fNameList.reset();
517 delete fInfo;
518 return OK;
521 DbRetVal SelStatement::setBindFieldAndValues()
523 totalFields = parsedData->getFieldNameList().size();
524 bindFields = (FieldValue**) malloc ( totalFields * sizeof(FieldValue*));
525 bindFieldValues = (char**) malloc( totalFields * sizeof(char*));
526 memset(bindFields, 0, totalFields * sizeof(FieldValue*));
527 memset(bindFieldValues, 0, totalFields * sizeof(char*));
528 ListIterator valIter = parsedData->getFieldValueList().getIterator();
529 int colNo =0;
530 FieldValue *value = NULL;
531 valIter.reset();
532 while(valIter.hasElement())
534 value = (FieldValue*) valIter.nextElement();
535 if (value == NULL)
537 free(bindFields); bindFields = NULL;
538 free(bindFieldValues); bindFieldValues = NULL;
539 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
540 return ErrSysFatal;
542 bindFields[colNo++ ] = value;
544 return OK;
548 DbRetVal SelStatement::resolveForCondition()
550 //get the fieldname list and validate field names
551 ListIterator iter = parsedData->getConditionValueList().getIterator();
553 ConditionValue *value;
554 FieldInfo *fInfo = new FieldInfo();
555 int paramPos =1;
556 DbRetVal rv = OK;
557 while (iter.hasElement())
559 value = (ConditionValue*) iter.nextElement();
560 if (NULL == value)
562 delete fInfo;
563 printError(ErrSysFatal, "Should never happen.");
564 return ErrSysFatal;
566 rv = table->getFieldInfo(value->fName, fInfo);
567 if (ErrNotFound == rv || ErrNotExists == rv)
569 delete fInfo;
570 printError(ErrSyntaxError, "Field %s does not exist in table",
571 value->fName);
572 return ErrSyntaxError;
574 value->type = fInfo->type;
575 value->length = fInfo->length;
576 value->isNullable = fInfo->isNull;
577 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
578 //table->bindFld(name->fldName, value->value);
579 if (value->parsedString == NULL)
581 delete fInfo;
582 printError(ErrSyntaxError, "Condition value should not be NULL");
583 return ErrSyntaxError;
585 if (value->parsedString[0] == '?')
587 if(!value->opLike) // checks if 'LIKE' operator is used
588 value->paramNo = paramPos++;
590 if (!value->paramNo) {
591 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
592 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
595 delete fInfo;
596 totalParams = paramPos -1;
597 if (0 == totalParams) return OK;
598 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
599 paramValues = (char**) malloc( totalParams * sizeof(char*));
600 memset(params, 0, totalParams * sizeof(FieldValue*));
601 memset(paramValues, 0, totalParams * sizeof(char*));
602 iter.reset();
603 while(iter.hasElement())
605 value = (ConditionValue*) iter.nextElement();
606 if (value == NULL)
608 free(params); params = NULL;
609 free(paramValues); paramValues = NULL;
610 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
611 return ErrSysFatal;
613 params[value->paramNo -1 ] = value;
615 return OK;
617 void* SelStatement::handleSingleTableAggWithoutGroup()
619 if (isPointReturned) return NULL;
620 TableImpl *tblImpl = (TableImpl*)table;
621 ListIterator iter = parsedData->getFieldNameList().getIterator();
622 int i=0;
623 DbRetVal rv = OK;
624 FieldName *name;
625 FieldValue *fVal = NULL;
626 while (iter.hasElement())
628 name = (FieldName*) iter.nextElement();
629 fVal = bindFields[i];
631 //rv = tblImpl->fetchAgg(name, (int) name->aType, fVal->value);
632 rv = tblImpl->fetchAgg(name->fldName, name->aType, fVal->value);
633 if (OK != rv) return NULL;
634 i++;
635 tblImpl->closeScan();
636 tblImpl->execute();
638 isPointReturned = true;
639 return fVal;
641 void* SelStatement::fetch()
643 if(handleAggWithTbl)
645 return handleSingleTableAggWithoutGroup();
647 void *tuple = table->fetch();
648 if (NULL == tuple) return NULL;
649 //copy values to binded buffer
650 FieldValue *value;
651 for (int i = 0; i < totalFields; i++)
653 value = bindFields[i];
654 if (bindFieldValues[i] == NULL)
656 printError(ErrBadCall, "Fields are not binded properly. Should never happen");
657 return NULL;
659 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
661 return tuple;
664 void* SelStatement::fetch(DbRetVal &rv)
666 if(handleAggWithTbl)
668 return handleSingleTableAggWithoutGroup();
670 void *tuple = table->fetch(rv);
671 if (NULL == tuple) return NULL;
672 //copy values to binded buffer
673 FieldValue *value;
674 for (int i = 0; i < totalFields; i++)
676 value = bindFields[i];
677 if (bindFieldValues[i] == NULL)
679 printError(ErrBadCall, "Fields are not binded properly. Should never happen %d", i);
680 return NULL;
682 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
684 return tuple;
687 DbRetVal SelStatement::close()
689 isPointReturned = false;
690 if (table) return table->closeScan();
691 else return OK;
694 void* SelStatement::getParamValuePtr( int pos )
696 ConditionValue *p = (ConditionValue*) params [pos-1];
697 return ( (void*) p->value );
700 char* SelStatement::getFieldName ( int pos )
702 //TODO::if not yet prepared return error
703 //TODO::check the upper limit for projpos
704 ListIterator iter = parsedData->getFieldNameList().getIterator();
705 int position =0;
706 while (iter.hasElement())
708 if (position == pos) {
709 FieldName *name = (FieldName*) iter.nextElement();
710 if (NULL == name)
712 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
713 return (char*) 0;
715 return name->fldName;
717 position++;
719 return (char*) 0;
722 DataType SelStatement::getFieldType( int pos )
724 FieldValue *v = bindFields[pos];
725 return ( (DataType) v->type );
728 int SelStatement::getFieldLength( int pos )
730 FieldValue *v = bindFields[pos];
731 return ( (int) v->type );
734 void* SelStatement::fetchAndPrint(bool SQL)
736 void *tuple = NULL;
737 if(handleAggWithTbl)
739 tuple = handleSingleTableAggWithoutGroup();
740 }else {
741 tuple = table->fetch();
743 if (NULL == tuple) return NULL;
744 FieldValue *value;
745 bool nullValueSet;
746 char stmt[128];
747 if (SQL) {
748 sprintf(stmt, "INSERT INTO %s VALUES(", table->getName());
749 printf("%s", stmt);
751 for (int i = 0; i < totalFields; i++)
753 value = bindFields[i];
754 nullValueSet = table->isFldNull(value->fldName);
755 if (nullValueSet)
756 if (SQL) {
757 if (i==0)
758 printf("NULL");
759 else
760 printf(", NULL");
762 else printf("NULL\t");
763 else {
764 if (SQL) {
765 switch(value->type)
767 case typeString:
768 case typeBinary:
769 case typeDate:
770 case typeTime:
771 case typeTimeStamp:
773 if (i==0)
774 printf(" '");
775 else
776 printf(", '");
777 break;
779 default:
781 if (i!=0)
782 printf(",");
786 AllDataType::printVal(value->value, value->type, value->length);
787 if (SQL) {
788 switch(value->type)
790 case typeString:
791 case typeBinary:
792 case typeDate:
793 case typeTime:
794 case typeTimeStamp:
795 printf("'");
797 } else printf("\t");
800 if (SQL) printf(");\n");
801 return tuple;
804 void* SelStatement::next()
806 if(handleAggWithTbl)
808 return handleSingleTableAggWithoutGroup();
810 return( table->fetch() );
814 void* SelStatement::getFieldValuePtr( int pos )
816 FieldValue *v = bindFields[pos];
817 return ( (void*) v->value );
820 int SelStatement::noOfProjFields()
822 return totalFields;
825 DbRetVal SelStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
827 //TODO::if not yet prepared return error
828 //TODO::check the upper limit for projpos
829 //TODO::validate if projpos is less than size of the list
830 ListIterator iter = parsedData->getFieldNameList().getIterator();
831 FieldName *name = NULL;
832 DbRetVal rv = OK;
833 int position =0;
834 while (iter.hasElement())
836 name = (FieldName*)iter.nextElement();
837 if (NULL == name)
839 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
840 return ErrSysFatal;
842 if (position == (projpos-1)) break;
843 position++;
846 rv = table->getFieldInfo(name->fldName, fInfo);
847 if (OK == rv)
849 //get back the qualified name(tablename.fldname)
850 char qualName[IDENTIFIER_LENGTH];
851 strcpy(qualName, name->fldName);
852 switch(name->aType)
854 case AGG_COUNT:
855 sprintf(fInfo->fldName, "COUNT(%s)", qualName);
856 break;
857 case AGG_MIN:
858 sprintf(fInfo->fldName, "MIN(%s)", qualName);
859 break;
860 case AGG_MAX:
861 sprintf(fInfo->fldName, "MAX(%s)", qualName);
862 break;
863 case AGG_SUM:
864 sprintf(fInfo->fldName, "SUM(%s)", qualName);
865 break;
866 case AGG_AVG:
867 sprintf(fInfo->fldName, "AVG(%s)", qualName);
868 break;
869 default:
870 strcpy(fInfo->fldName, qualName);
871 break;
874 return rv;
876 int SelStatement::getFldPos(char *name)
878 return table->getFldPos(name);