|
@@ -146,6 +146,28 @@ public class TaskSqlServiceImpl implements TaskSqlService {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+ private Map<String, Object> getParamMapFromCondition(String condition) {
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ if (null == condition) {
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+ String[] conditions = condition.split(";");
|
|
|
+ int index = -1;
|
|
|
+ for (String c : conditions) {
|
|
|
+ index = c.indexOf('=');
|
|
|
+ if (index < 0) {
|
|
|
+ throw new IllegalArgumentException("条件不合法:" + c);
|
|
|
+ }
|
|
|
+ String name = c.substring(0, index);
|
|
|
+ if (name.endsWith("_IN")) {
|
|
|
+ param.put(name, Arrays.asList(c.substring(index + 1).split(",")));
|
|
|
+ } else {
|
|
|
+ param.put(name, c.substring(index + 1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取sql检索结果
|
|
|
*
|
|
@@ -156,40 +178,39 @@ public class TaskSqlServiceImpl implements TaskSqlService {
|
|
|
long start = System.currentTimeMillis();
|
|
|
String dsid = taskSqlBean.getSrcConn();
|
|
|
DataSourceBean dsb = dataSourceService.selectByPrimaryKey(dsid);
|
|
|
- JdbcTemplate template = JdbcTemplateUtils.getJdbcTemplate(dsb);
|
|
|
+ NamedParameterJdbcTemplate nameJdbcTemplate = new NamedParameterJdbcTemplate(
|
|
|
+ JdbcTemplateUtils.getJdbcTemplate(dsb));
|
|
|
String sql = taskSqlBean.getQuerySql();
|
|
|
- if (StringUtils.hasText(condition)) {
|
|
|
- sql += " " + condition;
|
|
|
- }
|
|
|
- JSONObject result = template.query(sql, new ResultSetExtractor<JSONObject>() {
|
|
|
- @Override
|
|
|
- public JSONObject extractData(ResultSet rs) throws SQLException, DataAccessException {
|
|
|
- JSONObject result = new JSONObject();
|
|
|
- ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
- StringBuilder columns = new StringBuilder();
|
|
|
- for (int i = 0; i < rsmd.getColumnCount(); i++) {
|
|
|
- columns.append(rsmd.getColumnName(i + 1).toUpperCase());
|
|
|
- columns.append(";");
|
|
|
- }
|
|
|
- if (columns.length() > 0) {
|
|
|
- columns.deleteCharAt(columns.length() - 1);
|
|
|
- }
|
|
|
- JSONArray results = new JSONArray();
|
|
|
- while (rs.next()) {
|
|
|
- JSONObject resultJson = new JSONObject();
|
|
|
- for (String column : columns.toString().split(";")) {
|
|
|
- resultJson.put(column, rs.getObject(column));
|
|
|
+ JSONObject result = nameJdbcTemplate.query(sql, getParamMapFromCondition(condition),
|
|
|
+ new ResultSetExtractor<JSONObject>() {
|
|
|
+ @Override
|
|
|
+ public JSONObject extractData(ResultSet rs) throws SQLException, DataAccessException {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
+ StringBuilder columns = new StringBuilder();
|
|
|
+ for (int i = 0; i < rsmd.getColumnCount(); i++) {
|
|
|
+ columns.append(rsmd.getColumnName(i + 1).toUpperCase());
|
|
|
+ columns.append(";");
|
|
|
+ }
|
|
|
+ if (columns.length() > 0) {
|
|
|
+ columns.deleteCharAt(columns.length() - 1);
|
|
|
+ }
|
|
|
+ JSONArray results = new JSONArray();
|
|
|
+ while (rs.next()) {
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ for (String column : columns.toString().split(";")) {
|
|
|
+ resultJson.put(column, rs.getObject(column));
|
|
|
+ }
|
|
|
+ results.add(resultJson);
|
|
|
+ }
|
|
|
+ if (results.size() == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ result.put("columns", columns.toString());
|
|
|
+ result.put("data", results);
|
|
|
+ return result;
|
|
|
}
|
|
|
- results.add(resultJson);
|
|
|
- }
|
|
|
- if (results.size() == 0) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- result.put("columns", columns.toString());
|
|
|
- result.put("data", results);
|
|
|
- return result;
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
if (log.isInfoEnabled()) {
|
|
|
int size = 0;
|
|
|
if (result != null && result.getJSONArray("data") != null) {
|