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_
,newName
)== 0) && (strcmp(((CTABLE
*)((CFIELD
*)data
)->tblPtr_
)->tblName_
,tableName
) == 0) )
180 printError(ErrAlready
,
181 "New Field Name '%s' already exists in the table.", newName
);
185 iter
= fChunk
->getIterator();
186 while ((data
= iter
.nextElement())!= NULL
)
188 if ((strcmp(((CFIELD
*)data
)->fldName_
,oldName
)== 0) && (strcmp(((CTABLE
*)((CFIELD
*)data
)->tblPtr_
)->tblName_
,tableName
) == 0) )
190 strcpy(((CFIELD
*)data
)->fldName_
,newName
);
191 isFieldExists
= true;
196 printError(ErrNotExists
, "Old Field Name '%s' does not exist in table",
203 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
205 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
207 while (iter
.hasElement())
209 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
213 "Could not allocate for FIELD catalog table");
216 CFIELD
*fldInfo
= (CFIELD
*)fptr
;
217 FieldDef
*fDef
= iter
.nextElement();
218 strcpy(fldInfo
->fldName_
, fDef
->fldName_
);
219 fldInfo
->tblID_
= tblID
;
220 fldInfo
->tblPtr_
= tptr
;
221 fldInfo
->type_
= fDef
->type_
;
222 fldInfo
->length_
= fDef
->length_
;
223 fldInfo
->offset_
= fDef
->offset_
;
224 os::memcpy(fldInfo
->defaultValueBuf_
, fDef
->defaultValueBuf_
,
225 DEFAULT_VALUE_BUF_LENGTH
);
226 fldInfo
->isNull_
= fDef
->isNull_
;
227 fldInfo
->isPrimary_
= fDef
->isPrimary_
;
228 fldInfo
->isUnique_
= fDef
->isUnique_
;
229 fldInfo
->isDefault_
= fDef
->isDefault_
;
230 fldInfo
->isAutoIncrement_
= fDef
->isAutoIncrement_
;
231 fldInfo
->autoVal_
= 0;
232 fldInfo
->width_
= 0; //TODO
233 fldInfo
->scale_
= 0; //TODO
234 printDebug(DM_SystemDatabase
,"One Row inserted into FIELD %x %s",fldInfo
, fDef
->fldName_
);
240 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
242 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
243 ChunkIterator fIter
= fChunk
->getIterator();
245 while ((data
= fIter
.nextElement())!= NULL
)
247 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
249 //remove this element
250 fChunk
->free(systemDatabase_
, data
);
251 printDebug(DM_SystemDatabase
,"One Row deleted from FIELD %x",data
);
257 void *CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
259 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
260 ChunkIterator fIter
= fChunk
->getIterator();;
263 while (NULL
!= (data
= fIter
.nextElement()))
265 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
267 //add the information to the field list
268 CFIELD
*fTuple
= (CFIELD
*)data
;
270 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
271 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
272 fldDef
.type_
= fTuple
->type_
;
273 fldDef
.length_
= fTuple
->length_
;
274 fldDef
.offset_
= fTuple
->offset_
;
275 fldDef
.isDefault_
= fTuple
->isDefault_
;
276 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
277 DEFAULT_VALUE_BUF_LENGTH
);
278 fldDef
.isNull_
= fTuple
->isNull_
;
279 fldDef
.isUnique_
= fTuple
->isUnique_
;
280 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
281 fldDef
.isAutoIncrement_
= fTuple
->isAutoIncrement_
;
282 if(fTuple
->isAutoIncrement_
){
283 ptrToAutoVal
= &fTuple
->autoVal_
;
284 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
292 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
294 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
301 while (NULL
!= (fName
= fldList
.nextFieldName()))
303 ChunkIterator fIter
= fChunk
->getIterator();
305 while (NULL
!= (data
= fIter
.nextElement()))
307 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
309 if(0 == strcmp((char*)((CFIELD
*)data
)->fldName_
, fName
))
312 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
313 fptr
[i
++] = (char*) data
;
320 printError(ErrNotFound
,
321 "No entries found in FIELD catalog table for the table specified");
328 DbRetVal
CatalogTableINDEX::insert(const char *name
, void *tptr
, int numFlds
, bool isUnique
,
329 void* chunk
, int bucketSize
, void *hChunk
, void *&tupleptr
)
331 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
332 ChunkIterator iter
= tChunk
->getIterator();
334 //Checking for index having same name, proceed further only
335 //if no such indexes are
337 while ((data
= iter
.nextElement())!= NULL
)
339 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
341 printError(ErrAlready
, "Index with name \'%s\' already exists "
342 "on the table \'%s\'.", name
, ((CTABLE
*)tptr
)->tblName_
);
349 tupleptr
= tChunk
->allocate(systemDatabase_
, &rv
);
350 if (NULL
== tupleptr
)
353 "Could not allocate for INDEX catalog table");
356 CINDEX
*indexInfo
= (CINDEX
*)tupleptr
;
357 strcpy(indexInfo
->indName_
, name
);
358 indexInfo
->tblID_
= -1; //Not used currently
359 indexInfo
->tblPtr_
= tptr
;
360 indexInfo
->numFlds_
= numFlds
;
362 indexInfo
->indexType_
= treeIndex
;
364 indexInfo
->indexType_
= hashIndex
;
365 indexInfo
->chunkPtr_
= chunk
;
366 indexInfo
->hashNodeChunk_
= hChunk
;
367 indexInfo
->noOfBuckets_
= bucketSize
;
368 indexInfo
->isUnique_
= isUnique
;
369 indexInfo
->fstIndFld_
=NULL
;
370 printDebug(DM_SystemDatabase
,"One Row inserted into INDEX %x %s",tupleptr
, name
);
374 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
376 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
377 ChunkIterator iter
= fChunk
->getIterator();
380 while ((data
= iter
.nextElement())!= NULL
)
382 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
384 //remove this element and store the tuple ptr
385 //there will be only one row for this table(Primary key)
386 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
387 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
394 fChunk
->free(systemDatabase_
, iptr
);
395 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
399 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
404 DbRetVal
CatalogTableINDEX::get(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
406 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
407 ChunkIterator iter
= fChunk
->getIterator();
410 while ((data
= iter
.nextElement())!= NULL
)
412 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
414 //remove this element and store the tuple ptr
415 //there will be only one row for this table(Primary key)
416 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
417 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
424 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
430 DbRetVal
CatalogTableINDEX::setChunkPtr(const char *name
, ObjectType type
, void *bChunk
, void *firstPage
, void *curPage
)
432 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
433 ChunkIterator iter
= fChunk
->getIterator();
436 while ((data
= iter
.nextElement())!= NULL
)
438 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
440 //remove this element and store the tuple ptr
441 //there will be only one row for this table(Primary key)
443 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(bChunk
);
444 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(bChunk
);
445 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setFirstPage(firstPage
);
446 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setCurPage(curPage
);
447 } else if (type
== tIdx
) {
448 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(firstPage
);
449 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(curPage
);
450 ((CINDEX
*)data
)->hashNodeChunk_
= bChunk
;
458 int CatalogTableINDEX::getNumIndexes(void *tptr
)
460 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
461 ChunkIterator iter
= fChunk
->getIterator();
464 while (NULL
!= (iptr
= iter
.nextElement()))
466 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) numIndex
++;
471 ListIterator
CatalogTableINDEXFIELD::getIndexListIterater(char *name
)
474 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
475 ChunkIterator ifIter
= chunk
->getIterator();
477 while ((data
= ifIter
.nextElement())!= NULL
)
479 IndexInfoForDriver
*idxInfo
= new IndexInfoForDriver();
480 if(strcmp( name
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
) == 0)
482 strcpy(idxInfo
->indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
483 strcpy(idxInfo
->tableName
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
484 strcpy(idxInfo
->fieldName
,((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
485 idxInfo
->type
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
;
486 idxInfo
->isUnique
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->isUnique_
;
487 idxInfo
->isPrimary
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->isPrimary_
;
488 indexList
.append(idxInfo
);
491 return indexList
.getIterator();
494 char* CatalogTableINDEX::getIndexName(void *tptr
, int position
)
496 if (position
== 0) return NULL
;
497 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
498 ChunkIterator iter
= fChunk
->getIterator();
502 while (NULL
!= (iptr
= iter
.nextElement()))
504 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
505 if ( curPos
== position
) return ((CINDEX
*)iptr
)->indName_
;
511 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
514 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
515 ChunkIterator iter
= fChunk
->getIterator();
517 while (NULL
!= (iptr
= iter
.nextElement()))
519 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
)
521 array
[i
++] = (char*) iptr
;
527 ChunkIterator
CatalogTableINDEX::getIterator(void *iptr
)
529 CINDEX
*index
= (CINDEX
*)iptr
;
530 return ((Chunk
*)index
->chunkPtr_
)->getIterator();
534 int CatalogTableINDEX::getNoOfBuckets(void *iptr
)
536 CINDEX
*index
= (CINDEX
*)iptr
;
537 return index
->noOfBuckets_
;
540 int CatalogTableINDEX::getUnique(void *iptr
)
542 CINDEX
*index
= (CINDEX
*)iptr
;
543 return index
->isUnique_
;
545 IndexType
CatalogTableINDEX::getType(void *iptr
)
547 CINDEX
*index
= (CINDEX
*)iptr
;
548 return index
->indexType_
;
550 char* CatalogTableINDEX::getName(void *iptr
)
552 CINDEX
*index
= (CINDEX
*)iptr
;
553 return index
->indName_
;
555 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr
)
557 CINDEX
*index
= (CINDEX
*)iptr
;
558 return ((CFIELD
*)(((CINDEXFIELD
*)(index
->fstIndFld_
))->fieldPtr
))->offset_
;
560 DbRetVal
CatalogTableINDEXFIELD::insert(FieldNameList
&fldList
, void *indexPtr
,
561 void *tblPtr
, char **&fptr
)
565 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
566 ChunkIterator iter
= tChunk
->getIterator();
567 CINDEXFIELD
*fInd
=NULL
;
571 while ((data
= iter
.nextElement())!= NULL
)
573 if ((((CINDEX
*)data
)->tblPtr_
==tblPtr
)
574 && (((CINDEX
*)indexPtr
)->numFlds_
== ((CINDEX
*)data
)->numFlds_
)
575 && (((CINDEX
*)indexPtr
)->indexType_
==((CINDEX
*)data
)->indexType_
)
576 && (data
!= indexPtr
) )
579 while (NULL
!= (fName
= fldList
.nextFieldName()))
582 fInd
=(CINDEXFIELD
*)((CINDEX
*)data
)->fstIndFld_
;
585 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
596 printError(ErrAlready
, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE
*)tblPtr
)->tblName_
, ((CINDEX
*)data
)->indName_
);
603 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
606 while (NULL
!= (fName
= fldList
.nextFieldName()))
609 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
612 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
614 printError(ErrAlready
,"Composite Index Can't be created with same Name");
615 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
621 tChunk
->free(systemDatabase_
,fldI
);
627 void *fieldptr
= tChunk
->allocate(systemDatabase_
, &rv
);
628 if (NULL
== fieldptr
)
630 printError(rv
, "Could not allocate for USER catalog table");
633 CINDEXFIELD
*fldInfo
= (CINDEXFIELD
*)fieldptr
;
634 fldInfo
->tablePtr
= tblPtr
;
635 fldInfo
->fieldPtr
= (CFIELD
*)fptr
[i
++];
636 fldInfo
->indexPtr
= indexPtr
;
637 fldInfo
->next
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
638 ((CINDEX
*)indexPtr
)->fstIndFld_
=fldInfo
;
639 printDebug(DM_SystemDatabase
,"One Row inserted into INDEXFIELD %x", fldInfo
);
644 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
647 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
648 ChunkIterator fIter
= fChunk
->getIterator();
650 while ((data
= fIter
.nextElement())!= NULL
)
652 if (((CINDEXFIELD
*)data
)->indexPtr
== iptr
)
654 //remove this element
655 if(((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
) ((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
= false;
656 fChunk
->free(systemDatabase_
, data
);
657 printDebug(DM_SystemDatabase
,"One Row deleted from INDEXFIELD %x", data
);
663 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
664 char *&name
, DataType
&type
)
667 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
668 ChunkIterator ifIter
= ifChunk
->getIterator();
670 while ((data
= ifIter
.nextElement())!= NULL
)
672 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
674 //store the field name
675 name
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
;
676 type
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->type_
;
680 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
684 DbRetVal
CatalogTableINDEXFIELD::getFieldInfo(void *index
, FieldList
&list
)
687 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
688 ChunkIterator ifIter
= ifChunk
->getIterator();
691 while ((data
= ifIter
.nextElement())!= NULL
)
693 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
695 //add the information to the field list
696 CFIELD
*fTuple
= (CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
);
698 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
699 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
700 fldDef
.type_
= fTuple
->type_
;
701 fldDef
.length_
= fTuple
->length_
;
702 fldDef
.offset_
= fTuple
->offset_
;
703 fldDef
.isDefault_
= fTuple
->isDefault_
;
704 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
705 DEFAULT_VALUE_BUF_LENGTH
);
706 fldDef
.isNull_
= fTuple
->isNull_
;
707 fldDef
.isUnique_
= fTuple
->isUnique_
;
708 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
714 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
720 void CatalogTableINDEXFIELD::printAllIndex()
722 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
723 ChunkIterator ifIter
= chunk
->getIterator();
725 char indexName
[IDENTIFIER_LENGTH
] = {'\0'};
726 while ((data
= ifIter
.nextElement())!= NULL
)
728 if(strcmp(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
)!=0)
730 printf(" <Index Name> %s </Index Name> \n",((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
731 if(0==((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
)
732 printf(" <Index Type> Hash Index </Index Type> \n");
734 printf(" <Index Type> Tree Index </Index Type> \n");
735 printf(" <Table Name> %s </Table Name> \n",((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
736 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
740 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
742 strcpy(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
746 List
CatalogTableUSER::getUserList()
749 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
750 ChunkIterator iter
= chk
->getIterator();
752 while (NULL
!= (tptr
= iter
.nextElement()))
754 Identifier
*elem
= new Identifier();
755 strcpy(elem
->name
, ((CUSER
*)tptr
)->userName_
);
756 userList
.append(elem
);
762 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
764 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
766 ChunkIterator iter
= tChunk
->getIterator();
768 while ((data
= iter
.nextElement())!= NULL
)
770 if (0 == strcmp(((CUSER
*)data
)->userName_
, name
))
772 printError(ErrAlready
, "User with name \'%s\' already exists ", name
);
777 CUSER
*usrInfo
= (CUSER
*)tChunk
->allocate(systemDatabase_
, &rv
);
781 "Could not allocate for USER catalog table");
784 strcpy(usrInfo
->userName_
, name
);
785 strcpy(usrInfo
->password_
, pass
);
786 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
791 DbRetVal
CatalogTableUSER::authenticate(const char *name
, const char *pass
,
792 bool &isAuthenticated
, bool &isDba
)
794 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
795 ChunkIterator iter
= tChunk
->getIterator();
797 while (NULL
!= (data
= iter
.nextElement()))
799 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
801 //verify the password
802 //char * enpass = os::encrypt(pass,"A0");
803 char * enpass
= (char*) pass
;
804 if (0 == strcmp(enpass
, ((CUSER
*)data
)->password_
))
806 isAuthenticated
= true;
807 if (0 == strcmp(((CUSER
*)data
)->userName_
, DBAUSER
))
808 isDba
= true; else isDba
= false;
813 isAuthenticated
= false;
817 DbRetVal
CatalogTableUSER::remove(const char *name
)
819 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
820 ChunkIterator iter
= tChunk
->getIterator();
822 while ((data
= iter
.nextElement())!= NULL
)
824 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
826 //remove this element
827 tChunk
->free(systemDatabase_
, data
);
831 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
835 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
837 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
838 ChunkIterator iter
= tChunk
->getIterator();
840 while (NULL
!= (data
= iter
.nextElement()))
842 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
844 //change the password
845 strcpy(((CUSER
*)data
)->password_
,pass
);// os::encrypt(pass, "A0"));
849 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
853 DbRetVal
CatalogTableFK::insert(char *name
, void *tptr
, void *tPkptr
)
855 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
856 ChunkIterator iter
= tChunk
->getIterator();
858 while ((data
= iter
.nextElement())!= NULL
)
860 if (0 == strcmp(((CFK
*)data
)->fkName_
, name
))
862 printError(ErrAlready
, "Index with name \'%s\' already exists "
863 "on the table \'%s\'.", name
, ((CTABLE
*)tptr
)->tblName_
);
870 void *fkptr
= tChunk
->allocate(systemDatabase_
, &rv
);
873 printError(rv
, "Could not allocate for FK catalog table");
876 CFK
*fkTblInfo
= (CFK
*)fkptr
;
877 strcpy(fkTblInfo
->fkName_
, name
);
878 fkTblInfo
->fkTblPtr_
= tptr
;
879 fkTblInfo
->pkTblPtr_
= tPkptr
;
880 printDebug(DM_SystemDatabase
,"One Row inserted into FK %x %s",fkptr
, name
);
884 DbRetVal
CatalogTableFKFIELD::insert(char *cFKName
, char **fkFldPtrs
, char **pkFldPtrs
,int totalFld
)
886 Chunk
*tChunk
= NULL
;
887 tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
888 ChunkIterator iter
= tChunk
->getIterator();
890 while ((data
= iter
.nextElement())!= NULL
)
892 if (0 == strcmp(((CFK
*)data
)->fkName_
, cFKName
))
897 tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
902 void *fieldptr
= tChunk
->allocate(systemDatabase_
, &rv
);
903 if (NULL
== fieldptr
)
905 printError(rv
, "Could not allocate for USER catalog table");
908 CFKFIELD
*fldInfo
= (CFKFIELD
*)fieldptr
;
909 fldInfo
->fkPtr_
= data
;
910 fldInfo
->pfFldPtr_
= (CFIELD
*)pkFldPtrs
[i
];
911 fldInfo
->fkFldPtr_
= (CFIELD
*)fkFldPtrs
[i
++];
912 //printDebug(DM_TEST,"TYPE %d\n",((CFIELD*)fldInfo->pfFldPtr_)->type_);
913 //printDebug(DM_TEST,"FK name %s\n",((CFIELD*)fldInfo->fkFldPtr_)->fldName_);
914 if(!(((CFIELD
*)fldInfo
->pfFldPtr_
)->isUnique_
) || !(((CFIELD
*)fldInfo
->pfFldPtr_
)->isNull_
))
916 printError(ErrSysInternal
,"Parent Table field should have primary key field ");
917 tChunk
->free(systemDatabase_
,fieldptr
);
918 return ErrSysInternal
;
920 if(((CFIELD
*)fldInfo
->pfFldPtr_
)->type_
!=((CFIELD
*)fldInfo
->fkFldPtr_
)->type_
)
922 printError(ErrSysInternal
,"Type Missmatch in both PK field and FK field ");
923 tChunk
->free(systemDatabase_
,fieldptr
);
924 return ErrSysInternal
;
926 printDebug(DM_SystemDatabase
,"One Row inserted into FKFIELD %x", fldInfo
);
930 DbRetVal
CatalogTableFK::remove(void *ctptr
)
932 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
933 ChunkIterator iter
= tChunk
->getIterator();
935 while ((data
= iter
.nextElement())!= NULL
)
939 tChunk
->free(systemDatabase_
,data
);
940 printDebug(DM_SystemDatabase
,"One Row deleted from FKFIELD %x", data
);
947 DbRetVal
CatalogTableFKFIELD::remove(void *cFKfld
)
949 Chunk
*fChunk
= NULL
;
950 fChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
951 ChunkIterator iter
= fChunk
->getIterator();
953 while ((data
= iter
.nextElement())!= NULL
)
955 if (((CFKFIELD
*)data
)->fkPtr_
== cFKfld
)
957 fChunk
->free(systemDatabase_
, data
);
958 printDebug(DM_SystemDatabase
,"One Row deleted from CFKFIELD %x", data
);
963 void *CatalogTableFK::getFkCTable(void *ctptr
)
965 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
966 ChunkIterator iter
= tChunk
->getIterator();
968 while ((data
= iter
.nextElement())!= NULL
)
970 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
978 int CatalogTableFK::getNumFkTable(void *ctptr
)
980 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
981 ChunkIterator iter
= tChunk
->getIterator();
984 while ((data
= iter
.nextElement())!= NULL
)
986 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
994 bool CatalogTableFK::isFkTable(void *ctptr
)
996 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
997 ChunkIterator iter
= tChunk
->getIterator();
1000 while ((data
= iter
.nextElement())!= NULL
)
1002 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
1009 int CatalogTableFK::getNoOfFkTable(void *ctptr
)
1011 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1012 ChunkIterator iter
= tChunk
->getIterator();
1015 while ((data
= iter
.nextElement())!= NULL
)
1017 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
1025 int CatalogTableFK::getNoOfPkTable(void *ctptr
)
1027 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1028 ChunkIterator iter
= tChunk
->getIterator();
1031 while ((data
= iter
.nextElement())!= NULL
)
1033 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
1041 void CatalogTableFK::getPkTableName(void *ctptr
,char **&array
)
1043 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1044 ChunkIterator iter
= tChunk
->getIterator();
1047 while ((data
= iter
.nextElement())!= NULL
)
1049 if (((CFK
*)data
)->fkTblPtr_
== ctptr
)
1051 array
[i
++] = ((CTABLE
*)((CFK
*)data
)->pkTblPtr_
)->tblName_
;
1055 void CatalogTableFK::getFkTableName(void *ctptr
,char **&array
)
1057 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1058 ChunkIterator iter
= tChunk
->getIterator();
1061 while ((data
= iter
.nextElement())!= NULL
)
1063 if (((CFK
*)data
)->pkTblPtr_
== ctptr
)
1065 array
[i
++] = ((CTABLE
*)((CFK
*)data
)->fkTblPtr_
)->tblName_
;
1071 DbRetVal
CatalogTableFK::getPkFkFieldInfo(void *cpkptr
, void *cfkptr
, FieldNameList
&pklist
,FieldNameList
&fklist
)
1073 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyTableId
);
1074 ChunkIterator iter
= tChunk
->getIterator();
1076 while ((data
= iter
.nextElement())!= NULL
)
1078 if (((CFK
*)data
)->pkTblPtr_
== cpkptr
&& ((CFK
*)data
)->fkTblPtr_
== cfkptr
)
1085 printError(ErrNotExists
,"Foreign Key field CFK not found");
1086 return ErrNotExists
;
1088 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(ForeignKeyFieldTableId
);
1089 iter
= fChunk
->getIterator();
1091 while ((fdata
= iter
.nextElement())!= NULL
)
1093 if (((CFKFIELD
*)fdata
)->fkPtr_
==data
)
1095 //printDebug(DM_TEST,"PK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1096 //printDebug(DM_TEST,"FK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1097 pklist
.append(((CFIELD
*)((CFKFIELD
*)fdata
)->pfFldPtr_
)->fldName_
);
1098 fklist
.append(((CFIELD
*)((CFKFIELD
*)fdata
)->fkFldPtr_
)->fldName_
);