int main() { DBMSAPI dbms; //Declare the dbms api initRelations(&dbms); cout << displayMainMenu() << endl; int searchResults = 0; while (1) { //input loop string in; getline(cin, in); vector<string> tokens = splitAtSpace(in); //split the command given by spaces if (tokens[0] == "HELP") { //process HELP cout << displayMainMenu() << endl; } else if (tokens[0] == "EXIT") { //process EXIT saveRelations(&dbms); exit(0); } else if (tokens[0] == "ADD") { if (tokens[1] == "CAR") { //process ADD CAR vector<string> values; for (int i = 2; i < tokens.size(); i++) { values.push_back(tokens[i]); } insertValue("cars", values, &dbms); cout << "Addition approved!" << endl << endl; } else if (tokens[1] == "DEALERSHIP") {//process ADD DEALERSHIP vector<string> values; for (int i = 2; i < tokens.size(); i++) { values.push_back(tokens[i]); } insertValue("dealerships", values, &dbms); cout << "Addition approved!" << endl << endl; } } else if (tokens[0] == "LIST") { //process LIST if (tokens.size() != 5) { cout << "Invalid number of arguments!" << endl << endl; } else { //Price INTEGER, Date_Listed VARCHAR(20), VIN VARCHAR(17), Dealership_Name VARCHAR(30) string listing = "INSERT INTO listings VALUES FROM (" + tokens[1] + ", " + tokens[2] + ", " + tokens[3] + ", " + tokens[4] + ");"; dbms.execute(listing); cout << "Listing approved!" << endl << endl; } } else if (tokens[0] == "UNLIST") { //process UNLIST if (tokens.size() != 2) { cout << "Invalid number of arguments!" << endl << endl; } else { string removeListing = "DELETE FROM listings WHERE (VIN == \"" + tokens[1] + "\");"; dbms.execute(removeListing); } } else if (tokens[0] == "SELL") { //process SELL if (tokens.size() != 6) { cout << "Invalid number of arguments!" << endl << endl; } else { //Price INTEGER, Date_Sold VARCHAR(20), VIN VARCHAR(17), Dealership_Name VARCHAR(30), Buyer_Name VARCHAR(50) string sale = "INSERT INTO sales VALUES FROM (" + tokens[1] + ", " + tokens[2] + ", " + tokens[3] + ", " + tokens[4] + ", " + tokens[5] + ");"; dbms.execute(sale); string removeListing = "DELETE FROM listings WHERE (VIN == \"" + tokens[3] + "\");"; dbms.execute(removeListing); string removeInventory = "DELETE FROM cars WHERE (VIN == \"" + tokens[3] + "\");"; dbms.execute(removeInventory); cout << "Sale approved!" << endl << endl; } } else if (tokens[0] == "INVENTORY") { //process INVENTORY showRelation("cars", &dbms); } else if (tokens[0] == "DEALERSHIPS") { //process DEALERSHIPS showRelation("dealerships", &dbms); } else if (tokens[0] == "LISTINGS") { //process LISTINGS showRelation("listings", &dbms); } else if (tokens[0] == "SALES") { //process SALES showRelation("sales", &dbms); } else if (tokens[0] == "SEARCH") { //process SEARCH searchResults++; stringstream ss; ss << searchResults; string query = "searchresult" + ss.str() + " <- select ("; for (int i = 2; i < tokens.size(); i++) { query += tokens[i]; if (i + 1 < tokens.size()) { query += " "; } } query += ") " + tokens[1] + ";"; dbms.execute(query); string showResult = "SHOW searchresult" + ss.str() + ";"; dbms.execute(showResult); cout << endl; } else if (tokens[0] == "UPDATE") { if (tokens[1] == "LISTING") { //process UPDATE LISTING if (tokens.size() != 4) { cout << "Invalid number of arguments!" << endl; } else { string updateListings = "UPDATE listings SET Price = " + tokens[3] + " WHERE VIN == " + tokens[2] + ";"; dbms.execute(updateListings); } } } else if (tokens[0] == "CROSS") { //process CROSS string cross = "crossResult <- dealerships * cars;"; dbms.execute(cross); string showCross = "SHOW crossResult;"; dbms.execute(showCross); string closeCross = "CLOSE crossResult;"; dbms.execute(closeCross); } else if (tokens[0] == "PROJECT") { //process PROJECT string project = "projectResult <- project ("; for (int i = 2; i < tokens.size(); i++) { project += tokens[i]; if (i + 1 < tokens.size()) { project += ", "; } } project += ") " + tokens[1] + ";"; dbms.execute(project); string showProject = "SHOW projectResult;"; dbms.execute(showProject); string closeProject = "CLOSE projectResult;"; dbms.execute(closeProject); } else if (tokens[0] == "UNION") { if (tokens[1] == "SEARCHES") { //process UNION SEARCHES string unionQuery = "unionResult <- " + tokens[2] + " + " + tokens[3] + ";"; dbms.execute(unionQuery); string showUnion = "SHOW unionResult;"; dbms.execute(showUnion); string closeUnion = "CLOSE unionResult;"; dbms.execute(closeUnion); } } else if (tokens[0] == "DIFFERENCE") { if (tokens[1] == "SEARCHES") { //process DIFFERENCE SEARCHES string diffQuery = "diffResult <- " + tokens[2] + " - " + tokens[3] + ";"; dbms.execute(diffQuery); string showDiff = "SHOW diffResult;"; dbms.execute(showDiff); string closeDiff = "CLOSE diffResult;"; dbms.execute(closeDiff); } } else { //process unknown command cout << "Unknown command!" << endl << endl; } } }
int nestloop_cp_new(struct dbSysHead *head, relation *temp_data_dict, int old_relation_1, int old_relation_2 ){ //find empty temp_datadic int new_relation=0; int temp_num=0; for (temp_num = 0; temp_num < MAX_FILE_NUM; temp_num++) { if (strcmp(temp_data_dict[temp_num].getRelationName(),"") == 0) { new_relation = temp_num; // cout<<"new relation id: "<<new_relation<<endl; temp_data_dict[new_relation].init("temp_datadict_nestloop","WenHui"); merge_relation(head,temp_data_dict[old_relation_1],temp_data_dict[old_relation_2],& temp_data_dict[new_relation]); showRelation(&temp_data_dict[new_relation]); break; } } if (temp_num == MAX_FILE_NUM) { cout<<"no temp datadict for use!"<<endl; return -3; } //input error control int original_rec_length1 = temp_data_dict[old_relation_1].getRecordLength(); int original_rec_length2 =temp_data_dict[old_relation_2].getRecordLength(); int new_rec_length=temp_data_dict[new_relation].getRecordLength(); int original_attribute_length1=temp_data_dict[old_relation_1].getAttributeNum(); int original_attribute_length2=temp_data_dict[old_relation_2].getAttributeNum(); //look up which buffer is empty int buffer_id_; int i; for (i = 0; i < BUFF_NUM; i++) { if (head->buff[i].emptyOrnot == true) { buffer_id_ = i; head->buff[i].emptyOrnot = false; // ready for writein std::cout << "BufferID: " << i << std::endl; break; } } if (i == BUFF_NUM) { cout << "No Buffer Can be Used!" << endl; return -2; }else{ //RecordCursor scanTable(head, temp_datadic->fileID, original_rec_length, -temp_datadic->fileID); RecordCursorTmp scanTable1(head, temp_data_dict[old_relation_1].fileID, original_rec_length1, -temp_data_dict[old_relation_1].fileID, temp_data_dict[old_relation_1].getRecordNum()); cout << 1 << endl; char * one_Row_1 = (char *)malloc(sizeof(char) * original_rec_length1); char * one_Row_2 = (char *)malloc(sizeof(char) * original_rec_length2); char * new_Row_ = (char *)malloc(sizeof(char) * new_rec_length); Buffer t(head, -2); //to avoid positive number, no meaning int k = 0; while (true == scanTable1.getNextRecord(one_Row_1)) { //only scan RecordCursorTmp scanTable2(head, temp_data_dict[old_relation_2].fileID, original_rec_length2, -temp_data_dict[old_relation_2].fileID, temp_data_dict[old_relation_2].getRecordNum()); while(true == scanTable2.getNextRecord(one_Row_2)){ for(int i=0;i<original_rec_length1;i++) new_Row_[i]=one_Row_1[i]; //cout<<endl; for(int i=0;i<original_rec_length2;i++) new_Row_[i+original_rec_length1]=one_Row_2[i]; getOneRecord(new_Row_, &temp_data_dict[new_relation]); if (false == t.AppendBuffer(new_Row_, new_rec_length)) { t.writeBufferPage(t.filehead, buffer_id_, t.data_, t.current_size_); t.pageID++; if (t.pageID == SIZE_BUFF) { std::cout << "this buffer full" << std::endl; break; } memset(t.data_, 0, SIZE_PER_PAGE); memcpy(t.data_, new_Row_, new_rec_length); t.pointer_ = t.data_ + new_rec_length; t.current_size_ = new_rec_length; } } } //write remainder t.writeBufferPage(t.filehead, buffer_id_, t.data_, t.current_size_); temp_data_dict[new_relation].fileID = -buffer_id_; return new_relation; free(one_Row_1); free(one_Row_2); free(new_Row_); } }