您的位置:首页 > 数据库

如何解析类SQL语句,并提取参数

2013-09-23 17:40 916 查看
因为做的是XML数据库,但是必须能够兼容SQL,故当用户按照一定的要求,输入SQL语句后,XML数据库,会将SQL语句传入MYSQL或ORACLE等SQL数据库,当返回查询结果后,我们会在执行引擎层面,将结果以及SQL语句中的参数进行封装,形成一个XML片段,并将该片段返回出来,其中涉及到SQL语句中参数的提取问题,才疏学浅,只想到了一种办法,并且其中还存在着一个问题,暂时没有想到解决方法,由于急用,只能将就。



<1> 利用string相关函数去除参数中的所有引号(XML文档对于数据的引号有要求,不能双引号或者单引号嵌套,简单起见,直接去除,不会影响用户的使用)

void drop_stringquotation(std::string &Argument)
{
const char singlequote = '\'';// In execSQL querystate , the string of sql must use the singlequote rather than doublequote
size_t searchCursor = 0;
while(searchCursor <= Argument.size())
{
searchCursor = Argument.find(singlequote,searchCursor);
if(searchCursor >= 0 && searchCursor <= Argument.size())
{
Argument.erase(searchCursor,1);
}
else
{
break;
}
}
}


<2>利用string相关函数及<algorithm>中Count函数识别SQL语句中的参数间的逗号,从而将参数提取出来

void string_split(const std::string &str, const char &split_charactor, std::vector<std::string> &result)
{
size_t Split_B = 0;
size_t Split_E = 0;
size_t Split_C = 0;
uint64 nLBCount = 0;
uint64 nRBCount = 0;
uint64 nSQCount = 0;
const char Lbracket = '(';
const char Rbracket = ')';
const char singlequote = '\'';
string strSegment = "";
string Argument = "";

while(Split_B <= str.size())
{
Split_E = str.find(split_charactor,Split_C);
if(Split_E >= 0 && Split_E <= str.size())
{
strSegment = str.substr(Split_C, Split_E-Split_C);
nLBCount += std::count(strSegment.begin(),strSegment.end(),Lbracket);
nRBCount += std::count(strSegment.begin(),strSegment.end(),Rbracket);
nSQCount += std::count(strSegment.begin(),strSegment.end(),singlequote);
if(nLBCount == nRBCount && nSQCount % 2 == 0)
{
Argument = str.substr(Split_B,Split_E-Split_B);
drop_stringquotation(Argument);
result.push_back(Argument);
Split_B = Split_E + 1;
}
Split_C = Split_E + 1;
}
else
{
break;
}
}
Argument = str.substr(Split_B, str.size());
drop_stringquotation(Argument);
result.push_back(Argument);
}


存在的问题:

select concat(substr('100(38888',3,8),'tes),t') from dual


如何有效提取该SQL语句参数仍待解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: