You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/04/14 18:03:05 UTC

[camel] branch master updated: CAMEL-16507: camel-sql - Fixed issue with using selectOne and no data as response. Thanks to Pascal Schumacher for unit test.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e3ba7f7  CAMEL-16507: camel-sql - Fixed issue with using selectOne and no data as response. Thanks to Pascal Schumacher for unit test.
e3ba7f7 is described below

commit e3ba7f7a8b88831f658c6b103a5e10eb1fc56fc9
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Apr 14 20:02:21 2021 +0200

    CAMEL-16507: camel-sql - Fixed issue with using selectOne and no data as response. Thanks to Pascal Schumacher for unit test.
---
 .../apache/camel/component/sql/SqlProducer.java    |  9 ++-
 .../component/sql/CamelSqlEmptyResultTest.java     | 81 ++++++++++++++++++++++
 2 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
index 0890cce..c34bac9 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlProducer.java
@@ -43,6 +43,8 @@ public class SqlProducer extends DefaultProducer {
 
     private static final Logger LOG = LoggerFactory.getLogger(SqlProducer.class);
 
+    private static final Object EMPTY_RESULT = new Object();
+
     private final String query;
     private String resolvedQuery;
     private final JdbcTemplate jdbcTemplate;
@@ -134,8 +136,8 @@ public class SqlProducer extends DefaultProducer {
             exchange.getOut().setBody(exchange.getIn().getBody());
         }
         if (getEndpoint().getOutputHeader() != null) {
-            exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
-        } else if (data != null && !getEndpoint().isNoop()) {
+            exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data == EMPTY_RESULT ? null : data);
+        } else if (data != null && data != EMPTY_RESULT && !getEndpoint().isNoop()) {
             exchange.getOut().setBody(data);
         }
     }
@@ -175,6 +177,9 @@ public class SqlProducer extends DefaultProducer {
                                 data = getEndpoint().queryForObject(rs);
                                 if (data != null) {
                                     rowCount = 1;
+                                } else {
+                                    // need to mark special when no data
+                                    data = EMPTY_RESULT;
                                 }
                             } else {
                                 throw new IllegalArgumentException("Invalid outputType=" + outputType);
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/CamelSqlEmptyResultTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/CamelSqlEmptyResultTest.java
new file mode 100644
index 0000000..58c242f
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/CamelSqlEmptyResultTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.sql;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+public class CamelSqlEmptyResultTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+    private JdbcTemplate jdbcTemplate;
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+                .setName(getClass().getSimpleName())
+                .setType(EmbeddedDatabaseType.DERBY)
+                .build();
+
+        jdbcTemplate = new JdbcTemplate(db);
+        jdbcTemplate.execute("CREATE TABLE Persons (PersonID int, LastName varchar(255))");
+
+        super.setUp();
+    }
+
+    @Override
+    @AfterEach
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        db.shutdown();
+    }
+
+    @Test
+    public void testSelectOne() throws Exception {
+        MockEndpoint out = getMockEndpoint("mock:out");
+        out.expectedMessageCount(1);
+
+        template.sendBody("seda:in", "");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                getContext().getComponent("sql", SqlComponent.class).setDataSource(db);
+
+                from("seda:in")
+                        .to("sql:select * from Persons where PersonID=1?outputType=selectOne")
+                        .setHeader("PersonID", simple("${body[PersonID]}"))
+                        .to("mock:out");
+            }
+        };
+    }
+
+}