-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4838 from abomb4/master
在 DB2 数据库 INTERVAL 表达式输出时,将 VALUE 用括号包装
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/com/alibaba/druid/sql/dialect/db2/visitor/DB2OutputVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.alibaba.druid.sql.dialect.db2.visitor; | ||
|
||
import com.alibaba.druid.DbType; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.parser.SQLParserUtils; | ||
import com.alibaba.druid.sql.parser.SQLStatementParser; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author abomb4 2022-06-19 | ||
*/ | ||
public class DB2OutputVisitorTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
List<TestCase> testCases = Arrays.asList( | ||
new TestCase("interval 表达式应该带括号 select", | ||
"SELECT A, B, C FROM D WHERE C < CURRENT TIMESTAMP + (10 * 60) SECONDS", | ||
"SELECT A, B, C FROM D WHERE C < CURRENT TIMESTAMP + (10 * 60) SECONDS"), | ||
new TestCase("interval 表达式应该带括号 insert", | ||
"INSERT INTO TEST VALUES (1, CURRENT TIMESTAMP + ((10 + 60) SECONDS))", | ||
"INSERT INTO TEST VALUES (1, CURRENT TIMESTAMP + (10 + 60) SECONDS)"), | ||
new TestCase("interval 表达式应该带括号 update", | ||
"UPDATE test SET created = CURRENT TIMESTAMP + 10 SECONDS WHERE created < CURRENT TIMESTAMP + (10 * 60) SECONDS", | ||
"UPDATE test SET created = CURRENT TIMESTAMP + (10) SECONDS WHERE created < CURRENT TIMESTAMP + (10 * 60) SECONDS") | ||
); | ||
|
||
for (TestCase testCase : testCases) { | ||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(testCase.origin, DbType.db2); | ||
StringBuilder builder = new StringBuilder(); | ||
DB2OutputVisitor visitor = new DB2OutputVisitor(builder); | ||
visitor.setUppCase(false); | ||
visitor.setPrettyFormat(false); | ||
visitor.setParameterized(false); | ||
SQLStatement stmt = parser.parseStatement(); | ||
stmt.accept(visitor); | ||
String result = builder.toString(); | ||
Assert.assertEquals(testCase.name, testCase.expected, result); | ||
} | ||
System.out.println("DB2 Interval test ok."); | ||
} | ||
|
||
private static class TestCase { | ||
String name; | ||
String origin; | ||
String expected; | ||
|
||
public TestCase(String name, String origin, String expected) { | ||
this.name = name; | ||
this.origin = origin; | ||
this.expected = expected; | ||
} | ||
} | ||
} |