Skip to content

Commit

Permalink
Merge pull request #4838 from abomb4/master
Browse files Browse the repository at this point in the history
在 DB2 数据库 INTERVAL 表达式输出时,将 VALUE 用括号包装
  • Loading branch information
wenshao authored Aug 1, 2022
2 parents de0e199 + 087ac65 commit 117ade4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ protected void printOperator(SQLBinaryOperator operator) {

public boolean visit(SQLIntervalExpr x) {
SQLExpr value = x.getValue();
print('(');
value.accept(this);
print(')');

SQLIntervalUnit unit = x.getUnit();
if (unit != null) {
Expand Down
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;
}
}
}

0 comments on commit 117ade4

Please sign in to comment.