com.github.rrsunhome.excelsql.format.SqlRowFormatter Maven / Gradle / Ivy
package com.github.rrsunhome.excelsql.format;
import com.github.rrsunhome.excelsql.config.SqlFormatConfig;
import com.github.rrsunhome.excelsql.config.SqlParameterIndexValue;
import com.github.rrsunhome.excelsql.parser.CellDefinition;
import com.github.rrsunhome.excelsql.parser.RowDefinition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author : wangqijia
* create at: 2021/11/5 下午5:39
*/
public class SqlRowFormatter implements RowFormatter {
private final String sql;
private final Map> parameterIndexMap;
private final Formatter formatter;
public SqlRowFormatter(SqlFormatConfig config) {
this.sql = config.getSql();
this.parameterIndexMap = config.getParameterValueMap();
this.formatter = config.getFormatter();
}
@Override
public ResultSet format(List rowDefinitions) {
List sqlList = new ArrayList<>(16);
for (RowDefinition rowDefinition : rowDefinitions) {
sqlList.add(format(rowDefinition));
}
return new SqlResultSet(sqlList);
}
private String format(RowDefinition rowDefinition) {
Map cellValueMap = rowDefinition
.getCellDefinitions()
.stream()
.collect(Collectors.toMap(CellDefinition::getCellIndex, CellDefinition::getCellValue));
Object[] args = new String[parameterIndexMap.size()];
for (Map.Entry> entry : parameterIndexMap.entrySet()) {
SqlParameterIndexValue> parameterIndexValue = entry.getValue();
args[entry.getKey()] = parameterIndexValue.wrapValue(cellValueMap.get(parameterIndexValue.getCellIndex()));
}
return formatter.format(sql, args);
}
}