Completed row mapper provider for jdbc data query implements for sql core

This commit is contained in:
Sambo Chea 2022-07-05 23:26:38 +07:00
parent 429c8ba243
commit 0f2c0f8fa1
2 changed files with 82 additions and 2 deletions

View File

@ -11,6 +11,10 @@ public interface JdbcDataQuery extends JdbcAccessor {
IExecuteResult<List<Object>> queryForList(String sql, Object... args);
<R> IExecuteResult<List<R>> queryForList(String sql, RowMapperProvider<R> mapper);
<R> IExecuteResult<List<R>> queryForList(String sql, RowMapperProvider<R> mapper, Object... args);
IExecuteResult<Object> queryForObject(String sql);
IExecuteResult<Object> queryForObject(String sql, Object... args);
@ -19,6 +23,14 @@ public interface JdbcDataQuery extends JdbcAccessor {
<R> IExecuteResult<R> query(MapperProvider<Map<String, Object>, R> mapper, String sql, Class<R> clazz, Object... args);
/**
* Query the Object "R" and the first row.
*
* @param sql String
* @param mapper RowMapperProvider
* @param <R> Result
* @return IExecuteResult
*/
<R> IExecuteResult<R> query(String sql, RowMapperProvider<R> mapper);
<R> IExecuteResult<R> query(String sql, RowMapperProvider<R> mapper, Object... args);
@ -32,6 +44,15 @@ public interface JdbcDataQuery extends JdbcAccessor {
return queryForList(parameter.getFormatSql(), args);
}
default <R> IExecuteResult<List<R>> queryForList(ISqlMapParameter parameter, RowMapperProvider<R> mapper) {
if (parameter.getParams().size() == 0) {
return queryForList(parameter.getSql(), mapper);
}
Object[] args = parameter.getSqlArgs();
return queryForList(parameter.getFormatSql(), mapper, args);
}
default IExecuteResult<Object> queryForObject(ISqlMapParameter parameter) {
if (parameter.getParams().size() == 0) {
return queryForObject(parameter.getSql());

View File

@ -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 <R> IExecuteResult<List<R>> queryForList(String sql, RowMapperProvider<R> mapper) {
ExecuteResult.ExecuteResultBuilder<List<R>> builder = ExecuteResult.builder();
List<R> 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 <R> IExecuteResult<List<R>> queryForList(String sql, RowMapperProvider<R> mapper, Object... args) {
ExecuteResult.ExecuteResultBuilder<List<R>> builder = ExecuteResult.builder();
List<R> 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<Object> queryForObject(String sql) {
ExecuteResult.ExecuteResultBuilder<Object> builder = ExecuteResult.builder();
@ -105,11 +143,32 @@ public class JdbcDataQueryImpl implements JdbcDataQuery {
@Override
public <R> IExecuteResult<R> query(String sql, RowMapperProvider<R> mapper) {
return null;
ExecuteResult.ExecuteResultBuilder<R> 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 <R> IExecuteResult<R> query(String sql, RowMapperProvider<R> mapper, Object... args) {
return null;
ExecuteResult.ExecuteResultBuilder<R> 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();
}
}