1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.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 ***************************************************************************/
16 #include<CatalogTables.h>
21 char ChunkName
[MAX_CHUNKS
][CHUNK_NAME_LEN
]={"UserChunkTableId","LockTableHashBucketId","LockTableMutexId","LockTableId","TransHasTableId","UndoLogTableId","","","","","DatabaseTableId","UserTableId","TableTableId","FieldTableId","AccessTableId","IndexTableId","IndexFieldTableId","ForeignKeyTableId","ForeignKeyFieldTableId"};
24 DbRetVal
CatalogTableTABLE::insert(const char *name
, int id
, size_t size
,
25 int numFlds
, void* chunk
, void *&tptr
, void *vcchunk
)
27 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
29 tptr
= tChunk
->allocate(systemDatabase_
, &rv
);
33 "Could not allocate memory for for TABLE catalog table");
36 CTABLE
*tableInfo
= (CTABLE
*)tptr
;
37 strcpy(tableInfo
->tblName_
, name
);
38 tableInfo
->tblID_
= id
;
39 tableInfo
->length_
= size
;
40 tableInfo
->numFlds_
= numFlds
;
41 tableInfo
->numIndexes_
= 0;
42 tableInfo
->chunkPtr_
= chunk
;
43 tableInfo
->varcharChunkPtr_
= vcchunk
;
44 printDebug(DM_SystemDatabase
,"One Row inserted into TABLE %x %s",tptr
, name
);
47 DbRetVal
CatalogTableTABLE::renameTable( const char *oldName
,const char *newName
)
49 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
50 ChunkIterator iter
= tChunk
->getIterator();
52 bool isTableExits
= false ;
53 CTABLE
*oldTable
= NULL
;
54 while ((data
= iter
.nextElement())!= NULL
)
56 if (0 == strcmp(((CTABLE
*)data
)->tblName_
, oldName
))
58 oldTable
=(CTABLE
*)data
;
61 if (0 == strcmp(((CTABLE
*)data
)->tblName_
,newName
))
63 printError(ErrNotExists
,"A Table with name %s already exists", newName
);
67 strcpy(oldTable
->tblName_
, newName
);
69 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", oldName
);
73 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
74 iter
= tChunk
->getIterator();
75 char tmpName
[IDENTIFIER_LENGTH
]="";
76 sprintf(tmpName
, "%s_idx1_Primary", oldName
);
77 while ((data
= iter
.nextElement())!= NULL
)
79 if(strcmp(((CINDEX
*)data
)->indName_
,tmpName
)==0) {
80 sprintf(((CINDEX
*)data
)->indName_
, "%s_idx1_Primary", newName
);
86 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
88 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
89 ChunkIterator iter
= tChunk
->getIterator();
92 while ((data
= iter
.nextElement())!= NULL
)
94 if (0 == strcmp(((CTABLE
*)data
)->tblName_
, name
))
96 //remove this element and store the tblPtr
97 //there will be only one row for this table(Primary key)
99 chunk
= (Chunk
*) ((CTABLE
*)data
)->chunkPtr_
;
105 tChunk
->free(systemDatabase_
, tptr
);
106 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
110 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
116 DbRetVal
CatalogTableTABLE::getChunkAndTblPtr(const char *name
,
117 void *&chunk
, void *&tptr
, void *&vcchunk
)
119 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
120 ChunkIterator iter
= chk
->getIterator();;
121 while (NULL
!= (tptr
= iter
.nextElement()))
123 if (strcmp(((CTABLE
*)tptr
)->tblName_
, name
) == 0)
125 //there will be only one row for this table(Primary key)
126 chunk
= (Chunk
*) ((CTABLE
*)tptr
)->chunkPtr_
;
127 vcchunk
= (Chunk
*) ((CTABLE
*)tptr
)->varcharChunkPtr_
;
131 //table not found in TABLE
136 DbRetVal
CatalogTableTABLE::setChunkPtr(const char *name
, void *firstPage
, void *curPage
)
138 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
139 ChunkIterator iter
= chk
->getIterator();;
141 while (NULL
!= (tptr
= iter
.nextElement()))
143 if (strcmp(((CTABLE
*)tptr
)->tblName_
, name
) == 0)
145 //there will be only one row for this table(Primary key)
146 ((Chunk
*)((CTABLE
*)tptr
)->chunkPtr_
)->setFirstPage(firstPage
);
147 ((Chunk
*)((CTABLE
*)tptr
)->chunkPtr_
)->setCurPage(curPage
);
151 //table not found in TABLE
155 List
CatalogTableTABLE::getTableList()
158 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
159 ChunkIterator iter
= chk
->getIterator();
161 while (NULL
!= (tptr
= iter
.nextElement()))
163 Identifier
*elem
= new Identifier();
164 strcpy(elem
->name
, ((CTABLE
*)tptr
)->tblName_
);
165 tableList
.append(elem
);
170 DbRetVal
CatalogTableFIELD::renameField(const char *tableName
, const char *oldName
, const char *newName
)
172 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
173 ChunkIterator iter
= fChunk
->getIterator();
175 bool isFieldExists
=false;
176 while ((data
= iter
.nextElement())!= NULL
)
178 if ((strcmp(((CFIELD
*)data
)->fldName_
,oldName
)== 0) && (strcmp(((CTABLE
*)((CFIELD
*)data
)->tblPtr_
)->tblName_
,tableName
) == 0) )
180 strcpy(((CFIELD
*)data
)->fldName_
,newName
);
181 isFieldExists
= true;
186 printError(ErrNotExists
,"Field %s not exists in table", oldName
);
192 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
194 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
196 while (iter
.hasElement())
198 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
202 "Could not allocate for FIELD catalog table");
205 CFIELD
*fldInfo
= (CFIELD
*)fptr
;
206 FieldDef
*fDef
= iter
.nextElement();
207 strcpy(fldInfo
->fldName_
, fDef
->fldName_
);
208 fldInfo
->tblID_
= tblID
;
209 fldInfo
->tblPtr_
= tptr
;
210 fldInfo
->type_
= fDef
->type_
;
211 fldInfo
->length_
= fDef
->length_
;
212 fldInfo
->offset_
= fDef
->offset_
;
213 os::memcpy(fldInfo
->defaultValueBuf_
, fDef
->defaultValueBuf_
,
214 DEFAULT_VALUE_BUF_LENGTH
);
215 fldInfo
->isNull_
= fDef
->isNull_
;
216 fldInfo
->isPrimary_
= fDef
->isPrimary_
;
217 fldInfo
->isUnique_
= fDef
->isUnique_
;
218 fldInfo
->isDefault_
= fDef
->isDefault_
;
219 fldInfo
->isAutoIncrement_
= fDef
->isAutoIncrement_
;
220 fldInfo
->autoVal_
= 0;
221 fldInfo
->width_
= 0; //TODO
222 fldInfo
->scale_
= 0; //TODO
223 printDebug(DM_SystemDatabase
,"One Row inserted into FIELD %x %s",fldInfo
, fDef
->fldName_
);
229 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
231 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
232 ChunkIterator fIter
= fChunk
->getIterator();
234 while ((data
= fIter
.nextElement())!= NULL
)
236 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
238 //remove this element
239 fChunk
->free(systemDatabase_
, data
);
240 printDebug(DM_SystemDatabase
,"One Row deleted from FIELD %x",data
);
246 void *CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
248 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
249 ChunkIterator fIter
= fChunk
->getIterator();;
252 while (NULL
!= (data
= fIter
.nextElement()))
254 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
256 //add the information to the field list
257 CFIELD
*fTuple
= (CFIELD
*)data
;
259 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
260 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
261 fldDef
.type_
= fTuple
->type_
;
262 fldDef
.length_
= fTuple
->length_
;
263 fldDef
.offset_
= fTuple
->offset_
;
264 fldDef
.isDefault_
= fTuple
->isDefault_
;
265 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
266 DEFAULT_VALUE_BUF_LENGTH
);
267 fldDef
.isNull_
= fTuple
->isNull_
;
268 fldDef
.isUnique_
= fTuple
->isUnique_
;
269 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
270 fldDef
.isAutoIncrement_
= fTuple
->isAutoIncrement_
;
271 if(fTuple
->isAutoIncrement_
){
272 ptrToAutoVal
= &fTuple
->autoVal_
;
273 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
281 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
283 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
290 while (NULL
!= (fName
= fldList
.nextFieldName()))
292 ChunkIterator fIter
= fChunk
->getIterator();
294 while (NULL
!= (data
= fIter
.nextElement()))
296 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
298 if(0 == strcmp((char*)((CFIELD
*)data
)->fldName_
, fName
))
301 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
302 fptr
[i
++] = (char*) data
;
309 printError(ErrNotFound
,
310 "No entries found in FIELD catalog table for the table specified");
317 DbRetVal
CatalogTableINDEX::insert(const char *name
, void *tptr
, int numFlds
, bool isUnique
,
318 void* chunk
, int bucketSize
, void *hChunk
, void *&tupleptr
)
320 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
321 ChunkIterator iter
= tChunk
->getIterator();
323 //Checking for index having same name, proceed further only
324 //if no such indexes are
326 while ((data
= iter
.nextElement())!= NULL
)
328 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
330 printError(ErrAlready
, "Index with name \'%s\' already exists "
331 "on the table \'%s\'.", name
, ((CTABLE
*)tptr
)->tblName_
);
338 tupleptr
= tChunk
->allocate(systemDatabase_
, &rv
);
339 if (NULL
== tupleptr
)
342 "Could not allocate for INDEX catalog table");
345 CINDEX
*indexInfo
= (CINDEX
*)tupleptr
;
346 strcpy(indexInfo
->indName_
, name
);
347 indexInfo
->tblID_
= -1; //Not used currently
348 indexInfo
->tblPtr_
= tptr
;
349 indexInfo
->numFlds_
= numFlds
;
351 indexInfo
->indexType_
= treeIndex
;
353 indexInfo
->indexType_
= hashIndex
;
354 indexInfo
->chunkPtr_
= chunk
;
355 indexInfo
->hashNodeChunk_
= hChunk
;
356 indexInfo
->noOfBuckets_
= bucketSize
;
357 indexInfo
->isUnique_
= isUnique
;
358 indexInfo
->fstIndFld_
=NULL
;
359 printDebug(DM_SystemDatabase
,"One Row inserted into INDEX %x %s",tupleptr
, name
);
363 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
365 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
366 ChunkIterator iter
= fChunk
->getIterator();
369 while ((data
= iter
.nextElement())!= NULL
)
371 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
373 //remove this element and store the tuple ptr
374 //there will be only one row for this table(Primary key)
375 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
376 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
383 fChunk
->free(systemDatabase_
, iptr
);
384 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
388 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
393 DbRetVal
CatalogTableINDEX::get(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
395 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
396 ChunkIterator iter
= fChunk
->getIterator();
399 while ((data
= iter
.nextElement())!= NULL
)
401 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
403 //remove this element and store the tuple ptr
404 //there will be only one row for this table(Primary key)
405 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
406 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
413 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
419 DbRetVal
CatalogTableINDEX::setChunkPtr(const char *name
, ObjectType type
, void *bChunk
, void *firstPage
, void *curPage
)
421 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
422 ChunkIterator iter
= fChunk
->getIterator();
425 while ((data
= iter
.nextElement())!= NULL
)
427 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
429 //remove this element and store the tuple ptr
430 //there will be only one row for this table(Primary key)
432 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(bChunk
);
433 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(bChunk
);
434 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setFirstPage(firstPage
);
435 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setCurPage(curPage
);
436 } else if (type
== tIdx
) {
437 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(firstPage
);
438 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(curPage
);
439 ((CINDEX
*)data
)->hashNodeChunk_
= bChunk
;
447 int CatalogTableINDEX::getNumIndexes(void *tptr
)
449 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
450 ChunkIterator iter
= fChunk
->getIterator();
453 while (NULL
!= (iptr
= iter
.nextElement()))
455 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) numIndex
++;
460 ListIterator
CatalogTableINDEXFIELD::getIndexListIterater(char *name
)
463 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
464 ChunkIterator ifIter
= chunk
->getIterator();
466 while ((data
= ifIter
.nextElement())!= NULL
)
468 IndexInfoForDriver
*idxInfo
= new IndexInfoForDriver();
469 if(strcmp( name
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
) == 0)
471 strcpy(idxInfo
->indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
472 strcpy(idxInfo
->tableName
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
473 strcpy(idxInfo
->fieldName
,((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
474 idxInfo
->type
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
;
475 idxInfo
->isUnique
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->isUnique_
;
476 idxInfo
->isPrimary
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->isPrimary_
;
477 indexList
.append(idxInfo
);
480 return indexList
.getIterator();
483 char* CatalogTableINDEX::getIndexName(void *tptr
, int position
)
485 if (position
== 0) return NULL
;
486 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
487 ChunkIterator iter
= fChunk
->getIterator();
491 while (NULL
!= (iptr
= iter
.nextElement()))
493 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
494 if ( curPos
== position
) return ((CINDEX
*)iptr
)->indName_
;
500 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
503 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
504 ChunkIterator iter
= fChunk
->getIterator();
506 while (NULL
!= (iptr
= iter
.nextElement()))
508 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
)
510 array
[i
++] = (char*) iptr
;
516 ChunkIterator
CatalogTableINDEX::getIterator(void *iptr
)
518 CINDEX
*index
= (CINDEX
*)iptr
;
519 return ((Chunk
*)index
->chunkPtr_
)->getIterator();
523 int CatalogTableINDEX::getNoOfBuckets(void *iptr
)
525 CINDEX
*index
= (CINDEX
*)iptr
;
526 return index
->noOfBuckets_
;
529 int CatalogTableINDEX::getUnique(void *iptr
)
531 CINDEX
*index
= (CINDEX
*)iptr
;
532 return index
->isUnique_
;
534 IndexType
CatalogTableINDEX::getType(void *iptr
)
536 CINDEX
*index
= (CINDEX
*)iptr
;
537 return index
->indexType_
;
539 char* CatalogTableINDEX::getName(void *iptr
)
541 CINDEX
*index
= (CINDEX
*)iptr
;
542 return index
->indName_
;
544 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr
)
546 CINDEX
*index
= (CINDEX
*)iptr
;
547 return ((CFIELD
*)(((CINDEXFIELD
*)(index
->fstIndFld_
))->fieldPtr
))->offset_
;
549 DbRetVal
CatalogTableINDEXFIELD::insert(FieldNameList
&fldList
, void *indexPtr
,
550 void *tblPtr
, char **&fptr
)
554 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
555 ChunkIterator iter
= tChunk
->getIterator();
556 CINDEXFIELD
*fInd
=NULL
;
560 while ((data
= iter
.nextElement())!= NULL
)
562 if ((((CINDEX
*)data
)->tblPtr_
==tblPtr
)
563 && (((CINDEX
*)indexPtr
)->numFlds_
== ((CINDEX
*)data
)->numFlds_
)
564 && (((CINDEX
*)indexPtr
)->indexType_
==((CINDEX
*)data
)->indexType_
)
565 && (data
!= indexPtr
) )
568 while (NULL
!= (fName
= fldList
.nextFieldName()))
571 fInd
=(CINDEXFIELD
*)((CINDEX
*)data
)->fstIndFld_
;
574 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
585 printError(ErrAlready
, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE
*)tblPtr
)->tblName_
, ((CINDEX
*)data
)->indName_
);
592 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
595 while (NULL
!= (fName
= fldList
.nextFieldName()))
598 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
601 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
603 printError(ErrAlready
,"Composite Index Can't be created with same Name");
604 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
610 tChunk
->free(systemDatabase_
,fldI
);
616 void *fieldptr
= tChunk
->allocate(systemDatabase_
, &rv
);
617 if (NULL
== fieldptr
)
619 printError(rv
, "Could not allocate for USER catalog table");
622 CINDEXFIELD
*fldInfo
= (CINDEXFIELD
*)fieldptr
;
623 fldInfo
->tablePtr
= tblPtr
;
624 fldInfo
->fieldPtr
= (CFIELD
*)fptr
[i
++];
625 fldInfo
->indexPtr
= indexPtr
;
626 fldInfo
->next
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
627 ((CINDEX
*)indexPtr
)->fstIndFld_
=fldInfo
;
628 printDebug(DM_SystemDatabase
,"One Row inserted into INDEXFIELD %x", fldInfo
);
633 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
636 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
637 ChunkIterator fIter
= fChunk
->getIterator();
639 while ((data
= fIter
.nextElement())!= NULL
)
641 if (((CINDEXFIELD
*)data
)->indexPtr
== iptr
)
643 //remove this element
644 if(((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
) ((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
= false;
645 fChunk
->free(systemDatabase_
, data
);
646 printDebug(DM_SystemDatabase
,"One Row deleted from INDEXFIELD %x", data
);
652 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
653 char *&name
, DataType
&type
)
656 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
657 ChunkIterator ifIter
= ifChunk
->getIterator();
659 while ((data
= ifIter
.nextElement())!= NULL
)
661 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
663 //store the field name
664 name
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
;
665 type
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->type_
;
669 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
673 DbRetVal
CatalogTableINDEXFIELD::getFieldInfo(void *index
, FieldList
&list
)
676 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
677 ChunkIterator ifIter
= ifChunk
->getIterator();
680 while ((data
= ifIter
.nextElement())!= NULL
)
682 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
684 //add the information to the field list
685 CFIELD
*fTuple
= (CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
);
687 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
688 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
689 fldDef
.type_
= fTuple
->type_
;
690 fldDef
.length_
= fTuple
->length_
;
691 fldDef
.offset_
= fTuple
->offset_
;
692 fldDef
.isDefault_
= fTuple
->isDefault_
;
693 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
694 DEFAULT_VALUE_BUF_LENGTH
);
695 fldDef
.isNull_
= fTuple
->isNull_
;
696 fldDef
.isUnique_
= fTuple
->isUnique_
;
697 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
703 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
709 void CatalogTableINDEXFIELD::printAllIndex()
711 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
712 ChunkIterator ifIter
= chunk
->getIterator();
714 char indexName
[IDENTIFIER_LENGTH
] = {'\0'};
715 while ((data
= ifIter
.nextElement())!= NULL
)
717 if(strcmp(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
)!=0)
719 printf(" <Index Name> %s </Index Name> \n",((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
720 if(0==((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
)
721 printf(" <Index Type> Hash Index </Index Type> \n");
723 printf(" <Index Type> Tree Index </Index Type> \n");
724 printf(" <Table Name> %s </Table Name> \n",((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
725 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
729 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
731 strcpy(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
735 List
CatalogTableUSER::getUserList()
738 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
739 ChunkIterator iter
= chk
->getIterator();
741 while (NULL
!= (tptr
= iter
.nextElement()))
743 Identifier
*elem
= new Identifier();
744 strcpy(elem
->name
, ((CUSER
*)tptr
)->userName_
);
745 userList
.append(elem
);
751 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
753 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
755 ChunkIterator iter
= tChunk
->getIterator();
757 while ((data
= iter
.nextElement())!= NULL
)
759 if (0 == strcmp(((CUSER
*)data
)->userName_
, name
))
761 printError(ErrAlready
, "User with name \'%s\' already exists ", name
);
766 CUSER
*usrInfo
= (CUSER
*)tChunk
->allocate(systemDatabase_
, &rv
);
770 "Could not allocate for USER catalog table");
773 strcpy(usrInfo
->userName_
, name
);
774 strcpy(usrInfo
->password_
, pass
);
775 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
780 DbRetVal
CatalogTableUSER::authenticate(const char *name
, const char *pass
,
781 bool &isAuthenticated
, bool &isDba
)
783 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
784 ChunkIterator iter
= tChunk
->getIterator();
786 while (NULL
!= (data
= iter
.nextElement()))
788 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
790 //verify the password
791 //char * enpass = os::encrypt(pass,"A0");
792 char * enpass
= (char*) pass
;
793 if (0 == strcmp(enpass
, ((CUSER
*)data
)->password_
))
795 isAuthenticated
= true;
796 if (0 == strcmp(((CUSER
*)data
)->userName_
, DBAUSER
))
797 isDba
= true; else isDba
= false;
802 isAuthenticated
= false;
806 DbRetVal
CatalogTableUSER::remove(const char *name
)
808 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
809 ChunkIterator iter
= tChunk
->getIterator();
811 while ((data
= iter
.nextElement())!= NULL
)
813 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
815 //remove this element
816 tChunk
->free(systemDatabase_
, data
);
820 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
824 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
826 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
827 ChunkIterator iter
= tChunk
->getIterator();
829 while (NULL
!= (data
= iter
.nextElement()))
831 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
833 //change the password
834 strcpy(((CUSER
*)data
)->password_
,pass
);// os::encrypt(pass, "A0"));
838 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
842 DbRetVal
CatalogTableFK::insert(char *name
, void *tptr
, void *tPkptr
)
844 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
845 ChunkIterator iter
= tChunk
->getIterator();
847 while ((data
= iter
.nextElement())!= NULL
)
849 if (0 == strcmp(((CFK
*)data
)->fkName_
, name
))
851 printError(ErrAlready
, "Index with name \'%s\' already exists "
852 "on the table \'%s\'.", name
, ((CTABLE
*)tptr
)->tblName_
);
859 void *fkptr
= tChunk
->allocate(systemDatabase_
, &rv
);
862 printError(rv
, "Could not allocate for FK catalog table");
865 CFK
*fkTblInfo
= (CFK
*)fkptr
;
866 strcpy(fkTblInfo
->fkName_
, name
);
867 fkTblInfo
->fkTblPtr_
= tptr
;
868 fkTblInfo
->pkTblPtr_
= tPkptr
;
869 printDebug(DM_SystemDatabase
,"One Row inserted into FK %x %s",fkptr
, name
);
873 DbRetVal
CatalogTableFKFIELD::insert(char *cFKName
, char **fkFldPtrs
, char **pkFldPtrs
,int totalFld
)
875 Chunk
*tChunk
= NULL
;
876 tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
877 ChunkIterator iter
= tChunk
->getIterator();
879 while ((data
= iter
.nextElement())!= NULL
)
881 if (0 == strcmp(((CFK
*)data
)->fkName_
, cFKName
))
886 tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
891 void *fieldptr
= tChunk
->allocate(systemDatabase_
, &rv
);
892 if (NULL
== fieldptr
)
894 printError(rv
, "Could not allocate for USER catalog table");
897 CFKFIELD
*fldInfo
= (CFKFIELD
*)fieldptr
;
898 fldInfo
->fkPtr_
= data
;
899 fldInfo
->pfFldPtr_
= (CFIELD
*)pkFldPtrs
[i
];
900 fldInfo
->fkFldPtr_
= (CFIELD
*)fkFldPtrs
[i
++];
901 //printDebug(DM_TEST,"TYPE %d\n",((CFIELD*)fldInfo->pfFldPtr_)->type_);
902 //printDebug(DM_TEST,"FK name %s\n",((CFIELD*)fldInfo->fkFldPtr_)->fldName_);
903 if(!(((CFIELD
*)fldInfo
->pfFldPtr_
)->isUnique_
) || !(((CFIELD
*)fldInfo
->pfFldPtr_
)->isNull_
))
905 printError(ErrSysInternal
,"Parent Table field should have primary key field ");
906 tChunk
->free(systemDatabase_
,fieldptr
);
907 return ErrSysInternal
;
909 if(((CFIELD
*)fldInfo
->pfFldPtr_
)->type_
!=((CFIELD
*)fldInfo
->fkFldPtr_
)->type_
)
911 printError(ErrSysInternal
,"Type Missmatch in both PK field and FK field ");
912 tChunk
->free(systemDatabase_
,fieldptr
);
913 return ErrSysInternal
;
915 printDebug(DM_SystemDatabase
,"One Row inserted into FKFIELD %x", fldInfo
);
919 DbRetVal
CatalogTableFK::remove(void *ctptr
)
921 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
922 ChunkIterator iter
= tChunk
->getIterator();
924 while ((data
= iter
.nextElement())!= NULL
)
928 tChunk
->free(systemDatabase_
,data
);
929 printDebug(DM_SystemDatabase
,"One Row deleted from FKFIELD %x", data
);
936 DbRetVal
CatalogTableFKFIELD::remove(void *cFKfld
)
938 Chunk
*fChunk
= NULL
;
939 fChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
940 ChunkIterator iter
= fChunk
->getIterator();
942 while ((data
= iter
.nextElement())!= NULL
)
944 if (((CFKFIELD
*)data
)->fkPtr_
== cFKfld
)
946 fChunk
->free(systemDatabase_
, data
);
947 printDebug(DM_SystemDatabase
,"One Row deleted from CFKFIELD %x", data
);
952 void *CatalogTableFK::getFkCTable(void *ctptr
)
954 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
955 ChunkIterator iter
= tChunk
->getIterator();
957 while ((data
= iter
.nextElement())!= NULL
)
959 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
967 int CatalogTableFK::getNumFkTable(void *ctptr
)
969 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
970 ChunkIterator iter
= tChunk
->getIterator();
973 while ((data
= iter
.nextElement())!= NULL
)
975 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
983 bool CatalogTableFK::isFkTable(void *ctptr
)
985 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
986 ChunkIterator iter
= tChunk
->getIterator();
989 while ((data
= iter
.nextElement())!= NULL
)
991 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
998 int CatalogTableFK::getNoOfFkTable(void *ctptr
)
1000 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1001 ChunkIterator iter
= tChunk
->getIterator();
1004 while ((data
= iter
.nextElement())!= NULL
)
1006 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
1014 int CatalogTableFK::getNoOfPkTable(void *ctptr
)
1016 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1017 ChunkIterator iter
= tChunk
->getIterator();
1020 while ((data
= iter
.nextElement())!= NULL
)
1022 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
1030 void CatalogTableFK::getPkTableName(void *ctptr
,char **&array
)
1032 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1033 ChunkIterator iter
= tChunk
->getIterator();
1036 while ((data
= iter
.nextElement())!= NULL
)
1038 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
1040 array
[i
++] = ((CTABLE
*)((CFK
*)data
)->pkTblPtr_
)->tblName_
;
1044 void CatalogTableFK::getFkTableName(void *ctptr
,char **&array
)
1046 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1047 ChunkIterator iter
= tChunk
->getIterator();
1050 while ((data
= iter
.nextElement())!= NULL
)
1052 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
1054 array
[i
++] = ((CTABLE
*)((CFK
*)data
)->fkTblPtr_
)->tblName_
;
1060 DbRetVal
CatalogTableFK::getPkFkFieldInfo(void *cpkptr
, void *cfkptr
, FieldNameList
&pklist
,FieldNameList
&fklist
)
1062 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1063 ChunkIterator iter
= tChunk
->getIterator();
1065 while ((data
= iter
.nextElement())!= NULL
)
1067 if (((CFK
*)data
)->pkTblPtr_
== cpkptr
&& ((CFK
*)data
)->fkTblPtr_
== cfkptr
)
1074 printError(ErrNotExists
,"Foreign Key field CFK not found");
1075 return ErrNotExists
;
1077 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
1078 iter
= fChunk
->getIterator();
1080 while ((fdata
= iter
.nextElement())!= NULL
)
1082 if (((CFKFIELD
*)fdata
)->fkPtr_
==data
)
1084 //printDebug(DM_TEST,"PK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1085 //printDebug(DM_TEST,"FK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1086 pklist
.append(((CFIELD
*)((CFKFIELD
*)fdata
)->pfFldPtr_
)->fldName_
);
1087 fklist
.append(((CFIELD
*)((CFKFIELD
*)fdata
)->fkFldPtr_
)->fldName_
);