fix for trie index
[csql.git] / src / storage / CatalogTables.cxx
blob820dc5e12a7c7bcb1888a278da81bdb6f7ab8a69
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.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<CatalogTables.h>
17 #include<Database.h>
18 #include<Allocator.h>
19 #include<Field.h>
20 #include<Debug.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);
28 DbRetVal rv = OK;
29 tptr = tChunk->allocate(systemDatabase_, &rv);
30 if (NULL == tptr)
32 printError(rv,
33 "Could not allocate memory for for TABLE catalog table");
34 return rv;
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);
45 return OK;
47 DbRetVal CatalogTableTABLE::renameTable( const char *oldName,const char *newName)
49 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
50 ChunkIterator iter = tChunk->getIterator();
51 void *data = NULL;
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;
59 isTableExits = true;
61 if (0 == strcmp(((CTABLE*)data)->tblName_,newName))
63 printError(ErrNotExists,"A Table with name %s already exists", newName);
64 return ErrNotExists;
67 strcpy(oldTable->tblName_, newName);
68 if(!isTableExits){
69 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", oldName);
70 return ErrNotExists;
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);
81 break;
84 return OK;
86 DbRetVal CatalogTableTABLE::renameIndex( const char *oldName,const char *newName)
88 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
89 ChunkIterator iter = tChunk->getIterator();
90 void *data = NULL;
91 bool isIndexExits = false ;
92 CINDEX *oldIndex = NULL;
93 while ((data = iter.nextElement())!= NULL)
95 if (0 == strcmp(((CINDEX*)data)->indName_, oldName))
97 oldIndex=(CINDEX*)data;
98 isIndexExits = true;
100 if (0 == strcmp(((CINDEX*)data)->indName_,newName))
102 printError(ErrNotExists,"A Index with name %s already exists", newName);
103 return ErrNotExists;
106 if(!isIndexExits){
107 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", oldName);
108 return ErrNotExists;
110 strcpy(oldIndex->indName_, newName);
112 return OK;
115 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
117 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
118 ChunkIterator iter = tChunk->getIterator();
120 void *data = NULL;
121 while ((data = iter.nextElement())!= NULL)
123 if (0 == strcmp(((CTABLE*)data)->tblName_, name))
125 //remove this element and store the tblPtr
126 //there will be only one row for this table(Primary key)
127 tptr = (void*) data;
128 chunk = (Chunk*) ((CTABLE*)data)->chunkPtr_;
129 break;
132 if (NULL != tptr)
134 tChunk->free(systemDatabase_, tptr);
135 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
137 else
139 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
140 return ErrNotExists;
142 return OK;
145 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
146 void *&chunk, void *&tptr, void *&vcchunk)
148 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
149 ChunkIterator iter = chk->getIterator();;
150 while (NULL != (tptr = iter.nextElement()))
152 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
154 //there will be only one row for this table(Primary key)
155 chunk = (Chunk*) ((CTABLE*)tptr)->chunkPtr_;
156 vcchunk = (Chunk*) ((CTABLE*)tptr)->varcharChunkPtr_;
157 return OK;
160 //table not found in TABLE
161 return ErrNotFound;
165 DbRetVal CatalogTableTABLE::setChunkPtr(const char *name, void *firstPage, void *curPage)
167 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
168 ChunkIterator iter = chk->getIterator();;
169 void *tptr;
170 while (NULL != (tptr = iter.nextElement()))
172 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
174 //there will be only one row for this table(Primary key)
175 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setFirstPage(firstPage);
176 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setCurPage(curPage);
177 return OK;
180 //table not found in TABLE
181 return ErrNotFound;
184 List CatalogTableTABLE::getTableList()
186 List tableList;
187 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
188 ChunkIterator iter = chk->getIterator();
189 void *tptr;
190 while (NULL != (tptr = iter.nextElement()))
192 Identifier *elem = new Identifier();
193 strcpy(elem->name, ((CTABLE*)tptr)->tblName_);
194 tableList.append(elem);
196 return tableList;
199 DbRetVal CatalogTableFIELD::renameField(const char *tableName, const char *oldName, const char *newName)
201 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
202 ChunkIterator iter = fChunk->getIterator();
203 void *data = NULL;
204 bool isFieldExists=false;
205 while ((data = iter.nextElement())!= NULL)
207 if ((strcmp(((CFIELD*)data)->fldName_,newName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
209 printError(ErrAlready,
210 "New Field Name '%s' already exists in the table.", newName);
211 return ErrAlready;
214 iter = fChunk->getIterator();
215 while ((data = iter.nextElement())!= NULL)
217 if ((strcmp(((CFIELD*)data)->fldName_,oldName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
219 strcpy(((CFIELD*)data)->fldName_,newName);
220 isFieldExists = true;
221 break;
224 if(!isFieldExists){
225 printError(ErrNotExists, "Old Field Name '%s' does not exist in table",
226 oldName);
227 return ErrNotExists;
229 return OK;
232 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
234 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
235 DbRetVal rv = OK;
236 while (iter.hasElement())
238 void *fptr = fChunk->allocate(systemDatabase_, &rv);
239 if (NULL == fptr)
241 printError(rv,
242 "Could not allocate for FIELD catalog table");
243 return rv;
245 CFIELD *fldInfo = (CFIELD*)fptr;
246 FieldDef *fDef = iter.nextElement();
247 strcpy(fldInfo->fldName_, fDef->fldName_);
248 fldInfo->tblID_ = tblID;
249 fldInfo->tblPtr_ = tptr;
250 fldInfo->type_ = fDef->type_;
251 fldInfo->length_ = fDef->length_;
252 fldInfo->offset_ = fDef->offset_;
253 os::memcpy(fldInfo->defaultValueBuf_, fDef->defaultValueBuf_,
254 DEFAULT_VALUE_BUF_LENGTH);
255 fldInfo->isNull_ = fDef->isNull_;
256 fldInfo->isPrimary_ = fDef->isPrimary_;
257 fldInfo->isUnique_ = fDef->isUnique_;
258 fldInfo->isDefault_ = fDef->isDefault_;
259 fldInfo->isAutoIncrement_= fDef->isAutoIncrement_;
260 fldInfo->autoVal_ = 0;
261 fldInfo->width_ = 0; //TODO
262 fldInfo->scale_ = 0; //TODO
263 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef->fldName_);
266 return OK;
269 DbRetVal CatalogTableFIELD::remove(void *tptr)
271 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
272 ChunkIterator fIter = fChunk->getIterator();
273 void *data = NULL;
274 while ((data = fIter.nextElement())!= NULL)
276 if (((CFIELD*)data)->tblPtr_ == tptr)
278 //remove this element
279 fChunk->free(systemDatabase_, data);
280 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
283 return OK;
286 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
288 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
289 ChunkIterator fIter = fChunk->getIterator();;
290 void *data = NULL;
291 void *ptrToAutoVal;
292 while (NULL != (data = fIter.nextElement()))
294 if (((CFIELD*)data)->tblPtr_ == tptr)
296 //add the information to the field list
297 CFIELD *fTuple = (CFIELD*)data;
298 FieldDef fldDef;
299 strcpy(fldDef.fldName_, fTuple->fldName_);
300 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
301 fldDef.type_ = fTuple->type_;
302 fldDef.length_ = fTuple->length_;
303 fldDef.offset_ = fTuple->offset_;
304 fldDef.isDefault_ = fTuple->isDefault_;
305 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
306 DEFAULT_VALUE_BUF_LENGTH);
307 fldDef.isNull_ = fTuple->isNull_;
308 fldDef.isUnique_ = fTuple->isUnique_;
309 fldDef.isPrimary_ = fTuple->isPrimary_;
310 fldDef.isAutoIncrement_= fTuple->isAutoIncrement_;
311 if(fTuple->isAutoIncrement_){
312 ptrToAutoVal = &fTuple->autoVal_;
313 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
315 list.append(fldDef);
318 return ptrToAutoVal;
321 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
323 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
324 int i=0;
325 char *fName = NULL;
326 bool found = false;
327 fldList.resetIter();
328 void *data = NULL;
329 DbRetVal rv =OK;
330 while (NULL != (fName = fldList.nextFieldName()))
332 ChunkIterator fIter = fChunk->getIterator();
333 found = false;
334 while (NULL != (data = fIter.nextElement()))
336 if (((CFIELD*)data)->tblPtr_ == tptr)
338 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
340 found = true;
341 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
342 fptr[i++] = (char*) data;
343 break;
347 if (!found)
349 printError(ErrNotFound,
350 "No entries found in FIELD catalog table for the table specified");
351 return ErrNotFound;
354 return rv;
357 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
358 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
360 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
361 ChunkIterator iter = tChunk->getIterator();
363 //Checking for index having same name, proceed further only
364 //if no such indexes are
365 void *data = NULL;
366 while ((data = iter.nextElement())!= NULL)
368 if (0 == strcmp(((CINDEX*)data)->indName_, name))
370 printError(ErrAlready, "Index with name \'%s\' already exists "
371 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
372 return ErrAlready;
377 DbRetVal rv =OK;
378 tupleptr = tChunk->allocate(systemDatabase_, &rv);
379 if (NULL == tupleptr)
381 printError(rv,
382 "Could not allocate for INDEX catalog table");
383 return rv;
385 CINDEX *indexInfo = (CINDEX*)tupleptr;
386 strcpy(indexInfo->indName_, name);
387 indexInfo->tblID_ = -1; //Not used currently
388 indexInfo->tblPtr_ = tptr;
389 indexInfo->numFlds_ = numFlds;
390 if (NULL == hChunk)
391 indexInfo->indexType_ = treeIndex;
392 else
393 indexInfo->indexType_ = hashIndex;
394 if (0 == bucketSize) indexInfo->indexType_ = trieIndex;
395 indexInfo->chunkPtr_ = chunk;
396 indexInfo->hashNodeChunk_ = hChunk;
397 indexInfo->noOfBuckets_ = bucketSize;
398 indexInfo->isUnique_ = isUnique;
399 indexInfo->fstIndFld_=NULL;
400 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
401 return OK;
404 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
406 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
407 ChunkIterator iter = fChunk->getIterator();
409 void *data = NULL;
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_;
418 iptr = (void*) data;
419 break;
422 if (NULL != iptr)
424 fChunk->free(systemDatabase_, iptr);
425 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
427 else
429 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
430 return ErrNotExists;
432 return OK;
434 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
436 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
437 ChunkIterator iter = fChunk->getIterator();
439 void *data = NULL;
440 while ((data = iter.nextElement())!= NULL)
442 if (0 == strcmp(((CINDEX*)data)->indName_, name))
444 //remove this element and store the tuple ptr
445 //there will be only one row for this table(Primary key)
446 chunk = (Chunk*) ((CINDEX*)data)->chunkPtr_;
447 hchunk = (Chunk*) ((CINDEX*)data)->hashNodeChunk_;
448 iptr = (void*) data;
449 break;
452 if (NULL == iptr)
454 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
455 return ErrNotExists;
457 return OK;
460 DbRetVal CatalogTableINDEX::setChunkPtr(const char *name, ObjectType type, void *bChunk, void *firstPage, void *curPage)
462 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
463 ChunkIterator iter = fChunk->getIterator();
465 void *data = NULL;
466 while ((data = iter.nextElement())!= NULL)
468 if (0 == strcmp(((CINDEX*)data)->indName_, name))
470 //remove this element and store the tuple ptr
471 //there will be only one row for this table(Primary key)
472 if (type == hIdx) {
473 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(bChunk);
474 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(bChunk);
475 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setFirstPage(firstPage);
476 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setCurPage(curPage);
477 } else if (type == tIdx) {
478 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(firstPage);
479 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(curPage);
480 ((CINDEX*)data)->hashNodeChunk_ = bChunk;
482 break;
485 return OK;
488 int CatalogTableINDEX::getNumIndexes(void *tptr)
490 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
491 ChunkIterator iter = fChunk->getIterator();
492 void *iptr = NULL;
493 int numIndex =0;
494 while (NULL != (iptr = iter.nextElement()))
496 if (((CINDEX*)iptr)->tblPtr_ == tptr) numIndex++;
498 return numIndex;
501 ListIterator CatalogTableINDEXFIELD::getIndexListIterater(char *name)
503 List indexList;
504 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
505 ChunkIterator ifIter = chunk->getIterator();
506 void *data = NULL;
507 while ((data = ifIter.nextElement())!= NULL)
509 IndexInfoForDriver *idxInfo = new IndexInfoForDriver();
510 if(strcmp( name,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_) == 0)
512 strcpy(idxInfo->indexName ,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
513 strcpy(idxInfo->tableName ,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
514 strcpy(idxInfo->fieldName ,((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
515 idxInfo->type = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_ ;
516 idxInfo->isUnique = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->isUnique_;
517 idxInfo->isPrimary = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->isPrimary_;
518 indexList.append(idxInfo);
521 return indexList.getIterator();
524 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
526 if (position == 0) return NULL;
527 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
528 ChunkIterator iter = fChunk->getIterator();
529 void *iptr = NULL;
530 int numIndex =0;
531 int curPos =0;
532 while (NULL != (iptr = iter.nextElement()))
534 if (((CINDEX*)iptr)->tblPtr_ == tptr) curPos++;
535 if ( curPos == position ) return ((CINDEX*)iptr)->indName_;
537 return NULL;
541 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
543 void *iptr = NULL;
544 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
545 ChunkIterator iter = fChunk->getIterator();
546 int i=0;
547 while (NULL != (iptr = iter.nextElement()))
549 if (((CINDEX*)iptr)->tblPtr_ == tptr)
551 array[i++] = (char*) iptr;
554 return;
557 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
559 CINDEX *index = (CINDEX*)iptr;
560 return ((Chunk*)index->chunkPtr_)->getIterator();
564 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
566 CINDEX *index = (CINDEX*)iptr;
567 return index->noOfBuckets_;
570 int CatalogTableINDEX::getUnique(void *iptr)
572 CINDEX *index = (CINDEX*)iptr;
573 return index->isUnique_;
575 IndexType CatalogTableINDEX::getType(void *iptr)
577 CINDEX *index = (CINDEX*)iptr;
578 return index->indexType_;
580 char* CatalogTableINDEX::getName(void *iptr)
582 CINDEX *index = (CINDEX*)iptr;
583 return index->indName_;
585 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr)
587 CINDEX *index = (CINDEX*)iptr;
588 return ((CFIELD*)(((CINDEXFIELD*)(index->fstIndFld_))->fieldPtr))->offset_;
590 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
591 void *tblPtr, char **&fptr)
594 Chunk *tChunk;
595 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
596 ChunkIterator iter = tChunk->getIterator();
597 CINDEXFIELD *fInd=NULL;
598 char *fName =NULL;
599 void *data = NULL;
600 bool isFldInd=false;
601 while ((data = iter.nextElement())!= NULL)
603 if ((((CINDEX*)data)->tblPtr_==tblPtr)
604 && (((CINDEX*)indexPtr)->numFlds_ == ((CINDEX*)data)->numFlds_)
605 && (((CINDEX*)indexPtr)->indexType_==((CINDEX*)data)->indexType_)
606 && (data != indexPtr) )
608 fldList.resetIter();
609 while (NULL != (fName = fldList.nextFieldName()))
611 isFldInd=false;
612 fInd=(CINDEXFIELD*)((CINDEX*)data)->fstIndFld_ ;
613 while (fInd)
615 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
617 isFldInd=true;
618 break;
620 fInd=fInd->next;
622 if(!isFldInd) break;
624 if(isFldInd)
626 printError(ErrAlready, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE *)tblPtr)->tblName_, ((CINDEX *)data)->indName_);
627 return ErrAlready;
633 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
634 fldList.resetIter();
635 int i =0;
636 while (NULL != (fName = fldList.nextFieldName()))
638 DbRetVal rv = OK;
639 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
640 while(fInd)
642 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
644 printError(ErrAlready,"Composite Index Can't be created with same Name");
645 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
646 CINDEXFIELD *fldI;
647 while(fInd)
649 fldI=fInd;
650 fInd=fInd->next;
651 tChunk->free(systemDatabase_,fldI);
653 return ErrAlready;
655 fInd=fInd->next;
657 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
658 if (NULL == fieldptr)
660 printError(rv, "Could not allocate for USER catalog table");
661 return rv;
663 CINDEXFIELD *fldInfo = (CINDEXFIELD*)fieldptr;
664 fldInfo->tablePtr = tblPtr;
665 fldInfo->fieldPtr = (CFIELD*)fptr[i++];
666 fldInfo->indexPtr = indexPtr;
667 fldInfo->next=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
668 ((CINDEX *)indexPtr)->fstIndFld_=fldInfo;
669 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
671 return OK;
674 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
676 Chunk *fChunk;
677 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
678 ChunkIterator fIter = fChunk->getIterator();
679 void *data = NULL;
680 while ((data = fIter.nextElement())!= NULL)
682 if (((CINDEXFIELD*)data)->indexPtr == iptr)
684 //remove this element
685 if(((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_) ((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_ = false;
686 fChunk->free(systemDatabase_, data);
687 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
690 return OK;
693 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
694 char *&name, DataType &type)
696 Chunk *ifChunk;
697 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
698 ChunkIterator ifIter = ifChunk->getIterator();
699 void *data = NULL;
700 while ((data = ifIter.nextElement())!= NULL)
702 if (((CINDEXFIELD*)data)->indexPtr == index)
704 //store the field name
705 name = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_;
706 type = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->type_;
707 return OK;
710 printError(ErrNotExists,"Index %x not exists in catalog table", index);
711 return ErrNotExists;
714 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
716 Chunk *ifChunk;
717 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
718 ChunkIterator ifIter = ifChunk->getIterator();
719 void *data = NULL;
720 int rowCount =0;
721 while ((data = ifIter.nextElement())!= NULL)
723 if (((CINDEXFIELD*)data)->indexPtr == index)
725 //add the information to the field list
726 CFIELD *fTuple = (CFIELD*)(((CINDEXFIELD*)data)->fieldPtr);
727 FieldDef fldDef;
728 strcpy(fldDef.fldName_, fTuple->fldName_);
729 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
730 fldDef.type_ = fTuple->type_;
731 fldDef.length_ = fTuple->length_;
732 fldDef.offset_ = fTuple->offset_;
733 fldDef.isDefault_ = fTuple->isDefault_;
734 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
735 DEFAULT_VALUE_BUF_LENGTH);
736 fldDef.isNull_ = fTuple->isNull_;
737 fldDef.isUnique_ = fTuple->isUnique_;
738 fldDef.isPrimary_ = fTuple->isPrimary_;
739 list.append(fldDef);
741 rowCount++;
743 if (!rowCount) {
744 printError(ErrNotExists,"Index %x not exists in catalog table", index);
745 return ErrNotExists;
747 return OK;
750 void CatalogTableINDEXFIELD::printAllIndex()
752 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
753 ChunkIterator ifIter = chunk->getIterator();
754 void *data = NULL;
755 char indexName[IDENTIFIER_LENGTH] = {'\0'};
756 while ((data = ifIter.nextElement())!= NULL)
758 if(strcmp(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_)!=0)
760 printf(" <Index Name> %s </Index Name> \n",((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
761 if(0==((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_)
762 printf(" <Index Type> Hash Index </Index Type> \n");
763 else
764 printf(" <Index Type> Tree Index </Index Type> \n");
765 printf(" <Table Name> %s </Table Name> \n",((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
766 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
768 else
770 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
772 strcpy(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
776 List CatalogTableUSER::getUserList()
778 List userList;
779 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
780 ChunkIterator iter = chk->getIterator();
781 void *tptr;
782 while (NULL != (tptr = iter.nextElement()))
784 Identifier *elem = new Identifier();
785 strcpy(elem->name, ((CUSER*)tptr)->userName_);
786 userList.append(elem);
788 return userList;
792 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
794 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
795 DbRetVal rv = OK;
796 ChunkIterator iter = tChunk->getIterator();
797 void *data = NULL;
798 while ((data = iter.nextElement())!= NULL)
800 if (0 == strcmp(((CUSER*)data)->userName_, name))
802 printError(ErrAlready, "User with name \'%s\' already exists ", name);
803 return ErrAlready;
807 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
808 if (NULL == usrInfo)
810 printError(rv,
811 "Could not allocate for USER catalog table");
812 return rv;
814 strcpy(usrInfo->userName_, name);
815 strcpy(usrInfo->password_, pass);
816 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
817 return OK;
821 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
822 bool &isAuthenticated, bool &isDba)
824 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
825 ChunkIterator iter = tChunk->getIterator();
826 void *data = NULL;
827 while (NULL != (data = iter.nextElement()))
829 if (strcmp(((CUSER*)data)->userName_, name) == 0)
831 //verify the password
832 //char * enpass = os::encrypt(pass,"A0");
833 char * enpass = (char*) pass;
834 if (0 == strcmp(enpass, ((CUSER*)data)->password_))
836 isAuthenticated = true;
837 if (0 == strcmp(((CUSER*)data)->userName_, DBAUSER))
838 isDba = true; else isDba = false;
839 return OK;
843 isAuthenticated = false;
844 return OK;
847 DbRetVal CatalogTableUSER::remove(const char *name)
849 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
850 ChunkIterator iter = tChunk->getIterator();
851 void *data = NULL;
852 while ((data = iter.nextElement())!= NULL)
854 if (strcmp(((CUSER*)data)->userName_, name) == 0)
856 //remove this element
857 tChunk->free(systemDatabase_, data);
858 return OK;
861 printError(ErrNotExists,"User %s not exists in catalog table", name);
862 return ErrNotExists;
865 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
867 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
868 ChunkIterator iter = tChunk->getIterator();
869 void *data = NULL;
870 while (NULL != (data = iter.nextElement()))
872 if (strcmp(((CUSER*)data)->userName_, name) == 0)
874 //change the password
875 strcpy(((CUSER*)data)->password_,pass);// os::encrypt(pass, "A0"));
876 return OK;
879 printError(ErrNotExists,"User %s not exists in catalog table", name);
880 return ErrNotExists;
883 DbRetVal CatalogTableFK::insert(char *name, void *tptr, void *tPkptr)
885 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
886 ChunkIterator iter = tChunk->getIterator();
887 void *data = NULL;
888 while ((data = iter.nextElement())!= NULL)
890 if (0 == strcmp(((CFK*)data)->fkName_, name))
892 printError(ErrAlready, "Index with name \'%s\' already exists "
893 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
894 return ErrAlready;
899 DbRetVal rv =OK;
900 void *fkptr = tChunk->allocate(systemDatabase_, &rv);
901 if (NULL == fkptr)
903 printError(rv, "Could not allocate for FK catalog table");
904 return rv;
906 CFK *fkTblInfo = (CFK*)fkptr;
907 strcpy(fkTblInfo->fkName_, name);
908 fkTblInfo->fkTblPtr_= tptr;
909 fkTblInfo->pkTblPtr_= tPkptr;
910 printDebug(DM_SystemDatabase,"One Row inserted into FK %x %s",fkptr, name);
911 return OK;
914 DbRetVal CatalogTableFKFIELD::insert(char *cFKName, char **fkFldPtrs, char **pkFldPtrs,int totalFld)
916 Chunk *tChunk = NULL;
917 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
918 ChunkIterator iter = tChunk->getIterator();
919 void *data = NULL;
920 while ((data = iter.nextElement())!= NULL)
922 if (0 == strcmp(((CFK*)data)->fkName_, cFKName))
924 break;
927 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
928 int i =0;
929 DbRetVal rv = OK;
930 while (i < totalFld)
932 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
933 if (NULL == fieldptr)
935 printError(rv, "Could not allocate for USER catalog table");
936 return rv;
938 CFKFIELD *fldInfo = (CFKFIELD*)fieldptr;
939 fldInfo->fkPtr_ = data;
940 fldInfo->pfFldPtr_ = (CFIELD*)pkFldPtrs[i];
941 fldInfo->fkFldPtr_ = (CFIELD*)fkFldPtrs[i++];
942 //printDebug(DM_TEST,"TYPE %d\n",((CFIELD*)fldInfo->pfFldPtr_)->type_);
943 //printDebug(DM_TEST,"FK name %s\n",((CFIELD*)fldInfo->fkFldPtr_)->fldName_);
944 if(!(((CFIELD*)fldInfo->pfFldPtr_)->isUnique_) || !(((CFIELD*)fldInfo->pfFldPtr_)->isNull_))
946 printError(ErrSysInternal,"Parent Table field should have primary key field ");
947 tChunk->free(systemDatabase_,fieldptr);
948 return ErrSysInternal;
950 if(((CFIELD*)fldInfo->pfFldPtr_)->type_!=((CFIELD*)fldInfo->fkFldPtr_)->type_)
952 printError(ErrSysInternal,"Type Missmatch in both PK field and FK field ");
953 tChunk->free(systemDatabase_,fieldptr);
954 return ErrSysInternal;
956 printDebug(DM_SystemDatabase,"One Row inserted into FKFIELD %x", fldInfo);
958 return OK;
960 DbRetVal CatalogTableFK::remove(void *ctptr)
962 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
963 ChunkIterator iter = tChunk->getIterator();
964 void *data = NULL;
965 while ((data = iter.nextElement())!= NULL)
967 if (data == ctptr)
969 tChunk->free(systemDatabase_,data);
970 printDebug(DM_SystemDatabase,"One Row deleted from FKFIELD %x", data);
974 return OK;
977 DbRetVal CatalogTableFKFIELD::remove(void *cFKfld)
979 Chunk *fChunk = NULL;
980 fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
981 ChunkIterator iter = fChunk->getIterator();
982 void *data = NULL;
983 while ((data = iter.nextElement())!= NULL)
985 if (((CFKFIELD*)data)->fkPtr_== cFKfld )
987 fChunk->free(systemDatabase_, data);
988 printDebug(DM_SystemDatabase,"One Row deleted from CFKFIELD %x", data);
991 return OK;
993 void *CatalogTableFK::getFkCTable(void *ctptr)
995 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
996 ChunkIterator iter = tChunk->getIterator();
997 void *data = NULL;
998 while ((data = iter.nextElement())!= NULL)
1000 if (((CFK*)data)->fkTblPtr_== ctptr)
1002 return data;
1006 return NULL;
1008 int CatalogTableFK::getNumFkTable(void *ctptr)
1010 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1011 ChunkIterator iter = tChunk->getIterator();
1012 void *data = NULL;
1013 int count=0;
1014 while ((data = iter.nextElement())!= NULL)
1016 if (((CFK*)data)->pkTblPtr_== ctptr)
1018 count++;
1021 return count;
1024 bool CatalogTableFK::isFkTable(void *ctptr)
1026 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1027 ChunkIterator iter = tChunk->getIterator();
1028 void *data = NULL;
1029 int count=0;
1030 while ((data = iter.nextElement())!= NULL)
1032 if (((CFK*)data)->fkTblPtr_== ctptr)
1034 return true;
1037 return false;
1039 int CatalogTableFK::getNoOfFkTable(void *ctptr)
1041 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1042 ChunkIterator iter = tChunk->getIterator();
1043 void *data = NULL;
1044 int count=0;
1045 while ((data = iter.nextElement())!= NULL)
1047 if (((CFK*)data)->pkTblPtr_== ctptr)
1049 count++;
1052 return count;
1055 int CatalogTableFK::getNoOfPkTable(void *ctptr)
1057 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1058 ChunkIterator iter = tChunk->getIterator();
1059 void *data = NULL;
1060 int count=0;
1061 while ((data = iter.nextElement())!= NULL)
1063 if (((CFK*)data)->fkTblPtr_== ctptr)
1065 count++;
1068 return count;
1071 void CatalogTableFK::getPkTableName(void *ctptr,char **&array)
1073 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1074 ChunkIterator iter = tChunk->getIterator();
1075 void *data = NULL;
1076 int i=0;
1077 while ((data = iter.nextElement())!= NULL)
1079 if (((CFK*)data)->fkTblPtr_== ctptr)
1081 array[i++] = ((CTABLE*)((CFK*)data)->pkTblPtr_)->tblName_;
1085 void CatalogTableFK::getFkTableName(void *ctptr,char **&array)
1087 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1088 ChunkIterator iter = tChunk->getIterator();
1089 void *data = NULL;
1090 int i=0;
1091 while ((data = iter.nextElement())!= NULL)
1093 if (((CFK*)data)->pkTblPtr_== ctptr)
1095 array[i++] = ((CTABLE*)((CFK*)data)->fkTblPtr_)->tblName_;
1101 DbRetVal CatalogTableFK::getPkFkFieldInfo(void *cpkptr, void *cfkptr, FieldNameList &pklist,FieldNameList &fklist)
1103 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1104 ChunkIterator iter = tChunk->getIterator();
1105 void *data = NULL;
1106 while ((data = iter.nextElement())!= NULL)
1108 if (((CFK*)data)->pkTblPtr_== cpkptr && ((CFK*)data)->fkTblPtr_ == cfkptr)
1110 break;
1113 if(data == NULL)
1115 printError(ErrNotExists,"Foreign Key field CFK not found");
1116 return ErrNotExists;
1118 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
1119 iter = fChunk->getIterator();
1120 void *fdata=NULL;
1121 while ((fdata = iter.nextElement())!= NULL)
1123 if (((CFKFIELD*)fdata)->fkPtr_==data)
1125 //printDebug(DM_TEST,"PK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1126 //printDebug(DM_TEST,"FK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1127 pklist.append(((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1128 fklist.append(((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1131 return OK;