gnodet-bot commented on code in PR #30319:
URL: https://github.com/apache/camel/pull/30319#discussion_r4610285623


##########
components/camel-cassandraql/src/main/docs/cql-component.adoc:
##########
@@ -57,13 +57,21 @@ your own Cluster instance and give it to the Camel endpoint.
 
 ==== Incoming Message
 
-The Camel Cassandra endpoint expects a bunch of simple objects (`Object`
-or `Object[]` or `Collection<Object>`) which will be bound to the CQL
-statement as query parameters. If the message body is null, an empty array or
-an empty collection, then the CQL query will be executed without binding 
parameters.
-Any other body is bound as a single parameter, so a body that is left from an 
earlier
+The message body holds the values of the bind markers of the CQL statement:

Review Comment:
   💡 The corresponding catalog copy 
(`catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cql-component.adoc`)
 is not updated in this PR. CI runs a regen build and then checks `git status 
--porcelain` — it will fail if the catalog copy is stale. Please run:
   ```
   mvn clean install -Dquickly -pl catalog/camel-catalog
   ```



##########
components/camel-cassandraql/src/main/java/org/apache/camel/component/cassandra/CassandraProducer.java:
##########
@@ -154,6 +163,10 @@ private ResultSet executeStatement(CqlSession session, 
Object messageCql, Object
         }
         if (statement != null) {
             resultSet = session.execute(statement);

Review Comment:
   ⚠️ **Silent parameter drop**: when `messageCql` is a `SimpleStatement` (i.e. 
the `CamelCqlQuery` header holds a `SimpleStatement` object), `statement` is 
non-null and this branch runs — `session.execute(statement)` ignores 
`namedParams`. The `else if (namedParams != null)` branch added below is 
unreachable in that case.
   
   Consider handling the named-params case inside this `if` block when 
`statement` is a `SimpleStatement`, or document that `Map` bodies are not 
supported when the header is already a `SimpleStatement`.



##########
components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/integration/CassandraComponentProducerNamedIT.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.cassandra.integration;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.datastax.oss.driver.api.core.cql.Row;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cassandra.CassandraConstants;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A Map body binds the named markers (such as :login) of the CQL statement by 
name.
+ */
+public class CassandraComponentProducerNamedIT extends BaseCassandra {
+
+    static final String CQL = "insert into camel_user(login, first_name, 
last_name) values (:login, :firstName, :lastName)";
+
+    @Produce("direct:prepared")
+    ProducerTemplate prepared;
+
+    @Produce("direct:unprepared")
+    ProducerTemplate unprepared;
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:prepared").toF("cql://%s/%s?cql=%s", getUrl(), 
KEYSPACE_NAME, CQL);
+                from("direct:unprepared")
+                        .toF("cql://%s/%s?cql=%s&prepareStatements=false", 
getUrl(), KEYSPACE_NAME, CQL);
+            }
+        };
+    }
+
+    private static Map<String, Object> user(String login, String firstName, 
String lastName) {
+        // not in the order of the markers, and with a key the statement does 
not use
+        Map<String, Object> body = new LinkedHashMap<>();
+        body.put("lastName", lastName);
+        body.put("city", "Rome");
+        body.put("firstName", firstName);
+        body.put("login", login);
+        return body;
+    }
+
+    private Row findUser(String login) {
+        return getSession().execute("select login, first_name, last_name from 
camel_user where login = ?", login).one();
+    }
+
+    @Test
+    public void testPreparedBindsByName() {
+        prepared.requestBody(user("w_jiang", "Willem", "Jiang"));
+
+        Row row = findUser("w_jiang");
+        assertNotNull(row);
+        assertEquals("Willem", row.getString("first_name"));
+        assertEquals("Jiang", row.getString("last_name"));
+    }
+
+    @Test
+    public void testUnpreparedBindsByName() {
+        unprepared.requestBody(user("j_strachan", "James", "Strachan"));
+
+        Row row = findUser("j_strachan");
+        assertNotNull(row);
+        assertEquals("James", row.getString("first_name"));
+        assertEquals("Strachan", row.getString("last_name"));
+    }
+
+    @Test
+    public void testPreparedMessageCqlBindsByName() {

Review Comment:
   🔹 **Nit:** Use `assertNull(findUser("g_nodet"))` instead of 
`assertEquals(null, findUser("g_nodet"))` for clarity, matching JUnit 
conventions used throughout this module.
   
   ```suggestion
           assertNull(findUser("g_nodet"));
   ```



##########
components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/integration/CassandraComponentProducerNamedIT.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.cassandra.integration;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.datastax.oss.driver.api.core.cql.Row;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cassandra.CassandraConstants;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A Map body binds the named markers (such as :login) of the CQL statement by 
name.
+ */
+public class CassandraComponentProducerNamedIT extends BaseCassandra {
+
+    static final String CQL = "insert into camel_user(login, first_name, 
last_name) values (:login, :firstName, :lastName)";
+
+    @Produce("direct:prepared")
+    ProducerTemplate prepared;
+
+    @Produce("direct:unprepared")
+    ProducerTemplate unprepared;
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:prepared").toF("cql://%s/%s?cql=%s", getUrl(), 
KEYSPACE_NAME, CQL);
+                from("direct:unprepared")
+                        .toF("cql://%s/%s?cql=%s&prepareStatements=false", 
getUrl(), KEYSPACE_NAME, CQL);
+            }
+        };
+    }
+
+    private static Map<String, Object> user(String login, String firstName, 
String lastName) {
+        // not in the order of the markers, and with a key the statement does 
not use
+        Map<String, Object> body = new LinkedHashMap<>();
+        body.put("lastName", lastName);
+        body.put("city", "Rome");
+        body.put("firstName", firstName);
+        body.put("login", login);
+        return body;
+    }
+
+    private Row findUser(String login) {
+        return getSession().execute("select login, first_name, last_name from 
camel_user where login = ?", login).one();
+    }
+
+    @Test
+    public void testPreparedBindsByName() {
+        prepared.requestBody(user("w_jiang", "Willem", "Jiang"));
+
+        Row row = findUser("w_jiang");
+        assertNotNull(row);
+        assertEquals("Willem", row.getString("first_name"));
+        assertEquals("Jiang", row.getString("last_name"));
+    }
+
+    @Test
+    public void testUnpreparedBindsByName() {
+        unprepared.requestBody(user("j_strachan", "James", "Strachan"));
+
+        Row row = findUser("j_strachan");
+        assertNotNull(row);
+        assertEquals("James", row.getString("first_name"));
+        assertEquals("Strachan", row.getString("last_name"));
+    }
+
+    @Test
+    public void testPreparedMessageCqlBindsByName() {
+        prepared.requestBodyAndHeader(Map.of("login", "c_ibsen", "first", 
"Claus 2", "last", "Ibsen 2"),
+                CassandraConstants.CQL_QUERY, "update camel_user set 
first_name=:first, last_name=:last where login=:login");
+
+        Row row = findUser("c_ibsen");
+        assertNotNull(row);
+        assertEquals("Claus 2", row.getString("first_name"));
+        assertEquals("Ibsen 2", row.getString("last_name"));
+    }
+
+    @Test
+    public void testPreparedMissingValueNamesTheMarker() {
+        Map<String, Object> body = Map.of("login", "g_nodet", "firstName", 
"Guillaume");
+
+        CamelExecutionException e = 
assertThrows(CamelExecutionException.class, () -> prepared.requestBody(body));
+
+        IllegalArgumentException cause = 
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertTrue(cause.getMessage().contains(":lastname"), 
cause.getMessage());
+        assertEquals(null, findUser("g_nodet"));
+    }

Review Comment:
   Nit: use `assertNull(findUser("g_nodet"))` instead of `assertEquals(null, 
...)` — the idiomatic assertion produces a clearer failure message.
   
   ```suggestion
           assertNull(findUser("g_nodet"));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to