【发布时间】:2014-10-03 16:21:40
【问题描述】:
这里有点难过。我面前的任务是获取一个存储在字符串中的 SQL 查询(假设它现在是一个有效的查询);将“选择列表”存储在字符串数组中,每个数组元素与选择列表项相关;最后将“from 子句”存储在它自己的字符串变量中。我觉得练习是编写自己的 SQL 查询解析器,但我不知道 SQL Server 是如何解析查询的。非常感谢任何可以提供帮助的资源方向。
我已经能够使用蛮力方法解决问题,我执行以下操作:
1 - 通过启动一个循环来查找 from 子句,该循环查找单词“from”的后续实例,直到索引位置之后的所有内容都可以使用“select *”选择列表
private string GetFromClause(string selectstatement,string connectionString)
{
string teststatement = selectstatement;
int startindex = GetFirstFromIndex(teststatement);
while (startindex != -1)
{
teststatement=teststatement.Substring(startindex);
if (DoesSelectWork(string.Format("select * {0}", teststatement)
, connectionString))
return teststatement;
else
startindex = GetNextFromIndex(teststatement);
}
throw new ReportException("Could not find From Clause");
}
2- 从传递的查询中删除现在找到的“from 子句”;使用 .split(',') 将剩余的字符串放在一个数组中。现在遍历数组并使用找到的“from 子句”测试每个数组元素。如果测试通过,我们就有一个有效的选择列表项;如果不是,我们想将此元素与下一个数组元素组合,并继续这样做直到测试通过(以处理将逗号引入选择列表语法的转换语句等)
private string[] GetSelectFields(string query,
string fromclause,
string connectionString)
{
int index = query.IndexOf(fromclause);
string fromless = (index < 0)
? query
: query.Remove(index, fromclause.Length);
fromless = fromless.Substring(fromless.IndexOf("SELECT") + "SELECT".Length);
List<string> finalselect = new List<string>();
string[] selectFields = fromless.Split(',');
index = 0;
string currentString = string.Empty;
while (index<selectFields.Length)
{
currentString += selectFields[index];
if (DoesSelectWork(string.Format("select {0} {1}"
, currentString, fromclause), connectionString))
{
finalselect.Add(currentString);
currentString = string.Empty;
}
else {
currentString += ",";
}
index++;
}
return finalselect.ToArray();
}
【问题讨论】:
-
听起来你在测试字符串 is 对我来说是否是一个有效的查询?
-
这里使用存储过程有什么问题?为什么不使用存储过程并进行整个字符串查询?
标签: c# sql-server