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 group = org.xbib
name = database name = database
version = 1.3.0 version = 1.4.0
org.gradle.warning.mode = ALL 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 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); 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
@ -298,7 +297,7 @@ public class DatabaseImpl implements Database {
if (table.getOffset() != null && table.getSize() != null) { if (table.getOffset() != null && table.getSize() != null) {
statement = statement + " offset :offset rows fetch next :limit rows only"; statement = statement + " offset :offset rows fetch next :limit rows only";
} }
return getUnlimitedRows(statement, params); return getTable(statement, params, 0, options().fetchSize(), options().timeoutSeconds());
} }
@Override @Override
@ -322,16 +321,11 @@ public class DatabaseImpl implements Database {
@Override @Override
public Table getSingleRow(String statement, Map<String, Object> params) { 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 @Override
public Table getUnlimitedRows(String statement, Map<String, Object> params) { public Table getTable(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) { int limit, int fetchSize, int timeoutSeconds) {
SqlSelect sql = toSelect(statement).fetchSize(fetchSize).withTimeoutSeconds(timeoutSeconds); SqlSelect sql = toSelect(statement).fetchSize(fetchSize).withTimeoutSeconds(timeoutSeconds);
selectParams(sql, params); selectParams(sql, params);
@ -356,6 +350,32 @@ public class DatabaseImpl implements Database {
return table; 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 @Override
public void insert(String statement, Map<String, Object> params) { public void insert(String statement, Map<String, Object> params) {
SqlInsert sql = toInsert(statement); SqlInsert sql = toInsert(statement);
@ -494,34 +514,15 @@ public class DatabaseImpl implements Database {
for (int i = 0; i < classNames.size(); i++) { for (int i = 0; i < classNames.size(); i++) {
String className = classNames.get(i).toString(); String className = classNames.get(i).toString();
switch (className) { switch (className) {
case "java.lang.String": case "java.lang.String" -> row.add(rows.getStringOrEmpty(i + 1));
row.add(rows.getStringOrEmpty(i + 1)); case "java.lang.Integer" -> row.add(rows.getIntegerOrNull(i + 1));
break; case "java.lang.Long" -> row.add(rows.getLongOrNull(i + 1));
case "java.lang.Integer": case "java.lang.Boolean" -> row.add(rows.getBooleanOrFalse(i + 1));
row.add(rows.getIntegerOrNull(i + 1)); case "java.sql.Clob", "oracle.jdbc.OracleClob" -> row.add(rows.getClobStringOrEmpty(i + 1));
break; case "java.sql.Date" -> row.add(rows.getLocalDateOrNull(i + 1));
case "java.lang.Long": case "java.sql.Timestamp", "oracle.sql.TIMESTAMP" -> row.add(rows.getLocalDateTimeOrNull(i + 1));
row.add(rows.getLongOrNull(i + 1)); case "java.math.BigDecimal" -> row.add(rows.getBigDecimalOrNull(i + 1));
break; default -> throw new DatabaseException("unexpected column class name: " + className);
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);
} }
} }
return row; return row;