如何在C ++中创建一个简单的数据库 [英] How do I make a simple database in C++

查看:250
本文介绍了如何在C ++中创建一个简单的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发一个关于用c ++模拟数据库的项目,这就是我要做的事情:

-create / delete一个新表(CREATE TABLE ...) - 表数据可以有以下类型:INT,DOUBLE,STRING,BOOL,DATE;

-通过添加/来修改表删除列(ALTER TABLE ...)

-select rows(SELECT ... FROM ... WHERE ...)

-delete rows(DELETE ... )

- 在表中添加新行(INSERT INTO ... VALUES ...)

-sort表数据(SORT BY ... ASC / DESC)

-print数据库数据

我必须使用文件备份和STL容器和设计模式。







这是一个数据库类,我将在其中存储所有创建的表。

  class 数据库
{
private
string dname;
static vector< Table *> D b;

public
Database(){};
数据库(字符串& s){dname = s; }
数据库(字符串&,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 *>);
};







这是一个Table类,我将在其中存储所有元素。

  class 
{
私人
string tname;
vector< Element *> trows;

public
Table(){};
表(字符串& n){tname = n; }
表(字符串&,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 ;

表makeTableFromFile(string&);
};





这是一个Element类,我将在其中存储每个字段。

  class 元素
{
private
int uid;
vector< Field> efields;

public
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 ;
};





和Field类。

 模板< class Type> 
class 字段
{
private
类型价值;

public
void set(Type& x) { - > value = x; };
void outputField(ostream&,Type&) const ;
};





我真的不知道如何实现这个字段类,因为它可能有很多类型和向量来自Element类不能存储多于一种类型的数据。



你能给我一些关于整体实现的提示吗?这是一个好的开始吗?

解决方案

一种可能性是使用与 VARIANT [ ^ ]数据类型,即 struct 包含 union 支持的数据类型以及包含存储在 union 中的实际类型的字段。

您还可以查看可用的数据库实现源,例如, SQLite one。


您可以使用Field类从一个常见的非模板化基类继承,只在Element实例中存储指针。

  class  iField {
public
模板< typename T>
虚拟 void set( const T& value )= 0 ;
模板< typename T>
虚拟 void get(T& value const = 0 ;
};
模板< typename T0>
class 字段: public iField {
私人
T0 value_;
public
template < typename T>
虚拟 void set( const T& value );
模板< typename T>
虚拟 void get(T& value const ;
// 现在专门化相关功能:
template <>
虚拟 void set( const T0& value ){value_ = value ; }
模板<>
虚拟 void get(T0& value const { value = value_; }
};



当然这种方法还有其他意义 - 我还没有真正想到这一点到最后。但后来我猜这是你首先要完成的任务。 ;)


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?

解决方案

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 <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. ;)


这篇关于如何在C ++中创建一个简单的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