diff --git a/plugin-jdbc-mysql/src/test/java/io/kestra/plugin/jdbc/mysql/MysqlTest.java b/plugin-jdbc-mysql/src/test/java/io/kestra/plugin/jdbc/mysql/MysqlTest.java index 6322513b..cf87b4f9 100644 --- a/plugin-jdbc-mysql/src/test/java/io/kestra/plugin/jdbc/mysql/MysqlTest.java +++ b/plugin-jdbc-mysql/src/test/java/io/kestra/plugin/jdbc/mysql/MysqlTest.java @@ -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; @@ -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()); diff --git a/plugin-jdbc-postgres/src/test/java/io/kestra/plugin/jdbc/postgresql/PgsqlTest.java b/plugin-jdbc-postgres/src/test/java/io/kestra/plugin/jdbc/postgresql/PgsqlTest.java index 9dec791c..e0e7ffa5 100644 --- a/plugin-jdbc-postgres/src/test/java/io/kestra/plugin/jdbc/postgresql/PgsqlTest.java +++ b/plugin-jdbc-postgres/src/test/java/io/kestra/plugin/jdbc/postgresql/PgsqlTest.java @@ -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(0L)); + } + @Test void selectAndFetchToFile() throws Exception { RunContext runContext = runContextFactory.of(ImmutableMap.of()); diff --git a/plugin-jdbc/src/main/java/io/kestra/plugin/jdbc/AbstractJdbcQuery.java b/plugin-jdbc/src/main/java/io/kestra/plugin/jdbc/AbstractJdbcQuery.java index fb1187ab..562cbec3 100644 --- a/plugin-jdbc/src/main/java/io/kestra/plugin/jdbc/AbstractJdbcQuery.java +++ b/plugin-jdbc/src/main/java/io/kestra/plugin/jdbc/AbstractJdbcQuery.java @@ -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 -> {