void DataManager::insert(string &relationName, vector<string> &values) {
	Relation* relation = getRelationByName(relationName);

	if (relation != NULL) {
		relation->addTuple(values);
	}
}
void RDBMSInterpreter::readFacts(Relation& relation)
{
  for (vector<Fact*>::iterator it = m_facts.begin(); it != m_facts.end(); it++)
    {
      Fact* fact = *it;
      
      if (fact->id().compare(relation.name()) == 0)
        {
          relation.addTuple(fact);
        }
    }
}
Example #3
0
Relation Relation::naturalJoin(Relation relation1, Relation relation2){
    Relation newRelation;
    vector<pair<int, int>> pairIndexes;
    //Determine which columns relations have in common
    for (int i=0; i<relation1.schema.size(); i++) {
        for (int j=0; j<relation2.schema.size(); j++) {
            if(relation1.schema[i].getTokenType() == ID && relation2.schema[j].getTokenType() == ID &&
               !relation1.schema[i].getTokensValue().compare(relation2.schema[j].getTokensValue())){
                pairIndexes.push_back(make_pair(i, j));
            }
        }
    }
    
    for (int i=0; i<relation1.rows.size(); i++) {
        for (int j=0; j<relation2.rows.size(); j++) {
            bool equal = true;
            for (int k=0; k<pairIndexes.size(); k++) {
                if (relation1.getRowAtIndex(i).elements[pairIndexes[k].first].getTokensValue().compare(relation2.getRowAtIndex(j).elements[pairIndexes[k].second].getTokensValue())) {
                    equal = false;
                }
            }
            if (equal) {
                vector<Token> newVector = relation1.getRowAtIndex(i).elements;
                vector<Token> vectorToProject = relation2.getRowAtIndex(j).elements;
                
                //Eliminate Intersected Columns
                for (int k=pairIndexes.size() - 1.0; k>=0; k--) {
                    vectorToProject.erase(vectorToProject.begin() + pairIndexes[k].second);
                }
                
                newVector.insert(newVector.end(), vectorToProject.begin() ,vectorToProject.end());
                Tuple newTuple(newVector);
                newRelation.addTuple(newTuple);
            }
        }
    }
    
    vector<Token> newSchema = relation1.schema;
    vector<Token> schemaToEdit = relation2.schema;
    
    //Eliminate intercept columns in the schema
    for (int i=pairIndexes.size()-1.0; i>=0; i--) {
        schemaToEdit.erase(schemaToEdit.begin() + pairIndexes[i].second);
    }
    newSchema.insert(newSchema.end(), schemaToEdit.begin(), schemaToEdit.end());
    newRelation.schema = newSchema;
    
    return newRelation;
}
Example #4
0
Relation Relation::crossProduct(Relation relation1, Relation relation2){
    Relation newRelation;
    for (int i=0; i<relation1.rows.size(); i++) {
        for (int j=0; j<relation2.rows.size(); j++) {
            vector<Token> newVector = relation1.getRowAtIndex(i).elements;
            newVector.insert(newVector.end(), relation2.getRowAtIndex(i).elements.begin() ,relation2.getRowAtIndex(j).elements.end());
            Tuple newTuple(newVector);
            newRelation.addTuple(newTuple);
        }
    }
    
    vector<Token> newSchema = relation1.schema;
    newSchema.insert(newSchema.end(), relation2.schema.begin(), relation2.schema.end());
    
    newRelation.schema = newSchema;
    
    return newRelation;
}