| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
package net.smartlab.web.page; |
| 25 |
|
|
| 26 |
|
import java.sql.Connection; |
| 27 |
|
import java.sql.ResultSet; |
| 28 |
|
import java.sql.ResultSetMetaData; |
| 29 |
|
import java.sql.SQLException; |
| 30 |
|
import java.sql.Statement; |
| 31 |
|
|
| 32 |
|
import javax.sql.DataSource; |
| 33 |
|
|
| 34 |
|
import org.apache.commons.logging.Log; |
| 35 |
|
import org.apache.commons.logging.LogFactory; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
@author |
| 41 |
|
|
|
|
|
| 0% |
Uncovered Elements: 64 (64) |
Complexity: 18 |
Complexity Density: 0,41 |
|
| 42 |
|
public class SQLPaginator extends Paginator { |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
protected final Log logger = LogFactory.getLog(SQLPaginator.class); |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
private final static int DEFAULT_ISOLATION_LEVEL = -1; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private DataSource pool; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private String query; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private int isolation; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
@param |
| 76 |
|
@throws |
| 77 |
|
|
| 78 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 79 |
0
|
public SQLPaginator(DataSource pool, String query) throws SQLException {... |
| 80 |
0
|
this(pool, query, 0); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
@param |
| 88 |
|
@param |
| 89 |
|
@param |
| 90 |
|
@throws |
| 91 |
|
|
| 92 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 93 |
0
|
public SQLPaginator(DataSource pool, String query, int size) throws SQLException {... |
| 94 |
0
|
this(pool, query, size, Paginator.UNLIMITED_PAGES); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
@param |
| 101 |
|
@param |
| 102 |
|
@param |
| 103 |
|
@param |
| 104 |
|
@throws |
| 105 |
|
|
| 106 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 107 |
0
|
public SQLPaginator(DataSource pool, String query, int size, int pages) throws SQLException {... |
| 108 |
0
|
this(pool, query, size, pages, DEFAULT_ISOLATION_LEVEL); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@param |
| 115 |
|
@param |
| 116 |
|
@param |
| 117 |
|
@param |
| 118 |
|
@param |
| 119 |
|
|
| 120 |
|
@throws |
| 121 |
|
|
| 122 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 3 |
Complexity Density: 0,19 |
|
| 123 |
0
|
public SQLPaginator(DataSource pool, String query, int size, int pages, int isolation) throws SQLException {... |
| 124 |
0
|
super(size, pages); |
| 125 |
0
|
this.pool = pool; |
| 126 |
0
|
this.query = query; |
| 127 |
0
|
this.isolation = isolation; |
| 128 |
0
|
Connection connection = null; |
| 129 |
0
|
try { |
| 130 |
0
|
connection = pool.getConnection(); |
| 131 |
0
|
if (isolation != DEFAULT_ISOLATION_LEVEL) { |
| 132 |
0
|
connection.setTransactionIsolation(isolation); |
| 133 |
|
} |
| 134 |
0
|
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
| 135 |
0
|
statement.setFetchDirection(ResultSet.TYPE_FORWARD_ONLY); |
| 136 |
0
|
ResultSet rows = connection.createStatement().executeQuery(query); |
| 137 |
0
|
rows.last(); |
| 138 |
0
|
super.setCount(rows.getRow()); |
| 139 |
|
} finally { |
| 140 |
0
|
try { |
| 141 |
0
|
connection.close(); |
| 142 |
|
} catch (SQLException sqle) { |
| 143 |
|
logger.warn("SQLPaginator(pool = " + pool + ", query = " + query + ", size = " + size + ", pages = " + pages + ", isolation = " |
| 144 |
|
+ isolation + ") - failed connection close", sqle); |
| 145 |
|
} |
| 146 |
|
} |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
@see |
| 151 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 8 |
Complexity Density: 0,47 |
|
| 152 |
0
|
protected void setArray() {... |
| 153 |
0
|
if (logger.isTraceEnabled()) { |
| 154 |
0
|
logger.trace("setArray() - start"); |
| 155 |
|
} |
| 156 |
0
|
Connection connection = null; |
| 157 |
0
|
try { |
| 158 |
0
|
connection = pool.getConnection(); |
| 159 |
0
|
if (isolation != DEFAULT_ISOLATION_LEVEL) { |
| 160 |
0
|
connection.setTransactionIsolation(isolation); |
| 161 |
|
} |
| 162 |
0
|
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
| 163 |
0
|
ResultSet rows = statement.executeQuery(query); |
| 164 |
0
|
rows.absolute(((this.getPage() - 1) * array.length) + 1); |
| 165 |
0
|
for (int i = 0; i < array.length; i++) { |
| 166 |
0
|
array[i] = this.getObject(rows); |
| 167 |
0
|
if (!rows.next() && !(i == array.length - 1)) { |
| 168 |
0
|
array[i + 1] = null; |
| 169 |
0
|
break; |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
} catch (SQLException sqle) { |
| 173 |
|
throw new PaginationException(sqle); |
| 174 |
|
} finally { |
| 175 |
0
|
try { |
| 176 |
0
|
connection.close(); |
| 177 |
|
} catch (SQLException sqle) { |
| 178 |
|
logger.warn("setArray() - failed connection close", sqle); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
@param |
| 187 |
|
@return |
| 188 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0,5 |
|
| 189 |
0
|
protected Object[] getObject(ResultSet rows) {... |
| 190 |
0
|
if (logger.isTraceEnabled()) { |
| 191 |
0
|
logger.trace("getObject(rows = " + rows + ") - start"); |
| 192 |
|
} |
| 193 |
0
|
try { |
| 194 |
0
|
ResultSetMetaData info = rows.getMetaData(); |
| 195 |
0
|
Object[] values = new Object[info.getColumnCount()]; |
| 196 |
0
|
for (int i = 0; i < values.length; i++) { |
| 197 |
0
|
values[i] = rows.getObject(i + 1); |
| 198 |
|
} |
| 199 |
0
|
return values; |
| 200 |
|
} catch (SQLException sqle) { |
| 201 |
|
logger.error("getObject(rows = " + rows + ") - error", sqle); |
| 202 |
|
return null; |
| 203 |
|
} |
| 204 |
|
} |
| 205 |
|
} |