Alter Table fixes.
[csql.git] / src / storage / CatalogTables.cxx
blob0ab5a5b7b37f1cd89abfca8cd77db9a76333f018
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::remove(const char *name, void *&chunk, void *&tptr)
88 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
89 ChunkIterator iter = tChunk->getIterator();
91 void *data = NULL;
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)
98 tptr = (void*) data;
99 chunk = (Chunk*) ((CTABLE*)data)->chunkPtr_;
100 break;
103 if (NULL != tptr)
105 tChunk->free(systemDatabase_, tptr);
106 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
108 else
110 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
111 return ErrNotExists;
113 return OK;
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_;
128 return OK;
131 //table not found in TABLE
132 return ErrNotFound;
136 DbRetVal CatalogTableTABLE::setChunkPtr(const char *name, void *firstPage, void *curPage)
138 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
139 ChunkIterator iter = chk->getIterator();;
140 void *tptr;
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);
148 return OK;
151 //table not found in TABLE
152 return ErrNotFound;
155 List CatalogTableTABLE::getTableList()
157 List tableList;
158 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
159 ChunkIterator iter = chk->getIterator();
160 void *tptr;
161 while (NULL != (tptr = iter.nextElement()))
163 Identifier *elem = new Identifier();
164 strcpy(elem->name, ((CTABLE*)tptr)->tblName_);
165 tableList.append(elem);
167 return tableList;
170 DbRetVal CatalogTableFIELD::renameField(const char *tableName, const char *oldName, const char *newName)
172 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
173 ChunkIterator iter = fChunk->getIterator();
174 void *data = NULL;
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);
182 return ErrAlready;
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;
192 break;
195 if(!isFieldExists){
196 printError(ErrNotExists, "Old Field Name '%s' does not exist in table",
197 oldName);
198 return ErrNotExists;
200 return OK;
203 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
205 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
206 DbRetVal rv = OK;
207 while (iter.hasElement())
209 void *fptr = fChunk->allocate(systemDatabase_, &rv);
210 if (NULL == fptr)
212 printError(rv,
213 "Could not allocate for FIELD catalog table");
214 return rv;
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_);
237 return OK;
240 DbRetVal CatalogTableFIELD::remove(void *tptr)
242 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
243 ChunkIterator fIter = fChunk->getIterator();
244 void *data = NULL;
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);
254 return OK;
257 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
259 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
260 ChunkIterator fIter = fChunk->getIterator();;
261 void *data = NULL;
262 void *ptrToAutoVal;
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;
269 FieldDef fldDef;
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_,);
286 list.append(fldDef);
289 return ptrToAutoVal;
292 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
294 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
295 int i=0;
296 char *fName = NULL;
297 bool found = false;
298 fldList.resetIter();
299 void *data = NULL;
300 DbRetVal rv =OK;
301 while (NULL != (fName = fldList.nextFieldName()))
303 ChunkIterator fIter = fChunk->getIterator();
304 found = false;
305 while (NULL != (data = fIter.nextElement()))
307 if (((CFIELD*)data)->tblPtr_ == tptr)
309 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
311 found = true;
312 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
313 fptr[i++] = (char*) data;
314 break;
318 if (!found)
320 printError(ErrNotFound,
321 "No entries found in FIELD catalog table for the table specified");
322 return ErrNotFound;
325 return rv;
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
336 void *data = NULL;
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_);
343 return ErrAlready;
348 DbRetVal rv =OK;
349 tupleptr = tChunk->allocate(systemDatabase_, &rv);
350 if (NULL == tupleptr)
352 printError(rv,
353 "Could not allocate for INDEX catalog table");
354 return rv;
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;
361 if (NULL == hChunk)
362 indexInfo->indexType_ = treeIndex;
363 else
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);
371 return OK;
374 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
376 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
377 ChunkIterator iter = fChunk->getIterator();
379 void *data = NULL;
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_;
388 iptr = (void*) data;
389 break;
392 if (NULL != iptr)
394 fChunk->free(systemDatabase_, iptr);
395 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
397 else
399 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
400 return ErrNotExists;
402 return OK;
404 DbRetVal CatalogTableINDEX::get(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 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
425 return ErrNotExists;
427 return OK;
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();
435 void *data = NULL;
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)
442 if (type == hIdx) {
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;
452 break;
455 return OK;
458 int CatalogTableINDEX::getNumIndexes(void *tptr)
460 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
461 ChunkIterator iter = fChunk->getIterator();
462 void *iptr = NULL;
463 int numIndex =0;
464 while (NULL != (iptr = iter.nextElement()))
466 if (((CINDEX*)iptr)->tblPtr_ == tptr) numIndex++;
468 return numIndex;
471 ListIterator CatalogTableINDEXFIELD::getIndexListIterater(char *name)
473 List indexList;
474 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
475 ChunkIterator ifIter = chunk->getIterator();
476 void *data = NULL;
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();
499 void *iptr = NULL;
500 int numIndex =0;
501 int curPos =0;
502 while (NULL != (iptr = iter.nextElement()))
504 if (((CINDEX*)iptr)->tblPtr_ == tptr) curPos++;
505 if ( curPos == position ) return ((CINDEX*)iptr)->indName_;
507 return NULL;
511 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
513 void *iptr = NULL;
514 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
515 ChunkIterator iter = fChunk->getIterator();
516 int i=0;
517 while (NULL != (iptr = iter.nextElement()))
519 if (((CINDEX*)iptr)->tblPtr_ == tptr)
521 array[i++] = (char*) iptr;
524 return;
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)
564 Chunk *tChunk;
565 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
566 ChunkIterator iter = tChunk->getIterator();
567 CINDEXFIELD *fInd=NULL;
568 char *fName =NULL;
569 void *data = NULL;
570 bool isFldInd=false;
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) )
578 fldList.resetIter();
579 while (NULL != (fName = fldList.nextFieldName()))
581 isFldInd=false;
582 fInd=(CINDEXFIELD*)((CINDEX*)data)->fstIndFld_ ;
583 while (fInd)
585 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
587 isFldInd=true;
588 break;
590 fInd=fInd->next;
592 if(!isFldInd) break;
594 if(isFldInd)
596 printError(ErrAlready, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE *)tblPtr)->tblName_, ((CINDEX *)data)->indName_);
597 return ErrAlready;
603 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
604 fldList.resetIter();
605 int i =0;
606 while (NULL != (fName = fldList.nextFieldName()))
608 DbRetVal rv = OK;
609 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
610 while(fInd)
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_;
616 CINDEXFIELD *fldI;
617 while(fInd)
619 fldI=fInd;
620 fInd=fInd->next;
621 tChunk->free(systemDatabase_,fldI);
623 return ErrAlready;
625 fInd=fInd->next;
627 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
628 if (NULL == fieldptr)
630 printError(rv, "Could not allocate for USER catalog table");
631 return rv;
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);
641 return OK;
644 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
646 Chunk *fChunk;
647 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
648 ChunkIterator fIter = fChunk->getIterator();
649 void *data = NULL;
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);
660 return OK;
663 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
664 char *&name, DataType &type)
666 Chunk *ifChunk;
667 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
668 ChunkIterator ifIter = ifChunk->getIterator();
669 void *data = NULL;
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_;
677 return OK;
680 printError(ErrNotExists,"Index %x not exists in catalog table", index);
681 return ErrNotExists;
684 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
686 Chunk *ifChunk;
687 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
688 ChunkIterator ifIter = ifChunk->getIterator();
689 void *data = NULL;
690 int rowCount =0;
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);
697 FieldDef fldDef;
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_;
709 list.append(fldDef);
711 rowCount++;
713 if (!rowCount) {
714 printError(ErrNotExists,"Index %x not exists in catalog table", index);
715 return ErrNotExists;
717 return OK;
720 void CatalogTableINDEXFIELD::printAllIndex()
722 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
723 ChunkIterator ifIter = chunk->getIterator();
724 void *data = NULL;
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");
733 else
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_);
738 else
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()
748 List userList;
749 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
750 ChunkIterator iter = chk->getIterator();
751 void *tptr;
752 while (NULL != (tptr = iter.nextElement()))
754 Identifier *elem = new Identifier();
755 strcpy(elem->name, ((CUSER*)tptr)->userName_);
756 userList.append(elem);
758 return userList;
762 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
764 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
765 DbRetVal rv = OK;
766 ChunkIterator iter = tChunk->getIterator();
767 void *data = NULL;
768 while ((data = iter.nextElement())!= NULL)
770 if (0 == strcmp(((CUSER*)data)->userName_, name))
772 printError(ErrAlready, "User with name \'%s\' already exists ", name);
773 return ErrAlready;
777 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
778 if (NULL == usrInfo)
780 printError(rv,
781 "Could not allocate for USER catalog table");
782 return rv;
784 strcpy(usrInfo->userName_, name);
785 strcpy(usrInfo->password_, pass);
786 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
787 return OK;
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();
796 void *data = NULL;
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;
809 return OK;
813 isAuthenticated = false;
814 return OK;
817 DbRetVal CatalogTableUSER::remove(const char *name)
819 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
820 ChunkIterator iter = tChunk->getIterator();
821 void *data = NULL;
822 while ((data = iter.nextElement())!= NULL)
824 if (strcmp(((CUSER*)data)->userName_, name) == 0)
826 //remove this element
827 tChunk->free(systemDatabase_, data);
828 return OK;
831 printError(ErrNotExists,"User %s not exists in catalog table", name);
832 return ErrNotExists;
835 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
837 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
838 ChunkIterator iter = tChunk->getIterator();
839 void *data = NULL;
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"));
846 return OK;
849 printError(ErrNotExists,"User %s not exists in catalog table", name);
850 return ErrNotExists;
853 DbRetVal CatalogTableFK::insert(char *name, void *tptr, void *tPkptr)
855 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
856 ChunkIterator iter = tChunk->getIterator();
857 void *data = NULL;
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_);
864 return ErrAlready;
869 DbRetVal rv =OK;
870 void *fkptr = tChunk->allocate(systemDatabase_, &rv);
871 if (NULL == fkptr)
873 printError(rv, "Could not allocate for FK catalog table");
874 return rv;
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);
881 return OK;
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();
889 void *data = NULL;
890 while ((data = iter.nextElement())!= NULL)
892 if (0 == strcmp(((CFK*)data)->fkName_, cFKName))
894 break;
897 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
898 int i =0;
899 DbRetVal rv = OK;
900 while (i < totalFld)
902 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
903 if (NULL == fieldptr)
905 printError(rv, "Could not allocate for USER catalog table");
906 return rv;
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);
928 return OK;
930 DbRetVal CatalogTableFK::remove(void *ctptr)
932 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
933 ChunkIterator iter = tChunk->getIterator();
934 void *data = NULL;
935 while ((data = iter.nextElement())!= NULL)
937 if (data == ctptr)
939 tChunk->free(systemDatabase_,data);
940 printDebug(DM_SystemDatabase,"One Row deleted from FKFIELD %x", data);
944 return OK;
947 DbRetVal CatalogTableFKFIELD::remove(void *cFKfld)
949 Chunk *fChunk = NULL;
950 fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
951 ChunkIterator iter = fChunk->getIterator();
952 void *data = NULL;
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);
961 return OK;
963 void *CatalogTableFK::getFkCTable(void *ctptr)
965 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
966 ChunkIterator iter = tChunk->getIterator();
967 void *data = NULL;
968 while ((data = iter.nextElement())!= NULL)
970 if (((CFK*)data)->fkTblPtr_== ctptr)
972 return data;
976 return NULL;
978 int CatalogTableFK::getNumFkTable(void *ctptr)
980 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
981 ChunkIterator iter = tChunk->getIterator();
982 void *data = NULL;
983 int count=0;
984 while ((data = iter.nextElement())!= NULL)
986 if (((CFK*)data)->pkTblPtr_== ctptr)
988 count++;
991 return count;
994 bool CatalogTableFK::isFkTable(void *ctptr)
996 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
997 ChunkIterator iter = tChunk->getIterator();
998 void *data = NULL;
999 int count=0;
1000 while ((data = iter.nextElement())!= NULL)
1002 if (((CFK*)data)->fkTblPtr_== ctptr)
1004 return true;
1007 return false;
1009 int CatalogTableFK::getNoOfFkTable(void *ctptr)
1011 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1012 ChunkIterator iter = tChunk->getIterator();
1013 void *data = NULL;
1014 int count=0;
1015 while ((data = iter.nextElement())!= NULL)
1017 if (((CFK*)data)->pkTblPtr_== ctptr)
1019 count++;
1022 return count;
1025 int CatalogTableFK::getNoOfPkTable(void *ctptr)
1027 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1028 ChunkIterator iter = tChunk->getIterator();
1029 void *data = NULL;
1030 int count=0;
1031 while ((data = iter.nextElement())!= NULL)
1033 if (((CFK*)data)->fkTblPtr_== ctptr)
1035 count++;
1038 return count;
1041 void CatalogTableFK::getPkTableName(void *ctptr,char **&array)
1043 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1044 ChunkIterator iter = tChunk->getIterator();
1045 void *data = NULL;
1046 int i=0;
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();
1059 void *data = NULL;
1060 int i=0;
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();
1075 void *data = NULL;
1076 while ((data = iter.nextElement())!= NULL)
1078 if (((CFK*)data)->pkTblPtr_== cpkptr && ((CFK*)data)->fkTblPtr_ == cfkptr)
1080 break;
1083 if(data == NULL)
1085 printError(ErrNotExists,"Foreign Key field CFK not found");
1086 return ErrNotExists;
1088 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
1089 iter = fChunk->getIterator();
1090 void *fdata=NULL;
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_);
1101 return OK;