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 DbRetVal
CatalogTableTABLE::insert(const char *name
, int id
, size_t size
,
23 int numFlds
, void* chunk
, void *&tptr
)
25 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
27 tptr
= tChunk
->allocate(systemDatabase_
, &rv
);
31 "Could not allocate memory for for TABLE catalog table");
34 TABLE
*tableInfo
= (TABLE
*)tptr
;
35 strcpy(tableInfo
->tblName_
, name
);
36 tableInfo
->tblID_
= id
;
37 tableInfo
->length_
= size
;
38 tableInfo
->numFlds_
= numFlds
;
39 tableInfo
->numIndexes_
= 0;
40 tableInfo
->chunkPtr_
= chunk
;
41 printDebug(DM_SystemDatabase
,"One Row inserted into TABLE %x %s",tptr
, name
);
45 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
47 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
48 ChunkIterator iter
= tChunk
->getIterator();
51 while ((data
= iter
.nextElement())!= NULL
)
53 if (0 == strcmp(((TABLE
*)data
)->tblName_
, name
))
55 //remove this element and store the tblPtr
56 //there will be only one row for this table(Primary key)
58 chunk
= (Chunk
*) ((TABLE
*)data
)->chunkPtr_
;
64 tChunk
->free(systemDatabase_
, tptr
);
65 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
69 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
75 DbRetVal
CatalogTableTABLE::getChunkAndTblPtr(const char *name
,
76 void *&chunk
, void *&tptr
)
78 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
79 ChunkIterator iter
= chk
->getIterator();;
80 while (NULL
!= (tptr
= iter
.nextElement()))
82 if (strcmp(((TABLE
*)tptr
)->tblName_
, name
) == 0)
84 //there will be only one row for this table(Primary key)
85 chunk
= (Chunk
*) ((TABLE
*)tptr
)->chunkPtr_
;
89 //table not found in TABLE
93 List
CatalogTableTABLE::getTableList()
96 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
97 ChunkIterator iter
= chk
->getIterator();
99 while (NULL
!= (tptr
= iter
.nextElement()))
101 Identifier
*elem
= new Identifier();
102 strcpy(elem
->name
, ((TABLE
*)tptr
)->tblName_
);
103 tableList
.append(elem
);
108 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
110 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
112 while (iter
.hasElement())
114 void *fptr
= fChunk
->allocate(systemDatabase_
, &rv
);
118 "Could not allocate for FIELD catalog table");
121 FIELD
*fldInfo
= (FIELD
*)fptr
;
122 FieldDef fDef
= iter
.nextElement();
123 strcpy(fldInfo
->fldName_
, fDef
.fldName_
);
124 fldInfo
->tblID_
= tblID
;
125 fldInfo
->tblPtr_
= tptr
;
126 fldInfo
->type_
= fDef
.type_
;
127 fldInfo
->length_
= fDef
.length_
;
128 fldInfo
->offset_
= 0; //TODO
129 os::memcpy(fldInfo
->defaultValueBuf_
, fDef
.defaultValueBuf_
,
130 DEFAULT_VALUE_BUF_LENGTH
);
131 fldInfo
->isNull_
= fDef
.isNull_
;
132 fldInfo
->isPrimary_
= fDef
.isPrimary_
;
133 fldInfo
->isUnique_
= fDef
.isUnique_
;
134 fldInfo
->isDefault_
= fDef
.isDefault_
;
135 fldInfo
->width_
= 0; //TODO
136 fldInfo
->scale_
= 0; //TODO
137 printDebug(DM_SystemDatabase
,"One Row inserted into FIELD %x %s",fldInfo
, fDef
.fldName_
);
143 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
145 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
146 ChunkIterator fIter
= fChunk
->getIterator();
148 while ((data
= fIter
.nextElement())!= NULL
)
150 if (((FIELD
*)data
)->tblPtr_
== tptr
)
152 //remove this element
153 fChunk
->free(systemDatabase_
, data
);
154 printDebug(DM_SystemDatabase
,"One Row deleted from FIELD %x",data
);
160 void CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
162 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
163 ChunkIterator fIter
= fChunk
->getIterator();;
165 while (NULL
!= (data
= fIter
.nextElement()))
167 if (((FIELD
*)data
)->tblPtr_
== tptr
)
169 //add the information to the field list
170 FIELD
*fTuple
= (FIELD
*)data
;
172 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
173 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
174 fldDef
.type_
= fTuple
->type_
;
175 fldDef
.length_
= fTuple
->length_
;
176 fldDef
.isDefault_
= fTuple
->isDefault_
;
177 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
178 DEFAULT_VALUE_BUF_LENGTH
);
179 fldDef
.isNull_
= fTuple
->isNull_
;
180 fldDef
.isUnique_
= fTuple
->isUnique_
;
181 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
188 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
190 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
197 while (NULL
!= (fName
= fldList
.nextFieldName()))
199 ChunkIterator fIter
= fChunk
->getIterator();
201 while (NULL
!= (data
= fIter
.nextElement()))
203 if (((FIELD
*)data
)->tblPtr_
== tptr
)
205 if(0 == strcmp((char*)((FIELD
*)data
)->fldName_
, fName
))
208 //if (! ((FIELD*)data)->isNull_) rv = ErrBadCall;
209 fptr
[i
++] = (char*) data
;
216 printError(ErrNotFound
,
217 "No entries found in FIELD catalog table for the table specified");
224 DbRetVal
CatalogTableINDEX::insert(const char *name
, void *tptr
, int numFlds
, bool isUnique
,
225 void* chunk
, int bucketSize
, void *hChunk
, void *&tupleptr
)
227 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
228 ChunkIterator iter
= tChunk
->getIterator();
230 //Checking for index having same name, proceed further only
231 //if no such indexes are
233 while ((data
= iter
.nextElement())!= NULL
)
235 if (0 == strcmp(((INDEX
*)data
)->indName_
, name
))
237 printError(ErrAlready
, "Index with name \'%s\' already exists "
238 "on the table \'%s\'.", name
, ((TABLE
*)tptr
)->tblName_
);
245 tupleptr
= tChunk
->allocate(systemDatabase_
, &rv
);
246 if (NULL
== tupleptr
)
249 "Could not allocate for INDEX catalog table");
252 INDEX
*indexInfo
= (INDEX
*)tupleptr
;
253 strcpy(indexInfo
->indName_
, name
);
254 indexInfo
->tblID_
= -1; //Not used currently
255 indexInfo
->tblPtr_
= tptr
;
256 indexInfo
->numFlds_
= numFlds
;
257 indexInfo
->indexType_
= hashIndex
;
258 indexInfo
->chunkPtr_
= chunk
;
259 indexInfo
->hashNodeChunk_
= hChunk
;
260 indexInfo
->noOfBuckets_
= bucketSize
;
261 indexInfo
->isUnique_
= isUnique
;
262 printDebug(DM_SystemDatabase
,"One Row inserted into INDEX %x %s",tupleptr
, name
);
266 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
268 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
269 ChunkIterator iter
= fChunk
->getIterator();
272 while ((data
= iter
.nextElement())!= NULL
)
274 if (0 == strcmp(((INDEX
*)data
)->indName_
, name
))
276 //remove this element and store the tuple ptr
277 //there will be only one row for this table(Primary key)
278 chunk
= (Chunk
*) ((INDEX
*)data
)->chunkPtr_
;
279 hchunk
= (Chunk
*) ((INDEX
*)data
)->hashNodeChunk_
;
286 fChunk
->free(systemDatabase_
, iptr
);
287 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
291 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
296 DbRetVal
CatalogTableINDEX::get(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
298 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
299 ChunkIterator iter
= fChunk
->getIterator();
302 while ((data
= iter
.nextElement())!= NULL
)
304 if (0 == strcmp(((INDEX
*)data
)->indName_
, name
))
306 //remove this element and store the tuple ptr
307 //there will be only one row for this table(Primary key)
308 chunk
= (Chunk
*) ((INDEX
*)data
)->chunkPtr_
;
309 hchunk
= (Chunk
*) ((INDEX
*)data
)->hashNodeChunk_
;
316 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
322 int CatalogTableINDEX::getNumIndexes(void *tptr
)
324 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
325 ChunkIterator iter
= fChunk
->getIterator();
328 while (NULL
!= (iptr
= iter
.nextElement()))
330 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) numIndex
++;
335 char* CatalogTableINDEX::getIndexName(void *tptr
, int position
)
337 if (position
== 0) return NULL
;
338 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
339 ChunkIterator iter
= fChunk
->getIterator();
343 while (NULL
!= (iptr
= iter
.nextElement()))
345 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
346 if ( curPos
== position
) return ((INDEX
*)iptr
)->indName_
;
352 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
355 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
356 ChunkIterator iter
= fChunk
->getIterator();
358 while (NULL
!= (iptr
= iter
.nextElement()))
360 if (((INDEX
*)iptr
)->tblPtr_
== tptr
)
362 array
[i
++] = (char*) iptr
;
368 ChunkIterator
CatalogTableINDEX::getIterator(void *iptr
)
370 INDEX
*index
= (INDEX
*)iptr
;
371 return ((Chunk
*)index
->chunkPtr_
)->getIterator();
375 int CatalogTableINDEX::getNoOfBuckets(void *iptr
)
377 INDEX
*index
= (INDEX
*)iptr
;
378 return index
->noOfBuckets_
;
381 int CatalogTableINDEX::getUnique(void *iptr
)
383 INDEX
*index
= (INDEX
*)iptr
;
384 return index
->isUnique_
;
386 char* CatalogTableINDEX::getName(void *iptr
)
388 INDEX
*index
= (INDEX
*)iptr
;
389 return index
->indName_
;
392 DbRetVal
CatalogTableINDEXFIELD::insert(FieldNameList
&fldList
, void *indexPtr
,
393 void *tblPtr
, char **&fptr
)
397 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
402 ChunkIterator ifIter
= fChunk
->getIterator();
403 while (NULL
!= (fName
= fldList
.nextFieldName()))
405 ifIter
= fChunk
->getIterator();
406 while ((data
= ifIter
.nextElement()) != NULL
) {
407 if (0 == strcmp(((FIELD
*)((INDEXFIELD
*) data
)->fieldPtr
)->fldName_
, fName
) && ((INDEXFIELD
*)data
)->tablePtr
== tblPtr
) {
408 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_
);
413 void *fieldptr
= fChunk
->allocate(systemDatabase_
, &rv
);
414 if (NULL
== fieldptr
)
417 "Could not allocate for USER catalog table");
420 INDEXFIELD
*fldInfo
= (INDEXFIELD
*)fieldptr
;
421 fldInfo
->tablePtr
= tblPtr
;
422 fldInfo
->fieldPtr
= (FIELD
*)fptr
[i
++];
423 fldInfo
->indexPtr
= indexPtr
;
424 printDebug(DM_SystemDatabase
,"One Row inserted into INDEXFIELD %x", fldInfo
);
429 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
432 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
433 ChunkIterator fIter
= fChunk
->getIterator();
435 while ((data
= fIter
.nextElement())!= NULL
)
437 if (((INDEXFIELD
*)data
)->indexPtr
== iptr
)
439 //remove this element
440 fChunk
->free(systemDatabase_
, data
);
441 printDebug(DM_SystemDatabase
,"One Row deleted from INDEXFIELD %x", data
);
447 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
448 char *&name
, DataType
&type
)
451 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
452 ChunkIterator ifIter
= ifChunk
->getIterator();
454 while ((data
= ifIter
.nextElement())!= NULL
)
456 if (((INDEXFIELD
*)data
)->indexPtr
== index
)
458 //store the field name
459 name
= ((FIELD
*)(((INDEXFIELD
*)data
)->fieldPtr
))->fldName_
;
460 type
= ((FIELD
*)(((INDEXFIELD
*)data
)->fieldPtr
))->type_
;
464 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
468 DbRetVal
CatalogTableINDEXFIELD::getFieldInfo(void *index
, FieldList
&list
)
471 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
472 ChunkIterator ifIter
= ifChunk
->getIterator();
475 while ((data
= ifIter
.nextElement())!= NULL
)
477 if (((INDEXFIELD
*)data
)->indexPtr
== index
)
479 //add the information to the field list
480 FIELD
*fTuple
= (FIELD
*)(((INDEXFIELD
*)data
)->fieldPtr
);
482 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
483 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
484 fldDef
.type_
= fTuple
->type_
;
485 fldDef
.length_
= fTuple
->length_
;
486 fldDef
.isDefault_
= fTuple
->isDefault_
;
487 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
488 DEFAULT_VALUE_BUF_LENGTH
);
489 fldDef
.isNull_
= fTuple
->isNull_
;
490 fldDef
.isUnique_
= fTuple
->isUnique_
;
491 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
497 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
503 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
505 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
507 USER
*usrInfo
= (USER
*)tChunk
->allocate(systemDatabase_
, &rv
);
511 "Could not allocate for USER catalog table");
514 strcpy(usrInfo
->userName_
, name
);
515 strcpy(usrInfo
->password_
, os::encrypt(pass
, "A0"));
520 DbRetVal
CatalogTableUSER::authenticate(const char *name
, const char *pass
,
521 bool &isAuthenticated
, bool &isDba
)
523 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
524 ChunkIterator iter
= tChunk
->getIterator();
526 while (NULL
!= (data
= iter
.nextElement()))
528 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
530 //verify the password
531 char * enpass
= os::encrypt(pass
,"A0");
532 if (0 == strcmp(enpass
, ((USER
*)data
)->password_
))
534 isAuthenticated
= true;
535 if (0 == strcmp(((USER
*)data
)->userName_
, DBAUSER
))
536 isDba
= true; else isDba
= false;
541 isAuthenticated
= false;
545 DbRetVal
CatalogTableUSER::remove(const char *name
)
547 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
548 ChunkIterator iter
= tChunk
->getIterator();
550 while ((data
= iter
.nextElement())!= NULL
)
552 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
554 //remove this element
555 tChunk
->free(systemDatabase_
, data
);
559 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
563 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
565 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
566 ChunkIterator iter
= tChunk
->getIterator();
568 while (NULL
!= (data
= iter
.nextElement()))
570 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
572 //change the password
573 strcpy(((USER
*)data
)->password_
, os::encrypt(pass
, "A0"));
577 printError(ErrNotExists
,"User %s not exists in catalog table", name
);