This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.10.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push:
new a11062e28aa CAMEL-21829: camel-sql - Make it easier to use Map values
for non-named SQL statements
a11062e28aa is described below
commit a11062e28aab31977c9d23ea8d5de1d8aec31b28
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Mar 4 14:37:34 2025 +0100
CAMEL-21829: camel-sql - Make it easier to use Map values for non-named SQL
statements
---
.../sql/DefaultSqlPrepareStatementStrategy.java | 5 ++
.../SqlProducerInsertNonNamedParametersTest.java | 96 ++++++++++++++++++++++
2 files changed, 101 insertions(+)
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 59efa704be1..7f5527ccf5f 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
@@ -185,6 +185,11 @@ public class DefaultSqlPrepareStatementStrategy implements
SqlPrepareStatementSt
while (iterator != null && iterator.hasNext()) {
Object value = iterator.next();
+ if (value instanceof Map.Entry<?, ?> entry) {
+ // if we iterate a map then we want the value and not the key
+ value = entry.getValue();
+ }
+
// special for SQL IN where we need to set dynamic number of values
if (value instanceof CompositeIterator<?> it) {
while (it.hasNext()) {
diff --git
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInsertNonNamedParametersTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInsertNonNamedParametersTest.java
new file mode 100644
index 00000000000..e5badfcd9ae
--- /dev/null
+++
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInsertNonNamedParametersTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+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.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class SqlProducerInsertNonNamedParametersTest extends CamelTestSupport {
+
+ private EmbeddedDatabase db;
+
+ @Override
+
+ public void doPreSetup() throws Exception {
+ db = new EmbeddedDatabaseBuilder()
+ .setName(getClass().getSimpleName())
+ .setType(EmbeddedDatabaseType.H2)
+ .addScript("sql/createAndPopulateDatabase2.sql").build();
+
+ }
+
+ @Override
+ public void doPostTearDown() throws Exception {
+ if (db != null) {
+ db.shutdown();
+ }
+ }
+
+ @Test
+ public void testInsertNonNamedParameters() throws Exception {
+ Map<String, Object> map = new LinkedHashMap<>(); // must use linked to
ensure order
+ map.put("id", 4);
+ map.put("project", "Foo");
+ map.put("lic", "ASF");
+ map.put("description", null);
+
+ template.sendBody("direct:insert", map);
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Foo");
+
+ mock.assertIsSatisfied();
+
+ List<?> received = assertIsInstanceOf(List.class,
mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals(1, received.size());
+ Map<?, ?> row = assertIsInstanceOf(Map.class, received.get(0));
+ assertEquals("Foo", row.get("project"));
+ assertEquals("ASF", row.get("license"));
+ assertNull(row.get("description"));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ getContext().getComponent("sql",
SqlComponent.class).setDataSource(db);
+
+ from("direct:insert")
+ .to("sql:insert into projects (id, project, license,
description) values (#,#,#,#)");
+
+ from("direct:start")
+ .to("sql:select * from projects where project = #")
+ .to("mock:result");
+ }
+ };
+ }
+}