mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -60,11 +60,11 @@ public class PagerUtilsTest_Count_MySql_0 extends TestCase {
|
||||
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("SELECT COUNT(*)\n" +
|
||||
"FROM (\n" +
|
||||
"\t(SELECT id, name\n" +
|
||||
"\tFROM t1)\n" +
|
||||
"\tSELECT id, name\n" +
|
||||
"\tFROM t1\n" +
|
||||
"\tUNION\n" +
|
||||
"\t(SELECT id, name\n" +
|
||||
"\tFROM t2)\n" +
|
||||
"\tSELECT id, name\n" +
|
||||
"\tFROM t2\n" +
|
||||
") ALIAS_COUNT", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,16 +31,15 @@ public class DMLSelectParserTest extends TestCase {
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("(SELECT id\n" + //
|
||||
"FROM t1)\n" + //
|
||||
"UNION ALL\n" + //
|
||||
"(SELECT id\n" + //
|
||||
"FROM t2)\n" + //
|
||||
"UNION ALL\n" + //
|
||||
"(SELECT id\n" + //
|
||||
"FROM t3)\n" + //
|
||||
"ORDER BY d DESC\n" + //
|
||||
"LIMIT ?, 1", output);
|
||||
Assert.assertEquals("(SELECT id\n" +
|
||||
"FROM t1)\n" +
|
||||
"UNION ALL\n" +
|
||||
"(SELECT id\n" +
|
||||
"FROM t2)\n" +
|
||||
"UNION ALL\n" +
|
||||
"(SELECT id\n" +
|
||||
"FROM t3)\n" +
|
||||
"ORDER BY d DESC", output);
|
||||
}
|
||||
|
||||
public void test_union_1() throws Exception {
|
||||
@@ -49,16 +48,16 @@ public class DMLSelectParserTest extends TestCase {
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("(SELECT id\n" + //
|
||||
"FROM t1)\n" + //
|
||||
"UNION\n" + //
|
||||
"(SELECT id\n" + //
|
||||
"FROM t2\n" + //
|
||||
"ORDER BY id)\n" + //
|
||||
"UNION ALL\n" + //
|
||||
"(SELECT id\n" + //
|
||||
"FROM t3)\n" + //
|
||||
"ORDER BY d ASC", output);
|
||||
assertEquals("(SELECT id\n" +
|
||||
"FROM t1)\n" +
|
||||
"UNION\n" +
|
||||
"SELECT id\n" +
|
||||
"FROM t2\n" +
|
||||
"ORDER BY id\n" +
|
||||
"UNION ALL\n" +
|
||||
"(SELECT id\n" +
|
||||
"FROM t3)\n" +
|
||||
"ORDER BY d ASC", output);
|
||||
}
|
||||
|
||||
public void test_union_2() throws Exception {
|
||||
@@ -67,7 +66,14 @@ public class DMLSelectParserTest extends TestCase {
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("(SELECT id\nFROM t1)\nUNION DISTINCT\n(SELECT id\nFROM t2)\nUNION\n(SELECT id\nFROM t3)", output);
|
||||
Assert.assertEquals("(SELECT id\n" +
|
||||
"FROM t1)\n" +
|
||||
"UNION DISTINCT\n" +
|
||||
"(SELECT id\n" +
|
||||
"FROM t2)\n" +
|
||||
"UNION\n" +
|
||||
"SELECT id\n" +
|
||||
"FROM t3", output);
|
||||
}
|
||||
|
||||
public void test_select_0() throws Exception {
|
||||
|
||||
@@ -134,7 +134,9 @@ public class BitFunctionsTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("SELECT b + 0, BIN(b + 0), OCT(b + 0), HEX(b + 0)\nFROM t;", text);
|
||||
Assert.assertEquals("SELECT b + 0, BIN(b + 0), OCT(b + 0)\n" +
|
||||
"\t, HEX(b + 0)\n" +
|
||||
"FROM t;", text);
|
||||
}
|
||||
|
||||
private String output(List<SQLStatement> stmtList) {
|
||||
|
||||
@@ -35,7 +35,11 @@ public class ControlFlowFunctionsTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;", text);
|
||||
Assert.assertEquals("SELECT CASE 1\n" +
|
||||
"\t\tWHEN 1 THEN 'one'\n" +
|
||||
"\t\tWHEN 2 THEN 'two'\n" +
|
||||
"\t\tELSE 'more'\n" +
|
||||
"\tEND;", text);
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
|
||||
@@ -50,14 +50,14 @@ public class MySqlDeleteTest_3 extends MysqlTest {
|
||||
"\t\t\tSELECT *\n" +
|
||||
"\t\t\tFROM t3\n" +
|
||||
"\t\t\tWHERE ROW(5 * t2.s1, 77) = (\n" +
|
||||
"\t\t\t\t(SELECT 50, 11 * s1\n" +
|
||||
"\t\t\t\tFROM t4)\n" +
|
||||
"\t\t\t\tSELECT 50, 11 * s1\n" +
|
||||
"\t\t\t\tFROM t4\n" +
|
||||
"\t\t\t\tUNION\n" +
|
||||
"\t\t\t\t(SELECT 50, 77\n" +
|
||||
"\t\t\t\tSELECT 50, 77\n" +
|
||||
"\t\t\t\tFROM (\n" +
|
||||
"\t\t\t\t\tSELECT *\n" +
|
||||
"\t\t\t\t\tFROM t5\n" +
|
||||
"\t\t\t\t) t5)\n" +
|
||||
"\t\t\t\t) t5\n" +
|
||||
"\t\t\t)\n" +
|
||||
"\t\t)\n" +
|
||||
"\t)", SQLUtils.toMySqlString(stmt));
|
||||
@@ -69,14 +69,14 @@ public class MySqlDeleteTest_3 extends MysqlTest {
|
||||
"\t\t\tselect *\n" +
|
||||
"\t\t\tfrom t3\n" +
|
||||
"\t\t\twhere ROW(5 * t2.s1, 77) = (\n" +
|
||||
"\t\t\t\t(select 50, 11 * s1\n" +
|
||||
"\t\t\t\tfrom t4)\n" +
|
||||
"\t\t\t\tselect 50, 11 * s1\n" +
|
||||
"\t\t\t\tfrom t4\n" +
|
||||
"\t\t\t\tunion\n" +
|
||||
"\t\t\t\t(select 50, 77\n" +
|
||||
"\t\t\t\tselect 50, 77\n" +
|
||||
"\t\t\t\tfrom (\n" +
|
||||
"\t\t\t\t\tselect *\n" +
|
||||
"\t\t\t\t\tfrom t5\n" +
|
||||
"\t\t\t\t) t5)\n" +
|
||||
"\t\t\t\t) t5\n" +
|
||||
"\t\t\t)\n" +
|
||||
"\t\t)\n" +
|
||||
"\t)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
@@ -40,13 +40,13 @@ public class SQLUtilsTest extends TestCase {
|
||||
public void test_format_3() throws Exception {
|
||||
String sql = "select lottery_notice_issue,lottery_notice_date,lottery_notice_result from tb_lottery_notice where lottery_type_id=8 and lottery_notice_issue<=2014066 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL# and lottery_notice_issue>=2014062 order by lottery_notice_issue desc";
|
||||
String formattedSql = SQLUtils.formatMySql(sql);
|
||||
String expected = "(SELECT lottery_notice_issue, lottery_notice_date, lottery_notice_result"
|
||||
+ "\nFROM tb_lottery_notice"
|
||||
+ "\nWHERE lottery_type_id = 8"
|
||||
+ "\n\tAND lottery_notice_issue <= 2014066)"
|
||||
+ "\nUNION ALL"
|
||||
+ "\n(SELECT NULL, NULL, NULL, NULL, NULL" //
|
||||
+ "\n\t, NULL)# and lottery_notice_issue>=2014062 order by lottery_notice_issue desc";
|
||||
String expected = "SELECT lottery_notice_issue, lottery_notice_date, lottery_notice_result\n" +
|
||||
"FROM tb_lottery_notice\n" +
|
||||
"WHERE lottery_type_id = 8\n" +
|
||||
"\tAND lottery_notice_issue <= 2014066\n" +
|
||||
"UNION ALL\n" +
|
||||
"SELECT NULL, NULL, NULL, NULL, NULL\n" +
|
||||
"\t, NULL# and lottery_notice_issue>=2014062 order by lottery_notice_issue desc";
|
||||
Assert.assertEquals(expected, formattedSql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,27 +43,39 @@ public class MySqlCreateViewTest1 extends MysqlTest {
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
Assert.assertEquals("CREATE ALGORITHM = UNDEFINED\n" +
|
||||
assertEquals("CREATE ALGORITHM = UNDEFINED\n" +
|
||||
"\tDEFINER = root\n" +
|
||||
"\tSQL SECURITY = DEFINER\n" +
|
||||
"\tVIEW view_audit_enroll\n" +
|
||||
"AS\n" +
|
||||
"SELECT a.enroll_id AS 'enrollId', CASE WHEN (\n" +
|
||||
"\t\tSELECT audit\n" +
|
||||
"\t\tFROM actvty_audit\n" +
|
||||
"\t\tWHERE enroll_id = a.enroll_id\n" +
|
||||
"\t\t\tAND rankjurisdiction = 1\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) > 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) > 0 THEN '省馆已审核' ELSE NULL END AS 'provinceAudit'\n" +
|
||||
"SELECT a.enroll_id AS 'enrollId'\n" +
|
||||
"\t, CASE \n" +
|
||||
"\t\tWHEN (\n" +
|
||||
"\t\t\tSELECT audit\n" +
|
||||
"\t\t\tFROM actvty_audit\n" +
|
||||
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tAND rankjurisdiction = 1\n" +
|
||||
"\t\t) > 0 THEN '县站已审核'\n" +
|
||||
"\t\tELSE NULL\n" +
|
||||
"\tEND AS 'countyAudit'\n" +
|
||||
"\t, CASE \n" +
|
||||
"\t\tWHEN (\n" +
|
||||
"\t\t\tSELECT audit\n" +
|
||||
"\t\t\tFROM actvty_audit\n" +
|
||||
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tAND rankjurisdiction = 2\n" +
|
||||
"\t\t) > 0 THEN '市馆已审核'\n" +
|
||||
"\t\tELSE NULL\n" +
|
||||
"\tEND AS 'cityAudit'\n" +
|
||||
"\t, CASE \n" +
|
||||
"\t\tWHEN (\n" +
|
||||
"\t\t\tSELECT audit\n" +
|
||||
"\t\t\tFROM actvty_audit\n" +
|
||||
"\t\t\tWHERE enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tAND rankjurisdiction = 3\n" +
|
||||
"\t\t) > 0 THEN '省馆已审核'\n" +
|
||||
"\t\tELSE NULL\n" +
|
||||
"\tEND AS 'provinceAudit'\n" +
|
||||
"FROM actvty_audit a\n" +
|
||||
"GROUP BY a.enroll_id", //
|
||||
SQLUtils.toMySqlString(stmt));
|
||||
@@ -73,22 +85,34 @@ public class MySqlCreateViewTest1 extends MysqlTest {
|
||||
"\tsql security = DEFINER\n" +
|
||||
"\tview view_audit_enroll\n" +
|
||||
"as\n" +
|
||||
"select a.enroll_id as 'enrollId', case when (\n" +
|
||||
"\t\tselect audit\n" +
|
||||
"\t\tfrom actvty_audit\n" +
|
||||
"\t\twhere enroll_id = a.enroll_id\n" +
|
||||
"\t\t\tand rankjurisdiction = 1\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) > 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) > 0 then '省馆已审核' else null end as 'provinceAudit'\n" +
|
||||
"select a.enroll_id as 'enrollId'\n" +
|
||||
"\t, case \n" +
|
||||
"\t\twhen (\n" +
|
||||
"\t\t\tselect audit\n" +
|
||||
"\t\t\tfrom actvty_audit\n" +
|
||||
"\t\t\twhere enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tand rankjurisdiction = 1\n" +
|
||||
"\t\t) > 0 then '县站已审核'\n" +
|
||||
"\t\telse null\n" +
|
||||
"\tend as 'countyAudit'\n" +
|
||||
"\t, case \n" +
|
||||
"\t\twhen (\n" +
|
||||
"\t\t\tselect audit\n" +
|
||||
"\t\t\tfrom actvty_audit\n" +
|
||||
"\t\t\twhere enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tand rankjurisdiction = 2\n" +
|
||||
"\t\t) > 0 then '市馆已审核'\n" +
|
||||
"\t\telse null\n" +
|
||||
"\tend as 'cityAudit'\n" +
|
||||
"\t, case \n" +
|
||||
"\t\twhen (\n" +
|
||||
"\t\t\tselect audit\n" +
|
||||
"\t\t\tfrom actvty_audit\n" +
|
||||
"\t\t\twhere enroll_id = a.enroll_id\n" +
|
||||
"\t\t\t\tand rankjurisdiction = 3\n" +
|
||||
"\t\t) > 0 then '省馆已审核'\n" +
|
||||
"\t\telse null\n" +
|
||||
"\tend as 'provinceAudit'\n" +
|
||||
"from actvty_audit a\n" +
|
||||
"group by a.enroll_id", //
|
||||
SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
@@ -30,11 +30,12 @@ public class OracleFlashbackQueryTest3 extends TestCase {
|
||||
+ "FROM employees e, departments d\n" + "WHERE d.department_id = e.department_id\n"
|
||||
+ "GROUP BY ROLLUP (department_name, job_id);\n";
|
||||
|
||||
String expect = "SELECT DECODE(GROUPING(department_name), 1, 'All Departments', department_name) AS department, "
|
||||
+ "DECODE(GROUPING(job_id), 1, 'All Jobs', job_id) AS job, COUNT(*) AS \"Total Empl\", AVG(salary) * 12 AS \"Average Sal\"\n"
|
||||
+ "FROM employees e, departments d\n"
|
||||
+ "WHERE d.department_id = e.department_id\n"
|
||||
+ "GROUP BY ROLLUP(department_name, job_id);";
|
||||
String expect = "SELECT DECODE(GROUPING(department_name), 1, 'All Departments', department_name) AS department\n" +
|
||||
"\t, DECODE(GROUPING(job_id), 1, 'All Jobs', job_id) AS job\n" +
|
||||
"\t, COUNT(*) AS \"Total Empl\", AVG(salary) * 12 AS \"Average Sal\"\n" +
|
||||
"FROM employees e, departments d\n" +
|
||||
"WHERE d.department_id = e.department_id\n" +
|
||||
"GROUP BY ROLLUP(department_name, job_id);";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
|
||||
|
||||
@@ -35,18 +35,17 @@ public class OracleGroupingSetTest extends TestCase {
|
||||
+ "GROUP BY GROUPING SETS((channel_desc, calendar_month_desc, co.country_id), (channel_desc, co.country_id), "
|
||||
+ "( calendar_month_desc, co.country_id) );\n";
|
||||
|
||||
String expected = "SELECT channel_desc, calendar_month_desc, co.country_id, " //
|
||||
+ "TO_CHAR(SUM(amount_sold), '9,999,999,999') AS SALES$\n" //
|
||||
+ "FROM sales, customers, times, channels, countries co\n" //
|
||||
+ "WHERE sales.time_id = times.time_id" //
|
||||
+ "\n\tAND sales.cust_id = customers.cust_id" //
|
||||
+ "\n\tAND sales.channel_id = channels.channel_id" //
|
||||
+ "\n\tAND customers.country_id = co.country_id" //
|
||||
+ "\n\tAND channels.channel_desc IN ('Direct Sales', 'Internet')" //
|
||||
+ "\n\tAND times.calendar_month_desc IN ('2000-09', '2000-10')"
|
||||
+ "\n\tAND co.country_id IN ('UK', 'US')\n"
|
||||
+ "GROUP BY GROUPING SETS ((channel_desc, calendar_month_desc, co.country_id), (channel_desc, co.country_id), "
|
||||
+ "(calendar_month_desc, co.country_id));";
|
||||
String expected = "SELECT channel_desc, calendar_month_desc, co.country_id\n" +
|
||||
"\t, TO_CHAR(SUM(amount_sold), '9,999,999,999') AS SALES$\n" +
|
||||
"FROM sales, customers, times, channels, countries co\n" +
|
||||
"WHERE sales.time_id = times.time_id\n" +
|
||||
"\tAND sales.cust_id = customers.cust_id\n" +
|
||||
"\tAND sales.channel_id = channels.channel_id\n" +
|
||||
"\tAND customers.country_id = co.country_id\n" +
|
||||
"\tAND channels.channel_desc IN ('Direct Sales', 'Internet')\n" +
|
||||
"\tAND times.calendar_month_desc IN ('2000-09', '2000-10')\n" +
|
||||
"\tAND co.country_id IN ('UK', 'US')\n" +
|
||||
"GROUP BY GROUPING SETS ((channel_desc, calendar_month_desc, co.country_id), (channel_desc, co.country_id), (calendar_month_desc, co.country_id));";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
|
||||
|
||||
@@ -89,8 +89,11 @@ public class OracleCreateViewTest2 extends OracleTest {
|
||||
"AS\n" +
|
||||
"SELECT db.db_key, db.db_id, bs.bs_key, bs.bs_recid AS recid, bs.bs_stamp AS stamp\n" +
|
||||
"\t, bs.set_stamp, bs.set_count, bs.bck_type AS backup_type, bs.incr_level AS incremental_level, bs.pieces\n" +
|
||||
"\t, bs.start_time, bs.completion_time, abs((bs.completion_time - bs.start_time) * 86400) AS elapsed_seconds, bs.status, bs.controlfile_included\n" +
|
||||
"\t, bs.input_file_scan_only, decode(keep_options, 0, 'NO', 'YES') AS keep, keep_until, decode(keep_options, 256, 'LOGS', 512, 'NOLOGS', 1024, 'CONSISTENT', NULL) AS keep_options\n" +
|
||||
"\t, bs.start_time, bs.completion_time, abs((bs.completion_time - bs.start_time) * 86400) AS elapsed_seconds, bs.status\n" +
|
||||
"\t, bs.controlfile_included, bs.input_file_scan_only\n" +
|
||||
"\t, decode(keep_options, 0, 'NO', 'YES') AS keep\n" +
|
||||
"\t, keep_until\n" +
|
||||
"\t, decode(keep_options, 256, 'LOGS', 512, 'NOLOGS', 1024, 'CONSISTENT', NULL) AS keep_options\n" +
|
||||
"FROM db, bs\n" +
|
||||
"WHERE db.db_key = bs.db_key",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
@@ -71,8 +71,10 @@ public class OracleCreateViewTest4 extends OracleTest {
|
||||
"\t\"ROW_WAIT_ROW#\"\n" +
|
||||
")\n" +
|
||||
"AS\n" +
|
||||
"SELECT owner AS obj_owner, object_name AS obj_name, object_type AS obj_type, dbms_rowid.rowid_create(1, row_wait_obj#, ROW_WAIT_FILE#, ROW_WAIT_BLOCK#, ROW_WAIT_ROW#) AS obj_rowid, a.username AS db_user\n" +
|
||||
"\t, a.SID AS SID, a.TYPE AS lock_type, a.row_wait_file#, a.row_wait_block#, a.row_wait_row#\n" +
|
||||
"SELECT owner AS obj_owner, object_name AS obj_name, object_type AS obj_type\n" +
|
||||
"\t, dbms_rowid.rowid_create(1, row_wait_obj#, ROW_WAIT_FILE#, ROW_WAIT_BLOCK#, ROW_WAIT_ROW#) AS obj_rowid\n" +
|
||||
"\t, a.username AS db_user, a.SID AS SID, a.TYPE AS lock_type, a.row_wait_file#, a.row_wait_block#\n" +
|
||||
"\t, a.row_wait_row#\n" +
|
||||
"FROM TB_001, (\n" +
|
||||
"\tSELECT /*+ no_merge(a) no_merge(b) */ a.username, a.SID, a.row_wait_obj#, a.ROW_WAIT_FILE#, a.ROW_WAIT_BLOCK#\n" +
|
||||
"\t\t, a.ROW_WAIT_ROW#, b.TYPE\n" +
|
||||
|
||||
@@ -62,7 +62,8 @@ public class OracleCreateViewTest5 extends OracleTest {
|
||||
"\t\"LOCK_MODE\"\n" +
|
||||
")\n" +
|
||||
"AS\n" +
|
||||
"SELECT /*+ no_merge(lo) */ DO.object_name, lo.SESSION_ID, lo.oracle_username, lo.OS_USER_NAME, DECODE(locked_mode, 1, 'SELECT', 2, 'SELECT FOR UPDATE / LOCK ROW SHARE', 3, 'INSERT/UPDATE/DELETE/LOCK ROW EXCLUSIVE', 4, 'CREATE INDEX/LOCK SHARE', 5, 'LOCK SHARE ROW EXCLUSIVE', 6, 'ALTER TABLE/DROP TABLE/DROP INDEX/TRUNCATE TABLE/LOCK EXCLUSIVE') AS sql_actions\n" +
|
||||
"SELECT /*+ no_merge(lo) */ DO.object_name, lo.SESSION_ID, lo.oracle_username, lo.OS_USER_NAME\n" +
|
||||
"\t, DECODE(locked_mode, 1, 'SELECT', 2, 'SELECT FOR UPDATE / LOCK ROW SHARE', 3, 'INSERT/UPDATE/DELETE/LOCK ROW EXCLUSIVE', 4, 'CREATE INDEX/LOCK SHARE', 5, 'LOCK SHARE ROW EXCLUSIVE', 6, 'ALTER TABLE/DROP TABLE/DROP INDEX/TRUNCATE TABLE/LOCK EXCLUSIVE') AS sql_actions\n" +
|
||||
"\t, DECODE(locked_mode, 1, 'NULL', 2, 'SS - SUB SHARE', 3, 'SX - SUB EXCLUSIVE', 4, 'S - SHARE', 5, 'SSX - SHARE/SUB EXCLUSIVE', 6, 'X - EXCLUSIVE') AS Lock_mode\n" +
|
||||
"FROM sys.V_$LOCKED_OBJECT lo, TB_002 DO\n" +
|
||||
"WHERE DO.object_id = lo.object_id",//
|
||||
|
||||
@@ -119,13 +119,16 @@ public class OracleCreateViewTest6 extends OracleTest {
|
||||
"\t\"IS_MERGED\"\n" +
|
||||
")\n" +
|
||||
"AS\n" +
|
||||
"SELECT ID, GMT_CREATE, utl_raw.cast_to_raw(CREATOR) AS CREATOR, GMT_MODIFIED, utl_raw.cast_to_raw(MODIFIER) AS MODIFIER\n" +
|
||||
"\t, IS_DELETED, utl_raw.cast_to_raw(CONTRACT_PARTY) AS CONTRACT_PARTY, utl_raw.cast_to_raw(COMPANY_NAME) AS COMPANY_NAME, utl_raw.cast_to_raw(PRODUCT_TYPE) AS PRODUCT_TYPE, utl_raw.cast_to_raw(PERIOD) AS PERIOD\n" +
|
||||
"\t, JOIN, OVER, CONTRACT_TERM, CONTRACT_CASH, EXEC_CASH\n" +
|
||||
"\t, CANCELED_CASH, RECEIVERD_CASH, O_S_CASH, utl_raw.cast_to_raw(REMARK) AS REMARK, FENTAN_CHECK\n" +
|
||||
"\t, TOTAL_CASH, NOT_CONFIRMED, PRE_RECEIVED, NOT_RECEIVED, CONTRACT_DATE\n" +
|
||||
"\t, utl_raw.cast_to_raw(OWNER_1) AS OWNER_1, AREA_ID_1, utl_raw.cast_to_raw(FIRST_OWNER) AS FIRST_OWNER, FIRST_AREA_ID, utl_raw.cast_to_raw(CONTRACT_ID) AS CONTRACT_ID\n" +
|
||||
"\t, utl_raw.cast_to_raw(IS_RENEW_UPGRADE) AS IS_RENEW_UPGRADE, CATEGORY_ID_1, CATEGORY_ID_2, FIRST_RECEIPT_DATE, utl_raw.cast_to_raw(RECEIPT_REMARK) AS RECEIPT_REMARK\n" +
|
||||
"SELECT ID, GMT_CREATE, utl_raw.cast_to_raw(CREATOR) AS CREATOR, GMT_MODIFIED\n" +
|
||||
"\t, utl_raw.cast_to_raw(MODIFIER) AS MODIFIER, IS_DELETED, utl_raw.cast_to_raw(CONTRACT_PARTY) AS CONTRACT_PARTY\n" +
|
||||
"\t, utl_raw.cast_to_raw(COMPANY_NAME) AS COMPANY_NAME, utl_raw.cast_to_raw(PRODUCT_TYPE) AS PRODUCT_TYPE\n" +
|
||||
"\t, utl_raw.cast_to_raw(PERIOD) AS PERIOD, JOIN, OVER, CONTRACT_TERM\n" +
|
||||
"\t, CONTRACT_CASH, EXEC_CASH, CANCELED_CASH, RECEIVERD_CASH, O_S_CASH\n" +
|
||||
"\t, utl_raw.cast_to_raw(REMARK) AS REMARK, FENTAN_CHECK, TOTAL_CASH, NOT_CONFIRMED\n" +
|
||||
"\t, PRE_RECEIVED, NOT_RECEIVED, CONTRACT_DATE, utl_raw.cast_to_raw(OWNER_1) AS OWNER_1\n" +
|
||||
"\t, AREA_ID_1, utl_raw.cast_to_raw(FIRST_OWNER) AS FIRST_OWNER, FIRST_AREA_ID\n" +
|
||||
"\t, utl_raw.cast_to_raw(CONTRACT_ID) AS CONTRACT_ID, utl_raw.cast_to_raw(IS_RENEW_UPGRADE) AS IS_RENEW_UPGRADE, CATEGORY_ID_1\n" +
|
||||
"\t, CATEGORY_ID_2, FIRST_RECEIPT_DATE, utl_raw.cast_to_raw(RECEIPT_REMARK) AS RECEIPT_REMARK\n" +
|
||||
"\t, utl_raw.cast_to_raw(CONTRACT_SERIAL) AS CONTRACT_SERIAL, IS_MERGED\n" +
|
||||
"FROM TB_002",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
@@ -62,7 +62,13 @@ public class OracleSelectTest38 extends OracleTest {
|
||||
"\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\tSELECT xzqh, sglx\n" +
|
||||
"\t\t\t\t\t, CASE \n" +
|
||||
"\t\t\t\t\t\tWHEN swrs7 < 3 THEN '1'\n" +
|
||||
"\t\t\t\t\t\tWHEN swrs7 < 5 THEN '2'\n" +
|
||||
"\t\t\t\t\t\tWHEN swrs7 <= 9 THEN '3'\n" +
|
||||
"\t\t\t\t\t\tELSE '4'\n" +
|
||||
"\t\t\t\t\tEND 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" +
|
||||
@@ -73,7 +79,10 @@ public class OracleSelectTest38 extends OracleTest {
|
||||
"\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\tSELECT xzqh, nvl(MAX(decode(swrslx, '1', acd_totle)), 0) AS less3\n" +
|
||||
"\t\t\t, nvl(MAX(decode(swrslx, '2', acd_totle)), 0) AS f3to5\n" +
|
||||
"\t\t\t, nvl(MAX(decode(swrslx, '3', acd_totle)), 0) AS f5to9\n" +
|
||||
"\t\t\t, 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" +
|
||||
@@ -103,7 +112,13 @@ public class OracleSelectTest38 extends OracleTest {
|
||||
"\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\tselect xzqh, sglx\n" +
|
||||
"\t\t\t\t\t, case \n" +
|
||||
"\t\t\t\t\t\twhen swrs7 < 3 then '1'\n" +
|
||||
"\t\t\t\t\t\twhen swrs7 < 5 then '2'\n" +
|
||||
"\t\t\t\t\t\twhen swrs7 <= 9 then '3'\n" +
|
||||
"\t\t\t\t\t\telse '4'\n" +
|
||||
"\t\t\t\t\tend 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" +
|
||||
@@ -114,7 +129,10 @@ public class OracleSelectTest38 extends OracleTest {
|
||||
"\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\tselect xzqh, nvl(max(decode(swrslx, '1', acd_totle)), 0) as less3\n" +
|
||||
"\t\t\t, nvl(max(decode(swrslx, '2', acd_totle)), 0) as f3to5\n" +
|
||||
"\t\t\t, nvl(max(decode(swrslx, '3', acd_totle)), 0) as f5to9\n" +
|
||||
"\t\t\t, 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" +
|
||||
|
||||
@@ -66,7 +66,8 @@ public class OracleSelectTest62 extends OracleTest {
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
assertEquals("SELECT channel_desc, calendar_month_desc, co.country_id, TO_CHAR(SUM(amount_sold), '9,999,999,999') AS SALES$\n" +
|
||||
assertEquals("SELECT channel_desc, calendar_month_desc, co.country_id\n" +
|
||||
"\t, TO_CHAR(SUM(amount_sold), '9,999,999,999') AS SALES$\n" +
|
||||
"FROM sales, customers, times, channels, countries co\n" +
|
||||
"WHERE sales.time_id = times.time_id\n" +
|
||||
"\tAND sales.cust_id = customers.cust_id\n" +
|
||||
@@ -81,7 +82,8 @@ public class OracleSelectTest62 extends OracleTest {
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
|
||||
Assert.assertEquals("select channel_desc, calendar_month_desc, co.country_id, TO_CHAR(sum(amount_sold), '9,999,999,999') as SALES$\n" +
|
||||
Assert.assertEquals("select channel_desc, calendar_month_desc, co.country_id\n" +
|
||||
"\t, TO_CHAR(sum(amount_sold), '9,999,999,999') as SALES$\n" +
|
||||
"from sales, customers, times, channels, countries co\n" +
|
||||
"where sales.time_id = times.time_id\n" +
|
||||
"\tand sales.cust_id = customers.cust_id\n" +
|
||||
|
||||
@@ -80,7 +80,11 @@ public class SQLServerSelectTest7 extends TestCase {
|
||||
"\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" +
|
||||
"SELECT t.menu_id AS \"id\", t.menu_name AS \"name\", t.parent_id AS \"pId\"\n" +
|
||||
"\t, CASE t.level\n" +
|
||||
"\t\tWHEN 1 THEN 'true'\n" +
|
||||
"\t\tELSE 'false'\n" +
|
||||
"\tEND AS \"open\", t.link_type AS \"linkType\"\n" +
|
||||
"FROM menu_view t\n" +
|
||||
"WHERE 1 = 1\n" +
|
||||
"\tAND t.deleted = 0\n" +
|
||||
|
||||
@@ -40,24 +40,28 @@ public class SQLServerUpdateTest extends TestCase {
|
||||
" ) b " + //
|
||||
"where a.FId = b.FId and a.FId = ?";
|
||||
|
||||
String expect = "UPDATE reg_student_charge_item\n" +
|
||||
"SET FAmountReceived = b.amount\n" +
|
||||
"FROM reg_student_charge_item a, (\n" +
|
||||
"\t\tSELECT a.FId, CASE WHEN SUM(b.FChargeAmount) IS NULL THEN 0 ELSE SUM(b.FChargeAmount) END AS amount\n" +
|
||||
"\t\tFROM reg_student_charge_item a\n" +
|
||||
"\t\t\tLEFT JOIN reg_student_charge_daybook b ON a.FId = b.FChargeItemId\n" +
|
||||
"\t\tWHERE a.FId = ?\n" +
|
||||
"\t\tGROUP BY a.FId\n" +
|
||||
"\t) b\n" +
|
||||
"WHERE a.FId = b.FId\n" +
|
||||
"\tAND a.FId = ?";
|
||||
|
||||
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
String text = TestUtils.outputSqlServer(stmt);
|
||||
|
||||
Assert.assertEquals(expect, text);
|
||||
assertEquals("UPDATE reg_student_charge_item\n" +
|
||||
"SET FAmountReceived = b.amount\n" +
|
||||
"FROM reg_student_charge_item a, (\n" +
|
||||
"\t\tSELECT a.FId\n" +
|
||||
"\t\t\t, CASE \n" +
|
||||
"\t\t\t\tWHEN SUM(b.FChargeAmount) IS NULL THEN 0\n" +
|
||||
"\t\t\t\tELSE SUM(b.FChargeAmount)\n" +
|
||||
"\t\t\tEND AS amount\n" +
|
||||
"\t\tFROM reg_student_charge_item a\n" +
|
||||
"\t\t\tLEFT JOIN reg_student_charge_daybook b ON a.FId = b.FChargeItemId\n" +
|
||||
"\t\tWHERE a.FId = ?\n" +
|
||||
"\t\tGROUP BY a.FId\n" +
|
||||
"\t) b\n" +
|
||||
"WHERE a.FId = b.FId\n" +
|
||||
"\tAND a.FId = ?", text);
|
||||
|
||||
// System.out.println(text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user