1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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
);
26 tptr
= tChunk
->allocate(systemDatabase_
);
29 printError(ErrNoMemory
,
30 "No memory to allocate for TABLE catalog table");
33 TABLE
*tableInfo
= (TABLE
*)tptr
;
34 strcpy(tableInfo
->tblName_
, name
);
35 tableInfo
->tblID_
= id
;
36 tableInfo
->length_
= size
;
37 tableInfo
->numFlds_
= numFlds
;
38 tableInfo
->numIndexes_
= 0;
39 tableInfo
->chunkPtr_
= chunk
;
40 printDebug(DM_SystemDatabase
,"One Row inserted into TABLE %x %s",tptr
, name
);
44 DbRetVal
CatalogTableTABLE::remove(const char *name
, void *&chunk
, void *&tptr
)
46 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
47 ChunkIterator iter
= tChunk
->getIterator();
50 while ((data
= iter
.nextElement())!= NULL
)
52 if (0 == strcmp(((TABLE
*)data
)->tblName_
, name
))
54 //remove this element and store the tblPtr
55 //there will be only one row for this table(Primary key)
57 chunk
= (Chunk
*) ((TABLE
*)data
)->chunkPtr_
;
63 tChunk
->free(systemDatabase_
, tptr
);
64 printDebug(DM_SystemDatabase
,"One Row deleted from TABLE %x %s",tptr
, name
);
68 printError(ErrNotExists
,"Table %s not exists in TABLE catalog table", name
);
74 DbRetVal
CatalogTableTABLE::getChunkAndTblPtr(const char *name
,
75 void *&chunk
, void *&tptr
)
77 Chunk
*chk
= systemDatabase_
->getSystemDatabaseChunk(TableTableId
);
78 ChunkIterator iter
= chk
->getIterator();;
79 while (NULL
!= (tptr
= iter
.nextElement()))
81 if (strcmp(((TABLE
*)tptr
)->tblName_
, name
) == 0)
83 //there will be only one row for this table(Primary key)
84 chunk
= (Chunk
*) ((TABLE
*)tptr
)->chunkPtr_
;
88 //table not found in TABLE
89 printError(ErrNotFound
,
90 "No entries found in TABLE catalog table for the table %s", name
);
95 DbRetVal
CatalogTableFIELD::insert(FieldIterator
&iter
, int tblID
, void *tptr
)
97 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
98 while (0 == iter
.hasElement())
100 void *fptr
= fChunk
->allocate(systemDatabase_
);
103 printError(ErrNoMemory
,
104 "No memory to allocate for FIELD catalog table");
107 FIELD
*fldInfo
= (FIELD
*)fptr
;
108 FieldDef fDef
= iter
.nextElement();
109 strcpy(fldInfo
->fldName_
, fDef
.fldName_
);
110 fldInfo
->tblID_
= tblID
;
111 fldInfo
->tblPtr_
= tptr
;
112 fldInfo
->type_
= fDef
.type_
;
113 fldInfo
->length_
= fDef
.length_
;
114 fldInfo
->offset_
= 0; //TODO
115 os::memcpy(fldInfo
->defaultValueBuf_
, fDef
.defaultValueBuf_
,
116 DEFAULT_VALUE_BUF_LENGTH
);
117 fldInfo
->isNull_
= fDef
.isNull_
;
118 fldInfo
->isPrimary_
= fDef
.isPrimary_
;
119 fldInfo
->isDefault_
= fDef
.isDefault_
;
120 fldInfo
->width_
= 0; //TODO
121 fldInfo
->scale_
= 0; //TODO
122 printDebug(DM_SystemDatabase
,"One Row inserted into FIELD %x %s",fldInfo
, fDef
.fldName_
);
128 DbRetVal
CatalogTableFIELD::remove(void *tptr
)
130 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
131 ChunkIterator fIter
= fChunk
->getIterator();
133 while ((data
= fIter
.nextElement())!= NULL
)
135 if (((FIELD
*)data
)->tblPtr_
== tptr
)
137 //remove this element
138 fChunk
->free(systemDatabase_
, data
);
139 printDebug(DM_SystemDatabase
,"One Row deleted from FIELD %x",data
);
145 void CatalogTableFIELD::getFieldInfo(void* tptr
, FieldList
&list
)
147 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
148 ChunkIterator fIter
= fChunk
->getIterator();;
150 while (NULL
!= (data
= fIter
.nextElement()))
152 if (((FIELD
*)data
)->tblPtr_
== tptr
)
154 //add the information to the field list
155 FIELD
*fTuple
= (FIELD
*)data
;
157 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
158 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
159 fldDef
.type_
= fTuple
->type_
;
160 fldDef
.length_
= fTuple
->length_
;
161 fldDef
.isDefault_
= fTuple
->isDefault_
;
162 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
163 DEFAULT_VALUE_BUF_LENGTH
);
164 fldDef
.isNull_
= fTuple
->isNull_
;
165 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
172 DbRetVal
CatalogTableFIELD::getFieldPtrs(FieldNameList
&fldList
,void *tptr
, char **&fptr
)
174 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(FieldTableId
);
180 while (NULL
!= (fName
= fldList
.nextFieldName()))
182 ChunkIterator fIter
= fChunk
->getIterator();
184 while (NULL
!= (data
= fIter
.nextElement()))
186 if (((FIELD
*)data
)->tblPtr_
== tptr
)
188 if(0 == strcmp((char*)((FIELD
*)data
)->fldName_
, fName
))
191 fptr
[i
++] = (char*) data
;
198 printError(ErrNotFound
,
199 "No entries found in FIELD catalog table for the table specified");
206 DbRetVal
CatalogTableINDEX::insert(const char *name
, void *tptr
, int numFlds
,
207 void* chunk
, int bucketSize
, void *hChunk
, void *&tupleptr
)
209 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
210 tupleptr
= tChunk
->allocate(systemDatabase_
);
211 if (NULL
== tupleptr
)
213 printError(ErrNoMemory
,
214 "No memory to allocate for INDEX catalog table");
217 INDEX
*indexInfo
= (INDEX
*)tupleptr
;
218 strcpy(indexInfo
->indName_
, name
);
219 indexInfo
->tblID_
= -1; //Not used currently
220 indexInfo
->tblPtr_
= tptr
;
221 indexInfo
->numFlds_
= numFlds
;
222 indexInfo
->indexType_
= hashIndex
;
223 indexInfo
->chunkPtr_
= chunk
;
224 indexInfo
->hashNodeChunk_
= hChunk
;
225 indexInfo
->noOfBuckets_
= bucketSize
;
226 indexInfo
->isPrimary_
= false;
227 printDebug(DM_SystemDatabase
,"One Row inserted into INDEX %x %s",tupleptr
, name
);
231 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
233 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
234 ChunkIterator iter
= fChunk
->getIterator();
237 while ((data
= iter
.nextElement())!= NULL
)
239 if (0 == strcmp(((INDEX
*)data
)->indName_
, name
))
241 //remove this element and store the tuple ptr
242 //there will be only one row for this table(Primary key)
243 chunk
= (Chunk
*) ((INDEX
*)data
)->chunkPtr_
;
244 hchunk
= (Chunk
*) ((INDEX
*)data
)->hashNodeChunk_
;
251 fChunk
->free(systemDatabase_
, iptr
);
252 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
256 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
262 int CatalogTableINDEX::getNumIndexes(void *tptr
)
264 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
265 ChunkIterator iter
= fChunk
->getIterator();
268 while (NULL
!= (iptr
= iter
.nextElement()))
270 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) numIndex
++;
275 char* CatalogTableINDEX::getIndexName(void *tptr
, int position
)
277 if (position
== 0) return NULL
;
278 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
279 ChunkIterator iter
= fChunk
->getIterator();
283 while (NULL
!= (iptr
= iter
.nextElement()))
285 if (((INDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
286 if ( curPos
== position
) return ((INDEX
*)iptr
)->indName_
;
292 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
295 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
296 ChunkIterator iter
= fChunk
->getIterator();
298 while (NULL
!= (iptr
= iter
.nextElement()))
300 if (((INDEX
*)iptr
)->tblPtr_
== tptr
)
302 array
[i
++] = (char*) iptr
;
308 ChunkIterator
CatalogTableINDEX::getIterator(void *iptr
)
310 INDEX
*index
= (INDEX
*)iptr
;
311 return ((Chunk
*)index
->chunkPtr_
)->getIterator();
315 int CatalogTableINDEX::getNoOfBuckets(void *iptr
)
317 INDEX
*index
= (INDEX
*)iptr
;
318 return index
->noOfBuckets_
;
321 DbRetVal
CatalogTableINDEXFIELD::insert(FieldNameList
&fldList
, void *indexPtr
,
322 void *tblPtr
, char **&fptr
)
326 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
330 while (NULL
!= (fName
= fldList
.nextFieldName()))
332 void *fieldptr
= fChunk
->allocate(systemDatabase_
);
333 if (NULL
== fieldptr
)
335 printError(ErrNoMemory
,
336 "No memory to allocate for USER catalog table");
339 INDEXFIELD
*fldInfo
= (INDEXFIELD
*)fieldptr
;
340 fldInfo
->tablePtr
= tblPtr
;
341 fldInfo
->fieldPtr
= (FIELD
*)fptr
[i
++];
342 fldInfo
->indexPtr
= indexPtr
;
343 printDebug(DM_SystemDatabase
,"One Row inserted into INDEXFIELD %x", fldInfo
);
348 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
351 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
352 ChunkIterator fIter
= fChunk
->getIterator();
354 while ((data
= fIter
.nextElement())!= NULL
)
356 if (((INDEXFIELD
*)data
)->indexPtr
== iptr
)
358 //remove this element
359 fChunk
->free(systemDatabase_
, data
);
360 printDebug(DM_SystemDatabase
,"One Row deleted from INDEXFIELD %x", data
);
366 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
367 char *&name
, DataType
&type
)
370 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
371 ChunkIterator ifIter
= ifChunk
->getIterator();
373 while ((data
= ifIter
.nextElement())!= NULL
)
375 if (((INDEXFIELD
*)data
)->indexPtr
== index
)
377 //store the field name
378 name
= ((FIELD
*)(((INDEXFIELD
*)data
)->fieldPtr
))->fldName_
;
379 type
= ((FIELD
*)(((INDEXFIELD
*)data
)->fieldPtr
))->type_
;
383 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
387 DbRetVal
CatalogTableUSER::insert(const char *name
, const char *pass
)
389 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
390 USER
*usrInfo
= (USER
*)tChunk
->allocate(systemDatabase_
);
393 printError(ErrNoMemory
,
394 "No memory to allocate for USER catalog table");
397 strcpy(usrInfo
->userName_
, name
);
398 strcpy(usrInfo
->password_
, os::encrypt(pass
, "A0"));
403 DbRetVal
CatalogTableUSER::authenticate(const char *name
, const char *pass
,
404 bool &isAuthenticated
, bool &isDba
)
406 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
407 ChunkIterator iter
= tChunk
->getIterator();
409 while (NULL
!= (data
= iter
.nextElement()))
411 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
413 //verify the password
414 char * enpass
= os::encrypt(pass
,"A0");
415 if (0 == strcmp(enpass
, ((USER
*)data
)->password_
))
417 isAuthenticated
= true;
418 if (0 == strcmp(((USER
*)data
)->userName_
, DBAUSER
))
424 isAuthenticated
= false;
428 DbRetVal
CatalogTableUSER::remove(const char *name
)
430 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
431 ChunkIterator iter
= tChunk
->getIterator();
433 while ((data
= iter
.nextElement())!= NULL
)
435 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
437 //remove this element
438 tChunk
->free(systemDatabase_
, data
);
442 printError(ErrNotExists
,"User %s not exists in catalog table", name
);
446 DbRetVal
CatalogTableUSER::changePass(const char *name
, const char *pass
)
448 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(UserTableId
);
449 ChunkIterator iter
= tChunk
->getIterator();
451 while (NULL
!= (data
= iter
.nextElement()))
453 if (strcmp(((USER
*)data
)->userName_
, name
) == 0)
455 //change the password
456 strcpy(((USER
*)data
)->password_
, os::encrypt(pass
, "A0"));
460 printError(ErrNotExists
,"User %s not exists in catalog table", name
);