提交 f3ec4141 编写于 作者: T tristaZero

Add create sharding rule parsing definitions

上级 2ed28df3
......@@ -24,48 +24,49 @@ createSchema
;
createDatasource
: CREATE DATASOURCE datasource (COMMA datasource)*
: CREATE DATASOURCE dataSource (COMMA dataSource)*
;
createShardingrule
: CREATE SHARDINGRULE shardingrule (COMMA shardingrule)*
createShardingRule
: CREATE SHARDINGRULE tableRule (COMMA tableRule)*
;
shardingrule
: key EQ shardingruleValue
tableRule
: tableName EQ tableRuleDefinition
;
datasource
: key EQ datasourceValue
dataSource
: dataSourceName EQ dataSourceDefinition
;
key
: IDENTIFIER
;
datasourceValue
dataSourceDefinition
: hostName COLON port COLON dbName COLON user COLON password
;
shardingruleValue
: strategyType LP strategyValue RP
tableRuleDefinition
: strategyType LP strategyDefinition RP
;
strategyType
: IDENTIFIER
;
strategyValue
: tableName COMMA columName COMMA strategyProps+
strategyDefinition
: columName COMMA strategyProps
;
strategyProps
: strategyProp (COMMA strategyProp)*
;
strategyProp
: IDENTIFIER | NUMBER | INT
;
dataSourceName
: IDENTIFIER
;
schemaName
: IDENTIFIER
;
......
......@@ -20,14 +20,21 @@ package org.apache.shardingsphere.rdl.parser.sql.visitor;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementBaseVisitor;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.CreateSchemaContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.CreateDatasourceContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.DatasourceContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.DatasourceValueContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.CreateSchemaContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.CreateShardingRuleContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.DataSourceContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.DataSourceDefinitionContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.StrategyPropContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.StrategyPropsContext;
import org.apache.shardingsphere.rdl.parser.autogen.ShardingSphereStatementParser.TableRuleContext;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateSchemaStatement;
import org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement;
import org.apache.shardingsphere.rdl.parser.statement.rdl.DataSourceConnectionSegment;
import org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
import org.apache.shardingsphere.sql.parser.sql.value.collection.CollectionValue;
import java.util.Collection;
import java.util.LinkedList;
......@@ -46,21 +53,21 @@ public final class ShardingSphereVisitor extends ShardingSphereStatementBaseVisi
@Override
public ASTNode visitCreateDatasource(final CreateDatasourceContext ctx) {
Collection<DataSourceConnectionSegment> connectionInfos = new LinkedList<>();
for (DatasourceContext each : ctx.datasource()) {
for (DataSourceContext each : ctx.dataSource()) {
connectionInfos.add((DataSourceConnectionSegment) visit(each));
}
return new CreateDataSourcesStatement(connectionInfos);
}
@Override
public ASTNode visitDatasource(final DatasourceContext ctx) {
DataSourceConnectionSegment result = (DataSourceConnectionSegment) visitDatasourceValue(ctx.datasourceValue());
result.setName(ctx.key().getText());
public ASTNode visitDataSource(final DataSourceContext ctx) {
DataSourceConnectionSegment result = (DataSourceConnectionSegment) visit(ctx.dataSourceDefinition());
result.setName(ctx.dataSourceName().getText());
return result;
}
@Override
public ASTNode visitDatasourceValue(final DatasourceValueContext ctx) {
public ASTNode visitDataSourceDefinition(final DataSourceDefinitionContext ctx) {
DataSourceConnectionSegment result = new DataSourceConnectionSegment();
result.setHostName(ctx.hostName().getText());
result.setPort(ctx.port().getText());
......@@ -69,4 +76,34 @@ public final class ShardingSphereVisitor extends ShardingSphereStatementBaseVisi
result.setPassword(null == ctx.password() ? "" : ctx.password().getText());
return result;
}
@Override
public ASTNode visitCreateShardingRule(final CreateShardingRuleContext ctx) {
Collection<TableRuleSegment> tables = new LinkedList<>();
for (TableRuleContext each : ctx.tableRule()) {
tables.add((TableRuleSegment) visit(each));
}
return new CreateShardingRuleStatement(tables);
}
@Override
public ASTNode visitTableRule(final TableRuleContext ctx) {
TableRuleSegment result = new TableRuleSegment();
result.setLogicTable(ctx.tableName().getText());
result.setAlgorithmType(ctx.tableRuleDefinition().strategyType().getText());
result.setShardingColumn(ctx.tableRuleDefinition().strategyDefinition().columName().getText());
result.setDataSources(new LinkedList<>());
CollectionValue<String> props = (CollectionValue) visit(ctx.tableRuleDefinition().strategyDefinition().strategyProps());
result.setProperties(props.getValue());
return result;
}
@Override
public ASTNode visitStrategyProps(final StrategyPropsContext ctx) {
CollectionValue<String> result = new CollectionValue();
for (StrategyPropContext each : ctx.strategyProp()) {
result.getValue().add(each.getText());
}
return result;
}
}
......@@ -17,8 +17,17 @@
package org.apache.shardingsphere.rdl.parser.statement.rdl;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
* Create sharding rule statement.
*/
@RequiredArgsConstructor
@Getter
public final class CreateShardingRuleStatement extends RDLStatement {
private final Collection<TableRuleSegment> tables;
}
/*
* 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.rdl.parser.statement.rdl;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
import java.util.Collection;
/**
* Table rule segment.
*/
@Getter
@Setter
public final class TableRuleSegment implements ASTNode {
private String logicTable;
private Collection<String> dataSources;
private String shardingColumn;
private String algorithmType;
private Collection<String> properties;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册