📄 common.cpp
字号:
{
strValue += pIndex[i];
}
printf("文本值为[%s]\n",strValue.c_str());
return true;
}
return false;
}
//检查字段名是否在当前的字段列表中存在
int FindFieldExist(const char *pstrFieldName,vector<string> &pFieldList)
{
vector<string>::iterator pData;
int i = 0;
for(pData = pFieldList.begin(); pData != pFieldList.end(); pData++,i++)
{
if(strcmp(pData->c_str(),pstrFieldName) == 0)
return i;
}
return false;
}
bool CheckRuleForInsertField(vector<FieldNode> &pFieldSet,vector<string> &pFieldList,vector<string> &pValueList,vector<string> &pValue)
{
//若为0,则只进行值的合法性和个数的检查
if(pFieldList.size() == 0)
{
if(pFieldSet.size() != pValueList.size())
{
printf("要插入的值个数不匹配\n");
return false;
}
vector<FieldNode>::iterator pData ;
pData = pFieldSet.begin();
for(int i = 0; i < pFieldSet.size(); i++,pData++)
{
printf("开始进行第%d次操作\n",i+1);
string strValue;
if(!pData->bIsAllowNull && pValueList[i].length() == 0)
{
printf("当前字段[%s]的值不符合规则,不能为空\n",pData->strFieldName);
return false;
}
printf("开始switch第%d次操作\n",i+1);
switch(pData->iFieldType)
{
case VARCHAR_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
if(strValue.length() > pData->iValueLength)
{
printf("字段[%s]值[%s]越界\n",pData->strFieldName,strValue.c_str());
return false;
}
pValue.push_back(strValue);
break;
case INT_TYPE:
if(!CheckRuleForNum(pValueList[i].c_str()))
{
printf("字段[%s]值[%s]不符合整数规则\n",pData->strFieldName,strValue.c_str());
return false;
}
if(pData->bIsCheck)
{
long lValue = atol(pValueList[i].c_str());
if(!(lValue >= pData->lStartValue && lValue <= pData->IEndValue))
{
printf("输入的整形[%ld]没有通过规则检查,规则下限[%ld],规则上限[%ld]\n",lValue,pData->lStartValue,pData->IEndValue);
return false;
}
}
pValue.push_back(pValueList[i]);
break;
case TEXT_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
pValue.push_back(strValue);
break;
case DATE_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
pValue.push_back(strValue);
break;
default:
printf("不能识别的字段类型\n");
return false;
}
}
return true;
}
else if(pFieldList.size() > 0)
{
vector<FieldNode>::iterator pData;
int i = 0;
for(pData = pFieldSet.begin(); pData != pFieldSet.end(); pData++)
{
i = FindFieldExist(pData->strFieldName,pFieldList);
if(i < 0)
{
if(pData->bIsAllowNull)
{
if(pData->iFieldType == INT_TYPE)
pValue.push_back("0");
else
pValue.push_back(" ");
}
else
{
printf("要操作的字段值不能为空[%s]\n",pData->strFieldName);
return false;
}
}
else
{
string strValue;
if(!pData->bIsAllowNull && pValueList[i].length() == 0)
{
printf("当前字段[%s]的值不符合规则,不能为空\n",pData->strFieldName);
return false;
}
switch(pData->iFieldType)
{
case VARCHAR_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
if(strValue.length() > pData->iValueLength)
{
printf("字段[%s]值[%s]越界\n",pData->strFieldName,strValue.c_str());
return false;
}
pValue.push_back(strValue);
break;
case INT_TYPE:
if(!CheckRuleForNum(pValueList[i].c_str()))
{
printf("字段[%s]值[%s]不符合整数规则\n",pData->strFieldName,strValue.c_str());
return false;
}
if(pData->bIsCheck)
{
long lValue = atol(pValueList[i].c_str());
if(!(lValue >= pData->lStartValue && lValue <= pData->IEndValue))
{
printf("输入的整形[%ld]没有通过规则检查,规则下限[%ld],规则上限[%ld]\n",lValue,pData->lStartValue,pData->IEndValue);
return false;
}
}
pValue.push_back(pValueList[i]);
break;
case TEXT_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
pValue.push_back(strValue);
break;
case DATE_TYPE:
if(!CheckRuleForInsertTextType(strValue,pValueList[i]))
{
printf("字段[%s]值[%s],要插入的文本不符合规则\n",pData->strFieldName,pValueList[i].c_str());
return false;
}
pValue.push_back(strValue);
break;
default:
printf("不能识别的字段类型\n");
return false;
}
}
}
return true;
}
return false;
}
//具体执行该函数,将字段列表插入到当前的结果集中
bool ExecInsertRecord(const char *pStrTableName,vector<string> &pFieldList,vector<string> &pValueList)
{
//检查表是否存在
if(!IsTableExist(pStrTableName))
{
printf("表[%s]不存在\n",pStrTableName);
return false;
}
//获取表结构信息
vector<FieldNode> vFieldSet;
ReadFiledListStruct(vFieldSet,pStrTableName);
if(vFieldSet.size() <= 0)
{
printf("表[%s]中没有字段信息\n",pStrTableName);
return false;
}
//验证结构信息是否合法,对于待插入的数据
vector<string> pValue;//保存当前的结果
if(!CheckRuleForInsertField(vFieldSet,pFieldList,pValueList,pValue))
{
printf("检查字段合法性失败,请检查\n");
return false;
}
//向两个表中插入相关的信息
//先检查关键字段
string strPrimaryFieldName;
int iIndex;//用于游标,来提示当前的值
for(iIndex = 0; iIndex < vFieldSet.size(); iIndex++)
{
if(vFieldSet[iIndex].bIsPrimaryKey)
{
strPrimaryFieldName = vFieldSet[iIndex].strFieldName;
break;
}
}
char strFileName[2048] = "data\\";
strcat(strFileName,g_strCurrentDBName);
strcat(strFileName,"\\");
strcat(strFileName,pStrTableName);
char *pIndexFile = (char *)malloc(strlen(strFileName)+4);
char *pDataFile = (char *)malloc(strlen(strFileName)+4);
strcpy(pIndexFile,strFileName);
strcat(pIndexFile,".lid");
strcpy(pDataFile,strFileName);
strcat(pDataFile,".ltd");
FILE *fpIndex;
FILE *fpData;
if((fpIndex = fopen(pIndexFile,"ab+"))==NULL)
{
printf("打开索引文件失败\n");
return false;
}
if((fpData = fopen(pDataFile,"ab+"))==NULL)
{
printf("打开数据文件失败\n");
return false;
}
fseek(fpData,0L,SEEK_END);
//此处要添加去重操作,即在每个表的主键的值不可以重复
fputs("&&",fpIndex);
long fileposition=ftell(fpData);
fputs(pValue[iIndex].c_str(),fpIndex);
fputc('&',fpIndex);
char str[100];
sprintf(str,"%ld",fileposition);
fputs(str,fpIndex);
fclose(fpIndex);
///针对数据文件
vector<string>::iterator pData;
fputs("&",fpData);
for(pData = pValue.begin(); pData != pValue.end(); pData++)
{
fputs("&",fpData);
fputs(pData->c_str(),fpData);
}
fclose(fpData);
}
/*
#define BIG_OPERATOR 13
#define 14
#define 15
#define 16
#define 17
#define 18
#define 19
#define BWTEEN_OPERATOR 20
#define AND_OPERATOR 21
#define OR_OPERATOR 22
*/
//用于处理单个语句的增加
int AnalysisSingleCondition(vector<string> &pCommandField,int iIndex,TreeNode *pNode,int iTableIndex/*table 索引*/)
{
if(!CheckRuleForString(pCommandField[iIndex].c_str()))
{
printf("分析的字段不符合规则[%s]\n",pCommandField[iIndex].c_str());
return false;
}
if(!IsFieldExist(pCommandField[iIndex].c_str(),pCommandField[iTableIndex].c_str()))
{
printf("分析的字段不存在[%s]\n",pCommandField[iIndex].c_str());
return false;
}
pNode->strField = pCommandField[iIndex].c_str();
if(++iIndex >= pCommandField.size())
{
printf("后面要一个操作符\n");
return false;
}
switch(QueryKeyType(pCommandField[iIndex].c_str()))
{
case BIG_OPERATOR:
pNode->iProcessType = BIG_OPERATOR;
break;
case EQUAL_OPERATOR:
pNode->iProcessType = EQUAL_OPERATOR;
break;
case NOEQUAL_OPERATOR:
pNode->iProcessType = NOEQUAL_OPERATOR;
break;
case BIG_EQUAL_OPERATOR:
pNode->iProcessType = BIG_EQUAL_OPERATOR;
break;
case SMALL_OPERATOR:
pNode->iProcessType = SMALL_OPERATOR;
break;
case SMALL_EQUAL_OPERATOR:
pNode->iProcessType = SMALL_EQUAL_OPERATOR;
break;
case LIKE_OPERATOR:
pNode->iProcessType = LIKE_OPERATOR;
break;
case BWTEEN_OPERATOR: //between语句
pNode->iProcessType = BWTEEN_OPERATOR;
if(iIndex >= pCommandField.size()-3)
{
printf("between 后面需要操作符\n");
return false;
}
pNode->strValue = pCommandField[++iIndex].c_str();
if(strcmp(pCommandField[++iIndex].c_str(),"and") != 0)
{
printf("缺少And符\n");
return false;
}
pNode->strEndValue = pCommandField[++iIndex].c_str();
return iIndex;
default:
return false;
}
pNode->strValue = pCommandField[++iIndex].c_str();
return iIndex;
}
//进行Where语句的分析
bool AnalysisWhereStatment(vector<string> &pCommandField,struct Tree *pTree,int iCurIndex,int iTableIndex)
{
if(strcmp(pCommandField[iCurIndex].c_str(),"where") != 0)
{
printf("没有找到指定的Where语句\n");
return false;
}
for(int iIndex = iCurIndex+1; iIndex < pCommandField.size();iIndex++)
{
TreeNode node;
iIndex = AnalysisSingleCondition(pCommandField,iIndex,&node,iTableIndex);
if(iIndex == false)
{
printf("当前出现解析结点出错\n");
return false;
}
if(iIndex == pCommandField.size())
{
return true;
}
switch(QueryKeyType(pCommandField[iIndex].c_str()))
{
case AND_OPERATOR: // and语句
pTree->vAndSet.push_back(node);
break;
case OR_OPERATOR: //OR语句
pTree->vOrSet.push_back(node);
break;
default:
if((iIndex+1) == pCommandField.size())
{
pTree->vAndSet.push_back(node);
return true;
}
printf("出现缺省情况\n");
return false;
}
}
return true;
}
//定义删除记录
bool ParseDeteleRecord(vector<string> &pCommandField,Tree *pTree)
{
//delete from xxx where name = '',"",23 and name > name2;
if(!IsTableExist(pCommandField[2].c_str()))
{
printf("当前没有要操作的表[%s]\n",pCommandField[2].c_str());
return false;
}
if(!AnalysisWhereStatment(pCommandField,pTree,3))
{
printf("where语句有问题\n");
return false;
}
return true;
}
//执行删除操作
bool ExecDeleteRecord(const char *pStrTableName,Tree *pTree)
{
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;
}
printf("Exece this \n");
//进行数据分析
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;
bModifyFlag = true;
}
break;
case EQUAL_OPERATOR:
if(strValue == pData->strValue)
{
pData->bFlag = true;
bModifyFlag = true;
}
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;
}
}
//进行数据修改的操作,其操作办法是重写当前的值
if(bModifyFlag)
{
string strFileName ;
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++)
{
if(!pData->bFlag)
{
fputc('&',fp);
fputc('&',fp);
fputs(pData->strValue.c_str(),fp);
fputc('&',fp);
fputs(pData->strPosition.c_str(),fp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -