diff --git a/src/main/java/com/cubetiqs/sql/JdbcDataQuery.java b/src/main/java/com/cubetiqs/sql/JdbcDataQuery.java index 389dcfa..827c07a 100644 --- a/src/main/java/com/cubetiqs/sql/JdbcDataQuery.java +++ b/src/main/java/com/cubetiqs/sql/JdbcDataQuery.java @@ -11,6 +11,10 @@ public interface JdbcDataQuery extends JdbcAccessor { IExecuteResult> queryForList(String sql, Object... args); + IExecuteResult> queryForList(String sql, RowMapperProvider mapper); + + IExecuteResult> queryForList(String sql, RowMapperProvider mapper, Object... args); + IExecuteResult queryForObject(String sql); IExecuteResult queryForObject(String sql, Object... args); @@ -19,6 +23,14 @@ public interface JdbcDataQuery extends JdbcAccessor { IExecuteResult query(MapperProvider, R> mapper, String sql, Class clazz, Object... args); + /** + * Query the Object "R" and the first row. + * + * @param sql String + * @param mapper RowMapperProvider + * @param Result + * @return IExecuteResult + */ IExecuteResult query(String sql, RowMapperProvider mapper); IExecuteResult query(String sql, RowMapperProvider mapper, Object... args); @@ -32,6 +44,15 @@ public interface JdbcDataQuery extends JdbcAccessor { return queryForList(parameter.getFormatSql(), args); } + default IExecuteResult> queryForList(ISqlMapParameter parameter, RowMapperProvider mapper) { + if (parameter.getParams().size() == 0) { + return queryForList(parameter.getSql(), mapper); + } + + Object[] args = parameter.getSqlArgs(); + return queryForList(parameter.getFormatSql(), mapper, args); + } + default IExecuteResult queryForObject(ISqlMapParameter parameter) { if (parameter.getParams().size() == 0) { return queryForObject(parameter.getSql()); diff --git a/src/main/java/com/cubetiqs/sql/JdbcDataQueryImpl.java b/src/main/java/com/cubetiqs/sql/JdbcDataQueryImpl.java index b9c4fbf..9e605b7 100644 --- a/src/main/java/com/cubetiqs/sql/JdbcDataQueryImpl.java +++ b/src/main/java/com/cubetiqs/sql/JdbcDataQueryImpl.java @@ -7,6 +7,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -49,6 +50,43 @@ public class JdbcDataQueryImpl implements JdbcDataQuery { return builder.build(); } + @Override + public IExecuteResult> queryForList(String sql, RowMapperProvider mapper) { + ExecuteResult.ExecuteResultBuilder> builder = ExecuteResult.builder(); + List result = new LinkedList<>(); + try (Statement statement = manager.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + ResultSet rs = statement.executeQuery(sql); + int rowNum = 0; + while (rs.next()) { + rowNum++; + result.add(mapper.map(rs, rowNum)); + } + } catch (SQLException ex) { + builder.error(ex); + } + + return builder.data(result).build(); + } + + @Override + public IExecuteResult> queryForList(String sql, RowMapperProvider mapper, Object... args) { + ExecuteResult.ExecuteResultBuilder> builder = ExecuteResult.builder(); + List result = new LinkedList<>(); + try (PreparedStatement statement = manager.getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + ResultSetUtil.applyParameterized(statement, sql, args); + ResultSet rs = statement.executeQuery(); + int rowNum = 0; + while (rs.next()) { + rowNum++; + result.add(mapper.map(rs, rowNum)); + } + } catch (SQLException ex) { + builder.error(ex); + } + + return builder.data(result).build(); + } + @Override public IExecuteResult queryForObject(String sql) { ExecuteResult.ExecuteResultBuilder builder = ExecuteResult.builder(); @@ -105,11 +143,32 @@ public class JdbcDataQueryImpl implements JdbcDataQuery { @Override public IExecuteResult query(String sql, RowMapperProvider mapper) { - return null; + ExecuteResult.ExecuteResultBuilder builder = ExecuteResult.builder(); + try (Statement statement = manager.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + ResultSet rs = statement.executeQuery(sql); + if (rs.next()) { + builder.data(mapper.map(rs, 1)); + } + } catch (SQLException ex) { + builder.error(ex); + } + + return builder.build(); } @Override public IExecuteResult query(String sql, RowMapperProvider mapper, Object... args) { - return null; + ExecuteResult.ExecuteResultBuilder builder = ExecuteResult.builder(); + try (PreparedStatement statement = manager.getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + ResultSetUtil.applyParameterized(statement, sql, args); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + builder.data(mapper.map(rs, 1)); + } + } catch (SQLException ex) { + builder.error(ex); + } + + return builder.build(); } }