Skip to content

Commit

Permalink
Nullability of FetcherListener and ResultSetListener
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Sep 21, 2024
1 parent 2a417bb commit 000a744
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/main/org/firebirdsql/jdbc/FBObjectListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.firebirdsql.gds.ng.fields.RowValue;
import org.firebirdsql.util.InternalApi;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -37,6 +39,7 @@ private FBObjectListener() {
// no instances
}

@NullMarked
public interface FetcherListener {

/**
Expand All @@ -50,7 +53,7 @@ public interface FetcherListener {
* @param newRow
* new row
*/
default void rowChanged(FBFetcher fetcher, RowValue newRow) throws SQLException {
default void rowChanged(FBFetcher fetcher, @Nullable RowValue newRow) throws SQLException {
// do nothing
}

Expand All @@ -59,6 +62,7 @@ default void rowChanged(FBFetcher fetcher, RowValue newRow) throws SQLException
/**
* Listener for the events generated by the result set.
*/
@NullMarked
public interface ResultSetListener {

/**
Expand Down
11 changes: 7 additions & 4 deletions src/main/org/firebirdsql/jdbc/FBUpdatableFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.firebirdsql.jdbc;

import org.firebirdsql.gds.ng.fields.RowValue;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.sql.SQLException;
import java.sql.SQLNonTransientException;
Expand All @@ -44,13 +46,14 @@
*
* @since 5
*/
@NullMarked
final class FBUpdatableFetcher implements FBFetcher {

private final FBFetcher fetcher;
private final RowValue deletedRowMarker;

private FBObjectListener.FetcherListener fetcherListener;
private Map<Integer, RowValue> modifiedRows = new HashMap<>();
private Map<Integer, @Nullable RowValue> modifiedRows = new HashMap<>();

private int position;
private List<RowValue> insertedRows = new ArrayList<>();
Expand Down Expand Up @@ -100,7 +103,7 @@ private boolean notifyInsertedRow(int position) throws SQLException {
return notifyRow(position, rowValue);
}

private boolean notifyRow(int position, RowValue originalRowValue) throws SQLException {
private boolean notifyRow(int position, @Nullable RowValue originalRowValue) throws SQLException {
RowValue rowValue = modifiedRows.getOrDefault(position, originalRowValue);
fetcherListener.rowChanged(fetcher, rowValue);
return rowValue != null;
Expand Down Expand Up @@ -356,10 +359,10 @@ public boolean rowDeleted() throws SQLException {

private static final class InternalFetcherListener implements FBObjectListener.FetcherListener {

RowValue lastReceivedRow;
@Nullable RowValue lastReceivedRow;

@Override
public void rowChanged(FBFetcher fetcher, RowValue newRow) {
public void rowChanged(FBFetcher fetcher, @Nullable RowValue newRow) {
lastReceivedRow = newRow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ execute block (records INTEGER = ?)
}

private int extractId(RowValue rowValue) {
return db.getDatatypeCoder().decodeInt(rowValue.getFieldData(0));
return rowValue != null ? db.getDatatypeCoder().decodeInt(rowValue.getFieldData(0)) : Integer.MIN_VALUE;
}

}
9 changes: 6 additions & 3 deletions src/test/org/firebirdsql/jdbc/SimpleFetcherListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@
package org.firebirdsql.jdbc;

import org.firebirdsql.gds.ng.fields.RowValue;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static org.junit.jupiter.api.Assertions.assertTrue;

@NullMarked
final class SimpleFetcherListener implements FBObjectListener.FetcherListener {

final List<RowValue> receivedRows = new ArrayList<>();
final List<@Nullable RowValue> receivedRows = new ArrayList<>();

@Override
public void rowChanged(FBFetcher fetcher, RowValue newRow) {
public void rowChanged(FBFetcher fetcher, @Nullable RowValue newRow) {
receivedRows.add(newRow);
}

void assertRow(int rowIndex, Consumer<RowValue> assertion) {
void assertRow(int rowIndex, Consumer<@Nullable RowValue> assertion) {
assertTrue(0 <= rowIndex && rowIndex < receivedRows.size(), () -> "row index out of range: " + rowIndex);
assertion.accept(receivedRows.get(rowIndex));
}
Expand Down

0 comments on commit 000a744

Please sign in to comment.