208 lines
No EOL
4.1 KiB
C++
Executable file
208 lines
No EOL
4.1 KiB
C++
Executable file
#include <iostream>
|
|
#include <vector>
|
|
#include "Relation.h"
|
|
|
|
Relation::Relation(string n){
|
|
name = n;
|
|
size = 0;
|
|
}
|
|
|
|
Relation::Relation(string n, vector<Attribute> a){
|
|
name = n;
|
|
att = a;
|
|
size = a.size();
|
|
}
|
|
|
|
void Relation::insertAttributes(vector<Attribute> a){
|
|
for (int i = 0; i < a.size(); ++i){
|
|
att.push_back(a[i]);
|
|
}
|
|
}
|
|
|
|
string Relation::getTableName(){
|
|
return name;
|
|
}
|
|
|
|
void Relation::setTableName(string s){
|
|
name = s;
|
|
}
|
|
|
|
Attribute Relation::operator[](int i){
|
|
return att[i];
|
|
}
|
|
|
|
vector<string> Relation::getTuple(int index){
|
|
vector<string> temp;
|
|
|
|
for (int i = 0; i < att.size(); ++i){
|
|
temp.push_back(att[i][index]);
|
|
}
|
|
|
|
return temp;
|
|
}
|
|
|
|
vector<Attribute> Relation::getAttributes(){
|
|
return att;
|
|
}
|
|
|
|
void Relation::setAttributes(vector<Attribute> v){
|
|
size = v.size();
|
|
att = v;
|
|
}
|
|
|
|
vector<string> Relation::getAttributeNames(){
|
|
vector<string> temp;
|
|
for(int i = 0; i < size; ++i){
|
|
temp.push_back(att[i].getName());
|
|
}
|
|
return temp;
|
|
}
|
|
|
|
Attribute& Relation::getAttributeByName(string s) {
|
|
for(int i = 0; i < size; ++i){
|
|
if (att[i].getName() == s) {
|
|
return att[i];
|
|
}
|
|
}
|
|
|
|
cout << "Failure to return: the requested attribute does not exist.";
|
|
}
|
|
|
|
bool Relation::isAttribute(string s) {
|
|
for(int i = 0; i < size; ++i){
|
|
if (att[i].getName() == s) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
//cout << "Failure to return: the requested attribute does not exist.";
|
|
}
|
|
|
|
void Relation::renameAttribute(string oldstr, string newstr){
|
|
this->getAttributeByName(oldstr).setName(newstr);
|
|
}
|
|
|
|
int Relation::getSize(){
|
|
return size;
|
|
}
|
|
|
|
void Relation::display(){
|
|
cout << "--------------------------\n";
|
|
cout << getTableName() << "\n";
|
|
for (int i = 0; i < att.size(); ++i){
|
|
att[i].display();
|
|
}
|
|
|
|
cout << "--------------------------\n";
|
|
}
|
|
|
|
void Relation::insertTuple(vector<string> tuple){
|
|
if (tuple.size() != this->size) {
|
|
cout << "Failure to insert: the sizes do not match.";
|
|
}
|
|
|
|
else {
|
|
for (int i = 0; i < att.size(); ++i) {
|
|
att[i].addCell(tuple[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Relation::insertFromRelation(Relation r){
|
|
if (r.size != this->size) {
|
|
cout << "Failure to insert: the sizes do not match.";
|
|
return;
|
|
}
|
|
|
|
else if (this->getAttributeNames() != r.getAttributeNames()) {
|
|
cout << "Failure to insert: the attributes do not match.";
|
|
return;
|
|
}
|
|
|
|
else {
|
|
vector<Attribute> newAtts = r.getAttributes();
|
|
|
|
for (int i = 0; i < newAtts.size(); ++i) {
|
|
for (int j = 0; j < newAtts[i].getSize(); ++j) {
|
|
this->att[i].addCell(newAtts[i][j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Relation::removeTuple(int index){
|
|
if (index >= this->size)
|
|
{
|
|
cout << "Failure to delete: the requested index is out of bounds." << endl;
|
|
}
|
|
|
|
else
|
|
{
|
|
for (int i = 0; i < att.size(); ++i)
|
|
{
|
|
att[i].removeCell(index);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Relation::removeFromTuple(int rindex, int aindex)
|
|
{
|
|
if (rindex >= this->size)
|
|
{
|
|
cout << "Failure to delete: the requested index is out of bounds." << endl;
|
|
}
|
|
|
|
else
|
|
{
|
|
att[rindex].removeCell(aindex);
|
|
}
|
|
}
|
|
|
|
void Relation::updateCmd(string attNameSet, string attSet, string attNameWhere, string attWhere){
|
|
vector<int> inds;
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < att.size(); ++i){
|
|
if (att[i].getName() == attNameWhere){
|
|
found = true;
|
|
for (int j = 0; j < att[i].getSize(); ++j){
|
|
if (att[i][j] == attWhere){
|
|
inds.push_back(j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!found){
|
|
cout << "Failure to update, the requested attribute was not found." << endl;
|
|
}
|
|
|
|
for (int i = 0; i < att.size(); ++i){
|
|
if (att[i].getName() == attNameSet){
|
|
for (int j = 0; j < inds.size(); ++j){
|
|
att[i].setValue(attSet, inds[j]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void Relation::deleteCmd(string attName, string s){
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < att.size(); ++i){
|
|
if (att[i].getName() == attName){
|
|
found = true;
|
|
for (int j = 0; j < att[i].getSize(); ++j){
|
|
if (att[i][j] == s){
|
|
removeTuple(j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!found){
|
|
cout << "Failure to delete, the requested attribute was not found." << endl;
|
|
}
|
|
} |