Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,15 @@ public FlightInfo getInfo(final String query) {
* @return the number of rows affected.
*/
public long executeUpdate(final String query) {
return sqlClient.executeUpdate(query, getOptions());
return updateCount(sqlClient.executeUpdate(query, getOptions()));
}

/**
* Gets the update count to report. A server that does not know how many rows an update changed,
* as for most DDL, answers -1, which Avatica takes for "the statement returned a result set".
*/
private static long updateCount(final long recordCount) {
return Math.max(recordCount, 0L);
}

@Override
Expand Down Expand Up @@ -487,7 +495,7 @@ public FlightInfo executeQuery() throws SQLException {

@Override
public long executeUpdate() {
return preparedStatement.executeUpdate(getOptions());
return updateCount(preparedStatement.executeUpdate(getOptions()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,14 @@ public void testUpdateQueryWithBatchedParameters() throws SQLException {
assertEquals(42, updated[0]);
}
}

@Test
public void testUpdateQueryWithUnknownCountReturnsZero() throws SQLException {
// A server may answer -1 when it does not know the count, as for DDL.
String query = "Fake DDL with unknown count";
PRODUCER.addUpdateQuery(query, /*updatedRows*/ -1);
try (final PreparedStatement stmt = connection.prepareStatement(query)) {
assertEquals(0, stmt.executeUpdate());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -319,4 +320,14 @@ public void testExecuteShouldRunUpdateQueryForLargeUpdateV2() throws SQLExceptio
assertThat(updateCountSmall, is((long) Integer.MAX_VALUE));
assertThat(statement.getResultSet(), is(nullValue()));
}

@Test
public void testUpdateQueryWithUnknownCountReturnsZero() throws SQLException {
// A server may answer -1 when it does not know the count, as for DDL.
String query = "Fake DDL with unknown count";
PRODUCER.addUpdateQuery(query, /*updatedRows*/ -1);
try (final Statement st = connection.createStatement()) {
assertEquals(0, st.executeUpdate(query));
}
}
}
Loading