com.github.antelopeframework.mybatis.shard.converter.AbstractSqlConverter Maven / Gradle / Ivy
package com.github.antelopeframework.mybatis.shard.converter;
import java.util.Map;
import com.github.antelopeframework.mybatis.shard.ShardTable;
import com.github.antelopeframework.mybatis.shard.ShardTableHolder;
import com.github.antelopeframework.mybatis.shard.ShardContext.ShardOnPair;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.util.deparser.StatementDeParser;
/**
* SQL 语句转换接口的抽象实现.
*
* @author yangzhi.yzh
*
*/
public abstract class AbstractSqlConverter implements SqlConverter {
@Override
public String convert(String sqlMapperId, Statement statement, Map shardOns) {
return doDeParse(doConvert(sqlMapperId, statement, shardOns));
}
/**
* 使用 jsqlparser
, 生成 SQL 语句.
*
* @param statement
* @return
*/
protected String doDeParse(Statement statement) {
StatementDeParser deParser = new StatementDeParser(new StringBuilder());
statement.accept(deParser);
return deParser.getBuffer().toString();
}
/**
*
* @param sqlMapperId
* @param tableName
* @param shardOns
* @return
*/
protected String convertTableName(String sqlMapperId, String tableName, Map shardOns) {
ShardTable shardTable = ShardTableHolder.getInstance().getShardTable(tableName);
if (shardTable == null) {
return tableName;
}
ShardOnPair pair = shardOns.get(tableName);
if (pair == null) {
return tableName;
}
return shardTable.getStrategy().getTargetTableName(sqlMapperId, shardTable, pair.getShardOn(), pair.getShardValue());
}
/**
*
* @param paramString
* @param paramStatement
* @param paramObject
* @return
*/
protected abstract Statement doConvert(String sqlMapperId, Statement statement, Map shardOns);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy