improved sql parser.

This commit is contained in:
wenshao
2017-06-04 21:30:23 +08:00
parent de4bfb0ae3
commit 21ae9b5722
63 changed files with 1049 additions and 350 deletions
@@ -56,19 +56,20 @@ public class SQLServerInsertTest7 extends TestCase {
SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = "INSERT INTO MMS_SETTLEMENT_COM"
+ "\n\t(handler, handleTime, MID, MERCHANTNAME, TOTALAMT"
+ "\n\t, ACTUALAMT, paymentMoney, STATUS, SERIAL_NUM, REMARKS)"
+ "\nSELECT 'admin', getdate(), MID, MERCHANTNAME, SUM(CONVERT(DECIMAL(18, 2), isnull(TOTALAMT, 0))) AS TOTALAMT"
+ "\n\t, SUM(CONVERT(DECIMAL(18, 2), isnull(ACTUALAMT, 0))) AS ACTUALAMT, SUM(CONVERT(DECIMAL(18, 2), isnull(paymentMoney, 0))) AS paymentMoney, 2, 126, ("
+ "\n\t\tSELECT REMARKS + ''"
+ "\n\t\tFROM MMS_SETTLEMENT_COM"
+ "\n\t\tWHERE MID = A.MID"
+ "\n\t\tFOR XML PATH('')"
+ "\n\t\t) AS REMARKS"
+ "\nFROM MMS_SETTLEMENT_COM A"
+ "\nWHERE ID IN (304, 305, 306, 297, 108, 184)"
+ "\nGROUP BY MID, MERCHANTNAME";
String formatSql = "INSERT INTO MMS_SETTLEMENT_COM\n" +
"\t(handler, handleTime, MID, MERCHANTNAME, TOTALAMT\n" +
"\t, ACTUALAMT, paymentMoney, STATUS, SERIAL_NUM, REMARKS)\n" +
"SELECT 'admin', getdate(), MID, MERCHANTNAME, SUM(CONVERT(DECIMAL(18, 2), isnull(TOTALAMT, 0))) AS TOTALAMT\n" +
"\t, SUM(CONVERT(DECIMAL(18, 2), isnull(ACTUALAMT, 0))) AS ACTUALAMT, SUM(CONVERT(DECIMAL(18, 2), isnull(paymentMoney, 0))) AS paymentMoney, 2, 126\n" +
"\t, (\n" +
"\t\tSELECT REMARKS + ''\n" +
"\t\tFROM MMS_SETTLEMENT_COM\n" +
"\t\tWHERE MID = A.MID\n" +
"\t\tFOR XML PATH('')\n" +
"\t) AS REMARKS\n" +
"FROM MMS_SETTLEMENT_COM A\n" +
"WHERE ID IN (304, 305, 306, 297, 108, 184)\n" +
"GROUP BY MID, MERCHANTNAME";
Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}
@@ -76,7 +76,7 @@ public class SQLServerSelectTest13 extends TestCase {
"\t\t\tFROM config.sys_params t\n" +
"\t\t\tWHERE t.param_type = ?\n" +
"\t\t\t\tAND t.param_value = a.tran_type\n" +
"\t\t\t) AS tranTypeName\n" +
"\t\t) AS tranTypeName\n" +
"\tFROM bussiness.account_detail a\n" +
"\tWHERE 1 = 1\n" +
"\t\tAND a.time_add >= ?\n" +
@@ -75,7 +75,7 @@ public class SQLServerSelectTest14 extends TestCase {
"\t\t\tFROM config.sys_params t\n" +
"\t\t\tWHERE t.param_type = 2\n" +
"\t\t\t\tAND t.param_value = a.tran_type\n" +
"\t\t\t) AS tranTypeName\n" +
"\t\t) AS tranTypeName\n" +
"\tFROM bussiness.account_detail a\n" +
"\tWHERE 1 = 1\n" +
"\t\tAND a.time_add >= 2\n" +
@@ -62,49 +62,50 @@ public class SQLServerSelectTest7 extends TestCase {
"\n )" +
"\n order by t.sort_order";
String expect = "WITH" +
"\n\tmenu_view" +
"\n\tAS" +
"\n\t(" +
"\n\t\tSELECT t.*, 1 AS level" +
"\n\t\tFROM sec_portal_menu t" +
"\n\t\tWHERE t.parent_id = ?" +
"\n\t\tUNION ALL" +
"\n\t\tSELECT t.*, level + 1" +
"\n\t\tFROM sec_portal_menu t, menu_view x" +
"\n\t\tWHERE t.parent_id = x.menu_id" +
"\n\t)" +
"\nSELECT t.menu_id AS \"id\", t.menu_name AS \"name\", t.parent_id AS \"pId\", CASE t.level WHEN 1 THEN 'true' ELSE 'false' END AS \"open\", t.link_type AS \"linkType\"" +
"\nFROM menu_view t" +
"\nWHERE 1 = 1" +
"\n\tAND t.deleted = 0" +
"\n\tAND t.link_type IN ('simple', 'link')" +
"\n\tAND (EXISTS (" +
"\n\t\t\tSELECT p.entity_code" +
"\n\t\t\tFROM sec_role_auth p" +
"\n\t\t\tWHERE p.entity_code = t.menu_id" +
"\n\t\t\t\tAND p.entity_type = 'menu'" +
"\n\t\t\t\tAND p.role_id IN (SELECT r.role_code" +
"\n\t\t\t\t\tFROM sec_role_member rm, sec_role r" +
"\n\t\t\t\t\tWHERE rm.entity_type = 'user'" +
"\n\t\t\t\t\t\tAND entity_code = ?" +
"\n\t\t\t\t\t\tAND r.role_id = rm.role_id" +
"\n\t\t\t\t\t\tAND r.enabled = 1" +
"\n\t\t\t\t\t\tAND r.deleted = 0)"+
"\n\t\t\t)" +
"\n\t\tOR '1' = ?" +
"\n\t\tOR t.need_control = 0)" +
"\n\tAND (t.enabled = 1" +
"\n\t\tOR '1' = ?" +
"\n\t\tOR t.need_control = 0)" +
"\nORDER BY t.sort_order";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
String text = TestUtils.outputSqlServer(stmt);
Assert.assertEquals(expect, text);
assertEquals("WITH\n" +
"\tmenu_view\n" +
"\tAS\n" +
"\t(\n" +
"\t\tSELECT t.*, 1 AS level\n" +
"\t\tFROM sec_portal_menu t\n" +
"\t\tWHERE t.parent_id = ?\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT t.*, level + 1\n" +
"\t\tFROM sec_portal_menu t, menu_view x\n" +
"\t\tWHERE t.parent_id = x.menu_id\n" +
"\t)\n" +
"SELECT t.menu_id AS \"id\", t.menu_name AS \"name\", t.parent_id AS \"pId\", CASE t.level WHEN 1 THEN 'true' ELSE 'false' END AS \"open\", t.link_type AS \"linkType\"\n" +
"FROM menu_view t\n" +
"WHERE 1 = 1\n" +
"\tAND t.deleted = 0\n" +
"\tAND t.link_type IN ('simple', 'link')\n" +
"\tAND (EXISTS (\n" +
"\t\t\tSELECT p.entity_code\n" +
"\t\t\tFROM sec_role_auth p\n" +
"\t\t\tWHERE p.entity_code = t.menu_id\n" +
"\t\t\t\tAND p.entity_type = 'menu'\n" +
"\t\t\t\tAND p.role_id IN (\n" +
"\t\t\t\t\tSELECT r.role_code\n" +
"\t\t\t\t\tFROM sec_role_member rm, sec_role r\n" +
"\t\t\t\t\tWHERE rm.entity_type = 'user'\n" +
"\t\t\t\t\t\tAND entity_code = ?\n" +
"\t\t\t\t\t\tAND r.role_id = rm.role_id\n" +
"\t\t\t\t\t\tAND r.enabled = 1\n" +
"\t\t\t\t\t\tAND r.deleted = 0\n" +
"\t\t\t\t)\n" +
"\t\t)\n" +
"\t\tOR '1' = ?\n" +
"\t\tOR t.need_control = 0)\n" +
"\tAND (t.enabled = 1\n" +
"\t\tOR '1' = ?\n" +
"\t\tOR t.need_control = 0)\n" +
"ORDER BY t.sort_order", text);
// System.out.println(text);
}
@@ -45,10 +45,10 @@ public class SQLServerUpdateTest3 extends TestCase {
"\n\t\t\tSELECT MAX(OrderDate)" +
"\n\t\t\tFROM Sales.SalesOrderHeader so2" +
"\n\t\t\tWHERE so2.SalesPersonID = so.SalesPersonID" +
"\n\t\t\t)" +
"\n\t\t)" +
"\n\t\tAND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" +
"\n\tGROUP BY so.SalesPersonID" +
"\n\t)";
"\n)";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);