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
public:
Database(){};
Database(string &s) { dname = s; }
Database(string &, vector
void addTable(string &s) { db.push_back(&Table(s)); };
void addTable(string &s, vector &v) { db.push_back(&Table(s, v)); };
void removeTable(string &);
int searchForTable(string &);
string getDname() { return dname; };
const vector
void setDname(string &s) { dname = s; }
void setDb(vector
};
This is a Table class in which I will store all the elements.
class Table
{
private:
string tname;
vector trows;
public:
Table(){};
Table(string &n) { tname = n; }
Table(string &, vector);
void setTname(string &s) { tname = s; };
void setTrows(vector &);
const string getTname() { return tname; };
const vector & 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 efields;
public:
Element();
Element(int, vector);
const int& getUid() { return uid; };
const vector & getEfields() { return efields; };
void setUid(int &u) { uid = u; };
void setEfields(vector &);
void outputElement(ostream &, Element &) const;
};
And the Field class.
template
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?
解决方案One possibility is using something similar to the VARIANT[^] datatype, that is struct containing a union of the supported datatypes plus a field containing the actual type stored in the union itself.
You could also have a look at avaliable database implementation sources, like, for instance, SQLite one.
You could have your Field class inherit from a common non-templated base class and only store pointers in your "Element" instances.
class iField {
public:
template
virtual void set(const T& value)=0;
template
virtual void get(T& value) const=0;
};
template
class Field : public iField {
private:
T0 value_;
public:
template
virtual void set(const T& value);
template
virtual void get(T& value) const;
//now specialize the relevant functions:
template <>
virtual void set(const T0& value) { value_ = value; }
template <>
virtual void get(T0& value) const { value = value_; }
};
Of course such an approach has further implications - I haven't really thought this through to the end. But then I guess that is the task that was set to you in the first place. ;)