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>
22 char ChunkName
[MAX_CHUNKS
][CHUNK_NAME_LEN
]={
24 "LockTableHashBucketId",
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
);
47 tptr
= tChunk
->allocate(systemDatabase_
, &rv
);
51 "Could not allocate memory for for TABLE catalog table");
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
);
66 DbRetVal
CatalogTableTABLE::renameTable( const char *oldName
,const char *newName
)
68 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
69 ChunkIterator iter
= tChunk
->getIterator();
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
;
80 if (0 == strcmp(((CTABLE
*)data
)->tblName_
,newName
))
82 printError(ErrNotExists
,"A Table with name %s already exists", newName
);
86 strcpy(oldTable
->tblName_
, newName
);
88 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", oldName
);
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
);
106 DbRetVal
CatalogTableTABLE::renameIndex( const char *oldName
,const char *newName
)
108 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
109 ChunkIterator iter
= tChunk
->getIterator();
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
;
120 if (0 == strcmp(((CINDEX
*)data
)->indName_
,newName
))
122 printError(ErrNotExists
,"A Index with name %s already exists", newName
);
127 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", oldName
);
130 strcpy(oldIndex
->indName_
, newName
);
135 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
137 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
138 ChunkIterator iter
= tChunk
->getIterator();
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)
148 chunk
= (Chunk
*) ((CTABLE
*)data
)->chunkPtr_
;
154 tChunk
->free(systemDatabase_
, tptr
);
155 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
159 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
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_
;
180 //table not found in TABLE
185 DbRetVal
CatalogTableTABLE::setChunkPtr(const char *name
, void *firstPage
, void *curPage
)
187 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
188 ChunkIterator iter
= chk
->getIterator();;
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
);
200 //table not found in TABLE
204 List
CatalogTableTABLE::getTableList()
207 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
208 ChunkIterator iter
= chk
->getIterator();
210 while (NULL
!= (tptr
= iter
.nextElement()))
212 Identifier
*elem
= new Identifier();
213 strcpy(elem
->name
, ((CTABLE
*)tptr
)->tblName_
);
214 tableList
.append(elem
);
219 DbRetVal
CatalogTableFIELD::renameField(const char *tableName
, const char *oldName
, const char *newName
)
221 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
222 ChunkIterator iter
= fChunk
->getIterator();
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
);
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;
245 printError(ErrNotExists
, "Old Field Name '%s' does not exist in table",
252 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
254 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
256 while (iter
.hasElement())
258 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
262 "Could not allocate for FIELD catalog table");
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_
);
289 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
291 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
292 ChunkIterator fIter
= fChunk
->getIterator();
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
);
306 void *CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
308 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
309 ChunkIterator fIter
= fChunk
->getIterator();;
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
;
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_,);
341 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
343 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
350 while (NULL
!= (fName
= fldList
.nextFieldName()))
352 ChunkIterator fIter
= fChunk
->getIterator();
354 while (NULL
!= (data
= fIter
.nextElement()))
356 if (((CFIELD
*)data
)->tblPtr_
== tptr
)
358 if(0 == strcmp((char*)((CFIELD
*)data
)->fldName_
, fName
))
361 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
362 fptr
[i
++] = (char*) data
;
369 printError(ErrNotFound
,
370 "No entries found in FIELD catalog table for the table specified");
378 List
CatalogTableUSER::getUserList()
381 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
382 ChunkIterator iter
= chk
->getIterator();
384 while (NULL
!= (tptr
= iter
.nextElement()))
386 Identifier
*elem
= new Identifier();
387 strcpy(elem
->name
, ((CUSER
*)tptr
)->userName_
);
388 userList
.append(elem
);
394 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
396 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
398 ChunkIterator iter
= tChunk
->getIterator();
400 while ((data
= iter
.nextElement())!= NULL
)
402 if (0 == strcmp(((CUSER
*)data
)->userName_
, name
))
404 printError(ErrAlready
, "User with name \'%s\' already exists ", name
);
409 CUSER
*usrInfo
= (CUSER
*)tChunk
->allocate(systemDatabase_
, &rv
);
413 "Could not allocate for USER catalog table");
416 strcpy(usrInfo
->userName_
, name
);
417 strcpy(usrInfo
->password_
, pass
);
418 //strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
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();
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;
445 isAuthenticated
= false;
449 DbRetVal
CatalogTableUSER::remove(const char *name
)
451 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
452 ChunkIterator iter
= tChunk
->getIterator();
454 while ((data
= iter
.nextElement())!= NULL
)
456 if (strcmp(((CUSER
*)data
)->userName_
, name
) == 0)
458 //remove this element
459 tChunk
->free(systemDatabase_
, data
);
463 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
467 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
469 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
470 ChunkIterator iter
= tChunk
->getIterator();
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"));
481 printError(ErrNotExists
,"User %s not exists in catalog table", name
);