将一个 WHERE sql 子句拆分为数组

3

我正在尝试将SQL语句的WHERE子句拆分为一个包含5个输出的数组,其中每个索引下保存以下数据:

0 - The initial clauses (WHERE/AND/OR) plus any open brackets. e.g "AND((("
1 - Either the table the first clause comes from or "VALUE" if its a value. e.g. "transactions". 
2 - The field name or value. e.g. "id"
3 - The joining value. e.g. >
4 - Either the table the second clause comes from or "VALUE" if its a value. e.g. "transactions". 
5 - The field name or value. e.g. "id"
6 - Any closing brackets. e.g. ")))"

例如循环以下字符串将输出以下数组:
WHERE transactions.status_code= 'AFA 2'
AND (transactions.supp_ref = supplier.supp_ref
AND supplier.supp_addr_ref = address.addr_ref)
OR transactions.user_code = user.user_code

output[0] = "WHERE"
output[1] = "transactions"
output[2] = "status_code"
output[3] = "="
output[4] = "VALUE'
output[5] = "AFA 2"
output[6] = ""

output[0] = "AND("
output[1] = "transactions"
output[2] = "supp_ref"
output[3] = "="
output[4] = "supplier"
output[5] = "supp_ref"
output[6] = ""

output[0] = "AND"
output[1] = "supplier"
output[2] = "supp_addr_ref"
output[3] = "="
output[4] = "address"
output[5] = "addr_ref"
output[6] = ")"

output[0] = "OR"
output[1] = "transactions"
output[2] = "user_code"
output[3] = "="
output[4] = "user"
output[5] = "user_code"
output[6] = ""

对于SQL语句的其余部分,我已经成功地使用String.Split方法进行了类似的拆分,但是由于where从句的变化,我在处理这部分时遇到了困难。从周围的环境来看,我认为最好使用正则表达式,但无法确定所需内容。任何帮助或指导将不胜感激。


2
你到底为什么想要做这样的事情? - LoztInSpace
2
虽然我无法回答你的问题本身,但是为什么你需要解析SQL呢?这不是一个简单的任务。你试图解决什么潜在的问题,你将如何处理标记化的SQL? - CodeCaster
如果有BETWEEN、IN或一些子查询怎么办? - Sergey Berezovskiy
2
培训新手学习SQL基础知识会比经历这种做作的过程更加经济实惠。如果您有前端,为何需要解析SQL呢?您会得到失败的结果。从技术角度来看,唯一正确的处理此问题的方法是编写或获取一个SQL解析器,但我认为您的前提是有缺陷的。 - LoztInSpace
我赞同@LoztInSpace的观点。培训新手用户使用SQL真的会更省时间、逻辑和内存等各方面成本。但是如果不可能,可以查看以下链接,也许它提供了你的用户需要的界面。http://executequery.org/index.jsp - Suhani Mody
显示剩余3条评论
2个回答

0

好的,首先我认为正则表达式可能不是你尝试做的最好选择。话虽如此,这里有一个正则表达式可以解析你发布的内容,并将其转换为你想要的格式:

(?<Group>(?<Concat>where|\s*?\)?\s*?and\s*?\(?|\s*?\)?\s*?or\s*?\(?)(?<TableName>[\w\s]+(?=\.))\.?(?<ColName>.+?(?=\=|like|between|\<\>|\>\=|\<\=|in|\>|\<))\s*?(?<Compare>\=|like|between|\<\>|\>\=|\<\=|in|\>|\<)(?<Value>.*?(?=\s*?and\s*?\(*|or\*?\(*)|.*))

我确定这并不涵盖所有情况,而且根据正则表达式解析器的不同,它可能会有所不同。 我使用 The Regulator 进行正则表达式工作。

我建议编写一个执行此操作的解析器。 请看看下面的内容,如果您决定采用这种方法,它可能会有所帮助。 我不完全确定您正在处理那个“VALUE”字符串,但是如果您要识别什么是值以及什么是 table.colName,您可以轻松地将其添加到此中。 要识别类似于 in ('a','b') 的内容将更加困难,但我认为这是可行的。

    //A list of chars that we are going to replace with \s"char"\s this list may not be complete.
    // . is not in here. We will take care of that later.
    static string[] specChars = new string[] { "<", ">", "<=", ">=", "=", "like", "in", "between", "or", "and", "(", ")", "where" };
    static string[] delims = new string[] {"and", "or", "where" };
    static string testData = @"WHERE transactions.status_code= 'AFA 2'
    AND (transactions.supp_ref = supplier.supp_ref
    AND supplier.supp_addr_ref = address.addr_ref)
    OR transactions.user_code = user.user_code";
    static void Main(string[] args)
    {
        Print(Parse(testData));
        Console.ReadKey();
    }

    static List<List<string>> Parse(string input)
    {
        List<List<string>> ret = new List<List<string>>();
        //lets remove all the spaces first becaue we are going to put them back
        //the way we want to see them.
        input = input.Replace(" ", "").Replace("\r", "").Replace("\n", "").ToLower();
        foreach (string item in specChars)
        {
            //this will help clean the string so you can use it
            input = input.Replace(item, string.Format(" {0} ", item));   
        }
        string[] splits = input.Split(' ');

        List<string> currList = null;
        foreach (string item in splits.Where(x => x.Length > 0))
        {
            if (delims.Contains(item))
            {
                if (currList != null)
                {
                    ret.Add(currList);
                    currList = new List<string>();
                    currList.Add(item);
                }
                else
                {
                    currList = new List<string>();
                    currList.Add(item);
                }
            }
            else
            {
                if (item.Contains("."))
                {
                    string[] tmp = item.Split('.');
                    currList.Add(tmp[0]);
                    currList.Add(tmp[1]);
                }
                else
                    currList.Add(item);
            }
        }
        if (currList != null)
            ret.Add(currList);
        return ret;
    }

    static void Print(List<List<String>> input)
    {
        StringBuilder sb = new StringBuilder();
        foreach (List<String> item in input)
        {
            sb.Append("New Chunk:\n");
            foreach (string str in item)
            {
                sb.Append(string.Format("\t{0}\n", str));
            }
            sb.Append("\n");
        }

        Console.WriteLine(sb.ToString());
    }
}

0
如果您想解析SQL,您可能需要查看ScriptDom命名空间。它可能超出了您要做的事情,但它具有一些SQL解析器,可以为您提供有关给定SQL查询的详细信息。
以下是一些资源: MSDN ScriptDOM参考 更简单的介绍

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接