DatabaseHintSQLRouter.java 3.1 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>
 */

T
terrymanu 已提交
18
package com.dangdang.ddframe.rdb.sharding.routing.router;
T
terrymanu 已提交
19 20 21

import com.codahale.metrics.Timer.Context;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
T
terrymanu 已提交
22
import com.dangdang.ddframe.rdb.sharding.jdbc.core.ShardingContext;
T
terrymanu 已提交
23
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
T
terrymanu 已提交
24
import com.dangdang.ddframe.rdb.sharding.parsing.SQLJudgeEngine;
25
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement;
T
terrymanu 已提交
26 27
import com.dangdang.ddframe.rdb.sharding.routing.SQLExecutionUnit;
import com.dangdang.ddframe.rdb.sharding.routing.SQLRouteResult;
T
terrymanu 已提交
28 29
import com.dangdang.ddframe.rdb.sharding.routing.type.RoutingResult;
import com.dangdang.ddframe.rdb.sharding.routing.type.TableUnit;
30
import com.dangdang.ddframe.rdb.sharding.routing.type.hint.DatabaseHintRoutingEngine;
T
terrymanu 已提交
31 32 33 34 35
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
36
 * 通过提示且仅路由至数据库的SQL路由器.
T
terrymanu 已提交
37 38 39 40
 * 
 * @author zhangiang
 */
@Slf4j
41
public final class DatabaseHintSQLRouter implements SQLRouter {
T
terrymanu 已提交
42 43 44
    
    private final ShardingRule shardingRule;
    
45
    public DatabaseHintSQLRouter(final ShardingContext shardingContext) {
T
terrymanu 已提交
46 47 48 49
        shardingRule = shardingContext.getShardingRule();
    }
    
    @Override
50
    public SQLStatement parse(final String logicSQL, final int parametersSize) {
T
terrymanu 已提交
51
        return new SQLJudgeEngine(logicSQL).judge();
T
terrymanu 已提交
52 53 54
    }
    
    @Override
55
    // TODO insert的SQL仍然需要解析自增主键
56
    public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
T
terrymanu 已提交
57
        Context context = MetricsContext.start("Route SQL");
58
        SQLRouteResult result = new SQLRouteResult(sqlStatement);
59
        RoutingResult routingResult = new DatabaseHintRoutingEngine(shardingRule.getDataSourceRule(), shardingRule.getDatabaseShardingStrategy(), sqlStatement.getType()).route();
T
terrymanu 已提交
60 61
        for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
            result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), logicSQL));
62
        }
T
terrymanu 已提交
63 64 65 66 67 68 69 70
        MetricsContext.stop(context);
        logSQLRouteResult(result, parameters);
        return result;
    }
    
    private void logSQLRouteResult(final SQLRouteResult routeResult, final List<Object> parameters) {
        log.debug("final route result is {} target", routeResult.getExecutionUnits().size());
        for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
71
            log.debug("{}:{} {}", each.getDataSource(), each.getSql(), parameters);
T
terrymanu 已提交
72 73 74
        }
    }
}