Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
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.
C++
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.
C++
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.
C++
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?
Posted
Comments
BacchusBeale 16-May-14 6:00am    
Are you using SQL Server or Express or you are trying to make your own database?
If using Visual Studio, you can make the database and tables directly and then create a DataSet.
Ștefan Cenușă 16-May-14 7:20am    
I'm trying to make my own simple database. I don't need an interaction with a server or anything else fancy. Yes, I am using VS.

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.
 
Share this answer
 
You could have your Field class inherit from a common non-templated base class and only store pointers in your "Element" instances.
C++
class iField {
public:
   template <typename T>
   virtual void set(const T& value)=0;
   template <typename T>
   virtual void get(T& value) const=0;
};
template <typename T0>
class Field : public iField {
private:
   T0 value_;
public:
   template <typename T>
   virtual void set(const T& value);
   template <typename T>
   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. ;)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900