⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createtable.h

📁 C++实现简单数据库管理系统
💻 H
字号:
#ifndef _CREATETABLE_H
#define _CREATETABLE_H

#include "common.h"

 const string inline_explain ("(\\w+)\\s*(float|char)\\((\\d+)\\)\\s*(NULL|NOT NULL|NOTNULL)?\\s*;\\s*");

 const string total=string("\\s*create table\\s*(\\w+)\\s*\\(\\s*(")+inline_explain+string(")+\\s*\\)");

int create_table(const char* file_name)
{
	ifstream in;
	in.open(file_name);
	if (!in.is_open())
	{
		cerr<<"I can't open the file\n";
		return 0;//can't open the file
	}

	string create_cmd="",temp;
	bool right=false;// check the format

	while(getline(in,temp))create_cmd+=" "+temp; //get all the string in the create file
	in.close();

	string::const_iterator beg = create_cmd.begin();
	string::const_iterator end = create_cmd.end();
	smatch result;
	regex reg(total);
	while(regex_search(beg, end, result, reg))
	{
		column* ptr = new column;
		right=true;
		map<string, list<column *> *  >::iterator map_ptr;

		map_ptr = SQL.find(result[1].str());// get the table name and find whether already exist

		if (!SQL.empty()&&map_ptr!=SQL.end())//already has the same table name
		{
			cerr<<"tHe table has already exist\n";
			return 0;//error number
		}
		 table * table_ptr = new table;
		SQL.insert(make_pair(result[1].str(),table_ptr));//insert the table name and the table address
	
		string::const_iterator column_beg = beg;
		string::const_iterator column_end = result[0].second;
		smatch column_match;
		regex column_reg(inline_explain);

		while(regex_search(column_beg, column_end, column_match, column_reg))
		{
			column* column_ptr = new column;
			column_ptr->column_name=column_match[1].str();
			(column_match[2].str()=="float")?(column_ptr->type_float=true):(column_ptr->type_char=true);
			((column_match[4].str()=="NULL")||(column_match[4].str()==""))?(column_ptr->is_null=true):(column_ptr->is_null=false);
			if (column_ptr->type_float)
				column_ptr->date_length=4;
			if(column_ptr->type_char)
				column_ptr->date_length=atoi(column_match[3].str().c_str());
			column_ptr->data.clear();

		
			table_ptr->push_back(column_ptr);
			column_beg=column_match[0].second;
		}

		beg = result[0].second;	
	}
	if(!right)// the format of the create format is not right
		return 1;//error number
	return 2;//all the things are right
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -