/
JdbcSqlReadCursor.java
167 lines (141 loc) · 6.1 KB
/
JdbcSqlReadCursor.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.db.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JdbcSqlReadCursor<T> implements ISqlReadCursor<T> {
static final Logger log = LoggerFactory.getLogger(JdbcSqlReadCursor.class);
protected Connection c;
protected ResultSet rs;
protected Statement st;
protected boolean autoCommitFlag;
protected ISqlRowMapper<T> mapper;
protected JdbcSqlTemplate sqlTemplate;
protected int rowNumber;
protected int originalIsolationLevel;
protected ResultSetMetaData rsMetaData = null;
protected int rsColumnCount;
protected IConnectionHandler connectionHandler;
public JdbcSqlReadCursor() {
}
public JdbcSqlReadCursor(JdbcSqlTemplate sqlTemplate, ISqlRowMapper<T> mapper, String sql,
Object[] values, int[] types) {
this(sqlTemplate, mapper, sql, values, types, null);
}
public JdbcSqlReadCursor(JdbcSqlTemplate sqlTemplate, ISqlRowMapper<T> mapper, String sql,
Object[] values, int[] types, IConnectionHandler connectionHandler) {
this.sqlTemplate = sqlTemplate;
this.mapper = mapper;
this.connectionHandler = connectionHandler;
try {
c = sqlTemplate.getDataSource().getConnection();
if (this.connectionHandler != null) {
this.connectionHandler.before(c);
}
originalIsolationLevel = c.getTransactionIsolation();
autoCommitFlag = c.getAutoCommit();
if (c.getTransactionIsolation() != sqlTemplate.getIsolationLevel()) {
c.setTransactionIsolation(sqlTemplate.getIsolationLevel());
}
if (sqlTemplate.isRequiresAutoCommitFalseToSetFetchSize()) {
c.setAutoCommit(false);
}
try {
if (values != null) {
PreparedStatement pstmt = c.prepareStatement(sql,
sqlTemplate.getSettings().getResultSetType(),
ResultSet.CONCUR_READ_ONLY);
sqlTemplate.setValues(pstmt, values, types, sqlTemplate.getLobHandler()
.getDefaultHandler());
st = pstmt;
st.setQueryTimeout(sqlTemplate.getSettings().getQueryTimeout());
st.setFetchSize(sqlTemplate.getSettings().getFetchSize());
rs = pstmt.executeQuery();
} else {
st = c.createStatement(sqlTemplate.getSettings().getResultSetType(),
ResultSet.CONCUR_READ_ONLY);
st.setQueryTimeout(sqlTemplate.getSettings().getQueryTimeout());
st.setFetchSize(sqlTemplate.getSettings().getFetchSize());
rs = st.executeQuery(sql);
}
} catch (SQLException e) {
/*
* The Xerial SQLite JDBC driver throws an exception if a query
* returns an empty set This gets around that
*/
if (e.getMessage() == null
|| !e.getMessage().equalsIgnoreCase("query does not return results")) {
throw e;
}
}
SqlUtils.addSqlReadCursor(this);
} catch (SQLException ex) {
close();
throw sqlTemplate.translate("Failed to execute sql: " + sql, ex);
} catch (Throwable ex) {
close();
throw sqlTemplate.translate("Failed to execute sql: " + sql, ex);
}
}
public T next() {
try {
while (rs!=null && rs.next()) {
if (rsMetaData == null) {
rsMetaData = rs.getMetaData();
rsColumnCount = rsMetaData.getColumnCount();
}
Row row = getMapForRow(rs, rsMetaData, rsColumnCount, sqlTemplate.getSettings().isReadStringsAsBytes());
T value = mapper.mapRow(row);
if (value != null) {
return value;
}
}
return null;
} catch (SQLException ex) {
throw sqlTemplate.translate(ex);
}
}
protected static Row getMapForRow(ResultSet rs, ResultSetMetaData argResultSetMetaData,
int columnCount, boolean readStringsAsBytes) throws SQLException {
Row mapOfColValues = new Row(columnCount);
for (int i = 1; i <= columnCount; i++) {
String key = JdbcSqlTemplate.lookupColumnName(argResultSetMetaData, i);
Object obj = JdbcSqlTemplate.getResultSetValue(rs, argResultSetMetaData, i, readStringsAsBytes);
mapOfColValues.put(key, obj);
}
return mapOfColValues;
}
public void close() {
if (this.connectionHandler != null) {
this.connectionHandler.after(c);
}
JdbcSqlTemplate.close(rs);
JdbcSqlTemplate.close(st);
JdbcSqlTemplate.close(autoCommitFlag, originalIsolationLevel, c);
SqlUtils.removeSqlReadCursor(this);
}
}