camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r898201 - in /camel/trunk/components/camel-jdbc/src: main/java/org/apache/camel/component/jdbc/ test/java/org/apache/camel/component/jdbc/
Date Tue, 12 Jan 2010 07:21:16 GMT
Author: davsclaus
Date: Tue Jan 12 07:21:16 2010
New Revision: 898201

URL: http://svn.apache.org/viewvc?rev=898201&view=rev
Log:
CAMEL-2352: Using JDBC 4 semantic for column label name by default. Added option to select
old JDBC 3.0 semantic. Can be used to workaround issues with JDBC drivers. This only applies
when using SQL SELECT with column aliases.

Added:
    camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
  (with props)
    camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasTest.java
      - copied, changed from r898184, camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
Modified:
    camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
    camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
    camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java

Modified: camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java?rev=898201&r1=898200&r2=898201&view=diff
==============================================================================
--- camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
(original)
+++ camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java
Tue Jan 12 07:21:16 2010
@@ -24,7 +24,6 @@
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.IntrospectionSupport;
-import org.apache.camel.util.URISupport;
 
 /**
  * @version $Revision:520964 $

Modified: camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java?rev=898201&r1=898200&r2=898201&view=diff
==============================================================================
--- camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
(original)
+++ camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcEndpoint.java
Tue Jan 12 07:21:16 2010
@@ -32,6 +32,7 @@
     private int readSize;
     private DataSource dataSource;
     private Map<String, Object> parameters;
+    private boolean useJDBC4ColumnNameAndLabelSemantics = true;
 
     public JdbcEndpoint() {
     }
@@ -84,6 +85,25 @@
         this.parameters = parameters;
     }
 
+    public boolean isUseJDBC4ColumnNameAndLabelSemantics() {
+        return useJDBC4ColumnNameAndLabelSemantics;
+    }
+
+    /**
+     * Sets whether to use JDBC 4 or JDBC 3.0 or older semantic when retrieving column name.
+     * <p/>
+     * JDBC 4.0 uses columnLabel to get the column name where as JDBC 3.0 uses both columnName
or columnLabel.
+     * Unfortunately JDBC drivers behave differently so you can use this option to work out
issues around your
+     * JDBC driver if you get problem using this component
+     * <p/>
+     * This option is default <tt>true</tt>.
+     *
+     * @param useJDBC4ColumnNameAndLabelSemantics  <tt>true</tt> to use JDBC
4.0 semantics, <tt>false</tt> to use JDBC 3.0.
+     */
+    public void setUseJDBC4ColumnNameAndLabelSemantics(boolean useJDBC4ColumnNameAndLabelSemantics)
{
+        this.useJDBC4ColumnNameAndLabelSemantics = useJDBC4ColumnNameAndLabelSemantics;
+    }
+
     @Override
     protected String createEndpointUri() {
         return "jdbc";

Modified: camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java?rev=898201&r1=898200&r2=898201&view=diff
==============================================================================
--- camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
(original)
+++ camel/trunk/components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java
Tue Jan 12 07:21:16 2010
@@ -25,7 +25,6 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-
 import javax.sql.DataSource;
 
 import org.apache.camel.Exchange;
@@ -50,6 +49,11 @@
         this.parameters = parameters;
     }
 
+    @Override
+    public JdbcEndpoint getEndpoint() {
+        return (JdbcEndpoint) super.getEndpoint();
+    }
+
     /**
      * Execute sql of exchange and set results on output
      */
@@ -103,6 +107,9 @@
     protected void setResultSet(Exchange exchange, ResultSet rs) throws SQLException {
         ResultSetMetaData meta = rs.getMetaData();
 
+        // should we use jdbc4 or jdbc3 semantics
+        boolean jdbc4 = getEndpoint().isUseJDBC4ColumnNameAndLabelSemantics();
+
         int count = meta.getColumnCount();
         List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
         int rowNumber = 0;
@@ -110,8 +117,21 @@
             Map<String, Object> row = new HashMap<String, Object>();
             for (int i = 0; i < count; i++) {
                 int columnNumber = i + 1;
-                String columnName = meta.getColumnName(columnNumber);
-                row.put(columnName, rs.getObject(columnName));
+                // use column label to get the name as it also handled SQL SELECT aliases
+                String columnName;
+                if (jdbc4) {
+                    // jdbc 4 should use label to get the name
+                    columnName = meta.getColumnLabel(columnNumber);
+                } else {
+                    // jdbc 3 uses the label or name to get the name
+                    try {
+                        columnName = meta.getColumnLabel(columnNumber);
+                    } catch (SQLException e) {
+                        columnName = meta.getColumnName(columnNumber);
+                    }
+                }
+                // use index based which should be faster
+                row.put(columnName, rs.getObject(columnNumber));
             }
             data.add(row);
             rowNumber++;

Added: camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java?rev=898201&view=auto
==============================================================================
--- camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
(added)
+++ camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
Tue Jan 12 07:21:16 2010
@@ -0,0 +1,34 @@
+/**
+ * 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 org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class JdbcRouteUsingSqlSelectAliasJdbc3Test extends JdbcRouteUsingSqlSelectAliasTest
{
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:hello").to("jdbc:testdb?readSize=100?useJDBC4ColumnNameAndLabelSemantics=false");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasJdbc3Test.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasTest.java
(from r898184, camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasTest.java?p2=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasTest.java&p1=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java&r1=898184&r2=898201&rev=898201&view=diff
==============================================================================
--- camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java
(original)
+++ camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteUsingSqlSelectAliasTest.java
Tue Jan 12 07:21:16 2010
@@ -34,7 +34,7 @@
 /**
  * @version $Revision$
  */
-public class JdbcRouteTest extends CamelTestSupport {
+public class JdbcRouteUsingSqlSelectAliasTest extends CamelTestSupport {
     protected DataSource ds;
     private String driverClass = "org.hsqldb.jdbcDriver";
     private String url = "jdbc:hsqldb:mem:camel_jdbc";
@@ -48,7 +48,7 @@
         Endpoint endpoint = context.getEndpoint("direct:hello");
         Exchange exchange = endpoint.createExchange();
         // then we set the SQL on the in body
-        exchange.getIn().setBody("select * from customer order by ID");
+        exchange.getIn().setBody("select id as identifier, name from customer order by ID");
 
         // now we send the exchange to the endpoint, and receives the response from Camel
         Exchange out = template.send(endpoint, exchange);
@@ -61,10 +61,10 @@
             + out.getOut().getBody(), data);
         assertEquals(2, data.size());
         HashMap<String, Object> row = data.get(0);
-        assertEquals("cust1", row.get("ID"));
+        assertEquals("cust1", row.get("IDENTIFIER"));
         assertEquals("jstrachan", row.get("NAME"));
         row = data.get(1);
-        assertEquals("cust2", row.get("ID"));
+        assertEquals("cust2", row.get("IDENTIFIER"));
         assertEquals("nsandhu", row.get("NAME"));
         // END SNIPPET: invoke
     }
@@ -111,4 +111,4 @@
         jdbc.execute("drop table customer");
     }
 
-}
+}
\ No newline at end of file



Mime
View raw message