Skip to content

Commit

Permalink
[hibernate#1915] Fix NPE when batching inserts
Browse files Browse the repository at this point in the history
The NullPointerException because the method that converts the
generated id from the database to a ResultSet was not implemented.

We forgot about it because there was no test for identity generation
with batching.
  • Loading branch information
DavideD committed May 22, 2024
1 parent 511703c commit 226ca62
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.vertx.sqlclient.PropertyKind;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.desc.ColumnDescriptor;
import io.vertx.sqlclient.impl.RowBase;

Expand All @@ -56,13 +57,71 @@
*/
public class ResultSetAdaptor implements ResultSet {

private final Iterator<Row> iterator;
private final Iterator<? extends Row> iterator;

private final List<ColumnDescriptor> columnDescriptors;
private final List<String> columnNames;
private Row row;
private boolean wasNull;

public ResultSetAdaptor(Object id, Class<?> idClass, String columnName) {
requireNonNull( id );
this.iterator = List.of( new RowAdaptor( id, idClass, columnName ) ).iterator();
this.columnNames = columnName == null ? emptyList() : List.of( columnName );
this.columnDescriptors = List.of( toColumnDescriptor( idClass, columnName ) );
}

private static class RowAdaptor implements Row {
private final Object id;
private final Class<?> idClass;
private final String columnName;

public RowAdaptor(Object id, Class<?> idClass, String columnName) {
this.id = id;
this.idClass = idClass;
this.columnName = columnName;
}

@Override
public Object getValue(String column) {
return id;
}

@Override
public String getColumnName(int pos) {
return columnName;
}

@Override
public int getColumnIndex(String column) {
return 0;
}

@Override
public Object getValue(int pos) {
return id;
}

@Override
public Tuple addValue(Object value) {
return null;
}

@Override
public int size() {
return 1;
}

@Override
public void clear() {
}

@Override
public List<Class<?>> types() {
return List.of( idClass );
}
}

public ResultSetAdaptor(RowSet<Row> rows) {
requireNonNull( rows );
this.iterator = rows.iterator();
Expand All @@ -83,7 +142,11 @@ private ResultSetAdaptor(RowSet<Row> rows, Row row, String idColumnName, Class<?
requireNonNull( idColumnName );
this.iterator = List.of( row ).iterator();
this.columnNames = List.of( idColumnName );
ColumnDescriptor columnDescriptor = new ColumnDescriptor() {
this.columnDescriptors = List.of( toColumnDescriptor( idClass, idColumnName ) );
}

private static ColumnDescriptor toColumnDescriptor(Class<?> idClass, String idColumnName) {
return new ColumnDescriptor() {
@Override
public String name() {
return idColumnName;
Expand All @@ -104,7 +167,6 @@ public JDBCType jdbcType() {
return null;
}
};
this.columnDescriptors = List.of( columnDescriptor );
}

private static class RowFromId extends RowBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.concurrent.CompletionStage;


import org.hibernate.reactive.adaptor.impl.ResultSetAdaptor;

import io.vertx.sqlclient.spi.DatabaseMetadata;

import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
Expand Down Expand Up @@ -169,11 +171,7 @@ public CompletionStage<ResultSet> insertAndSelectIdentifierAsResultSet(
Class<?> idClass,
String idColumnName) {
return insertAndSelectIdentifier( sql, paramValues, idClass, idColumnName )
.thenApply( this::convertToResultSet );
}

private ResultSet convertToResultSet(Object o) {
return null;
.thenApply( id -> new ResultSetAdaptor( id, idClass, idColumnName ) );
}

public CompletionStage<ReactiveConnection.Result> select(String sql) {
Expand Down

0 comments on commit 226ca62

Please sign in to comment.