// // Remove the tuple corresponding to relName from relcat. It performs the following steps: // // start a filter scan on relcat to locate the rid of the desired tuple // call deleteRecord() to remove the tuple // // Returns: // OK on success // error code otherwise // const Status RelCatalog::removeInfo(const string & relation) { Status status; RID rid; HeapFileScan* hfs; if (relation.empty()) return BADCATPARM; //create a new file scan hfs=new HeapFileScan(RELCATNAME, status); if (status!=OK) return status; const char * relation_name_ptr = relation.c_str(); // open startScan, use the predicate to find matching record status=hfs->startScan(0, MAXNAME, STRING, relation_name_ptr, EQ); if (status!=OK) return status; Record rec; // scan through relation catalog // assume there is no duplicate while ((status = hfs->scanNext(rid)) != FILEEOF){ // get record status = hfs->getRecord(rec); if (status!=OK) return status; //delete current record hfs->deleteRecord(); delete hfs; return OK; } return RELNOTFOUND; }
const Status RelCatalog::removeInfo(const string & relation) { Status status = OK; RID rid; HeapFileScan* hfs; if (relation.empty()) return BADCATPARM; while (status == OK) { hfs = new HeapFileScan(RELCATNAME, status); // you have to start a filter scan on relcat to locate the rid of the desired tuple. status = hfs->startScan(0, relation.length() + 1, STRING, relation.c_str(), EQ); status = hfs->scanNext(rid); // delete the record status = hfs->deleteRecord(); break; } if (status == NORECORDS) { return OK; } else if (status == FILEEOF) { return RELNOTFOUND; } else { return status; } }
const Status RelCatalog::removeInfo(const string & relation) { Status status; RID rid; HeapFileScan* hfs; if (relation.empty()) return BADCATPARM; hfs = new HeapFileScan(RELCATNAME,status); if (status != OK) return status; hfs->startScan(0,relation.length(),STRING,relation.c_str(),EQ); if (status != OK) return status; status=hfs->scanNext(rid); if (status != OK) return status; status = hfs->deleteRecord(); if (status != OK) return status; hfs->endScan(); delete hfs; return OK; }
const Status AttrCatalog::removeInfo(const string & relation, const string & attrName) { Status status; Record rec; RID rid; AttrDesc record; HeapFileScan* hfs; if (relation.empty() || attrName.empty()) return BADCATPARM; hfs = new HeapFileScan(ATTRCATNAME,status); if (status != OK) return status; hfs->startScan(0,sizeof(record.relName),STRING,relation.c_str(),EQ); if (status != OK) return status; while ((status=hfs->scanNext(rid)) != FILEEOF) { if (status != OK) return status; status=hfs->getRecord(rec); if (status != OK) return status; if (strncmp((char*)rec.data + sizeof(record.relName),attrName.c_str(),sizeof(record.attrName))==0) { status = hfs->deleteRecord(); if (status != OK) return status; break; } } hfs->endScan(); delete hfs; return OK; }
const Status QU_Delete(const string & relation, const string & attrName, const Operator op, const Datatype type, const char *attrValue) { Status status; HeapFileScan *hfs; AttrDesc record; RID rid; int tempInt; float tempFloat; hfs = new HeapFileScan(relation, status); //Get the AttrDesc from the attrCat table if (attrValue != NULL){ status = attrCat->getInfo(relation, attrName, record); if (status != OK) return status; } //check type and cast accordingly if(type == INTEGER){ tempInt = atoi(attrValue); attrValue = (char *) &tempInt; } else if(type == FLOAT){ tempFloat = atof(attrValue); attrValue = (char *) &tempFloat; } //Start HFS on the relation table status = hfs->startScan(record.attrOffset, record.attrLen, type, attrValue, op); if(status != OK) return status; while((status = hfs->scanNext(rid)) != FILEEOF){ if(status != OK) return status; //Delete record if found in relation table status = hfs->deleteRecord(); if(status != OK) return status; } //If end of file then return attribute not found if (status == FILEEOF){ status = OK; } delete hfs; return status; }
const Status QU_Delete(const string & relation, const string & attrName, const Operator op, const Datatype type, const char *attrValue) { Status status; Status ScanStatus = OK; RID rid; AttrDesc *ad; int attrCnt; HeapFileScan *hfs = new HeapFileScan(relation,status); if(status != OK) {delete(hfs); return status;} status = attrCat->getRelInfo(relation, attrCnt, ad); if(status != OK) {delete(hfs); return status;} // Find out the attribute's size int attrSize; int attrOffset; for(int i = 0; i < attrCnt; i++) if(strcmp(ad[i].attrName,attrName.c_str()) == 0) { attrSize = ad[i].attrLen; attrOffset = ad[i].attrOffset; } // Start a scan (* DO WE NEED TO CAST ATTRVALUE??) status = hfs->startScan(attrOffset,attrSize,type,attrValue,op); if(status != OK) {delete(hfs); return status;} // Find and delete all matching records ScanStatus = hfs->scanNext(rid); while(ScanStatus != FILEEOF){ status = hfs->deleteRecord(); if(status != OK) {delete(hfs); return status;} ScanStatus = hfs->scanNext(rid); if(ScanStatus == FILEEOF) {delete(hfs); return OK; } } // Done delete(hfs); return ScanStatus; }
const Status AttrCatalog::removeInfo(const string & relation, const string & attrName) { Status status = OK; Record rec; RID rid; AttrDesc record; HeapFileScan* hfs; if (relation.empty() || attrName.empty()) return BADCATPARM; while (status == OK) { hfs = new HeapFileScan(ATTRCATNAME, status); // you have to start a filter scan on relcat to locate the rid of the desired tuple. status = hfs->startScan(0, relation.length() + 1, STRING, relation.c_str(), EQ); while (hfs->scanNext(rid) != FILEEOF) { status = hfs->getRecord(rec); memcpy(&record, rec.data, rec.length); if (strcmp(record.attrName, attrName.c_str()) == 0) { // Then you can call deleteRecord() to remove it status = hfs->deleteRecord(); // break instead? return status; } } break; } if (status == FILEEOF) { return status; } else if (status == NORECORDS) { return OK; } else { return status; } }
// // Removes the tuple from attrcat that corresponds to attribute attrName of relation. // // Returns: // OK on success // error code otherwise // const Status AttrCatalog::removeInfo(const string & relation, const string & attrName) { Status status; Record rec; RID rid; HeapFileScan* hfs; if (relation.empty() || attrName.empty()) return BADCATPARM; hfs = new HeapFileScan(ATTRCATNAME,status); if(status != OK) return status; const char* rel_name_ptr = relation.c_str(); const char* attr_name_ptr = attrName.c_str(); status = hfs->startScan(0,MAXNAME,STRING,rel_name_ptr,EQ); if(status != OK){ delete hfs; return status; } //first search all the attribute correspond to the relation while((status = hfs->scanNext(rid)) != FILEEOF){ status = hfs->getRecord(rec); if(status != OK){ delete hfs; return status; } //find the attribute if(memcmp(attr_name_ptr,((AttrDesc *)rec.data)->attrName,attrName.length() ) == 0){ //attribute found and detele hfs->deleteRecord(); delete hfs; //decrease the relation attrCnt return OK; } } delete hfs; return ATTRNOTFOUND; }