This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 2ad42b1 optimize postgresql dollar parameter marker parse logic
(#14668)
2ad42b1 is described below
commit 2ad42b13b3aa398ca5634ebdbcb1b6822789d656
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Jan 10 19:13:11 2022 +0800
optimize postgresql dollar parameter marker parse logic (#14668)
* optimize postgresql dollar parameter marker parse logic
* fix checkstyle and update unit test
---
.../statement/impl/MySQLStatementSQLVisitor.java | 6 ++-
.../src/main/antlr4/imports/opengauss/BaseRule.g4 | 6 +--
.../impl/OpenGaussStatementSQLVisitor.java | 13 +++++-
.../statement/impl/OracleStatementSQLVisitor.java | 9 ++--
.../src/main/antlr4/imports/postgresql/BaseRule.g4 | 7 +--
.../impl/PostgreSQLStatementSQLVisitor.java | 13 +++++-
.../statement/impl/SQL92StatementSQLVisitor.java | 9 ++--
.../impl/SQLServerStatementSQLVisitor.java | 9 ++--
.../ParameterMarkerType.java} | 11 ++---
.../simple/ParameterMarkerExpressionSegment.java | 12 ++++-
.../parametermarker/ParameterMarkerValue.java | 3 ++
.../jaxb/sql/loader/SQLCasesLoader.java | 52 +++++++++++-----------
.../src/main/resources/case/dml/select.xml | 49 +++++++++++++-------
.../main/resources/sql/supported/dml/select.xml | 2 +-
14 files changed, 127 insertions(+), 74 deletions(-)
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
index 072d2f2..c86e386 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
@@ -131,6 +131,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowF
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.JoinType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.UnionType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
@@ -222,7 +223,7 @@ public abstract class MySQLStatementSQLVisitor extends
MySQLStatementBaseVisitor
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -533,7 +534,8 @@ public abstract class MySQLStatementSQLVisitor extends
MySQLStatementBaseVisitor
return new SubqueryExpressionSegment(subquerySegment);
}
if (null != ctx.parameterMarker()) {
- return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.literals()) {
return SQLUtil.createLiteralExpression(visit(ctx.literals()),
startIndex, stopIndex, ctx.literals().start.getInputStream().getText(new
Interval(startIndex, stopIndex)));
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/antlr4/imports/opengauss/BaseRule.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/antlr4/imports/opengauss/BaseRule.g4
index 9dedf8b..b5a3962 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/antlr4/imports/opengauss/BaseRule.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/antlr4/imports/opengauss/BaseRule.g4
@@ -21,11 +21,7 @@ import Keyword, OpenGaussKeyword, Symbol, Literals;
parameterMarker
: QUESTION_ literalsType?
- | POSITIONAL_PARAMETER_
- ;
-
-POSITIONAL_PARAMETER_
- : DOLLAR_ INT_
+ | DOLLAR_ numberLiterals
;
reservedKeyword
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
index 79aeb54..d63e92d 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
@@ -101,6 +101,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.Whe
import
org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowClauseContext;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.UnionType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
@@ -190,7 +191,14 @@ public abstract class OpenGaussStatementSQLVisitor extends
OpenGaussStatementBas
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ if (null != ctx.DOLLAR_()) {
+ int parameterIndex = ((NumberLiteralValue)
visit(ctx.numberLiterals())).getValue().intValue();
+ if (parameterIndex > currentParameterIndex) {
+ currentParameterIndex = parameterIndex;
+ }
+ return new ParameterMarkerValue(parameterIndex - 1,
ParameterMarkerType.DOLLAR);
+ }
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -314,7 +322,8 @@ public abstract class OpenGaussStatementSQLVisitor extends
OpenGaussStatementBas
return visit(ctx.columnref());
}
if (null != ctx.parameterMarker()) {
- return new
ParameterMarkerExpressionSegment(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), ((ParameterMarkerValue)
visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new
ParameterMarkerExpressionSegment(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.aexprConst()) {
ASTNode astNode = visit(ctx.aexprConst());
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
index 29f7ff0..59612407 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
@@ -68,6 +68,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.Unrese
import
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ViewNameContext;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndextypeSegment;
@@ -131,7 +132,7 @@ public abstract class OracleStatementSQLVisitor extends
OracleStatementBaseVisit
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -416,7 +417,8 @@ public abstract class OracleStatementSQLVisitor extends
OracleStatementBaseVisit
return new LiteralExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
}
if (astNode instanceof ParameterMarkerValue) {
- return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
astNode;
+ return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), parameterMarker.getValue(),
parameterMarker.getType());
}
if (astNode instanceof SubquerySegment) {
return new SubqueryExpressionSegment((SubquerySegment) astNode);
@@ -435,7 +437,8 @@ public abstract class OracleStatementSQLVisitor extends
OracleStatementBaseVisit
return new SubquerySegment(startIndex, stopIndex,
(OracleSelectStatement) visit(ctx.subquery()));
}
if (null != ctx.parameterMarker()) {
- return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.literals()) {
return SQLUtil.createLiteralExpression(visit(ctx.literals()),
startIndex, stopIndex, ctx.literals().start.getInputStream().getText(new
Interval(startIndex, stopIndex)));
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/BaseRule.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/BaseRule.g4
index b0896bb..de68b0f 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/BaseRule.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/BaseRule.g4
@@ -21,13 +21,8 @@ import Keyword, PostgreSQLKeyword, Symbol, Literals;
parameterMarker
: QUESTION_ literalsType?
- | POSITIONAL_PARAMETER_
+ | DOLLAR_ numberLiterals
;
-
-POSITIONAL_PARAMETER_
- : DOLLAR_ INT_
- ;
-
reservedKeyword
: ALL
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
index c16bf45..0abe939 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
@@ -99,6 +99,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.Wh
import
org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WindowClauseContext;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.UnionType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
@@ -187,7 +188,14 @@ public abstract class PostgreSQLStatementSQLVisitor
extends PostgreSQLStatementB
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ if (null != ctx.DOLLAR_()) {
+ int parameterIndex = ((NumberLiteralValue)
visit(ctx.numberLiterals())).getValue().intValue();
+ if (parameterIndex > currentParameterIndex) {
+ currentParameterIndex = parameterIndex;
+ }
+ return new ParameterMarkerValue(parameterIndex - 1,
ParameterMarkerType.DOLLAR);
+ }
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -311,7 +319,8 @@ public abstract class PostgreSQLStatementSQLVisitor extends
PostgreSQLStatementB
return visit(ctx.columnref());
}
if (null != ctx.parameterMarker()) {
- return new
ParameterMarkerExpressionSegment(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), ((ParameterMarkerValue)
visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new
ParameterMarkerExpressionSegment(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.aexprConst()) {
ASTNode astNode = visit(ctx.aexprConst());
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92StatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92StatementSQLVisitor.java
index 80d2dfa..d8183f4 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92StatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92StatementSQLVisitor.java
@@ -60,6 +60,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.TableNa
import
org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.UnreservedWordContext;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
@@ -117,7 +118,7 @@ public abstract class SQL92StatementSQLVisitor extends
SQL92StatementBaseVisitor
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -355,7 +356,8 @@ public abstract class SQL92StatementSQLVisitor extends
SQL92StatementBaseVisitor
return new LiteralExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
}
if (astNode instanceof ParameterMarkerValue) {
- return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
astNode;
+ return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), parameterMarker.getValue(),
parameterMarker.getType());
}
if (astNode instanceof SubquerySegment) {
return new SubqueryExpressionSegment((SubquerySegment) astNode);
@@ -374,7 +376,8 @@ public abstract class SQL92StatementSQLVisitor extends
SQL92StatementBaseVisitor
return new SubquerySegment(startIndex, stopIndex,
(SQL92SelectStatement) visit(ctx.subquery()));
}
if (null != ctx.parameterMarker()) {
- return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.literals()) {
return SQLUtil.createLiteralExpression(visit(ctx.literals()),
startIndex, stopIndex, ctx.literals().start.getInputStream().getText(new
Interval(startIndex, stopIndex)));
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerStatementSQLVisitor.java
index 88a7282..f7df82b 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerStatementSQLVisitor.java
@@ -97,6 +97,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Whe
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WithClauseContext;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
@@ -190,7 +191,7 @@ public abstract class SQLServerStatementSQLVisitor extends
SQLServerStatementBas
@Override
public final ASTNode visitParameterMarker(final ParameterMarkerContext
ctx) {
- return new ParameterMarkerValue(currentParameterIndex++);
+ return new ParameterMarkerValue(currentParameterIndex++,
ParameterMarkerType.QUESTION);
}
@Override
@@ -447,7 +448,8 @@ public abstract class SQLServerStatementSQLVisitor extends
SQLServerStatementBas
return new LiteralExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
}
if (astNode instanceof ParameterMarkerValue) {
- return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
astNode;
+ return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), parameterMarker.getValue(),
parameterMarker.getType());
}
if (astNode instanceof SubquerySegment) {
return new SubqueryExpressionSegment((SubquerySegment) astNode);
@@ -466,7 +468,8 @@ public abstract class SQLServerStatementSQLVisitor extends
SQLServerStatementBas
return new SubquerySegment(startIndex, stopIndex,
(SQLServerSelectStatement) visit(ctx.subquery()));
}
if (null != ctx.parameterMarker()) {
- return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
+ ParameterMarkerValue parameterMarker = (ParameterMarkerValue)
visit(ctx.parameterMarker());
+ return new ParameterMarkerExpressionSegment(startIndex, stopIndex,
parameterMarker.getValue(), parameterMarker.getType());
}
if (null != ctx.literals()) {
return SQLUtil.createLiteralExpression(visit(ctx.literals()),
startIndex, stopIndex, ctx.literals().start.getInputStream().getText(new
Interval(startIndex, stopIndex)));
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/ParameterMarkerType.java
similarity index 75%
copy from
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
copy to
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/ParameterMarkerType.java
index 86bd7b2..68cc580 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/ParameterMarkerType.java
@@ -15,18 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker;
+package org.apache.shardingsphere.sql.parser.sql.common.constant;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sql.parser.sql.common.value.ValueASTNode;
/**
- * Parameter marker value.
+ * Parameter marker type enum.
*/
@RequiredArgsConstructor
@Getter
-public final class ParameterMarkerValue implements ValueASTNode<Integer> {
+public enum ParameterMarkerType {
- private final Integer value;
+ QUESTION("?"), DOLLAR("$");
+
+ private final String marker;
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
index 1cbd810..0414090 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
@@ -22,6 +22,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasAvailable;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
@@ -42,9 +43,18 @@ public class ParameterMarkerExpressionSegment implements
SimpleExpressionSegment
private final int stopIndex;
private final int parameterMarkerIndex;
-
+
+ private final ParameterMarkerType parameterMarkerType;
+
@Setter
private AliasSegment alias;
+
+ public ParameterMarkerExpressionSegment(final int startIndex, final int
stopIndex, final int parameterMarkerIndex) {
+ this.startIndex = startIndex;
+ this.stopIndex = stopIndex;
+ this.parameterMarkerIndex = parameterMarkerIndex;
+ this.parameterMarkerType = ParameterMarkerType.QUESTION;
+ }
@Override
public Optional<String> getAlias() {
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
index 86bd7b2..220563a 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/parametermarker/ParameterMarkerValue.java
@@ -19,6 +19,7 @@ package
org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.sql.parser.sql.common.value.ValueASTNode;
/**
@@ -29,4 +30,6 @@ import
org.apache.shardingsphere.sql.parser.sql.common.value.ValueASTNode;
public final class ParameterMarkerValue implements ValueASTNode<Integer> {
private final Integer value;
+
+ private final ParameterMarkerType type;
}
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/sql/loader/SQLCasesLoader.java
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/sql/loader/SQLCasesLoader.java
index 76e761d..e86f30e 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/sql/loader/SQLCasesLoader.java
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/sql/loader/SQLCasesLoader.java
@@ -20,6 +20,7 @@ package
org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.sql.loader;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.Case;
import
org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.sql.SQLCase;
import
org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.sql.SQLCaseType;
@@ -41,7 +42,9 @@ import java.util.regex.Pattern;
* SQL test cases loader.
*/
public final class SQLCasesLoader extends CasesLoader {
-
+
+ private static final Pattern PARAMETER_MARKER =
Pattern.compile("\\?|\\$[0-9]+");
+
public SQLCasesLoader(final String rootDirection) {
super(rootDirection);
}
@@ -71,7 +74,7 @@ public final class SQLCasesLoader extends CasesLoader {
public String getCaseValue(final String sqlCaseId, final SQLCaseType
sqlCaseType, final List<?> parameters, final String databaseType) {
switch (sqlCaseType) {
case Literal:
- return getLiteralSQL(getSQLFromMap(sqlCaseId,
super.getCases()), parameters, databaseType);
+ return getLiteralSQL(getSQLFromMap(sqlCaseId,
super.getCases()), parameters);
case Placeholder:
return getPlaceholderSQL(getSQLFromMap(sqlCaseId,
super.getCases()));
default:
@@ -104,11 +107,11 @@ public final class SQLCasesLoader extends CasesLoader {
return sql;
}
- private String getLiteralSQL(final String sql, final List<?> parameters,
final String databaseType) {
+ private String getLiteralSQL(final String sql, final List<?> parameters) {
if (null == parameters || parameters.isEmpty()) {
return sql;
}
- return "PostgreSQL".equals(databaseType) ||
"openGauss".equals(databaseType) ? replace(sql, "\\?|\\$[0-9]+",
parameters.toArray()) : replace(sql, "\\?", parameters.toArray());
+ return replace(sql, parameters.toArray());
}
private Collection<Object[]> getSQLTestParameters(final Collection<String>
databaseTypes, final SQLCase sqlCase) {
@@ -140,33 +143,32 @@ public final class SQLCasesLoader extends CasesLoader {
private static Collection<String> getAllDatabaseTypes() {
return Arrays.asList("H2", "MySQL", "PostgreSQL", "Oracle",
"SQLServer", "SQL92", "openGauss");
}
-
- /**
- * Replaces each substring of this string that matches the literal target
sequence with
- * literal replacements one by one.
- *
- * @param source the source string need to be replaced
- * @param target the sequence of char values to be replaced
- * @param replacements array of replacement
- * @return the resulting string
- * @throws IllegalArgumentException when replacements is not enough to
replace found target.
- */
- private static String replace(final String source, final CharSequence
target, final Object... replacements) {
+
+ private static String replace(final String source, final Object...
replacements) {
if (null == source || null == replacements) {
return source;
}
- Matcher matcher = Pattern.compile(target.toString()).matcher(source);
+ Matcher matcher = PARAMETER_MARKER.matcher(source);
int found = 0;
- StringBuffer sb = new StringBuffer();
+ StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
- found++;
- if (found > replacements.length) {
- throw new IllegalArgumentException(
- String.format("Missing replacement for '%s' at [%s].",
target, found));
+ String group = matcher.group();
+ if (ParameterMarkerType.QUESTION.getMarker().equals(group)) {
+ appendReplacement(++found, replacements, matcher, buffer);
+ } else {
+ int dollarMarker =
Integer.parseInt(group.replace(ParameterMarkerType.DOLLAR.getMarker(), ""));
+ appendReplacement(dollarMarker, replacements, matcher, buffer);
}
- matcher.appendReplacement(sb,
Matcher.quoteReplacement(replacements[found - 1].toString()));
}
- matcher.appendTail(sb);
- return sb.toString();
+ matcher.appendTail(buffer);
+ return buffer.toString();
+ }
+
+ private static void appendReplacement(final int markerIndex, final
Object[] replacements, final Matcher matcher, final StringBuffer buffer) {
+ if (markerIndex > replacements.length) {
+ throw new IllegalArgumentException(
+ String.format("Missing replacement for '%s' at [%s].",
PARAMETER_MARKER.pattern(), markerIndex));
+ }
+ matcher.appendReplacement(buffer,
Matcher.quoteReplacement(replacements[markerIndex - 1].toString()));
}
}
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
index 6996a8c..bee01e8 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
@@ -3884,38 +3884,55 @@
</projections>
</select>
- <select sql-case-id="select_with_parameter_postgresql" parameters="1, 12" >
- <projections start-index="7" stop-index="8">
- <column-projection name="id" start-index="7" stop-index="8"/>
+ <select sql-case-id="select_with_dollar_parameter_for_postgresql"
parameters="1, 12" >
+ <projections start-index="7" stop-index="14">
+ <column-projection name="order_id" start-index="7"
stop-index="14"/>
</projections>
<from>
- <simple-table name="t_order" start-index="15" stop-index="21" />
+ <simple-table name="t_order" start-index="21" stop-index="27" />
</from>
- <where start-index="23" stop-index="45">
+ <where start-index="29" stop-index="80" literal-stop-index="79">
<expr>
- <binary-operation-expression start-index="29" stop-index="45">
+ <binary-operation-expression start-index="35" stop-index="80"
literal-stop-index="79">
<left>
- <binary-operation-expression start-index="29"
stop-index="34">
+ <binary-operation-expression start-index="35"
stop-index="64" literal-stop-index="63">
<left>
- <column name="id" start-index="29"
stop-index="30" />
+ <binary-operation-expression start-index="35"
stop-index="46">
+ <left>
+ <column name="user_id"
start-index="35" stop-index="41" />
+ </left>
+ <operator>=</operator>
+ <right>
+ <parameter-marker-expression value="1"
start-index="45" stop-index="46" />
+ <literal-expression value="12"
start-index="45" stop-index="46" />
+ </right>
+ </binary-operation-expression>
</left>
- <operator>=</operator>
+ <operator>AND</operator>
<right>
- <parameter-marker-expression value="0"
start-index="34" stop-index="34" />
- <literal-expression value="1" start-index="34"
stop-index="34" />
+ <binary-operation-expression start-index="52"
stop-index="64" literal-stop-index="63">
+ <left>
+ <column name="order_id"
start-index="52" stop-index="59" />
+ </left>
+ <operator>=</operator>
+ <right>
+ <parameter-marker-expression value="0"
start-index="63" stop-index="64" literal-stop-index="63" />
+ <literal-expression value="1"
start-index="63" stop-index="63" />
+ </right>
+ </binary-operation-expression>
</right>
- </binary-operation-expression>
+ </binary-operation-expression>
</left>
<operator>OR</operator>
<right>
- <binary-operation-expression start-index="39"
stop-index="45">
+ <binary-operation-expression start-index="69"
stop-index="80" literal-start-index="68" literal-stop-index="79">
<left>
- <column name="id" start-index="39"
stop-index="40" />
+ <column name="user_id" start-index="69"
stop-index="75" literal-start-index="68" literal-stop-index="74" />
</left>
<operator>=</operator>
<right>
- <parameter-marker-expression value="1"
start-index="44" stop-index="45" />
- <literal-expression value="12"
start-index="44" stop-index="45" />
+ <parameter-marker-expression value="1"
start-index="79" stop-index="80" literal-start-index="78"
literal-stop-index="79" />
+ <literal-expression value="12"
start-index="78" stop-index="79" />
</right>
</binary-operation-expression>
</right>
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
index cb70553..cbf90e3 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
@@ -111,5 +111,5 @@
<sql-case id="select_with_model_with_multi_column_for_loop" value="SELECT
country, product, year, s FROM sales_view MODEL DIMENSION BY (country, product,
year) MEASURES (sales s) IGNORE NAV RULES UPSERT (s[FOR (country, product,
year) IN (SELECT DISTINCT 'new_country', product, year FROM sales_view WHERE
country = 'Poland')] = s['Poland',CV(),CV()]) ORDER BY country, year, product"
db-types="Oracle" />
<sql-case id="select_with_comments" value="-- begin comments
SELECT
* FROM # middle comments
 t_order; -- end comments" db-types="MySQL"/>
<sql-case id="select_with_model_in" value="SELECT
order_id_value,order_item_id_value FROM (select 1001 as order_id_value, 100001
as order_item_id_value from dual) MODEL RETURN UPDATED ROWS DIMENSION
BY(order_item_id_value) MEASURES(order_id_value) RULES(order_id_value[1] =
10001)" db-types="Oracle" />
- <sql-case id="select_with_parameter_postgresql" value="SELECT id FROM
t_order WHERE id = ? OR id = $1" db-types="PostgreSQL,openGauss" />
+ <sql-case id="select_with_dollar_parameter_for_postgresql" value="SELECT
order_id FROM t_order WHERE user_id = $2 AND order_id = $1 OR user_id = $2"
db-types="PostgreSQL,openGauss" />
</sql-cases>