NULL fixes through network and SetBinaryParam with int argument added
[csql.git] / src / sql / SelStatement.cxx
blob8dd690ecc151c38f1f78d373e94b97fd0405ca58
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, int length)
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->paramNo ==1) continue;//For Predecate t1.f1=t2.f1
580 if (value->parsedString == NULL)
582 delete fInfo;
583 printError(ErrSyntaxError, "Condition value should not be NULL");
584 return ErrSyntaxError;
586 if (value->parsedString[0] == '?')
588 if(!value->opLike) // checks if 'LIKE' operator is used
589 value->paramNo = paramPos++;
591 if (!value->paramNo) {
592 // Here for binary dataType it is not strcpy'd bcos internally memcmp is done for predicates like f2 = 'abcd' where f2 is binary
593 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, fInfo->length);
596 delete fInfo;
597 totalParams = paramPos -1;
598 if (0 == totalParams) return OK;
599 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
600 paramValues = (char**) malloc( totalParams * sizeof(char*));
601 memset(params, 0, totalParams * sizeof(FieldValue*));
602 memset(paramValues, 0, totalParams * sizeof(char*));
603 iter.reset();
604 while(iter.hasElement())
606 value = (ConditionValue*) iter.nextElement();
607 if (value == NULL)
609 free(params); params = NULL;
610 free(paramValues); paramValues = NULL;
611 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
612 return ErrSysFatal;
614 params[value->paramNo -1 ] = value;
616 return OK;
618 void* SelStatement::handleSingleTableAggWithoutGroup()
620 if (isPointReturned) return NULL;
621 TableImpl *tblImpl = (TableImpl*)table;
622 ListIterator iter = parsedData->getFieldNameList().getIterator();
623 int i=0;
624 DbRetVal rv = OK;
625 FieldName *name;
626 FieldValue *fVal = NULL;
627 while (iter.hasElement())
629 name = (FieldName*) iter.nextElement();
630 fVal = bindFields[i];
632 //rv = tblImpl->fetchAgg(name, (int) name->aType, fVal->value);
633 rv = tblImpl->fetchAgg(name->fldName, name->aType, fVal->value);
634 if (OK != rv) return NULL;
635 i++;
636 tblImpl->closeScan();
637 tblImpl->execute();
639 isPointReturned = true;
640 return fVal;
642 void* SelStatement::fetch()
644 if(handleAggWithTbl)
646 return handleSingleTableAggWithoutGroup();
648 void *tuple = table->fetch();
649 if (NULL == tuple) return NULL;
650 //copy values to binded buffer
651 FieldValue *value;
652 for (int i = 0; i < totalFields; i++)
654 value = bindFields[i];
655 if (bindFieldValues[i] == NULL)
657 printError(ErrBadCall, "Fields are not binded properly. Should never happen");
658 return NULL;
660 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
662 return tuple;
665 void* SelStatement::fetch(DbRetVal &rv)
667 if(handleAggWithTbl)
669 return handleSingleTableAggWithoutGroup();
671 void *tuple = table->fetch(rv);
672 if (NULL == tuple) return NULL;
673 //copy values to binded buffer
674 FieldValue *value;
675 for (int i = 0; i < totalFields; i++)
677 value = bindFields[i];
678 if (bindFieldValues[i] == NULL)
680 printError(ErrBadCall, "Fields are not binded properly. Should never happen %d", i);
681 return NULL;
683 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
685 return tuple;
688 DbRetVal SelStatement::close()
690 isPointReturned = false;
691 if (table) return table->closeScan();
692 else return OK;
695 void* SelStatement::getParamValuePtr( int pos )
697 ConditionValue *p = (ConditionValue*) params [pos-1];
698 return ( (void*) p->value );
701 char* SelStatement::getFieldName ( int pos )
703 //TODO::if not yet prepared return error
704 //TODO::check the upper limit for projpos
705 ListIterator iter = parsedData->getFieldNameList().getIterator();
706 int position =0;
707 while (iter.hasElement())
709 if (position == pos) {
710 FieldName *name = (FieldName*) iter.nextElement();
711 if (NULL == name)
713 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
714 return (char*) 0;
716 return name->fldName;
718 position++;
720 return (char*) 0;
723 DataType SelStatement::getFieldType( int pos )
725 FieldValue *v = bindFields[pos];
726 return ( (DataType) v->type );
729 int SelStatement::getFieldLength( int pos )
731 FieldValue *v = bindFields[pos];
732 return ( (int) v->type );
735 void* SelStatement::fetchAndPrint(bool SQL)
737 void *tuple = NULL;
738 if(handleAggWithTbl)
740 tuple = handleSingleTableAggWithoutGroup();
741 }else {
742 tuple = table->fetch();
744 if (NULL == tuple) return NULL;
745 FieldValue *value;
746 bool nullValueSet;
747 char stmt[128];
748 if (SQL) {
749 sprintf(stmt, "INSERT INTO %s VALUES(", table->getName());
750 printf("%s", stmt);
752 for (int i = 0; i < totalFields; i++)
754 value = bindFields[i];
755 nullValueSet = table->isFldNull(value->fldName);
756 if (nullValueSet)
757 if (SQL) {
758 if (i==0)
759 printf("NULL");
760 else
761 printf(", NULL");
763 else printf("NULL\t");
764 else {
765 if (SQL) {
766 switch(value->type)
768 case typeString:
769 case typeBinary:
770 case typeDate:
771 case typeTime:
772 case typeTimeStamp:
774 if (i==0)
775 printf(" '");
776 else
777 printf(", '");
778 break;
780 default:
782 if (i!=0)
783 printf(",");
787 AllDataType::printVal(value->value, value->type, value->length);
788 if (SQL) {
789 switch(value->type)
791 case typeString:
792 case typeBinary:
793 case typeDate:
794 case typeTime:
795 case typeTimeStamp:
796 printf("'");
798 } else printf("\t");
801 if (SQL) printf(");\n");
802 return tuple;
805 void* SelStatement::next()
807 if(handleAggWithTbl)
809 return handleSingleTableAggWithoutGroup();
811 return( table->fetch() );
815 void* SelStatement::getFieldValuePtr( int pos )
817 FieldValue *v = bindFields[pos];
818 return ( (void*) v->value );
821 int SelStatement::noOfProjFields()
823 return totalFields;
826 DbRetVal SelStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
828 //TODO::if not yet prepared return error
829 //TODO::check the upper limit for projpos
830 //TODO::validate if projpos is less than size of the list
831 ListIterator iter = parsedData->getFieldNameList().getIterator();
832 FieldName *name = NULL;
833 DbRetVal rv = OK;
834 int position =0;
835 while (iter.hasElement())
837 name = (FieldName*)iter.nextElement();
838 if (NULL == name)
840 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
841 return ErrSysFatal;
843 if (position == (projpos-1)) break;
844 position++;
847 rv = table->getFieldInfo(name->fldName, fInfo);
848 if (OK == rv)
850 //get back the qualified name(tablename.fldname)
851 char qualName[IDENTIFIER_LENGTH];
852 strcpy(qualName, name->fldName);
853 switch(name->aType)
855 case AGG_COUNT:
856 sprintf(fInfo->fldName, "COUNT(%s)", qualName);
857 break;
858 case AGG_MIN:
859 sprintf(fInfo->fldName, "MIN(%s)", qualName);
860 break;
861 case AGG_MAX:
862 sprintf(fInfo->fldName, "MAX(%s)", qualName);
863 break;
864 case AGG_SUM:
865 sprintf(fInfo->fldName, "SUM(%s)", qualName);
866 break;
867 case AGG_AVG:
868 sprintf(fInfo->fldName, "AVG(%s)", qualName);
869 break;
870 default:
871 strcpy(fInfo->fldName, qualName);
872 break;
875 return rv;
877 int SelStatement::getFldPos(char *name)
879 return table->getFldPos(name);