Renamed server directory to storage directory in src
[csql.git] / src / storage / CatalogTables.cxx
blob032496a009cb5ac89805f46482114cc44949ca01
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>
22 DbRetVal CatalogTableTABLE::insert(const char *name, int id, size_t size,
23 int numFlds, void* chunk, void *&tptr)
25 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
26 DbRetVal rv = OK;
27 tptr = tChunk->allocate(systemDatabase_, &rv);
28 if (NULL == tptr)
30 printError(rv,
31 "Could not allocate memory for for TABLE catalog table");
32 return rv;
34 TABLE *tableInfo = (TABLE*)tptr;
35 strcpy(tableInfo->tblName_, name);
36 tableInfo->tblID_ = id;
37 tableInfo->length_ = size;
38 tableInfo->numFlds_ = numFlds;
39 tableInfo->numIndexes_ = 0;
40 tableInfo->chunkPtr_ = chunk;
41 printDebug(DM_SystemDatabase,"One Row inserted into TABLE %x %s",tptr, name);
42 return OK;
45 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
47 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
48 ChunkIterator iter = tChunk->getIterator();
50 void *data = NULL;
51 while ((data = iter.nextElement())!= NULL)
53 if (0 == strcmp(((TABLE*)data)->tblName_, name))
55 //remove this element and store the tblPtr
56 //there will be only one row for this table(Primary key)
57 tptr = (void*) data;
58 chunk = (Chunk*) ((TABLE*)data)->chunkPtr_;
59 break;
62 if (NULL != tptr)
64 tChunk->free(systemDatabase_, tptr);
65 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
67 else
69 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
70 return ErrNotExists;
72 return OK;
75 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
76 void *&chunk, void *&tptr)
78 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
79 ChunkIterator iter = chk->getIterator();;
80 while (NULL != (tptr = iter.nextElement()))
82 if (strcmp(((TABLE*)tptr)->tblName_, name) == 0)
84 //there will be only one row for this table(Primary key)
85 chunk = (Chunk*) ((TABLE*)tptr)->chunkPtr_;
86 return OK;
89 //table not found in TABLE
90 return ErrNotFound;
93 List CatalogTableTABLE::getTableList()
95 List tableList;
96 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
97 ChunkIterator iter = chk->getIterator();
98 void *tptr;
99 while (NULL != (tptr = iter.nextElement()))
101 Identifier *elem = new Identifier();
102 strcpy(elem->name, ((TABLE*)tptr)->tblName_);
103 tableList.append(elem);
105 return tableList;
108 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
110 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
111 DbRetVal rv = OK;
112 while (iter.hasElement())
114 void *fptr = fChunk->allocate(systemDatabase_, &rv);
115 if (NULL == fptr)
117 printError(rv,
118 "Could not allocate for FIELD catalog table");
119 return rv;
121 FIELD *fldInfo = (FIELD*)fptr;
122 FieldDef fDef = iter.nextElement();
123 strcpy(fldInfo->fldName_, fDef.fldName_);
124 fldInfo->tblID_ = tblID;
125 fldInfo->tblPtr_ = tptr;
126 fldInfo->type_ = fDef.type_;
127 fldInfo->length_ = fDef.length_;
128 fldInfo->offset_ = 0; //TODO
129 os::memcpy(fldInfo->defaultValueBuf_, fDef.defaultValueBuf_,
130 DEFAULT_VALUE_BUF_LENGTH);
131 fldInfo->isNull_ = fDef.isNull_;
132 fldInfo->isPrimary_ = fDef.isPrimary_;
133 fldInfo->isUnique_ = fDef.isUnique_;
134 fldInfo->isDefault_ = fDef.isDefault_;
135 fldInfo->width_ = 0; //TODO
136 fldInfo->scale_ = 0; //TODO
137 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef.fldName_);
140 return OK;
143 DbRetVal CatalogTableFIELD::remove(void *tptr)
145 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
146 ChunkIterator fIter = fChunk->getIterator();
147 void *data = NULL;
148 while ((data = fIter.nextElement())!= NULL)
150 if (((FIELD*)data)->tblPtr_ == tptr)
152 //remove this element
153 fChunk->free(systemDatabase_, data);
154 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
157 return OK;
160 void CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
162 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
163 ChunkIterator fIter = fChunk->getIterator();;
164 void *data = NULL;
165 while (NULL != (data = fIter.nextElement()))
167 if (((FIELD*)data)->tblPtr_ == tptr)
169 //add the information to the field list
170 FIELD *fTuple = (FIELD*)data;
171 FieldDef fldDef;
172 strcpy(fldDef.fldName_, fTuple->fldName_);
173 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
174 fldDef.type_ = fTuple->type_;
175 fldDef.length_ = fTuple->length_;
176 fldDef.isDefault_ = fTuple->isDefault_;
177 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
178 DEFAULT_VALUE_BUF_LENGTH);
179 fldDef.isNull_ = fTuple->isNull_;
180 fldDef.isUnique_ = fTuple->isUnique_;
181 fldDef.isPrimary_ = fTuple->isPrimary_;
182 list.append(fldDef);
185 return;
188 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
190 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
191 int i=0;
192 char *fName = NULL;
193 bool found = false;
194 fldList.resetIter();
195 void *data = NULL;
196 DbRetVal rv =OK;
197 while (NULL != (fName = fldList.nextFieldName()))
199 ChunkIterator fIter = fChunk->getIterator();
200 found = false;
201 while (NULL != (data = fIter.nextElement()))
203 if (((FIELD*)data)->tblPtr_ == tptr)
205 if(0 == strcmp((char*)((FIELD*)data)->fldName_, fName))
207 found = true;
208 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
209 fptr[i++] = (char*) data;
210 break;
214 if (!found)
216 printError(ErrNotFound,
217 "No entries found in FIELD catalog table for the table specified");
218 return ErrNotFound;
221 return rv;
224 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
225 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
227 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
228 ChunkIterator iter = tChunk->getIterator();
230 //Checking for index having same name, proceed further only
231 //if no such indexes are
232 void *data = NULL;
233 while ((data = iter.nextElement())!= NULL)
235 if (0 == strcmp(((INDEX*)data)->indName_, name))
237 printError(ErrAlready, "Index with name \'%s\' already exists "
238 "on the table \'%s\'.", name, ((TABLE *)tptr)->tblName_);
239 return ErrAlready;
244 DbRetVal rv =OK;
245 tupleptr = tChunk->allocate(systemDatabase_, &rv);
246 if (NULL == tupleptr)
248 printError(rv,
249 "Could not allocate for INDEX catalog table");
250 return rv;
252 INDEX *indexInfo = (INDEX*)tupleptr;
253 strcpy(indexInfo->indName_, name);
254 indexInfo->tblID_ = -1; //Not used currently
255 indexInfo->tblPtr_ = tptr;
256 indexInfo->numFlds_ = numFlds;
257 indexInfo->indexType_ = hashIndex;
258 indexInfo->chunkPtr_ = chunk;
259 indexInfo->hashNodeChunk_ = hChunk;
260 indexInfo->noOfBuckets_ = bucketSize;
261 indexInfo->isUnique_ = isUnique;
262 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
263 return OK;
266 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
268 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
269 ChunkIterator iter = fChunk->getIterator();
271 void *data = NULL;
272 while ((data = iter.nextElement())!= NULL)
274 if (0 == strcmp(((INDEX*)data)->indName_, name))
276 //remove this element and store the tuple ptr
277 //there will be only one row for this table(Primary key)
278 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
279 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
280 iptr = (void*) data;
281 break;
284 if (NULL != iptr)
286 fChunk->free(systemDatabase_, iptr);
287 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
289 else
291 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
292 return ErrNotExists;
294 return OK;
296 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
298 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
299 ChunkIterator iter = fChunk->getIterator();
301 void *data = NULL;
302 while ((data = iter.nextElement())!= NULL)
304 if (0 == strcmp(((INDEX*)data)->indName_, name))
306 //remove this element and store the tuple ptr
307 //there will be only one row for this table(Primary key)
308 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
309 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
310 iptr = (void*) data;
311 break;
314 if (NULL == iptr)
316 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
317 return ErrNotExists;
319 return OK;
322 int CatalogTableINDEX::getNumIndexes(void *tptr)
324 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
325 ChunkIterator iter = fChunk->getIterator();
326 void *iptr = NULL;
327 int numIndex =0;
328 while (NULL != (iptr = iter.nextElement()))
330 if (((INDEX*)iptr)->tblPtr_ == tptr) numIndex++;
332 return numIndex;
335 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
337 if (position == 0) return NULL;
338 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
339 ChunkIterator iter = fChunk->getIterator();
340 void *iptr = NULL;
341 int numIndex =0;
342 int curPos =0;
343 while (NULL != (iptr = iter.nextElement()))
345 if (((INDEX*)iptr)->tblPtr_ == tptr) curPos++;
346 if ( curPos == position ) return ((INDEX*)iptr)->indName_;
348 return NULL;
352 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
354 void *iptr = NULL;
355 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
356 ChunkIterator iter = fChunk->getIterator();
357 int i=0;
358 while (NULL != (iptr = iter.nextElement()))
360 if (((INDEX*)iptr)->tblPtr_ == tptr)
362 array[i++] = (char*) iptr;
365 return;
368 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
370 INDEX *index = (INDEX*)iptr;
371 return ((Chunk*)index->chunkPtr_)->getIterator();
375 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
377 INDEX *index = (INDEX*)iptr;
378 return index->noOfBuckets_;
381 int CatalogTableINDEX::getUnique(void *iptr)
383 INDEX *index = (INDEX*)iptr;
384 return index->isUnique_;
386 char* CatalogTableINDEX::getName(void *iptr)
388 INDEX *index = (INDEX*)iptr;
389 return index->indName_;
392 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
393 void *tblPtr, char **&fptr)
396 Chunk *fChunk;
397 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
398 fldList.resetIter();
399 int i =0;
400 char *fName =NULL;
401 void *data = NULL;
402 ChunkIterator ifIter = fChunk->getIterator();
403 while (NULL != (fName = fldList.nextFieldName()))
405 ifIter = fChunk->getIterator();
406 while ((data = ifIter.nextElement()) != NULL) {
407 if (0 == strcmp(((FIELD *)((INDEXFIELD *) data)->fieldPtr)->fldName_, fName) && ((INDEXFIELD *)data)->tablePtr == tblPtr) {
408 printError(ErrAlready, "Index on field \'%s\' already exists on table \'%s\' by name \'%s\'", ((FIELD *)((INDEXFIELD *)data)->fieldPtr)->fldName_, ((TABLE *)((INDEXFIELD *)data)->tablePtr)->tblName_, ((INDEX *)((INDEXFIELD *)data)->indexPtr)->indName_);
409 return ErrAlready;
412 DbRetVal rv = OK;
413 void *fieldptr = fChunk->allocate(systemDatabase_, &rv);
414 if (NULL == fieldptr)
416 printError(rv,
417 "Could not allocate for USER catalog table");
418 return rv;
420 INDEXFIELD *fldInfo = (INDEXFIELD*)fieldptr;
421 fldInfo->tablePtr = tblPtr;
422 fldInfo->fieldPtr = (FIELD*)fptr[i++];
423 fldInfo->indexPtr = indexPtr;
424 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
426 return OK;
429 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
431 Chunk *fChunk;
432 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
433 ChunkIterator fIter = fChunk->getIterator();
434 void *data = NULL;
435 while ((data = fIter.nextElement())!= NULL)
437 if (((INDEXFIELD*)data)->indexPtr == iptr)
439 //remove this element
440 fChunk->free(systemDatabase_, data);
441 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
444 return OK;
447 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
448 char *&name, DataType &type)
450 Chunk *ifChunk;
451 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
452 ChunkIterator ifIter = ifChunk->getIterator();
453 void *data = NULL;
454 while ((data = ifIter.nextElement())!= NULL)
456 if (((INDEXFIELD*)data)->indexPtr == index)
458 //store the field name
459 name = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->fldName_;
460 type = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->type_;
461 return OK;
464 printError(ErrNotExists,"Index %x not exists in catalog table", index);
465 return ErrNotExists;
468 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
470 Chunk *ifChunk;
471 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
472 ChunkIterator ifIter = ifChunk->getIterator();
473 void *data = NULL;
474 int rowCount =0;
475 while ((data = ifIter.nextElement())!= NULL)
477 if (((INDEXFIELD*)data)->indexPtr == index)
479 //add the information to the field list
480 FIELD *fTuple = (FIELD*)(((INDEXFIELD*)data)->fieldPtr);
481 FieldDef fldDef;
482 strcpy(fldDef.fldName_, fTuple->fldName_);
483 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
484 fldDef.type_ = fTuple->type_;
485 fldDef.length_ = fTuple->length_;
486 fldDef.isDefault_ = fTuple->isDefault_;
487 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
488 DEFAULT_VALUE_BUF_LENGTH);
489 fldDef.isNull_ = fTuple->isNull_;
490 fldDef.isUnique_ = fTuple->isUnique_;
491 fldDef.isPrimary_ = fTuple->isPrimary_;
492 list.append(fldDef);
494 rowCount++;
496 if (!rowCount) {
497 printError(ErrNotExists,"Index %x not exists in catalog table", index);
498 return ErrNotExists;
500 return OK;
503 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
505 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
506 DbRetVal rv = OK;
507 USER *usrInfo = (USER*)tChunk->allocate(systemDatabase_, &rv);
508 if (NULL == usrInfo)
510 printError(rv,
511 "Could not allocate for USER catalog table");
512 return rv;
514 strcpy(usrInfo->userName_, name);
515 strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
516 return OK;
520 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
521 bool &isAuthenticated, bool &isDba)
523 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
524 ChunkIterator iter = tChunk->getIterator();
525 void *data = NULL;
526 while (NULL != (data = iter.nextElement()))
528 if (strcmp(((USER*)data)->userName_, name) == 0)
530 //verify the password
531 char * enpass = os::encrypt(pass,"A0");
532 if (0 == strcmp(enpass, ((USER*)data)->password_))
534 isAuthenticated = true;
535 if (0 == strcmp(((USER*)data)->userName_, DBAUSER))
536 isDba = true; else isDba = false;
537 return OK;
541 isAuthenticated = false;
542 return OK;
545 DbRetVal CatalogTableUSER::remove(const char *name)
547 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
548 ChunkIterator iter = tChunk->getIterator();
549 void *data = NULL;
550 while ((data = iter.nextElement())!= NULL)
552 if (strcmp(((USER*)data)->userName_, name) == 0)
554 //remove this element
555 tChunk->free(systemDatabase_, data);
556 return OK;
559 printError(ErrNotExists,"User %s not exists in catalog table", name);
560 return ErrNotExists;
563 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
565 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
566 ChunkIterator iter = tChunk->getIterator();
567 void *data = NULL;
568 while (NULL != (data = iter.nextElement()))
570 if (strcmp(((USER*)data)->userName_, name) == 0)
572 //change the password
573 strcpy(((USER*)data)->password_, os::encrypt(pass, "A0"));
574 return OK;
577 printError(ErrNotExists,"User %s not exists in catalog table", name);
578 return ErrNotExists;