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
@@ -0,0 +1,31 @@
package com.alibaba.druid.bvt.sql;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.parser.SQLExprParser;
import junit.framework.TestCase;
import java.util.List;
/**
* Created by wenshao on 04/06/2017.
*/
public class SplitTest extends TestCase {
public void test_0() throws Exception {
String sql = "((1 + 2) + (3 + 4) + 5) + ((6 + 7) + (8 + 9) + 10)";
// sql = "(1 + 2) + (3 + 4)";
SQLBinaryOpExpr expr = (SQLBinaryOpExpr) new SQLExprParser(sql).expr();
List<SQLExpr> items = SQLBinaryOpExpr.split(expr);
System.out.println(sql);
System.out.println(items);
assertEquals(10, items.size());
for (int i = 0; i < 10; ++i) {
SQLExpr item = items.get(i);
assertEquals(Integer.toString(i + 1), SQLUtils.toSQLString(item));
}
}
}
@@ -0,0 +1,40 @@
package com.alibaba.druid.bvt.sql;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.parser.SQLExprParser;
import junit.framework.TestCase;
import java.util.List;
/**
* Created by wenshao on 04/06/2017.
*/
public class SplitTest2 extends TestCase {
public void test_0() throws Exception {
int count = 10;
String sql = "";
for (int i = 0; i < count; ++i) {
if (sql.length() != 0) {
sql += " + ";
}
sql += Integer.toString(i);
}
// sql = "(1 + 2) + (3 + 4)";
SQLBinaryOpExpr expr = (SQLBinaryOpExpr) new SQLExprParser(sql).expr();
List<SQLExpr> items = SQLBinaryOpExpr.split(expr);
System.out.println(sql);
System.out.println(items);
assertEquals(count, items.size());
for (int i = 0; i < count; ++i) {
SQLExpr item = items.get(i);
assertEquals(Integer.toString(i ), SQLUtils.toSQLString(item));
}
}
}
@@ -69,32 +69,36 @@ public class DB2SelectTest_11 extends DB2Test {
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("SELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100"
+ "\nFROM A, B"
+ "\nWHERE B.F_2211 = '5'"
+ "\n\tAND A.F_0301 = B.F_0301"
+ "\n\tAND (Substr(B.F_0815, 1, 4) CONCAT Substr(B.F_0815, 5, 2)) > A.F_0802"
+ "\n\tAND A.F_2100 > 0"
+ "\nUNION"
+ "\nSELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100"
+ "\nFROM A"
+ "\nWHERE A.F_0301 NOT IN (SELECT F_0301"
+ "\n\t\tFROM B)"
+ "\n\tAND A.F_2100 > 0", //
Assert.assertEquals("SELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100\n" +
"FROM A, B\n" +
"WHERE B.F_2211 = '5'\n" +
"\tAND A.F_0301 = B.F_0301\n" +
"\tAND (Substr(B.F_0815, 1, 4) CONCAT Substr(B.F_0815, 5, 2)) > A.F_0802\n" +
"\tAND A.F_2100 > 0\n" +
"UNION\n" +
"SELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100\n" +
"FROM A\n" +
"WHERE A.F_0301 NOT IN (\n" +
"\t\tSELECT F_0301\n" +
"\t\tFROM B\n" +
"\t)\n" +
"\tAND A.F_2100 > 0", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("select A.F_0201, A.F_0301, A.F_0802, A.F_2100"
+ "\nfrom A, B"
+ "\nwhere B.F_2211 = '5'"
+ "\n\tand A.F_0301 = B.F_0301"
+ "\n\tand (Substr(B.F_0815, 1, 4) concat Substr(B.F_0815, 5, 2)) > A.F_0802"
+ "\n\tand A.F_2100 > 0"
+ "\nunion"
+ "\nselect A.F_0201, A.F_0301, A.F_0802, A.F_2100"
+ "\nfrom A"
+ "\nwhere A.F_0301 not in (select F_0301"
+ "\n\t\tfrom B)"
+ "\n\tand A.F_2100 > 0", //
assertEquals("select A.F_0201, A.F_0301, A.F_0802, A.F_2100\n" +
"from A, B\n" +
"where B.F_2211 = '5'\n" +
"\tand A.F_0301 = B.F_0301\n" +
"\tand (Substr(B.F_0815, 1, 4) concat Substr(B.F_0815, 5, 2)) > A.F_0802\n" +
"\tand A.F_2100 > 0\n" +
"union\n" +
"select A.F_0201, A.F_0301, A.F_0802, A.F_2100\n" +
"from A\n" +
"where A.F_0301 not in (\n" +
"\t\tselect F_0301\n" +
"\t\tfrom B\n" +
"\t)\n" +
"\tand A.F_2100 > 0", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -58,8 +58,8 @@ public class MySqlDeleteTest_3 extends MysqlTest {
"\t\t\t\t\tSELECT *\n" +
"\t\t\t\t\tFROM t5\n" +
"\t\t\t\t) t5)\n" +
"\t\t\t\t)\n" +
"\t\t\t)\n" +
"\t\t)\n" +
"\t)", SQLUtils.toMySqlString(stmt));
assertEquals("delete from t1\n" +
"where s11 > any (\n" +
@@ -77,8 +77,8 @@ public class MySqlDeleteTest_3 extends MysqlTest {
"\t\t\t\t\tselect *\n" +
"\t\t\t\t\tfrom t5\n" +
"\t\t\t\t) t5)\n" +
"\t\t\t\t)\n" +
"\t\t\t)\n" +
"\t\t)\n" +
"\t)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
@@ -35,7 +35,7 @@ public class MySqlFormatTest2 extends TestCase {
+ "\n\t\tFROM ce_queue cq2"
+ "\n\t\tWHERE cq.component_uuid = cq2.component_uuid"
+ "\n\t\t\tAND cq2.status <> ?"
+ "\n\t\t)"
+ "\n\t)"
+ "\nORDER BY cq.created_at ASC, cq.id ASC", SQLUtils.format(text, JdbcUtils.MYSQL));
}
}
@@ -117,7 +117,12 @@ public class SELECT_Syntax_Test extends TestCase {
String text = output(stmtList);
Assert.assertEquals("SELECT *\nFROM t1\nWHERE column1 = (\n\tSELECT column1\n\tFROM t2\n\t);", text);
assertEquals("SELECT *\n" +
"FROM t1\n" +
"WHERE column1 = (\n" +
"\tSELECT column1\n" +
"\tFROM t2\n" +
");", text);
}
public void test_7() throws Exception {
@@ -133,7 +138,7 @@ public class SELECT_Syntax_Test extends TestCase {
+ "\nWHERE EXISTS (" //
+ "\n\tSELECT *" //
+ "\n\tFROM t2"
+ "\n\t);", text);
+ "\n);", text);
}
public void test_8() throws Exception {
@@ -150,7 +155,7 @@ public class SELECT_Syntax_Test extends TestCase {
+ "\n\tSELECT *"
+ "\n\tFROM cities_stores"
+ "\n\tWHERE cities_stores.store_type = stores.store_type"
+ "\n\t);",
+ "\n);",
text);
}
@@ -209,8 +214,18 @@ public class SELECT_Syntax_Test extends TestCase {
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement stmt = stmtList.get(0);
Assert.assertEquals("SELECT s1\nFROM t1\nWHERE s1 NOT IN (SELECT s1\n\tFROM t2)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals("select s1\nfrom t1\nwhere s1 not in (select s1\n\tfrom t2)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
assertEquals("SELECT s1\n" +
"FROM t1\n" +
"WHERE s1 NOT IN (\n" +
"\tSELECT s1\n" +
"\tFROM t2\n" +
")", SQLUtils.toMySqlString(stmt));
assertEquals("select s1\n" +
"from t1\n" +
"where s1 not in (\n" +
"\tselect s1\n" +
"\tfrom t2\n" +
")", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
public void test_13() throws Exception {
@@ -220,8 +235,18 @@ public class SELECT_Syntax_Test extends TestCase {
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement stmt = stmtList.get(0);
Assert.assertEquals("SELECT s1\nFROM t1\nWHERE s1 IN (SELECT s1\n\tFROM t2)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals("select s1\nfrom t1\nwhere s1 in (select s1\n\tfrom t2)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
assertEquals("SELECT s1\n" +
"FROM t1\n" +
"WHERE s1 IN (\n" +
"\tSELECT s1\n" +
"\tFROM t2\n" +
")", SQLUtils.toMySqlString(stmt));
assertEquals("select s1\n" +
"from t1\n" +
"where s1 in (\n" +
"\tselect s1\n" +
"\tfrom t2\n" +
")", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
public void test_14() throws Exception {
@@ -53,17 +53,17 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\tFROM actvty_audit\n" +
"\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\tAND rankjurisdiction = 1\n" +
"\t\t) > 0 THEN '县站已审核' ELSE NULL END AS 'countyAudit', CASE WHEN (\n" +
"\t) > 0 THEN '县站已审核' ELSE NULL END AS 'countyAudit', CASE WHEN (\n" +
"\t\tSELECT audit\n" +
"\t\tFROM actvty_audit\n" +
"\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\tAND rankjurisdiction = 2\n" +
"\t\t) > 0 THEN '市馆已审核' ELSE NULL END AS 'cityAudit', CASE WHEN (\n" +
"\t) > 0 THEN '市馆已审核' ELSE NULL END AS 'cityAudit', CASE WHEN (\n" +
"\t\tSELECT audit\n" +
"\t\tFROM actvty_audit\n" +
"\t\tWHERE enroll_id = a.enroll_id\n" +
"\t\t\tAND rankjurisdiction = 3\n" +
"\t\t) > 0 THEN '省馆已审核' ELSE NULL END AS 'provinceAudit'\n" +
"\t) > 0 THEN '省馆已审核' ELSE NULL END AS 'provinceAudit'\n" +
"FROM actvty_audit a\n" +
"GROUP BY a.enroll_id", //
SQLUtils.toMySqlString(stmt));
@@ -78,17 +78,17 @@ public class MySqlCreateViewTest1 extends MysqlTest {
"\t\tfrom actvty_audit\n" +
"\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\tand rankjurisdiction = 1\n" +
"\t\t) > 0 then '县站已审核' else null end as 'countyAudit', case when (\n" +
"\t) > 0 then '县站已审核' else null end as 'countyAudit', case when (\n" +
"\t\tselect audit\n" +
"\t\tfrom actvty_audit\n" +
"\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\tand rankjurisdiction = 2\n" +
"\t\t) > 0 then '市馆已审核' else null end as 'cityAudit', case when (\n" +
"\t) > 0 then '市馆已审核' else null end as 'cityAudit', case when (\n" +
"\t\tselect audit\n" +
"\t\tfrom actvty_audit\n" +
"\t\twhere enroll_id = a.enroll_id\n" +
"\t\t\tand rankjurisdiction = 3\n" +
"\t\t) > 0 then '省馆已审核' else null end as 'provinceAudit'\n" +
"\t) > 0 then '省馆已审核' else null end as 'provinceAudit'\n" +
"from actvty_audit a\n" +
"group by a.enroll_id", //
SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
@@ -32,7 +32,8 @@ public class MySqlParameterizedOutputVisitorTest_30 extends TestCase {
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata `udata`\n" +
"WHERE `udata`.`id` IN (SELECT MAX(`udata`.`id`)\n" +
"WHERE `udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata `udata`\n" +
"\t\tWHERE `udata`.`uid` = ?\n" +
"\t\t\tAND `udata`.`user_type` = ?\n" +
@@ -41,7 +42,8 @@ public class MySqlParameterizedOutputVisitorTest_30 extends TestCase {
"\t\t\tAND `udata`.`charging_period` = ?\n" +
"\t\t\tAND `udata`.`task_type` = ?\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = ?\n" +
"\t\tAND `udata`.`user_type` = ?)\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
@@ -80,7 +82,8 @@ public class MySqlParameterizedOutputVisitorTest_30 extends TestCase {
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata_0888 `udata`\n" +
"WHERE `udata`.`id` IN (SELECT MAX(`udata`.`id`)\n" +
"WHERE `udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata_0888 `udata`\n" +
"\t\tWHERE `udata`.`uid` = 1039100792\n" +
"\t\t\tAND `udata`.`user_type` = 2\n" +
@@ -89,7 +92,8 @@ public class MySqlParameterizedOutputVisitorTest_30 extends TestCase {
"\t\t\tAND `udata`.`charging_period` = 1\n" +
"\t\t\tAND `udata`.`task_type` = 1\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = '1039100792'\n" +
"\t\tAND `udata`.`user_type` = 2)\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
@@ -29,7 +29,8 @@ public class MySqlParameterizedOutputVisitorTest_31 extends TestCase {
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata `udata`\n" +
"WHERE `udata`.`id` IN (SELECT MAX(`udata`.`id`)\n" +
"WHERE `udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata `udata`\n" +
"\t\tWHERE `udata`.`uid` = ?\n" +
"\t\t\tAND `udata`.`user_type` = ?\n" +
@@ -38,7 +39,8 @@ public class MySqlParameterizedOutputVisitorTest_31 extends TestCase {
"\t\t\tAND `udata`.`charging_period` = ?\n" +
"\t\t\tAND `udata`.`task_type` = ?\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = ?\n" +
"\t\tAND `udata`.`user_type` = ?)\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
@@ -77,7 +79,8 @@ public class MySqlParameterizedOutputVisitorTest_31 extends TestCase {
"\t, `udata`.`status` AS `status`, `udata`.`charging_period` AS `chargingPeriod`, `udata`.`sn` AS `sn`, `udata`.`cpd` AS `chargingPeriodDesc`, `udata`.`task_total_num` AS `taskTotalNum`\n" +
"\t, `udata`.`tcn` AS `taCoNu`, `udata`.`task_type` AS `taskType`, `udata`.`ilbu` AS `isLaBiUs`\n" +
"FROM udata_0888 `udata`\n" +
"WHERE `udata`.`id` IN (SELECT MAX(`udata`.`id`)\n" +
"WHERE `udata`.`id` IN (\n" +
"\t\tSELECT MAX(`udata`.`id`)\n" +
"\t\tFROM udata_0888 `udata`\n" +
"\t\tWHERE `udata`.`uid` = 1039100792\n" +
"\t\t\tAND `udata`.`user_type` = 2\n" +
@@ -86,7 +89,8 @@ public class MySqlParameterizedOutputVisitorTest_31 extends TestCase {
"\t\t\tAND `udata`.`charging_period` = 1\n" +
"\t\t\tAND `udata`.`task_type` = 1\n" +
"\t\t\tAND `udata`.`task_total_num` <= `udata`.`tcn`\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`)\n" +
"\t\tGROUP BY `udata`.`charging_period`, `udata`.`start_period_time`, `udata`.`ept`\n" +
"\t)\n" +
"\tAND (`udata`.`uid` = '1039100792'\n" +
"\t\tAND `udata`.`user_type` = 2)\n" +
"ORDER BY `udata`.`start_period_time` DESC\n" +
@@ -61,12 +61,12 @@ public class MySqlSelectTest_14 extends MysqlTest {
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t2")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *" //
+ "\nFROM t1"//
+ "\nWHERE column1 = ("//
+ "\n\tSELECT column1"//
+ "\n\tFROM t2"//
+ "\n\t)", //
Assert.assertEquals("SELECT *\n" +
"FROM t1\n" +
"WHERE column1 = (\n" +
"\tSELECT column1\n" +
"\tFROM t2\n" +
")", //
output);
String output_lcase = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
@@ -75,7 +75,7 @@ public class MySqlSelectTest_14 extends MysqlTest {
+ "\nwhere column1 = ("//
+ "\n\tselect column1"//
+ "\n\tfrom t2"//
+ "\n\t)", //
+ "\n)", //
output_lcase);
}
}
@@ -55,24 +55,28 @@ public class MySqlSelectTest_29 extends MysqlTest {
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM Function"
+ "\nWHERE Id IN (SELECT FunctionId"
+ "\n\t\tFROM RoleFunction"
+ "\n\t\tWHERE RoleId = '001'"
+ "\n\t\t\tAND LogicalDel = 0)"
+ "\n\tAND LogicalDel = 0", //
assertEquals("SELECT *\n" +
"FROM Function\n" +
"WHERE Id IN (\n" +
"\t\tSELECT FunctionId\n" +
"\t\tFROM RoleFunction\n" +
"\t\tWHERE RoleId = '001'\n" +
"\t\t\tAND LogicalDel = 0\n" +
"\t)\n" +
"\tAND LogicalDel = 0", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *"
+ "\nfrom Function"
+ "\nwhere Id in (select FunctionId"
+ "\n\t\tfrom RoleFunction"
+ "\n\t\twhere RoleId = '001'"
+ "\n\t\t\tand LogicalDel = 0)"
+ "\n\tand LogicalDel = 0", //
assertEquals("select *\n" +
"from Function\n" +
"where Id in (\n" +
"\t\tselect FunctionId\n" +
"\t\tfrom RoleFunction\n" +
"\t\twhere RoleId = '001'\n" +
"\t\t\tand LogicalDel = 0\n" +
"\t)\n" +
"\tand LogicalDel = 0", //
output);
}
}
@@ -42,13 +42,14 @@ public class OdpsSelectTest17 extends TestCase {
") top )\n" +
"ORDER BY cnt DESC\n" +
"LIMIT 800";//
Assert.assertEquals("SELECT prov\n" +
assertEquals("SELECT prov\n" +
"\t, name\n" +
"\t, cnt\n" +
"FROM mock_app.adl_mock_v_fct\n" +
"WHERE ds = 20160920\n" +
"\tAND name != 'none'\n" +
"\tAND prov IN (SELECT prov\n" +
"\tAND prov IN (\n" +
"\t\tSELECT prov\n" +
"\t\tFROM (\n" +
"\t\t\tSELECT prov\n" +
"\t\t\t\t, SUM(cnt) AS cnt\n" +
@@ -57,16 +58,18 @@ public class OdpsSelectTest17 extends TestCase {
"\t\t\tGROUP BY prov\n" +
"\t\t\tORDER BY cnt DESC\n" +
"\t\t\tLIMIT 5\n" +
"\t\t) top)\n" +
"\t\t) top\n" +
"\t)\n" +
"ORDER BY cnt DESC\n" +
"LIMIT 800", SQLUtils.formatOdps(sql));
Assert.assertEquals("select prov\n" +
assertEquals("select prov\n" +
"\t, name\n" +
"\t, cnt\n" +
"from mock_app.adl_mock_v_fct\n" +
"where ds = 20160920\n" +
"\tand name != 'none'\n" +
"\tand prov in (select prov\n" +
"\tand prov in (\n" +
"\t\tselect prov\n" +
"\t\tfrom (\n" +
"\t\t\tselect prov\n" +
"\t\t\t\t, sum(cnt) as cnt\n" +
@@ -75,7 +78,8 @@ public class OdpsSelectTest17 extends TestCase {
"\t\t\tgroup by prov\n" +
"\t\t\torder by cnt desc\n" +
"\t\t\tlimit 5\n" +
"\t\t) top)\n" +
"\t\t) top\n" +
"\t)\n" +
"order by cnt desc\n" +
"limit 800", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
@@ -28,20 +28,24 @@ public class OdpsSelectTest18 extends TestCase {
public void test_select() throws Exception {
String sql = "select a, b, c from tableA where a in ( select a from tableB limit 10 )";//
Assert.assertEquals("SELECT a\n" +
assertEquals("SELECT a\n" +
"\t, b\n" +
"\t, c\n" +
"FROM tableA\n" +
"WHERE a IN (SELECT a\n" +
"WHERE a IN (\n" +
"\tSELECT a\n" +
"\tFROM tableB\n" +
"\tLIMIT 10)", SQLUtils.formatOdps(sql));
Assert.assertEquals("select a\n" +
"\tLIMIT 10\n" +
")", SQLUtils.formatOdps(sql));
assertEquals("select a\n" +
"\t, b\n" +
"\t, c\n" +
"from tableA\n" +
"where a in (select a\n" +
"where a in (\n" +
"\tselect a\n" +
"\tfrom tableB\n" +
"\tlimit 10)", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
"\tlimit 10\n" +
")", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
SQLStatement stmt = statementList.get(0);
@@ -53,7 +53,7 @@ public class OracleSubqueryFactoringTest extends TestCase {
+ "WHERE dept_total > (\n" //
+ "\tSELECT avg\n" //
+ "\tFROM avg_cost\n" //
+ "\t)\n" //
+ ")\n" //
+ "ORDER BY department_name;";
OracleStatementParser parser = new OracleStatementParser(sql);
@@ -104,7 +104,7 @@ public class OracleCreateViewTest3 extends OracleTest {
"\t\tAND outer.dbinc_key = bdf.dbinc_key\n" +
"\t\tAND outer.file# = file#\n" +
"\t\tAND outer.stamp < bs.stamp\n" +
"\t)",//
")",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
@@ -0,0 +1,51 @@
package com.alibaba.druid.bvt.sql.oracle.param;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wenshao on 16/8/23.
*/
public class Oracle_param_0 extends TestCase {
public void test_for_parameterize() throws Exception {
final String dbType = JdbcConstants.MYSQL;
String sql = "select * from t where rownum = 1";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
visitor.setExportTables(true);
/*visitor.setPrettyFormat(false);*/
SQLStatement stmt = stmtList.get(0);
stmt.accept(visitor);
// System.out.println(parameters);
assertEquals(0, parameters.size());
StringBuilder buf = new StringBuilder();
SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
visitor1.addTableMapping("a_entry", "a_entry_2664");
visitor1.setParameters(visitor.getParameters());
stmt.accept(visitor1);
assertEquals("SELECT *\n" +
"FROM t\n" +
"WHERE rownum = 1", buf.toString());
}
}
@@ -54,81 +54,85 @@ public class OracleSelectTest38 extends OracleTest {
{
String result = SQLUtils.toOracleString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM ("
+ "\n\tWITH"
+ "\n\t\tvw_kreis_statics_t"
+ "\n\t\tAS"
+ "\n\t\t("
+ "\n\t\t\tSELECT substr(xzqh, 1, 6) AS xzqh, swrslx, SUM(swrs_count) AS acd_totle"
+ "\n\t\t\tFROM ("
+ "\n\t\t\t\tSELECT xzqh, sglx, CASE WHEN swrs7 < 3 THEN '1' WHEN swrs7 < 5 THEN '2' WHEN swrs7 <= 9 THEN '3' ELSE '4' END AS swrslx, 1 AS swrs_count"
+ "\n\t\t\t\tFROM acduser.vw_acd_info"
+ "\n\t\t\t\tWHERE sglx = '1'"
+ "\n\t\t\t\t\tAND sgfssj >= ?"
+ "\n\t\t\t)"
+ "\n\t\t\tGROUP BY substr(xzqh, 1, 6), swrslx"
+ "\n\t\t)"
+ "\n\tSELECT e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\""
+ "\n\t\t, kreis_code, kreis_name, px1, py1, px2"
+ "\n\t\t, py2"
+ "\n\tFROM ("
+ "\n\t\tSELECT xzqh, nvl(MAX(decode(swrslx, '1', acd_totle)), 0) AS less3, nvl(MAX(decode(swrslx, '2', acd_totle)), 0) AS f3to5, nvl(MAX(decode(swrslx, '3', acd_totle)), 0) AS f5to9, nvl(MAX(decode(swrslx, '4', acd_totle)), 0) AS more9"
+ "\n\t\tFROM ("
+ "\n\t\t\tSELECT *"
+ "\n\t\t\tFROM acduser.vw_kreis_statics_t"
+ "\n\t\t)"
+ "\n\t\tGROUP BY xzqh"
+ "\n\t) e"
+ "\n\t\tLEFT JOIN acduser.vw_sc_kreis_code_lv2 f ON e.xzqh = f.short_kreis_code "
+ "\n)"
+ "\nWHERE kreis_code IN (SELECT *"
+ "\n\tFROM ("
+ "\n\t\tSELECT tbek_code"
+ "\n\t\tFROM acduser.vw_kreis_code"
+ "\n\t\tSTART WITH tbek_code = ?"
+ "\n\t\tCONNECT BY PRIOR tbek_pk = tbek_parent"
+ "\n\t)"
+ "\n\tWHERE tbek_code != ?)", result);
Assert.assertEquals("SELECT *\n" +
"FROM (\n" +
"\tWITH\n" +
"\t\tvw_kreis_statics_t\n" +
"\t\tAS\n" +
"\t\t(\n" +
"\t\t\tSELECT substr(xzqh, 1, 6) AS xzqh, swrslx, SUM(swrs_count) AS acd_totle\n" +
"\t\t\tFROM (\n" +
"\t\t\t\tSELECT xzqh, sglx, CASE WHEN swrs7 < 3 THEN '1' WHEN swrs7 < 5 THEN '2' WHEN swrs7 <= 9 THEN '3' ELSE '4' END AS swrslx, 1 AS swrs_count\n" +
"\t\t\t\tFROM acduser.vw_acd_info\n" +
"\t\t\t\tWHERE sglx = '1'\n" +
"\t\t\t\t\tAND sgfssj >= ?\n" +
"\t\t\t)\n" +
"\t\t\tGROUP BY substr(xzqh, 1, 6), swrslx\n" +
"\t\t)\n" +
"\tSELECT e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\"\n" +
"\t\t, kreis_code, kreis_name, px1, py1, px2\n" +
"\t\t, py2\n" +
"\tFROM (\n" +
"\t\tSELECT xzqh, nvl(MAX(decode(swrslx, '1', acd_totle)), 0) AS less3, nvl(MAX(decode(swrslx, '2', acd_totle)), 0) AS f3to5, nvl(MAX(decode(swrslx, '3', acd_totle)), 0) AS f5to9, nvl(MAX(decode(swrslx, '4', acd_totle)), 0) AS more9\n" +
"\t\tFROM (\n" +
"\t\t\tSELECT *\n" +
"\t\t\tFROM acduser.vw_kreis_statics_t\n" +
"\t\t)\n" +
"\t\tGROUP BY xzqh\n" +
"\t) e\n" +
"\t\tLEFT JOIN acduser.vw_sc_kreis_code_lv2 f ON e.xzqh = f.short_kreis_code \n" +
")\n" +
"WHERE kreis_code IN (\n" +
"\tSELECT *\n" +
"\tFROM (\n" +
"\t\tSELECT tbek_code\n" +
"\t\tFROM acduser.vw_kreis_code\n" +
"\t\tSTART WITH tbek_code = ?\n" +
"\t\tCONNECT BY PRIOR tbek_pk = tbek_parent\n" +
"\t)\n" +
"\tWHERE tbek_code != ?\n" +
")", result);
}
{
String result = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *"
+ "\nfrom ("
+ "\n\twith"
+ "\n\t\tvw_kreis_statics_t"
+ "\n\t\tas"
+ "\n\t\t("
+ "\n\t\t\tselect substr(xzqh, 1, 6) as xzqh, swrslx, sum(swrs_count) as acd_totle"
+ "\n\t\t\tfrom ("
+ "\n\t\t\t\tselect xzqh, sglx, case when swrs7 < 3 then '1' when swrs7 < 5 then '2' when swrs7 <= 9 then '3' else '4' end as swrslx, 1 as swrs_count"
+ "\n\t\t\t\tfrom acduser.vw_acd_info"
+ "\n\t\t\t\twhere sglx = '1'"
+ "\n\t\t\t\t\tand sgfssj >= ?"
+ "\n\t\t\t)"
+ "\n\t\t\tgroup by substr(xzqh, 1, 6), swrslx"
+ "\n\t\t)"
+ "\n\tselect e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\""
+ "\n\t\t, kreis_code, kreis_name, px1, py1, px2"
+ "\n\t\t, py2"
+ "\n\tfrom ("
+ "\n\t\tselect xzqh, nvl(max(decode(swrslx, '1', acd_totle)), 0) as less3, nvl(max(decode(swrslx, '2', acd_totle)), 0) as f3to5, nvl(max(decode(swrslx, '3', acd_totle)), 0) as f5to9, nvl(max(decode(swrslx, '4', acd_totle)), 0) as more9"
+ "\n\t\tfrom ("
+ "\n\t\t\tselect *"
+ "\n\t\t\tfrom acduser.vw_kreis_statics_t"
+ "\n\t\t)"
+ "\n\t\tgroup by xzqh"
+ "\n\t) e"
+ "\n\t\tleft join acduser.vw_sc_kreis_code_lv2 f on e.xzqh = f.short_kreis_code "
+ "\n)"
+ "\nwhere kreis_code in (select *"
+ "\n\tfrom ("
+ "\n\t\tselect tbek_code"
+ "\n\t\tfrom acduser.vw_kreis_code"
+ "\n\t\tstart with tbek_code = ?"
+ "\n\t\tconnect by prior tbek_pk = tbek_parent"
+ "\n\t)"
+ "\n\twhere tbek_code != ?)", result);
assertEquals("select *\n" +
"from (\n" +
"\twith\n" +
"\t\tvw_kreis_statics_t\n" +
"\t\tas\n" +
"\t\t(\n" +
"\t\t\tselect substr(xzqh, 1, 6) as xzqh, swrslx, sum(swrs_count) as acd_totle\n" +
"\t\t\tfrom (\n" +
"\t\t\t\tselect xzqh, sglx, case when swrs7 < 3 then '1' when swrs7 < 5 then '2' when swrs7 <= 9 then '3' else '4' end as swrslx, 1 as swrs_count\n" +
"\t\t\t\tfrom acduser.vw_acd_info\n" +
"\t\t\t\twhere sglx = '1'\n" +
"\t\t\t\t\tand sgfssj >= ?\n" +
"\t\t\t)\n" +
"\t\t\tgroup by substr(xzqh, 1, 6), swrslx\n" +
"\t\t)\n" +
"\tselect e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\"\n" +
"\t\t, kreis_code, kreis_name, px1, py1, px2\n" +
"\t\t, py2\n" +
"\tfrom (\n" +
"\t\tselect xzqh, nvl(max(decode(swrslx, '1', acd_totle)), 0) as less3, nvl(max(decode(swrslx, '2', acd_totle)), 0) as f3to5, nvl(max(decode(swrslx, '3', acd_totle)), 0) as f5to9, nvl(max(decode(swrslx, '4', acd_totle)), 0) as more9\n" +
"\t\tfrom (\n" +
"\t\t\tselect *\n" +
"\t\t\tfrom acduser.vw_kreis_statics_t\n" +
"\t\t)\n" +
"\t\tgroup by xzqh\n" +
"\t) e\n" +
"\t\tleft join acduser.vw_sc_kreis_code_lv2 f on e.xzqh = f.short_kreis_code \n" +
")\n" +
"where kreis_code in (\n" +
"\tselect *\n" +
"\tfrom (\n" +
"\t\tselect tbek_code\n" +
"\t\tfrom acduser.vw_kreis_code\n" +
"\t\tstart with tbek_code = ?\n" +
"\t\tconnect by prior tbek_pk = tbek_parent\n" +
"\t)\n" +
"\twhere tbek_code != ?\n" +
")", result);
}
Assert.assertEquals(1, statementList.size());
@@ -77,17 +77,19 @@ public class OracleSelectTest56 extends OracleTest {
{
String text = SQLUtils.toOracleString(stmt);
Assert.assertEquals("SELECT AA.ID, AA.CODE, AA.TYPE, AA.STATUS, AA.EMPLOYEENAME\n" +
assertEquals("SELECT AA.ID, AA.CODE, AA.TYPE, AA.STATUS, AA.EMPLOYEENAME\n" +
"\t, AA.CREATORNAME, AA.OPERATIONTYPE, AA.CREATEDATE, AA.REMARK, W.NAME\n" +
"\t, DD.DESC\n" +
"FROM a AA, w W, d DD\n" +
"WHERE AA.employeeNo IN (SELECT employeeno\n" +
"WHERE AA.employeeNo IN (\n" +
"\t\tSELECT employeeno\n" +
"\t\tFROM employeeauditor ea\n" +
"\t\tWHERE auditorno = 1\n" +
"\t\tGROUP BY employeeno\n" +
"\t\tUNION ALL\n" +
"\t\tSELECT 1\n" +
"\t\tFROM dual)\n" +
"\t\tFROM dual\n" +
"\t)\n" +
"\tAND AA.WNO = W.WNO(+)\n" +
"\tAND AA.DEPTNO = DD.DEPTNO(+)", text);
}
@@ -95,17 +97,19 @@ public class OracleSelectTest56 extends OracleTest {
{
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select AA.ID, AA.CODE, AA.TYPE, AA.STATUS, AA.EMPLOYEENAME\n" +
assertEquals("select AA.ID, AA.CODE, AA.TYPE, AA.STATUS, AA.EMPLOYEENAME\n" +
"\t, AA.CREATORNAME, AA.OPERATIONTYPE, AA.CREATEDATE, AA.REMARK, W.NAME\n" +
"\t, DD.DESC\n" +
"from a AA, w W, d DD\n" +
"where AA.employeeNo in (select employeeno\n" +
"where AA.employeeNo in (\n" +
"\t\tselect employeeno\n" +
"\t\tfrom employeeauditor ea\n" +
"\t\twhere auditorno = 1\n" +
"\t\tgroup by employeeno\n" +
"\t\tunion all\n" +
"\t\tselect 1\n" +
"\t\tfrom dual)\n" +
"\t\tfrom dual\n" +
"\t)\n" +
"\tand AA.WNO = W.WNO(+)\n" +
"\tand AA.DEPTNO = DD.DEPTNO(+)", text);
}
@@ -38,14 +38,14 @@ public class PGSelectTest25 extends PGTest {
+ "\n\t\tSELECT project_deduct_mandays"
+ "\n\t\tFROM mytable_01"
+ "\n\t\tLIMIT 1"
+ "\n\t\t))"
+ "\n\t))"
+ "\nFROM t", SQLUtils.toPGString(stmt));
Assert.assertEquals("select COALESCE(("
+ "\n\t\tselect project_deduct_mandays"
+ "\n\t\tfrom mytable_01"
+ "\n\t\tlimit 1"
+ "\n\t\t))"
+ "\n\t))"
+ "\nfrom t", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
@@ -33,19 +33,23 @@ public class PGSelectTest43 extends PGTest {
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("SELECT task_id\n" +
assertEquals("SELECT task_id\n" +
"FROM mod_period_time\n" +
"WHERE task_id IN (SELECT task_id AS b\n" +
"WHERE task_id IN (\n" +
"\tSELECT task_id AS b\n" +
"\tFROM mod_period_time\n" +
"\tORDER BY task_id DESC\n" +
"\tLIMIT 1)", SQLUtils.toPGString(stmt));
"\tLIMIT 1\n" +
")", SQLUtils.toPGString(stmt));
Assert.assertEquals("select task_id\n" +
assertEquals("select task_id\n" +
"from mod_period_time\n" +
"where task_id in (select task_id as b\n" +
"where task_id in (\n" +
"\tselect task_id as b\n" +
"\tfrom mod_period_time\n" +
"\torder by task_id desc\n" +
"\tlimit 1)", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
"\tlimit 1\n" +
")", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
@@ -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);