1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
17 #include <Statement.h>
18 #include <TableImpl.h>
19 #include <OrderTableImpl.h>
22 ListIterator
MetadataStatement::getExportKeyIterator()
25 TableImpl
*tbl
= (TableImpl
*)table
;
28 return datalist
.getIterator();
31 TableImpl
*fkTbl
=NULL
;
32 ListIterator tblIter
=tbl
->tblFkList
.getIterator();
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();
51 while((fkFldName
= fkFieldList
.nextFieldName())!= NULL
)
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()
74 TableImpl
*tbl
= (TableImpl
*)table
;
77 return datalist
.getIterator();
80 TableImpl
*fkTbl
=NULL
;
81 ListIterator tblIter
= tbl
->tblList
.getIterator();
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();
100 while((pkFldName
= pkFieldList
.nextFieldName())!= NULL
)
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()
130 bindFieldValues
= NULL
;
138 MetadataStatement::~MetadataStatement()
141 if (table
) { table
->close(); table
= 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];
163 printError(ErrSysFatal
, "condition value is null. Should never happen");
166 table
->getFieldNameAlone(cValue
->fName
,info
->fldName
);
167 info
->type
= cValue
->type
;
168 info
->length
= cValue
->length
;
169 info
->isNull
= cValue
->isNullable
;
172 DbRetVal
MetadataStatement::execute(int &rowsAffected
)
179 dataListIter
= dbMgr
->getAllTableNames().getIterator();
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
)
197 printError(ErrSysFatal
, "Should never happen.");
201 colList
.append(fInfo
);
205 dataListIter
= colList
.getIterator();
211 CatalogTableINDEXFIELD
cIndexField(((DatabaseManagerImpl
*)dbMgr
)->sysDb());
212 dataListIter
= cIndexField
.getIndexListIterater(table
->getName());
218 CatalogTableINDEXFIELD
cIndexField(((DatabaseManagerImpl
*)dbMgr
)->sysDb());
219 dataListIter
= cIndexField
.getIndexListIterater(table
->getName());
224 dataListIter
= getImportKeyIterator();
229 dataListIter
= getExportKeyIterator();
234 dataListIter
= getDataTypeInfoIterator();
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
);
260 printError(ErrNotExists
,
261 "Unable to open the table:Table not exists");
262 if (prevHdl
) delete prevHdl
;
265 TableImpl
*tImpl
= (TableImpl
*) tHdl
;
269 jHdl
= new JoinTableImpl();
270 jHdl
->setTable(prevHdl
, tHdl
);
271 JoinTypeNode
*node
= (JoinTypeNode
*) jiter
.nextElement();
273 printError(ErrSyntaxError
,
274 "Join Type and number of tables do not match");
276 return ErrSyntaxError
;
278 jHdl
->setJoinType(node
->jType
);
285 if (joinInvolved
) table
= jHdl
; else table
= tHdl
;
288 DbRetVal
MetadataStatement::resolve()
291 plan
= parsedData
->getResultSetPlan();
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
;
306 newVal
->type
= typeString
;
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;
315 bindFields
[i
] = newVal
;
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();
329 strcpy(newVal
->fldName
,"TABLE_CAT");
330 newVal
->type
= typeString
;
333 newVal
->type
= typeString
;
334 strcpy(newVal
->fldName
,"TABLE_SCHEM");
337 newVal
->type
= typeString
;
338 strcpy(newVal
->fldName
,"TABLE_NAME");
341 newVal
->type
= typeString
;
342 strcpy(newVal
->fldName
,"COLUMN_NAME");
345 newVal
->type
= typeInt
;
346 strcpy(newVal
->fldName
,"DATA_TYPE");
349 newVal
->type
= typeString
;
350 strcpy(newVal
->fldName
,"TYPE_NAME");
353 newVal
->type
= typeInt
;
354 strcpy(newVal
->fldName
,"COLUMN_SIZE");
357 newVal
->type
= typeInt
;
358 strcpy(newVal
->fldName
,"BUFFER_LENGTH");
361 newVal
->type
= typeShort
;
362 strcpy(newVal
->fldName
,"DECIMAL_DIGITS");
365 newVal
->type
= typeShort
;
366 strcpy(newVal
->fldName
,"NUM_PREC_RADIX");
369 newVal
->type
= typeShort
;
370 strcpy(newVal
->fldName
,"NULLABLE");
373 newVal
->type
= typeString
;
374 strcpy(newVal
->fldName
,"REMARKS");
377 newVal
->type
= typeString
;
378 strcpy(newVal
->fldName
,"COLUMN_DEF");
381 newVal
->type
= typeShort
;
382 strcpy(newVal
->fldName
,"SQL_DATA_TYPE");
385 newVal
->type
= typeInt
;
386 strcpy(newVal
->fldName
,"SQL_DATETIME_SUB");
389 newVal
->type
= typeInt
;
390 strcpy(newVal
->fldName
,"CHAR_OCTET_LENGTH");
393 newVal
->type
= typeInt
;
394 strcpy(newVal
->fldName
,"ORDINAL_POSITION");
397 newVal
->type
= typeString
;
398 strcpy(newVal
->fldName
,"IS_NULLABLE");
401 newVal
->type
= typeString
;
402 strcpy(newVal
->fldName
,"SCOPE_CATLOG");
405 newVal
->type
= typeString
;
406 strcpy(newVal
->fldName
,"SCOPE_SCHEMA");
409 newVal
->type
= typeString
;
410 strcpy(newVal
->fldName
,"SCOPE_TABLE");
413 newVal
->type
= typeShort
;
414 strcpy(newVal
->fldName
,"SOURCE_DATA_TYPE");
417 newVal
->type
= typeString
;
418 strcpy(newVal
->fldName
,"IS_AUTOINCREMENT");
421 newVal
->type
= typeString
;
424 newVal
->parsedString
= NULL
;
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;
439 bindFields
[i
] = newVal
;
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();
454 strcpy(newVal
->fldName
,"TABLE_CAT");
455 newVal
->type
= typeString
;
458 newVal
->type
= typeString
;
459 strcpy(newVal
->fldName
,"TABLE_SCHEM");
462 newVal
->type
= typeString
;
463 strcpy(newVal
->fldName
,"TABLE_NAME");
466 newVal
->type
= typeInt
;
467 strcpy(newVal
->fldName
,"NON_UNIQUE");
470 newVal
->type
= typeString
;
471 strcpy(newVal
->fldName
,"INDEX_QUALIFIER");
474 newVal
->type
= typeString
;
475 strcpy(newVal
->fldName
,"INDEX_NAME");
478 newVal
->type
= typeShort
;
479 strcpy(newVal
->fldName
,"TYPE");
482 newVal
->type
= typeShort
;
483 strcpy(newVal
->fldName
,"ORDINAL_POSITION");
486 newVal
->type
= typeString
;
487 strcpy(newVal
->fldName
,"COLUMN_NAME");
490 newVal
->type
= typeString
;
491 strcpy(newVal
->fldName
,"ASC_OR_DESC");
494 newVal
->type
= typeInt
;
495 strcpy(newVal
->fldName
,"CARDINALITY");
498 newVal
->type
= typeInt
;
499 strcpy(newVal
->fldName
,"PAGES");
502 newVal
->type
= typeString
;
503 strcpy(newVal
->fldName
,"FILTER_CONDITION");
506 newVal
->type
= typeString
;
509 newVal
->parsedString
= NULL
;
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;
518 bindFields
[i
] = newVal
;
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();
533 strcpy(newVal
->fldName
,"TABLE_CAT");
534 newVal
->type
= typeString
;
537 newVal
->type
= typeString
;
538 strcpy(newVal
->fldName
,"TABLE_SCHEM");
541 newVal
->type
= typeString
;
542 strcpy(newVal
->fldName
,"TABLE_NAME");
545 newVal
->type
= typeString
;
546 strcpy(newVal
->fldName
,"COLUMN_NAME");
549 newVal
->type
= typeShort
;
550 strcpy(newVal
->fldName
,"KEY_SEQ");
553 newVal
->type
= typeString
;
554 strcpy(newVal
->fldName
,"PK_NAME");
559 newVal
->parsedString
= NULL
;
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;
567 bindFields
[i
] = newVal
;
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
;
580 newVal
->length
= AllDataType::size(newVal
->type
, 64);
581 newVal
->value
= AllDataType::alloc(newVal
->type
, newVal
->length
);
582 newVal
->isAllocVal
=true;
584 bindFields
[0] = newVal
;
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
;
596 newVal
->length
= AllDataType::size(newVal
->type
, 64);
597 newVal
->value
= AllDataType::alloc(newVal
->type
, newVal
->length
);
598 newVal
->isAllocVal
=true;
600 bindFields
[0] = newVal
;
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();
613 strcpy(newVal
->fldName
,"TYPE_NAME");
614 newVal
->type
= typeString
;
617 newVal
->type
= typeInt
;
618 strcpy(newVal
->fldName
,"DATA_TYPE");
621 strcpy(newVal
->fldName
,"PRECISION");
622 newVal
->type
= typeInt
;
625 strcpy(newVal
->fldName
,"LITERAL_PREFIX");
626 newVal
->type
= typeString
;
629 strcpy(newVal
->fldName
,"LITERAL_SUFFIX");
630 newVal
->type
= typeString
;
633 strcpy(newVal
->fldName
,"CREATE_PARAMS");
634 newVal
->type
= typeString
;
637 strcpy(newVal
->fldName
,"NULLABLE");
638 newVal
->type
= typeShort
;
641 strcpy(newVal
->fldName
,"CASE_SENSITIVE");
642 newVal
->type
= typeInt
;
645 strcpy(newVal
->fldName
,"SEARCHABLE");
646 newVal
->type
= typeShort
;
649 strcpy(newVal
->fldName
,"UNSIGNED_ATTRIBUTE");
650 newVal
->type
= typeInt
;
653 strcpy(newVal
->fldName
,"FIXED_PREC_SCALE");
654 newVal
->type
= typeInt
;
657 strcpy(newVal
->fldName
,"AUTO_INCREMENT");
658 newVal
->type
= typeInt
;
661 strcpy(newVal
->fldName
,"LOCAL_TYPE_NAME");
662 newVal
->type
= typeString
;
665 strcpy(newVal
->fldName
,"MINIMUM_SCALE");
666 newVal
->type
= typeShort
;
669 strcpy(newVal
->fldName
,"MAXIMUM_SCALE");
670 newVal
->type
= typeShort
;
673 strcpy(newVal
->fldName
,"SQL_DATA_TYPE");
674 newVal
->type
= typeInt
;
677 strcpy(newVal
->fldName
,"SQL_DATETIME_SUB");
678 newVal
->type
= typeInt
;
681 strcpy(newVal
->fldName
,"NUM_PREC_RADIX");
682 newVal
->type
= typeInt
;
685 strcpy(newVal
->fldName
,"INTERVAL_PRECISION");
686 newVal
->type
= typeShort
;
691 newVal
->parsedString
= NULL
;
693 newVal
->length
= AllDataType::size(newVal
->type
, 64);
694 newVal
->value
= AllDataType::alloc(newVal
->type
,newVal
->length
);
695 newVal
->isAllocVal
= true;
697 bindFields
[i
] = newVal
;
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();
712 strcpy(newVal
->fldName
,"PKTABLE_CAT");
713 newVal
->type
= typeString
;
716 newVal
->type
= typeString
;
717 strcpy(newVal
->fldName
,"PKTABLE_SCHEM");
720 newVal
->type
= typeString
;
721 strcpy(newVal
->fldName
,"PKTABLE_NAME");
724 newVal
->type
= typeString
;
725 strcpy(newVal
->fldName
,"PKCOLUMN_NAME");
728 newVal
->type
= typeString
;
729 strcpy(newVal
->fldName
,"FKTABLE_CAT");
732 newVal
->type
= typeString
;
733 strcpy(newVal
->fldName
,"FKTABLE_SCHEM");
736 newVal
->type
= typeString
;
737 strcpy(newVal
->fldName
,"FKTABLE_NAME");
740 newVal
->type
= typeString
;
741 strcpy(newVal
->fldName
,"FKCOLUMN_NAME");
744 newVal
->type
= typeShort
;
745 strcpy(newVal
->fldName
,"KEY_SEQ");
748 newVal
->type
= typeShort
;
749 strcpy(newVal
->fldName
,"UPDATE_RULE");
752 newVal
->type
= typeShort
;
753 strcpy(newVal
->fldName
,"DELETE_RULE");
756 newVal
->type
= typeString
;
757 strcpy(newVal
->fldName
,"FK_NAME");
760 newVal
->type
= typeString
;
761 strcpy(newVal
->fldName
,"PK_NAME");
764 newVal
->type
= typeShort
;
765 strcpy(newVal
->fldName
,"DEFERRABILITY");
767 newVal
->type
= typeString
;
770 newVal
->parsedString
= NULL
;
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;
778 bindFields
[i
] = newVal
;
788 void* MetadataStatement::fetch()
792 tuple
= fetchMetaData();
793 if (NULL
== tuple
) return NULL
;
795 for (int i
= 0; i
< totalFields
; i
++)
797 value
= bindFields
[i
];
798 if (bindFieldValues
[i
] == NULL
)
802 AllDataType::copyVal(bindFieldValues
[i
], value
->value
, value
->type
, value
->length
);
807 void* MetadataStatement::fetch(DbRetVal
&rv
)
810 tuple
= fetchMetaData();
811 if (NULL
== tuple
) return NULL
;
813 for (int i
= 0; i
< totalFields
; i
++)
815 value
= bindFields
[i
];
816 if (bindFieldValues
[i
] == NULL
)
820 AllDataType::copyVal(bindFieldValues
[i
], value
->value
, value
->type
, value
->length
);
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
);
839 FieldValue
*newVal
= NULL
;
841 for(int i
=0;i
<5;i
++){
842 newVal
=bindFields
[i
];
843 if (newVal
->isAllocVal
) free(newVal
->value
);
853 for(int i
=0;i
<23;i
++){
854 newVal
=bindFields
[i
];
855 if ( newVal
->isAllocVal
) free(newVal
->value
);
865 for(int i
=0;i
<13;i
++){
866 newVal
=bindFields
[i
];
867 if ( newVal
->isAllocVal
) free(newVal
->value
);
877 for(int i
=0;i
<6;i
++){
878 newVal
=bindFields
[i
];
879 if ( newVal
->isAllocVal
) free(newVal
->value
);
889 for(int i
=0;i
<1;i
++){
890 newVal
=bindFields
[i
];
891 // if ( newVal->isAllocVal) free(newVal->value);
901 for(int i
=0;i
<1;i
++){
902 newVal
=bindFields
[i
];
903 // if ( newVal->isAllocVal) free(newVal->value);
913 for(int i
=0;i
<18;i
++){
914 newVal
=bindFields
[i
];
915 if ( newVal
->isAllocVal
) free(newVal
->value
);
925 for(int i
=0;i
<14;i
++){
926 newVal
=bindFields
[i
];
927 if ( newVal
->isAllocVal
) free(newVal
->value
);
937 for(int i
=0;i
<14;i
++){
938 newVal
=bindFields
[i
];
939 if ( newVal
->isAllocVal
) free(newVal
->value
);
950 DbRetVal
MetadataStatement::close()
952 if(plan
!=Normal
){ freeMetadata(); }
953 if (table
) return table
->closeScan();
958 char* MetadataStatement::getFieldName ( int pos
)
960 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
962 while (iter
.hasElement())
964 if (position
== pos
) {
965 FieldName
*name
= (FieldName
*) iter
.nextElement();
968 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
971 return name
->fldName
;
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
)
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
)
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
);
1024 void MetadataStatement::getProjFieldType(int *data
)
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
;
1043 int MetadataStatement::noOfProjFields()
1048 DbRetVal
MetadataStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
1054 FieldValue
*v
= bindFields
[projpos
-1];
1055 strcpy(fInfo
->fldName
, v
->fldName
);
1056 fInfo
->type
= v
->type
;
1057 fInfo
->length
= v
->length
;
1060 ListIterator iter
= parsedData
->getFieldNameList().getIterator();
1061 FieldName
*name
= NULL
;
1063 while (iter
.hasElement())
1065 name
= (FieldName
*)iter
.nextElement();
1068 printError(ErrSysFatal
, "Should never happen. Field Name list has NULL");
1071 if (position
== (projpos
-1)) break;
1075 rv
= table
->getFieldInfo(name
->fldName
, fInfo
);
1078 //get back the qualified name(tablename.fldname)
1079 char qualName
[IDENTIFIER_LENGTH
];
1080 strcpy(qualName
, name
->fldName
);
1084 sprintf(fInfo
->fldName
, "COUNT(%s)", qualName
);
1085 fInfo
->type
= typeInt
;
1086 fInfo
->length
= sizeof(int);
1089 sprintf(fInfo
->fldName
, "MIN(%s)", qualName
);
1092 sprintf(fInfo
->fldName
, "MAX(%s)", qualName
);
1095 sprintf(fInfo
->fldName
, "SUM(%s)", qualName
);
1098 sprintf(fInfo
->fldName
, "AVG(%s)", qualName
);
1099 fInfo
->type
= typeDouble
;
1100 fInfo
->length
= sizeof(double);
1103 strcpy(fInfo
->fldName
, qualName
);
1109 int MetadataStatement::getFldPos(char *name
)
1111 return table
->getFldPos(name
);
1114 void *MetadataStatement::fetchMetaData()
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
);
1132 FieldInfo
*info
= NULL
;
1133 if(dataListIter
.hasElement())
1135 info
= (FieldInfo
*) dataListIter
.nextElement();
1137 for(int i
=0;i
<23;i
++)
1144 AllDataType::copyVal(v
->value
, info
->fldName
, v
->type
, v
->length
);
1151 type
= AllDataType::convertToCSQLSQLType(info
->type
);
1152 AllDataType::copyVal(v
->value
,&type
,v
->type
, v
->length
);
1158 AllDataType::copyVal(v
->value
,AllDataType::getSQLString(info
->type
),v
->type
, v
->length
);
1165 AllDataType::copyVal(v
->value
,&info
->length
,v
->type
, v
->length
);
1172 (*(short*)v
->value
)=0;
1174 (*(short*)v
->value
)=1;
1180 AllDataType::copyVal(v
->value
,info
->defaultValueBuf
,v
->type
, v
->length
);
1186 (*(int*)v
->value
) = table
->getFldPos(info
->fldName
);
1193 AllDataType::copyVal(v
->value
,(void *)"NO",v
->type
, v
->length
);
1195 AllDataType::copyVal(v
->value
,(void *)"YES",v
->type
, v
->length
);
1200 if(info
->isAutoIncrement
)
1201 AllDataType::copyVal(v
->value
,(void *)"YES",v
->type
, v
->length
);
1203 AllDataType::copyVal(v
->value
,(void *)"NO",v
->type
, v
->length
);
1215 IndexInfoForDriver
*info
=NULL
;
1216 if(dataListIter
.hasElement())
1218 info
= (IndexInfoForDriver
*) dataListIter
.nextElement();
1220 for(int i
=0;i
<22;i
++)
1227 AllDataType::copyVal(v
->value
, info
->tableName
, v
->type
, v
->length
);
1233 if(info
->isUnique
) { ( *(int*) v
->value
) = 1; }
1234 else (*(int*) v
->value
) = 0;
1240 AllDataType::copyVal(v
->value
,info
->indexName
, v
->type
, v
->length
);
1246 if(info
->type
==0) (*(short *) v
->value
) = SQL_INDEX_HASHED
;
1247 else (*(short *) v
->value
) = SQL_INDEX_OTHER
;
1253 (*(short *) v
->value
) = table
->getFldPos(info
->fieldName
) ;
1259 AllDataType::copyVal(v
->value
, info
->fieldName
, v
->type
, v
->length
);
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
++)
1288 AllDataType::copyVal(v
->value
, table
->getName(), v
->type
, v
->length
);
1294 AllDataType::copyVal(v
->value
, info
->fieldName
, v
->type
, v
->length
);
1300 (*(short *) v
->value
) = table
->getFldPos(info
->fieldName
) ;
1306 AllDataType::copyVal(v
->value
,info
->indexName
, v
->type
, v
->length
);
1307 // printf("Index name %s\n",info->indexName);
1321 FieldValue
*v
= NULL
;
1325 strcpy((char*)v
->value
,"csql");
1334 FieldValue
*v
= NULL
;
1338 strcpy((char*)v
->value
,"TABLE");
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
++)
1361 AllDataType::copyVal(v
->value
, info
->pkTableName
, v
->type
, v
->length
);
1367 AllDataType::copyVal(v
->value
, info
->pkColName
, v
->type
, v
->length
);
1373 AllDataType::copyVal(v
->value
, info
->fkTableName
, v
->type
, v
->length
);
1379 AllDataType::copyVal(v
->value
, info
->fkColName
, v
->type
, v
->length
);
1385 (*(short *) v
->value
) = info
->keySeq
;
1391 (*(short *) v
->value
) = info
->updateRule
;
1397 (*(short *) v
->value
) = info
->deleteRule
;
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
++)
1424 AllDataType::copyVal(v
->value
, info
->name
, v
->type
, v
->length
);
1431 type
= AllDataType::convertToCSQLSQLType((DataType
)info
->type
);
1432 AllDataType::copyVal(v
->value
,&type
,v
->type
, v
->length
);
1438 AllDataType::copyVal(v
->value
, &info
->precision
, v
->type
, v
->length
);
1444 if( info
->type
== typeString
|| info
->type
== typeDate
|| info
->type
== typeTime
|| info
->type
== typeTimeStamp
)
1445 AllDataType::copyVal(v
->value
, (void *)"'", v
->type
, v
->length
);
1447 AllDataType::copyVal(v
->value
, (void *)"", v
->type
, v
->length
);
1453 if( info
->type
== typeString
|| info
->type
== typeDate
|| info
->type
== typeTime
|| info
->type
== typeTimeStamp
)
1454 AllDataType::copyVal(v
->value
, (void *)"'", v
->type
, v
->length
);
1456 AllDataType::copyVal(v
->value
, (void *)"", v
->type
, v
->length
);
1462 AllDataType::copyVal(v
->value
, (void *)"", v
->type
, v
->length
);
1468 (*(short *) v
->value
) = 1;
1474 (*(short *) v
->value
) = 0;
1480 (*(short *) v
->value
) = 3;
1486 (*(short *) v
->value
) = 0;
1492 (*(short *) v
->value
) = 0;
1498 if(info
->autoIncrement
)
1499 (*(short *) v
->value
) = 1;
1501 (*(short *) v
->value
) = 0;
1507 AllDataType::copyVal(v
->value
, info
->name
, v
->type
, v
->length
);
1513 if(info
->type
== typeFloat
)
1514 (*(short *) v
->value
) = -38;
1515 else if(info
->type
== typeDouble
)
1516 (*(short *) v
->value
) = -308;
1518 (*(short *) v
->value
) = 0;
1524 if(info
->type
== typeFloat
)
1525 (*(short *) v
->value
) = 38;
1526 else if(info
->type
== typeDouble
)
1527 (*(short *) v
->value
) = 308;
1529 (*(short *) v
->value
) = 0;
1535 (*(short *) v
->value
) =0; //TODO
1541 (*(short *) v
->value
) = 0;//TODO
1547 (*(int *) v
->value
) = 10;
1553 (*(short *) v
->value
) = 0;
1571 ListIterator
MetadataStatement::getDataTypeInfoIterator()
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");
1582 info
->precision
= 3;
1583 info
->autoIncrement
= true;
1584 info
= (DataTypeInfo
*)datalist
.get(2);
1585 strcpy(info
->name
,"SMALLINT");
1587 info
->precision
= 5;
1588 info
->autoIncrement
= true;
1589 info
=(DataTypeInfo
*) datalist
.get(3);
1590 strcpy(info
->name
,"INT");
1592 info
->precision
= 10 ;
1593 info
->autoIncrement
= true;
1594 info
=(DataTypeInfo
*) datalist
.get(4);
1595 strcpy(info
->name
,"BIGINT");
1597 info
->precision
= 19;
1598 info
->autoIncrement
= true;
1599 info
=(DataTypeInfo
*) datalist
.get(5);
1600 strcpy(info
->name
,"FLOAT");
1602 info
->precision
= 10;
1603 info
->autoIncrement
= false;
1604 info
=(DataTypeInfo
*) datalist
.get(6);
1605 strcpy(info
->name
,"DOUBLE");
1607 info
->precision
= 17;
1608 info
->autoIncrement
= false;
1609 info
=(DataTypeInfo
*) datalist
.get(7);
1610 strcpy(info
->name
,"CHAR");
1612 info
->precision
= 255;
1613 info
->autoIncrement
= false;
1614 info
=(DataTypeInfo
*) datalist
.get(8);
1615 strcpy(info
->name
,"DATE");
1617 info
->precision
= 0;
1618 info
->autoIncrement
= false;
1619 info
=(DataTypeInfo
*) datalist
.get(9);
1620 strcpy(info
->name
,"TIME");
1622 info
->precision
= 0;
1623 info
->autoIncrement
= false;
1624 info
=(DataTypeInfo
*) datalist
.get(10);
1625 strcpy(info
->name
,"TIMESTAMP");
1627 info
->precision
= 0;
1628 info
->autoIncrement
= false;
1629 return datalist
.getIterator();