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

📄 common.h

📁 DBMS(Database Management System)在当前的信息系统开发中处于主导位置
💻 H
字号:
//----------------------------------------------------------------------------------
// 作者:刘冰
// 功能:实现结构体的定义,宏定义
//		2008.4.13日
//
//注意事项:
//    1.当前在analysis时由于命名使用了前向指针,并没有进行越界检查,故存在此隐患
//	  2.在执行插入时,没有加入对主键是否重复的检查,故存在此隐患
//----------------------------------------------------------------------------------
#ifndef _COMMON_
#define _COMMON_

#include<vector>
#include<string>
using namespace std;
//------------------------------struct 树形结构------------------------------------
//进行结点树的创建,用于分析其匹配
struct TreeNode
{
	TreeNode()
	{
		strField = "";
		strValue = "";
		iProcessType = 0;
		iCompareType = 0;
	}
	string  strField;
	string  strValue;
	string	strEndValue;
	int		iFieldType;
	int		iProcessType; //进行操作类型,为= > < <= >= like 
	int		iCompareType; //类型的对比,或者为字段名=值,字段名=字段名 ,between (0,1,2) 
};

//一棵树,用于分析Where语句
struct Tree
{
	vector<TreeNode> vAndSet;
	vector<TreeNode> vOrSet;
};
//索引结点的结构体
struct IndexData
{
	string strValue;//索引中的值
	string strPosition;//在数据文件中的真正的位置
	bool   bFlag; //用于标识位
	IndexData()
	{
		bFlag = false;
	}
	long GetPosition()
	{
		if(strPosition.size() <= 0)
		{
			return -1;
		}
		return atol(strPosition.c_str());
	}
};
//字段结点信息
struct FieldNode
{
	FieldNode()
	{
		strcpy(strFieldName,"");
		iFieldType = 0;
		bIsAllowNull = true;
		iValueLength = 0;
		bIsPrimaryKey = false;
		bIsCheck = false;
		lStartValue = 0;
		IEndValue = 0;
	};
    char strFieldName[200]; //字段名
	int  iFieldType;        //字段类型
	bool bIsAllowNull;		//是否允许为空
	int  iValueLength;		//若为字符串,其长度
	bool bIsPrimaryKey;		//是否为主键,若为主键则会为其创建索引文件,若不指定,则默认第一个字段为主键
	bool bIsCheck;			//对于值是否进行检查,即取值范围
	long lStartValue;		//上限
	long IEndValue;			//下限
};


//查询后的结果集合
struct RecordNode
{
	vector<string> vStrValue;
};

struct RecordNodeSet
{
	vector<RecordNode> vRecordList;
	vector<FieldNode>  vFieldNode; //当前信息表的字段信息
	vector<string>  vSelectField;//当前请求的字段信息
	long	lCount; //总记录数
	int		iIndex; //当前的游标位置
	bool	bIsAllField;//当前是否选择了全部的字段,true为是
	
	//下面是操作函数
	RecordNodeSet()
	{
		lCount = 0;
		iIndex = 0;
		bIsAllField = true;
	}	

	int	GetCurrentIndex()
	{
		return iIndex;
	}

	long GetRecordSetCount()
	{
		return lCount;
	}

	bool Next() //指向下一条记录
	{
		if(iIndex++ >= lCount)
		{
			return false;
		}
		return true;
	}

	bool First()//指向首记录
	{
		if(lCount > 0)
		{
			iIndex = 0;
			return true;
		}
		return false;
	}

	bool End()//指向最后一个记录
	{
		if(lCount > 0)
		{
			iIndex = lCount - 1;
			return true;
		}
		return false;
	}
	
	const char* GetStrValue(int iNum)
	{
		if(iIndex < lCount)
			return vRecordList[iIndex].vStrValue[iNum].c_str();
		return NULL;
	}	

	int GetIntValue(int iNum)
	{
		string strValue;
		strValue = GetStrValue(iNum);
		return atoi(strValue.c_str());
	}

	int GetLongValue(int iNum)
	{
		string strValue;
		strValue = GetStrValue(iNum);
		return atol(strValue.c_str());
	}

	void AddRecord(RecordNode &pRecord)
	{
		vRecordList.push_back(pRecord);
	}
};

//用于标识要更新的字段列表
struct UpdateField
{
	string strFieldName;
	string strUpdateValue;
	int iPosition;//相对于字段中的位置
	bool isPrimary ;// 是否为主键
	UpdateField()
	{
		iPosition = 0;
		isPrimary = false;
	}
};
//----------------------------------   公共操作函数API    ------------------------------------------
//进行词法分析,将命令符分解成一个个字串
bool SplitCommand(char *pStrCommand,vector<string> &pCommandField);
//进行语法分析,返回的是操作类型,即那一种命令
int	QuerySQLType(vector<string> &pCommandField);
//用于查询关键符号的类型
int QueryKeyType(const char *pSQL);
//检查字符是否为字母或数字
bool CharIsAlphabet(char ch);
bool CharIsNum(char ch);
//检查是否符合命名规则
bool CheckRuleForString(const char *pStr);
bool CheckRuleForNum(const char *pStr);
//设置时间
void SetTime(long &lTime,char *pStrTime);


//----------------------有关数据库操作API开发库-------------------------
//检查当前要操作的数据库是否存在
bool IsDBExist(const char *pStrDBName); 
//进一步检查数据库的SQL语法有无问题
bool CheckDBSQLRule(vector<string> &pCommandField);
//从数据库配置表中写入当前的记录信息
bool WriteDBInfor(struct DataBase pDB);
//从配置表中读取当前所有信息,到数据库向量
void ReadDBInfor( vector<DataBase> &pDB);
//删除当前记录
bool DeleteDBinfor(const char *pStrDBName);
//具体执行创建命令
bool CreateDataBase(const char *pStrDBName);
//具体执行删除命令
bool DropDatabase(const char *pStrDBName);

//------------------------有关表操作API开发库------------------------------------
// 每个数据库里面,有一个tables.list文件,用于存放表文件
//检查当前的表是否存在
bool IsTableExist(const char *pStrTableName);
bool WriteTableInfor(struct TableNode pTable);
//从配置表中读取当前所有信息,到数据库向量
void ReadTableInfor( vector<TableNode> &pTable);
//删除当前记录
bool DeleteTableinfor(const char *pStrTableName);
//删除三张表
bool DropTable(const char *pStrTableName);
//创建一张表,进行SQL语句解析
//检查是否存在primary键
//设置为主键
bool SetPrimaryField(vector<struct FieldNode> &pFieldList);
//是否为主键
bool IsPrimaryExist(vector<struct FieldNode> &pFiledList);
//该字段是否存在,不过此函数其具体应用性较差,本处没有使用
bool IsFieldExist(const char *pstrFieldName,const char *pTableName);
//分析字段,生成字段信息
int AnalysisField(vector<string> &pCommandField,int iIndex,vector<FieldNode> &pFieldList);
//检查当前的字段是否为要求进行check fieldname between xxx and xxx
int AnalysisCheck(vector<string> &pCommandField,int iIndex,vector<FieldNode> &pFieldList);
//预处理SQL语句,生成相应的结构体信息
bool ParseCreateTable(vector<struct FieldNode> &pFieldList,vector<string> &pCommandField);
//执行上一步的结果
bool ExecCreateTable(const char *pStrTableName,vector<struct FieldNode> &pFieldList);

