remove limited/unlimited, add consumeRows

This commit is contained in:
Jörg Prante 2023-08-03 16:50:32 +02:00
parent d8144bd168
commit ad589e0d12
3 changed files with 51 additions and 41 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib
name = database
version = 1.3.0
version = 1.4.0
org.gradle.warning.mode = ALL

View file

@ -215,9 +215,18 @@ public interface Database extends Supplier<Database> {
Table getSingleRow(String statement, Map<String, Object> params);
Table getUnlimitedRows(String statement, Map<String, Object> params);
Table getTable(String statement,
Map<String, Object> params,
int limit,
int fetchSize,
int timeoutSeconds);
Table getLimitedRows(String statement, Map<String, Object> params, int limit, int fetchSize, int timeoutSeconds);
void consumeRows(String statement,
Map<String, Object> params,
int limit,
int fetchSize,
int timeoutSeconds,
Consumer<List<Object>> consumer);
void insert(String statement, Map<String, Object> params);

View file

@ -15,7 +15,6 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Level;
@ -298,7 +297,7 @@ public class DatabaseImpl implements Database {
if (table.getOffset() != null && table.getSize() != null) {
statement = statement + " offset :offset rows fetch next :limit rows only";
}
return getUnlimitedRows(statement, params);
return getTable(statement, params, 0, options().fetchSize(), options().timeoutSeconds());
}
@Override
@ -322,17 +321,12 @@ public class DatabaseImpl implements Database {
@Override
public Table getSingleRow(String statement, Map<String, Object> params) {
return getLimitedRows(statement, params, 1, options().fetchSize(), options().timeoutSeconds());
return getTable(statement, params, 1, options().fetchSize(), options().timeoutSeconds());
}
@Override
public Table getUnlimitedRows(String statement, Map<String, Object> params) {
return getLimitedRows(statement, params, 0, options().fetchSize(), options().timeoutSeconds());
}
@Override
public Table getLimitedRows(String statement, Map<String, Object> params,
int limit, int fetchSize, int timeoutSeconds) {
public Table getTable(String statement, Map<String, Object> params,
int limit, int fetchSize, int timeoutSeconds) {
SqlSelect sql = toSelect(statement).fetchSize(fetchSize).withTimeoutSeconds(timeoutSeconds);
selectParams(sql, params);
Table table = new Table();
@ -356,6 +350,32 @@ public class DatabaseImpl implements Database {
return table;
}
public void consumeRows(String statement,
Map<String, Object> params,
int limit,
int fetchSize,
int timeoutSeconds,
Consumer<List<Object>> consumer) {
SqlSelect sql = toSelect(statement).fetchSize(fetchSize).withTimeoutSeconds(timeoutSeconds);
selectParams(sql, params);
sql.query(rows -> {
ResultSetMetaData md = rows.getMetadata();
List<Object> columnLabels = new ArrayList<>();
List<Object> classNames = new ArrayList<>();
for (int i = 1; i <= md.getColumnCount(); i++) {
columnLabels.add(md.getColumnLabel(i));
classNames.add(md.getColumnClassName(i));
}
consumer.accept(columnLabels);
consumer.accept(classNames);
int i = 0;
while (rows.next() && (limit <= 0 || i++ < limit)) {
consumer.accept(getRow(rows, classNames));
}
return true;
});
}
@Override
public void insert(String statement, Map<String, Object> params) {
SqlInsert sql = toInsert(statement);
@ -494,34 +514,15 @@ public class DatabaseImpl implements Database {
for (int i = 0; i < classNames.size(); i++) {
String className = classNames.get(i).toString();
switch (className) {
case "java.lang.String":
row.add(rows.getStringOrEmpty(i + 1));
break;
case "java.lang.Integer":
row.add(rows.getIntegerOrNull(i + 1));
break;
case "java.lang.Long":
row.add(rows.getLongOrNull(i + 1));
break;
case "java.lang.Boolean":
row.add(rows.getBooleanOrFalse(i + 1));
break;
case "java.sql.Clob":
case "oracle.jdbc.OracleClob":
row.add(rows.getClobStringOrEmpty(i + 1));
break;
case "java.sql.Date":
row.add(rows.getLocalDateOrNull(i + 1));
break;
case "java.sql.Timestamp":
case "oracle.sql.TIMESTAMP":
row.add(rows.getLocalDateTimeOrNull(i + 1));
break;
case "java.math.BigDecimal":
row.add(rows.getBigDecimalOrNull(i + 1));
break;
default:
throw new DatabaseException("unexpected column class name: " + className);
case "java.lang.String" -> row.add(rows.getStringOrEmpty(i + 1));
case "java.lang.Integer" -> row.add(rows.getIntegerOrNull(i + 1));
case "java.lang.Long" -> row.add(rows.getLongOrNull(i + 1));
case "java.lang.Boolean" -> row.add(rows.getBooleanOrFalse(i + 1));
case "java.sql.Clob", "oracle.jdbc.OracleClob" -> row.add(rows.getClobStringOrEmpty(i + 1));
case "java.sql.Date" -> row.add(rows.getLocalDateOrNull(i + 1));
case "java.sql.Timestamp", "oracle.sql.TIMESTAMP" -> row.add(rows.getLocalDateTimeOrNull(i + 1));
case "java.math.BigDecimal" -> row.add(rows.getBigDecimalOrNull(i + 1));
default -> throw new DatabaseException("unexpected column class name: " + className);
}
}
return row;