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
]={"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
);
29 tptr
= tChunk
->allocate(systemDatabase_
, &rv
);
33 "Could not allocate memory for for TABLE catalog table");
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
);
47 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
49 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
50 ChunkIterator iter
= tChunk
->getIterator();
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)
60 chunk
= (Chunk
*) ((TABLE
*)data
)->chunkPtr_
;
66 tChunk
->free(systemDatabase_
, tptr
);
67 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
71 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
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_
;
91 //table not found in TABLE
95 List
CatalogTableTABLE::getTableList()
98 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
99 ChunkIterator iter
= chk
->getIterator();
101 while (NULL
!= (tptr
= iter
.nextElement()))
103 Identifier
*elem
= new Identifier();
104 strcpy(elem
->name
, ((TABLE
*)tptr
)->tblName_
);
105 tableList
.append(elem
);
110 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
112 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
114 while (iter
.hasElement())
116 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
120 "Could not allocate for FIELD catalog table");
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_
);
145 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
147 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
148 ChunkIterator fIter
= fChunk
->getIterator();
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
);
162 void CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
164 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
165 ChunkIterator fIter
= fChunk
->getIterator();;
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
;
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_
;
190 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
192 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
199 while (NULL
!= (fName
= fldList
.nextFieldName()))
201 ChunkIterator fIter
= fChunk
->getIterator();
203 while (NULL
!= (data
= fIter
.nextElement()))
205 if (((FIELD
*)data
)->tblPtr_
== tptr
)
207 if(0 == strcmp((char*)((FIELD
*)data
)->fldName_
, fName
))
210 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
211 fptr
[i
++] = (char*) data
;
218 printError(ErrNotFound
,
219 "No entries found in FIELD catalog table for the table specified");
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
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_
);
247 tupleptr
= tChunk
->allocate(systemDatabase_
, &rv
);
248 if (NULL
== tupleptr
)
251 "Could not allocate for INDEX catalog table");
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
);
268 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
270 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
271 ChunkIterator iter
= fChunk
->getIterator();
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_
;
288 fChunk
->free(systemDatabase_
, iptr
);
289 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
293 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
298 DbRetVal
CatalogTableINDEX::get(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
300 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
301 ChunkIterator iter
= fChunk
->getIterator();
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_
;
318 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
324 int CatalogTableINDEX::getNumIndexes(void *tptr
)
326 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
327 ChunkIterator iter
= fChunk
->getIterator();
330 while (NULL
!= (iptr
= iter
.nextElement()))
332 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) 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();
345 while (NULL
!= (iptr
= iter
.nextElement()))
347 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
348 if ( curPos
== position
) return ((INDEX
*)iptr
)->indName_
;
354 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
357 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
358 ChunkIterator iter
= fChunk
->getIterator();
360 while (NULL
!= (iptr
= iter
.nextElement()))
362 if (((INDEX
*)iptr
)->tblPtr_
== tptr
)
364 array
[i
++] = (char*) iptr
;
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
)
399 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
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_
);
415 void *fieldptr
= fChunk
->allocate(systemDatabase_
, &rv
);
416 if (NULL
== fieldptr
)
419 "Could not allocate for USER catalog table");
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
);
431 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
434 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
435 ChunkIterator fIter
= fChunk
->getIterator();
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
);
449 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
450 char *&name
, DataType
&type
)
453 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
454 ChunkIterator ifIter
= ifChunk
->getIterator();
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_
;
466 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
470 DbRetVal
CatalogTableINDEXFIELD::getFieldInfo(void *index
, FieldList
&list
)
473 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
474 ChunkIterator ifIter
= ifChunk
->getIterator();
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
);
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_
;
499 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
505 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
507 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
509 USER
*usrInfo
= (USER
*)tChunk
->allocate(systemDatabase_
, &rv
);
513 "Could not allocate for USER catalog table");
516 strcpy(usrInfo
->userName_
, name
);
517 strcpy(usrInfo
->password_
, os::encrypt(pass
, "A0"));
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();
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;
543 isAuthenticated
= false;
547 DbRetVal
CatalogTableUSER::remove(const char *name
)
549 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
550 ChunkIterator iter
= tChunk
->getIterator();
552 while ((data
= iter
.nextElement())!= NULL
)
554 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
556 //remove this element
557 tChunk
->free(systemDatabase_
, data
);
561 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
565 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
567 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
568 ChunkIterator iter
= tChunk
->getIterator();
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"));
579 printError(ErrNotExists
,"User %s not exists in catalog table", name
);