|
@@ -94,7 +94,7 @@ public abstract class AbstractDatabaseConnector implements Database {
|
|
|
// 获取查询SQL
|
|
|
Table table = commandConfig.getTable();
|
|
|
String type = SqlBuilderEnum.QUERY.getName();
|
|
|
- String querySql = getQuerySql(type, table, queryFilterSql);
|
|
|
+ String querySql = buildSql(type, table, queryFilterSql);
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
map.put(type, querySql);
|
|
|
return map;
|
|
@@ -107,23 +107,43 @@ public abstract class AbstractDatabaseConnector implements Database {
|
|
|
Table table = commandConfig.getTable();
|
|
|
|
|
|
String insert = SqlBuilderEnum.INSERT.getName();
|
|
|
- map.put(insert, getQuerySql(insert, table, null));
|
|
|
+ map.put(insert, buildSql(insert, table, null));
|
|
|
|
|
|
String update = SqlBuilderEnum.UPDATE.getName();
|
|
|
- map.put(update, getQuerySql(update, table, null));
|
|
|
+ map.put(update, buildSql(update, table, null));
|
|
|
|
|
|
String delete = SqlBuilderEnum.DELETE.getName();
|
|
|
- map.put(delete, getQuerySql(delete, table, null));
|
|
|
+ map.put(delete, buildSql(delete, table, null));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Result reader(ConnectorConfig config, Map<String, String> command, int pageIndex, int pageSize) {
|
|
|
- // TODO 实现读取
|
|
|
- // 1、获取连接
|
|
|
- // 2、获取select SQL
|
|
|
- // 3、设置参数
|
|
|
- return null;
|
|
|
+ // 1、获取select SQL
|
|
|
+ String querySql = command.get(SqlBuilderEnum.QUERY.getName());
|
|
|
+ Assert.hasText(querySql, "查询语句不能为空.");
|
|
|
+
|
|
|
+ DatabaseConfig cfg = (DatabaseConfig) config;
|
|
|
+ JdbcTemplate jdbcTemplate = null;
|
|
|
+ try {
|
|
|
+ // 2、获取连接
|
|
|
+ jdbcTemplate = getJdbcTemplate(cfg);
|
|
|
+
|
|
|
+ // 3、设置参数
|
|
|
+ Object[] args = getPageArgs(pageIndex, pageSize);
|
|
|
+
|
|
|
+ // 4、执行SQL
|
|
|
+ List<Map<String, Object>> list = jdbcTemplate.queryForList(querySql, args);
|
|
|
+
|
|
|
+ // 5、返回结果集
|
|
|
+ return new Result(list);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ throw new ConnectorException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ // 释放连接
|
|
|
+ this.close(jdbcTemplate);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -193,7 +213,7 @@ public abstract class AbstractDatabaseConnector implements Database {
|
|
|
* @param queryFilterSQL
|
|
|
* @return
|
|
|
*/
|
|
|
- private String getQuerySql(String type, Table table, String queryFilterSQL) {
|
|
|
+ private String buildSql(String type, Table table, String queryFilterSQL) {
|
|
|
if (null == table) {
|
|
|
logger.error("Table can not be null.");
|
|
|
throw new ConnectorException("Table can not be null.");
|