This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 57bbca54ac6 Refactor SQLParsingException (#20300)
57bbca54ac6 is described below
commit 57bbca54ac6778b2b28c9d3443bae3b1831d013e
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Aug 19 18:56:32 2022 +0800
Refactor SQLParsingException (#20300)
* Refactor SQLParsingException
* Refactor SQLParsingException
* Refactor SQLParsingException
---
docs/document/content/user-manual/error-code/_index.cn.md | 2 +-
docs/document/content/user-manual/error-code/_index.en.md | 2 +-
.../core/featured/FeaturedDistSQLStatementParserEngine.java | 2 +-
.../core/utility/UtilityDistSQLStatementParserEngine.java | 2 +-
.../shadow/distsql/parser/ShadowDistSQLTest.java | 4 ++--
.../backend/handler/distsql/rul/sql/FormatSQLHandler.java | 7 +------
.../handler/distsql/rul/sql/ParseDistSQLHandler.java | 7 +------
.../proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java | 4 ++--
.../sql/parser/core/database/parser/SQLParserExecutor.java | 2 +-
.../sql/parser/core/database/visitor/SQLVisitorFactory.java | 2 +-
.../sql/parser/exception/SQLParsingException.java | 13 ++-----------
.../engine/UnsupportedSQLParserParameterizedTest.java | 2 ++
12 files changed, 16 insertions(+), 33 deletions(-)
diff --git a/docs/document/content/user-manual/error-code/_index.cn.md
b/docs/document/content/user-manual/error-code/_index.cn.md
index 56c8ec44f95..d4139b51db0 100644
--- a/docs/document/content/user-manual/error-code/_index.cn.md
+++ b/docs/document/content/user-manual/error-code/_index.cn.md
@@ -11,7 +11,7 @@ chapter = true
| --------- | ----------- | ------ |
| 01000 | 10000 | Circuit break open, the request has been ignored |
| 08000 | 10001 | The URL \`%s\` is not recognized, please refer to
the pattern \`%s\` |
-| 42000 | 11000 | You have an error in your SQL syntax |
+| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
| 42000 | 11003 | Rule does not exist |
diff --git a/docs/document/content/user-manual/error-code/_index.en.md
b/docs/document/content/user-manual/error-code/_index.en.md
index ea3cb18bcc0..9a625b54f3b 100644
--- a/docs/document/content/user-manual/error-code/_index.en.md
+++ b/docs/document/content/user-manual/error-code/_index.en.md
@@ -11,7 +11,7 @@ This chapter lists all error codes of Apache ShardingSphere,
it provides by stan
| --------- | ----------- | ------ |
| 01000 | 10000 | Circuit break open, the request has been ignored |
| 08000 | 10001 | The URL \`%s\` is not recognized, please refer to
the pattern \`%s\` |
-| 42000 | 11000 | You have an error in your SQL syntax |
+| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
| 42000 | 11003 | Rule does not exist |
diff --git
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
index 95473d245d8..19dce70a39e 100644
---
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
+++
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
@@ -52,7 +52,7 @@ public final class FeaturedDistSQLStatementParserEngine {
} catch (final ParseCancellationException | SQLParsingException
ignored) {
}
}
- throw new SQLParsingException();
+ throw new SQLParsingException(sql);
}
@SneakyThrows(ReflectiveOperationException.class)
diff --git
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
index 3c80b4826a8..b8b7b2c38e9 100644
---
a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
+++
b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/utility/UtilityDistSQLStatementParserEngine.java
@@ -45,7 +45,7 @@ public final class UtilityDistSQLStatementParserEngine {
try {
return SQLParserFactory.newInstance(sql,
UtilityDistSQLLexer.class, UtilityDistSQLParser.class).parse();
} catch (final ParseCancellationException | SQLParsingException
ignored) {
- throw new SQLParsingException();
+ throw new SQLParsingException(sql);
}
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/test/java/org/apache/shardingsphere/shadow/distsql/parser/ShadowDistSQLTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/test/java/org/apache/shardingsphere/shadow/distsql/parser/ShadowDistSQLTest.java
index c8b0e941c41..4fe33417fe0 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/test/java/org/apache/shardingsphere/shadow/distsql/parser/ShadowDistSQLTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/test/java/org/apache/shardingsphere/shadow/distsql/parser/ShadowDistSQLTest.java
@@ -45,7 +45,7 @@ public final class ShadowDistSQLTest {
assertThat(shadowDistSQLStatement.getRules().size(), is(1));
assertShadowRuleSegment(shadowDistSQLStatement.getRules().iterator().next());
}
-
+
@Test
public void assertAlterShadowRule() {
String sql = "ALTER SHADOW RULE
`shadow_rule`(SOURCE=demo_ds,SHADOW=demo_ds_shadow,t_order((simple_hint_algorithm,TYPE(NAME='SIMPLE_HINT',PROPERTIES('shadow'='true','foo'='bar')))))";
@@ -53,7 +53,7 @@ public final class ShadowDistSQLTest {
assertThat(shadowDistSQLStatement.getRules().size(), is(1));
assertShadowRuleSegment(shadowDistSQLStatement.getRules().iterator().next());
}
-
+
private void assertShadowRuleSegment(final ShadowRuleSegment
shadowRuleSegment) {
assertThat(shadowRuleSegment.getRuleName(), is("shadow_rule"));
assertThat(shadowRuleSegment.getSource(), is("demo_ds"));
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
index f5ef875eda7..9a507e7a305 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/FormatSQLHandler.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.api.SQLParserEngine;
import org.apache.shardingsphere.sql.parser.api.SQLVisitorEngine;
import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
-import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import java.sql.SQLException;
import java.util.Collection;
@@ -58,10 +57,6 @@ public final class FormatSQLHandler extends
SQLRULBackendHandler<FormatStatement
}
private ParseASTNode parseSQL(final String sql, final String databaseType)
{
- try {
- return new SQLParserEngine(databaseType, new CacheOption(1,
1L)).parse(sql, false);
- } catch (final SQLParsingException ex) {
- throw new SQLParsingException();
- }
+ return new SQLParserEngine(databaseType, new CacheOption(1,
1L)).parse(sql, false);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
index f7c7710ecaf..7ba53c587c0 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/ParseDistSQLHandler.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryRes
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.rul.SQLRULBackendHandler;
-import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
@@ -53,10 +52,6 @@ public final class ParseDistSQLHandler extends
SQLRULBackendHandler<ParseStateme
private SQLStatement parseSQL(final ContextManager contextManager) {
SQLParserRule sqlParserRule =
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
String databaseType =
getConnectionSession().getDatabaseType().getType();
- try {
- return
sqlParserRule.getSQLParserEngine(databaseType).parse(getSqlStatement().getSql(),
false);
- } catch (final SQLParsingException ex) {
- throw new SQLParsingException();
- }
+ return
sqlParserRule.getSQLParserEngine(databaseType).parse(getSqlStatement().getSql(),
false);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
index 029dea010f1..95c26486ad7 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactoryTest.java
@@ -160,14 +160,14 @@ public final class MySQLErrPacketFactoryTest {
@Test
public void assertNewInstanceWithSQLParsingException() {
- assertSQLParsingException(MySQLErrPacketFactory.newInstance(new
SQLParsingException()));
+ assertSQLParsingException(MySQLErrPacketFactory.newInstance(new
SQLParsingException("test")));
}
private void assertSQLParsingException(final MySQLErrPacket actual) {
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(11000));
assertThat(actual.getSqlState(), is("42000"));
- assertThat(actual.getErrorMessage(), is("You have an error in your SQL
syntax"));
+ assertThat(actual.getErrorMessage(), is("You have an error in your SQL
syntax: test"));
}
@Test
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
index 4e81b5c649f..fb510a8f9d7 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/parser/SQLParserExecutor.java
@@ -62,7 +62,7 @@ public final class SQLParserExecutor {
try {
return (ParseASTNode) sqlParser.parse();
} catch (final ParseCancellationException e) {
- throw new SQLParsingException();
+ throw new SQLParsingException(sql);
}
}
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
index 97fa8e0ad38..7b042231fea 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorFactory.java
@@ -65,7 +65,7 @@ public final class SQLVisitorFactory {
case RL:
return (ParseTreeVisitor<T>)
visitorFacade.getRLVisitorClass().getConstructor(Properties.class).newInstance(props);
default:
- throw new SQLParsingException(type);
+ throw new SQLParsingException(type.name());
}
}
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
index 36a468953b8..ff125091730 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/exception/SQLParsingException.java
@@ -19,24 +19,15 @@ package org.apache.shardingsphere.sql.parser.exception;
import
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
import
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
/**
- * Throw exception when SQL parsing error.
+ * SQL parsing exception.
*/
public final class SQLParsingException extends ShardingSphereSQLException {
private static final long serialVersionUID = -6408790652103666096L;
- public SQLParsingException() {
- super(XOpenSQLState.SYNTAX_ERROR, 11000, "You have an error in your
SQL syntax");
- }
-
public SQLParsingException(final String sql) {
- super(XOpenSQLState.SYNTAX_ERROR, 11000, "Unsupported SQL: %s", sql);
- }
-
- public SQLParsingException(final SQLStatementType type) {
- super(XOpenSQLState.SYNTAX_ERROR, 11000, "Unsupported SQL statement
type: `%s`", type.toString());
+ super(XOpenSQLState.SYNTAX_ERROR, 11000, "You have an error in your
SQL syntax: %s", sql);
}
}
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/engine/UnsupportedSQLParserParameterizedTest.java
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/engine/UnsupportedSQLParserParameterizedTest.java
index ceaa91d13c9..7107c72029f 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/engine/UnsupportedSQLParserParameterizedTest.java
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/engine/UnsupportedSQLParserParameterizedTest.java
@@ -48,11 +48,13 @@ public abstract class UnsupportedSQLParserParameterizedTest
{
}
@Test(expected = Exception.class)
+ // TODO should expect SQLParsingException only
public final void assertUnsupportedSQL() {
String sql = SQL_CASES_LOADER.getCaseValue(sqlCaseId, sqlCaseType,
Collections.emptyList(), databaseType);
String databaseType = "H2".equals(this.databaseType) ? "MySQL" :
this.databaseType;
CacheOption cacheOption = new CacheOption(128, 1024L);
ParseASTNode parseContext = new SQLParserEngine(databaseType,
cacheOption).parse(sql, false);
+ // TODO remove SQLStatement sqlStatement =
SQLStatement sqlStatement = new SQLVisitorEngine(databaseType,
"STATEMENT", true, new Properties()).visit(parseContext);
}
}