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

📄 common.cpp

📁 DBMS(Database Management System)在当前的信息系统开发中处于主导位置
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					{
						vRecord.vStrValue.push_back(strValue);					
						return true;
					}

				}
				else
				{
					//下一个字段信息
					vRecord.vStrValue.push_back(strValue);
					strValue = "";
					strValue += nextCh;
				}
				break;
			case '\\':
				strValue += fgetc(pFile);
				break;
			case EOF:
				vRecord.vStrValue.push_back(strValue);
				break;
			default:
				strValue += ch;
				break;
			}
		}	
	return true;
}
//用于执行更新
bool ExecUpdateRecord(const char *pStrTableName,vector<UpdateField> &vUpdateFieldList,Tree *pTree)
{
	//通过Where语句找出当前要处理索引集合
	//取出每个索引值的真实数据,同时进行格式化处理,然后添加索引
	//添加一条记录,同时将其索引值进行同步更新,然后重写索引
	if(pTree == NULL)
	{
		return false;
	}
	
	if(pTree->vAndSet.size() <= 0)
	{
		printf("当前没有要操作的条件\n");
		return false;
	}
	
	vector<IndexData> vIndexHead;
	if(!ReadIndexRecord(vIndexHead,pStrTableName))
	{
		printf("读取索引数据失败\n");
		return false;
	}
	printf("Exece this \n");
	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<IndexData>::iterator pData;
	bool bModifyFlag = false;
	for(pData = vIndexHead.begin(); pData != vIndexHead.end(); pData++)
	{
		//
		//printf("TT:%s\n",pTree->vAndSet[0].strField.c_str());
		switch(	pTree->vAndSet[0].iProcessType )
		{
		case BIG_OPERATOR:
			if(strValue < pData->strValue)
			{	
				pData->bFlag = true;
				bModifyFlag = true;
			}
			break;
		case EQUAL_OPERATOR:
			printf("equal \n");
			if(strValue == pData->strValue)
			{					
				bModifyFlag = true;
				pData->bFlag = true;	
				printf("equal \n");
			}
			break;
		case NOEQUAL_OPERATOR:
			if(strValue != pData->strValue)
			{	
				pData->bFlag = true;
				bModifyFlag = true;
			}
			break;
		case BIG_EQUAL_OPERATOR:
			if(strValue <= pData->strValue)
			{	
				pData->bFlag = true;
				bModifyFlag = true;
			}
			break;
		case SMALL_OPERATOR:
			if(strValue > pData->strValue)
			{	
				pData->bFlag = true;
				bModifyFlag = true;
			}
			break;
		case SMALL_EQUAL_OPERATOR:
			if(strValue >= pData->strValue)
			{	
				pData->bFlag = true;
				bModifyFlag = true;
			}
			break;
		case LIKE_OPERATOR:
			
			break;
		case BWTEEN_OPERATOR: //between语句		
			
			break;
		}
	}
	printf("Exece  bGlag \n");
	//当前已经获得了所有值
/*	if(!bModifyFlag)
	{
		printf("当前没有要更新的据\n");
		return true;
	}
*/
	if(bModifyFlag)
	{

		//循环处理每个记录
		//先整理其结果集合,然后进行字段分析
		string strFileName ;	
		strFileName = "data\\" ;
		strFileName.append(g_strCurrentDBName);
		strFileName += "\\" ;
		strFileName.append(pStrTableName);
		strFileName += ".ltd";

		//打开一个数据文件
		FILE *pFile;
		if((pFile = fopen(strFileName.c_str(),"ab+")) == NULL)
		{
			return false;
		}

		for(pData = vIndexHead.begin(); pData != vIndexHead.end(); pData++)
		{
			if(pData->bFlag)
			{
				//先定位其长度,然后将值顺次取出
				//将新结果集进行更新,检查更新的是否有主键,也同步更新Position和Value
				//重写到数据文件
				//最后统一写入到索引文件中
				//fseek(pFile,pData->GetPosition(),0);
				RecordNode record;
				FindOneRecord(pFile,pData->GetPosition(),record);
				
				//进行比对添加
				vector<UpdateField>::iterator pUpdate;
				for(pUpdate = vUpdateFieldList.begin();pUpdate != vUpdateFieldList.end(); pUpdate++)
				{
					//此句有问题
					record.vStrValue[pUpdate->iPosition] = pUpdate->strUpdateValue;

					if(pUpdate->isPrimary)
						pData->strValue = pUpdate->strUpdateValue;
				}
				
				//顺次更新到结果集 同时更新索引
				fseek(pFile,0L,SEEK_END);
				long lPos = ftell(pFile);
				char value[50];
				sprintf(value,"%d",lPos);
				pData->strPosition = value ;

			//	fseek(pFile,0L,SEEK_END);
				vector<string>::iterator pNew;
				fputc('&',pFile);
				for(pNew = record.vStrValue.begin(); pNew != record.vStrValue.end(); pNew++)
				{
					fputc('&',pFile);
					fputs(pNew->c_str(),pFile);		
				}
			}
		}
		fclose(pFile);

		strFileName = "data\\" ;
		strFileName.append(g_strCurrentDBName);
		strFileName += "\\" ;
		strFileName.append(pStrTableName);
		strFileName += ".lid";
		//pStrTableName
		if(remove(strFileName.c_str()) == -1)
		{
			printf("无法Delete该索引\nFile Path[%s]\n",strFileName.c_str());
			return false;
		}

		FILE *fp;
		if((fp=fopen(strFileName.c_str(),"ab+"))==NULL)
		{	
			printf("创建索引文件失败\n");
			return false;
		}
		
		int count = 0;
		for(pData = vIndexHead.begin(); pData != vIndexHead.end(); pData++)
		{
			fputc('&',fp);
			fputc('&',fp);
			fputs(pData->strValue.c_str(),fp);
			fputc('&',fp);
			fputs(pData->strPosition.c_str(),fp);
			if(pData->bFlag)
			{
				count++;

			}
		}
		fclose(fp);
		printf("共影响了[%d] 条记录\n",count);
	}
	return true;
}
//==================有关数据表与索引表的操作库-API────────────────────────
//将索引文件中的结果读入指定的结构体中
bool ReadIndexRecord(vector<IndexData> &vIndexHead,const char *pFileName)
{
	if(pFileName == NULL)
	{
		printf("File Name is NULL \n");
		return false;
	}
	string strFileName ;	
	strFileName = "data\\" ;
	strFileName.append(g_strCurrentDBName);
	strFileName += "\\" ;
	strFileName.append(pFileName);
	strFileName += ".lid";

//	printf("===[%s]=\n",strFileName.c_str());
	FILE *pFile;
	if((pFile = fopen(strFileName.c_str(),"rb")) == NULL)
	{
		printf("打开文件失败[%s]\n",strFileName.c_str());
		return false;
	}
	
	char ch = fgetc(pFile);
    //	printf("start parse[%c] ..............\n",ch);
	while(ch != -1 && !feof(pFile))
	{	
	  // printf("start hu parse[%c] ..............\n",ch);
       char next = fgetc(pFile);
	   if((ch =='&'&&next=='&')&&(ch !='\0'&&next!='\0'))
	   {
           IndexData node;
		   ch = fgetc(pFile);
		   while(ch !='\0'&&ch!='&')
		   {
			   if(ch=='\\')
				   ch = fgetc(pFile);
				node.strValue += ch;
			    ch = fgetc(pFile);
		   }
		
		   ch = fgetc(pFile);
		  // printf("chis = %c\n",ch);
           while(ch !='\0' && ch!='&' && ch != -1)
		   {
			   if(ch=='\\')
				   ch = fgetc(pFile);
               node.strPosition += ch;

			   if(!feof(pFile))
				 ch = fgetc(pFile);
			   else
				  break;
		   }		   
		   vIndexHead.push_back(node);
		  // printf("[%s] [%c] [%d]\n",node.strPosition.c_str(),ch,ch);
		 }
	}
	fclose(pFile);
	return true;
}
//将数据值读入指定的结果集中
bool ReadDataRecord(RecordNodeSet *pRecordSet,const char *pFileName)
{
	if(pFileName == NULL)
	{
		printf("File Name is NULL \n");
		return false;
	}

	ReadFiledListStruct(pRecordSet->vFieldNode,pFileName);

	string strFileName ;	
	strFileName = "data\\" ;
	strFileName.append(g_strCurrentDBName);
	strFileName += "\\" ;
	strFileName.append(pFileName);
	strFileName += ".ltd";


	FILE *pFile;
	if((pFile = fopen(strFileName.c_str(),"rb")) == NULL)
	{
		return false;
	}
	
	string strValue;
	RecordNode rNode;
	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);					
					pRecordSet->AddRecord(rNode);
					rNode.vStrValue.clear();
					strValue = "";
				}

			}
			else
			{
				//下一个字段信息
				rNode.vStrValue.push_back(strValue);
				strValue = "";
				strValue += nextCh;
			}
			break;
		case '\\':
			strValue += fgetc(pFile);
			break;
		case EOF:
			rNode.vStrValue.push_back(strValue);
			pRecordSet->AddRecord(rNode);
			return true;
		default:
			strValue += ch;
			break;
		}		
	}
	fclose(pFile);
	return true;
}
//---------------------------操作API----------------
void ProcessCreateDB(vector<string> &pCommandField)
{
	if(!CheckDBSQLRule(pCommandField))
	{
		printf("当前的SQL语句,Create Database不符合规则\n");
		return ;
	}

	if(IsDBExist(pCommandField[2].c_str()))
	{
		printf("当前要创建的数据库与原有数据库重名[%s]\n",pCommandField[2].c_str());
		return ;
	}
	if(!CheckRuleForString(pCommandField[2].c_str()))
	{
		printf("数据库名[%s]不合法\n",pCommandField[2].c_str());
		return ;
	}
	
	if(CreateDataBase(pCommandField[2].c_str()))
	{
		printf("数据库[%s]创建成功\n",pCommandField[2].c_str());
	}
	else
	{
		printf("数据库[%s]创建失败\n",pCommandField[2].c_str());
	}
}
void ProcessDropDB(vector<string> &pCommandField)
{
	if(!CheckDBSQLRule(pCommandField))
	{
		printf("当前的SQL语句,Drop Database不符合规则\n");
		return ;
	}

	if(!IsDBExist(pCommandField[2].c_str()))
	{
		printf("不存在这个[%s]数据库\n",pCommandField[2].c_str());
		return ;
	}
	if(DropDatabase(pCommandField[2].c_str()))
	{
		printf("数据库[%s]删除成功\n",pCommandField[2].c_str());
	}
	else
	{
		printf("数据库[%s]删除失败\n",pCommandField[2].c_str());
	}
}
//用于创建一张表
void ProcessCreateTable(vector<string> &pCommandField)
{
	if(pCommandField.size() < 7)
	{
		printf("Create Table 失败,参数不正确");
		return ;
	}
	if(strlen(g_strCurrentDBName) <= 0)
	{
		printf("当前没有选中的数据库\n");
		return ;
	}

	vector<FieldNode> pFieldNode;
	if(!ParseCreateTable(pFieldNode,pCommandField))
	{
		printf("解析SQL,Create Table失败\n");
		return ;
	}

	if(!ExecCreateTable(pCommandField[2].c_str(),pFieldNode))
	{
		printf("执行Create Table失败\n");
		return ;
	}
	else
	{
		printf("执行Create Table成功\n");
	}
}
//用于删除一张表
void ProcessDropTable(vector<string> &pCommandField)
{
	if(pCommandField.size() != 3)
	{
		printf("Drop Table 失败,参数不正确");
		return ;
	}
	if(strlen(g_strCurrentDBName) <= 0)
	{
		printf("当前没有选中的数据库\n");
		return ;
	}

	if(!DropTable(pCommandField[2].c_str()))
	{
		printf("删除表[%s]失败\n",pCommandField[2].c_str());
		return ;
	}
	else
	{
		printf("删除表[%s]成功\n",pCommandField[2].c_str());
	}
}

