Varchar Implementation part 1
[csql.git] / src / storage / CatalogTables.cxx
blob5456f62355650ec0db5df01eca2b916f0517906d
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;
48 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
50 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
51 ChunkIterator iter = tChunk->getIterator();
53 void *data = NULL;
54 while ((data = iter.nextElement())!= NULL)
56 if (0 == strcmp(((CTABLE*)data)->tblName_, name))
58 //remove this element and store the tblPtr
59 //there will be only one row for this table(Primary key)
60 tptr = (void*) data;
61 chunk = (Chunk*) ((CTABLE*)data)->chunkPtr_;
62 break;
65 if (NULL != tptr)
67 tChunk->free(systemDatabase_, tptr);
68 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
70 else
72 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
73 return ErrNotExists;
75 return OK;
78 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
79 void *&chunk, void *&tptr, void *&vcchunk)
81 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
82 ChunkIterator iter = chk->getIterator();;
83 while (NULL != (tptr = iter.nextElement()))
85 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
87 //there will be only one row for this table(Primary key)
88 chunk = (Chunk*) ((CTABLE*)tptr)->chunkPtr_;
89 vcchunk = (Chunk*) ((CTABLE*)tptr)->varcharChunkPtr_;
90 return OK;
93 //table not found in TABLE
94 return ErrNotFound;
98 DbRetVal CatalogTableTABLE::setChunkPtr(const char *name, void *firstPage, void *curPage)
100 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
101 ChunkIterator iter = chk->getIterator();;
102 void *tptr;
103 while (NULL != (tptr = iter.nextElement()))
105 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
107 //there will be only one row for this table(Primary key)
108 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setFirstPage(firstPage);
109 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setCurPage(curPage);
110 return OK;
113 //table not found in TABLE
114 return ErrNotFound;
117 List CatalogTableTABLE::getTableList()
119 List tableList;
120 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
121 ChunkIterator iter = chk->getIterator();
122 void *tptr;
123 while (NULL != (tptr = iter.nextElement()))
125 Identifier *elem = new Identifier();
126 strcpy(elem->name, ((CTABLE*)tptr)->tblName_);
127 tableList.append(elem);
129 return tableList;
132 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
134 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
135 DbRetVal rv = OK;
136 while (iter.hasElement())
138 void *fptr = fChunk->allocate(systemDatabase_, &rv);
139 if (NULL == fptr)
141 printError(rv,
142 "Could not allocate for FIELD catalog table");
143 return rv;
145 CFIELD *fldInfo = (CFIELD*)fptr;
146 FieldDef *fDef = iter.nextElement();
147 strcpy(fldInfo->fldName_, fDef->fldName_);
148 fldInfo->tblID_ = tblID;
149 fldInfo->tblPtr_ = tptr;
150 fldInfo->type_ = fDef->type_;
151 fldInfo->length_ = fDef->length_;
152 fldInfo->offset_ = fDef->offset_;
153 os::memcpy(fldInfo->defaultValueBuf_, fDef->defaultValueBuf_,
154 DEFAULT_VALUE_BUF_LENGTH);
155 fldInfo->isNull_ = fDef->isNull_;
156 fldInfo->isPrimary_ = fDef->isPrimary_;
157 fldInfo->isUnique_ = fDef->isUnique_;
158 fldInfo->isDefault_ = fDef->isDefault_;
159 fldInfo->isAutoIncrement_= fDef->isAutoIncrement_;
160 fldInfo->autoVal_ = 0;
161 fldInfo->width_ = 0; //TODO
162 fldInfo->scale_ = 0; //TODO
163 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef->fldName_);
166 return OK;
169 DbRetVal CatalogTableFIELD::remove(void *tptr)
171 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
172 ChunkIterator fIter = fChunk->getIterator();
173 void *data = NULL;
174 while ((data = fIter.nextElement())!= NULL)
176 if (((CFIELD*)data)->tblPtr_ == tptr)
178 //remove this element
179 fChunk->free(systemDatabase_, data);
180 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
183 return OK;
186 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
188 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
189 ChunkIterator fIter = fChunk->getIterator();;
190 void *data = NULL;
191 void *ptrToAutoVal;
192 while (NULL != (data = fIter.nextElement()))
194 if (((CFIELD*)data)->tblPtr_ == tptr)
196 //add the information to the field list
197 CFIELD *fTuple = (CFIELD*)data;
198 FieldDef fldDef;
199 strcpy(fldDef.fldName_, fTuple->fldName_);
200 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
201 fldDef.type_ = fTuple->type_;
202 fldDef.length_ = fTuple->length_;
203 fldDef.offset_ = fTuple->offset_;
204 fldDef.isDefault_ = fTuple->isDefault_;
205 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
206 DEFAULT_VALUE_BUF_LENGTH);
207 fldDef.isNull_ = fTuple->isNull_;
208 fldDef.isUnique_ = fTuple->isUnique_;
209 fldDef.isPrimary_ = fTuple->isPrimary_;
210 fldDef.isAutoIncrement_= fTuple->isAutoIncrement_;
211 if(fTuple->isAutoIncrement_){
212 ptrToAutoVal = &fTuple->autoVal_;
213 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
215 list.append(fldDef);
218 return ptrToAutoVal;
221 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
223 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
224 int i=0;
225 char *fName = NULL;
226 bool found = false;
227 fldList.resetIter();
228 void *data = NULL;
229 DbRetVal rv =OK;
230 while (NULL != (fName = fldList.nextFieldName()))
232 ChunkIterator fIter = fChunk->getIterator();
233 found = false;
234 while (NULL != (data = fIter.nextElement()))
236 if (((CFIELD*)data)->tblPtr_ == tptr)
238 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
240 found = true;
241 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
242 fptr[i++] = (char*) data;
243 break;
247 if (!found)
249 printError(ErrNotFound,
250 "No entries found in FIELD catalog table for the table specified");
251 return ErrNotFound;
254 return rv;
257 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
258 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
260 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
261 ChunkIterator iter = tChunk->getIterator();
263 //Checking for index having same name, proceed further only
264 //if no such indexes are
265 void *data = NULL;
266 while ((data = iter.nextElement())!= NULL)
268 if (0 == strcmp(((CINDEX*)data)->indName_, name))
270 printError(ErrAlready, "Index with name \'%s\' already exists "
271 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
272 return ErrAlready;
277 DbRetVal rv =OK;
278 tupleptr = tChunk->allocate(systemDatabase_, &rv);
279 if (NULL == tupleptr)
281 printError(rv,
282 "Could not allocate for INDEX catalog table");
283 return rv;
285 CINDEX *indexInfo = (CINDEX*)tupleptr;
286 strcpy(indexInfo->indName_, name);
287 indexInfo->tblID_ = -1; //Not used currently
288 indexInfo->tblPtr_ = tptr;
289 indexInfo->numFlds_ = numFlds;
290 if (NULL == hChunk)
291 indexInfo->indexType_ = treeIndex;
292 else
293 indexInfo->indexType_ = hashIndex;
294 indexInfo->chunkPtr_ = chunk;
295 indexInfo->hashNodeChunk_ = hChunk;
296 indexInfo->noOfBuckets_ = bucketSize;
297 indexInfo->isUnique_ = isUnique;
298 indexInfo->fstIndFld_=NULL;
299 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
300 return OK;
303 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
305 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
306 ChunkIterator iter = fChunk->getIterator();
308 void *data = NULL;
309 while ((data = iter.nextElement())!= NULL)
311 if (0 == strcmp(((CINDEX*)data)->indName_, name))
313 //remove this element and store the tuple ptr
314 //there will be only one row for this table(Primary key)
315 chunk = (Chunk*) ((CINDEX*)data)->chunkPtr_;
316 hchunk = (Chunk*) ((CINDEX*)data)->hashNodeChunk_;
317 iptr = (void*) data;
318 break;
321 if (NULL != iptr)
323 fChunk->free(systemDatabase_, iptr);
324 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
326 else
328 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
329 return ErrNotExists;
331 return OK;
333 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
335 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
336 ChunkIterator iter = fChunk->getIterator();
338 void *data = NULL;
339 while ((data = iter.nextElement())!= NULL)
341 if (0 == strcmp(((CINDEX*)data)->indName_, name))
343 //remove this element and store the tuple ptr
344 //there will be only one row for this table(Primary key)
345 chunk = (Chunk*) ((CINDEX*)data)->chunkPtr_;
346 hchunk = (Chunk*) ((CINDEX*)data)->hashNodeChunk_;
347 iptr = (void*) data;
348 break;
351 if (NULL == iptr)
353 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
354 return ErrNotExists;
356 return OK;
359 DbRetVal CatalogTableINDEX::setChunkPtr(const char *name, ObjectType type, void *bChunk, void *firstPage, void *curPage)
361 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
362 ChunkIterator iter = fChunk->getIterator();
364 void *data = NULL;
365 while ((data = iter.nextElement())!= NULL)
367 if (0 == strcmp(((CINDEX*)data)->indName_, name))
369 //remove this element and store the tuple ptr
370 //there will be only one row for this table(Primary key)
371 if (type == hIdx) {
372 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(bChunk);
373 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(bChunk);
374 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setFirstPage(firstPage);
375 ((Chunk*)((CINDEX*)data)->hashNodeChunk_)->setCurPage(curPage);
376 } else if (type == tIdx) {
377 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setFirstPage(firstPage);
378 ((Chunk*) ((CINDEX*)data)->chunkPtr_)->setCurPage(curPage);
379 ((CINDEX*)data)->hashNodeChunk_ = bChunk;
381 break;
384 return OK;
387 int CatalogTableINDEX::getNumIndexes(void *tptr)
389 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
390 ChunkIterator iter = fChunk->getIterator();
391 void *iptr = NULL;
392 int numIndex =0;
393 while (NULL != (iptr = iter.nextElement()))
395 if (((CINDEX*)iptr)->tblPtr_ == tptr) numIndex++;
397 return numIndex;
400 ListIterator CatalogTableINDEXFIELD::getIndexListIterater(char *name)
402 List indexList;
403 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
404 ChunkIterator ifIter = chunk->getIterator();
405 void *data = NULL;
406 while ((data = ifIter.nextElement())!= NULL)
408 IndexInfoForDriver *idxInfo = new IndexInfoForDriver();
409 if(strcmp( name,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_) == 0)
411 strcpy(idxInfo->indexName ,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
412 strcpy(idxInfo->tableName ,((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
413 strcpy(idxInfo->fieldName ,((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
414 idxInfo->type = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_ ;
415 idxInfo->isUnique = ((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->isUnique_;
416 idxInfo->isPrimary = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->isPrimary_;
417 indexList.append(idxInfo);
420 return indexList.getIterator();
423 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
425 if (position == 0) return NULL;
426 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
427 ChunkIterator iter = fChunk->getIterator();
428 void *iptr = NULL;
429 int numIndex =0;
430 int curPos =0;
431 while (NULL != (iptr = iter.nextElement()))
433 if (((CINDEX*)iptr)->tblPtr_ == tptr) curPos++;
434 if ( curPos == position ) return ((CINDEX*)iptr)->indName_;
436 return NULL;
440 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
442 void *iptr = NULL;
443 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
444 ChunkIterator iter = fChunk->getIterator();
445 int i=0;
446 while (NULL != (iptr = iter.nextElement()))
448 if (((CINDEX*)iptr)->tblPtr_ == tptr)
450 array[i++] = (char*) iptr;
453 return;
456 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
458 CINDEX *index = (CINDEX*)iptr;
459 return ((Chunk*)index->chunkPtr_)->getIterator();
463 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
465 CINDEX *index = (CINDEX*)iptr;
466 return index->noOfBuckets_;
469 int CatalogTableINDEX::getUnique(void *iptr)
471 CINDEX *index = (CINDEX*)iptr;
472 return index->isUnique_;
474 IndexType CatalogTableINDEX::getType(void *iptr)
476 CINDEX *index = (CINDEX*)iptr;
477 return index->indexType_;
479 char* CatalogTableINDEX::getName(void *iptr)
481 CINDEX *index = (CINDEX*)iptr;
482 return index->indName_;
484 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr)
486 CINDEX *index = (CINDEX*)iptr;
487 return ((CFIELD*)(((CINDEXFIELD*)(index->fstIndFld_))->fieldPtr))->offset_;
489 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
490 void *tblPtr, char **&fptr)
493 Chunk *tChunk;
494 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
495 ChunkIterator iter = tChunk->getIterator();
496 CINDEXFIELD *fInd=NULL;
497 char *fName =NULL;
498 void *data = NULL;
499 bool isFldInd=false;
500 while ((data = iter.nextElement())!= NULL)
502 if ((((CINDEX*)data)->tblPtr_==tblPtr)
503 && (((CINDEX*)indexPtr)->numFlds_ == ((CINDEX*)data)->numFlds_)
504 && (((CINDEX*)indexPtr)->indexType_==((CINDEX*)data)->indexType_)
505 && (data != indexPtr) )
507 fldList.resetIter();
508 while (NULL != (fName = fldList.nextFieldName()))
510 isFldInd=false;
511 fInd=(CINDEXFIELD*)((CINDEX*)data)->fstIndFld_ ;
512 while (fInd)
514 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
516 isFldInd=true;
517 break;
519 fInd=fInd->next;
521 if(!isFldInd) break;
523 if(isFldInd)
525 printError(ErrAlready, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE *)tblPtr)->tblName_, ((CINDEX *)data)->indName_);
526 return ErrAlready;
532 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
533 fldList.resetIter();
534 int i =0;
535 while (NULL != (fName = fldList.nextFieldName()))
537 DbRetVal rv = OK;
538 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
539 while(fInd)
541 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
543 printError(ErrAlready,"Composite Index Can't be created with same Name");
544 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
545 CINDEXFIELD *fldI;
546 while(fInd)
548 fldI=fInd;
549 fInd=fInd->next;
550 tChunk->free(systemDatabase_,fldI);
552 return ErrAlready;
554 fInd=fInd->next;
556 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
557 if (NULL == fieldptr)
559 printError(rv, "Could not allocate for USER catalog table");
560 return rv;
562 CINDEXFIELD *fldInfo = (CINDEXFIELD*)fieldptr;
563 fldInfo->tablePtr = tblPtr;
564 fldInfo->fieldPtr = (CFIELD*)fptr[i++];
565 fldInfo->indexPtr = indexPtr;
566 fldInfo->next=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
567 ((CINDEX *)indexPtr)->fstIndFld_=fldInfo;
568 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
570 return OK;
573 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
575 Chunk *fChunk;
576 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
577 ChunkIterator fIter = fChunk->getIterator();
578 void *data = NULL;
579 while ((data = fIter.nextElement())!= NULL)
581 if (((CINDEXFIELD*)data)->indexPtr == iptr)
583 //remove this element
584 if(((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_) ((CFIELD *)((CINDEXFIELD*)data)->fieldPtr)->isUnique_ = false;
585 fChunk->free(systemDatabase_, data);
586 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
589 return OK;
592 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
593 char *&name, DataType &type)
595 Chunk *ifChunk;
596 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
597 ChunkIterator ifIter = ifChunk->getIterator();
598 void *data = NULL;
599 while ((data = ifIter.nextElement())!= NULL)
601 if (((CINDEXFIELD*)data)->indexPtr == index)
603 //store the field name
604 name = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_;
605 type = ((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->type_;
606 return OK;
609 printError(ErrNotExists,"Index %x not exists in catalog table", index);
610 return ErrNotExists;
613 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
615 Chunk *ifChunk;
616 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
617 ChunkIterator ifIter = ifChunk->getIterator();
618 void *data = NULL;
619 int rowCount =0;
620 while ((data = ifIter.nextElement())!= NULL)
622 if (((CINDEXFIELD*)data)->indexPtr == index)
624 //add the information to the field list
625 CFIELD *fTuple = (CFIELD*)(((CINDEXFIELD*)data)->fieldPtr);
626 FieldDef fldDef;
627 strcpy(fldDef.fldName_, fTuple->fldName_);
628 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
629 fldDef.type_ = fTuple->type_;
630 fldDef.length_ = fTuple->length_;
631 fldDef.offset_ = fTuple->offset_;
632 fldDef.isDefault_ = fTuple->isDefault_;
633 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
634 DEFAULT_VALUE_BUF_LENGTH);
635 fldDef.isNull_ = fTuple->isNull_;
636 fldDef.isUnique_ = fTuple->isUnique_;
637 fldDef.isPrimary_ = fTuple->isPrimary_;
638 list.append(fldDef);
640 rowCount++;
642 if (!rowCount) {
643 printError(ErrNotExists,"Index %x not exists in catalog table", index);
644 return ErrNotExists;
646 return OK;
649 void CatalogTableINDEXFIELD::printAllIndex()
651 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
652 ChunkIterator ifIter = chunk->getIterator();
653 void *data = NULL;
654 char indexName[IDENTIFIER_LENGTH] = {'\0'};
655 while ((data = ifIter.nextElement())!= NULL)
657 if(strcmp(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_)!=0)
659 printf(" <Index Name> %s </Index Name> \n",((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
660 if(0==((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indexType_)
661 printf(" <Index Type> Hash Index </Index Type> \n");
662 else
663 printf(" <Index Type> Tree Index </Index Type> \n");
664 printf(" <Table Name> %s </Table Name> \n",((CTABLE*)(((CINDEXFIELD*)data)->tablePtr))->tblName_);
665 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
667 else
669 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
671 strcpy(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);
675 List CatalogTableUSER::getUserList()
677 List userList;
678 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
679 ChunkIterator iter = chk->getIterator();
680 void *tptr;
681 while (NULL != (tptr = iter.nextElement()))
683 Identifier *elem = new Identifier();
684 strcpy(elem->name, ((CUSER*)tptr)->userName_);
685 userList.append(elem);
687 return userList;
691 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
693 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
694 DbRetVal rv = OK;
695 ChunkIterator iter = tChunk->getIterator();
696 void *data = NULL;
697 while ((data = iter.nextElement())!= NULL)
699 if (0 == strcmp(((CUSER*)data)->userName_, name))
701 printError(ErrAlready, "User with name \'%s\' already exists ", name);
702 return ErrAlready;
706 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
707 if (NULL == usrInfo)
709 printError(rv,
710 "Could not allocate for USER catalog table");
711 return rv;
713 strcpy(usrInfo->userName_, name);
714 strcpy(usrInfo->password_, pass);
715 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
716 return OK;
720 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
721 bool &isAuthenticated, bool &isDba)
723 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
724 ChunkIterator iter = tChunk->getIterator();
725 void *data = NULL;
726 while (NULL != (data = iter.nextElement()))
728 if (strcmp(((CUSER*)data)->userName_, name) == 0)
730 //verify the password
731 //char * enpass = os::encrypt(pass,"A0");
732 char * enpass = (char*) pass;
733 if (0 == strcmp(enpass, ((CUSER*)data)->password_))
735 isAuthenticated = true;
736 if (0 == strcmp(((CUSER*)data)->userName_, DBAUSER))
737 isDba = true; else isDba = false;
738 return OK;
742 isAuthenticated = false;
743 return OK;
746 DbRetVal CatalogTableUSER::remove(const char *name)
748 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
749 ChunkIterator iter = tChunk->getIterator();
750 void *data = NULL;
751 while ((data = iter.nextElement())!= NULL)
753 if (strcmp(((CUSER*)data)->userName_, name) == 0)
755 //remove this element
756 tChunk->free(systemDatabase_, data);
757 return OK;
760 printError(ErrNotExists,"User %s not exists in catalog table", name);
761 return ErrNotExists;
764 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
766 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
767 ChunkIterator iter = tChunk->getIterator();
768 void *data = NULL;
769 while (NULL != (data = iter.nextElement()))
771 if (strcmp(((CUSER*)data)->userName_, name) == 0)
773 //change the password
774 strcpy(((CUSER*)data)->password_,pass);// os::encrypt(pass, "A0"));
775 return OK;
778 printError(ErrNotExists,"User %s not exists in catalog table", name);
779 return ErrNotExists;
782 DbRetVal CatalogTableFK::insert(char *name, void *tptr, void *tPkptr)
784 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
785 ChunkIterator iter = tChunk->getIterator();
786 void *data = NULL;
787 while ((data = iter.nextElement())!= NULL)
789 if (0 == strcmp(((CFK*)data)->fkName_, name))
791 printError(ErrAlready, "Index with name \'%s\' already exists "
792 "on the table \'%s\'.", name, ((CTABLE *)tptr)->tblName_);
793 return ErrAlready;
798 DbRetVal rv =OK;
799 void *fkptr = tChunk->allocate(systemDatabase_, &rv);
800 if (NULL == fkptr)
802 printError(rv, "Could not allocate for FK catalog table");
803 return rv;
805 CFK *fkTblInfo = (CFK*)fkptr;
806 strcpy(fkTblInfo->fkName_, name);
807 fkTblInfo->fkTblPtr_= tptr;
808 fkTblInfo->pkTblPtr_= tPkptr;
809 printDebug(DM_SystemDatabase,"One Row inserted into FK %x %s",fkptr, name);
810 return OK;
813 DbRetVal CatalogTableFKFIELD::insert(char *cFKName, char **fkFldPtrs, char **pkFldPtrs,int totalFld)
815 Chunk *tChunk = NULL;
816 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
817 ChunkIterator iter = tChunk->getIterator();
818 void *data = NULL;
819 while ((data = iter.nextElement())!= NULL)
821 if (0 == strcmp(((CFK*)data)->fkName_, cFKName))
823 break;
826 tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
827 int i =0;
828 DbRetVal rv = OK;
829 while (i < totalFld)
831 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
832 if (NULL == fieldptr)
834 printError(rv, "Could not allocate for USER catalog table");
835 return rv;
837 CFKFIELD *fldInfo = (CFKFIELD*)fieldptr;
838 fldInfo->fkPtr_ = data;
839 fldInfo->pfFldPtr_ = (CFIELD*)pkFldPtrs[i];
840 fldInfo->fkFldPtr_ = (CFIELD*)fkFldPtrs[i++];
841 //printDebug(DM_TEST,"TYPE %d\n",((CFIELD*)fldInfo->pfFldPtr_)->type_);
842 //printDebug(DM_TEST,"FK name %s\n",((CFIELD*)fldInfo->fkFldPtr_)->fldName_);
843 if(!(((CFIELD*)fldInfo->pfFldPtr_)->isUnique_) || !(((CFIELD*)fldInfo->pfFldPtr_)->isNull_))
845 printError(ErrSysInternal,"Parent Table field should have primary key field ");
846 tChunk->free(systemDatabase_,fieldptr);
847 return ErrSysInternal;
849 if(((CFIELD*)fldInfo->pfFldPtr_)->type_!=((CFIELD*)fldInfo->fkFldPtr_)->type_)
851 printError(ErrSysInternal,"Type Missmatch in both PK field and FK field ");
852 tChunk->free(systemDatabase_,fieldptr);
853 return ErrSysInternal;
855 printDebug(DM_SystemDatabase,"One Row inserted into FKFIELD %x", fldInfo);
857 return OK;
859 DbRetVal CatalogTableFK::remove(void *ctptr)
861 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
862 ChunkIterator iter = tChunk->getIterator();
863 void *data = NULL;
864 while ((data = iter.nextElement())!= NULL)
866 if (data == ctptr)
868 tChunk->free(systemDatabase_,data);
869 printDebug(DM_SystemDatabase,"One Row deleted from FKFIELD %x", data);
873 return OK;
876 DbRetVal CatalogTableFKFIELD::remove(void *cFKfld)
878 Chunk *fChunk = NULL;
879 fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
880 ChunkIterator iter = fChunk->getIterator();
881 void *data = NULL;
882 while ((data = iter.nextElement())!= NULL)
884 if (((CFKFIELD*)data)->fkPtr_== cFKfld )
886 fChunk->free(systemDatabase_, data);
887 printDebug(DM_SystemDatabase,"One Row deleted from CFKFIELD %x", data);
890 return OK;
892 void *CatalogTableFK::getFkCTable(void *ctptr)
894 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
895 ChunkIterator iter = tChunk->getIterator();
896 void *data = NULL;
897 while ((data = iter.nextElement())!= NULL)
899 if (((CFK*)data)->fkTblPtr_== ctptr)
901 return data;
905 return NULL;
907 int CatalogTableFK::getNumFkTable(void *ctptr)
909 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
910 ChunkIterator iter = tChunk->getIterator();
911 void *data = NULL;
912 int count=0;
913 while ((data = iter.nextElement())!= NULL)
915 if (((CFK*)data)->pkTblPtr_== ctptr)
917 count++;
920 return count;
923 bool CatalogTableFK::isFkTable(void *ctptr)
925 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
926 ChunkIterator iter = tChunk->getIterator();
927 void *data = NULL;
928 int count=0;
929 while ((data = iter.nextElement())!= NULL)
931 if (((CFK*)data)->fkTblPtr_== ctptr)
933 return true;
936 return false;
938 int CatalogTableFK::getNoOfFkTable(void *ctptr)
940 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
941 ChunkIterator iter = tChunk->getIterator();
942 void *data = NULL;
943 int count=0;
944 while ((data = iter.nextElement())!= NULL)
946 if (((CFK*)data)->pkTblPtr_== ctptr)
948 count++;
951 return count;
954 int CatalogTableFK::getNoOfPkTable(void *ctptr)
956 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
957 ChunkIterator iter = tChunk->getIterator();
958 void *data = NULL;
959 int count=0;
960 while ((data = iter.nextElement())!= NULL)
962 if (((CFK*)data)->fkTblPtr_== ctptr)
964 count++;
967 return count;
970 void CatalogTableFK::getPkTableName(void *ctptr,char **&array)
972 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
973 ChunkIterator iter = tChunk->getIterator();
974 void *data = NULL;
975 int i=0;
976 while ((data = iter.nextElement())!= NULL)
978 if (((CFK*)data)->fkTblPtr_== ctptr)
980 array[i++] = ((CTABLE*)((CFK*)data)->pkTblPtr_)->tblName_;
984 void CatalogTableFK::getFkTableName(void *ctptr,char **&array)
986 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
987 ChunkIterator iter = tChunk->getIterator();
988 void *data = NULL;
989 int i=0;
990 while ((data = iter.nextElement())!= NULL)
992 if (((CFK*)data)->pkTblPtr_== ctptr)
994 array[i++] = ((CTABLE*)((CFK*)data)->fkTblPtr_)->tblName_;
1000 DbRetVal CatalogTableFK::getPkFkFieldInfo(void *cpkptr, void *cfkptr, FieldNameList &pklist,FieldNameList &fklist)
1002 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyTableId);
1003 ChunkIterator iter = tChunk->getIterator();
1004 void *data = NULL;
1005 while ((data = iter.nextElement())!= NULL)
1007 if (((CFK*)data)->pkTblPtr_== cpkptr && ((CFK*)data)->fkTblPtr_ == cfkptr)
1009 break;
1012 if(data == NULL)
1014 printError(ErrNotExists,"Foreign Key field CFK not found");
1015 return ErrNotExists;
1017 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(ForeignKeyFieldTableId);
1018 iter = fChunk->getIterator();
1019 void *fdata=NULL;
1020 while ((fdata = iter.nextElement())!= NULL)
1022 if (((CFKFIELD*)fdata)->fkPtr_==data)
1024 //printDebug(DM_TEST,"PK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1025 //printDebug(DM_TEST,"FK Field name %s\n",((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1026 pklist.append(((CFIELD*)((CFKFIELD*)fdata)->pfFldPtr_)->fldName_);
1027 fklist.append(((CFIELD*)((CFKFIELD*)fdata)->fkFldPtr_)->fldName_);
1030 return OK;