コード例 #1
0
 public string LexSampleSQL(string querySQL)
 {
     return SQLLexer.LexSampleSQL(querySQL);
 }
コード例 #2
0
        public void TestLexSampleSQL()
        {
            string[][] testcases = new string[][] {
                new string[] {
                    "select * from A where a=b and c=d",
                    "select * from A where 1=0 and a=b and c=d"
                },
                new string[] {
                    "select * from A where 1=0",
                    "select * from A where 1=0 and 1=0"
                },
                new string[] {
                    "select * from A",
                    "select * from A where 1=0"
                },
                new string[] {
                    "select * from A group by x",
                    "select * from A where 1=0 group by x"
                },
                new string[] {
                    "select * from A having a>b",
                    "select * from A where 1=0 having a>b"
                },
                new string[] {
                    "select * from A order by d",
                    "select * from A where 1=0 order by d"
                },
                new string[] {
                    "select * from A group by a having b>c order by d",
                    "select * from A where 1=0 group by a having b>c order by d"
                },
                new string[] {
                    "select * from A where (7<4) group by a having b>c order by d",
                    "select * from A where 1=0 and (7<4) group by a having b>c order by d"
                },
                new string[] {
                    "select * from A union select * from B",
                    "select * from A  where 1=0 union  select * from B where 1=0"
                },
                new string[] {
                    "select * from A where a=2 union select * from B where 2=3",
                    "select * from A where 1=0 and a=2 union  select * from B where 1=0 and 2=3"
                },
                new string[] {
                    "select * from A union select * from B union select * from C",
                    "select * from A  where 1=0 union  select * from B  where 1=0 union  select * from C where 1=0"
                },
            };

            for (int i = 0; i < testcases.Length; i++)
            {
                string result = null;
                try {
                    result = SQLLexer.LexSampleSQL(testcases[i][0]).Trim();
                } catch (Exception e) {
                    while (e != null)
                    {
                        Console.WriteLine($"Exception: {e.GetType().Name}");
                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine("----------------------------------------");
                        e = e.InnerException;
                    }

                    Assert.Fail("failed case with exception:" + testcases[i][0]);
                }
                string expected = testcases[i][1].Trim();
                Assert.AreEqual(expected, result, "failed case " + i + " :" + testcases[i][0]);
            }
        }