reorg files
[csql.git] / src / storage / CatalogTables.cxx
blob76452c944cf36d470d35739c92b0f0afe0d5d867
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]={
23 "UserChunkTableId",
24 "LockTableHashBucketId",
25 "LockTableMutexId",
26 "LockTableId",
27 "TransHasTableId",
28 "UndoLogTableId",
29 "","","","",
30 "DatabaseTableId",
31 "UserTableId",
32 "TableTableId",
33 "FieldTableId",
34 "AccessTableId",
35 "IndexTableId",
36 "IndexFieldTableId",
37 "ForeignKeyTableId",
38 "ForeignKeyFieldTableId"
42 DbRetVal CatalogTableTABLE::insert(const char *name, int id, size_t size,
43 int numFlds, void* chunk, void *&tptr, void *vcchunk)
45 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
46 DbRetVal rv = OK;
47 tptr = tChunk->allocate(systemDatabase_, &rv);
48 if (NULL == tptr)
50 printError(rv,
51 "Could not allocate memory for for TABLE catalog table");
52 return rv;
54 CTABLE *tableInfo = (CTABLE*)tptr;
55 strcpy(tableInfo->tblName_, name);
56 tableInfo->tblID_ = id;
57 tableInfo->length_ = size;
58 tableInfo->numFlds_ = numFlds;
59 tableInfo->numIndexes_ = 0;
60 tableInfo->chunkPtr_ = chunk;
61 tableInfo->varcharChunkPtr_ = vcchunk;
62 printDebug(DM_SystemDatabase,"One Row inserted into TABLE %x %s",tptr, name);
63 return OK;
66 DbRetVal CatalogTableTABLE::renameTable( const char *oldName,const char *newName)
68 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
69 ChunkIterator iter = tChunk->getIterator();
70 void *data = NULL;
71 bool isTableExits = false ;
72 CTABLE *oldTable = NULL;
73 while ((data = iter.nextElement())!= NULL)
75 if (0 == strcmp(((CTABLE*)data)->tblName_, oldName))
77 oldTable =(CTABLE*)data;
78 isTableExits = true;
80 if (0 == strcmp(((CTABLE*)data)->tblName_,newName))
82 printError(ErrNotExists,"A Table with name %s already exists", newName);
83 return ErrNotExists;
86 strcpy(oldTable->tblName_, newName);
87 if(!isTableExits){
88 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", oldName);
89 return ErrNotExists;
92 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
93 iter = tChunk->getIterator();
94 char tmpName[IDENTIFIER_LENGTH]="";
95 sprintf(tmpName, "%s_idx1_Primary", oldName);
96 while ((data = iter.nextElement())!= NULL)
98 if(strcmp(((CINDEX*)data)->indName_ ,tmpName)==0) {
99 sprintf(((CINDEX*)data)->indName_, "%s_idx1_Primary", newName);
100 break;
103 return OK;
106 DbRetVal CatalogTableTABLE::renameIndex( const char *oldName,const char *newName)
108 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
109 ChunkIterator iter = tChunk->getIterator();
110 void *data = NULL;
111 bool isIndexExits = false ;
112 CINDEX *oldIndex = NULL;
113 while ((data = iter.nextElement())!= NULL)
115 if (0 == strcmp(((CINDEX*)data)->indName_, oldName))
117 oldIndex=(CINDEX*)data;
118 isIndexExits = true;
120 if (0 == strcmp(((CINDEX*)data)->indName_,newName))
122 printError(ErrNotExists,"A Index with name %s already exists", newName);
123 return ErrNotExists;
126 if(!isIndexExits){
127 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", oldName);
128 return ErrNotExists;
130 strcpy(oldIndex->indName_, newName);
132 return OK;
135 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
137 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
138 ChunkIterator iter = tChunk->getIterator();
140 void *data = NULL;
141 while ((data = iter.nextElement())!= NULL)
143 if (0 == strcmp(((CTABLE*)data)->tblName_, name))
145 //remove this element and store the tblPtr
146 //there will be only one row for this table(Primary key)
147 tptr = (void*) data;
148 chunk = (Chunk*) ((CTABLE*)data)->chunkPtr_;
149 break;
152 if (NULL != tptr)
154 tChunk->free(systemDatabase_, tptr);
155 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
157 else
159 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
160 return ErrNotExists;
162 return OK;
165 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
166 void *&chunk, void *&tptr, void *&vcchunk)
168 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
169 ChunkIterator iter = chk->getIterator();;
170 while (NULL != (tptr = iter.nextElement()))
172 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
174 //there will be only one row for this table(Primary key)
175 chunk = (Chunk*) ((CTABLE*)tptr)->chunkPtr_;
176 vcchunk = (Chunk*) ((CTABLE*)tptr)->varcharChunkPtr_;
177 return OK;
180 //table not found in TABLE
181 return ErrNotFound;
185 DbRetVal CatalogTableTABLE::setChunkPtr(const char *name, void *firstPage, void *curPage)
187 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
188 ChunkIterator iter = chk->getIterator();;
189 void *tptr;
190 while (NULL != (tptr = iter.nextElement()))
192 if (strcmp(((CTABLE*)tptr)->tblName_, name) == 0)
194 //there will be only one row for this table(Primary key)
195 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setFirstPage(firstPage);
196 ((Chunk*)((CTABLE*)tptr)->chunkPtr_)->setCurPage(curPage);
197 return OK;
200 //table not found in TABLE
201 return ErrNotFound;
204 List CatalogTableTABLE::getTableList()
206 List tableList;
207 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
208 ChunkIterator iter = chk->getIterator();
209 void *tptr;
210 while (NULL != (tptr = iter.nextElement()))
212 Identifier *elem = new Identifier();
213 strcpy(elem->name, ((CTABLE*)tptr)->tblName_);
214 tableList.append(elem);
216 return tableList;
219 DbRetVal CatalogTableFIELD::renameField(const char *tableName, const char *oldName, const char *newName)
221 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
222 ChunkIterator iter = fChunk->getIterator();
223 void *data = NULL;
224 bool isFieldExists=false;
225 while ((data = iter.nextElement())!= NULL)
227 if ((strcmp(((CFIELD*)data)->fldName_,newName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
229 printError(ErrAlready,
230 "New Field Name '%s' already exists in the table.", newName);
231 return ErrAlready;
234 iter = fChunk->getIterator();
235 while ((data = iter.nextElement())!= NULL)
237 if ((strcmp(((CFIELD*)data)->fldName_,oldName)== 0) && (strcmp(((CTABLE *)((CFIELD*)data)->tblPtr_)->tblName_,tableName) == 0) )
239 strcpy(((CFIELD*)data)->fldName_,newName);
240 isFieldExists = true;
241 break;
244 if(!isFieldExists){
245 printError(ErrNotExists, "Old Field Name '%s' does not exist in table",
246 oldName);
247 return ErrNotExists;
249 return OK;
252 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
254 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
255 DbRetVal rv = OK;
256 while (iter.hasElement())
258 void *fptr = fChunk->allocate(systemDatabase_, &rv);
259 if (NULL == fptr)
261 printError(rv,
262 "Could not allocate for FIELD catalog table");
263 return rv;
265 CFIELD *fldInfo = (CFIELD*)fptr;
266 FieldDef *fDef = iter.nextElement();
267 strcpy(fldInfo->fldName_, fDef->fldName_);
268 fldInfo->tblID_ = tblID;
269 fldInfo->tblPtr_ = tptr;
270 fldInfo->type_ = fDef->type_;
271 fldInfo->length_ = fDef->length_;
272 fldInfo->offset_ = fDef->offset_;
273 os::memcpy(fldInfo->defaultValueBuf_, fDef->defaultValueBuf_,
274 DEFAULT_VALUE_BUF_LENGTH);
275 fldInfo->isNull_ = fDef->isNull_;
276 fldInfo->isPrimary_ = fDef->isPrimary_;
277 fldInfo->isUnique_ = fDef->isUnique_;
278 fldInfo->isDefault_ = fDef->isDefault_;
279 fldInfo->isAutoIncrement_= fDef->isAutoIncrement_;
280 fldInfo->autoVal_ = 0;
281 fldInfo->width_ = 0; //TODO
282 fldInfo->scale_ = 0; //TODO
283 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef->fldName_);
286 return OK;
289 DbRetVal CatalogTableFIELD::remove(void *tptr)
291 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
292 ChunkIterator fIter = fChunk->getIterator();
293 void *data = NULL;
294 while ((data = fIter.nextElement())!= NULL)
296 if (((CFIELD*)data)->tblPtr_ == tptr)
298 //remove this element
299 fChunk->free(systemDatabase_, data);
300 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
303 return OK;
306 void *CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
308 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
309 ChunkIterator fIter = fChunk->getIterator();;
310 void *data = NULL;
311 void *ptrToAutoVal;
312 while (NULL != (data = fIter.nextElement()))
314 if (((CFIELD*)data)->tblPtr_ == tptr)
316 //add the information to the field list
317 CFIELD *fTuple = (CFIELD*)data;
318 FieldDef fldDef;
319 strcpy(fldDef.fldName_, fTuple->fldName_);
320 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
321 fldDef.type_ = fTuple->type_;
322 fldDef.length_ = fTuple->length_;
323 fldDef.offset_ = fTuple->offset_;
324 fldDef.isDefault_ = fTuple->isDefault_;
325 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
326 DEFAULT_VALUE_BUF_LENGTH);
327 fldDef.isNull_ = fTuple->isNull_;
328 fldDef.isUnique_ = fTuple->isUnique_;
329 fldDef.isPrimary_ = fTuple->isPrimary_;
330 fldDef.isAutoIncrement_= fTuple->isAutoIncrement_;
331 if(fTuple->isAutoIncrement_){
332 ptrToAutoVal = &fTuple->autoVal_;
333 //os::memcpy(fldDef.autoVal_, fTuple->autoVal_,);
335 list.append(fldDef);
338 return ptrToAutoVal;
341 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
343 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
344 int i=0;
345 char *fName = NULL;
346 bool found = false;
347 fldList.resetIter();
348 void *data = NULL;
349 DbRetVal rv =OK;
350 while (NULL != (fName = fldList.nextFieldName()))
352 ChunkIterator fIter = fChunk->getIterator();
353 found = false;
354 while (NULL != (data = fIter.nextElement()))
356 if (((CFIELD*)data)->tblPtr_ == tptr)
358 if(0 == strcmp((char*)((CFIELD*)data)->fldName_, fName))
360 found = true;
361 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
362 fptr[i++] = (char*) data;
363 break;
367 if (!found)
369 printError(ErrNotFound,
370 "No entries found in FIELD catalog table for the table specified");
371 return ErrNotFound;
374 return rv;
378 List CatalogTableUSER::getUserList()
380 List userList;
381 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
382 ChunkIterator iter = chk->getIterator();
383 void *tptr;
384 while (NULL != (tptr = iter.nextElement()))
386 Identifier *elem = new Identifier();
387 strcpy(elem->name, ((CUSER*)tptr)->userName_);
388 userList.append(elem);
390 return userList;
394 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
396 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
397 DbRetVal rv = OK;
398 ChunkIterator iter = tChunk->getIterator();
399 void *data = NULL;
400 while ((data = iter.nextElement())!= NULL)
402 if (0 == strcmp(((CUSER*)data)->userName_, name))
404 printError(ErrAlready, "User with name \'%s\' already exists ", name);
405 return ErrAlready;
409 CUSER *usrInfo = (CUSER*)tChunk->allocate(systemDatabase_, &rv);
410 if (NULL == usrInfo)
412 printError(rv,
413 "Could not allocate for USER catalog table");
414 return rv;
416 strcpy(usrInfo->userName_, name);
417 strcpy(usrInfo->password_, pass);
418 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
419 return OK;
423 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
424 bool &isAuthenticated, bool &isDba)
426 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
427 ChunkIterator iter = tChunk->getIterator();
428 void *data = NULL;
429 while (NULL != (data = iter.nextElement()))
431 if (strcmp(((CUSER*)data)->userName_, name) == 0)
433 //verify the password
434 //char * enpass = os::encrypt(pass,"A0");
435 char * enpass = (char*) pass;
436 if (0 == strcmp(enpass, ((CUSER*)data)->password_))
438 isAuthenticated = true;
439 if (0 == strcmp(((CUSER*)data)->userName_, DBAUSER))
440 isDba = true; else isDba = false;
441 return OK;
445 isAuthenticated = false;
446 return OK;
449 DbRetVal CatalogTableUSER::remove(const char *name)
451 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
452 ChunkIterator iter = tChunk->getIterator();
453 void *data = NULL;
454 while ((data = iter.nextElement())!= NULL)
456 if (strcmp(((CUSER*)data)->userName_, name) == 0)
458 //remove this element
459 tChunk->free(systemDatabase_, data);
460 return OK;
463 printError(ErrNotExists,"User %s not exists in catalog table", name);
464 return ErrNotExists;
467 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
469 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
470 ChunkIterator iter = tChunk->getIterator();
471 void *data = NULL;
472 while (NULL != (data = iter.nextElement()))
474 if (strcmp(((CUSER*)data)->userName_, name) == 0)
476 //change the password
477 strcpy(((CUSER*)data)->password_,pass);// os::encrypt(pass, "A0"));
478 return OK;
481 printError(ErrNotExists,"User %s not exists in catalog table", name);
482 return ErrNotExists;