Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 DB2 数据库 INTERVAL 表达式输出时,将 VALUE 用括号包装 #4838

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}