使用C#在SQL中解析嵌套块

时间:2009-02-24 12:15:06

标签: c# sql parsing

我想对SQL代码执行分析。 在这里我想分开SQL代码中的块。 例如,在下面的代码中

for condition1 loop
stmt1;
for condition2 loop
stmt2;
end loop;
end loop;

我想将这两个块分开并创建一个包含外部循环块的数据结构,并且还将具有内部循环块。

这是最好的方法吗?是否有任何解析器可以以最佳方式处理嵌套循环?

2 个答案:

答案 0 :(得分:2)

我建议使用这种块的合成模式(以及SELECT,CASE,WITH等)。

public interface ICodeEntity { }
public interface IObjectEntity { }
public class Column: IObjectEntity
{
    public string name;
    public System.Data.DbType type;
}
public class Table: IObjectEntity
{
    public List<Column> columns = new List<Column>();
    public string alias;
}
public class Where : ICodeEntity { }
public class GroupBy : ICodeEntity { }
public class OrderBy : ICodeEntity { }
public class Select : Table, ICodeEntity
{
    public List<Table> joinList = new List<Table>();
    public Where where;
    public GroupBy groupBy;
    public OrderBy orderBy;
}
public class Condition : ICodeEntity { }
public class If : ICodeEntity
{
    public Condition condition;
    public List<ICodeEntity> codeList = new List<ICodeEntity>();
}

以及该计划的某个地方:

If if1 = new If();
if1.codeList.Add(new If());

见相关链接:
CodeProject: Illustrated GOF Design Patterns in C#
data&object factory: Composite pattern

答案 1 :(得分:0)

应该已经有SQL Parser可以为您自动执行此操作,您无需重新发明轮子。只是google“sql parser”。