Browse Source

git-svn-id: https://192.168.57.71/svn/jsgkj@313 931142cf-59ea-a443-aa0e-51397b428577

xt_yuanxd 9 years ago
parent
commit
4e409d07e8

+ 5 - 0
xtdsp/trunk/src/main/java/com/xt/dsp/controller/TaskCtl.java

@@ -24,6 +24,11 @@ public class TaskCtl {
 	public JsonResult runTask(@PathVariable String code, String condition) {
 		TaskBean task = taskService.selectByCode(code);
 		JsonResult result = new JsonResult();
+		if (task == null) {
+			result.setSuccess(false);
+			result.setMessage("任务不存在:" + code);
+		}
+
 		if (TaskBean.TYPE_SQL.equals(task.getType())) {
 			int res = taskSqlService.runTask(task, condition);
 			if (res == 0) {

+ 53 - 32
xtdsp/trunk/src/main/java/com/xt/dsp/serviceImpl/TaskSqlServiceImpl.java

@@ -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) {