code reorg
[csql.git] / src / storage / CatalogTables.cxx
blobaedab398faeda4e9ccd07b0dbf0518d6304bdf72
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::renameTable( const char *oldName,const char *newName)
50 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
51 ChunkIterator iter = tChunk->getIterator();
52 void *data = NULL;
53 bool isTableExits = false ;
54 CTABLE *oldTable = NULL;
55 while ((data = iter.nextElement())!= NULL)
57 if (0 == strcmp(((CTABLE*)data)->tblName_, oldName))
59 oldTable =(CTABLE*)data;
60 isTableExits = true;
62 if (0 == strcmp(((CTABLE*)data)->tblName_,newName))
64 printError(ErrNotExists,"A Table with name %s already exists", newName);
65 return ErrNotExists;
68 strcpy(oldTable->tblName_, newName);
69 if(!isTableExits){
70 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", oldName);
71 return ErrNotExists;
74 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
75 iter = tChunk->getIterator();
76 char tmpName[IDENTIFIER_LENGTH]="";
77 sprintf(tmpName, "%s_idx1_Primary", oldName);
78 while ((data = iter.nextElement())!= NULL)
80 if(strcmp(((CINDEX*)data)->indName_ ,tmpName)==0) {
81 sprintf(((CINDEX*)data)->indName_, "%s_idx1_Primary", newName);
82 break;
85 return OK;
88 DbRetVal CatalogTableTABLE::renameIndex( const char *oldName,const char *newName)
90 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
91 ChunkIterator iter = tChunk->getIterator();
92 void *data = NULL;
93 bool isIndexExits = false ;
94 CINDEX *oldIndex = NULL;
95 while ((data = iter.nextElement())!= NULL)
97 if (0 == strcmp(((CINDEX*)data)->indName_, oldName))
99 oldIndex=(CINDEX*)data;
100 isIndexExits = true;
102 if (0 == strcmp(((CINDEX*)data)->indName_,newName))
104 printError(ErrNotExists,"A Index with name %s already exists", newName);
105 return ErrNotExists;
108 if(!isIndexExits){
109 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", oldName);
110 return ErrNotExists;
112 strcpy(oldIndex->indName_, newName);
114 return OK;
117 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
119 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
120 ChunkIterator iter = tChunk->getIterator();
122 void *data = NULL;
123 while ((data = iter.nextElement())!= NULL)
125 if (0 == strcmp(((CTABLE*)data)->tblName_, name))
127 //remove this element and store the tblPtr
128 //there will be only one row for this table(Primary key)
129 tptr = (void*) data;
130 chunk = (Chunk*) ((CTABLE*)data)->chunkPtr_;
131 break;
134 if (NULL != tptr)
136 tChunk->free(systemDatabase_, tptr);
137 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
139 else
141 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
142 return ErrNotExists;
144 return OK;
147 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
148 void *&chunk, void *&tptr, void *&vcchunk)
150 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
151 ChunkIterator iter = chk->getIterator();;
152 while (NULL != (tptr = iter.nextElement()))
154 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
156 //there will be only one row for this table(Primary key)
157 chunk = (Chunk*) ((CTABLE*)tptr)->chunkPtr_;
158 vcchunk = (Chunk*) ((CTABLE*)tptr)->varcharChunkPtr_;
159 return OK;
162 //table not found in TABLE
163 return ErrNotFound;
167 DbRetVal CatalogTableTABLE::setChunkPtr(const char *name, void *firstPage, void *curPage)
169 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
170 ChunkIterator iter = chk->getIterator();;
171 void *tptr;
172 while (NULL != (tptr = iter.nextElement()))
174 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
176 //there will be only one row for this table(Primary key)
177 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setFirstPage(firstPage);
178 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setCurPage(curPage);
179 return OK;
182 //table not found in TABLE
183 return ErrNotFound;
186 List CatalogTableTABLE::getTableList()
188 List tableList;
189 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
190 ChunkIterator iter = chk->getIterator();
191 void *tptr;
192 while (NULL != (tptr = iter.nextElement()))
194 Identifier *elem = new Identifier();
195 strcpy(elem->name, ((CTABLE*)tptr)->tblName_);
196 tableList.append(elem);
198 return tableList;
201 DbRetVal CatalogTableFIELD::renameField(const char *tableName, const char *oldName, const char *newName)
203 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
204 ChunkIterator iter = fChunk->getIterator();
205 void *data = NULL;
206 bool isFieldExists=false;
207 while ((data = iter.nextElement())!= NULL)
209 if ((strcmp(((CFIELD*)data)->fldName_,newName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
211 printError(ErrAlready,
212 "New Field Name '%s' already exists in the table.", newName);
213 return ErrAlready;
216 iter = fChunk->getIterator();
217 while ((data = iter.nextElement())!= NULL)
219 if ((strcmp(((CFIELD*)data)->fldName_,oldName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
221 strcpy(((CFIELD*)data)->fldName_,newName);
222 isFieldExists = true;
223 break;
226 if(!isFieldExists){
227 printError(ErrNotExists, "Old Field Name '%s' does not exist in table",
228 oldName);
229 return ErrNotExists;
231 return OK;
234 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
236 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
237 DbRetVal rv = OK;
238 while (iter.hasElement())
240 void *fptr = fChunk->allocate(systemDatabase_, &rv);
241 if (NULL == fptr)
243 printError(rv,
244 "Could not allocate for FIELD catalog table");
245 return rv;
247 CFIELD *fldInfo = (CFIELD*)fptr;
248 FieldDef *fDef = iter.nextElement();
249 strcpy(fldInfo->fldName_, fDef->fldName_);
250 fldInfo->tblID_ = tblID;
251 fldInfo->tblPtr_ = tptr;
252 fldInfo->type_ = fDef->type_;
253 fldInfo->length_ = fDef->length_;
254 fldInfo->offset_ = fDef->offset_;
255 os::memcpy(fldInfo->defaultValueBuf_, fDef->defaultValueBuf_,
256 DEFAULT_VALUE_BUF_LENGTH);
257 fldInfo->isNull_ = fDef->isNull_;
258 fldInfo->isPrimary_ = fDef->isPrimary_;
259 fldInfo->isUnique_ = fDef->isUnique_;
260 fldInfo->isDefault_ = fDef->isDefault_;
261 fldInfo->isAutoIncrement_= fDef->isAutoIncrement_;
262 fldInfo->autoVal_ = 0;
263 fldInfo->width_ = 0; //TODO
264 fldInfo->scale_ = 0; //TODO
265 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef->fldName_);
268 return OK;
271 DbRetVal CatalogTableFIELD::remove(void *tptr)
273 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
274 ChunkIterator fIter = fChunk->getIterator();
275 void *data = NULL;
276 while ((data = fIter.nextElement())!= NULL)
278 if (((CFIELD*)data)->tblPtr_ == tptr)
280 //remove this element
281 fChunk->free(systemDatabase_, data);
282 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
285 return OK;
288 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
290 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
291 ChunkIterator fIter = fChunk->getIterator();;
292 void *data = NULL;
293 void *ptrToAutoVal;
294 while (NULL != (data = fIter.nextElement()))
296 if (((CFIELD*)data)->tblPtr_ == tptr)
298 //add the information to the field list
299 CFIELD *fTuple = (CFIELD*)data;
300 FieldDef fldDef;
301 strcpy(fldDef.fldName_, fTuple->fldName_);
302 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
303 fldDef.type_ = fTuple->type_;
304 fldDef.length_ = fTuple->length_;
305 fldDef.offset_ = fTuple->offset_;
306 fldDef.isDefault_ = fTuple->isDefault_;
307 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
308 DEFAULT_VALUE_BUF_LENGTH);
309 fldDef.isNull_ = fTuple->isNull_;
310 fldDef.isUnique_ = fTuple->isUnique_;
311 fldDef.isPrimary_ = fTuple->isPrimary_;
312 fldDef.isAutoIncrement_= fTuple->isAutoIncrement_;
313 if(fTuple->isAutoIncrement_){
314 ptrToAutoVal = &fTuple->autoVal_;
315 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
317 list.append(fldDef);
320 return ptrToAutoVal;
323 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
325 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
326 int i=0;
327 char *fName = NULL;
328 bool found = false;
329 fldList.resetIter();
330 void *data = NULL;
331 DbRetVal rv =OK;
332 while (NULL != (fName = fldList.nextFieldName()))
334 ChunkIterator fIter = fChunk->getIterator();
335 found = false;
336 while (NULL != (data = fIter.nextElement()))
338 if (((CFIELD*)data)->tblPtr_ == tptr)
340 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
342 found = true;
343 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
344 fptr[i++] = (char*) data;
345 break;
349 if (!found)
351 printError(ErrNotFound,
352 "No entries found in FIELD catalog table for the table specified");
353 return ErrNotFound;
356 return rv;
360 List CatalogTableUSER::getUserList()
362 List userList;
363 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
364 ChunkIterator iter = chk->getIterator();
365 void *tptr;
366 while (NULL != (tptr = iter.nextElement()))
368 Identifier *elem = new Identifier();
369 strcpy(elem->name, ((CUSER*)tptr)->userName_);
370 userList.append(elem);
372 return userList;
376 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
378 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
379 DbRetVal rv = OK;
380 ChunkIterator iter = tChunk->getIterator();
381 void *data = NULL;
382 while ((data = iter.nextElement())!= NULL)
384 if (0 == strcmp(((CUSER*)data)->userName_, name))
386 printError(ErrAlready, "User with name \'%s\' already exists ", name);
387 return ErrAlready;
391 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
392 if (NULL == usrInfo)
394 printError(rv,
395 "Could not allocate for USER catalog table");
396 return rv;
398 strcpy(usrInfo->userName_, name);
399 strcpy(usrInfo->password_, pass);
400 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
401 return OK;
405 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
406 bool &isAuthenticated, bool &isDba)
408 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
409 ChunkIterator iter = tChunk->getIterator();
410 void *data = NULL;
411 while (NULL != (data = iter.nextElement()))
413 if (strcmp(((CUSER*)data)->userName_, name) == 0)
415 //verify the password
416 //char * enpass = os::encrypt(pass,"A0");
417 char * enpass = (char*) pass;
418 if (0 == strcmp(enpass, ((CUSER*)data)->password_))
420 isAuthenticated = true;
421 if (0 == strcmp(((CUSER*)data)->userName_, DBAUSER))
422 isDba = true; else isDba = false;
423 return OK;
427 isAuthenticated = false;
428 return OK;
431 DbRetVal CatalogTableUSER::remove(const char *name)
433 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
434 ChunkIterator iter = tChunk->getIterator();
435 void *data = NULL;
436 while ((data = iter.nextElement())!= NULL)
438 if (strcmp(((CUSER*)data)->userName_, name) == 0)
440 //remove this element
441 tChunk->free(systemDatabase_, data);
442 return OK;
445 printError(ErrNotExists,"User %s not exists in catalog table", name);
446 return ErrNotExists;
449 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
451 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
452 ChunkIterator iter = tChunk->getIterator();
453 void *data = NULL;
454 while (NULL != (data = iter.nextElement()))
456 if (strcmp(((CUSER*)data)->userName_, name) == 0)
458 //change the password
459 strcpy(((CUSER*)data)->password_,pass);// os::encrypt(pass, "A0"));
460 return OK;
463 printError(ErrNotExists,"User %s not exists in catalog table", name);
464 return ErrNotExists;