//delete the record matching the conditions in the table //@param tableName: name of table //@param conditionVector: vector of condition void API::recordDelete(string tableName, vector<Condition>* conditionVector) { if (!tableExist(tableName)) return; int num = 0; vector<Attribute> attributeVector; attributeGet(tableName, &attributeVector); int blockoffset = -1; if (conditionVector != NULL) { for (Condition condition : *conditionVector) { if (condition.operate == Condition::OPERATOR_EQUAL) { for (Attribute attribute : attributeVector) { if (attribute.index != ""&& attribute.name == condition.attributeName) { blockoffset = im->searchIndex(rm->indexFileNameGet(attribute.index), condition.value, attribute.type); } } } } } if (blockoffset == -1) { //if we cannot find the block by index ,we need to find all block num = rm->recordAllDelete(tableName, conditionVector); } else { //find the block by index, search in the block num = rm->recordBlockDelete(tableName, conditionVector, blockoffset); } //delete the number of record in the table cm->deleteValue(tableName, num); printf("delete %d record in table %s\n", num, tableName.c_str()); }
//show the record matching the conditions of attribute in the table and the number of the record //@param tableName: name of table //@param attributeNameVector: vector of name of attribute //@param conditionVector:vector of condition void API::recordShow(string tableName, vector<string>* attributeNameVector, vector<Condition>* conditionVector) { if (cm->findTable(tableName) == TABLE_FILE) { int num = 0; vector<Attribute> attributeVector; attributeGet(tableName, &attributeVector); vector<string> allAttributeName; if (attributeNameVector == NULL) { for (Attribute attribute : attributeVector) { allAttributeName.insert(allAttributeName.end(), attribute.name); } attributeNameVector = &allAttributeName; } //print attribuet name you want to show tableAttributePrint(attributeNameVector); for (string name : (*attributeNameVector)) { int i = 0; for (int i = 0; i < attributeVector.size(); i++) { if (attributeVector[i].name == name) { break; } } if (i == attributeVector.size()) { cout << "The attribute which you want to print is not exist in the table" << endl; return; } } int blockOffset = -1; if (conditionVector != NULL) { for (Condition condition : *conditionVector) { int i = 0; for (int i = 0; i < attributeVector.size(); i++) { if (attributeVector[i].name == condition.attributeName) { if (condition.operate == Condition::OPERATOR_EQUAL && attributeVector[i].index != "") { blockOffset = im->searchIndex(rm->indexFileNameGet(attributeVector[i].index), condition.value, attributeVector[i].type); } break; } } if (i == attributeVector.size()) { cout << "the attribute is not in the table" << endl; return; } } } if (blockOffset == -1) { //cout << "if we cannot find the block by index,we need to find all block" << endl; num = rm->recordAllShow(tableName, attributeNameVector, conditionVector); } else { //find the block by indx, search in the block num = rm->recordBlockShow(tableName, attributeNameVector, conditionVector,blockOffset); } printf("%d records selected\n", num); } else { cout << "There is no table " << tableName << endl; } }
//insert a record to a table //@param tableName:name of table //@param recordContent : Vector of these content of a record void API::recordInsert(string tableName, vector<string>* recordContent) { if (!tableExist(tableName)) return; string indexName; //deal if index is exist vector<Attribute> attributeVector; vector<Condition> conditionVector; attributeGet(tableName, &attributeVector); for (int i = 0; i < attributeVector.size(); i++) { indexName = attributeVector[i].indexNameGet();//get the index value if (indexName != "") { //if the attribute has a index int blockoffset = im->searchIndex(rm->indexFileNameGet(indexName), (*recordContent)[i], attributeVector[i].type); if (blockoffset != -1) { //if the value has exist the index tree then fail to insert the record cout << "insert fail because index value exist" << endl; return; } } else if (attributeVector[i].ifUnique) { //if the attribute if unique but not index Condition condition(attributeVector[i].name, (*recordContent)[i], Condition::OPERATOR_EQUAL); conditionVector.insert(conditionVector.end(), condition); } } if (conditionVector.size() > 0) { for (int i = 0; i < conditionVector.size(); i++) { vector<Condition> conditionTmp; conditionTmp.insert(conditionTmp.begin(), conditionVector[i]); //寻找冲突的元组数目 int recordConflictNum = rm->recordAllFind(tableName, &conditionTmp); if (recordConflictNum > 0) { cout << "insert fail because unique value exist" << endl; return; } } } char recordString[2000]; memset(recordString, 0, sizeof(recordString)); //CatalogManager to get the record string cm->recordStringGet(tableName, recordContent , recordString); //RecordManager to insert the record into file; and get the position of block being insert int recordSize = cm->calcuteLenth(tableName); int blockOffset = rm->recordInsert(tableName, recordString, recordSize); if (blockOffset >= 0) { recordIndexInsert(recordString, recordSize, &attributeVector, blockOffset); cm->insertRecord(tableName, 1); printf("insert record into table %s successfully\n", tableName.c_str()); } else { cout << "insert record into table " << tableName << "fail" << endl; } }
std::string html::Form::HiddenField::value() const { return attributeGet("value"); }