code reorg
[csql.git] / src / server / CatalogTables.cxx
blob4ab0b286c1fb6fcdb6031cc05d8e650641181487
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 char ChunkName[MAX_CHUNKS][CHUNK_NAME_LEN]={"UserChunkTableId","LockTableHashBucketId","LockTableMutexId","LockTableId","TransHasTableId","UndoLogTableId","","","","","DatabaseTableId","UserTableId","TableTableId","FieldTableId","AccessTableId","IndexTableId","IndexFieldTableId",""};
24 DbRetVal CatalogTableTABLE::insert(const char *name, int id, size_t size,
25 int numFlds, void* chunk, void *&tptr)
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 TABLE *tableInfo = (TABLE*)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 printDebug(DM_SystemDatabase,"One Row inserted into TABLE %x %s",tptr, name);
44 return OK;
47 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
49 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
50 ChunkIterator iter = tChunk->getIterator();
52 void *data = NULL;
53 while ((data = iter.nextElement())!= NULL)
55 if (0 == strcmp(((TABLE*)data)->tblName_, name))
57 //remove this element and store the tblPtr
58 //there will be only one row for this table(Primary key)
59 tptr = (void*) data;
60 chunk = (Chunk*) ((TABLE*)data)->chunkPtr_;
61 break;
64 if (NULL != tptr)
66 tChunk->free(systemDatabase_, tptr);
67 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
69 else
71 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
72 return ErrNotExists;
74 return OK;
77 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
78 void *&chunk, void *&tptr)
80 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
81 ChunkIterator iter = chk->getIterator();;
82 while (NULL != (tptr = iter.nextElement()))
84 if (strcmp(((TABLE*)tptr)->tblName_, name) == 0)
86 //there will be only one row for this table(Primary key)
87 chunk = (Chunk*) ((TABLE*)tptr)->chunkPtr_;
88 return OK;
91 //table not found in TABLE
92 return ErrNotFound;
95 List CatalogTableTABLE::getTableList()
97 List tableList;
98 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
99 ChunkIterator iter = chk->getIterator();
100 void *tptr;
101 while (NULL != (tptr = iter.nextElement()))
103 Identifier *elem = new Identifier();
104 strcpy(elem->name, ((TABLE*)tptr)->tblName_);
105 tableList.append(elem);
107 return tableList;
110 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
112 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
113 DbRetVal rv = OK;
114 while (iter.hasElement())
116 void *fptr = fChunk->allocate(systemDatabase_, &rv);
117 if (NULL == fptr)
119 printError(rv,
120 "Could not allocate for FIELD catalog table");
121 return rv;
123 FIELD *fldInfo = (FIELD*)fptr;
124 FieldDef fDef = iter.nextElement();
125 strcpy(fldInfo->fldName_, fDef.fldName_);
126 fldInfo->tblID_ = tblID;
127 fldInfo->tblPtr_ = tptr;
128 fldInfo->type_ = fDef.type_;
129 fldInfo->length_ = fDef.length_;
130 fldInfo->offset_ = 0; //TODO
131 os::memcpy(fldInfo->defaultValueBuf_, fDef.defaultValueBuf_,
132 DEFAULT_VALUE_BUF_LENGTH);
133 fldInfo->isNull_ = fDef.isNull_;
134 fldInfo->isPrimary_ = fDef.isPrimary_;
135 fldInfo->isUnique_ = fDef.isUnique_;
136 fldInfo->isDefault_ = fDef.isDefault_;
137 fldInfo->width_ = 0; //TODO
138 fldInfo->scale_ = 0; //TODO
139 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef.fldName_);
142 return OK;
145 DbRetVal CatalogTableFIELD::remove(void *tptr)
147 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
148 ChunkIterator fIter = fChunk->getIterator();
149 void *data = NULL;
150 while ((data = fIter.nextElement())!= NULL)
152 if (((FIELD*)data)->tblPtr_ == tptr)
154 //remove this element
155 fChunk->free(systemDatabase_, data);
156 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
159 return OK;
162 void CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
164 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
165 ChunkIterator fIter = fChunk->getIterator();;
166 void *data = NULL;
167 while (NULL != (data = fIter.nextElement()))
169 if (((FIELD*)data)->tblPtr_ == tptr)
171 //add the information to the field list
172 FIELD *fTuple = (FIELD*)data;
173 FieldDef fldDef;
174 strcpy(fldDef.fldName_, fTuple->fldName_);
175 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
176 fldDef.type_ = fTuple->type_;
177 fldDef.length_ = fTuple->length_;
178 fldDef.isDefault_ = fTuple->isDefault_;
179 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
180 DEFAULT_VALUE_BUF_LENGTH);
181 fldDef.isNull_ = fTuple->isNull_;
182 fldDef.isUnique_ = fTuple->isUnique_;
183 fldDef.isPrimary_ = fTuple->isPrimary_;
184 list.append(fldDef);
187 return;
190 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
192 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
193 int i=0;
194 char *fName = NULL;
195 bool found = false;
196 fldList.resetIter();
197 void *data = NULL;
198 DbRetVal rv =OK;
199 while (NULL != (fName = fldList.nextFieldName()))
201 ChunkIterator fIter = fChunk->getIterator();
202 found = false;
203 while (NULL != (data = fIter.nextElement()))
205 if (((FIELD*)data)->tblPtr_ == tptr)
207 if(0 == strcmp((char*)((FIELD*)data)->fldName_, fName))
209 found = true;
210 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
211 fptr[i++] = (char*) data;
212 break;
216 if (!found)
218 printError(ErrNotFound,
219 "No entries found in FIELD catalog table for the table specified");
220 return ErrNotFound;
223 return rv;
226 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
227 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
229 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
230 ChunkIterator iter = tChunk->getIterator();
232 //Checking for index having same name, proceed further only
233 //if no such indexes are
234 void *data = NULL;
235 while ((data = iter.nextElement())!= NULL)
237 if (0 == strcmp(((INDEX*)data)->indName_, name))
239 printError(ErrAlready, "Index with name \'%s\' already exists "
240 "on the table \'%s\'.", name, ((TABLE *)tptr)->tblName_);
241 return ErrAlready;
246 DbRetVal rv =OK;
247 tupleptr = tChunk->allocate(systemDatabase_, &rv);
248 if (NULL == tupleptr)
250 printError(rv,
251 "Could not allocate for INDEX catalog table");
252 return rv;
254 INDEX *indexInfo = (INDEX*)tupleptr;
255 strcpy(indexInfo->indName_, name);
256 indexInfo->tblID_ = -1; //Not used currently
257 indexInfo->tblPtr_ = tptr;
258 indexInfo->numFlds_ = numFlds;
259 indexInfo->indexType_ = hashIndex;
260 indexInfo->chunkPtr_ = chunk;
261 indexInfo->hashNodeChunk_ = hChunk;
262 indexInfo->noOfBuckets_ = bucketSize;
263 indexInfo->isUnique_ = isUnique;
264 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
265 return OK;
268 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
270 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
271 ChunkIterator iter = fChunk->getIterator();
273 void *data = NULL;
274 while ((data = iter.nextElement())!= NULL)
276 if (0 == strcmp(((INDEX*)data)->indName_, name))
278 //remove this element and store the tuple ptr
279 //there will be only one row for this table(Primary key)
280 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
281 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
282 iptr = (void*) data;
283 break;
286 if (NULL != iptr)
288 fChunk->free(systemDatabase_, iptr);
289 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
291 else
293 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
294 return ErrNotExists;
296 return OK;
298 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
300 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
301 ChunkIterator iter = fChunk->getIterator();
303 void *data = NULL;
304 while ((data = iter.nextElement())!= NULL)
306 if (0 == strcmp(((INDEX*)data)->indName_, name))
308 //remove this element and store the tuple ptr
309 //there will be only one row for this table(Primary key)
310 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
311 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
312 iptr = (void*) data;
313 break;
316 if (NULL == iptr)
318 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
319 return ErrNotExists;
321 return OK;
324 int CatalogTableINDEX::getNumIndexes(void *tptr)
326 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
327 ChunkIterator iter = fChunk->getIterator();
328 void *iptr = NULL;
329 int numIndex =0;
330 while (NULL != (iptr = iter.nextElement()))
332 if (((INDEX*)iptr)->tblPtr_ == tptr) numIndex++;
334 return numIndex;
337 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
339 if (position == 0) return NULL;
340 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
341 ChunkIterator iter = fChunk->getIterator();
342 void *iptr = NULL;
343 int numIndex =0;
344 int curPos =0;
345 while (NULL != (iptr = iter.nextElement()))
347 if (((INDEX*)iptr)->tblPtr_ == tptr) curPos++;
348 if ( curPos == position ) return ((INDEX*)iptr)->indName_;
350 return NULL;
354 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
356 void *iptr = NULL;
357 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
358 ChunkIterator iter = fChunk->getIterator();
359 int i=0;
360 while (NULL != (iptr = iter.nextElement()))
362 if (((INDEX*)iptr)->tblPtr_ == tptr)
364 array[i++] = (char*) iptr;
367 return;
370 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
372 INDEX *index = (INDEX*)iptr;
373 return ((Chunk*)index->chunkPtr_)->getIterator();
377 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
379 INDEX *index = (INDEX*)iptr;
380 return index->noOfBuckets_;
383 int CatalogTableINDEX::getUnique(void *iptr)
385 INDEX *index = (INDEX*)iptr;
386 return index->isUnique_;
388 char* CatalogTableINDEX::getName(void *iptr)
390 INDEX *index = (INDEX*)iptr;
391 return index->indName_;
394 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
395 void *tblPtr, char **&fptr)
398 Chunk *fChunk;
399 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
400 fldList.resetIter();
401 int i =0;
402 char *fName =NULL;
403 void *data = NULL;
404 ChunkIterator ifIter = fChunk->getIterator();
405 while (NULL != (fName = fldList.nextFieldName()))
407 ifIter = fChunk->getIterator();
408 while ((data = ifIter.nextElement()) != NULL) {
409 if (0 == strcmp(((FIELD *)((INDEXFIELD *) data)->fieldPtr)->fldName_, fName) && ((INDEXFIELD *)data)->tablePtr == tblPtr) {
410 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_);
411 return ErrAlready;
414 DbRetVal rv = OK;
415 void *fieldptr = fChunk->allocate(systemDatabase_, &rv);
416 if (NULL == fieldptr)
418 printError(rv,
419 "Could not allocate for USER catalog table");
420 return rv;
422 INDEXFIELD *fldInfo = (INDEXFIELD*)fieldptr;
423 fldInfo->tablePtr = tblPtr;
424 fldInfo->fieldPtr = (FIELD*)fptr[i++];
425 fldInfo->indexPtr = indexPtr;
426 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
428 return OK;
431 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
433 Chunk *fChunk;
434 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
435 ChunkIterator fIter = fChunk->getIterator();
436 void *data = NULL;
437 while ((data = fIter.nextElement())!= NULL)
439 if (((INDEXFIELD*)data)->indexPtr == iptr)
441 //remove this element
442 fChunk->free(systemDatabase_, data);
443 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
446 return OK;
449 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
450 char *&name, DataType &type)
452 Chunk *ifChunk;
453 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
454 ChunkIterator ifIter = ifChunk->getIterator();
455 void *data = NULL;
456 while ((data = ifIter.nextElement())!= NULL)
458 if (((INDEXFIELD*)data)->indexPtr == index)
460 //store the field name
461 name = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->fldName_;
462 type = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->type_;
463 return OK;
466 printError(ErrNotExists,"Index %x not exists in catalog table", index);
467 return ErrNotExists;
470 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
472 Chunk *ifChunk;
473 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
474 ChunkIterator ifIter = ifChunk->getIterator();
475 void *data = NULL;
476 int rowCount =0;
477 while ((data = ifIter.nextElement())!= NULL)
479 if (((INDEXFIELD*)data)->indexPtr == index)
481 //add the information to the field list
482 FIELD *fTuple = (FIELD*)(((INDEXFIELD*)data)->fieldPtr);
483 FieldDef fldDef;
484 strcpy(fldDef.fldName_, fTuple->fldName_);
485 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
486 fldDef.type_ = fTuple->type_;
487 fldDef.length_ = fTuple->length_;
488 fldDef.isDefault_ = fTuple->isDefault_;
489 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
490 DEFAULT_VALUE_BUF_LENGTH);
491 fldDef.isNull_ = fTuple->isNull_;
492 fldDef.isUnique_ = fTuple->isUnique_;
493 fldDef.isPrimary_ = fTuple->isPrimary_;
494 list.append(fldDef);
496 rowCount++;
498 if (!rowCount) {
499 printError(ErrNotExists,"Index %x not exists in catalog table", index);
500 return ErrNotExists;
502 return OK;
505 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
507 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
508 DbRetVal rv = OK;
509 USER *usrInfo = (USER*)tChunk->allocate(systemDatabase_, &rv);
510 if (NULL == usrInfo)
512 printError(rv,
513 "Could not allocate for USER catalog table");
514 return rv;
516 strcpy(usrInfo->userName_, name);
517 strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
518 return OK;
522 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
523 bool &isAuthenticated, bool &isDba)
525 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
526 ChunkIterator iter = tChunk->getIterator();
527 void *data = NULL;
528 while (NULL != (data = iter.nextElement()))
530 if (strcmp(((USER*)data)->userName_, name) == 0)
532 //verify the password
533 char * enpass = os::encrypt(pass,"A0");
534 if (0 == strcmp(enpass, ((USER*)data)->password_))
536 isAuthenticated = true;
537 if (0 == strcmp(((USER*)data)->userName_, DBAUSER))
538 isDba = true; else isDba = false;
539 return OK;
543 isAuthenticated = false;
544 return OK;
547 DbRetVal CatalogTableUSER::remove(const char *name)
549 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
550 ChunkIterator iter = tChunk->getIterator();
551 void *data = NULL;
552 while ((data = iter.nextElement())!= NULL)
554 if (strcmp(((USER*)data)->userName_, name) == 0)
556 //remove this element
557 tChunk->free(systemDatabase_, data);
558 return OK;
561 printError(ErrNotExists,"User %s not exists in catalog table", name);
562 return ErrNotExists;
565 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
567 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
568 ChunkIterator iter = tChunk->getIterator();
569 void *data = NULL;
570 while (NULL != (data = iter.nextElement()))
572 if (strcmp(((USER*)data)->userName_, name) == 0)
574 //change the password
575 strcpy(((USER*)data)->password_, os::encrypt(pass, "A0"));
576 return OK;
579 printError(ErrNotExists,"User %s not exists in catalog table", name);
580 return ErrNotExists;