void ProcessInsert(vector<string> &pCommandField)
{
	if(pCommandField.size() < 7)
	{
		printf("Insert into 失败,参数不正确");
		return ;
	}
	if(strlen(g_strCurrentDBName) <= 0)
	{
		printf("当前没有选中的数据库\n");
		return ;
	}

	vector<string> pFieldList;
	vector<string> pValueList;
	if(!ParseInsertRecord(pCommandField,pFieldList,pValueList))
	{
		printf("解析SQL出错[processInsert]\n");
		return ;
	}
	if(!ExecInsertRecord(pCommandField[2].c_str(),pFieldList,pValueList))
	{
		printf("执行SQL出错[processInsert]\n");
		return ;
	}
	printf("插入成功\n");
}
void ProcessSelect(vector<string> &pCommandField)
{
	if(pCommandField.size() >= 4)
	{
		RecordNodeSet vRecordSet;
		Tree pTree;
		string strTableName;
		if(!ParseSelectRecord(pCommandField,vRecordSet,&pTree,strTableName))
		{
			printf("解析Select语句时出错\n");
			return;
		}
		if(!ExecSelectRecord(strTableName.c_str(),vRecordSet,&pTree))
		{
			printf("执行Select语句时出错\n");
			return;
		}
		vRecordSet.lCount = vRecordSet.vRecordList.size();
		if(vRecordSet.bIsAllField)
		{
			vector<FieldNode>::iterator pData;
			printf("编号	");
			for(pData = vRecordSet.vFieldNode.begin(); pData != vRecordSet.vFieldNode.end(); pData++)
			{
				printf("%s	",pData->strFieldName);
			}
			printf("\n");
			for(int i = 0; i < vRecordSet.lCount; i++)
			{
				printf("%d	",i+1);
				for(int j = 0; j < vRecordSet.vRecordList[i].vStrValue.size();

⌨️ 快捷键说明

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