00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include<CatalogTables.h>
00017 #include<Database.h>
00018 #include<Allocator.h>
00019 #include<Field.h>
00020 #include<Debug.h>
00021
00022 DbRetVal CatalogTableTABLE::insert(const char *name, int id, size_t size,
00023 int numFlds, void* chunk, void *&tptr)
00024 {
00025 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
00026 DbRetVal rv = OK;
00027 tptr = tChunk->allocate(systemDatabase_, &rv);
00028 if (NULL == tptr)
00029 {
00030 printError(rv,
00031 "Could not allocate memory for for TABLE catalog table");
00032 return rv;
00033 }
00034 TABLE *tableInfo = (TABLE*)tptr;
00035 strcpy(tableInfo->tblName_, name);
00036 tableInfo->tblID_ = id;
00037 tableInfo->length_ = size;
00038 tableInfo->numFlds_ = numFlds;
00039 tableInfo->numIndexes_ = 0;
00040 tableInfo->chunkPtr_ = chunk;
00041 printDebug(DM_SystemDatabase,"One Row inserted into TABLE %x %s",tptr, name);
00042 return OK;
00043 }
00044
00045 DbRetVal CatalogTableTABLE::remove(const char *name, void *&chunk, void *&tptr)
00046 {
00047 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
00048 ChunkIterator iter = tChunk->getIterator();
00049
00050 void *data = NULL;
00051 while ((data = iter.nextElement())!= NULL)
00052 {
00053 if (0 == strcmp(((TABLE*)data)->tblName_, name))
00054 {
00055
00056
00057 tptr = (void*) data;
00058 chunk = (Chunk*) ((TABLE*)data)->chunkPtr_;
00059 break;
00060 }
00061 }
00062 if (NULL != tptr)
00063 {
00064 tChunk->free(systemDatabase_, tptr);
00065 printDebug(DM_SystemDatabase,"One Row deleted from TABLE %x %s",tptr, name);
00066 }
00067 else
00068 {
00069 printError(ErrNotExists,"Table %s not exists in TABLE catalog table", name);
00070 return ErrNotExists;
00071 }
00072 return OK;
00073 }
00074
00075 DbRetVal CatalogTableTABLE::getChunkAndTblPtr(const char *name,
00076 void *&chunk, void *&tptr)
00077 {
00078 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
00079 ChunkIterator iter = chk->getIterator();;
00080 while (NULL != (tptr = iter.nextElement()))
00081 {
00082 if (strcmp(((TABLE*)tptr)->tblName_, name) == 0)
00083 {
00084
00085 chunk = (Chunk*) ((TABLE*)tptr)->chunkPtr_;
00086 return OK;
00087 }
00088 }
00089
00090 return ErrNotFound;
00091 }
00092
00093 List CatalogTableTABLE::getTableList()
00094 {
00095 List tableList;
00096 Chunk *chk = systemDatabase_->getSystemDatabaseChunk(TableTableId);
00097 ChunkIterator iter = chk->getIterator();
00098 void *tptr;
00099 while (NULL != (tptr = iter.nextElement()))
00100 {
00101 Identifier *elem = new Identifier();
00102 strcpy(elem->name, ((TABLE*)tptr)->tblName_);
00103 tableList.append(elem);
00104 }
00105 return tableList;
00106 }
00107
00108 DbRetVal CatalogTableFIELD::insert(FieldIterator &iter, int tblID, void *tptr)
00109 {
00110 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
00111 DbRetVal rv = OK;
00112 while (iter.hasElement())
00113 {
00114 void *fptr = fChunk->allocate(systemDatabase_, &rv);
00115 if (NULL == fptr)
00116 {
00117 printError(rv,
00118 "Could not allocate for FIELD catalog table");
00119 return rv;
00120 }
00121 FIELD *fldInfo = (FIELD*)fptr;
00122 FieldDef fDef = iter.nextElement();
00123 strcpy(fldInfo->fldName_, fDef.fldName_);
00124 fldInfo->tblID_ = tblID;
00125 fldInfo->tblPtr_ = tptr;
00126 fldInfo->type_ = fDef.type_;
00127 fldInfo->length_ = fDef.length_;
00128 fldInfo->offset_ = 0;
00129 os::memcpy(fldInfo->defaultValueBuf_, fDef.defaultValueBuf_,
00130 DEFAULT_VALUE_BUF_LENGTH);
00131 fldInfo->isNull_ = fDef.isNull_;
00132 fldInfo->isPrimary_ = fDef.isPrimary_;
00133 fldInfo->isUnique_ = fDef.isUnique_;
00134 fldInfo->isDefault_ = fDef.isDefault_;
00135 fldInfo->width_ = 0;
00136 fldInfo->scale_ = 0;
00137 printDebug(DM_SystemDatabase,"One Row inserted into FIELD %x %s",fldInfo, fDef.fldName_);
00138
00139 }
00140 return OK;
00141 }
00142
00143 DbRetVal CatalogTableFIELD::remove(void *tptr)
00144 {
00145 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
00146 ChunkIterator fIter = fChunk->getIterator();
00147 void *data = NULL;
00148 while ((data = fIter.nextElement())!= NULL)
00149 {
00150 if (((FIELD*)data)->tblPtr_ == tptr)
00151 {
00152
00153 fChunk->free(systemDatabase_, data);
00154 printDebug(DM_SystemDatabase,"One Row deleted from FIELD %x",data);
00155 }
00156 }
00157 return OK;
00158 }
00159
00160 void CatalogTableFIELD::getFieldInfo(void* tptr, FieldList &list)
00161 {
00162 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
00163 ChunkIterator fIter = fChunk->getIterator();;
00164 void *data = NULL;
00165 while (NULL != (data = fIter.nextElement()))
00166 {
00167 if (((FIELD*)data)->tblPtr_ == tptr)
00168 {
00169
00170 FIELD *fTuple = (FIELD*)data;
00171 FieldDef fldDef;
00172 strcpy(fldDef.fldName_, fTuple->fldName_);
00173 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
00174 fldDef.type_ = fTuple->type_;
00175 fldDef.length_ = fTuple->length_;
00176 fldDef.isDefault_ = fTuple->isDefault_;
00177 os::memcpy(fldDef.defaultValueBuf_, fTuple->defaultValueBuf_,
00178 DEFAULT_VALUE_BUF_LENGTH);
00179 fldDef.isNull_ = fTuple->isNull_;
00180 fldDef.isUnique_ = fTuple->isUnique_;
00181 fldDef.isPrimary_ = fTuple->isPrimary_;
00182 list.append(fldDef);
00183 }
00184 }
00185 return;
00186 }
00187
00188 DbRetVal CatalogTableFIELD::getFieldPtrs(FieldNameList &fldList,void *tptr, char **&fptr)
00189 {
00190 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(FieldTableId);
00191 int i=0;
00192 char *fName = NULL;
00193 bool found = false;
00194 fldList.resetIter();
00195 void *data = NULL;
00196 DbRetVal rv =OK;
00197 while (NULL != (fName = fldList.nextFieldName()))
00198 {
00199 ChunkIterator fIter = fChunk->getIterator();
00200 found = false;
00201 while (NULL != (data = fIter.nextElement()))
00202 {
00203 if (((FIELD*)data)->tblPtr_ == tptr)
00204 {
00205 if(0 == strcmp((char*)((FIELD*)data)->fldName_, fName))
00206 {
00207 found = true;
00208
00209 fptr[i++] = (char*) data;
00210 break;
00211 }
00212 }
00213 }
00214 if (!found)
00215 {
00216 printError(ErrNotFound,
00217 "No entries found in FIELD catalog table for the table specified");
00218 return ErrNotFound;
00219 }
00220 }
00221 return rv;
00222 }
00223
00224 DbRetVal CatalogTableINDEX::insert(const char *name, void *tptr, int numFlds, bool isUnique,
00225 void* chunk, int bucketSize, void *hChunk, void *&tupleptr)
00226 {
00227 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00228 ChunkIterator iter = tChunk->getIterator();
00229
00230
00231
00232 void *data = NULL;
00233 while ((data = iter.nextElement())!= NULL)
00234 {
00235 if (0 == strcmp(((INDEX*)data)->indName_, name))
00236 {
00237 printError(ErrAlready, "Index with name \'%s\' already exists "
00238 "on the table \'%s\'.", name, ((TABLE *)tptr)->tblName_);
00239 return ErrAlready;
00240 }
00241
00242 }
00243
00244 DbRetVal rv =OK;
00245 tupleptr = tChunk->allocate(systemDatabase_, &rv);
00246 if (NULL == tupleptr)
00247 {
00248 printError(rv,
00249 "Could not allocate for INDEX catalog table");
00250 return rv;
00251 }
00252 INDEX *indexInfo = (INDEX*)tupleptr;
00253 strcpy(indexInfo->indName_, name);
00254 indexInfo->tblID_ = -1;
00255 indexInfo->tblPtr_ = tptr;
00256 indexInfo->numFlds_ = numFlds;
00257 indexInfo->indexType_ = hashIndex;
00258 indexInfo->chunkPtr_ = chunk;
00259 indexInfo->hashNodeChunk_ = hChunk;
00260 indexInfo->noOfBuckets_ = bucketSize;
00261 indexInfo->isUnique_ = isUnique;
00262 printDebug(DM_SystemDatabase,"One Row inserted into INDEX %x %s",tupleptr, name);
00263 return OK;
00264 }
00265
00266 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
00267 {
00268 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00269 ChunkIterator iter = fChunk->getIterator();
00270
00271 void *data = NULL;
00272 while ((data = iter.nextElement())!= NULL)
00273 {
00274 if (0 == strcmp(((INDEX*)data)->indName_, name))
00275 {
00276
00277
00278 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
00279 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
00280 iptr = (void*) data;
00281 break;
00282 }
00283 }
00284 if (NULL != iptr)
00285 {
00286 fChunk->free(systemDatabase_, iptr);
00287 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
00288 }
00289 else
00290 {
00291 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
00292 return ErrNotExists;
00293 }
00294 return OK;
00295 }
00296 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
00297 {
00298 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00299 ChunkIterator iter = fChunk->getIterator();
00300
00301 void *data = NULL;
00302 while ((data = iter.nextElement())!= NULL)
00303 {
00304 if (0 == strcmp(((INDEX*)data)->indName_, name))
00305 {
00306
00307
00308 chunk = (Chunk*) ((INDEX*)data)->chunkPtr_;
00309 hchunk = (Chunk*) ((INDEX*)data)->hashNodeChunk_;
00310 iptr = (void*) data;
00311 break;
00312 }
00313 }
00314 if (NULL == iptr)
00315 {
00316 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
00317 return ErrNotExists;
00318 }
00319 return OK;
00320 }
00321
00322 int CatalogTableINDEX::getNumIndexes(void *tptr)
00323 {
00324 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00325 ChunkIterator iter = fChunk->getIterator();
00326 void *iptr = NULL;
00327 int numIndex =0;
00328 while (NULL != (iptr = iter.nextElement()))
00329 {
00330 if (((INDEX*)iptr)->tblPtr_ == tptr) numIndex++;
00331 }
00332 return numIndex;
00333 }
00334
00335 char* CatalogTableINDEX::getIndexName(void *tptr, int position)
00336 {
00337 if (position == 0) return NULL;
00338 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00339 ChunkIterator iter = fChunk->getIterator();
00340 void *iptr = NULL;
00341 int numIndex =0;
00342 int curPos =0;
00343 while (NULL != (iptr = iter.nextElement()))
00344 {
00345 if (((INDEX*)iptr)->tblPtr_ == tptr) curPos++;
00346 if ( curPos == position ) return ((INDEX*)iptr)->indName_;
00347 }
00348 return NULL;
00349
00350 }
00351
00352 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
00353 {
00354 void *iptr = NULL;
00355 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
00356 ChunkIterator iter = fChunk->getIterator();
00357 int i=0;
00358 while (NULL != (iptr = iter.nextElement()))
00359 {
00360 if (((INDEX*)iptr)->tblPtr_ == tptr)
00361 {
00362 array[i++] = (char*) iptr;
00363 }
00364 }
00365 return;
00366 }
00367
00368 ChunkIterator CatalogTableINDEX::getIterator(void *iptr)
00369 {
00370 INDEX *index = (INDEX*)iptr;
00371 return ((Chunk*)index->chunkPtr_)->getIterator();
00372 }
00373
00374
00375 int CatalogTableINDEX::getNoOfBuckets(void *iptr)
00376 {
00377 INDEX *index = (INDEX*)iptr;
00378 return index->noOfBuckets_;
00379 }
00380
00381 int CatalogTableINDEX::getUnique(void *iptr)
00382 {
00383 INDEX *index = (INDEX*)iptr;
00384 return index->isUnique_;
00385 }
00386 char* CatalogTableINDEX::getName(void *iptr)
00387 {
00388 INDEX *index = (INDEX*)iptr;
00389 return index->indName_;
00390 }
00391
00392 DbRetVal CatalogTableINDEXFIELD::insert(FieldNameList &fldList, void *indexPtr,
00393 void *tblPtr, char **&fptr)
00394 {
00395
00396 Chunk *fChunk;
00397 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
00398 fldList.resetIter();
00399 int i =0;
00400 char *fName =NULL;
00401 void *data = NULL;
00402 ChunkIterator ifIter = fChunk->getIterator();
00403 while (NULL != (fName = fldList.nextFieldName()))
00404 {
00405 ifIter = fChunk->getIterator();
00406 while ((data = ifIter.nextElement()) != NULL) {
00407 if (0 == strcmp(((FIELD *)((INDEXFIELD *) data)->fieldPtr)->fldName_, fName) && ((INDEXFIELD *)data)->tablePtr == tblPtr) {
00408 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_);
00409 return ErrAlready;
00410 }
00411 }
00412 DbRetVal rv = OK;
00413 void *fieldptr = fChunk->allocate(systemDatabase_, &rv);
00414 if (NULL == fieldptr)
00415 {
00416 printError(rv,
00417 "Could not allocate for USER catalog table");
00418 return rv;
00419 }
00420 INDEXFIELD *fldInfo = (INDEXFIELD*)fieldptr;
00421 fldInfo->tablePtr = tblPtr;
00422 fldInfo->fieldPtr = (FIELD*)fptr[i++];
00423 fldInfo->indexPtr = indexPtr;
00424 printDebug(DM_SystemDatabase,"One Row inserted into INDEXFIELD %x", fldInfo);
00425 }
00426 return OK;
00427 }
00428
00429 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
00430 {
00431 Chunk *fChunk;
00432 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
00433 ChunkIterator fIter = fChunk->getIterator();
00434 void *data = NULL;
00435 while ((data = fIter.nextElement())!= NULL)
00436 {
00437 if (((INDEXFIELD*)data)->indexPtr == iptr)
00438 {
00439
00440 fChunk->free(systemDatabase_, data);
00441 printDebug(DM_SystemDatabase,"One Row deleted from INDEXFIELD %x", data);
00442 }
00443 }
00444 return OK;
00445 }
00446
00447 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
00448 char *&name, DataType &type)
00449 {
00450 Chunk *ifChunk;
00451 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
00452 ChunkIterator ifIter = ifChunk->getIterator();
00453 void *data = NULL;
00454 while ((data = ifIter.nextElement())!= NULL)
00455 {
00456 if (((INDEXFIELD*)data)->indexPtr == index)
00457 {
00458
00459 name = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->fldName_;
00460 type = ((FIELD*)(((INDEXFIELD*)data)->fieldPtr))->type_;
00461 return OK;
00462 }
00463 }
00464 printError(ErrNotExists,"Index %x not exists in catalog table", index);
00465 return ErrNotExists;
00466 }
00467
00468 DbRetVal CatalogTableUSER::insert(const char *name, const char *pass)
00469 {
00470 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
00471 DbRetVal rv = OK;
00472 USER *usrInfo = (USER*)tChunk->allocate(systemDatabase_, &rv);
00473 if (NULL == usrInfo)
00474 {
00475 printError(rv,
00476 "Could not allocate for USER catalog table");
00477 return rv;
00478 }
00479 strcpy(usrInfo->userName_, name);
00480 strcpy(usrInfo->password_, os::encrypt(pass, "A0"));
00481 return OK;
00482
00483 }
00484
00485 DbRetVal CatalogTableUSER::authenticate(const char *name, const char *pass,
00486 bool &isAuthenticated, bool &isDba)
00487 {
00488 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
00489 ChunkIterator iter = tChunk->getIterator();
00490 void *data = NULL;
00491 while (NULL != (data = iter.nextElement()))
00492 {
00493 if (strcmp(((USER*)data)->userName_, name) == 0)
00494 {
00495
00496 char * enpass = os::encrypt(pass,"A0");
00497 if (0 == strcmp(enpass, ((USER*)data)->password_))
00498 {
00499 isAuthenticated = true;
00500 if (0 == strcmp(((USER*)data)->userName_, DBAUSER))
00501 isDba = true; else isDba = false;
00502 return OK;
00503 }
00504 }
00505 }
00506 isAuthenticated = false;
00507 return OK;
00508 }
00509
00510 DbRetVal CatalogTableUSER::remove(const char *name)
00511 {
00512 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
00513 ChunkIterator iter = tChunk->getIterator();
00514 void *data = NULL;
00515 while ((data = iter.nextElement())!= NULL)
00516 {
00517 if (strcmp(((USER*)data)->userName_, name) == 0)
00518 {
00519
00520 tChunk->free(systemDatabase_, data);
00521 return OK;
00522 }
00523 }
00524 printError(ErrNotExists,"User %s not exists in catalog table", name);
00525 return ErrNotExists;
00526 }
00527
00528 DbRetVal CatalogTableUSER::changePass(const char *name, const char *pass)
00529 {
00530 Chunk *tChunk = systemDatabase_->getSystemDatabaseChunk(UserTableId);
00531 ChunkIterator iter = tChunk->getIterator();
00532 void *data = NULL;
00533 while (NULL != (data = iter.nextElement()))
00534 {
00535 if (strcmp(((USER*)data)->userName_, name) == 0)
00536 {
00537
00538 strcpy(((USER*)data)->password_, os::encrypt(pass, "A0"));
00539 return OK;
00540 }
00541 }
00542 printError(ErrNotExists,"User %s not exists in catalog table", name);
00543 return ErrNotExists;
00544 }