Removing dependency for Cache module in MMDB build
[csql.git] / src / sql / MetadataStatement.cxx
blobb4c53870fb5f4ef7de8965023a7e36a3b2bfd2a8
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 <os.h>
17 #include <Statement.h>
18 #include <TableImpl.h>
19 #include <OrderTableImpl.h>
22 ListIterator MetadataStatement::getExportKeyIterator()
24 List datalist;
25 TableImpl *tbl = (TableImpl*)table;
26 if(!tbl->isPkTbl)
28 return datalist.getIterator();
29 }else
31 TableImpl *fkTbl =NULL;
32 ListIterator tblIter =tbl->tblFkList.getIterator();
33 tblIter.reset();
34 void *tPkptr =NULL;
35 void *tFkptr = NULL;
36 void *chunkPk = NULL;
37 char *pkFldName = NULL;
38 char *fkFldName = NULL;
39 ForeignKeyMetadata *fkMeta= NULL;
40 while (tblIter.hasElement()){
41 fkTbl = (TableImpl *) tblIter.nextElement();
42 FieldNameList pkFieldList,fkFieldList;
43 CatalogTableTABLE cTable(tbl->sysDB_);
44 cTable.getChunkAndTblPtr(tbl->getName(), chunkPk, tPkptr);
45 cTable.getChunkAndTblPtr(fkTbl->getName(), chunkPk, tFkptr);
46 CatalogTableFK cFk(tbl->sysDB_);
47 cFk.getPkFkFieldInfo(tPkptr,tFkptr,pkFieldList,fkFieldList);
48 pkFieldList.resetIter();
49 fkFieldList.resetIter();
50 int count = 0;
51 while((fkFldName = fkFieldList.nextFieldName())!= NULL)
53 count++;
54 fkMeta = new ForeignKeyMetadata();
55 pkFldName = pkFieldList.nextFieldName();
56 strcpy(fkMeta->pkTableName,tbl->getName());
57 strcpy(fkMeta->pkColName,pkFldName);
58 strcpy(fkMeta->fkTableName,fkTbl->getName());
59 strcpy(fkMeta->fkColName,fkFldName);
60 fkMeta->keySeq = count;
61 fkMeta->updateRule = 3;
62 fkMeta->deleteRule = 3;
63 datalist.append(fkMeta);
68 return datalist.getIterator();
71 ListIterator MetadataStatement::getImportKeyIterator()
73 List datalist;
74 TableImpl *tbl = (TableImpl*)table;
75 if(!tbl->isFkTbl)
77 return datalist.getIterator();
78 }else
80 TableImpl *fkTbl =NULL;
81 ListIterator tblIter = tbl->tblList.getIterator();
82 tblIter.reset();
83 void *tPkptr =NULL;
84 void *tFkptr = NULL;
85 void *chunkPk = NULL;
86 char *pkFldName = NULL;
87 char *fkFldName = NULL;
88 ForeignKeyMetadata *fkMeta= NULL;
89 while (tblIter.hasElement()){
90 fkTbl = (TableImpl *) tblIter.nextElement();
91 FieldNameList pkFieldList,fkFieldList;
92 CatalogTableTABLE cTable(tbl->sysDB_);
93 cTable.getChunkAndTblPtr(tbl->getName(), chunkPk, tPkptr);
94 cTable.getChunkAndTblPtr(fkTbl->getName(), chunkPk, tFkptr);
95 CatalogTableFK cFk(tbl->sysDB_);
96 cFk.getPkFkFieldInfo(tFkptr,tPkptr,fkFieldList,pkFieldList);
97 pkFieldList.resetIter();
98 fkFieldList.resetIter();
99 int count = 0;
100 while((pkFldName = pkFieldList.nextFieldName())!= NULL)
102 count++;
103 fkMeta = new ForeignKeyMetadata();
104 fkFldName = fkFieldList.nextFieldName();
105 strcpy(fkMeta->pkTableName,fkTbl->getName());
106 strcpy(fkMeta->pkColName,fkFldName);
107 strcpy(fkMeta->fkTableName,tbl->getName());
108 strcpy(fkMeta->fkColName,pkFldName);
109 fkMeta->keySeq = count;
110 fkMeta->updateRule = 3;
111 fkMeta->deleteRule = 3;
112 datalist.append(fkMeta);
117 return datalist.getIterator();
121 MetadataStatement::MetadataStatement()
123 parsedData = NULL;
124 dbMgr = NULL;
125 table = NULL;
126 params = NULL;
127 paramValues = NULL;
128 totalParams = 0;
129 bindFields = NULL;
130 bindFieldValues = NULL;
131 totalFields = 0;
132 plan = Normal;
133 numRecords = 0;
134 isPlanFreed=false;
135 isSecondFetch=false;
138 MetadataStatement::~MetadataStatement()
140 close();
141 if (table) { table->close(); table = NULL; }
142 if (totalParams) {
143 free(params);
144 params = NULL;
145 free(paramValues);
146 paramValues = NULL;
148 if (totalFields)
150 free(bindFields);
151 bindFields = NULL;
152 free(bindFieldValues);
153 bindFieldValues = NULL;
157 DbRetVal MetadataStatement::getParamFldInfo(int paramNo, FieldInfo *&info)
159 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
160 ConditionValue *cValue = (ConditionValue*) params [paramNo-1];
161 if (NULL == cValue)
163 printError(ErrSysFatal, "condition value is null. Should never happen");
164 return ErrSysFatal;
166 table->getFieldNameAlone(cValue->fName,info->fldName);
167 info->type = cValue->type;
168 info->length = cValue->length;
169 info->isNull = cValue->isNullable;
170 return OK;
172 DbRetVal MetadataStatement::execute(int &rowsAffected)
174 DbRetVal rv = OK;
175 switch( plan )
177 case GetTables:
179 dataListIter = dbMgr->getAllTableNames().getIterator();
180 break;
182 case GetColumns:
184 List colList;
185 List fNameList = table->getFieldNameList();
186 ListIterator fNameIter = fNameList.getIterator();
187 for (int i = 0; i < fNameList.size() ; i++)
189 FieldInfo *fInfo = new FieldInfo();
190 Identifier *elem= (Identifier*)fNameIter.nextElement();
191 char *fName = elem->name;
192 rv = table->getFieldInfo(fName, fInfo);
193 if (ErrNotFound == rv || ErrNotExists == rv)
195 delete fInfo;
196 fNameList.reset();
197 printError(ErrSysFatal, "Should never happen.");
198 return ErrSysFatal;
200 delete elem;
201 colList.append(fInfo);
203 fNameIter.reset();
204 fNameList.reset();
205 dataListIter = colList.getIterator();
206 break;
208 case GetIndexes:
210 List colList;
211 CatalogTableINDEXFIELD cIndexField(((DatabaseManagerImpl *)dbMgr)->sysDb());
212 dataListIter = cIndexField.getIndexListIterater(table->getName());
213 break;
215 case GetPriIndex:
217 List colList;
218 CatalogTableINDEXFIELD cIndexField(((DatabaseManagerImpl *)dbMgr)->sysDb());
219 dataListIter = cIndexField.getIndexListIterater(table->getName());
220 break;
222 case GetImportKey:
224 dataListIter = getImportKeyIterator();
225 break;
227 case GetExportKey:
229 dataListIter = getExportKeyIterator();
230 break;
232 case GetDataType:
234 dataListIter = getDataTypeInfoIterator();
235 break;
237 default:
238 break;
240 return rv;
243 DbRetVal MetadataStatement::openTables()
245 //TODO : need to remove unnecessery code
246 if (dbMgr == NULL) return ErrNoConnection;
247 JoinTableImpl *jHdl = NULL;
248 Table *tHdl = NULL, *prevHdl = NULL;
249 bool joinInvolved = false;
250 //check whether all the table exists
251 ListIterator titer = parsedData->getTableNameList().getIterator();
252 ListIterator jiter = parsedData->getJoinTypeList().getIterator();
253 while (titer.hasElement())
255 TableName *t = (TableName*)titer.nextElement();
256 parsedData->setTableName(t->tblName);
257 tHdl = dbMgr->openTable(t->tblName);
258 if ( NULL == tHdl )
260 printError(ErrNotExists,
261 "Unable to open the table:Table not exists");
262 if (prevHdl) delete prevHdl;
263 return ErrNotExists;
265 TableImpl *tImpl= (TableImpl*) tHdl;
266 if (NULL != prevHdl)
268 joinInvolved = true;
269 jHdl = new JoinTableImpl();
270 jHdl->setTable(prevHdl, tHdl);
271 JoinTypeNode *node = (JoinTypeNode*) jiter.nextElement();
272 if (node == NULL) {
273 printError(ErrSyntaxError,
274 "Join Type and number of tables do not match");
275 delete jHdl;
276 return ErrSyntaxError;
278 jHdl->setJoinType(node->jType);
279 prevHdl = jHdl;
280 continue;
282 prevHdl = tHdl;
285 if (joinInvolved) table = jHdl; else table = tHdl;
286 return OK;
288 DbRetVal MetadataStatement::resolve()
290 DbRetVal rv =OK;
291 plan = parsedData->getResultSetPlan();
292 switch(plan)
294 case GetTables:
296 bindFields = (FieldValue**) malloc (5 * sizeof(FieldValue*));
297 bindFieldValues = (char**) malloc (5 * sizeof(char*));
298 for(int i=0;i<5;i++){
299 FieldValue *newVal = new FieldValue();
300 if(i==0) strcpy(newVal->fldName,"TABLE_CAT");
301 if(i==1) strcpy(newVal->fldName,"TABLE_SCHEM");
302 if(i==2) strcpy(newVal->fldName,"TABLE_NAME");
303 if(i==3) strcpy(newVal->fldName,"TABLE_TYPE");
304 newVal->parsedString = NULL;
305 newVal->paramNo = 0;
306 newVal->type = typeString;
307 newVal->length = 64;
308 newVal->value = AllDataType::alloc(newVal->type, 64);
309 if(i==0) strcpy((char*)newVal->value,(char*)"CSQL");
310 if(i==1) strcpy((char*)newVal->value,(char*)"CSQL");
311 if(i==3) strcpy((char*)newVal->value,(char*)"TABLE");
312 if(i==4) strcpy((char*)newVal->value,(char*)"TABLE Catalog");
313 newVal->isAllocVal=true;
314 totalFields =i+1;
315 bindFields[i] = newVal;
317 break;
319 case GetColumns:
321 rv = openTables();
322 if (rv != OK) break; //return rv;
323 bindFields = (FieldValue**) malloc (23 * sizeof(FieldValue*));
324 bindFieldValues = (char**) malloc (23 * sizeof(char*));
325 for(int i=0;i<23;i++){
326 FieldValue *newVal = new FieldValue();
327 switch(i){
328 case 0:
329 strcpy(newVal->fldName,"TABLE_CAT");
330 newVal->type = typeString;
331 break;
332 case 1:
333 newVal->type = typeString;
334 strcpy(newVal->fldName,"TABLE_SCHEM");
335 break;
336 case 2:
337 newVal->type = typeString;
338 strcpy(newVal->fldName,"TABLE_NAME");
339 break;
340 case 3:
341 newVal->type = typeString;
342 strcpy(newVal->fldName,"COLUMN_NAME");
343 break;
344 case 4:
345 newVal->type = typeInt;
346 strcpy(newVal->fldName,"DATA_TYPE");
347 break;
348 case 5:
349 newVal->type = typeString;
350 strcpy(newVal->fldName,"TYPE_NAME");
351 break;
352 case 6:
353 newVal->type = typeInt;
354 strcpy(newVal->fldName,"COLUMN_SIZE");
355 break;
356 case 7:
357 newVal->type = typeInt;
358 strcpy(newVal->fldName,"BUFFER_LENGTH");
359 break;
360 case 8:
361 newVal->type = typeShort;
362 strcpy(newVal->fldName,"DECIMAL_DIGITS");
363 break;
364 case 9:
365 newVal->type = typeShort;
366 strcpy(newVal->fldName,"NUM_PREC_RADIX");
367 break;
368 case 10:
369 newVal->type = typeShort;
370 strcpy(newVal->fldName,"NULLABLE");
371 break;
372 case 11:
373 newVal->type = typeString;
374 strcpy(newVal->fldName,"REMARKS");
375 break;
376 case 12:
377 newVal->type = typeString;
378 strcpy(newVal->fldName,"COLUMN_DEF");
379 break;
380 case 13:
381 newVal->type = typeShort;
382 strcpy(newVal->fldName,"SQL_DATA_TYPE");
383 break;
384 case 14:
385 newVal->type = typeInt;
386 strcpy(newVal->fldName,"SQL_DATETIME_SUB");
387 break;
388 case 15:
389 newVal->type = typeInt;
390 strcpy(newVal->fldName,"CHAR_OCTET_LENGTH");
391 break;
392 case 16:
393 newVal->type = typeInt;
394 strcpy(newVal->fldName,"ORDINAL_POSITION");
395 break;
396 case 17:
397 newVal->type = typeString;
398 strcpy(newVal->fldName,"IS_NULLABLE");
399 break;
400 case 18:
401 newVal->type = typeString;
402 strcpy(newVal->fldName,"SCOPE_CATLOG");
403 break;
404 case 19:
405 newVal->type = typeString;
406 strcpy(newVal->fldName,"SCOPE_SCHEMA");
407 break;
408 case 20:
409 newVal->type = typeString;
410 strcpy(newVal->fldName,"SCOPE_TABLE");
411 break;
412 case 21:
413 newVal->type = typeShort;
414 strcpy(newVal->fldName,"SOURCE_DATA_TYPE");
415 break;
416 case 22:
417 newVal->type = typeString;
418 strcpy(newVal->fldName,"IS_AUTOINCREMENT");
419 break;
420 default:
421 newVal->type = typeString;
422 break;
424 newVal->parsedString = NULL;
425 newVal->paramNo = 0;
426 newVal->length = AllDataType::size(newVal->type, 64);
427 if(i==12) newVal->length = DEFAULT_VALUE_BUF_LENGTH;
428 newVal->value = AllDataType::alloc(newVal->type,newVal->length);
429 if(newVal->type == typeInt) (*(int*)newVal->value) = 0;
430 if(newVal->type == typeShort) (*(short*)newVal->value) = 0;
431 if(newVal->type == typeString) strcpy((char*)newVal->value,(char*)"");
432 if(i==0) strcpy((char*)newVal->value,(char*)"CSQL");
433 if(i==1) strcpy((char*)newVal->value,(char*)"CSQL");
434 if(i==2) strcpy((char*)newVal->value,(char*)table->getName());
435 if(i==11) strcpy((char*)newVal->value,(char*)"Columns Catalog");
436 // if(i==16) (*(int *)newVal->value) = 0;
437 newVal->isAllocVal=true;
438 totalFields =i+1;
439 bindFields[i] = newVal;
441 break;
443 case GetIndexes:
445 rv = openTables();
446 if (rv != OK) break; //return rv;
447 bindFields = (FieldValue**) malloc (13 * sizeof(FieldValue*));
448 bindFieldValues = (char**) malloc (13 * sizeof(char*));
449 for(int i=0;i<13;i++){
450 FieldValue *newVal = new FieldValue();
451 switch(i)
453 case 0:
454 strcpy(newVal->fldName,"TABLE_CAT");
455 newVal->type = typeString;
456 break;
457 case 1:
458 newVal->type = typeString;
459 strcpy(newVal->fldName,"TABLE_SCHEM");
460 break;
461 case 2:
462 newVal->type = typeString;
463 strcpy(newVal->fldName,"TABLE_NAME");
464 break;
465 case 3:
466 newVal->type = typeInt;
467 strcpy(newVal->fldName,"NON_UNIQUE");
468 break;
469 case 4:
470 newVal->type = typeString;
471 strcpy(newVal->fldName,"INDEX_QUALIFIER");
472 break;
473 case 5:
474 newVal->type = typeString;
475 strcpy(newVal->fldName,"INDEX_NAME");
476 break;
477 case 6:
478 newVal->type = typeShort;
479 strcpy(newVal->fldName,"TYPE");
480 break;
481 case 7:
482 newVal->type = typeShort;
483 strcpy(newVal->fldName,"ORDINAL_POSITION");
484 break;
485 case 8:
486 newVal->type = typeString;
487 strcpy(newVal->fldName,"COLUMN_NAME");
488 break;
489 case 9:
490 newVal->type = typeString;
491 strcpy(newVal->fldName,"ASC_OR_DESC");
492 break;
493 case 10:
494 newVal->type = typeInt;
495 strcpy(newVal->fldName,"CARDINALITY");
496 break;
497 case 11:
498 newVal->type = typeInt;
499 strcpy(newVal->fldName,"PAGES");
500 break;
501 case 12:
502 newVal->type = typeString;
503 strcpy(newVal->fldName,"FILTER_CONDITION");
504 break;
505 default:
506 newVal->type = typeString;
507 break;
509 newVal->parsedString = NULL;
510 newVal->paramNo = 0;
511 newVal->length = AllDataType::size(newVal->type, 64);
512 newVal->value = AllDataType::alloc(newVal->type,newVal->length);
513 if(newVal->type == typeString) strcpy((char*)newVal->value,(char*)"");
514 if(i==0) strcpy((char*)newVal->value,(char*)"CSQL");
515 if(i==1) strcpy((char*)newVal->value,(char*)"CSQL");
516 newVal->isAllocVal=true;
517 totalFields =i+1;
518 bindFields[i] = newVal;
520 break;
522 case GetPriIndex:
524 rv = openTables();
525 if (rv != OK) break; //return rv;
526 bindFields = (FieldValue**) malloc (6 * sizeof(FieldValue*));
527 bindFieldValues = (char**) malloc (6 * sizeof(char*));
528 for(int i=0;i<6;i++){
529 FieldValue *newVal = new FieldValue();
530 switch(i)
532 case 0:
533 strcpy(newVal->fldName,"TABLE_CAT");
534 newVal->type = typeString;
535 break;
536 case 1:
537 newVal->type = typeString;
538 strcpy(newVal->fldName,"TABLE_SCHEM");
539 break;
540 case 2:
541 newVal->type = typeString;
542 strcpy(newVal->fldName,"TABLE_NAME");
543 break;
544 case 3:
545 newVal->type = typeString;
546 strcpy(newVal->fldName,"COLUMN_NAME");
547 break;
548 case 4:
549 newVal->type = typeShort;
550 strcpy(newVal->fldName,"KEY_SEQ");
551 break;
552 case 5:
553 newVal->type = typeString;
554 strcpy(newVal->fldName,"PK_NAME");
555 break;
556 default:
557 break;
559 newVal->parsedString = NULL;
560 newVal->paramNo = 0;
561 newVal->length = AllDataType::size(newVal->type, 64);
562 newVal->value = AllDataType::alloc(newVal->type, newVal->length);
563 if(i==0) strcpy((char*)newVal->value,(char*)"CSQL");
564 if(i==1) strcpy((char*)newVal->value,(char*)"CSQL");
565 newVal->isAllocVal=true;
566 totalFields = i+1;
567 bindFields[i] = newVal;
569 break;
571 case GetCatalogs:
573 bindFields = (FieldValue**) malloc (1 * sizeof(FieldValue*));
574 bindFieldValues = (char**) malloc (1 * sizeof(char*));
575 FieldValue *newVal = new FieldValue();
576 strcpy(newVal->fldName,"TABLE_CAT");
577 newVal->type = typeString;
578 newVal->parsedString = NULL;
579 newVal->paramNo = 0;
580 newVal->length = AllDataType::size(newVal->type, 64);
581 newVal->value = AllDataType::alloc(newVal->type, newVal->length);
582 newVal->isAllocVal=true;
583 totalFields = 1;
584 bindFields[0] = newVal;
585 break;
587 case GetTableType:
589 bindFields = (FieldValue**) malloc (1 * sizeof(FieldValue*));
590 bindFieldValues = (char**) malloc (1 * sizeof(char*));
591 FieldValue *newVal = new FieldValue();
592 strcpy(newVal->fldName,"TABLE_TYPE");
593 newVal->type = typeString;
594 newVal->parsedString = NULL;
595 newVal->paramNo = 0;
596 newVal->length = AllDataType::size(newVal->type, 64);
597 newVal->value = AllDataType::alloc(newVal->type, newVal->length);
598 newVal->isAllocVal=true;
599 totalFields = 1;
600 bindFields[0] = newVal;
601 break;
603 case GetDataType:
605 bindFields = (FieldValue**) malloc (19 * sizeof(FieldValue*));
606 bindFieldValues = (char**) malloc (19 * sizeof(char*));
607 for(int i=0;i<19;i++)
609 FieldValue *newVal = new FieldValue();
610 switch(i)
612 case 0:
613 strcpy(newVal->fldName,"TYPE_NAME");
614 newVal->type = typeString;
615 break;
616 case 1:
617 newVal->type = typeInt;
618 strcpy(newVal->fldName,"DATA_TYPE");
619 break;
620 case 2:
621 strcpy(newVal->fldName,"PRECISION");
622 newVal->type = typeInt;
623 break;
624 case 3:
625 strcpy(newVal->fldName,"LITERAL_PREFIX");
626 newVal->type = typeString;
627 break;
628 case 4:
629 strcpy(newVal->fldName,"LITERAL_SUFFIX");
630 newVal->type = typeString;
631 break;
632 case 5:
633 strcpy(newVal->fldName,"CREATE_PARAMS");
634 newVal->type = typeString;
635 break;
636 case 6:
637 strcpy(newVal->fldName,"NULLABLE");
638 newVal->type = typeShort;
639 break;
640 case 7:
641 strcpy(newVal->fldName,"CASE_SENSITIVE");
642 newVal->type = typeInt;
643 break;
644 case 8:
645 strcpy(newVal->fldName,"SEARCHABLE");
646 newVal->type = typeShort;
647 break;
648 case 9:
649 strcpy(newVal->fldName,"UNSIGNED_ATTRIBUTE");
650 newVal->type = typeInt;
651 break;
652 case 10:
653 strcpy(newVal->fldName,"FIXED_PREC_SCALE");
654 newVal->type = typeInt;
655 break;
656 case 11:
657 strcpy(newVal->fldName,"AUTO_INCREMENT");
658 newVal->type = typeInt;
659 break;
660 case 12:
661 strcpy(newVal->fldName,"LOCAL_TYPE_NAME");
662 newVal->type = typeString;
663 break;
664 case 13:
665 strcpy(newVal->fldName,"MINIMUM_SCALE");
666 newVal->type = typeShort;
667 break;
668 case 14:
669 strcpy(newVal->fldName,"MAXIMUM_SCALE");
670 newVal->type = typeShort;
671 break;
672 case 15:
673 strcpy(newVal->fldName,"SQL_DATA_TYPE");
674 newVal->type = typeInt;
675 break;
676 case 16:
677 strcpy(newVal->fldName,"SQL_DATETIME_SUB");
678 newVal->type = typeInt;
679 break;
680 case 17:
681 strcpy(newVal->fldName,"NUM_PREC_RADIX");
682 newVal->type = typeInt;
683 break;
684 case 18:
685 strcpy(newVal->fldName,"INTERVAL_PRECISION");
686 newVal->type = typeShort;
687 break;
688 default:
689 break;
691 newVal->parsedString = NULL;
692 newVal->paramNo = 0;
693 newVal->length = AllDataType::size(newVal->type, 64);
694 newVal->value = AllDataType::alloc(newVal->type,newVal->length);
695 newVal->isAllocVal = true;
696 totalFields = i+1;
697 bindFields[i] = newVal;
699 break;
701 case GetImportKey:
702 case GetExportKey:
704 rv = openTables();
705 if (rv != OK) break; //return rv;
706 bindFields = (FieldValue**) malloc (14 * sizeof(FieldValue*));
707 bindFieldValues = (char**) malloc (14 * sizeof(char*));
708 for(int i=0;i<14;i++){
709 FieldValue *newVal = new FieldValue();
710 switch(i){
711 case 0:
712 strcpy(newVal->fldName,"PKTABLE_CAT");
713 newVal->type = typeString;
714 break;
715 case 1:
716 newVal->type = typeString;
717 strcpy(newVal->fldName,"PKTABLE_SCHEM");
718 break;
719 case 2:
720 newVal->type = typeString;
721 strcpy(newVal->fldName,"PKTABLE_NAME");
722 break;
723 case 3:
724 newVal->type = typeString;
725 strcpy(newVal->fldName,"PKCOLUMN_NAME");
726 break;
727 case 4:
728 newVal->type = typeString;
729 strcpy(newVal->fldName,"FKTABLE_CAT");
730 break;
731 case 5:
732 newVal->type = typeString;
733 strcpy(newVal->fldName,"FKTABLE_SCHEM");
734 break;
735 case 6:
736 newVal->type = typeString;
737 strcpy(newVal->fldName,"FKTABLE_NAME");
738 break;
739 case 7:
740 newVal->type = typeString;
741 strcpy(newVal->fldName,"FKCOLUMN_NAME");
742 break;
743 case 8:
744 newVal->type = typeShort;
745 strcpy(newVal->fldName,"KEY_SEQ");
746 break;
747 case 9:
748 newVal->type = typeShort;
749 strcpy(newVal->fldName,"UPDATE_RULE");
750 break;
751 case 10:
752 newVal->type = typeShort;
753 strcpy(newVal->fldName,"DELETE_RULE");
754 break;
755 case 11:
756 newVal->type = typeString;
757 strcpy(newVal->fldName,"FK_NAME");
758 break;
759 case 12:
760 newVal->type = typeString;
761 strcpy(newVal->fldName,"PK_NAME");
762 break;
763 case 13:
764 newVal->type = typeShort;
765 strcpy(newVal->fldName,"DEFERRABILITY");
766 default:
767 newVal->type = typeString;
768 break;
770 newVal->parsedString = NULL;
771 newVal->paramNo = 0;
772 newVal->length = AllDataType::size(newVal->type, 64);
773 newVal->value = AllDataType::alloc(newVal->type,newVal->length);
774 if(typeString == newVal->type) strcpy((char*)newVal->value,(char*)"");
775 if(i==0||i==1||i == 4 || i==5) strcpy((char*)newVal->value,(char*)"csql");
776 newVal->isAllocVal=true;
777 totalFields =i+1;
778 bindFields[i] = newVal;
780 break;
782 default:
783 break;
785 return rv;
788 void* MetadataStatement::fetch()
791 void *tuple = NULL;
792 tuple = fetchMetaData();
793 if (NULL == tuple) return NULL;
794 FieldValue *value;
795 for (int i = 0; i < totalFields; i++)
797 value = bindFields[i];
798 if (bindFieldValues[i] == NULL)
800 continue;
802 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
804 return tuple;
807 void* MetadataStatement::fetch(DbRetVal &rv)
809 void *tuple = NULL;
810 tuple = fetchMetaData();
811 if (NULL == tuple) return NULL;
812 FieldValue *value;
813 for (int i = 0; i < totalFields; i++)
815 value = bindFields[i];
816 if (bindFieldValues[i] == NULL)
818 continue;
820 AllDataType::copyVal(bindFieldValues[i], value->value, value->type, value->length);
822 return tuple;
825 void MetadataStatement::freeMetadata()
827 if(isPlanFreed) return;
828 dataListIter.reset();
829 ListNode *head= (ListNode *)dataListIter.getCurrentListNode();
830 if(head && (plan != GetTableType && plan != GetCatalogs)){
831 List *list = new List(head);
832 list->reset();
833 delete list;
835 switch(plan)
837 case GetTables:
839 FieldValue *newVal = NULL;
840 if(bindFields){
841 for(int i=0;i<5;i++){
842 newVal=bindFields[i];
843 if (newVal->isAllocVal) free(newVal->value);
844 delete newVal;
847 break;
849 case GetColumns:
851 FieldValue *newVal;
852 if(bindFields){
853 for(int i=0;i<23;i++){
854 newVal=bindFields[i];
855 if ( newVal->isAllocVal) free(newVal->value);
856 delete newVal;
859 break;
861 case GetIndexes:
863 FieldValue *newVal;
864 if(bindFields){
865 for(int i=0;i<13;i++){
866 newVal=bindFields[i];
867 if ( newVal->isAllocVal) free(newVal->value);
868 delete newVal;
871 break;
873 case GetPriIndex:
875 FieldValue *newVal;
876 if(bindFields){
877 for(int i=0;i<6;i++){
878 newVal=bindFields[i];
879 if ( newVal->isAllocVal) free(newVal->value);
880 delete newVal;
883 break;
885 case GetCatalogs:
887 FieldValue *newVal;
888 if(bindFields){
889 for(int i=0;i<1;i++){
890 newVal=bindFields[i];
891 // if ( newVal->isAllocVal) free(newVal->value);
892 delete newVal;
895 break;
897 case GetTableType:
899 FieldValue *newVal;
900 if(bindFields){
901 for(int i=0;i<1;i++){
902 newVal=bindFields[i];
903 // if ( newVal->isAllocVal) free(newVal->value);
904 delete newVal;
907 break;
909 case GetDataType:
911 FieldValue *newVal;
912 if(bindFields){
913 for(int i=0;i<18;i++){
914 newVal=bindFields[i];
915 if ( newVal->isAllocVal) free(newVal->value);
916 delete newVal;
919 break;
921 case GetImportKey:
923 FieldValue *newVal;
924 if(bindFields){
925 for(int i=0;i<14;i++){
926 newVal=bindFields[i];
927 if ( newVal->isAllocVal) free(newVal->value);
928 delete newVal;
931 break;
933 case GetExportKey:
935 FieldValue *newVal;
936 if(bindFields){
937 for(int i=0;i<14;i++){
938 newVal=bindFields[i];
939 if ( newVal->isAllocVal) free(newVal->value);
940 delete newVal;
943 break;
945 default:
946 break;
948 isPlanFreed=true;
950 DbRetVal MetadataStatement::close()
952 if(plan!=Normal){ freeMetadata(); }
953 if (table) return table->closeScan();
954 return OK;
958 char* MetadataStatement::getFieldName ( int pos )
960 ListIterator iter = parsedData->getFieldNameList().getIterator();
961 int position =0;
962 while (iter.hasElement())
964 if (position == pos) {
965 FieldName *name = (FieldName*) iter.nextElement();
966 if (NULL == name)
968 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
969 return (char*) 0;
971 return name->fldName;
973 position++;
975 return (char*) 0;
978 DataType MetadataStatement::getFieldType( int pos )
980 FieldValue *v = bindFields[pos];
981 return ( (DataType) v->type );
984 int MetadataStatement::getFieldLength( int pos )
986 FieldValue *v = bindFields[pos];
987 return ( (int) v->type );
990 void* MetadataStatement::fetchAndPrint(bool SQL)
992 void *tuple = NULL;
993 return tuple;
996 void* MetadataStatement::next()
998 return fetchMetaData();
1001 void* MetadataStatement::getFieldValuePtr( int pos )
1003 FieldValue *v = bindFields[pos];
1004 return ( (void*) v->value );
1007 void* MetadataStatement::getFieldValuePtr( char *name )
1009 FieldValue *value;
1010 char fName[IDENTIFIER_LENGTH];
1011 for (int i = 0; i < totalFields; i++)
1013 value = bindFields[i];
1014 table->getFieldNameAlone(value->fldName,fName);
1015 if (strcmp(fName,name)==0)
1017 return ( (void*) value->value );
1021 return NULL;
1024 void MetadataStatement::getProjFieldType(int *data)
1026 FieldValue *value;
1027 for (int i = 0; i < totalFields; i++)
1029 value = bindFields[i];
1030 data[i+1] = value->type;
1034 DbRetVal MetadataStatement::setBindField(int colNo, void *value)
1036 if (colNo <=0) return ErrBadArg;
1037 if (NULL == value) return ErrBadArg;
1038 bindFieldValues[colNo -1] = (char*) value;
1039 return OK;
1043 int MetadataStatement::noOfProjFields()
1045 return totalFields;
1048 DbRetVal MetadataStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
1051 DbRetVal rv = OK;
1052 if(plan != Normal)
1054 FieldValue *v = bindFields[projpos-1];
1055 strcpy(fInfo->fldName, v->fldName);
1056 fInfo->type = v->type;
1057 fInfo->length = v->length;
1058 return rv;
1060 ListIterator iter = parsedData->getFieldNameList().getIterator();
1061 FieldName *name = NULL;
1062 int position =0;
1063 while (iter.hasElement())
1065 name = (FieldName*)iter.nextElement();
1066 if (NULL == name)
1068 printError(ErrSysFatal, "Should never happen. Field Name list has NULL");
1069 return ErrSysFatal;
1071 if (position == (projpos-1)) break;
1072 position++;
1075 rv = table->getFieldInfo(name->fldName, fInfo);
1076 if (OK == rv)
1078 //get back the qualified name(tablename.fldname)
1079 char qualName[IDENTIFIER_LENGTH];
1080 strcpy(qualName, name->fldName);
1081 switch(name->aType)
1083 case AGG_COUNT:
1084 sprintf(fInfo->fldName, "COUNT(%s)", qualName);
1085 fInfo->type = typeInt;
1086 fInfo->length = sizeof(int);
1087 break;
1088 case AGG_MIN:
1089 sprintf(fInfo->fldName, "MIN(%s)", qualName);
1090 break;
1091 case AGG_MAX:
1092 sprintf(fInfo->fldName, "MAX(%s)", qualName);
1093 break;
1094 case AGG_SUM:
1095 sprintf(fInfo->fldName, "SUM(%s)", qualName);
1096 break;
1097 case AGG_AVG:
1098 sprintf(fInfo->fldName, "AVG(%s)", qualName);
1099 fInfo->type = typeDouble;
1100 fInfo->length = sizeof(double);
1101 break;
1102 default:
1103 strcpy(fInfo->fldName, qualName);
1104 break;
1107 return rv;
1109 int MetadataStatement::getFldPos(char *name)
1111 return table->getFldPos(name);
1114 void *MetadataStatement::fetchMetaData()
1116 switch(plan)
1118 case GetTables:
1120 Identifier *elem = NULL;
1121 if(dataListIter.hasElement())
1123 FieldValue *v = bindFields[2];
1124 elem = (Identifier*) dataListIter.nextElement();
1125 AllDataType::copyVal(v->value, elem->name, v->type, v->length);
1126 delete elem;
1127 return v->value;
1128 } else return NULL;
1130 case GetColumns:
1132 FieldInfo *info = NULL;
1133 if(dataListIter.hasElement())
1135 info = (FieldInfo *) dataListIter.nextElement();
1136 FieldValue *v;
1137 for(int i=0;i<23;i++)
1139 switch(i)
1141 case 3:
1143 v = bindFields [i];
1144 AllDataType::copyVal(v->value, info->fldName, v->type, v->length);
1145 break;
1147 case 4:
1149 v = bindFields [i];
1150 int type=0;
1151 type = AllDataType::convertToCSQLSQLType(info->type);
1152 AllDataType::copyVal(v->value,&type,v->type, v->length);
1153 break;
1155 case 5:
1157 v = bindFields [i];
1158 AllDataType::copyVal(v->value,AllDataType::getSQLString(info->type),v->type, v->length);
1159 break;
1162 case 6:
1164 v = bindFields [i];
1165 AllDataType::copyVal(v->value,&info->length,v->type, v->length);
1166 break;
1168 case 10:
1170 v = bindFields [i];
1171 if(info->isNull)
1172 (*(short*)v->value)=0;
1173 else
1174 (*(short*)v->value)=1;
1175 break;
1177 case 12:
1179 v = bindFields [i];
1180 AllDataType::copyVal(v->value,info->defaultValueBuf,v->type, v->length);
1181 break;
1183 case 16:
1185 v = bindFields [i];
1186 (*(int*)v->value) = table->getFldPos(info->fldName);
1187 break;
1189 case 17:
1191 v = bindFields [i];
1192 if(info->isNull)
1193 AllDataType::copyVal(v->value,(void *)"NO",v->type, v->length);
1194 else
1195 AllDataType::copyVal(v->value,(void *)"YES",v->type, v->length);
1196 break;
1198 case 22:
1199 v = bindFields [i];
1200 if(info->isAutoIncrement)
1201 AllDataType::copyVal(v->value,(void *)"YES",v->type, v->length);
1202 else
1203 AllDataType::copyVal(v->value,(void *)"NO",v->type, v->length);
1205 default:
1206 break;
1209 delete info;
1210 return v->value ;
1211 }else return NULL;
1213 case GetIndexes:
1215 IndexInfoForDriver *info=NULL;
1216 if(dataListIter.hasElement())
1218 info = (IndexInfoForDriver *) dataListIter.nextElement();
1219 FieldValue *v;
1220 for(int i=0;i<22;i++)
1222 switch(i)
1224 case 2:
1226 v = bindFields [i];
1227 AllDataType::copyVal(v->value, info->tableName, v->type, v->length);
1228 break;
1230 case 3:
1232 v = bindFields [i];
1233 if(info->isUnique) { ( *(int*) v->value) = 1; }
1234 else (*(int*) v->value) = 0;
1235 break;
1237 case 5:
1239 v = bindFields [i];
1240 AllDataType::copyVal(v->value,info->indexName, v->type, v->length);
1241 break;
1243 case 6:
1245 v = bindFields [i];
1246 if(info->type ==0) (*(short *) v->value) = SQL_INDEX_HASHED ;
1247 else (*(short *) v->value) = SQL_INDEX_OTHER ;
1248 break;
1250 case 7:
1252 v = bindFields [i];
1253 (*(short *) v->value) = table->getFldPos(info->fieldName) ;
1254 break;
1256 case 8:
1258 v = bindFields [i];
1259 AllDataType::copyVal(v->value, info->fieldName, v->type, v->length);
1260 break;
1262 default:
1263 break;
1266 delete info;
1267 return v->value ;
1268 }else return NULL;
1270 case GetPriIndex:
1272 IndexInfoForDriver *info=NULL;
1273 char name[IDENTIFIER_LENGTH];
1274 while(dataListIter.hasElement())
1276 info = (IndexInfoForDriver *) dataListIter.nextElement();
1277 FieldValue *v = NULL;
1278 //sprintf(name, "%s_idx1_Primary",info->tableName);
1279 //if(strcmp(name, info->indexName) != 0){ continue; }
1280 if( !info->isPrimary ){ continue; }
1281 for(int i=0;i<6;i++)
1283 switch(i)
1285 case 2:
1287 v = bindFields [i];
1288 AllDataType::copyVal(v->value, table->getName(), v->type, v->length);
1289 break;
1291 case 3:
1293 v = bindFields [i];
1294 AllDataType::copyVal(v->value, info->fieldName, v->type, v->length);
1295 break;
1297 case 4:
1299 v = bindFields [i];
1300 (*(short *) v->value) = table->getFldPos(info->fieldName) ;
1301 break;
1303 case 5:
1305 v = bindFields [i];
1306 AllDataType::copyVal(v->value,info->indexName, v->type, v->length);
1307 // printf("Index name %s\n",info->indexName);
1308 break;
1310 default :
1311 break;
1314 delete info;
1315 return v->value;
1317 return NULL;
1319 case GetCatalogs:
1321 FieldValue *v = NULL;
1322 v = bindFields [0];
1323 if(!isSecondFetch)
1325 strcpy((char*)v->value,"csql");
1326 isSecondFetch=true;
1327 return v->value;
1329 free(v->value);
1330 return NULL;
1332 case GetTableType:
1334 FieldValue *v = NULL;
1335 v = bindFields [0];
1336 if(!isSecondFetch)
1338 strcpy((char*)v->value,"TABLE");
1339 isSecondFetch=true;
1340 return v->value;
1342 free(v->value);
1343 return NULL;
1345 case GetImportKey:
1346 case GetExportKey:
1348 ForeignKeyMetadata *info=NULL;
1349 char name[IDENTIFIER_LENGTH];
1350 while(dataListIter.hasElement())
1352 info = (ForeignKeyMetadata *) dataListIter.nextElement();
1353 FieldValue *v = NULL;
1354 for(int i=0;i<14;i++)
1356 switch(i)
1358 case 2:
1360 v = bindFields [i];
1361 AllDataType::copyVal(v->value, info->pkTableName, v->type, v->length);
1362 break;
1364 case 3:
1366 v = bindFields [i];
1367 AllDataType::copyVal(v->value, info->pkColName, v->type, v->length);
1368 break;
1370 case 6:
1372 v = bindFields [i];
1373 AllDataType::copyVal(v->value, info->fkTableName, v->type, v->length);
1374 break;
1376 case 7:
1378 v = bindFields [i];
1379 AllDataType::copyVal(v->value, info->fkColName, v->type, v->length);
1380 break;
1382 case 8:
1384 v = bindFields [i];
1385 (*(short *) v->value) = info->keySeq;
1386 break;
1388 case 9:
1390 v = bindFields [i];
1391 (*(short *) v->value) = info->updateRule;
1392 break;
1394 case 10:
1396 v = bindFields [i];
1397 (*(short *) v->value) = info->deleteRule;
1398 break;
1400 default :
1401 break;
1404 delete info;
1405 return v->value;
1407 return NULL;
1409 case GetDataType:
1411 DataTypeInfo *info=NULL;
1412 char name[IDENTIFIER_LENGTH];
1413 while(dataListIter.hasElement())
1415 info = (DataTypeInfo *) dataListIter.nextElement();
1416 FieldValue *v = NULL;
1417 for(int i=0;i<19;i++)
1419 switch(i)
1421 case 0:
1423 v = bindFields [i];
1424 AllDataType::copyVal(v->value, info->name, v->type, v->length);
1425 break;
1427 case 1:
1429 v = bindFields [i];
1430 int type=0;
1431 type = AllDataType::convertToCSQLSQLType((DataType)info->type);
1432 AllDataType::copyVal(v->value,&type,v->type, v->length);
1433 break;
1435 case 2:
1437 v = bindFields [i];
1438 AllDataType::copyVal(v->value, &info->precision, v->type, v->length);
1439 break;
1441 case 3:
1443 v = bindFields [i];
1444 if( info->type == typeString || info->type == typeDate || info->type == typeTime || info->type == typeTimeStamp )
1445 AllDataType::copyVal(v->value, (void *)"'", v->type, v->length);
1446 else
1447 AllDataType::copyVal(v->value, (void *)"", v->type, v->length);
1448 break;
1450 case 4:
1452 v = bindFields [i];
1453 if( info->type == typeString || info->type == typeDate || info->type == typeTime || info->type == typeTimeStamp )
1454 AllDataType::copyVal(v->value, (void *)"'", v->type, v->length);
1455 else
1456 AllDataType::copyVal(v->value, (void *)"", v->type, v->length);
1457 break;
1459 case 5:
1461 v = bindFields [i];
1462 AllDataType::copyVal(v->value, (void *)"", v->type, v->length);
1463 break;
1465 case 6:
1467 v = bindFields [i];
1468 (*(short *) v->value) = 1;
1469 break;
1471 case 7:
1473 v = bindFields [i];
1474 (*(short *) v->value) = 0;
1475 break;
1477 case 8:
1479 v = bindFields [i];
1480 (*(short *) v->value) = 3;
1481 break;
1483 case 9:
1485 v = bindFields [i];
1486 (*(short *) v->value) = 0;
1487 break;
1489 case 10:
1491 v = bindFields [i];
1492 (*(short *) v->value) = 0;
1493 break;
1495 case 11:
1497 v = bindFields [i];
1498 if(info->autoIncrement)
1499 (*(short *) v->value) = 1;
1500 else
1501 (*(short *) v->value) = 0;
1502 break;
1504 case 12:
1506 v = bindFields [i];
1507 AllDataType::copyVal(v->value, info->name, v->type, v->length);
1508 break;
1510 case 13:
1512 v = bindFields [i];
1513 if(info->type == typeFloat)
1514 (*(short *) v->value) = -38;
1515 else if(info->type == typeDouble)
1516 (*(short *) v->value) = -308;
1517 else
1518 (*(short *) v->value) = 0;
1519 break;
1521 case 14:
1523 v = bindFields [i];
1524 if(info->type == typeFloat)
1525 (*(short *) v->value) = 38;
1526 else if(info->type == typeDouble)
1527 (*(short *) v->value) = 308;
1528 else
1529 (*(short *) v->value) = 0;
1530 break;
1532 case 15:
1534 v = bindFields [i];
1535 (*(short *) v->value) =0; //TODO
1536 break;
1538 case 16:
1540 v = bindFields [i];
1541 (*(short *) v->value) = 0;//TODO
1542 break;
1544 case 17:
1546 v = bindFields [i];
1547 (*(int *) v->value) = 10;
1548 break;
1550 case 18:
1552 v = bindFields [i];
1553 (*(short *) v->value) = 0;
1554 break;
1556 default :
1557 break;
1560 delete info;
1561 return v->value;
1563 return NULL;
1565 default:
1566 break;
1568 return NULL;
1571 ListIterator MetadataStatement::getDataTypeInfoIterator()
1573 List datalist;
1574 for(int i=0; i<10; i++)
1576 DataTypeInfo *typeInfo = new DataTypeInfo();
1577 datalist.append(typeInfo);
1579 DataTypeInfo *info = (DataTypeInfo *)datalist.get(1);
1580 strcpy(info->name,"TINYINT");
1581 info->type = 4;
1582 info->precision = 3;
1583 info->autoIncrement = true;
1584 info = (DataTypeInfo *)datalist.get(2);
1585 strcpy(info->name,"SMALLINT");
1586 info->type =3 ;
1587 info->precision = 5;
1588 info->autoIncrement = true;
1589 info =(DataTypeInfo *) datalist.get(3);
1590 strcpy(info->name,"INT");
1591 info->type = 0 ;
1592 info->precision = 10 ;
1593 info->autoIncrement = true;
1594 info =(DataTypeInfo *) datalist.get(4);
1595 strcpy(info->name,"BIGINT");
1596 info->type =1 ;
1597 info->precision = 19;
1598 info->autoIncrement = true;
1599 info =(DataTypeInfo *) datalist.get(5);
1600 strcpy(info->name,"FLOAT");
1601 info->type = 11;
1602 info->precision = 10;
1603 info->autoIncrement = false;
1604 info =(DataTypeInfo *) datalist.get(6);
1605 strcpy(info->name,"DOUBLE");
1606 info->type =10 ;
1607 info->precision = 17;
1608 info->autoIncrement = false;
1609 info =(DataTypeInfo *) datalist.get(7);
1610 strcpy(info->name,"CHAR");
1611 info->type =30 ;
1612 info->precision = 255;
1613 info->autoIncrement = false;
1614 info =(DataTypeInfo *) datalist.get(8);
1615 strcpy(info->name,"DATE");
1616 info->type =20 ;
1617 info->precision = 0;
1618 info->autoIncrement = false;
1619 info =(DataTypeInfo *) datalist.get(9);
1620 strcpy(info->name,"TIME");
1621 info->type =21 ;
1622 info->precision = 0;
1623 info->autoIncrement = false;
1624 info =(DataTypeInfo *) datalist.get(10);
1625 strcpy(info->name,"TIMESTAMP");
1626 info->type =22;
1627 info->precision = 0;
1628 info->autoIncrement = false;
1629 return datalist.getIterator();