Java tutorial
/* * 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.sharksharding.core.shard; import org.aspectj.lang.ProceedingJoinPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sharksharding.core.config.DataSourceHolder; import com.sharksharding.factory.DataSourceHolderFactory; import com.sharksharding.factory.RouteFacadeFactory; /** * sql?? * * @author gaoxianglong * * @version 1.3.5 */ public class SQLExecute { private SharkInfo sharkInfo; private DataSourceHolder dataSourceHolder; private Route route; private static Logger logger = LoggerFactory.getLogger(SQLExecute.class); public SQLExecute() { sharkInfo = SharkInfo.getShardInfo(); dataSourceHolder = DataSourceHolderFactory.getDataSourceHolder(); route = RouteFacadeFactory.getRoute(); } /** * ??,? * * @author gaoxianglong * * @param proceedingJoinPoint * ? * * @param indexType * truemaster?,falseslave? * * @exception Throwable * * @return Object */ protected Object execute(ProceedingJoinPoint proceedingJoinPoint, boolean indexType) { 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("before sql-->" + sql); /* sharding? */ if (sharkInfo.getIsShard()) { if (sharkInfo.getShardMode()) { params = route.dbRouteByOne(sql, params, indexType); } else { params = route.dbRouteByMany(sql, params, indexType); } sql = params[0].toString(); } else { /* ?master/slave??? */ final int index = ResolveIndex.getIndex(sharkInfo.getWr_index(), indexType); SetDatasource.setIndex(index, dataSourceHolder); } logger.info("after sql-->" + sql); } try { obj = proceedingJoinPoint.proceed(params); } catch (Throwable e) { e.printStackTrace(); } } return obj; } }