|
1 | 1 | package me.zort.sqllib; |
2 | 2 |
|
| 3 | +import com.google.common.annotations.Beta; |
3 | 4 | import com.google.gson.Gson; |
4 | 5 | import lombok.Getter; |
5 | 6 | import lombok.RequiredArgsConstructor; |
|
32 | 33 | import org.jetbrains.annotations.NotNull; |
33 | 34 | import org.jetbrains.annotations.Nullable; |
34 | 35 |
|
| 36 | +import javax.sql.rowset.CachedRowSet; |
| 37 | +import javax.sql.rowset.RowSetProvider; |
35 | 38 | import java.sql.*; |
36 | 39 | import java.util.Objects; |
37 | 40 | import java.util.Optional; |
@@ -274,11 +277,14 @@ public QueryRowsResult<Row> query(final @NotNull String query) { |
274 | 277 | @NotNull |
275 | 278 | QueryRowsResult<Row> query(final @NotNull Query query, boolean isRetry) { |
276 | 279 | Objects.requireNonNull(query); |
277 | | - if (!handleAutoReconnect()) |
| 280 | + if (!handleAutoReconnect()) { |
278 | 281 | return new QueryRowsResult<>(false, "Cannot connect to database!"); |
| 282 | + } |
279 | 283 |
|
280 | 284 | QueryResult cachedResult = cacheManager.get(query, false); |
281 | | - if (cachedResult instanceof QueryRowsResult) return (QueryRowsResult<Row>) cachedResult; |
| 285 | + if (cachedResult instanceof QueryRowsResult) { |
| 286 | + return (QueryRowsResult<Row>) cachedResult; |
| 287 | + } |
282 | 288 |
|
283 | 289 | try (PreparedStatement stmt = buildStatement(query); |
284 | 290 | ResultSet resultSet = stmt.executeQuery()) { |
@@ -310,6 +316,31 @@ QueryRowsResult<Row> query(final @NotNull Query query, boolean isRetry) { |
310 | 316 | } |
311 | 317 | } |
312 | 318 |
|
| 319 | + /** |
| 320 | + * Executes given query and returns raw ResultSet. |
| 321 | + * Please note that this function is not recommended to be frequently used and is provided |
| 322 | + * as is as feature. |
| 323 | + * |
| 324 | + * @param query Query to use for building query string. |
| 325 | + * @return ResultSet object or null if there was an error in connection. |
| 326 | + * @throws SQLException If there was an error while executing query. |
| 327 | + */ |
| 328 | + @Beta |
| 329 | + @Nullable |
| 330 | + public ResultSet queryRaw(Query query) throws SQLException { |
| 331 | + Objects.requireNonNull(query); |
| 332 | + if (!handleAutoReconnect()) { |
| 333 | + return null; |
| 334 | + } |
| 335 | + try (PreparedStatement stmt = buildStatement(query); |
| 336 | + ResultSet resultSet = stmt.executeQuery()) { |
| 337 | + // Create in-memory cached result set |
| 338 | + CachedRowSet cachedResultSet = RowSetProvider.newFactory().createCachedRowSet(); |
| 339 | + cachedResultSet.populate(resultSet); |
| 340 | + return cachedResultSet; |
| 341 | + } |
| 342 | + } |
| 343 | + |
313 | 344 | /** |
314 | 345 | * Executes given query and returns execution result. |
315 | 346 | * This result does not contain any rows. If you want to |
|
0 commit comments