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 2024/04/19 13:33:29 UTC

(camel) branch sql-var created (now 3a98d33a59b)

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

davsclaus pushed a change to branch sql-var
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 3a98d33a59b CAMEL-20696: camel-sql - Add support for using variables with named query parameters

This branch includes the following new commits:

     new 3a98d33a59b CAMEL-20696: camel-sql - Add support for using variables with named query parameters

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-20696: camel-sql - Add support for using variables with named query parameters

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3a98d33a59b2966b6278fd7dc86475f0354fe13e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Apr 19 15:33:15 2024 +0200

    CAMEL-20696: camel-sql - Add support for using variables with named query parameters
---
 components/camel-sql/src/main/docs/sql-component.adoc       |  6 ++++--
 .../component/sql/DefaultSqlPrepareStatementStrategy.java   |  6 ++++++
 .../org/apache/camel/component/sql/SqlProducerInTest.java   |  4 +---
 ...lProducerInTest.java => SqlProducerInVariablesTest.java} | 13 ++++++-------
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/components/camel-sql/src/main/docs/sql-component.adoc b/components/camel-sql/src/main/docs/sql-component.adoc
index 81b408f34b3..d2016b0d357 100644
--- a/components/camel-sql/src/main/docs/sql-component.adoc
+++ b/components/camel-sql/src/main/docs/sql-component.adoc
@@ -65,6 +65,7 @@ When using named parameters, Camel will look up the names in the given precedenc
 1. from a xref:languages:simple-language.adoc[Simple] expressions
 2. from message body if its a `java.util.Map`
 3. from message headers
+4. from exchange variables
 
 If a named parameter cannot be resolved, then an exception is thrown.
 
@@ -225,8 +226,9 @@ In the given route below, we want to get all the projects from the
 `projects` table.
 Notice the SQL query has two named parameters, `:#lic` and
 `:#min`.
-Camel will then look up for these parameters from the message body or
-message headers.
+Camel will then look up for these parameters from the message body,
+message headers and exchange variables.
+
 Notice in the example above we set two headers with
 constant value for the named parameters:
 
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
index 143ad54e3b4..b555af13b8f 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
@@ -262,6 +262,7 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
     protected static Object lookupParameter(String nextParam, Exchange exchange, Object body) {
         Map<?, ?> bodyMap = safeMap(exchange.getContext().getTypeConverter().tryConvertTo(Map.class, body));
         Map<?, ?> headersMap = safeMap(exchange.getIn().getHeaders());
+        Map<?, ?> variablesMap = safeMap(exchange.getVariables());
 
         Object answer = null;
         if ((nextParam.startsWith("$simple{") || nextParam.startsWith("${")) && nextParam.endsWith("}")) {
@@ -271,6 +272,8 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
             answer = bodyMap.get(nextParam);
         } else if (headersMap.containsKey(nextParam)) {
             answer = headersMap.get(nextParam);
+        } else if (variablesMap.containsKey(nextParam)) {
+            answer = variablesMap.get(nextParam);
         }
 
         return answer;
@@ -279,6 +282,7 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
     protected static boolean hasParameter(String nextParam, Exchange exchange, Object body) {
         Map<?, ?> bodyMap = safeMap(exchange.getContext().getTypeConverter().tryConvertTo(Map.class, body));
         Map<?, ?> headersMap = safeMap(exchange.getIn().getHeaders());
+        Map<?, ?> variablesMap = safeMap(exchange.getVariables());
 
         if ((nextParam.startsWith("$simple{") || nextParam.startsWith("${")) && nextParam.endsWith("}")) {
             return true;
@@ -286,6 +290,8 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
             return true;
         } else if (headersMap.containsKey(nextParam)) {
             return true;
+        } else if (variablesMap.containsKey(nextParam)) {
+            return true;
         }
 
         return false;
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java
index 76a1c0910fe..c860cf36d02 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java
@@ -53,9 +53,7 @@ public class SqlProducerInTest extends CamelTestSupport {
         super.tearDown();
 
         if (db != null) {
-            if (db != null) {
-                db.shutdown();
-            }
+            db.shutdown();
         }
     }
 
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInVariablesTest.java
similarity index 90%
copy from components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java
copy to components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInVariablesTest.java
index 76a1c0910fe..19f128bb252 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInVariablesTest.java
@@ -32,7 +32,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
-public class SqlProducerInTest extends CamelTestSupport {
+public class SqlProducerInVariablesTest extends CamelTestSupport {
 
     EmbeddedDatabase db;
 
@@ -53,9 +53,7 @@ public class SqlProducerInTest extends CamelTestSupport {
         super.tearDown();
 
         if (db != null) {
-            if (db != null) {
-                db.shutdown();
-            }
+            db.shutdown();
         }
     }
 
@@ -64,7 +62,8 @@ public class SqlProducerInTest extends CamelTestSupport {
         MockEndpoint mock = getMockEndpoint("mock:query");
         mock.expectedMessageCount(1);
 
-        template.requestBodyAndHeader("direct:query", "Hi there!", "names", new String[] { "Camel", "AMQ" });
+        fluentTemplate.to("direct:query").withBody("Hi there!").withVariable("names", new String[] { "Camel", "AMQ" })
+                .request();
 
         MockEndpoint.assertIsSatisfied(context);
 
@@ -85,7 +84,7 @@ public class SqlProducerInTest extends CamelTestSupport {
         names.add("Camel");
         names.add("AMQ");
 
-        template.requestBodyAndHeader("direct:query", "Hi there!", "names", names);
+        fluentTemplate.to("direct:query").withBody("Hi there!").withVariable("names", names).request();
 
         MockEndpoint.assertIsSatisfied(context);
 
@@ -102,7 +101,7 @@ public class SqlProducerInTest extends CamelTestSupport {
         MockEndpoint mock = getMockEndpoint("mock:query");
         mock.expectedMessageCount(1);
 
-        template.requestBodyAndHeader("direct:query", "Hi there!", "names", "Camel,AMQ");
+        fluentTemplate.to("direct:query").withBody("Hi there!").withVariable("names", "Camel,AMQ").request();
 
         MockEndpoint.assertIsSatisfied(context);