use of org.sagacity.sqltoy.config.model.ShardingStrategyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseShardingTables.
/**
* @todo 解析table的sharding
* @param sqlToyConfig
* @param shardingTables
*/
private static void parseShardingTables(SqlToyConfig sqlToyConfig, NodeList shardingTables) {
if (shardingTables == null || shardingTables.getLength() == 0) {
return;
}
List<ShardingStrategyConfig> tablesShardings = new ArrayList();
String[] paramName;
String[] paramsAlias;
int size;
Element elt;
// 全部参数
List<String> params = new ArrayList<String>();
for (int i = 0; i < shardingTables.getLength(); i++) {
elt = (Element) shardingTables.item(i);
if (elt.hasAttribute("tables") && elt.hasAttribute("strategy")) {
ShardingStrategyConfig shardingModel = new ShardingStrategyConfig(1);
shardingModel.setTables(trimParams(elt.getAttribute("tables").split("\\,")));
String[] fields;
if (elt.hasAttribute("params")) {
fields = elt.getAttribute("params").replace(";", ",").toLowerCase().split("\\,");
// params="a:a1,b:b1";params为{a:a1, b:b1}
size = fields.length;
paramsAlias = new String[size];
for (int j = 0; j < size; j++) {
paramName = fields[j].split("\\:");
// 重置params数组值
fields[j] = paramName[0].trim();
if (!params.contains(fields[j])) {
params.add(fields[j]);
}
paramsAlias[j] = paramName[paramName.length - 1].trim();
}
shardingModel.setFields(fields);
shardingModel.setAliasNames(paramsAlias);
}
if (elt.hasAttribute("strategy-value")) {
shardingModel.setDecisionType(elt.getAttribute("strategy-value"));
} else if (elt.hasAttribute("strategy-type")) {
shardingModel.setDecisionType(elt.getAttribute("strategy-type"));
} else if (elt.hasAttribute("decision-type")) {
shardingModel.setDecisionType(elt.getAttribute("decision-type"));
}
shardingModel.setStrategy(elt.getAttribute("strategy"));
tablesShardings.add(shardingModel);
}
}
if (!params.isEmpty()) {
String[] paramAry = new String[params.size()];
params.toArray(paramAry);
sqlToyConfig.setTableShardingParams(paramAry);
}
sqlToyConfig.setTableShardings(tablesShardings);
}
use of org.sagacity.sqltoy.config.model.ShardingStrategyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseShardingDataSource.
/**
* @todo 解析dataSource的sharding
* @param sqlToyConfig
* @param shardingDBNode
*/
private static void parseShardingDataSource(SqlToyConfig sqlToyConfig, NodeList shardingDBNode) {
if (shardingDBNode == null || shardingDBNode.getLength() == 0) {
return;
}
Element shardingDataSource = (Element) shardingDBNode.item(0);
ShardingStrategyConfig shardingConfig = new ShardingStrategyConfig(0);
// 策略辨别值
if (shardingDataSource.hasAttribute("strategy-value")) {
shardingConfig.setDecisionType(shardingDataSource.getAttribute("strategy-value"));
} else if (shardingDataSource.hasAttribute("strategy-type")) {
shardingConfig.setDecisionType(shardingDataSource.getAttribute("strategy-type"));
} else if (shardingDataSource.hasAttribute("decision-type")) {
shardingConfig.setDecisionType(shardingDataSource.getAttribute("decision-type"));
}
// 全部参数
List<String> params = new ArrayList<String>();
if (shardingDataSource.hasAttribute("params")) {
String[] fields = shardingDataSource.getAttribute("params").replace(";", ",").toLowerCase().split("\\,");
int size = fields.length;
String[] paramsAlias = new String[size];
String[] paramName;
for (int i = 0; i < size; i++) {
paramName = fields[i].split("\\:");
fields[i] = paramName[0].trim();
if (!params.contains(fields[i])) {
params.add(fields[i]);
}
paramsAlias[i] = paramName[paramName.length - 1].trim();
}
shardingConfig.setFields(fields);
shardingConfig.setAliasNames(paramsAlias);
}
if (!params.isEmpty()) {
String[] paramAry = new String[params.size()];
params.toArray(paramAry);
sqlToyConfig.setDbShardingParams(paramAry);
}
shardingConfig.setStrategy(shardingDataSource.getAttribute("strategy"));
sqlToyConfig.setDataSourceSharding(shardingConfig);
}
use of org.sagacity.sqltoy.config.model.ShardingStrategyConfig in project sagacity-sqltoy by chenrenfei.
the class QueryExecutorBuilder method getDbShardingParams.
/**
* @TODO 获取分库的参数名称
* @param extend
* @param sqlToyConfig
* @return
*/
private static String[] getDbShardingParams(QueryExecutorExtend extend, SqlToyConfig sqlToyConfig) {
Set<String> keys = new HashSet<String>();
List<String> params = new ArrayList<String>();
String key;
// 分库参数
ShardingStrategyConfig dbSharding = extend.dbSharding;
if (dbSharding == null) {
dbSharding = sqlToyConfig.getDataSourceSharding();
}
if (dbSharding != null && dbSharding.getFields() != null) {
for (String item : dbSharding.getFields()) {
key = item.toLowerCase();
if (!keys.contains(key)) {
keys.add(key);
params.add(item);
}
}
}
if (params.isEmpty()) {
return null;
}
return params.toArray(new String[params.size()]);
}
use of org.sagacity.sqltoy.config.model.ShardingStrategyConfig in project sagacity-sqltoy by chenrenfei.
the class QueryExecutorBuilder method getTableShardingParams.
/**
* @TODO 获取分表的参数名称
* @param extend
* @param sqlToyConfig
* @return
*/
private static String[] getTableShardingParams(QueryExecutorExtend extend, SqlToyConfig sqlToyConfig) {
Set<String> keys = new HashSet<String>();
List<String> params = new ArrayList<String>();
String key;
// 分表参数
List<ShardingStrategyConfig> tableShardings = extend.tableShardings;
if (tableShardings == null || tableShardings.isEmpty()) {
tableShardings = sqlToyConfig.getTableShardings();
}
if (tableShardings != null && tableShardings.size() > 0) {
for (ShardingStrategyConfig shardingStrategy : tableShardings) {
if (shardingStrategy.getFields() != null) {
for (String item : shardingStrategy.getFields()) {
key = item.toLowerCase();
if (!keys.contains(key)) {
keys.add(key);
params.add(item);
}
}
}
}
}
if (params.isEmpty()) {
return null;
}
return params.toArray(new String[params.size()]);
}
use of org.sagacity.sqltoy.config.model.ShardingStrategyConfig in project sagacity-sqltoy by chenrenfei.
the class QueryExecutor method tableSharding.
/**
* @TODO 设置分表策略,再复杂场景则推荐用xml的sql中定义
* @param strategy
* @param tables
* @param paramNames 分表策略依赖的参数
* @return
*/
public QueryExecutor tableSharding(String strategy, String[] tables, String... paramNames) {
ShardingStrategyConfig sharding = new ShardingStrategyConfig(1);
sharding.setTables(tables);
sharding.setStrategy(strategy);
sharding.setFields(paramNames);
sharding.setAliasNames(paramNames);
innerModel.tableShardings.add(sharding);
return this;
}
Aggregations