//-------------------------有关记录操作API开发库---------------------------------
//用于读取字段结点的数据结构
void ReadFiledListStruct(vector<struct FieldNode> &pFieldList,const char *pStrTableName);
//分析要插入的值结果
int AnalysisInsertRecordValue(vector<string> &pCommandField,int iIndex,vector<string> &pValueList);
//分析要插入的字段列表信息
int AnalysisInsertSet(vector<string> &pCommandField,int iIndex,vector<string> &pFieldList);
//检查要插入的文本类型是否合法
bool CheckRuleForInsertTextType(string &strValue,string &pStr);
//检查字段名是否在当前的字段列表中存在
int FindFieldExist(const char *pstrFieldName,vector<string> &pFieldList);
//检查当前字段名是否存在
bool IsFieldExist(const char *pstrFieldName,vector<FieldNode> &pFiledList,vector<FieldNode>::iterator pData);
//检查输入规则是否合法
bool CheckRuleForInsertField(vector<FieldNode> &pFieldSet,vector<string> &pFieldList,vector<string> &pValueList,vector<string> &pValue);
//插入记录的预处理SQL语句,返回字段结构体,与结果集结构体
bool ParseInsertRecord(vector<string> &pCommandField,vector<string> &pFieldList,vector<string> &pValueList);
bool ExecInsertRecord(const char *pStrTableName,vector<string> &pFieldList,vector<string> &pValueList);
//Where语句的解析
bool AnalysisWhereStatment(vector<string> &pCommandField,struct Tree *pTree,int iCurIndex,int iTableIndex = 2);
//删除语句,返回要删除的结构体,主要面向Where单语句操作
//定义结构体 string name string value string operator string or,and
bool ParseDeteleRecord(vector<string> &pCommandField,Tree *pTree);
bool ExecDeleteRecord(const char *pStrTableName,Tree *pTree);

//将数据进行更新,首先还是要创建更新的结构体
//分析更新的字段列表
int	 AnalysisUpdateFieldSet(vector<string> &pCommandField,vector<UpdateField> &vUpdateFieldList);
//找出一条记录
bool FindOneRecord(FILE *pFile,long lPosition,RecordNode &vRecord);
//进行解析更新的字段
bool ParseUpdateRecord(vector<string> &pCommandField,vector<UpdateField> &vUpdateFieldList,Tree *pTree,string &strTableName);
//用于执行更新
bool ExecUpdateRecord(const char *pStrTableName,vector<UpdateField> &vUpdateFieldList,Tree *pTree);

//定义查询结构体,执行查询语句
int AnalysisSelectFieldSet(vector<string> &pCommandField,RecordNodeSet vRecordSet);
bool ParseSelectRecord(vector<string> &pCommandField,RecordNodeSet &vRecordSet,Tree *pTree,string &strTableName);
bool ExecSelectRecord(const char *pStrTableName,RecordNodeSet &vRecordSet,Tree *pTree);

//==================有关数据表与索引表的操作库-API────────────────────────
//将索引文件中的结果读入指定的结构体中
bool ReadIndexRecord(vector<IndexData> &vIndexHead,const char *pFileName);
//将数据值读入指定的结果集中
bool ReadDataRecord(RecordNodeSet *pRecordSet,const char *pFileName);

//++++++++++++++++++++++++++++++++有关操作API,在控制台下的工作
void ProcessCreateDB(vector<string> &pCommandField);
void ProcessDropDB(vector<string> &pCommandField);

void ProcessCreateTable(vector<string> &pCommandField);
void ProcessDropTable(vector<string> &pCommandField);

void ProcessInsert(vector<string> &pCommandField);
void ProcessSelect(vector<string> &pCommandField);
void ProcessUpdate(vector<string> &pCommandField);
void ProcessDelete(vector<string> &pCommandField);

void ProcessDesc(vector<string> &pCommandField);
void ProcessHelp(vector<string> &pCommandField);

void ProcessShowDB(vector<string> &pCommandField);
void ProcessShowTable(vector<string> &pCommandField);

void ProcessUse(vector<string> &pCommandField);
void ProcessGetCWD(vector<string> &pCommandField);
void ParseSQL(char *strCommand);

//对外开发函数
bool CreateDB(vector<string> &pCommandField);
bool DropDB(vector<string> &pCommandField);

bool CreateTable(vector<string> &pCommandField);
bool DropTable(vector<string> &pCommandField);

bool InsertRecord(vector<string> &pCommandField);
bool SelectRecord(vector<string> &pCommandField,RecordNodeSet &pRecordSet);
bool UpdateRecord(vector<string> &pCommandField);
bool DeleteRecord(vector<string> &pCommandField);
//对外开发包
bool OpenLiuDBMS(const char *pStrDBName,const char *pUsername);
void CloseLiuDBMS();
bool ExecSQL(char *pSQL,RecordNodeSet *pRecordSet = NULL);
#endif

⌨️ 快捷键说明

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