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 2013/09/18 15:01:25 UTC
[2/2] git commit: CAMEL-6368: Added option outputType. Thanks to
Sachin Handiekar for the patch.
CAMEL-6368: Added option outputType. Thanks to Sachin Handiekar for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/60637028
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/60637028
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/60637028
Branch: refs/heads/camel-2.12.x
Commit: 606370284f0ada842e6fade3c7b6d6117bd2e0b1
Parents: bb6b291
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Sep 18 15:00:53 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Sep 18 15:01:09 2013 +0200
----------------------------------------------------------------------
.../camel/component/jdbc/JdbcEndpoint.java | 10 ++++
.../camel/component/jdbc/JdbcOutputType.java | 21 +++++++
.../camel/component/jdbc/JdbcProducer.java | 38 ++++++++++--
.../JdbcProducerOutputTypeSelectListTest.java | 61 ++++++++++++++++++++
4 files changed, 125 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/60637028/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
index 4a55093..9751aa2 100755
--- a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
+++ b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.jdbc;
import java.util.Map;
+
import javax.sql.DataSource;
import org.apache.camel.Component;
@@ -38,6 +39,7 @@ public class JdbcEndpoint extends DefaultEndpoint {
private JdbcPrepareStatementStrategy prepareStatementStrategy = new DefaultJdbcPrepareStatementStrategy();
private boolean allowNamedParameters = true;
private boolean useHeadersAsParameters;
+ private JdbcOutputType outputType = JdbcOutputType.SelectList;
public JdbcEndpoint() {
}
@@ -150,6 +152,14 @@ public class JdbcEndpoint extends DefaultEndpoint {
this.useHeadersAsParameters = useHeadersAsParameters;
}
+ public JdbcOutputType getOutputType() {
+ return outputType;
+ }
+
+ public void setOutputType(JdbcOutputType outputType) {
+ this.outputType = outputType;
+ }
+
@Override
protected String createEndpointUri() {
return "jdbc";
http://git-wip-us.apache.org/repos/asf/camel/blob/60637028/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcOutputType.java
----------------------------------------------------------------------
diff --git a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcOutputType.java b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcOutputType.java
new file mode 100644
index 0000000..183735f
--- /dev/null
+++ b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcOutputType.java
@@ -0,0 +1,21 @@
+/**
+ * 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.jdbc;
+
+public enum JdbcOutputType {
+ SelectOne, SelectList
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/60637028/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
index 758701f..6c35d83 100644
--- a/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
+++ b/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
@@ -20,6 +20,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
+import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
@@ -261,13 +262,19 @@ public class JdbcProducer extends DefaultProducer {
* Sets the result from the ResultSet to the Exchange as its OUT body.
*/
protected void setResultSet(Exchange exchange, ResultSet rs) throws SQLException {
- List<Map<String, Object>> data = extractResultSetData(rs);
+ JdbcOutputType outputType = getEndpoint().getOutputType();
- exchange.getOut().setHeader(JdbcConstants.JDBC_ROW_COUNT, data.size());
- if (!data.isEmpty()) {
- exchange.getOut().setHeader(JdbcConstants.JDBC_COLUMN_NAMES, data.get(0).keySet());
+ if (outputType == JdbcOutputType.SelectList) {
+ List<Map<String, Object>> data = extractResultSetData(rs);
+ exchange.getOut().setHeader(JdbcConstants.JDBC_ROW_COUNT, data.size());
+ if (!data.isEmpty()) {
+ exchange.getOut().setHeader(JdbcConstants.JDBC_COLUMN_NAMES, data.get(0).keySet());
+ }
+ exchange.getOut().setBody(data);
+ } else if (outputType == JdbcOutputType.SelectOne) {
+ Object obj = queryForObject(rs);
+ exchange.getOut().setBody(obj);
}
- exchange.getOut().setBody(data);
}
/**
@@ -316,4 +323,25 @@ public class JdbcProducer extends DefaultProducer {
return data;
}
+
+ @SuppressWarnings("unchecked")
+ protected Object queryForObject(ResultSet rs) throws SQLException {
+ Object result = null;
+ List<Map<String, Object>> data = extractResultSetData(rs);
+ if (data.size() > 1) {
+ throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
+ } else if (data.size() == 1) {
+ // Set content depend on number of column from query result
+ Map<String, Object> row = data.get(0);
+ if (row.size() == 1) {
+ result = row.values().iterator().next();
+ } else {
+ result = row;
+ }
+ }
+
+ // If data.size is zero, let result be null.
+ return result;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/60637028/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcProducerOutputTypeSelectListTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcProducerOutputTypeSelectListTest.java b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcProducerOutputTypeSelectListTest.java
new file mode 100644
index 0000000..90827bf
--- /dev/null
+++ b/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcProducerOutputTypeSelectListTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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.jdbc;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class JdbcProducerOutputTypeSelectListTest extends AbstractJdbcTestSupport {
+
+ @EndpointInject(uri = "mock:result")
+ private MockEndpoint mock;
+
+ @SuppressWarnings({"unchecked"})
+ @Test
+ public void testOutputTypeSelectList() throws Exception {
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", "select * from customer");
+
+ assertMockEndpointsSatisfied();
+
+ List<?> received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals(3, received.size());
+
+ Map<String, Object> row = assertIsInstanceOf(Map.class, received.get(0));
+ assertEquals("cust1", row.get("ID"));
+ assertEquals("jstrachan", row.get("NAME"));
+
+ row = assertIsInstanceOf(Map.class, received.get(1));
+ assertEquals("cust2", row.get("ID"));
+ assertEquals("nsandhu", row.get("NAME"));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:start").to("jdbc:testdb?outputType=SelectList").to("mock:result");
+ }
+ };
+ }
+}