cleared out the bad code
This commit is contained in:
parent
e2776d2459
commit
c8ecb606fd
6 changed files with 73 additions and 205 deletions
30
Attribute.h
30
Attribute.h
|
@ -6,7 +6,7 @@ using namespace std;
|
|||
//Funtional, might need more functionality
|
||||
|
||||
//template<typename T>
|
||||
class Attribute {
|
||||
class Attribute{
|
||||
vector<string> values;
|
||||
string name;
|
||||
string type;
|
||||
|
@ -21,30 +21,28 @@ public:
|
|||
size = 0;
|
||||
}
|
||||
|
||||
void addRow(string v) {
|
||||
void addCell(string v){
|
||||
values.push_back(v);
|
||||
size++;
|
||||
}
|
||||
|
||||
string getElementAt(int pos)
|
||||
{
|
||||
return values[pos];
|
||||
}
|
||||
|
||||
vector<string> getValues() { return values; }
|
||||
string operator[](int i){ return values[i]; }
|
||||
vector<string> getValues(){ return values; }
|
||||
string getName(){ return name; }
|
||||
string getType(){ return type; }
|
||||
bool isKey(){ return key; }
|
||||
int getSize(){ return size; }
|
||||
int getSize(){ return size; } //may need to change primary key implementation
|
||||
|
||||
void display()
|
||||
{
|
||||
cout<<"Attribute name:\t"<< name <<"\n";
|
||||
cout<<"Elements: ";
|
||||
void display(){
|
||||
cout << "-------------\n";
|
||||
cout << name << "\n" << type << "\n\n";
|
||||
|
||||
for (int i = 0; i < values.size(); ++i)
|
||||
{
|
||||
cout<<values[i]<<" ";
|
||||
vector<string>::iterator it = values.begin();
|
||||
while (it != values.end()){
|
||||
cout << *it << "\n";
|
||||
it++;
|
||||
}
|
||||
|
||||
cout << "-------------\n";
|
||||
}
|
||||
};
|
||||
|
|
45
DBEngine.h
45
DBEngine.h
|
@ -13,25 +13,37 @@ public:
|
|||
size = 0;
|
||||
}
|
||||
|
||||
void createTable(string n) {
|
||||
Relation r(n);
|
||||
tables.push_back(r);
|
||||
size++;
|
||||
}
|
||||
|
||||
void createTable(string n, vector<Attribute> a) {
|
||||
Relation r(n, a);
|
||||
tables.push_back(r);
|
||||
size++;
|
||||
}
|
||||
|
||||
void createTable(Relation r) { tables.push_back(r); }
|
||||
vector<Relation> getRelations() { return tables; }
|
||||
void showTable(Relation r) { r.display(); }
|
||||
void createTable(Relation r){
|
||||
tables.push_back(r);
|
||||
size++;
|
||||
}
|
||||
|
||||
Relation getTableFromName(string n) {
|
||||
vector<Relation> getRelations(){ return tables; }
|
||||
//void showTable(Relation r){}
|
||||
|
||||
Relation getTableFromName(string n){
|
||||
//will return first occurence
|
||||
for(int i = 0; i < tables.size(); i++) {
|
||||
if (tables[i].getTableName() == n) {
|
||||
for(int i = 0; i < tables.size(); i++){
|
||||
if (tables[i].getTableName() == n){
|
||||
return tables[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void saveToFile(vector<string> cmds) {
|
||||
void saveToFile(vector<string> cmds){
|
||||
//writes nothing meaningful
|
||||
ofstream file;
|
||||
file.open("savefile.db");
|
||||
|
||||
|
@ -41,23 +53,4 @@ public:
|
|||
|
||||
file.close();
|
||||
}
|
||||
|
||||
//void insertTuple(Relation r, vector<string> t) { r.addTuple(t); }
|
||||
|
||||
//need to add find by name
|
||||
/*void deleteTuple(Relation r, int n) {
|
||||
cout << "a";
|
||||
r.removeTuple(n); }*/
|
||||
|
||||
void selectTuples() {
|
||||
//
|
||||
}
|
||||
|
||||
void project(Relation r, string n) { r.projectQuery(n); }
|
||||
|
||||
void product() { /*Brandon*/ }
|
||||
|
||||
bool unionComp(Relation r1, Relation r2) {
|
||||
return ((r1.getSize() == r2.getSize()) && (r1.getDomains() == r2.getDomains()));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -53,12 +53,14 @@ With an arbitrary vector of strings as cmds (until we are able to parse effectiv
|
|||
engine.saveToFile(cmds);
|
||||
|
||||
|
||||
This will result in a text file with the contents:
|
||||
This will result in a db file with the contents:
|
||||
|
||||
CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);
|
||||
|
||||
SHOW animals;
|
||||
|
||||
|
||||
|
||||
//insert
|
||||
|
||||
//delete
|
||||
|
|
91
Relation.h
91
Relation.h
|
@ -9,76 +9,21 @@ class Relation {
|
|||
int size;
|
||||
|
||||
public:
|
||||
Relation(string n) {
|
||||
name = n;
|
||||
size = 0;
|
||||
}
|
||||
Relation(string n, vector<Attribute> a) {
|
||||
name = n;
|
||||
att = a;
|
||||
size = a.size();
|
||||
}
|
||||
|
||||
int getSize() { return size; }
|
||||
//addAttribute
|
||||
|
||||
/*void addTuple(vector<string> tuple) {
|
||||
//Loop through the attribute columns
|
||||
for(int i = 0; i < att.size(); i++) {
|
||||
|
||||
//Loop through the elements in the i'th column
|
||||
for(int j = 0; j < att[i].getValues().size(); j++){
|
||||
|
||||
//In this column, at this element's spot, assign an element from the tuple vector to this spot
|
||||
att[i].addRow(tuple[i]);
|
||||
size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void removeTuple(int tupleNum) {
|
||||
if (tupleNum > att[0].getSize() || tupleNum < 0)
|
||||
{
|
||||
cout<<"ERROR! index out of bound"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout << "a" + att.size();
|
||||
for(int i = 0; i < att.size(); ++i) //for all the attributes
|
||||
{
|
||||
cout << "SSSSSSSSSSSSSSSSSSSSSSSSSS" + tupleNum;
|
||||
att[i].getValues().erase(att[i].getValues().begin()+ tupleNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> findTuple(string attributeType, string type)
|
||||
{
|
||||
vector<int> tupleSlot; // tuples that have the attribute in question
|
||||
for (int i = 0; i < att.size(); ++i)// find attribute in question
|
||||
{
|
||||
if(att[i].getName() == attributeType)
|
||||
{
|
||||
for (int j = 0; j < att[i].getSize(); ++j)//search through all the values of the attribute column
|
||||
{
|
||||
if (att[i].getElementAt(j) == type)
|
||||
{
|
||||
tupleSlot.push_back(j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return tupleSlot;
|
||||
}*/
|
||||
|
||||
string getTableName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void displayTableName() {
|
||||
cout << "The table name is: " << name << endl;
|
||||
}
|
||||
|
||||
vector<Attribute> getAttributes() {
|
||||
return att;
|
||||
}
|
||||
string getTableName() { return name; }
|
||||
vector<Attribute> getAttributes() { return att; }
|
||||
int getSize() { return size; }
|
||||
|
||||
//assumes that all attribute titles are unique
|
||||
void projectQuery(string input) {
|
||||
|
@ -98,24 +43,12 @@ public:
|
|||
}
|
||||
|
||||
void display() {
|
||||
cout<<"\nDisplay of relation--------------------------------"<<endl;
|
||||
cout<<"Relation name: "<<name<<endl;
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
cout<<"\n";
|
||||
cout << "--------------------------\n";
|
||||
cout << name << "\n";
|
||||
for (int i = 0; i < att.size(); ++i){
|
||||
att[i].display();
|
||||
}
|
||||
}
|
||||
|
||||
//make this better
|
||||
vector<string> getDomains() {
|
||||
vector<string> ds;
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
ds.push_back(att[i].getType());
|
||||
}
|
||||
|
||||
return ds;
|
||||
cout << "--------------------------\n";
|
||||
}
|
||||
};
|
||||
|
|
BIN
a.out
BIN
a.out
Binary file not shown.
108
test.cpp
108
test.cpp
|
@ -5,92 +5,34 @@
|
|||
using namespace std;
|
||||
|
||||
//still in progress
|
||||
int main() {
|
||||
int main() {
|
||||
DBEngine engine;
|
||||
|
||||
/*Attribute att1("shamWow", "VARCHAR(10)", true);
|
||||
att1.addRow("rag");
|
||||
att1.addRow("sponge");
|
||||
att1.addRow("wooow");
|
||||
att1.addRow("cloth");
|
||||
Attribute att1("Breakfast", "VARCHAR(20)", true);
|
||||
Attribute att2("Lunch", "VARCHAR(20)", false);
|
||||
Attribute att3("Dinner", "VARCHAR(20)", false);
|
||||
|
||||
Attribute att2("doom", "VARCHAR(20)", false);
|
||||
att2.addRow("zombieman");
|
||||
att2.addRow("revenant");
|
||||
att2.addRow("imp");
|
||||
att2.addRow("archvile");
|
||||
att1.addCell("Pancakes");
|
||||
att1.addCell("Waffles");
|
||||
att1.addCell("Biscuits");
|
||||
att2.addCell("Turkey Sandwich");
|
||||
att2.addCell("Caesar Salad");
|
||||
att2.addCell("Pizza");
|
||||
att3.addCell("Steak");
|
||||
att3.addCell("Shrimp");
|
||||
att3.addCell("Ribs");
|
||||
|
||||
vector<Attribute> vec;
|
||||
vec.push_back(att1);
|
||||
vec.push_back(att2);*/
|
||||
|
||||
Attribute att3("name", "VARCHAR(20)", true);
|
||||
att3.addRow("Fry");
|
||||
att3.addRow("Bender");
|
||||
att3.addRow("Leela");
|
||||
att3.addRow("Zoidberg");
|
||||
vector<Attribute> v;
|
||||
v.push_back(att1);
|
||||
v.push_back(att2);
|
||||
v.push_back(att3);
|
||||
|
||||
Attribute att4("age", "INTEGER", false);
|
||||
att4.addRow("22");
|
||||
att4.addRow("5");
|
||||
att4.addRow("22");
|
||||
att4.addRow("50");
|
||||
Relation r("AltFood", v);
|
||||
//r.display();
|
||||
|
||||
vector<Attribute> vec2;
|
||||
vec2.push_back(att3);
|
||||
vec2.push_back(att4);
|
||||
|
||||
//beginning testing of core DB functions
|
||||
cout << "\a";
|
||||
engine.createTable("table1", vec2);
|
||||
cout << "\a";
|
||||
//engine.createTable("table2", vec);
|
||||
engine.showTable(engine.getTableFromName("table1"));
|
||||
|
||||
cout << "\n\n";
|
||||
|
||||
cout << "\a";
|
||||
|
||||
/*Attribute att5("name", "VARCHAR(20)", true);
|
||||
att5.addRow("Yrf");
|
||||
att5.addRow("Redneb");
|
||||
att5.addRow("Aleel");
|
||||
att5.addRow("Grebdoiz");
|
||||
|
||||
cout << "\a";
|
||||
|
||||
Attribute att6("age", "INTEGER", false);
|
||||
att6.addRow("44");
|
||||
att6.addRow("10");
|
||||
att6.addRow("44");
|
||||
att6.addRow("100");
|
||||
|
||||
vector<Attribute> vec3;
|
||||
vec3.push_back(att5);
|
||||
vec3.push_back(att6);
|
||||
|
||||
engine.createTable("table3", vec3);*/
|
||||
|
||||
//cout << "\n";
|
||||
//cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table2"));
|
||||
//cout << engine.unionComp(engine.getTableFromName("table1"), engine.getTableFromName("table3"));
|
||||
//cout << "\n";
|
||||
|
||||
//engine.project((engine.getTableFromName("table1")), "name");
|
||||
|
||||
/*vector<string> cmds;
|
||||
cmds.push_back("CREATE TABLE animals (name VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);");
|
||||
cmds.push_back("SHOW animals;");
|
||||
|
||||
engine.saveToFile(cmds);*/
|
||||
|
||||
vector<string> t;
|
||||
t.push_back("Professor");
|
||||
t.push_back("180");
|
||||
|
||||
//engine.insertTuple((engine.getTableFromName("table1")), t);
|
||||
//engine.deleteTuple((engine.getTableFromName("table1")), 2);
|
||||
|
||||
cout << "\n\n";
|
||||
engine.showTable((engine.getTableFromName("table1")));
|
||||
engine.createTable("Food", v);
|
||||
engine.createTable(r);
|
||||
engine.createTable("Sadness");
|
||||
//vector<Relation> rs = engine.getRelations();
|
||||
Relation r2 = engine.getTableFromName("Food");
|
||||
r2.display();
|
||||
}
|
||||
|
|
Reference in a new issue