Skip to content

Commit

Permalink
for #2777, replace PARENTHESES on SQLUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 29, 2019
1 parent a0eb741 commit cad41c7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.core.parse.core.constant;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
Expand All @@ -25,23 +26,24 @@
* @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.
*
* @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;
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,12 +76,25 @@ public static String getExactlyValue(final String value) {
/**
* Get exactly SQL expression.
*
* <p>remove space for SQL expression</p>
* <p>remove space for SQL expression and outside parentheses</p>
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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('{', ']'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));
}
}

0 comments on commit cad41c7

Please sign in to comment.