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/08/26 12:14:36 UTC

(camel) branch sql-batch created (now e71365cc713)

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

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


      at e71365cc713 CAMEL-21112: camel-sql - SQL producer in batch mode using inlined simple expression should use the individual batch body when evaluating. Thanks to Alexander Zobkov for the analysis.

This branch includes the following new commits:

     new e71365cc713 CAMEL-21112: camel-sql - SQL producer in batch mode using inlined simple expression should use the individual batch body when evaluating. Thanks to Alexander Zobkov for the analysis.

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-21112: camel-sql - SQL producer in batch mode using inlined simple expression should use the individual batch body when evaluating. Thanks to Alexander Zobkov for the analysis.

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

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

commit e71365cc713855361ca6881a0e215e4c2c49c82f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 26 14:14:18 2024 +0200

    CAMEL-21112: camel-sql - SQL producer in batch mode using inlined simple expression should use the individual batch body when evaluating. Thanks to Alexander Zobkov for the analysis.
---
 .../sql/DefaultSqlPrepareStatementStrategy.java    | 16 +++-
 .../component/sql/ProducerBatchSimpleExpTest.java  | 89 ++++++++++++++++++++++
 2 files changed, 101 insertions(+), 4 deletions(-)

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 b555af13b8f..59efa704be1 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
@@ -29,7 +29,9 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
 import org.apache.camel.RuntimeExchangeException;
+import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.util.StringQuoteHelper;
 import org.slf4j.Logger;
@@ -69,7 +71,7 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
                 Matcher matcher = REPLACE_IN_PATTERN.matcher(query);
                 while (matcher.find()) {
                     String found = matcher.group(1);
-                    Object parameter = lookupParameter(found, exchange, exchange.getIn().getBody());
+                    Object parameter = lookupParameter(found, exchange, null);
                     if (parameter != null) {
                         Iterator<?> it = createInParameterIterator(parameter);
                         StringJoiner replaceBuilder = new StringJoiner(",");
@@ -259,15 +261,21 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
         }
     }
 
-    protected static Object lookupParameter(String nextParam, Exchange exchange, Object body) {
+    protected static Object lookupParameter(String nextParam, Exchange exchange, Object batchBody) {
+        Object body = batchBody != null ? batchBody : exchange.getMessage().getBody();
         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("}")) {
-            answer = exchange.getContext().resolveLanguage("simple").createExpression(nextParam).evaluate(exchange,
-                    Object.class);
+            if (batchBody != null) {
+                // in batch mode then need to work on a copy of the original exchange and set the batch body
+                exchange = ExchangeHelper.createCopy(exchange, true);
+                exchange.getMessage().setBody(batchBody);
+            }
+            Expression exp = exchange.getContext().resolveLanguage("simple").createExpression(nextParam);
+            answer = exp.evaluate(exchange, Object.class);
         } else if (bodyMap.containsKey(nextParam)) {
             answer = bodyMap.get(nextParam);
         } else if (headersMap.containsKey(nextParam)) {
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/ProducerBatchSimpleExpTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/ProducerBatchSimpleExpTest.java
new file mode 100644
index 00000000000..0735971fd3e
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/ProducerBatchSimpleExpTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+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.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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ProducerBatchSimpleExpTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+    private JdbcTemplate jdbcTemplate;
+
+    public record MyData(int id, String project, String license) {
+    }
+
+    @Override
+    public void doPreSetup() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+                .setName(getClass().getSimpleName())
+                .setType(EmbeddedDatabaseType.H2)
+                .addScript("sql/createAndPopulateDatabase.sql").build();
+
+        jdbcTemplate = new JdbcTemplate(db);
+    }
+
+    @Override
+    public void doPostTearDown() throws Exception {
+        if (db != null) {
+            db.shutdown();
+        }
+    }
+
+    @Test
+    public void shouldExecuteBatch() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:query");
+        mock.expectedMessageCount(1);
+
+        List data = new ArrayList();
+        data.add(new MyData(4, "Donald", "DIS"));
+        data.add(new MyData(5, "Goofy", "DIS"));
+        template.requestBody("direct:query", data);
+
+        MockEndpoint.assertIsSatisfied(context);
+
+        assertEquals("Donald", jdbcTemplate.queryForObject("select project from projects where id = 4", String.class));
+        assertEquals("Goofy", jdbcTemplate.queryForObject("select project from projects where id = 5", String.class));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                getContext().getComponent("sql", SqlComponent.class).setDataSource(db);
+
+                from("direct:query")
+                        .to("sql:insert into projects values " +
+                            "(:#$simple{body.id},:#$simple{body.project},:#$simple{body.license})" +
+                            "?batch=true")
+                        .to("mock:query");
+            }
+        };
+    }
+}