This is an automated email from the ASF dual-hosted git repository.
lujingshang 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 4f19459 add SQLServer grammar Create View (#12247)
4f19459 is described below
commit 4f194595500bea450c1c718ef6043771798dd79d
Author: Icesource <[email protected]>
AuthorDate: Wed Sep 8 09:41:59 2021 +0800
add SQLServer grammar Create View (#12247)
* add SQLServer grammar create view
* move sqlserver visitSelect from DMLVisitor to SQLVisitor
---
.../src/main/antlr4/imports/sqlserver/BaseRule.g4 | 6 +-
.../main/antlr4/imports/sqlserver/DDLStatement.g4 | 22 +++
.../antlr4/imports/sqlserver/SQLServerKeyword.g4 | 4 +
.../sql/parser/autogen/SQLServerStatement.g4 | 1 +
.../impl/SQLServerDDLStatementSQLVisitor.java | 11 ++
.../impl/SQLServerDMLStatementSQLVisitor.java | 159 +-------------------
.../impl/SQLServerStatementSQLVisitor.java | 164 +++++++++++++++++++++
.../ddl/SQLServerCreateViewStatement.java | 45 ++++++
.../resources/sql/supported/ddl/create-view.xml | 2 +-
9 files changed, 254 insertions(+), 160 deletions(-)
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4
index 35ec87e..3bfbe02 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4
@@ -99,7 +99,7 @@ unreservedWord
| OUTPUT | INSERTED | DELETED | KB | MB | GB | TB | FILENAME | MAXSIZE |
FILEGROWTH | UNLIMITED | MEMORY_OPTIMIZED_DATA | FILEGROUP |
NON_TRANSACTED_ACCESS
| DB_CHAINING | TRUSTWORTHY | GROUP | ROWS | DATE | DATEPART | CAST | DAY
| FORWARD_ONLY | KEYSET | FAST_FORWARD | READ_ONLY | SCROLL_LOCKS |
OPTIMISTIC | TYPE_WARNING | SCHEMABINDING | CALLER
- | OWNER | SNAPSHOT | REPEATABLE | SERIALIZABLE | NATIVE_COMPILATION
+ | OWNER | SNAPSHOT | REPEATABLE | SERIALIZABLE | NATIVE_COMPILATION |
VIEW_METADATA
;
databaseName
@@ -118,6 +118,10 @@ procedureName
: (owner DOT_)? name (SEMI_ numberLiterals)?
;
+viewName
+ : (owner DOT_)? name
+ ;
+
tableName
: (owner DOT_)? name
;
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/DDLStatement.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/DDLStatement.g4
index 893fb91..05ae8f5 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/DDLStatement.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/DDLStatement.g4
@@ -39,6 +39,10 @@ createProcedure
: CREATE (OR ALTER)? (PROC | PROCEDURE) procedureName procParameters
createProcClause
;
+createView
+ : CREATE (OR ALTER)? VIEW viewName createViewClause
+ ;
+
alterTable
: ALTER TABLE tableName alterDefinitionClause (COMMA_
alterDefinitionClause)*
;
@@ -656,3 +660,21 @@ procSetOption
| DATEFORMAT = stringLiterals
| DELAYED_DURABILITY = ( OFF | ON )
;
+
+createViewClause
+ : (WITH viewAttribute (COMMA_ viewAttribute)*)? AS withCommonTableExpr?
select (WITH CHECK OPTION)?
+ ;
+
+viewAttribute
+ : ENCRYPTION
+ | SCHEMABINDING
+ | VIEW_METADATA
+ ;
+
+withCommonTableExpr
+ : WITH commonTableExpr (COMMA_ commonTableExpr)*
+ ;
+
+commonTableExpr
+ : name (LP_ columnName (COMMA_ columnName)* RP_)? AS LP_ select RP_
+ ;
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/SQLServerKeyword.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/SQLServerKeyword.g4
index b270611..45c67c6 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/SQLServerKeyword.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/imports/sqlserver/SQLServerKeyword.g4
@@ -1246,3 +1246,7 @@ SERIALIZABLE
NATIVE_COMPILATION
: N A T I V E UL_ C O M P I L A T I O N
;
+
+VIEW_METADATA
+ : V I E W UL_ M E T A D A T A
+ ;
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/SQLServerStatement.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/SQLServerStatement.g4
index 9d3e432..c626063 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/SQLServerStatement.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/SQLServerStatement.g4
@@ -30,6 +30,7 @@ execute
| createTable
| createDatabase
| createProcedure
+ | createView
| alterTable
| dropTable
| truncateTable
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/SQLServerDDLStatementSQLVisitor.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/SQLServerDDLStatementSQLVisitor.java
index 7e40405..4be40d0 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/SQLServerDDLStatementSQLVisitor.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/SQLServerDDLStatementSQLVisitor.java
@@ -39,6 +39,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Cre
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateTableDefinitionContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateProcedureContext;
+import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateViewContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropColumnSpecificationContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropConstraintNameContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropIndexContext;
@@ -69,9 +70,11 @@ import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateIndexStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateProcedureStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateTableStatement;
+import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateViewStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerDropIndexStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerDropTableStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerTruncateStatement;
+import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement;
import java.util.Collection;
import java.util.Collections;
@@ -333,4 +336,12 @@ public final class SQLServerDDLStatementSQLVisitor extends
SQLServerStatementSQL
public ASTNode visitCreateProcedure(final CreateProcedureContext ctx) {
return new SQLServerCreateProcedureStatement();
}
+
+ @Override
+ public ASTNode visitCreateView(final CreateViewContext ctx) {
+ SQLServerCreateViewStatement result = new
SQLServerCreateViewStatement();
+ result.setView((SimpleTableSegment) visit(ctx.viewName()));
+ result.setSelect((SQLServerSelectStatement)
visit(ctx.createViewClause().select()));
+ return result;
+ }
}
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/SQLServerDMLStatementSQLVisitor.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/SQLServerDMLStatementSQLVisitor.java
index 2d3d87d..0da1074 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/SQLServerDMLStatementSQLVisitor.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/SQLServerDMLStatementSQLVisitor.java
@@ -22,7 +22,6 @@ import org.antlr.v4.runtime.misc.Interval;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import
org.apache.shardingsphere.sql.parser.api.visitor.operation.SQLStatementVisitor;
import org.apache.shardingsphere.sql.parser.api.visitor.type.DMLSQLVisitor;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AliasContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentValueContext;
@@ -34,8 +33,6 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Del
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DuplicateSpecificationContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ExprContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.FromClauseContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.GroupByClauseContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.HavingClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertDefaultValueContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertSelectClauseContext;
@@ -44,27 +41,20 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Joi
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JoinedTableContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.MultipleTableNamesContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.MultipleTablesClauseContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OrderByClauseContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OrderByItemContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OutputClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OutputTableNameContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OutputWithColumnContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.OutputWithColumnsContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ProjectionContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ProjectionsContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.QualifiedShorthandContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SelectClauseContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SelectContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SetAssignmentsClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SingleTableClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SubqueryContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableFactorContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableNameContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableReferenceContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableReferencesContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TopContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.UpdateContext;
-import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WhereClauseContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WithClauseContext;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
@@ -77,28 +67,17 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonTableExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.PaginationValueSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.RowNumberValueSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.top.TopProjectionSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OutputSegment;
@@ -359,112 +338,10 @@ public final class SQLServerDMLStatementSQLVisitor
extends SQLServerStatementSQL
}
@Override
- public ASTNode visitSelect(final SelectContext ctx) {
- // TODO :Unsupported for withClause.
- SQLServerSelectStatement result = (SQLServerSelectStatement)
visit(ctx.aggregationClause());
- result.setParameterCount(getCurrentParameterIndex());
- return result;
- }
-
- @Override
- public ASTNode visitAggregationClause(final AggregationClauseContext ctx) {
- // TODO :Unsupported for union | except | intersect SQL.
- return visit(ctx.selectClause(0));
- }
-
- @Override
- public ASTNode visitSelectClause(final SelectClauseContext ctx) {
- SQLServerSelectStatement result = new SQLServerSelectStatement();
- result.setProjections((ProjectionsSegment) visit(ctx.projections()));
- if (null != ctx.duplicateSpecification()) {
- result.getProjections().setDistinctRow(isDistinct(ctx));
- }
- if (null != ctx.fromClause()) {
- TableSegment tableSource = (TableSegment)
visit(ctx.fromClause().tableReferences());
- result.setFrom(tableSource);
- }
- if (null != ctx.whereClause()) {
- result.setWhere((WhereSegment) visit(ctx.whereClause()));
- }
- if (null != ctx.groupByClause()) {
- result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
- }
- if (null != ctx.havingClause()) {
- result.setHaving((HavingSegment) visit(ctx.havingClause()));
- }
- if (null != ctx.orderByClause()) {
- result = visitOrderBy(result, ctx.orderByClause());
- }
- return result;
- }
-
- @Override
- public ASTNode visitHavingClause(final HavingClauseContext ctx) {
- ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
- return new HavingSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), expr);
- }
-
- private SQLServerSelectStatement visitOrderBy(final
SQLServerSelectStatement selectStatement, final OrderByClauseContext ctx) {
- Collection<OrderByItemSegment> items = new LinkedList<>();
- int orderByStartIndex = ctx.start.getStartIndex();
- int orderByStopIndex = ctx.start.getStartIndex();
- for (OrderByItemContext each : ctx.orderByItem()) {
- items.add((OrderByItemSegment) visit(each));
- orderByStopIndex = each.stop.getStopIndex();
- }
- OrderBySegment orderBySegment = new OrderBySegment(orderByStartIndex,
orderByStopIndex, items);
- selectStatement.setOrderBy(orderBySegment);
- PaginationValueSegment offset = null;
- PaginationValueSegment rowcount = null;
- LimitSegment limitSegment = null;
- if (null != ctx.OFFSET()) {
- ASTNode astNode = visit(ctx.expr(0));
- if (astNode instanceof LiteralExpressionSegment &&
((LiteralExpressionSegment) astNode).getLiterals() instanceof Number) {
- offset = new
NumberLiteralLimitValueSegment(ctx.expr(0).start.getStartIndex(),
ctx.expr(0).stop.getStopIndex(),
- ((Number) ((LiteralExpressionSegment)
astNode).getLiterals()).longValue());
- } else if (astNode instanceof ParameterMarkerExpressionSegment) {
- offset = new
ParameterMarkerLimitValueSegment(ctx.expr(0).start.getStartIndex(),
ctx.expr(0).stop.getStopIndex(), getCurrentParameterIndex());
- }
- }
- if (null != ctx.FETCH()) {
- ASTNode astNode = visit(ctx.expr(1));
- if (astNode instanceof LiteralExpressionSegment &&
((LiteralExpressionSegment) astNode).getLiterals() instanceof Number) {
- rowcount = new
NumberLiteralLimitValueSegment(ctx.expr(1).start.getStartIndex(),
ctx.expr(1).stop.getStopIndex(),
- ((Number) ((LiteralExpressionSegment)
astNode).getLiterals()).longValue());
- } else if (astNode instanceof ParameterMarkerExpressionSegment) {
- rowcount = new
ParameterMarkerLimitValueSegment(ctx.expr(1).start.getStartIndex(),
ctx.expr(1).stop.getStopIndex(), getCurrentParameterIndex());
- }
- }
- if (null != offset) {
- limitSegment = new
LimitSegment(ctx.OFFSET().getSymbol().getStartIndex(), ctx.stop.getStopIndex(),
offset, rowcount);
- }
- selectStatement.setLimit(limitSegment);
- return selectStatement;
- }
-
- private boolean isDistinct(final SelectClauseContext ctx) {
- return ((BooleanLiteralValue)
visit(ctx.duplicateSpecification())).getValue();
- }
-
- @Override
public ASTNode visitDuplicateSpecification(final
DuplicateSpecificationContext ctx) {
return new BooleanLiteralValue(null != ctx.DISTINCT());
}
-
- @Override
- public ASTNode visitProjections(final ProjectionsContext ctx) {
- Collection<ProjectionSegment> projections = new LinkedList<>();
- if (null != ctx.unqualifiedShorthand()) {
- projections.add(new
ShorthandProjectionSegment(ctx.unqualifiedShorthand().getStart().getStartIndex(),
ctx.unqualifiedShorthand().getStop().getStopIndex()));
- }
- for (ProjectionContext each : ctx.projection()) {
- projections.add((ProjectionSegment) visit(each));
- }
- ProjectionsSegment result = new
ProjectionsSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex());
- result.getProjections().addAll(projections);
- return result;
- }
-
+
@Override
public ASTNode visitProjection(final ProjectionContext ctx) {
// FIXME :The stop index of project is the stop index of projection,
instead of alias.
@@ -558,26 +435,6 @@ public final class SQLServerDMLStatementSQLVisitor extends
SQLServerStatementSQL
}
@Override
- public ASTNode visitTableReferences(final TableReferencesContext ctx) {
- TableSegment result = (TableSegment) visit(ctx.tableReference(0));
- if (ctx.tableReference().size() > 1) {
- for (int i = 1; i < ctx.tableReference().size(); i++) {
- result =
generateJoinTableSourceFromTableReference(ctx.tableReference(i), result);
- }
- }
- return result;
- }
-
- private JoinTableSegment generateJoinTableSourceFromTableReference(final
TableReferenceContext ctx, final TableSegment tableSegment) {
- JoinTableSegment result = new JoinTableSegment();
- result.setStartIndex(tableSegment.getStartIndex());
- result.setStopIndex(ctx.stop.getStopIndex());
- result.setLeft(tableSegment);
- result.setRight((TableSegment) visit(ctx));
- return result;
- }
-
- @Override
public ASTNode visitTableReference(final TableReferenceContext ctx) {
TableSegment result;
TableSegment left;
@@ -641,20 +498,6 @@ public final class SQLServerDMLStatementSQLVisitor extends
SQLServerStatementSQL
}
@Override
- public ASTNode visitWhereClause(final WhereClauseContext ctx) {
- return new WhereSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), (ExpressionSegment) visit(ctx.expr()));
- }
-
- @Override
- public ASTNode visitGroupByClause(final GroupByClauseContext ctx) {
- Collection<OrderByItemSegment> items = new LinkedList<>();
- for (OrderByItemContext each : ctx.orderByItem()) {
- items.add((OrderByItemSegment) visit(each));
- }
- return new GroupBySegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), items);
- }
-
- @Override
public ASTNode visitSubquery(final SubqueryContext ctx) {
return visit(ctx.aggregationClause());
}
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 d29ebb9..49a66ce 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
@@ -25,6 +25,7 @@ import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementBaseVisitor;
+import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.BitExprContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.BitValueLiteralsContext;
@@ -60,6 +61,7 @@ import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Str
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableNameContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.TableNamesContext;
import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.UnreservedWordContext;
+import
org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.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.segment.ddl.constraint.ConstraintSegment;
@@ -79,14 +81,28 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.PaginationValueSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeLengthSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import
org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
@@ -100,6 +116,7 @@ import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.
import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
@@ -542,4 +559,151 @@ public abstract class SQLServerStatementSQLVisitor
extends SQLServerStatementBas
}
return result;
}
+
+ @Override
+ public final ASTNode visitViewName(final ViewNameContext ctx) {
+ SimpleTableSegment result = new SimpleTableSegment(new
TableNameSegment(ctx.name().getStart().getStartIndex(),
+ ctx.name().getStop().getStopIndex(), (IdentifierValue)
visit(ctx.name())));
+ OwnerContext owner = ctx.owner();
+ if (null != owner) {
+ result.setOwner(new OwnerSegment(owner.getStart().getStartIndex(),
owner.getStop().getStopIndex(), (IdentifierValue) visit(owner.identifier())));
+ }
+ return result;
+ }
+
+ @Override
+ public ASTNode visitSelect(final SQLServerStatementParser.SelectContext
ctx) {
+ // TODO :Unsupported for withClause.
+ SQLServerSelectStatement result = (SQLServerSelectStatement)
visit(ctx.aggregationClause());
+ result.setParameterCount(getCurrentParameterIndex());
+ return result;
+ }
+
+ @Override
+ public ASTNode visitAggregationClause(final
SQLServerStatementParser.AggregationClauseContext ctx) {
+ // TODO :Unsupported for union | except | intersect SQL.
+ return visit(ctx.selectClause(0));
+ }
+
+ @Override
+ public ASTNode visitSelectClause(final
SQLServerStatementParser.SelectClauseContext ctx) {
+ SQLServerSelectStatement result = new SQLServerSelectStatement();
+ result.setProjections((ProjectionsSegment) visit(ctx.projections()));
+ if (null != ctx.duplicateSpecification()) {
+ result.getProjections().setDistinctRow(isDistinct(ctx));
+ }
+ if (null != ctx.fromClause()) {
+ TableSegment tableSource = (TableSegment)
visit(ctx.fromClause().tableReferences());
+ result.setFrom(tableSource);
+ }
+ if (null != ctx.whereClause()) {
+ result.setWhere((WhereSegment) visit(ctx.whereClause()));
+ }
+ if (null != ctx.groupByClause()) {
+ result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
+ }
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
+ if (null != ctx.orderByClause()) {
+ result = visitOrderBy(result, ctx.orderByClause());
+ }
+ return result;
+ }
+
+ @Override
+ public ASTNode visitHavingClause(final
SQLServerStatementParser.HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
+ return new HavingSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), expr);
+ }
+
+ private SQLServerSelectStatement visitOrderBy(final
SQLServerSelectStatement selectStatement, final
SQLServerStatementParser.OrderByClauseContext ctx) {
+ Collection<OrderByItemSegment> items = new LinkedList<>();
+ int orderByStartIndex = ctx.start.getStartIndex();
+ int orderByStopIndex = ctx.start.getStartIndex();
+ for (OrderByItemContext each : ctx.orderByItem()) {
+ items.add((OrderByItemSegment) visit(each));
+ orderByStopIndex = each.stop.getStopIndex();
+ }
+ OrderBySegment orderBySegment = new OrderBySegment(orderByStartIndex,
orderByStopIndex, items);
+ selectStatement.setOrderBy(orderBySegment);
+ PaginationValueSegment offset = null;
+ PaginationValueSegment rowcount = null;
+ LimitSegment limitSegment = null;
+ if (null != ctx.OFFSET()) {
+ ASTNode astNode = visit(ctx.expr(0));
+ if (astNode instanceof LiteralExpressionSegment &&
((LiteralExpressionSegment) astNode).getLiterals() instanceof Number) {
+ offset = new
NumberLiteralLimitValueSegment(ctx.expr(0).start.getStartIndex(),
ctx.expr(0).stop.getStopIndex(),
+ ((Number) ((LiteralExpressionSegment)
astNode).getLiterals()).longValue());
+ } else if (astNode instanceof ParameterMarkerExpressionSegment) {
+ offset = new
ParameterMarkerLimitValueSegment(ctx.expr(0).start.getStartIndex(),
ctx.expr(0).stop.getStopIndex(), getCurrentParameterIndex());
+ }
+ }
+ if (null != ctx.FETCH()) {
+ ASTNode astNode = visit(ctx.expr(1));
+ if (astNode instanceof LiteralExpressionSegment &&
((LiteralExpressionSegment) astNode).getLiterals() instanceof Number) {
+ rowcount = new
NumberLiteralLimitValueSegment(ctx.expr(1).start.getStartIndex(),
ctx.expr(1).stop.getStopIndex(),
+ ((Number) ((LiteralExpressionSegment)
astNode).getLiterals()).longValue());
+ } else if (astNode instanceof ParameterMarkerExpressionSegment) {
+ rowcount = new
ParameterMarkerLimitValueSegment(ctx.expr(1).start.getStartIndex(),
ctx.expr(1).stop.getStopIndex(), getCurrentParameterIndex());
+ }
+ }
+ if (null != offset) {
+ limitSegment = new
LimitSegment(ctx.OFFSET().getSymbol().getStartIndex(), ctx.stop.getStopIndex(),
offset, rowcount);
+ }
+ selectStatement.setLimit(limitSegment);
+ return selectStatement;
+ }
+
+ private boolean isDistinct(final
SQLServerStatementParser.SelectClauseContext ctx) {
+ return ((BooleanLiteralValue)
visit(ctx.duplicateSpecification())).getValue();
+ }
+
+ @Override
+ public ASTNode visitProjections(final
SQLServerStatementParser.ProjectionsContext ctx) {
+ Collection<ProjectionSegment> projections = new LinkedList<>();
+ if (null != ctx.unqualifiedShorthand()) {
+ projections.add(new
ShorthandProjectionSegment(ctx.unqualifiedShorthand().getStart().getStartIndex(),
ctx.unqualifiedShorthand().getStop().getStopIndex()));
+ }
+ for (SQLServerStatementParser.ProjectionContext each :
ctx.projection()) {
+ projections.add((ProjectionSegment) visit(each));
+ }
+ ProjectionsSegment result = new
ProjectionsSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex());
+ result.getProjections().addAll(projections);
+ return result;
+ }
+
+ @Override
+ public ASTNode visitTableReferences(final
SQLServerStatementParser.TableReferencesContext ctx) {
+ TableSegment result = (TableSegment) visit(ctx.tableReference(0));
+ if (ctx.tableReference().size() > 1) {
+ for (int i = 1; i < ctx.tableReference().size(); i++) {
+ result =
generateJoinTableSourceFromTableReference(ctx.tableReference(i), result);
+ }
+ }
+ return result;
+ }
+
+ private JoinTableSegment generateJoinTableSourceFromTableReference(final
SQLServerStatementParser.TableReferenceContext ctx, final TableSegment
tableSegment) {
+ JoinTableSegment result = new JoinTableSegment();
+ result.setStartIndex(tableSegment.getStartIndex());
+ result.setStopIndex(ctx.stop.getStopIndex());
+ result.setLeft(tableSegment);
+ result.setRight((TableSegment) visit(ctx));
+ return result;
+ }
+
+ @Override
+ public ASTNode visitWhereClause(final
SQLServerStatementParser.WhereClauseContext ctx) {
+ return new WhereSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), (ExpressionSegment) visit(ctx.expr()));
+ }
+
+ @Override
+ public ASTNode visitGroupByClause(final
SQLServerStatementParser.GroupByClauseContext ctx) {
+ Collection<OrderByItemSegment> items = new LinkedList<>();
+ for (OrderByItemContext each : ctx.orderByItem()) {
+ items.add((OrderByItemSegment) visit(each));
+ }
+ return new GroupBySegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), items);
+ }
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/ddl/SQLServerCreateViewStatement.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/ddl/SQLServerCreateViewStatement.java
new file mode 100644
index 0000000..4307cb7
--- /dev/null
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/ddl/SQLServerCreateViewStatement.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl;
+
+import lombok.Setter;
+import lombok.ToString;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateViewStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
+
+import java.util.Optional;
+
+/**
+ * SQLServer create table statement.
+ */
+@Setter
+@ToString
+public final class SQLServerCreateViewStatement extends CreateViewStatement
implements SQLServerStatement {
+
+ private SelectStatement select;
+
+ /**
+ * Get select statement.
+ *
+ * @return select statement
+ */
+ public Optional<SelectStatement> getSelect() {
+ return Optional.ofNullable(select);
+ }
+}
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-view.xml
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-view.xml
index fca1d58..f3a3cee 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-view.xml
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-view.xml
@@ -20,7 +20,7 @@
<sql-case id="create_view" value="CREATE VIEW comedies AS
SELECT *
FROM films
- WHERE kind = 'Comedy'" db-types="PostgreSQL" />
+ WHERE kind = 'Comedy'" db-types="PostgreSQL,SQLServer" />
<sql-case id="create_view_with_check_option" value="CREATE VIEW
universal_comedies AS
SELECT *
FROM comedies