[#5960] Improved API naming, added ResultSet column name info

This commit is contained in:
lukaseder 2018-04-07 15:34:04 +02:00
parent c39ff053ea
commit a0bf244038
3 changed files with 89 additions and 36 deletions

View File

@ -56,10 +56,10 @@ public interface DiagnosticsContext {
ResultSet resultSet();
/**
* The number of rows that were fetched from {@link #resultSet()}, or
* The number of rows that were consumed from {@link #resultSet()}, or
* <code>-1</code> if there was no result set.
*/
int resultSetFetchedRows();
int resultSetConsumedRows();
/**
* The number of rows that were actually available from
@ -73,12 +73,12 @@ public interface DiagnosticsContext {
* {@link ResultSet#close()} call), and scrolling back to the current row
* after scrolling to the end of {@link #resultSet()} is not possible (e.g.
* because the driver supports only {@link ResultSet#TYPE_FORWARD_ONLY}),
* then this will return the same value as {@link #resultSetFetchedRows()}.
* then this will return the same value as {@link #resultSetConsumedRows()}.
*/
int resultSetActualRows();
int resultSetFetchedRows();
/**
* The number of columns that were fetched from the {@link #resultSet()}, or
* The number of columns that were consumed from the {@link #resultSet()}, or
* <code>-1</code> if there was no result set.
* <p>
* If the result set is still being consumed (i.e. prior to the
@ -86,17 +86,34 @@ public interface DiagnosticsContext {
* columns that were retrieved from the {@link #resultSet()} set <em>thus
* far</em>.
*/
int resultSetFetchedColumns();
int resultSetConsumedColumnCount();
/**
* The number of columns that were actually available from
* {@link #resultSet()}, or <code>-1</code> if there was no result set.
*/
int resultSetActualColumns();
int resultSetFetchedColumnCount();
/**
* The number of columns that were consumed from the {@link #resultSet()}, or
* <code>-1</code> if there was no result set.
* <p>
* If the result set is still being consumed (i.e. prior to the
* {@link ResultSet#close()} call), then this will return the number of
* columns that were retrieved from the {@link #resultSet()} set <em>thus
* far</em>.
*/
List<String> resultSetConsumedColumnNames();
/**
* The number of columns that were actually available from
* {@link #resultSet()}, or <code>-1</code> if there was no result set.
*/
List<String> resultSetFetchedColumnNames();
/**
* There had been an unnecessary {@link ResultSet#wasNull()} call to check
* the a non-primitive type fetched previously was null, or the call was
* that a non-primitive type consumed previously was null, or the call was
* made more than once.
* <p>
* {@link #resultSetColumnIndex()} will return the relevant column index for
@ -108,7 +125,7 @@ public interface DiagnosticsContext {
/**
* There had been a missing {@link ResultSet#wasNull()} call on a previously
* fetched primitive type, which is reported to be
* consumed primitive type, which is reported to be
* {@link ResultSetMetaData#isNullable(int)}.
* <p>
* {@link #resultSetColumnIndex()} will return the relevant column index for

View File

@ -38,31 +38,37 @@
package org.jooq.impl;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.jooq.DiagnosticsContext;
import org.jooq.tools.JooqLogger;
/**
* @author Lukas Eder
*/
final class DefaultDiagnosticsContext implements DiagnosticsContext {
ResultSet resultSet;
boolean resultSetClosing;
int resultSetFetchedColumns;
int resultSetActualColumns;
int resultSetFetchedRows;
int resultSetActualRows;
final String actualStatement;
final String normalisedStatement;
final Set<String> duplicateStatements;
final List<String> repeatedStatements;
boolean resultSetUnnecessaryWasNullCall;
boolean resultSetMissingWasNullCall;
int resultSetColumnIndex;
private static final JooqLogger log = JooqLogger.getLogger(DefaultDiagnosticsContext.class);
ResultSet resultSet;
DiagnosticsResultSet resultSetWrapper;
boolean resultSetClosing;
int resultSetFetchedColumnCount;
int resultSetConsumedColumnCount;
int resultSetFetchedRows;
int resultSetConsumedRows;
final String actualStatement;
final String normalisedStatement;
final Set<String> duplicateStatements;
final List<String> repeatedStatements;
boolean resultSetUnnecessaryWasNullCall;
boolean resultSetMissingWasNullCall;
int resultSetColumnIndex;
DefaultDiagnosticsContext(String actualStatement) {
this(actualStatement, actualStatement, Collections.singleton(actualStatement), Collections.singletonList(actualStatement));
@ -81,36 +87,65 @@ final class DefaultDiagnosticsContext implements DiagnosticsContext {
}
@Override
public final int resultSetFetchedRows() {
return resultSet == null ? -1 : resultSetFetchedRows;
public final int resultSetConsumedRows() {
return resultSet == null ? -1 : resultSetConsumedRows;
}
@Override
public final int resultSetActualRows() {
public final int resultSetFetchedRows() {
if (resultSet == null)
return -1;
try {
if (resultSetClosing || resultSet.getType() != ResultSet.TYPE_FORWARD_ONLY) {
while (resultSet.next())
resultSetActualRows++;
resultSetFetchedRows++;
resultSet.absolute(resultSetFetchedRows);
resultSet.absolute(resultSetConsumedRows);
}
}
catch (SQLException ignore) {}
return resultSetActualRows;
return resultSetFetchedRows;
}
@Override
public final int resultSetFetchedColumns() {
return resultSet == null ? -1 : resultSetFetchedColumns;
public final int resultSetConsumedColumnCount() {
return resultSet == null ? -1 : resultSetConsumedColumnCount;
}
@Override
public final int resultSetActualColumns() {
return resultSet == null ? -1 : resultSetActualColumns;
public final int resultSetFetchedColumnCount() {
return resultSet == null ? -1 : resultSetFetchedColumnCount;
}
@Override
public final List<String> resultSetConsumedColumnNames() {
return resultSetColumnNames(false);
}
@Override
public final List<String> resultSetFetchedColumnNames() {
return resultSetColumnNames(true);
}
private final List<String> resultSetColumnNames(boolean fetched) {
List<String> result = new ArrayList<String>();
if (resultSet != null) {
try {
ResultSetMetaData meta = resultSet.getMetaData();
for (int i = 1; i <= meta.getColumnCount(); i++)
if (fetched || resultSetWrapper.read.get(i - 1))
result.add(meta.getColumnLabel(i));
}
catch (SQLException e) {
log.info(e);
}
}
return Collections.unmodifiableList(result);
}
@Override

View File

@ -632,10 +632,11 @@ final class DiagnosticsResultSet extends DefaultResultSet {
DefaultDiagnosticsContext ctx = new DefaultDiagnosticsContext(sql);
ctx.resultSet = super.getDelegate();
ctx.resultSetFetchedColumns = read.cardinality();
ctx.resultSetActualColumns = columns;
ctx.resultSetFetchedRows = current;
ctx.resultSetActualRows = current + 1;
ctx.resultSetWrapper = this;
ctx.resultSetConsumedColumnCount = read.cardinality();
ctx.resultSetFetchedColumnCount = columns;
ctx.resultSetConsumedRows = current;
ctx.resultSetFetchedRows = current + 1;
return ctx;
}