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

📄 common.cpp

📁 DBMS(Database Management System)在当前的信息系统开发中处于主导位置
💻 CPP
📖 第 1 页 / 共 5 页
字号:

			}
			else
			{
				count++;
			}
		}
		fclose(fp);
		printf("共影响了[%d] 条记录\n",count);
	}
	return true;
}
//用于生成字段集合
int AnalysisSelectFieldSet(vector<string> &pCommandField,RecordNodeSet vRecordSet)
{
	if(strcmp(pCommandField[1].c_str(), "*") == 0)
	{
		if(strcmp(pCommandField[2].c_str(),"from") == 0)
			return 2;
		else 
			return false;
	}
	else 
	{
		int iIndex = 1;
		while(pCommandField.size() > iIndex)
		{
			if(strcmp(pCommandField[iIndex].c_str(), "from") == 0)
				return iIndex;
			else if(strcmp(pCommandField[iIndex].c_str() ,",") == 0)
			{
				iIndex++;
			}
			else
			{
				if(!CheckRuleForString(pCommandField[iIndex].c_str()))
					return false;
				vRecordSet.vSelectField.push_back(pCommandField[iIndex]);
				iIndex++;
			}
		}
	}
	return false;
}
//Select语句主要有如下三种方式:
// select * [fieldname] from lid 
// select  from lid where xxx = xx
// select [*] fieldname1,fieldname2 from xxx where xxx = xxx
bool ParseSelectRecord(vector<string> &pCommandField,RecordNodeSet &vRecordSet,Tree *pTree,string &strTableName)
{
	//第一部分用于分析where语句之前的内容 
	int iIndex = AnalysisSelectFieldSet(pCommandField,vRecordSet);
	if(iIndex  == false)
	{
		printf("执行Select字段选择时出错\n");
		return false;
	}

	iIndex++;
	//进行表的分析
	int iTableIndex = 2;
	if(pCommandField.size() > iIndex)
	{
		if(!IsTableExist(pCommandField[iIndex].c_str()))
		{
			printf("要操作的表不存在\n");
			return false;
		}
		iTableIndex = iIndex;
		strTableName = pCommandField[iIndex];
		ReadFiledListStruct(vRecordSet.vFieldNode,pCommandField[iIndex].c_str());
		//检查当前的字段信息,同时进行排序,注意若值为*则别当别论
		if(iIndex > 3)
		{
			vRecordSet.bIsAllField = false;//标识当前不为全部的结果集
			vector<string>::iterator pStr;
			for(pStr = vRecordSet.vSelectField.begin(); pStr != vRecordSet.vSelectField.end(); pStr++)
			{
				bool bFlag = false;
				vector<FieldNode>::iterator pData;
				for(pData = vRecordSet.vFieldNode.begin(); pData != vRecordSet.vFieldNode.end(); pData++)
				{
					if(strcmp(pStr->c_str(), pData->strFieldName) == 0)
					{
						bFlag = true;
					}
				}
				if(!bFlag)
				{
					printf("不能识别的字段\n",pStr->c_str());
					return false;
				}
			}
		}		
	}
	else
		return false;

	iIndex++;
	if(pCommandField.size() == iIndex)
		return true;
	else if(pCommandField.size() > iIndex)
	{
		//进行where的分析
		if(!AnalysisWhereStatment(pCommandField,pTree,iIndex,iTableIndex))
		{
			printf("where语句有问题\n");
			return false;
		}
	}

	return true;
}
bool ExecSelectRecord(const char *pStrTableName,RecordNodeSet &vRecordSet,Tree *pTree)
{
	if(pTree == NULL)
	{
		return false;
	}	
	
	vector<IndexData> vIndexHead;
	if(!ReadIndexRecord(vIndexHead,pStrTableName))
	{
		printf("读取索引数据失败\n");
		return false;
	}

	if(pTree->vAndSet.size() <= 0)
	{
		string strFileName ;	
		strFileName = "data\\" ;
		strFileName.append(g_strCurrentDBName);
		strFileName += "\\" ;
		strFileName.append(pStrTableName);
		strFileName += ".ltd";

		FILE *pFile;
		if((pFile=fopen(strFileName.c_str(),"rb"))==NULL)
		{	
			printf("创建索引文件失败\n");
			return false;
		}
		
		int iCount = 0;
		string strValue;
		RecordNode rNode;
		while(iCount < vIndexHead.size())
		{	
			fseek(pFile,vIndexHead[iCount].GetPosition(),0);
			while(!feof(pFile))
			{
				char ch = fgetc(pFile);
				char nextCh;
				switch(ch)
				{
				case '&':			
					nextCh	= fgetc(pFile);
					if(nextCh == '&')
					{
						//进入下一条记录,或者当前是首记录
						//如果当前从开始字符串起
						if(rNode.vStrValue.size() <= 0)
							break;
						else
						{
							rNode.vStrValue.push_back(strValue);					
							vRecordSet.AddRecord(rNode);
							rNode.vStrValue.clear();
							strValue = "";
							goto Lable;
						}

					}
					else
					{
						//下一个字段信息
						rNode.vStrValue.push_back(strValue);
						strValue = "";
						strValue += nextCh;
					}
					break;
				case '\\':
					strValue += fgetc(pFile);
					break;
				case EOF:
					rNode.vStrValue.push_back(strValue);
					vRecordSet.AddRecord(rNode);
					rNode.vStrValue.clear();
					strValue = "";
					break;
				default:
					strValue += ch;
					break;
				}
			}		
		Lable:	iCount++;
		}
		fclose(pFile);
		printf("共查找了[%d] 条记录\n",iCount);
		return true;
	}
	string strValue;
	const char *pStr = pTree->vAndSet[0].strValue.c_str();
	if(pStr[0] == '\'' || pStr[0] == '\"')
	{
		if(!CheckRuleForInsertTextType(strValue,pTree->vAndSet[0].strValue))
		{
			printf("当前的值出现问题");
			return false;
		}
		pTree->vAndSet[0].iFieldType = VARCHAR_TYPE;
	}
	else
	{
		strValue = pStr;
		pTree->vAndSet[0].iFieldType = INT_TYPE;
	}
	
	//进行数据分析
	vector<long> vDataPosition;
	vector<IndexData>::iterator pData;
	bool bModifyFlag = false;
	for(pData = vIndexHead.begin(); pData != vIndexHead.end(); pData++)
	{
		switch(	pTree->vAndSet[0].iProcessType )
		{
		case BIG_OPERATOR:
			if(strValue < pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case EQUAL_OPERATOR:
			if(strValue == pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case NOEQUAL_OPERATOR:
			if(strValue != pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case BIG_EQUAL_OPERATOR:
			if(strValue <= pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case SMALL_OPERATOR:
			if(strValue > pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case SMALL_EQUAL_OPERATOR:
			if(strValue >= pData->strValue)
			{	
				pData->bFlag = true;
				vDataPosition.push_back(pData->GetPosition());
				bModifyFlag = true;
			}
			break;
		case LIKE_OPERATOR:
			
			break;
		case BWTEEN_OPERATOR: //between语句	
			
			break;
		}
	}
	//进行数据修改的操作,其操作办法是重写当前的值
	if(bModifyFlag)
	{
		string strFileName ;	
		strFileName = "data\\" ;
		strFileName.append(g_strCurrentDBName);
		strFileName += "\\" ;
		strFileName.append(pStrTableName);
		strFileName += ".ltd";

		FILE *pFile;
		if((pFile=fopen(strFileName.c_str(),"rb"))==NULL)
		{	
			printf("创建索引文件失败\n");
			return false;
		}
		
		int iCount = 0;
		string strValue;
		RecordNode rNode;
		while(iCount < vDataPosition.size())
		{	
			fseek(pFile,vDataPosition[iCount],0);
			while(!feof(pFile))
			{
				char ch = fgetc(pFile);
				char nextCh;
				switch(ch)
				{
				case '&':			
					nextCh	= fgetc(pFile);
					if(nextCh == '&')
					{
						//进入下一条记录,或者当前是首记录
						//如果当前从开始字符串起
						if(rNode.vStrValue.size() <= 0)
							break;
						else
						{
							rNode.vStrValue.push_back(strValue);					
							vRecordSet.AddRecord(rNode);
							rNode.vStrValue.clear();
							strValue = "";
							goto Lable1;
						}

					}
					else
					{
						//下一个字段信息
						rNode.vStrValue.push_back(strValue);
						strValue = "";
						strValue += nextCh;
					}
					break;
				case '\\':
					strValue += fgetc(pFile);
					break;
				case EOF:
					rNode.vStrValue.push_back(strValue);
					vRecordSet.AddRecord(rNode);
					break;
				default:
					strValue += ch;
					break;
				}
			}		
		Lable1:	iCount++;
		}
		fclose(pFile);
		printf("共查找了[%d] 条记录\n",iCount);
	}
	return true;
}
//分析更新的字段列表 Update xxx set field = fw , fe = 2f where name > 23
int	 AnalysisUpdateFieldSet(vector<string> &pCommandField,vector<UpdateField> &vUpdateFieldList)
{
	int iIndex = 1;
	if(!IsTableExist(pCommandField[1].c_str()))
	{
		printf("当前要操作表不存在\n");
		return false;
	}

	iIndex++;
	if(iIndex >= pCommandField.size())
	{
		printf("%s\n",COMMANDLENGTHISNOTENOUGH);
		return false;
	}
	if(!strcmp(pCommandField[iIndex].c_str(),"set") == 0)
	{
		printf("缺少Set符,请检查输入\n");
		return false;
	}

	iIndex++;
	//进行分析字段列表,中间以逗号进行间隔
	vector<FieldNode> pFiledList;
	ReadFiledListStruct(pFiledList,pCommandField[1].c_str());
	vector<FieldNode>::iterator pData;
	

	while( (pCommandField.size() > iIndex) 
		&& (strcmp(pCommandField[iIndex].c_str(), "where") != 0))
	{
		UpdateField pNode;
		//进行字段分析
		if(!CheckRuleForString(pCommandField[iIndex].c_str()))
		{
			printf("分析的字段不符合规则[%s]\n",pCommandField[iIndex].c_str());
			return false;
		}
	
		int iCount = -1;
		for(pData = pFiledList.begin(); pData != pFiledList.end(); pData++)
		{
			iCount++;
			if(strcmp(pData->strFieldName,pCommandField[iIndex].c_str()) == 0)
			{
				pNode.strFieldName = pCommandField[iIndex].c_str();
				pNode.iPosition = iCount;
				pNode.isPrimary = pData->bIsPrimaryKey;
				string strValue;
				if(++iIndex >= pCommandField.size())
				{
					printf("后面要一个操作符\n");
					return false;
				}
				if(strcmp(pCommandField[iIndex].c_str(),"=") != 0)
				{
					printf("当前第[%d]个操作符,应该是个=\n",iIndex+1);
					return false;
				}

				//进行设定当前的字段值
				if(++iIndex >= pCommandField.size())
				{
					printf("后面要一个操作符\n");
					return false;
				}				
				switch(pData->iFieldType)
				{
					case VARCHAR_TYPE:
					if(!CheckRuleForInsertTextType(strValue,pCommandField[iIndex]))
					{
						printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pCommandField[iIndex].c_str());
						return false;
					}
					if(strValue.length() > pData->iValueLength)
					{
						printf("字段[%s]值[%s]越界\n",pData->strFieldName,strValue.c_str());
						return false;
					}
					pNode.strUpdateValue = strValue;
					break;
				case INT_TYPE:
					if(!CheckRuleForNum(pCommandField[iIndex].c_str()))
					{
						printf("字段[%s]值[%s]不符合整数规则\n",pData->strFieldName,pCommandField[iIndex].c_str());
						return false;
					}
					if(pData->bIsCheck)
					{
						long lValue = atol(pCommandField[iIndex].c_str());
						if(!(lValue >= pData->lStartValue && lValue <= pData->IEndValue))
						{
							printf("输入的整形[%ld]没有通过规则检查,规则下限[%ld],规则上限[%ld]\n",lValue,pData->lStartValue,pData->IEndValue);
							return false;
						}
					}
					pNode.strUpdateValue = strValue;
					break;
				case TEXT_TYPE: 
					if(!CheckRuleForInsertTextType(strValue,pCommandField[iIndex]))
					{
						printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pCommandField[iIndex].c_str());
						return false;
					}
					pNode.strUpdateValue = strValue;
					break;
				case DATE_TYPE:
					if(!CheckRuleForInsertTextType(strValue,pCommandField[iIndex]))
					{
						printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pCommandField[iIndex].c_str());
						return false;
					}
					pNode.strUpdateValue = strValue;
					break;
				default:
					printf("不能识别的字段类型\n");
					return false;
				}	
			}
		}
		vUpdateFieldList.push_back(pNode);
		//进行逗号的判断
		if(++iIndex >= pCommandField.size())
		{
			printf("后面要一个操作符\n");
			return false;
		}
		if((strcmp(pCommandField[iIndex].c_str(),",") != 0)
			&& (strcmp(pCommandField[iIndex].c_str(),"where") != 0))
		{
			printf("当前第[%d]个操作符,应该是个, 或where\n",iIndex+1);
			return false;
		}
		if(strcmp(pCommandField[iIndex].c_str(),"where") == 0)
		{
			return iIndex;
		}
		//进行下一字段的分析
		iIndex++;
		
	}

	return iIndex;
}
//进行解析更新的字段
bool ParseUpdateRecord(vector<string> &pCommandField,vector<UpdateField> &vUpdateFieldList,Tree *pTree,string &strTableName)
{
	int iIndex = AnalysisUpdateFieldSet(pCommandField,vUpdateFieldList);
	if(iIndex  == false)
	{
		printf("分析更新字段列表时出错\n");
		return false;
	}
	
	if(!AnalysisWhereStatment(pCommandField,pTree,iIndex,1))
	{
		printf("分析Where语句失败\n");
		return false;
	}	
	return true;
}

//选中一条记录
bool FindOneRecord(FILE *pFile,long lPosition,RecordNode &vRecord)
{
	fseek(pFile,lPosition,0);
	string strValue;
		
	while(!feof(pFile))
	{
		char ch = fgetc(pFile);
		char nextCh;
		switch(ch)
		{
			case '&':			
				nextCh	= fgetc(pFile);
				if(nextCh == '&')
				{
					//进入下一条记录,或者当前是首记录
					//如果当前从开始字符串起
					if(vRecord.vStrValue.size() <= 0)
						break;
					else

⌨️ 快捷键说明

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