Hello everyone,
I'm working on a project about simulating a database in c++ and this is what I have to do:
-create/delete a new table (CREATE TABLE ...) - table data can have the following types: INT, DOUBLE, STRING, BOOL, DATE;
-modify a table by adding/removing columns (ALTER TABLE ...)
-select rows (SELECT ... FROM ... WHERE ...)
-delete rows (DELETE ...)
-adding new rows in a table (INSERT INTO ... VALUES ...)
-sort table data (SORT BY ... ASC/DESC)
-print database data
I have to use files for backup and STL containers and design patterns.
This is a Database class in which I will store all the tables created.
class Database
{
private:
string dname;
static vector<Table *> db;
public:
Database(){};
Database(string &s) { dname = s; }
Database(string &, vector<Table *> &);
void addTable(string &s) { db.push_back(&Table(s)); };
void addTable(string &s, vector<Element *> &v) { db.push_back(&Table(s, v)); };
void removeTable(string &);
int searchForTable(string &);
string getDname() { return dname; };
const vector<Table *> & getDb() { return db; };
void setDname(string &s) { dname = s; }
void setDb(vector<Table *>);
};
This is a Table class in which I will store all the elements.
class Table
{
private:
string tname;
vector<Element *> trows;
public:
Table(){};
Table(string &n) { tname = n; }
Table(string &, vector<Element *>);
void setTname(string &s) { tname = s; };
void setTrows(vector <Element *> &);
const string getTname() { return tname; };
const vector <Element *> & getTrows() { return trows; };
void addElement(Element &e) { trows.push_back(&e); }
void removeElement(Element &);
int searchForElement(Element &);
void outputTable(ostream &, Table &) const;
Table makeTableFromFile(string &);
};
This is an Element class in which I will store each field.
class Element
{
private:
int uid;
vector<Field> efields;
public:
Element();
Element(int, vector<Field>);
const int& getUid() { return uid; };
const vector<Field> & getEfields() { return efields; };
void setUid(int &u) { uid = u; };
void setEfields(vector<Field> &);
void outputElement(ostream &, Element &) const;
};
And the Field class.
template <class Type>
class Field
{
private:
Type value;
public:
void set(Type &x) { this->value = x; };
void outputField(ostream &, Type &) const;
};
I don't really know how to implement this field class because it could have many types and the vector from the Element class can't store more then one type data.
Could you give me some hints about the overall implementation? Is it a good start?