use of org.sagacity.sqltoy.config.model.QueryShardingModel in project sagacity-sqltoy by chenrenfei.
the class SqlXMLConfigParse method parseShardingTables.
/**
* @todo 解析table的sharding
* @param sqlToyModel
* @param shardingTables
*/
private static void parseShardingTables(SqlToyConfig sqlToyConfig, List<Element> shardingTables) {
if (shardingTables == null || shardingTables.isEmpty())
return;
List<QueryShardingModel> tablesShardings = new ArrayList();
String[] paramName;
String[] paramsAlias;
int size;
List<String> params = new ArrayList();
for (Element elt : shardingTables) {
if (elt.attribute("tables") != null && elt.attribute("strategy") != null) {
QueryShardingModel shardingModel = new QueryShardingModel();
shardingModel.setTables(elt.attributeValue("tables").split(","));
if (elt.attribute("params") != null) {
// params="a:a1,b:b1";params为{a:a1, b:b1}
shardingModel.setParams(elt.attributeValue("params").replace(";", ",").toLowerCase().split(","));
size = shardingModel.getParams().length;
paramsAlias = new String[size];
for (int i = 0; i < size; i++) {
paramName = shardingModel.getParams()[i].split(":");
// 重置params数组值
shardingModel.getParams()[i] = paramName[0].trim();
params.add(shardingModel.getParams()[i]);
paramsAlias[i] = paramName[paramName.length - 1].trim();
}
shardingModel.setParamsAlias(paramsAlias);
}
if (elt.attribute("strategy-value") != null)
shardingModel.setStrategyValue(elt.attributeValue("strategy-value"));
shardingModel.setStrategy(elt.attributeValue("strategy"));
tablesShardings.add(shardingModel);
}
}
if (!params.isEmpty()) {
String[] paramAry = new String[params.size()];
params.toArray(paramAry);
sqlToyConfig.setTableShardingParams(paramAry);
}
sqlToyConfig.setTablesShardings(tablesShardings);
}
use of org.sagacity.sqltoy.config.model.QueryShardingModel in project sagacity-sqltoy by chenrenfei.
the class ShardingUtils method getShardingTables.
/**
* @todo 获取sharding对应的表
* @param sqlToyContext
* @param sqlToyConfig
* @param paramNames
* @param paramValues
* @return
*/
private static HashMap<String, String> getShardingTables(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, String[] paramNames, Object[] paramValues) {
if (sqlToyConfig.getTablesShardings() == null)
return null;
IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(paramNames, paramValues);
HashMap<String, String> tableMap = new HashMap<String, String>();
String[] tables;
String table;
String shardingTable;
ShardingStrategy shardingStrategy;
IgnoreCaseLinkedMap<String, Object> realDataMap = null;
for (QueryShardingModel shardingModel : sqlToyConfig.getTablesShardings()) {
shardingStrategy = sqlToyContext.getShardingStrategy(shardingModel.getStrategy());
if (shardingStrategy != null) {
tables = shardingModel.getTables();
if (shardingModel.getParams() != null) {
realDataMap = new IgnoreCaseLinkedMap<String, Object>();
for (int i = 0, n = shardingModel.getParams().length; i < n; i++) {
realDataMap.put(shardingModel.getParamsAlias()[i], valueMap.get(shardingModel.getParams()[i]));
}
} else
realDataMap = valueMap;
for (int i = 0; i < tables.length; i++) {
table = tables[i];
shardingTable = shardingStrategy.getShardingTable(sqlToyContext, null, table, shardingModel.getStrategyValue(), realDataMap);
if (null != shardingTable && !shardingTable.equalsIgnoreCase(table))
tableMap.put(table, shardingTable);
}
} else {
System.err.println("sharding strategy:".concat(shardingModel.getStrategy()).concat(" don't exist,please check sharding config!"));
logger.error("sharding strategy:{} don't exist,please check sharding config!", shardingModel.getStrategy());
}
}
return tableMap;
}
Aggregations