MultiRowSQLQueryResultHandler.java
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package io.mycat.sqlengine;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 当SQLJob的结果有多行时,利用该处理器进行处理
* @author digdeep@126.com
*/
public class MultiRowSQLQueryResultHandler extends OneRawSQLQueryResultHandler{
private static final Logger LOGGER = LoggerFactory
.getLogger(MultiRowSQLQueryResultHandler.class);
// 获得结果之后,利用该对象进行回调进行通知和处理结果
private final SQLQueryResultListener<SQLQueryResult<List<Map<String, String>>>> callback;
private List<Map<String, String>> resultRows = new LinkedList<>(); // 保存结果行
public MultiRowSQLQueryResultHandler(String[] fetchCols,
SQLQueryResultListener<SQLQueryResult<List<Map<String, String>>>> callback) {
super(fetchCols, null);
this.callback = callback;
}
@Override
public boolean onRowData(String dataNode, byte[] rowData) {
super.onRowData(dataNode, rowData);
resultRows.add(getResult());
return false;
}
@Override
public void finished(String dataNode, boolean failed, String errorMsg) {
SQLQueryResult<List<Map<String, String>>> queryResult =
new SQLQueryResult<List<Map<String, String>>>(this.resultRows, !failed);
queryResult.setErrMsg(errorMsg);
if(callback != null)
this.callback.onResult(queryResult); // callback 是构造函数传进来,在得到结果是进行回调
else
LOGGER.warn(" callback is null ");
}
}