未验证 提交 18e9025c 编写于 作者: L Liang Zhang 提交者: GitHub

Init version of SQLServer visitor (#4319)

上级 284a27a0
......@@ -219,6 +219,12 @@ public enum RuleName {
DROP_ROLE("DropRole"),
CREATE_LOGIN("CreateLogin"),
ALTER_LOGIN("AlterLogin"),
DROP_LOGIN("DropLogin"),
SET_DEFAULT_ROLE("SetDefaultRole"),
SET_ROLE("SetRole"),
......
......@@ -52,7 +52,8 @@ public final class ParseTreeVisitorFactory {
RuleName.SET_AUTOCOMMIT.getName(), RuleName.COMMIT.getName(), RuleName.ROLLBACK.getName(), RuleName.SAVE_POINT.getName()));
SQL_VISITOR_RULES.put("DCLVisitor", Lists.newArrayList(RuleName.GRANT.getName(), RuleName.REVOKE.getName(), RuleName.CREATE_USER.getName(),
RuleName.DROP_USER.getName(), RuleName.ALTER_USER.getName(), RuleName.RENAME_USER.getName(), RuleName.CREATE_ROLE.getName(), RuleName.ALTER_ROLE.getName(),
RuleName.DROP_ROLE.getName(), RuleName.SET_DEFAULT_ROLE.getName(), RuleName.SET_ROLE.getName(), RuleName.SET_PASSWORD.getName()));
RuleName.DROP_ROLE.getName(), RuleName.CREATE_LOGIN.getName(), RuleName.ALTER_LOGIN.getName(),
RuleName.DROP_LOGIN.getName(), RuleName.SET_DEFAULT_ROLE.getName(), RuleName.SET_ROLE.getName(), RuleName.SET_PASSWORD.getName()));
SQL_VISITOR_RULES.put("DALVisitor", Lists.newArrayList(RuleName.USE.getName(), RuleName.DESC.getName(), RuleName.SHOW_DATABASES.getName(),
RuleName.SHOW_TABLES.getName(), RuleName.SHOW_TABLE_STATUS.getName(), RuleName.SHOW_COLUMNS.getName(), RuleName.SHOW_INDEX.getName(),
RuleName.SHOW_CREATE_TABLE.getName(), RuleName.SHOW_OTHER.getName(), RuleName.SHOW.getName(), RuleName.SET_VARIABLE.getName(), RuleName.SET.getName(),
......
......@@ -22,6 +22,11 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.apache.shardingsphere.sql.parser.api.SQLParser;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementLexer;
import org.apache.shardingsphere.sql.parser.spi.SQLParserEntry;
import org.apache.shardingsphere.sql.parser.visitor.impl.SQLServerDALVisitor;
import org.apache.shardingsphere.sql.parser.visitor.impl.SQLServerDCLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.impl.SQLServerDDLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.impl.SQLServerDMLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.impl.SQLServerTCLVisitor;
/**
* SQL parser entry for SQLServer.
......@@ -48,6 +53,18 @@ public final class SQLServerParserEntry implements SQLParserEntry {
@Override
public Class<? extends ParseTreeVisitor> getVisitorClass(final String visitorName) {
return SQLServerVisitor.class;
if (SQLServerDMLVisitor.class.getSimpleName().contains(visitorName)) {
return SQLServerDMLVisitor.class;
}
if (SQLServerDDLVisitor.class.getSimpleName().contains(visitorName)) {
return SQLServerDDLVisitor.class;
}
if (SQLServerTCLVisitor.class.getSimpleName().contains(visitorName)) {
return SQLServerTCLVisitor.class;
}
if (SQLServerDCLVisitor.class.getSimpleName().contains(visitorName)) {
return SQLServerDCLVisitor.class;
}
return SQLServerDALVisitor.class;
}
}
......@@ -15,23 +15,14 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sql.parser;
package org.apache.shardingsphere.sql.parser.visitor.impl;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementBaseVisitor;
import org.apache.shardingsphere.sql.parser.sql.ASTNode;
import org.apache.shardingsphere.sql.parser.visitor.SQLServerVisitor;
/**
* SQL server visitor.
* SQLServer DAL visitor.
*
* @author panjuan
* @author zhangliang
*/
public final class SQLServerVisitor extends SQLServerStatementBaseVisitor<ASTNode> {
// DALStatement.g4
// DCLStatement.g4
// DDLStatement.g4
// DMLStatement.g4
// TCLStatement.g4
// StoreProcedure.g4
// BaseRule.g4
public final class SQLServerDALVisitor extends SQLServerVisitor {
}
/*
* 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.visitor.impl;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AlterLoginContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AlterRoleContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AlterUserContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ClassPrivilegesClause_Context;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ClassTypePrivilegesClause_Context;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateLoginContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateRoleContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateUserContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropLoginContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropRoleContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.DropUserContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.GrantContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.RevokeContext;
import org.apache.shardingsphere.sql.parser.sql.ASTNode;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.AlterLoginStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.AlterRoleStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.AlterUserStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.CreateLoginStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.CreateRoleStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.CreateUserStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.DropLoginStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.DropRoleStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.DropUserStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.GrantStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dcl.RevokeStatement;
import org.apache.shardingsphere.sql.parser.visitor.SQLServerVisitor;
import java.util.Collection;
import java.util.Collections;
/**
* SQLServer DCL visitor.
*
* @author zhangliang
*/
public final class SQLServerDCLVisitor extends SQLServerVisitor {
@Override
public ASTNode visitGrant(final GrantContext ctx) {
GrantStatement result = new GrantStatement();
if (null != ctx.classPrivilegesClause_()) {
for (TableSegment each : getTableFromPrivilegeClause(ctx.classPrivilegesClause_())) {
result.getAllSQLSegments().add(each);
result.getTables().add(each);
}
}
if (null != ctx.classTypePrivilegesClause_()) {
for (TableSegment each : getTableFromPrivilegeClause(ctx.classTypePrivilegesClause_())) {
result.getAllSQLSegments().add(each);
result.getTables().add(each);
}
}
return result;
}
@Override
public ASTNode visitRevoke(final RevokeContext ctx) {
RevokeStatement result = new RevokeStatement();
if (null != ctx.classPrivilegesClause_()) {
for (TableSegment each : getTableFromPrivilegeClause(ctx.classPrivilegesClause_())) {
result.getAllSQLSegments().add(each);
result.getTables().add(each);
}
}
if (null != ctx.classTypePrivilegesClause_()) {
for (TableSegment each : getTableFromPrivilegeClause(ctx.classTypePrivilegesClause_())) {
result.getAllSQLSegments().add(each);
result.getTables().add(each);
}
}
return result;
}
private Collection<TableSegment> getTableFromPrivilegeClause(final ClassPrivilegesClause_Context ctx) {
return null == ctx.onClassClause_().tableName() ? Collections.<TableSegment>emptyList() : Collections.singletonList((TableSegment) visit(ctx.onClassClause_().tableName()));
}
private Collection<TableSegment> getTableFromPrivilegeClause(final ClassTypePrivilegesClause_Context ctx) {
return null == ctx.onClassTypeClause_().tableName() ? Collections.<TableSegment>emptyList() : Collections.singletonList((TableSegment) visit(ctx.onClassTypeClause_().tableName()));
}
@Override
public ASTNode visitCreateUser(final CreateUserContext ctx) {
return new CreateUserStatement();
}
@Override
public ASTNode visitDropUser(final DropUserContext ctx) {
return new DropUserStatement();
}
@Override
public ASTNode visitAlterUser(final AlterUserContext ctx) {
return new AlterUserStatement();
}
@Override
public ASTNode visitCreateRole(final CreateRoleContext ctx) {
return new CreateRoleStatement();
}
@Override
public ASTNode visitAlterRole(final AlterRoleContext ctx) {
return new AlterRoleStatement();
}
@Override
public ASTNode visitDropRole(final DropRoleContext ctx) {
return new DropRoleStatement();
}
@Override
public ASTNode visitCreateLogin(final CreateLoginContext ctx) {
return new CreateLoginStatement();
}
@Override
public ASTNode visitAlterLogin(final AlterLoginContext ctx) {
return new AlterLoginStatement();
}
@Override
public ASTNode visitDropLogin(final DropLoginContext ctx) {
return new DropLoginStatement();
}
}
/*
* 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.visitor.impl;
import org.apache.shardingsphere.sql.parser.visitor.SQLServerVisitor;
/**
* SQLServer DDL visitor.
*
* @author zhangliang
*/
public final class SQLServerDDLVisitor extends SQLServerVisitor {
// @Override
// public ASTNode visitCreateTable(final CreateTableContext ctx) {
// CreateTableStatement result = new CreateTableStatement();
// TableSegment table = (TableSegment) visit(ctx.tableName());
// result.getTables().add(table);
// result.getAllSQLSegments().add(table);
// if (null != ctx.createDefinitionClause()) {
// CreateTableStatement createDefinition = (CreateTableStatement) visit(ctx.createDefinitionClause());
// result.getColumnDefinitions().addAll(createDefinition.getColumnDefinitions());
// for (SQLSegment each : createDefinition.getAllSQLSegments()) {
// result.getAllSQLSegments().add(each);
// if (each instanceof TableSegment) {
// result.getTables().add((TableSegment) each);
// }
// }
// }
// return result;
// }
//
// @Override
// public ASTNode visitCreateDefinitionClause(final CreateDefinitionClauseContext ctx) {
// CreateTableStatement result = new CreateTableStatement();
// for (RelationalPropertyContext each : ctx.relationalProperties().relationalProperty()) {
// ColumnDefinitionContext columnDefinition = each.columnDefinition();
// if (null != columnDefinition) {
// result.getColumnDefinitions().add((ColumnDefinitionSegment) visit(columnDefinition));
// Collection<TableSegment> tableSegments = getTableSegments(columnDefinition);
// result.getTables().addAll(tableSegments);
// result.getAllSQLSegments().addAll(tableSegments);
// }
// if (null != each.outOfLineConstraint() && null != each.outOfLineConstraint().referencesClause() && null != each.outOfLineConstraint().referencesClause().tableName()) {
// TableSegment tableSegment = (TableSegment) visit(each.outOfLineConstraint().referencesClause().tableName());
// result.getTables().add(tableSegment);
// result.getAllSQLSegments().add(tableSegment);
// }
// if (null != each.outOfLineRefConstraint() && null != each.outOfLineRefConstraint().referencesClause() && null != each.outOfLineRefConstraint().referencesClause().tableName()) {
// TableSegment tableSegment = (TableSegment) visit(each.outOfLineRefConstraint().referencesClause().tableName());
// result.getTables().add(tableSegment);
// result.getAllSQLSegments().add(tableSegment);
// }
// }
// if (result.getColumnDefinitions().isEmpty()) {
// result.getAllSQLSegments().addAll(result.getColumnDefinitions());
// }
// return result;
// }
//
// @Override
// public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
// ColumnSegment column = (ColumnSegment) visit(ctx.columnName());
// IdentifierValue dataType = (IdentifierValue) visit(ctx.dataType().dataTypeName());
// boolean isPrimaryKey = containsPrimaryKey(ctx);
// return new ColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), column.getIdentifier().getValue(), dataType.getValue(), isPrimaryKey);
// }
//
// private boolean containsPrimaryKey(final ColumnDefinitionContext ctx) {
// for (InlineConstraintContext each : ctx.inlineConstraint()) {
// if (null != each.primaryKey()) {
// return true;
// }
// }
// return false;
// }
//
// private Collection<TableSegment> getTableSegments(final ColumnDefinitionContext columnDefinition) {
// Collection<TableSegment> result = new LinkedList<>();
// for (InlineConstraintContext each : columnDefinition.inlineConstraint()) {
// if (null != each.referencesClause()) {
// result.add((TableSegment) visit(each.referencesClause().tableName()));
// }
// }
// if (null != columnDefinition.inlineRefConstraint()) {
// result.add((TableSegment) visit(columnDefinition.inlineRefConstraint().tableName()));
// }
// return result;
// }
//
// private Collection<TableSegment> getTableSegments(final VirtualColumnDefinitionContext virtualColumnDefinition) {
// Collection<TableSegment> result = new LinkedList<>();
// for (InlineConstraintContext each : virtualColumnDefinition.inlineConstraint()) {
// if (null != each.referencesClause()) {
// result.add((TableSegment) visit(each.referencesClause().tableName()));
// }
// }
// return result;
// }
//
// @Override
// public ASTNode visitAlterTable(final AlterTableContext ctx) {
// AlterTableStatement result = new AlterTableStatement();
// TableSegment table = (TableSegment) visit(ctx.tableName());
// result.getTables().add(table);
// result.getAllSQLSegments().add(table);
// if (null != ctx.alterDefinitionClause()) {
// AlterTableStatement alterDefinition = (AlterTableStatement) visit(ctx.alterDefinitionClause());
// result.getAddedColumnDefinitions().addAll(alterDefinition.getAddedColumnDefinitions());
// result.getChangedPositionColumns().addAll(alterDefinition.getChangedPositionColumns());
// result.getDroppedColumnNames().addAll(alterDefinition.getDroppedColumnNames());
// for (SQLSegment each : alterDefinition.getAllSQLSegments()) {
// result.getAllSQLSegments().add(each);
// if (each instanceof TableSegment) {
// result.getTables().add((TableSegment) each);
// }
// }
// }
// return result;
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitAlterDefinitionClause(final AlterDefinitionClauseContext ctx) {
// final AlterTableStatement result = new AlterTableStatement();
// if (null != ctx.columnClauses()) {
// for (OperateColumnClauseContext each : ctx.columnClauses().operateColumnClause()) {
// AddColumnSpecificationContext addColumnSpecification = each.addColumnSpecification();
// if (null != addColumnSpecification) {
// CollectionValue<AddColumnDefinitionSegment> addColumnDefinitions = (CollectionValue<AddColumnDefinitionSegment>) visit(addColumnSpecification);
// for (AddColumnDefinitionSegment addColumnDefinition : addColumnDefinitions.getValue()) {
// result.getAddedColumnDefinitions().add(addColumnDefinition.getColumnDefinition());
// Optional<ColumnPositionSegment> columnPositionSegment = addColumnDefinition.getColumnPosition();
// // TODO refactor SQLStatement
// // CHECKSTYLE:OFF
// if (columnPositionSegment.isPresent()) {
// result.getChangedPositionColumns().add(columnPositionSegment.get());
// }
// // CHECKSTYLE:ON
// }
// for (ColumnOrVirtualDefinitionContext columnOrVirtualDefinition : addColumnSpecification.columnOrVirtualDefinitions().columnOrVirtualDefinition()) {
// // TODO refactor SQLStatement
// // CHECKSTYLE:OFF
// if (null != columnOrVirtualDefinition.columnDefinition()) {
// result.getAllSQLSegments().addAll(getTableSegments(columnOrVirtualDefinition.columnDefinition()));
// }
// if (null != columnOrVirtualDefinition.virtualColumnDefinition()) {
// result.getAllSQLSegments().addAll(getTableSegments(columnOrVirtualDefinition.virtualColumnDefinition()));
// }
// // CHECKSTYLE:ON
// }
// }
// ModifyColumnSpecificationContext modifyColumnSpecification = each.modifyColumnSpecification();
// if (null != modifyColumnSpecification) {
// Optional<ColumnPositionSegment> columnPositionSegment = ((ModifyColumnDefinitionSegment) visit(modifyColumnSpecification)).getColumnPosition();
// // TODO refactor SQLStatement
// // CHECKSTYLE:OFF
// if (columnPositionSegment.isPresent()) {
// result.getChangedPositionColumns().add(columnPositionSegment.get());
// }
// // CHECKSTYLE:ON
// }
// DropColumnClauseContext dropColumnClause = each.dropColumnClause();
// if (null != dropColumnClause) {
// result.getDroppedColumnNames().add(((DropColumnDefinitionSegment) visit(dropColumnClause)).getColumnName());
// }
// }
// }
// if (result.getAddedColumnDefinitions().isEmpty()) {
// result.getAllSQLSegments().addAll(result.getAddedColumnDefinitions());
// }
// if (result.getChangedPositionColumns().isEmpty()) {
// result.getAllSQLSegments().addAll(result.getChangedPositionColumns());
// }
// return result;
// }
//
// @Override
// public ASTNode visitAddColumnSpecification(final AddColumnSpecificationContext ctx) {
// CollectionValue<AddColumnDefinitionSegment> result = new CollectionValue<>();
// for (ColumnOrVirtualDefinitionContext each : ctx.columnOrVirtualDefinitions().columnOrVirtualDefinition()) {
// if (null != each.columnDefinition()) {
// AddColumnDefinitionSegment addColumnDefinition = new AddColumnDefinitionSegment(
// each.columnDefinition().getStart().getStartIndex(), each.columnDefinition().getStop().getStopIndex(), (ColumnDefinitionSegment) visit(each.columnDefinition()));
// result.getValue().add(addColumnDefinition);
// }
// }
// return result;
// }
//
// @Override
// public ASTNode visitModifyColumnSpecification(final ModifyColumnSpecificationContext ctx) {
// // TODO visit column definition, need to change g4 for modifyColumn
// return new ModifyColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null);
// }
//
// @Override
// public ASTNode visitDropColumnSpecification(final DropColumnSpecificationContext ctx) {
// // TODO can drop multiple columns
// return new DropColumnDefinitionSegment(
// ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ColumnSegment) visit(ctx.columnOrColumnList().columnName(0))).getIdentifier().getValue());
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitDropTable(final DropTableContext ctx) {
// DropTableStatement result = new DropTableStatement();
// TableSegment table = (TableSegment) visit(ctx.tableName());
// result.getTables().add(table);
// result.getAllSQLSegments().add(table);
// return result;
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitTruncateTable(final TruncateTableContext ctx) {
// TruncateStatement result = new TruncateStatement();
// TableSegment table = (TableSegment) visit(ctx.tableName());
// result.getTables().add(table);
// result.getAllSQLSegments().add(table);
// return result;
// }
//
// @Override
// public ASTNode visitCreateIndex(final CreateIndexContext ctx) {
// CreateIndexStatement result = new CreateIndexStatement();
// if (null != ctx.createIndexDefinitionClause_().tableIndexClause_()) {
// TableSegment table = (TableSegment) visit(ctx.createIndexDefinitionClause_().tableIndexClause_().tableName());
// result.setTable(table);
// result.getAllSQLSegments().add(table);
// }
// return result;
// }
//
// @Override
// public ASTNode visitAlterIndex(final AlterIndexContext ctx) {
// return new AlterIndexStatement();
// }
//
// @Override
// public ASTNode visitDropIndex(final DropIndexContext ctx) {
// return new DropIndexStatement();
// }
}
/*
* 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.visitor.impl;
import org.apache.shardingsphere.sql.parser.visitor.SQLServerVisitor;
/**
* SQLServer DML visitor.
*
* @author zhangliang
*/
public final class SQLServerDMLVisitor extends SQLServerVisitor {
// @Override
// public ASTNode visitInsert(final InsertContext ctx) {
// // TODO :FIXME, since there is no segment for insertValuesClause, InsertStatement is created by sub rule.
// // TODO deal with insert select
// if (null != ctx.insertSingleTable()) {
// InsertStatement result = (InsertStatement) visit(ctx.insertSingleTable().insertValuesClause());
// TableSegment table = (TableSegment) visit(ctx.insertSingleTable().insertIntoClause().tableName());
// result.setTable(table);
// result.getAllSQLSegments().add(table);
// result.setParametersCount(getCurrentParameterIndex());
// return result;
// }
// throw new UnsupportedOperationException("Cannot support insert multi table");
// }
//
// @Override
// public ASTNode visitInsertValuesClause(final InsertValuesClauseContext ctx) {
// InsertStatement result = new InsertStatement();
// if (null != ctx.columnNames()) {
// InsertColumnsSegment insertColumnsSegment = (InsertColumnsSegment) visit(ctx.columnNames());
// result.setColumns(insertColumnsSegment);
// result.getAllSQLSegments().add(insertColumnsSegment);
// }
// Collection<InsertValuesSegment> insertValuesSegments = createInsertValuesSegments(ctx.assignmentValues());
// result.getValues().addAll(insertValuesSegments);
// result.getAllSQLSegments().addAll(insertValuesSegments);
// return result;
// }
//
// private Collection<InsertValuesSegment> createInsertValuesSegments(final Collection<AssignmentValuesContext> assignmentValuesContexts) {
// Collection<InsertValuesSegment> result = new LinkedList<>();
// for (AssignmentValuesContext each : assignmentValuesContexts) {
// result.add((InsertValuesSegment) visit(each));
// }
// return result;
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitUpdate(final UpdateContext ctx) {
// UpdateStatement result = new UpdateStatement();
// CollectionValue<TableSegment> tables = (CollectionValue<TableSegment>) visit(ctx.tableReferences());
// SetAssignmentSegment setSegment = (SetAssignmentSegment) visit(ctx.setAssignmentsClause());
// result.getTables().addAll(tables.getValue());
// result.setSetAssignment(setSegment);
// result.getAllSQLSegments().addAll(tables.getValue());
// result.getAllSQLSegments().add(setSegment);
// if (null != ctx.whereClause()) {
// WhereSegment whereSegment = (WhereSegment) visit(ctx.whereClause());
// result.setWhere(whereSegment);
// result.getAllSQLSegments().add(whereSegment);
// }
// result.setParametersCount(getCurrentParameterIndex());
// return result;
// }
//
// @Override
// public ASTNode visitSetAssignmentsClause(final SetAssignmentsClauseContext ctx) {
// Collection<AssignmentSegment> assignments = new LinkedList<>();
// for (AssignmentContext each : ctx.assignment()) {
// assignments.add((AssignmentSegment) visit(each));
// }
// return new SetAssignmentSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), assignments);
// }
//
// @Override
// public ASTNode visitAssignmentValues(final AssignmentValuesContext ctx) {
// List<ExpressionSegment> segments = new LinkedList<>();
// for (AssignmentValueContext each : ctx.assignmentValue()) {
// segments.add((ExpressionSegment) visit(each));
// }
// return new InsertValuesSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), segments);
// }
//
// @Override
// public ASTNode visitAssignment(final AssignmentContext ctx) {
// ColumnSegment column = (ColumnSegment) visitColumnName(ctx.columnName());
// ExpressionSegment value = (ExpressionSegment) visit(ctx.assignmentValue());
// return new AssignmentSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), column, value);
// }
//
// @Override
// public ASTNode visitAssignmentValue(final AssignmentValueContext ctx) {
// ExprContext expr = ctx.expr();
// if (null != expr) {
// return visit(expr);
// }
// return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitDelete(final DeleteContext ctx) {
// DeleteStatement result = new DeleteStatement();
// if (null != ctx.multipleTablesClause()) {
// CollectionValue<TableSegment> tables = (CollectionValue<TableSegment>) visit(ctx.multipleTablesClause());
// result.getTables().addAll(tables.getValue());
// result.getAllSQLSegments().addAll(tables.getValue());
// } else {
// TableSegment table = (TableSegment) visit(ctx.singleTableClause());
// result.getTables().add(table);
// result.getAllSQLSegments().add(table);
// }
// if (null != ctx.whereClause()) {
// WhereSegment where = (WhereSegment) visit(ctx.whereClause());
// result.setWhere(where);
// result.getAllSQLSegments().add(where);
// }
// result.setParametersCount(getCurrentParameterIndex());
// return result;
// }
//
// @Override
// public ASTNode visitSingleTableClause(final SingleTableClauseContext ctx) {
// TableSegment result = (TableSegment) visit(ctx.tableName());
// if (null != ctx.alias()) {
// result.setAlias(ctx.alias().getText());
// }
// return result;
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitMultipleTablesClause(final MultipleTablesClauseContext ctx) {
// CollectionValue<TableSegment> result = new CollectionValue<>();
// result.combine((CollectionValue<TableSegment>) visit(ctx.multipleTableNames()));
// result.combine((CollectionValue<TableSegment>) visit(ctx.tableReferences()));
// return result;
// }
//
// @Override
// public ASTNode visitMultipleTableNames(final MultipleTableNamesContext ctx) {
// CollectionValue<TableSegment> result = new CollectionValue<>();
// for (TableNameContext each : ctx.tableName()) {
// result.getValue().add((TableSegment) visit(each));
// }
// return result;
// }
//
// @Override
// public ASTNode visitSelect(final SelectContext ctx) {
// // TODO : Unsupported for withClause.
// SelectStatement result = (SelectStatement) visit(ctx.unionClause());
// result.setParametersCount(getCurrentParameterIndex());
// return result;
// }
//
// @Override
// public ASTNode visitUnionClause(final UnionClauseContext ctx) {
// // TODO : Unsupported for union SQL.
// return visit(ctx.selectClause(0));
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitSelectClause(final SelectClauseContext ctx) {
// SelectStatement result = new SelectStatement();
// ProjectionsSegment projections = (ProjectionsSegment) visit(ctx.projections());
// result.setProjections(projections);
// result.getAllSQLSegments().add(projections);
// if (null != ctx.duplicateSpecification()) {
// result.getProjections().setDistinctRow(isDistinct(ctx));
// }
// if (null != ctx.fromClause()) {
// CollectionValue<TableSegment> tables = (CollectionValue<TableSegment>) visit(ctx.fromClause());
// result.getTables().addAll(tables.getValue());
// result.getAllSQLSegments().addAll(tables.getValue());
// }
// if (null != ctx.whereClause()) {
// WhereSegment where = (WhereSegment) visit(ctx.whereClause());
// result.setWhere(where);
// result.getAllSQLSegments().add(where);
// }
// if (null != ctx.groupByClause()) {
// GroupBySegment groupBy = (GroupBySegment) visit(ctx.groupByClause());
// result.setGroupBy(groupBy);
// result.getAllSQLSegments().add(groupBy);
// }
// if (null != ctx.orderByClause()) {
// OrderBySegment orderBy = (OrderBySegment) visit(ctx.orderByClause());
// result.setOrderBy(orderBy);
// result.getAllSQLSegments().add(orderBy);
// }
// return result;
// }
//
// 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(), ctx.unqualifiedShorthand().getText()));
// }
// 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.
// if (null != ctx.qualifiedShorthand()) {
// QualifiedShorthandContext shorthand = ctx.qualifiedShorthand();
// ShorthandProjectionSegment result = new ShorthandProjectionSegment(shorthand.getStart().getStartIndex(), shorthand.getStop().getStopIndex(), shorthand.getText());
// IdentifierValue identifier = new IdentifierValue(shorthand.identifier().getText());
// result.setOwner(new TableSegment(shorthand.identifier().getStart().getStartIndex(), shorthand.identifier().getStop().getStopIndex(), identifier));
// return result;
// }
// String alias = null == ctx.alias() ? null : ctx.alias().getText();
// if (null != ctx.columnName()) {
// ColumnSegment column = (ColumnSegment) visit(ctx.columnName());
// ColumnProjectionSegment result = new ColumnProjectionSegment(ctx.columnName().getText(), column);
// result.setAlias(alias);
// return result;
// }
// return createProjection(ctx, alias);
// }
//
// private ASTNode createProjection(final ProjectionContext ctx, final String alias) {
// ASTNode projection = visit(ctx.expr());
// if (projection instanceof AggregationProjectionSegment) {
// ((AggregationProjectionSegment) projection).setAlias(alias);
// return projection;
// }
// if (projection instanceof ExpressionProjectionSegment) {
// ((ExpressionProjectionSegment) projection).setAlias(alias);
// return projection;
// }
// if (projection instanceof CommonExpressionSegment) {
// CommonExpressionSegment segment = (CommonExpressionSegment) projection;
// ExpressionProjectionSegment result = new ExpressionProjectionSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getText());
// result.setAlias(alias);
// return result;
// }
// // FIXME :For DISTINCT()
// if (projection instanceof ColumnSegment) {
// ExpressionProjectionSegment result = new ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText());
// result.setAlias(alias);
// return result;
// }
// LiteralExpressionSegment column = (LiteralExpressionSegment) projection;
// ExpressionProjectionSegment result = Strings.isNullOrEmpty(alias) ? new ExpressionProjectionSegment(column.getStartIndex(), column.getStopIndex(), String.valueOf(column.getLiterals()))
// : new ExpressionProjectionSegment(column.getStartIndex(), ctx.alias().stop.getStopIndex(), String.valueOf(column.getLiterals()));
// result.setAlias(alias);
// return result;
// }
//
// @Override
// public ASTNode visitFromClause(final FromClauseContext ctx) {
// return visit(ctx.tableReferences());
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public ASTNode visitTableReferences(final TableReferencesContext ctx) {
// CollectionValue<TableSegment> result = new CollectionValue<>();
// for (TableReferenceContext each : ctx.tableReference()) {
// result.combine((CollectionValue<TableSegment>) visit(each));
// }
// return result;
// }
//
// @Override
// public ASTNode visitTableReference(final TableReferenceContext ctx) {
// CollectionValue<TableSegment> result = new CollectionValue<>();
// if (null != ctx.tableFactor()) {
// result.getValue().add((TableSegment) visit(ctx.tableFactor()));
// }
// if (null != ctx.joinedTable()) {
// for (JoinedTableContext each : ctx.joinedTable()) {
// result.getValue().add((TableSegment) visit(each));
// }
// }
// return result;
// }
//
// @Override
// public ASTNode visitTableFactor(final TableFactorContext ctx) {
// if (null != ctx.tableReferences()) {
// return visit(ctx.tableReferences());
// }
// TableSegment table = (TableSegment) visit(ctx.tableName());
// if (null != ctx.alias()) {
// table.setAlias(ctx.alias().getText());
// }
// return table;
// }
//
// @Override
// public ASTNode visitJoinedTable(final JoinedTableContext ctx) {
// return visit(ctx.tableFactor());
// }
//
// @Override
// public ASTNode visitWhereClause(final WhereClauseContext ctx) {
// WhereSegment result = new WhereSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
// ASTNode segment = visit(ctx.expr());
// if (segment instanceof OrPredicateSegment) {
// result.getAndPredicates().addAll(((OrPredicateSegment) segment).getAndPredicates());
// } else if (segment instanceof PredicateSegment) {
// AndPredicate andPredicate = new AndPredicate();
// andPredicate.getPredicates().add((PredicateSegment) segment);
// result.getAndPredicates().add(andPredicate);
// }
// return result;
// }
//
// @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);
// }
}
/*
* 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.visitor.impl;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CommitContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.RollbackContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SavepointContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SetImplicitTransactionsContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.SetTransactionContext;
import org.apache.shardingsphere.sql.parser.sql.ASTNode;
import org.apache.shardingsphere.sql.parser.sql.statement.tcl.CommitStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.tcl.RollbackStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.tcl.SavepointStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.tcl.SetTransactionStatement;
import org.apache.shardingsphere.sql.parser.visitor.SQLServerVisitor;
/**
* SQLServer TCL visitor.
*
* @author zhangliang
*/
public final class SQLServerTCLVisitor extends SQLServerVisitor {
@Override
public ASTNode visitSetTransaction(final SetTransactionContext ctx) {
return new SetTransactionStatement();
}
@Override
public ASTNode visitSetImplicitTransactions(final SetImplicitTransactionsContext ctx) {
return new SetTransactionStatement();
}
@Override
public ASTNode visitCommit(final CommitContext ctx) {
return new CommitStatement();
}
@Override
public ASTNode visitRollback(final RollbackContext ctx) {
return new RollbackStatement();
}
@Override
public ASTNode visitSavepoint(final SavepointContext ctx) {
return new SavepointStatement();
}
}
......@@ -98,8 +98,8 @@ public final class VisitorParameterizedParsingTest {
if (isPassedSqlCase(sqlCaseId)) {
continue;
}
// if (!"MySQL".contains(databaseType) && !"PostgreSQL".contains(databaseType) && !"Oracle".contains(databaseType) && !"SQLServer".contains(databaseType)) {
if (!"MySQL".contains(databaseType) && !"PostgreSQL".contains(databaseType) && !"Oracle".contains(databaseType)) {
// if (!"MySQL".contains(databaseType) && !"PostgreSQL".contains(databaseType)) {
continue;
}
try {
......@@ -147,6 +147,10 @@ public final class VisitorParameterizedParsingTest {
sqlCases.add("select_pagination_with_row_number_for_greater_than_and_equal");
sqlCases.add("alter_table_add_foreign_key");
sqlCases.add("alter_table_add_primary_foreign_key");
// TODO no rule in SQLServer's g4
sqlCases.add("beginTransaction");
// TODO no rule in SQLServer's g4
sqlCases.add("beginWithName");
return sqlCases.contains(sqlCaseId);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册