1
|
package eu.dnetlib.enabling.database.resultset;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.springframework.jdbc.support.rowset.SqlRowSet;
|
11
|
|
12
|
import eu.dnetlib.enabling.database.rmi.DatabaseException;
|
13
|
import eu.dnetlib.enabling.database.utils.DatabaseUtils;
|
14
|
import eu.dnetlib.enabling.resultset.ResultSetListener;
|
15
|
|
16
|
public class SQLResultSetListener implements ResultSetListener {
|
17
|
|
18
|
private String db;
|
19
|
private String sql;
|
20
|
private DatabaseUtils dbUtils;
|
21
|
|
22
|
private int lastToPosition = 0;
|
23
|
private SqlRowSet rowSet = null;
|
24
|
|
25
|
private Integer size = null;
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(SQLResultSetListener.class); // NOPMD by marko on 11/24/08 5:02 PM
|
28
|
|
29
|
protected SQLResultSetListener() {
|
30
|
}
|
31
|
|
32
|
protected SQLResultSetListener(String db, String sql, DatabaseUtils dbUtils) {
|
33
|
super();
|
34
|
this.db = db;
|
35
|
this.sql = sql;
|
36
|
this.dbUtils = dbUtils;
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
public List<String> getResult(int fromPosition, int toPosition) {
|
41
|
if (fromPosition < 1) {
|
42
|
throw new IllegalArgumentException("fromPosition must be >= 1");
|
43
|
} else if (toPosition < fromPosition) {
|
44
|
throw new IllegalArgumentException("toPosition must be equal or greater than fromPosition");
|
45
|
}
|
46
|
int limit = toPosition - fromPosition + 1;
|
47
|
|
48
|
try {
|
49
|
List<String> list = new ArrayList<String>();
|
50
|
|
51
|
final SqlRowSet rows = getPage(fromPosition, toPosition);
|
52
|
while (rows.next() && limit-- > 0) {
|
53
|
Map<?, ?> o = parseRowSet(rows);
|
54
|
if (o instanceof Map<?, ?>) {
|
55
|
list.add(dbUtils.rowToDocument((Map<?, ?>) o).asXML());
|
56
|
}
|
57
|
}
|
58
|
|
59
|
lastToPosition = toPosition;
|
60
|
|
61
|
return list;
|
62
|
} catch (DatabaseException e) {
|
63
|
log.error("Error in getResult " + fromPosition + "-" + toPosition, e);
|
64
|
throw new IllegalStateException("Error in getResult " + fromPosition + "-" + toPosition, e);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
private SqlRowSet getPage(int fromPosition, int toPosition) throws DatabaseException {
|
69
|
if (fromPosition == lastToPosition + 1 && rowSet != null) {
|
70
|
log.debug("returning old rowset");
|
71
|
rowSet.previous();
|
72
|
return rowSet;
|
73
|
}
|
74
|
|
75
|
final int offset = fromPosition - 1;
|
76
|
|
77
|
String query = sql;
|
78
|
if (offset > 0) {
|
79
|
query += " OFFSET " + offset;
|
80
|
}
|
81
|
this.rowSet = dbUtils.executeSql(db, query, SqlRowSet.class);
|
82
|
return rowSet;
|
83
|
}
|
84
|
|
85
|
private Map<?, ?> parseRowSet(SqlRowSet rows) {
|
86
|
HashMap<String, Object> res = new HashMap<String, Object>();
|
87
|
for (String column : rows.getMetaData().getColumnNames())
|
88
|
res.put(column, rows.getObject(column));
|
89
|
return res;
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public int getSize() {
|
94
|
if (size != null) {
|
95
|
return size;
|
96
|
}
|
97
|
|
98
|
String query = "SELECT count(*) FROM ( " + sql + " ) AS TABLELISTENER";
|
99
|
|
100
|
try {
|
101
|
this.size = dbUtils.executeSql(db, query, Integer.class);
|
102
|
return size;
|
103
|
} catch (DatabaseException e) {
|
104
|
log.error("Error in getSize, query: " + query, e);
|
105
|
throw new IllegalStateException("Error in getSize, query: " + query, e);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
public String getDb() {
|
110
|
return db;
|
111
|
}
|
112
|
|
113
|
public void setDb(String db) {
|
114
|
this.db = db;
|
115
|
}
|
116
|
|
117
|
public String getSql() {
|
118
|
return sql;
|
119
|
}
|
120
|
|
121
|
public void setSql(String sql) {
|
122
|
this.sql = sql;
|
123
|
}
|
124
|
|
125
|
public DatabaseUtils getDbUtils() {
|
126
|
return dbUtils;
|
127
|
}
|
128
|
|
129
|
public void setDbUtils(DatabaseUtils dbUtils) {
|
130
|
this.dbUtils = dbUtils;
|
131
|
}
|
132
|
|
133
|
}
|