1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
16 #include<CatalogTables.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
);
29 tptr
= tChunk
->allocate(systemDatabase_
, &rv
);
33 "Could not allocate memory for for TABLE catalog table");
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
);
48 DbRetVal
CatalogTableTABLE::renameTable( const char *oldName
,const char *newName
)
50 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
51 ChunkIterator iter
= tChunk
->getIterator();
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
;
62 if (0 == strcmp(((CTABLE
*)data
)->tblName_
,newName
))
64 printError(ErrNotExists
,"A Table with name %s already exists", newName
);
68 strcpy(oldTable
->tblName_
, newName
);
70 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", oldName
);
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
);
88 DbRetVal
CatalogTableTABLE::renameIndex( const char *oldName
,const char *newName
)
90 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
91 ChunkIterator iter
= tChunk
->getIterator();
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
;
102 if (0 == strcmp(((CINDEX
*)data
)->indName_
,newName
))
104 printError(ErrNotExists
,"A Index with name %s already exists", newName
);
109 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", oldName
);
112 strcpy(oldIndex
->indName_
, newName
);
117 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
119 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
120 ChunkIterator iter
= tChunk
->getIterator();
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)
130 chunk
= (Chunk
*) ((CTABLE
*)data
)->chunkPtr_
;
136 tChunk
->free(systemDatabase_
, tptr
);
137 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
141 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
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_
;
162 //table not found in TABLE
167 DbRetVal
CatalogTableTABLE::setChunkPtr(const char *name
, void *firstPage
, void *curPage
)
169 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
170 ChunkIterator iter
= chk
->getIterator();;
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
);
182 //table not found in TABLE
186 List
CatalogTableTABLE::getTableList()
189 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
190 ChunkIterator iter
= chk
->getIterator();
192 while (NULL
!= (tptr
= iter
.nextElement()))
194 Identifier
*elem
= new Identifier();
195 strcpy(elem
->name
, ((CTABLE
*)tptr
)->tblName_
);
196 tableList
.append(elem
);
201 DbRetVal
CatalogTableFIELD::renameField(const char *tableName
, const char *oldName
, const char *newName
)
203 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
204 ChunkIterator iter
= fChunk
->getIterator();
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
);
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;
227 printError(ErrNotExists
, "Old Field Name '%s' does not exist in table",
234 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
236 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
238 while (iter
.hasElement())
240 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
244 "Could not allocate for FIELD catalog table");
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_
);
271 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
273 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
274 ChunkIterator fIter
= fChunk
->getIterator();
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
);
288 void *CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
290 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
291 ChunkIterator fIter
= fChunk
->getIterator();;
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
;
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_,);
323 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
325 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
332 while (NULL
!= (fName
= fldList
.nextFieldName()))
334 ChunkIterator fIter
= fChunk
->getIterator();
336 while (NULL
!= (data
= fIter
.nextElement()))
338 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
340 if(0 == strcmp((char*)((CFIELD
*)data
)->fldName_
, fName
))
343 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
344 fptr
[i
++] = (char*) data
;
351 printError(ErrNotFound
,
352 "No entries found in FIELD catalog table for the table specified");
360 List
CatalogTableUSER::getUserList()
363 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
364 ChunkIterator iter
= chk
->getIterator();
366 while (NULL
!= (tptr
= iter
.nextElement()))
368 Identifier
*elem
= new Identifier();
369 strcpy(elem
->name
, ((CUSER
*)tptr
)->userName_
);
370 userList
.append(elem
);
376 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
378 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
380 ChunkIterator iter
= tChunk
->getIterator();
382 while ((data
= iter
.nextElement())!= NULL
)
384 if (0 == strcmp(((CUSER
*)data
)->userName_
, name
))
386 printError(ErrAlready
, "User with name \'%s\' already exists ", name
);
391 CUSER
*usrInfo
= (CUSER
*)tChunk
->allocate(systemDatabase_
, &rv
);
395 "Could not allocate for USER catalog table");
398 strcpy(usrInfo
->userName_
, name
);
399 strcpy(usrInfo
->password_
, pass
);
400 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
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();
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;
427 isAuthenticated
= false;
431 DbRetVal
CatalogTableUSER::remove(const char *name
)
433 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
434 ChunkIterator iter
= tChunk
->getIterator();
436 while ((data
= iter
.nextElement())!= NULL
)
438 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
440 //remove this element
441 tChunk
->free(systemDatabase_
, data
);
445 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
449 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
451 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
452 ChunkIterator iter
= tChunk
->getIterator();
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"));
463 printError(ErrNotExists
,"User %s not exists in catalog table", name
);