Skip to content

Commit

Permalink
fix: return size 0 for fetchOne (FETCH_ONE) when there is no result
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle committed Dec 16, 2024
1 parent 3aef481 commit c89ce6e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.time.*;
import java.util.Map;

import static io.kestra.core.models.tasks.common.FetchType.FETCH_ONE;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -108,6 +109,24 @@ void select() throws Exception {
assertThat(runOutput.getRow().get("blob_type"), is(Hex.decodeHex("DEADBEEF".toCharArray())));
}

@Test
void selectQueryReturnNoValue_sizeShouldBeZero() throws Exception {
RunContext runContext = runContextFactory.of(Map.of());

Query task = Query.builder()
.url(getUrl())
.username(getUsername())
.password(getPassword())
.fetchType(FETCH_ONE)
.timeZoneId("Europe/Paris")
.sql("select * from mysql_types where concert_id='random'")
.build();

AbstractJdbcQuery.Output runOutput = task.run(runContext);
assertThat(runOutput.getRow(), nullValue());
assertThat(runOutput.getSize(), equalTo(0L));
}

@Test
void update() throws Exception {
RunContext runContext = runContextFactory.of(ImmutableMap.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,29 @@ void selectAndFetch() throws Exception {
checkRow(runOutput.getRows().get(1), 2);
}

@Test
void selectAndFetchOne_EmptyResult() throws Exception {
RunContext runContext = runContextFactory.of(Map.of());

Query task = Query.builder()
.url(TestUtils.url())
.username(TestUtils.username())
.password(TestUtils.password())
.ssl(TestUtils.ssl())
.sslMode(TestUtils.sslMode())
.sslRootCert(TestUtils.ca())
.sslCert(TestUtils.cert())
.sslKey(TestUtils.keyNoPass())
.fetchType(FETCH_ONE)
.timeZoneId("Europe/Paris")
.sql("select concert_id from pgsql_types where b='random'")
.build();

AbstractJdbcQuery.Output runOutput = task.run(runContext);
assertThat(runOutput.getRow(), nullValue());
assertThat(runOutput.getSize(), is(0));
}

@Test
void selectAndFetchToFile() throws Exception {
RunContext runContext = runContextFactory.of(ImmutableMap.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public AbstractJdbcBaseQuery.Output run(RunContext runContext) throws Exception
//Populate result fro result set
switch (this.getFetchType()) {
case FETCH_ONE -> {
size = 1L;
var result = fetchResult(rs, cellConverter, conn);
size = result == null ? 0L : 1L;
output
.row(fetchResult(rs, cellConverter, conn))
.row(result)
.size(size);
}
case STORE -> {
Expand Down

0 comments on commit c89ce6e

Please sign in to comment.