Commit 16eb5731 authored by haocao's avatar haocao
Browse files

For #525 3th.

parent eec4c94a
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ public abstract class AbstractSQLStatement implements SQLStatement {
    
    private int parametersIndex;
    
    private boolean containsTableName = true;
    
    @Override
    public final SQLType getType() {
        return type;
@@ -59,4 +61,14 @@ public abstract class AbstractSQLStatement implements SQLStatement {
    public int increaseParametersIndex() {
        return ++parametersIndex;
    }
    
    @Override
    public void setContainsTableName(final boolean containsTableName) {
        this.containsTableName = containsTableName;
    }
    
    @Override
    public boolean containsTableName() {
        return this.containsTableName;
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -79,4 +79,16 @@ public interface SQLStatement {
     * @return 增加后的索引偏移量
     */
    int increaseParametersIndex();
    
    /**
     * Set contain table name.
     */
    void setContainsTableName(boolean containsTableName);
    
    /**
     * Adjust contains table name is empty or not.
     *
     * @return table name is empty or not
     */
    boolean containsTableName();
}
+6 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import io.shardingjdbc.core.parsing.parser.exception.SQLParsingException;
import io.shardingjdbc.core.parsing.parser.sql.SQLParser;
import io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingjdbc.core.parsing.parser.token.IndexToken;
import io.shardingjdbc.core.parsing.parser.token.SQLToken;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.AccessLevel;
import lombok.Getter;
@@ -64,6 +65,11 @@ public abstract class AbstractDropParser implements SQLParser {
            throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE, DROP INDEX.");
        }
        tableReferencesClauseParser.parse(result, true);
        for (SQLToken each : result.getSqlTokens()) {
            if (each instanceof IndexToken && result.getSqlTokens().size() == 1) {
                result.setContainsTableName(false);
            }
        }
        return result;
    }
    
+5 −1
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@

package io.shardingjdbc.core.routing.router;

import io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingjdbc.core.routing.type.all.DatabaseAllRoutingEngine;
import io.shardingjdbc.core.routing.type.none.NoneTableRoutingEngine;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.jdbc.core.ShardingContext;
@@ -109,7 +111,9 @@ public final class ParsingSQLRouter implements SQLRouter {
    private RoutingResult route(final List<Object> parameters, final SQLStatement sqlStatement) {
        Collection<String> tableNames = sqlStatement.getTables().getTableNames();
        RoutingEngine routingEngine;
        if (tableNames.isEmpty()) {
        if (sqlStatement instanceof DDLStatement && !sqlStatement.containsTableName() && tableNames.size() == 0) {
            routingEngine = new NoneTableRoutingEngine(shardingRule, parameters, sqlStatement); 
        } else if (tableNames.isEmpty()) {
            routingEngine = new DatabaseAllRoutingEngine(shardingRule.getDataSourceMap());
        } else if (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames) || shardingRule.isAllInDefaultDataSource(tableNames)) {
            routingEngine = new SimpleRoutingEngine(shardingRule, parameters, tableNames.iterator().next(), sqlStatement);
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingjdbc.core.routing.type.none;

import com.google.common.base.Preconditions;
import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import io.shardingjdbc.core.parsing.parser.token.IndexToken;
import io.shardingjdbc.core.routing.type.RoutingEngine;
import io.shardingjdbc.core.routing.type.RoutingResult;
import io.shardingjdbc.core.routing.type.simple.SimpleRoutingEngine;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.rule.TableRule;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * None table routing engine.
 * 
 * @author caohao
 */
@RequiredArgsConstructor
public final class NoneTableRoutingEngine implements RoutingEngine {
    
    private final ShardingRule shardingRule;
    
    private final List<Object> parameters;
    
    private final SQLStatement sqlStatement;
    
    @Override
    public RoutingResult route() {
        return new SimpleRoutingEngine(shardingRule, parameters, getLogicTableName(), sqlStatement).route();
    }
    
    private String getLogicTableName() {
        Preconditions.checkState(sqlStatement.getSqlTokens().size() == 1);
        IndexToken indexToken = (IndexToken) sqlStatement.getSqlTokens().get(0);
        String indexName = indexToken.getIndexName();
        String logicTableName = "";
        for (TableRule each : shardingRule.getTableRules()) {
            if (indexName.equals(each.getLogicIndex())) {
                logicTableName = each.getLogicTable();
            }
        }
        return logicTableName;
    }
}
Loading