net.n2oapp.framework.engine.sql.SqlJpqlUtil Maven / Gradle / Ivy
package net.n2oapp.framework.engine.sql;
import net.n2oapp.criteria.api.util.QueryBlank;
import net.n2oapp.framework.api.exception.N2oException;
import net.n2oapp.framework.api.exception.N2oUserException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.toList;
import static net.n2oapp.criteria.api.util.NamedParameterUtils.prepareQuery;
/**
* User: iryabov
* Date: 21.02.13
* Time: 12:20
*/
@Deprecated
public class SqlJpqlUtil {
public static Object[] executeQueries(NamedParameterJdbcTemplate template, List queries, Map args) {
queries = queries.stream().map(String::trim).filter(q -> !q.isEmpty()).collect(toList());
Object[] res = new Object[queries.size()];
for (int i = 0; i < queries.size(); i++) {
try {
res[i] = executeQuery(template, args, queries.get(i));
} catch (Exception e) {
String summary = InvocationUtil.findSqlSummary(e);
if (summary != null) {
throw new N2oUserException(summary);
}
throw new N2oException("SQL:" + queries.get(i)+ " Args:" + args, e);
}
}
return res;
}
private static Object executeQuery(NamedParameterJdbcTemplate template, Map args, String query) {
QueryBlank queryBlank = prepareQuery(query, args);
query = queryBlank.getQuery();
args = queryBlank.getArgs();
if (isSelect(query)) {
List