Initial revision
[csql.git] / src / server / CatalogTables.c
blob0f22923dc7a8a04a763d1ef9981b66e68f4f1fc9
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<CatalogTables.h>
17 #include<Database.h>
18 #include<Allocator.h>
19 #include<Field.h>
20 #include<Debug.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_);
27 if (NULL == tptr)
29 printError(ErrNoMemory,
30 "No memory to allocate for TABLE catalog table");
31 return ErrNoMemory;
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);
41 return OK;
44 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
46 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
47 ChunkIterator iter = tChunk->getIterator();
49 void *data = NULL;
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)
56 tptr = (void*) data;
57 chunk = (Chunk*) ((TABLE*)data)->chunkPtr_;
58 break;
61 if (NULL != tptr)
63 tChunk->free(systemDatabase_, tptr);
64 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
66 else
68 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
69 return ErrNotExists;
71 return OK;
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_;
85 return OK;
88 //table not found in TABLE
89 printError(ErrNotFound,
90 "No entries found in TABLE catalog table for the table %s", name);
91 return ErrNotFound;
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_);
101 if (NULL == fptr)
103 printError(ErrNoMemory,
104 "No memory to allocate for FIELD catalog table");
105 return ErrNoMemory;
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_);
125 return OK;
128 DbRetVal CatalogTableFIELD::remove(void *tptr)
130 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
131 ChunkIterator fIter = fChunk->getIterator();
132 void *data = NULL;
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);
142 return OK;
145 void CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
147 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
148 ChunkIterator fIter = fChunk->getIterator();;
149 void *data = NULL;
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;
156 FieldDef fldDef;
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_;
166 list.append(fldDef);
169 return;
172 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
174 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
175 int i=0;
176 char *fName = NULL;
177 bool found = false;
178 fldList.resetIter();
179 void *data = NULL;
180 while (NULL != (fName = fldList.nextFieldName()))
182 ChunkIterator fIter = fChunk->getIterator();
183 found = false;
184 while (NULL != (data = fIter.nextElement()))
186 if (((FIELD*)data)->tblPtr_ == tptr)
188 if(0 == strcmp((char*)((FIELD*)data)->fldName_, fName))
190 found = true;
191 fptr[i++] = (char*) data;
192 break;
196 if (!found)
198 printError(ErrNotFound,
199 "No entries found in FIELD catalog table for the table specified");
200 return ErrNotFound;
203 return OK;
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");
215 return ErrNoMemory;
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);
228 return OK;
231 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
233 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
234 ChunkIterator iter = fChunk->getIterator();
236 void *data = NULL;
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_;
245 iptr = (void*) data;
246 break;
249 if (NULL != iptr)
251 fChunk->free(systemDatabase_, iptr);
252 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
254 else
256 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
257 return ErrNotExists;
259 return OK;
262 int CatalogTableINDEX::getNumIndexes(void *tptr)
264 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
265 ChunkIterator iter = fChunk->getIterator();
266 void *iptr = NULL;
267 int numIndex =0;
268 while (NULL != (iptr = iter.nextElement()))
270 if (((INDEX*)iptr)->tblPtr_ == tptr) numIndex++;
272 return 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();
280 void *iptr = NULL;
281 int numIndex =0;
282 int curPos =0;
283 while (NULL != (iptr = iter.nextElement()))
285 if (((INDEX*)iptr)->tblPtr_ == tptr) curPos++;
286 if ( curPos == position ) return ((INDEX*)iptr)->indName_;
288 return NULL;
292 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
294 void *iptr = NULL;
295 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
296 ChunkIterator iter = fChunk->getIterator();
297 int i=0;
298 while (NULL != (iptr = iter.nextElement()))
300 if (((INDEX*)iptr)->tblPtr_ == tptr)
302 array[i++] = (char*) iptr;
305 return;
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)
325 Chunk *fChunk;
326 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
327 fldList.resetIter();
328 int i =0;
329 char *fName =NULL;
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");
337 return ErrNoMemory;
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);
345 return OK;
348 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
350 Chunk *fChunk;
351 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
352 ChunkIterator fIter = fChunk->getIterator();
353 void *data = NULL;
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);
363 return OK;
366 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
367 char *&name, DataType &type)
369 Chunk *ifChunk;
370 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
371 ChunkIterator ifIter = ifChunk->getIterator();
372 void *data = NULL;
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_;
380 return OK;
383 printError(ErrNotExists,"Index %x not exists in catalog table", index);
384 return ErrNotExists;
387 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
389 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
390 USER *usrInfo = (USER*)tChunk->allocate(systemDatabase_);
391 if (NULL == usrInfo)
393 printError(ErrNoMemory,
394 "No memory to allocate for USER catalog table");
395 return ErrNoMemory;
397 strcpy(usrInfo->userName_, name);
398 strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
399 return OK;
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();
408 void *data = NULL;
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))
419 isDba = true;
420 return OK;
424 isAuthenticated = false;
425 return OK;
428 DbRetVal CatalogTableUSER::remove(const char *name)
430 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
431 ChunkIterator iter = tChunk->getIterator();
432 void *data = NULL;
433 while ((data = iter.nextElement())!= NULL)
435 if (strcmp(((USER*)data)->userName_, name) == 0)
437 //remove this element
438 tChunk->free(systemDatabase_, data);
439 return OK;
442 printError(ErrNotExists,"User %s not exists in catalog table", name);
443 return ErrNotExists;
446 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
448 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
449 ChunkIterator iter = tChunk->getIterator();
450 void *data = NULL;
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"));
457 return OK;
460 printError(ErrNotExists,"User %s not exists in catalog table", name);
461 return ErrNotExists;