com.gxl.shark.core.shard.SQLExecute.java Source code

Java tutorial

Introduction

Here is the source code for com.gxl.shark.core.shard.SQLExecute.java

Source

/*
 * Copyright 2015-2101 gaoxianglong
 *
 * 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.
 */
package com.gxl.shark.core.shard;

import javax.annotation.Resource;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.gxl.shark.util.ResolveRWIndex;

/**
 * SQL??
 * 
 * @author gaoxianglong
 */
@Component
public class SQLExecute {
    private static Logger logger = LoggerFactory.getLogger(SQLExecute.class);

    @Resource
    private SharkJdbcTemplate jdbcTemplate;
    @Resource
    private SetDataSource setDataSource;
    @Resource(name = "dbRouteFacade")
    private Route route;

    /**
     * ??,?
     * 
     * @author gaoxianglong
     * 
     * @param proceedingJoinPoint
     *            ?
     * 
     * @param indexType
     *            truemaster?,falseslave?
     * 
     * @exception Throwable
     * 
     * @return Object
     */
    protected Object execute(ProceedingJoinPoint proceedingJoinPoint, boolean indexType) {
        long befor = System.currentTimeMillis();
        Object obj = null;
        if (null != proceedingJoinPoint) {
            Object[] params = proceedingJoinPoint.getArgs();
            if (0 > params.length)
                return obj;
            Object param = params[0];
            /*
             * org.springframework.jdbc.core.JdbcTemplateupdate*()query*()
             * ?SQL
             */
            if (param instanceof String) {
                String sql = param.toString();
                logger.info("sharding?SQL-->" + sql);
                if (jdbcTemplate.getIsShard()) {
                    if (jdbcTemplate.getShardMode()) {
                        params = route.dbRouteByOne(sql, params, indexType);
                    } else {
                        params = route.dbRouteByMany(sql, params, indexType);
                    }
                    sql = params[0].toString();
                    logger.info("sharding?SQL-->" + sql);
                } else {
                    /* ?master/slave??? */
                    final int INDEX = ResolveRWIndex.getIndex(jdbcTemplate.getWr_index(), indexType);
                    setDataSource.setIndex(INDEX);
                }
            }
            try {
                obj = proceedingJoinPoint.proceed(params);
                logger.debug("->" + (System.currentTimeMillis() - befor) + "ms");
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return obj;
    }
}