statement caching modifications and cache stmt display
[csql.git] / src / storage / CatalogTables.cxx
blobc2a092fdb58011ccd5e272bc98c14a13247736e7
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_,oldName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
180 strcpy(((CFIELD*)data)->fldName_,newName);
181 isFieldExists = true;
182 break;
185 if(!isFieldExists){
186 printError(ErrNotExists,"Field %s not exists in table", oldName);
187 return ErrNotExists;
189 return OK;
192 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
194 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
195 DbRetVal rv = OK;
196 while (iter.hasElement())
198 void *fptr = fChunk->allocate(systemDatabase_, &rv);
199 if (NULL == fptr)
201 printError(rv,
202 "Could not allocate for FIELD catalog table");
203 return rv;
205 CFIELD *fldInfo = (CFIELD*)fptr;
206 FieldDef *fDef = iter.nextElement();
207 strcpy(fldInfo->fldName_, fDef->fldName_);
208 fldInfo->tblID_ = tblID;
209 fldInfo->tblPtr_ = tptr;
210 fldInfo->type_ = fDef->type_;
211 fldInfo->length_ = fDef->length_;
212 fldInfo->offset_ = fDef->offset_;
213 os::memcpy(fldInfo->defaultValueBuf_, fDef->defaultValueBuf_,
214 DEFAULT_VALUE_BUF_LENGTH);
215 fldInfo->isNull_ = fDef->isNull_;
216 fldInfo->isPrimary_ = fDef->isPrimary_;
217 fldInfo->isUnique_ = fDef->isUnique_;
218 fldInfo->isDefault_ = fDef->isDefault_;
219 fldInfo->isAutoIncrement_= fDef->isAutoIncrement_;
220 fldInfo->autoVal_ = 0;
221 fldInfo->width_ = 0; //TODO
222 fldInfo->scale_ = 0; //TODO
223 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef->fldName_);
226 return OK;
229 DbRetVal CatalogTableFIELD::remove(void *tptr)
231 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
232 ChunkIterator fIter = fChunk->getIterator();
233 void *data = NULL;
234 while ((data = fIter.nextElement())!= NULL)
236 if (((CFIELD*)data)->tblPtr_ == tptr)
238 //remove this element
239 fChunk->free(systemDatabase_, data);
240 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
243 return OK;
246 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
248 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
249 ChunkIterator fIter = fChunk->getIterator();;
250 void *data = NULL;
251 void *ptrToAutoVal;
252 while (NULL != (data = fIter.nextElement()))
254 if (((CFIELD*)data)->tblPtr_ == tptr)
256 //add the information to the field list
257 CFIELD *fTuple = (CFIELD*)data;
258 FieldDef fldDef;
259 strcpy(fldDef.fldName_, fTuple->fldName_);
260 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
261 fldDef.type_ = fTuple->type_;
262 fldDef.length_ = fTuple->length_;
263 fldDef.offset_ = fTuple->offset_;
264 fldDef.isDefault_ = fTuple->isDefault_;
265 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
266 DEFAULT_VALUE_BUF_LENGTH);
267 fldDef.isNull_ = fTuple->isNull_;
268 fldDef.isUnique_ = fTuple->isUnique_;
269 fldDef.isPrimary_ = fTuple->isPrimary_;
270 fldDef.isAutoIncrement_= fTuple->isAutoIncrement_;
271 if(fTuple->isAutoIncrement_){
272 ptrToAutoVal = &fTuple->autoVal_;
273 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
275 list.append(fldDef);
278 return ptrToAutoVal;
281 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
283 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
284 int i=0;
285 char *fName = NULL;
286 bool found = false;
287 fldList.resetIter();
288 void *data = NULL;
289 DbRetVal rv =OK;
290 while (NULL != (fName = fldList.nextFieldName()))
292 ChunkIterator fIter = fChunk->getIterator();
293 found = false;
294 while (NULL != (data = fIter.nextElement()))
296 if (((CFIELD*)data)->tblPtr_ == tptr)
298 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
300 found = true;
301 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
302 fptr[i++] = (char*) data;
303 break;
307 if (!found)
309 printError(ErrNotFound,
310 "No entries found in FIELD catalog table for the table specified");
311 return ErrNotFound;
314 return rv;
317 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
318 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
320 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
321 ChunkIterator iter = tChunk->getIterator();
323 //Checking for index having same name, proceed further only
324 //if no such indexes are
325 void *data = NULL;
326 while ((data = iter.nextElement())!= NULL)
328 if (0 == strcmp(((CINDEX*)data)->indName_, name))
330 printError(ErrAlready, "Index with name \'%s\' already exists "
331 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
332 return ErrAlready;
337 DbRetVal rv =OK;
338 tupleptr = tChunk->allocate(systemDatabase_, &rv);
339 if (NULL == tupleptr)
341 printError(rv,
342 "Could not allocate for INDEX catalog table");
343 return rv;
345 CINDEX *indexInfo = (CINDEX*)tupleptr;
346 strcpy(indexInfo->indName_, name);
347 indexInfo->tblID_ = -1; //Not used currently
348 indexInfo->tblPtr_ = tptr;
349 indexInfo->numFlds_ = numFlds;
350 if (NULL == hChunk)
351 indexInfo->indexType_ = treeIndex;
352 else
353 indexInfo->indexType_ = hashIndex;
354 indexInfo->chunkPtr_ = chunk;
355 indexInfo->hashNodeChunk_ = hChunk;
356 indexInfo->noOfBuckets_ = bucketSize;
357 indexInfo->isUnique_ = isUnique;
358 indexInfo->fstIndFld_=NULL;
359 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
360 return OK;
363 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
365 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
366 ChunkIterator iter = fChunk->getIterator();
368 void *data = NULL;
369 while ((data = iter.nextElement())!= NULL)
371 if (0 == strcmp(((CINDEX*)data)->indName_, name))
373 //remove this element and store the tuple ptr
374 //there will be only one row for this table(Primary key)
375 chunk = (Chunk*) ((CINDEX*)data)->chunkPtr_;
376 hchunk = (Chunk*) ((CINDEX*)data)->hashNodeChunk_;
377 iptr = (void*) data;
378 break;
381 if (NULL != iptr)
383 fChunk->free(systemDatabase_, iptr);
384 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
386 else
388 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
389 return ErrNotExists;
391 return OK;
393 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
395 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
396 ChunkIterator iter = fChunk->getIterator();
398 void *data = NULL;
399 while ((data = iter.nextElement())!= NULL)
401 if (0 == strcmp(((CINDEX*)data)->indName_, name))
403 //remove this element and store the tuple ptr
404 //there will be only one row for this table(Primary key)
405 chunk = (Chunk*) ((CINDEX*)data)->chunkPtr_;
406 hchunk = (Chunk*) ((CINDEX*)data)->hashNodeChunk_;
407 iptr = (void*) data;
408 break;
411 if (NULL == iptr)
413 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
414 return ErrNotExists;
416 return OK;
419 DbRetVal CatalogTableINDEX::setChunkPtr(const char *name, ObjectType type, void *bChunk, void *firstPage, void *curPage)
421 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
422 ChunkIterator iter = fChunk->getIterator();
424 void *data = NULL;
425 while ((data = iter.nextElement())!= NULL)
427 if (0 == strcmp(((CINDEX*)data)->indName_, name))
429 //remove this element and store the tuple ptr
430 //there will be only one row for this table(Primary key)
431 if (type == hIdx) {
432 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(bChunk);
433 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(bChunk);
434 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setFirstPage(firstPage);
435 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setCurPage(curPage);
436 } else if (type == tIdx) {
437 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(firstPage);
438 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(curPage);
439 ((CINDEX*)data)->hashNodeChunk_ = bChunk;
441 break;
444 return OK;
447 int CatalogTableINDEX::getNumIndexes(void *tptr)
449 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
450 ChunkIterator iter = fChunk->getIterator();
451 void *iptr = NULL;
452 int numIndex =0;
453 while (NULL != (iptr = iter.nextElement()))
455 if (((CINDEX*)iptr)->tblPtr_ == tptr) numIndex++;
457 return numIndex;
460 ListIterator CatalogTableINDEXFIELD::getIndexListIterater(char *name)
462 List indexList;
463 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
464 ChunkIterator ifIter = chunk->getIterator();
465 void *data = NULL;
466 while ((data = ifIter.nextElement())!= NULL)
468 IndexInfoForDriver *idxInfo = new IndexInfoForDriver();
469 if(strcmp( name,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_) == 0)
471 strcpy(idxInfo->indexName ,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
472 strcpy(idxInfo->tableName ,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
473 strcpy(idxInfo->fieldName ,((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
474 idxInfo->type = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_ ;
475 idxInfo->isUnique = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->isUnique_;
476 idxInfo->isPrimary = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->isPrimary_;
477 indexList.append(idxInfo);
480 return indexList.getIterator();
483 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
485 if (position == 0) return NULL;
486 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
487 ChunkIterator iter = fChunk->getIterator();
488 void *iptr = NULL;
489 int numIndex =0;
490 int curPos =0;
491 while (NULL != (iptr = iter.nextElement()))
493 if (((CINDEX*)iptr)->tblPtr_ == tptr) curPos++;
494 if ( curPos == position ) return ((CINDEX*)iptr)->indName_;
496 return NULL;
500 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
502 void *iptr = NULL;
503 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
504 ChunkIterator iter = fChunk->getIterator();
505 int i=0;
506 while (NULL != (iptr = iter.nextElement()))
508 if (((CINDEX*)iptr)->tblPtr_ == tptr)
510 array[i++] = (char*) iptr;
513 return;
516 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
518 CINDEX *index = (CINDEX*)iptr;
519 return ((Chunk*)index->chunkPtr_)->getIterator();
523 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
525 CINDEX *index = (CINDEX*)iptr;
526 return index->noOfBuckets_;
529 int CatalogTableINDEX::getUnique(void *iptr)
531 CINDEX *index = (CINDEX*)iptr;
532 return index->isUnique_;
534 IndexType CatalogTableINDEX::getType(void *iptr)
536 CINDEX *index = (CINDEX*)iptr;
537 return index->indexType_;
539 char* CatalogTableINDEX::getName(void *iptr)
541 CINDEX *index = (CINDEX*)iptr;
542 return index->indName_;
544 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr)
546 CINDEX *index = (CINDEX*)iptr;
547 return ((CFIELD*)(((CINDEXFIELD*)(index->fstIndFld_))->fieldPtr))->offset_;
549 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
550 void *tblPtr, char **&fptr)
553 Chunk *tChunk;
554 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
555 ChunkIterator iter = tChunk->getIterator();
556 CINDEXFIELD *fInd=NULL;
557 char *fName =NULL;
558 void *data = NULL;
559 bool isFldInd=false;
560 while ((data = iter.nextElement())!= NULL)
562 if ((((CINDEX*)data)->tblPtr_==tblPtr)
563 && (((CINDEX*)indexPtr)->numFlds_ == ((CINDEX*)data)->numFlds_)
564 && (((CINDEX*)indexPtr)->indexType_==((CINDEX*)data)->indexType_)
565 && (data != indexPtr) )
567 fldList.resetIter();
568 while (NULL != (fName = fldList.nextFieldName()))
570 isFldInd=false;
571 fInd=(CINDEXFIELD*)((CINDEX*)data)->fstIndFld_ ;
572 while (fInd)
574 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
576 isFldInd=true;
577 break;
579 fInd=fInd->next;
581 if(!isFldInd) break;
583 if(isFldInd)
585 printError(ErrAlready, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE *)tblPtr)->tblName_, ((CINDEX *)data)->indName_);
586 return ErrAlready;
592 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
593 fldList.resetIter();
594 int i =0;
595 while (NULL != (fName = fldList.nextFieldName()))
597 DbRetVal rv = OK;
598 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
599 while(fInd)
601 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
603 printError(ErrAlready,"Composite Index Can't be created with same Name");
604 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
605 CINDEXFIELD *fldI;
606 while(fInd)
608 fldI=fInd;
609 fInd=fInd->next;
610 tChunk->free(systemDatabase_,fldI);
612 return ErrAlready;
614 fInd=fInd->next;
616 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
617 if (NULL == fieldptr)
619 printError(rv, "Could not allocate for USER catalog table");
620 return rv;
622 CINDEXFIELD *fldInfo = (CINDEXFIELD*)fieldptr;
623 fldInfo->tablePtr = tblPtr;
624 fldInfo->fieldPtr = (CFIELD*)fptr[i++];
625 fldInfo->indexPtr = indexPtr;
626 fldInfo->next=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
627 ((CINDEX *)indexPtr)->fstIndFld_=fldInfo;
628 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
630 return OK;
633 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
635 Chunk *fChunk;
636 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
637 ChunkIterator fIter = fChunk->getIterator();
638 void *data = NULL;
639 while ((data = fIter.nextElement())!= NULL)
641 if (((CINDEXFIELD*)data)->indexPtr == iptr)
643 //remove this element
644 if(((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_) ((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_ = false;
645 fChunk->free(systemDatabase_, data);
646 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
649 return OK;
652 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
653 char *&name, DataType &type)
655 Chunk *ifChunk;
656 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
657 ChunkIterator ifIter = ifChunk->getIterator();
658 void *data = NULL;
659 while ((data = ifIter.nextElement())!= NULL)
661 if (((CINDEXFIELD*)data)->indexPtr == index)
663 //store the field name
664 name = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_;
665 type = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->type_;
666 return OK;
669 printError(ErrNotExists,"Index %x not exists in catalog table", index);
670 return ErrNotExists;
673 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
675 Chunk *ifChunk;
676 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
677 ChunkIterator ifIter = ifChunk->getIterator();
678 void *data = NULL;
679 int rowCount =0;
680 while ((data = ifIter.nextElement())!= NULL)
682 if (((CINDEXFIELD*)data)->indexPtr == index)
684 //add the information to the field list
685 CFIELD *fTuple = (CFIELD*)(((CINDEXFIELD*)data)->fieldPtr);
686 FieldDef fldDef;
687 strcpy(fldDef.fldName_, fTuple->fldName_);
688 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
689 fldDef.type_ = fTuple->type_;
690 fldDef.length_ = fTuple->length_;
691 fldDef.offset_ = fTuple->offset_;
692 fldDef.isDefault_ = fTuple->isDefault_;
693 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
694 DEFAULT_VALUE_BUF_LENGTH);
695 fldDef.isNull_ = fTuple->isNull_;
696 fldDef.isUnique_ = fTuple->isUnique_;
697 fldDef.isPrimary_ = fTuple->isPrimary_;
698 list.append(fldDef);
700 rowCount++;
702 if (!rowCount) {
703 printError(ErrNotExists,"Index %x not exists in catalog table", index);
704 return ErrNotExists;
706 return OK;
709 void CatalogTableINDEXFIELD::printAllIndex()
711 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
712 ChunkIterator ifIter = chunk->getIterator();
713 void *data = NULL;
714 char indexName[IDENTIFIER_LENGTH] = {'\0'};
715 while ((data = ifIter.nextElement())!= NULL)
717 if(strcmp(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_)!=0)
719 printf(" <Index Name> %s </Index Name> \n",((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
720 if(0==((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_)
721 printf(" <Index Type> Hash Index </Index Type> \n");
722 else
723 printf(" <Index Type> Tree Index </Index Type> \n");
724 printf(" <Table Name> %s </Table Name> \n",((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
725 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
727 else
729 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
731 strcpy(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
735 List CatalogTableUSER::getUserList()
737 List userList;
738 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
739 ChunkIterator iter = chk->getIterator();
740 void *tptr;
741 while (NULL != (tptr = iter.nextElement()))
743 Identifier *elem = new Identifier();
744 strcpy(elem->name, ((CUSER*)tptr)->userName_);
745 userList.append(elem);
747 return userList;
751 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
753 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
754 DbRetVal rv = OK;
755 ChunkIterator iter = tChunk->getIterator();
756 void *data = NULL;
757 while ((data = iter.nextElement())!= NULL)
759 if (0 == strcmp(((CUSER*)data)->userName_, name))
761 printError(ErrAlready, "User with name \'%s\' already exists ", name);
762 return ErrAlready;
766 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
767 if (NULL == usrInfo)
769 printError(rv,
770 "Could not allocate for USER catalog table");
771 return rv;
773 strcpy(usrInfo->userName_, name);
774 strcpy(usrInfo->password_, pass);
775 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
776 return OK;
780 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
781 bool &isAuthenticated, bool &isDba)
783 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
784 ChunkIterator iter = tChunk->getIterator();
785 void *data = NULL;
786 while (NULL != (data = iter.nextElement()))
788 if (strcmp(((CUSER*)data)->userName_, name) == 0)
790 //verify the password
791 //char * enpass = os::encrypt(pass,"A0");
792 char * enpass = (char*) pass;
793 if (0 == strcmp(enpass, ((CUSER*)data)->password_))
795 isAuthenticated = true;
796 if (0 == strcmp(((CUSER*)data)->userName_, DBAUSER))
797 isDba = true; else isDba = false;
798 return OK;
802 isAuthenticated = false;
803 return OK;
806 DbRetVal CatalogTableUSER::remove(const char *name)
808 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
809 ChunkIterator iter = tChunk->getIterator();
810 void *data = NULL;
811 while ((data = iter.nextElement())!= NULL)
813 if (strcmp(((CUSER*)data)->userName_, name) == 0)
815 //remove this element
816 tChunk->free(systemDatabase_, data);
817 return OK;
820 printError(ErrNotExists,"User %s not exists in catalog table", name);
821 return ErrNotExists;
824 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
826 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
827 ChunkIterator iter = tChunk->getIterator();
828 void *data = NULL;
829 while (NULL != (data = iter.nextElement()))
831 if (strcmp(((CUSER*)data)->userName_, name) == 0)
833 //change the password
834 strcpy(((CUSER*)data)->password_,pass);// os::encrypt(pass, "A0"));
835 return OK;
838 printError(ErrNotExists,"User %s not exists in catalog table", name);
839 return ErrNotExists;
842 DbRetVal CatalogTableFK::insert(char *name, void *tptr, void *tPkptr)
844 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
845 ChunkIterator iter = tChunk->getIterator();
846 void *data = NULL;
847 while ((data = iter.nextElement())!= NULL)
849 if (0 == strcmp(((CFK*)data)->fkName_, name))
851 printError(ErrAlready, "Index with name \'%s\' already exists "
852 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
853 return ErrAlready;
858 DbRetVal rv =OK;
859 void *fkptr = tChunk->allocate(systemDatabase_, &rv);
860 if (NULL == fkptr)
862 printError(rv, "Could not allocate for FK catalog table");
863 return rv;
865 CFK *fkTblInfo = (CFK*)fkptr;
866 strcpy(fkTblInfo->fkName_, name);
867 fkTblInfo->fkTblPtr_= tptr;
868 fkTblInfo->pkTblPtr_= tPkptr;
869 printDebug(DM_SystemDatabase,"One Row inserted into FK %x %s",fkptr, name);
870 return OK;
873 DbRetVal CatalogTableFKFIELD::insert(char *cFKName, char **fkFldPtrs, char **pkFldPtrs,int totalFld)
875 Chunk *tChunk = NULL;
876 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
877 ChunkIterator iter = tChunk->getIterator();
878 void *data = NULL;
879 while ((data = iter.nextElement())!= NULL)
881 if (0 == strcmp(((CFK*)data)->fkName_, cFKName))
883 break;
886 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
887 int i =0;
888 DbRetVal rv = OK;
889 while (i < totalFld)
891 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
892 if (NULL == fieldptr)
894 printError(rv, "Could not allocate for USER catalog table");
895 return rv;
897 CFKFIELD *fldInfo = (CFKFIELD*)fieldptr;
898 fldInfo->fkPtr_ = data;
899 fldInfo->pfFldPtr_ = (CFIELD*)pkFldPtrs[i];
900 fldInfo->fkFldPtr_ = (CFIELD*)fkFldPtrs[i++];
901 //printDebug(DM_TEST,"TYPE %d\n",((CFIELD*)fldInfo->pfFldPtr_)->type_);
902 //printDebug(DM_TEST,"FK name %s\n",((CFIELD*)fldInfo->fkFldPtr_)->fldName_);
903 if(!(((CFIELD*)fldInfo->pfFldPtr_)->isUnique_) || !(((CFIELD*)fldInfo->pfFldPtr_)->isNull_))
905 printError(ErrSysInternal,"Parent Table field should have primary key field ");
906 tChunk->free(systemDatabase_,fieldptr);
907 return ErrSysInternal;
909 if(((CFIELD*)fldInfo->pfFldPtr_)->type_!=((CFIELD*)fldInfo->fkFldPtr_)->type_)
911 printError(ErrSysInternal,"Type Missmatch in both PK field and FK field ");
912 tChunk->free(systemDatabase_,fieldptr);
913 return ErrSysInternal;
915 printDebug(DM_SystemDatabase,"One Row inserted into FKFIELD %x", fldInfo);
917 return OK;
919 DbRetVal CatalogTableFK::remove(void *ctptr)
921 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
922 ChunkIterator iter = tChunk->getIterator();
923 void *data = NULL;
924 while ((data = iter.nextElement())!= NULL)
926 if (data == ctptr)
928 tChunk->free(systemDatabase_,data);
929 printDebug(DM_SystemDatabase,"One Row deleted from FKFIELD %x", data);
933 return OK;
936 DbRetVal CatalogTableFKFIELD::remove(void *cFKfld)
938 Chunk *fChunk = NULL;
939 fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
940 ChunkIterator iter = fChunk->getIterator();
941 void *data = NULL;
942 while ((data = iter.nextElement())!= NULL)
944 if (((CFKFIELD*)data)->fkPtr_== cFKfld )
946 fChunk->free(systemDatabase_, data);
947 printDebug(DM_SystemDatabase,"One Row deleted from CFKFIELD %x", data);
950 return OK;
952 void *CatalogTableFK::getFkCTable(void *ctptr)
954 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
955 ChunkIterator iter = tChunk->getIterator();
956 void *data = NULL;
957 while ((data = iter.nextElement())!= NULL)
959 if (((CFK*)data)->fkTblPtr_== ctptr)
961 return data;
965 return NULL;
967 int CatalogTableFK::getNumFkTable(void *ctptr)
969 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
970 ChunkIterator iter = tChunk->getIterator();
971 void *data = NULL;
972 int count=0;
973 while ((data = iter.nextElement())!= NULL)
975 if (((CFK*)data)->pkTblPtr_== ctptr)
977 count++;
980 return count;
983 bool CatalogTableFK::isFkTable(void *ctptr)
985 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
986 ChunkIterator iter = tChunk->getIterator();
987 void *data = NULL;
988 int count=0;
989 while ((data = iter.nextElement())!= NULL)
991 if (((CFK*)data)->fkTblPtr_== ctptr)
993 return true;
996 return false;
998 int CatalogTableFK::getNoOfFkTable(void *ctptr)
1000 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1001 ChunkIterator iter = tChunk->getIterator();
1002 void *data = NULL;
1003 int count=0;
1004 while ((data = iter.nextElement())!= NULL)
1006 if (((CFK*)data)->pkTblPtr_== ctptr)
1008 count++;
1011 return count;
1014 int CatalogTableFK::getNoOfPkTable(void *ctptr)
1016 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1017 ChunkIterator iter = tChunk->getIterator();
1018 void *data = NULL;
1019 int count=0;
1020 while ((data = iter.nextElement())!= NULL)
1022 if (((CFK*)data)->fkTblPtr_== ctptr)
1024 count++;
1027 return count;
1030 void CatalogTableFK::getPkTableName(void *ctptr,char **&array)
1032 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1033 ChunkIterator iter = tChunk->getIterator();
1034 void *data = NULL;
1035 int i=0;
1036 while ((data = iter.nextElement())!= NULL)
1038 if (((CFK*)data)->fkTblPtr_== ctptr)
1040 array[i++] = ((CTABLE*)((CFK*)data)->pkTblPtr_)->tblName_;
1044 void CatalogTableFK::getFkTableName(void *ctptr,char **&array)
1046 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1047 ChunkIterator iter = tChunk->getIterator();
1048 void *data = NULL;
1049 int i=0;
1050 while ((data = iter.nextElement())!= NULL)
1052 if (((CFK*)data)->pkTblPtr_== ctptr)
1054 array[i++] = ((CTABLE*)((CFK*)data)->fkTblPtr_)->tblName_;
1060 DbRetVal CatalogTableFK::getPkFkFieldInfo(void *cpkptr, void *cfkptr, FieldNameList &pklist,FieldNameList &fklist)
1062 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1063 ChunkIterator iter = tChunk->getIterator();
1064 void *data = NULL;
1065 while ((data = iter.nextElement())!= NULL)
1067 if (((CFK*)data)->pkTblPtr_== cpkptr && ((CFK*)data)->fkTblPtr_ == cfkptr)
1069 break;
1072 if(data == NULL)
1074 printError(ErrNotExists,"Foreign Key field CFK not found");
1075 return ErrNotExists;
1077 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
1078 iter = fChunk->getIterator();
1079 void *fdata=NULL;
1080 while ((fdata = iter.nextElement())!= NULL)
1082 if (((CFKFIELD*)fdata)->fkPtr_==data)
1084 //printDebug(DM_TEST,"PK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1085 //printDebug(DM_TEST,"FK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1086 pklist.append(((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1087 fklist.append(((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1090 return OK;