From cad41c74217cee412452b4f40037f3e8a63f5693 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 29 Jul 2019 13:06:27 +0800 Subject: [PATCH] for #2777, replace PARENTHESES on SQLUtil --- .../core/parse/core/SQLParseKernel.java | 3 +++ .../core/parse/core/constant/Paren.java | 16 ++++++++------- .../core/parse/util/SQLUtil.java | 19 ++++++++++++++++-- .../core/parse/core/constant/ParenTest.java | 20 +++++++++---------- .../core/parse/util/SQLUtilTest.java | 14 ++++++++----- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/SQLParseKernel.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/SQLParseKernel.java index cf7ea846acb12..c140f7087c7fa 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/SQLParseKernel.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/SQLParseKernel.java @@ -47,6 +47,9 @@ public final class SQLParseKernel { public SQLParseKernel(final ParseRuleRegistry parseRuleRegistry, final DatabaseType databaseType, final String sql) { DatabaseType trunkDatabaseType = DatabaseTypes.getTrunkDatabaseType(databaseType.getName()); + if (sql.equals("SELECT DISTINCT(item_id) FROM t_order_item ORDER BY item_id")) { + System.out.println("-----------------" + sql); + } parserEngine = new SQLParserEngine(parseRuleRegistry, trunkDatabaseType, sql); extractorEngine = new SQLSegmentsExtractorEngine(); fillerEngine = new SQLStatementFillerEngine(parseRuleRegistry, trunkDatabaseType); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/constant/Paren.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/constant/Paren.java index 1da258572b218..529ea96b10868 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/constant/Paren.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/core/constant/Paren.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.core.parse.core.constant; +import lombok.Getter; import lombok.RequiredArgsConstructor; /** @@ -25,13 +26,14 @@ * @author zhangliang */ @RequiredArgsConstructor +@Getter public enum Paren { - PARENTHESES("(", ")"), BRACKET("[", "]"), BRACES("{", "}"); + PARENTHESES('(', ')'), BRACKET('[', ']'), BRACES('{', '}'); - private final String leftParen; + private final char leftParen; - private final String rightParen; + private final char rightParen; /** * Judge passed token is left paren or not. @@ -39,9 +41,9 @@ public enum Paren { * @param token token * @return is left paren or not */ - public static boolean isLeftParen(final String token) { + public static boolean isLeftParen(final char token) { for (Paren each : Paren.values()) { - if (each.leftParen.equals(token)) { + if (each.leftParen == token) { return true; } } @@ -55,9 +57,9 @@ public static boolean isLeftParen(final String token) { * @param rightToken right token * @return match or not */ - public static boolean match(final String leftToken, final String rightToken) { + public static boolean match(final char leftToken, final char rightToken) { for (Paren each : Paren.values()) { - if (each.leftParen.equals(leftToken) && each.rightParen.equals(rightToken)) { + if (each.leftParen == leftToken && each.rightParen == rightToken) { return true; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/util/SQLUtil.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/util/SQLUtil.java index 8635ff577b413..bfffa111adcfe 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/util/SQLUtil.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/util/SQLUtil.java @@ -18,8 +18,10 @@ package org.apache.shardingsphere.core.parse.util; import com.google.common.base.CharMatcher; +import com.google.common.base.Strings; import lombok.AccessLevel; import lombok.AllArgsConstructor; +import org.apache.shardingsphere.core.parse.core.constant.Paren; import java.math.BigDecimal; import java.math.BigInteger; @@ -74,12 +76,25 @@ public static String getExactlyValue(final String value) { /** * Get exactly SQL expression. * - *

remove space for SQL expression

+ *

remove space for SQL expression and outside parentheses

* * @param value SQL expression * @return exactly SQL expression */ public static String getExactlyExpression(final String value) { - return null == value ? null : CharMatcher.anyOf(" ").removeFrom(value); + return Strings.isNullOrEmpty(value) ? value : CharMatcher.anyOf(" ").removeFrom(removeOutsideParentheses(value)); + } + + private static String removeOutsideParentheses(final String value) { + int parenthesesOffset = 0; + while (value.charAt(parenthesesOffset) == Paren.PARENTHESES.getLeftParen()) { + parenthesesOffset++; + } + if (0 != parenthesesOffset) { + String result = value.substring(parenthesesOffset, value.length()); + result = result.substring(0, result.length() - parenthesesOffset); + return result; + } + return value; } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/core/constant/ParenTest.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/core/constant/ParenTest.java index 66965c185bf40..8f45ff06ad031 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/core/constant/ParenTest.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/core/constant/ParenTest.java @@ -26,43 +26,43 @@ public final class ParenTest { @Test public void assertIsLeftParenForParentheses() { - assertTrue(Paren.isLeftParen("(")); + assertTrue(Paren.isLeftParen('(')); } @Test public void assertIsLeftParenForBracket() { - assertTrue(Paren.isLeftParen("[")); + assertTrue(Paren.isLeftParen('[')); } @Test public void assertIsLeftParenForBraces() { - assertTrue(Paren.isLeftParen("{")); + assertTrue(Paren.isLeftParen('{')); } @Test public void assertIsNotLeftParen() { - assertFalse(Paren.isLeftParen(")")); - assertFalse(Paren.isLeftParen("]")); - assertFalse(Paren.isLeftParen("}")); + assertFalse(Paren.isLeftParen(')')); + assertFalse(Paren.isLeftParen(']')); + assertFalse(Paren.isLeftParen('}')); } @Test public void assertMatchForParentheses() { - assertTrue(Paren.match("(", ")")); + assertTrue(Paren.match('(', ')')); } @Test public void assertMatchForBracket() { - assertTrue(Paren.match("[", "]")); + assertTrue(Paren.match('[', ']')); } @Test public void assertMatchForBraces() { - assertTrue(Paren.match("{", "}")); + assertTrue(Paren.match('{', '}')); } @Test public void assertNotMatch() { - assertFalse(Paren.match("{", "]")); + assertFalse(Paren.match('{', ']')); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/util/SQLUtilTest.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/util/SQLUtilTest.java index 498179329db69..86ba207c6d411 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/util/SQLUtilTest.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/test/java/org/apache/shardingsphere/core/parse/util/SQLUtilTest.java @@ -23,7 +23,6 @@ import java.math.BigInteger; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -66,18 +65,23 @@ public void assertGetExactlyValue() { assertThat(SQLUtil.getExactlyValue("'xxx'"), is("xxx")); } + @Test + public void assertGetExactlyValueUsingNull() { + assertNull(SQLUtil.getExactlyValue(null)); + } + @Test public void assertGetExactlyExpressionUsingAndReturningNull() { assertNull(SQLUtil.getExactlyExpression(null)); } @Test - public void testGetExactlyExpressionUsingAndReturningEmptyString() { - assertEquals("", SQLUtil.getExactlyExpression("")); + public void assertGetExactlyExpressionUsingAndReturningEmptyString() { + assertThat(SQLUtil.getExactlyExpression(""), is("")); } @Test - public void assertGetExactlyValueUsingNull() { - assertNull(SQLUtil.getExactlyValue(null)); + public void assertGetExactlyExpression() { + assertThat(SQLUtil.getExactlyExpression("((a + b*c))"), is("a+b*c")); } }