camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r883648 - in /camel/trunk/components/camel-jdbc/src: main/java/org/apache/camel/component/jdbc/ test/java/org/apache/camel/component/jdbc/
Date Tue, 24 Nov 2009 10:31:13 GMT
Author: davsclaus
Date: Tue Nov 24 10:31:10 2009
New Revision: 883648

URL: http://svn.apache.org/viewvc?rev=883648&view=rev
Log:
CAMEL-2220: Now poosible to set java.sql.Statement options from uri using statement. prefix.

Added:
    camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcStatementParametersTest.java
      - copied, changed from r883593, 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=883648&r1=883647&r2=883648&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 Nov 24 10:31:10 2009
@@ -23,6 +23,8 @@
 import org.apache.camel.Endpoint;
 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 $
@@ -48,8 +50,12 @@
             dataSource = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining,
DataSource.class);
         }
 
+        Map<String, Object> params = IntrospectionSupport.extractProperties(parameters,
"statement.");
+
         JdbcEndpoint jdbc = new JdbcEndpoint(uri, this, dataSource);
+        jdbc.setParameters(params);
         setProperties(jdbc, parameters);
+
         return jdbc;
     }
 

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=883648&r1=883647&r2=883648&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 Nov 24 10:31:10 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.jdbc;
 
+import java.util.Map;
 import javax.sql.DataSource;
 
 import org.apache.camel.Component;
@@ -30,6 +31,7 @@
 public class JdbcEndpoint extends DefaultEndpoint {
     private int readSize;
     private DataSource dataSource;
+    private Map<String, Object> parameters;
 
     public JdbcEndpoint() {
     }
@@ -48,7 +50,7 @@
     }
 
     public Producer createProducer() throws Exception {
-        return new JdbcProducer(this, dataSource, readSize);
+        return new JdbcProducer(this, dataSource, readSize, parameters);
     }
 
     public int getReadSize() {
@@ -67,6 +69,21 @@
         this.dataSource = dataSource;
     }
 
+    public Map<String, Object> getParameters() {
+        return parameters;
+    }
+
+    /**
+     * Optional parameters to the {@link java.sql.Statement}.
+     * <p/>
+     * For example to set maxRows, fetchSize etc.
+     * 
+     * @param parameters parameters which will be set using reflection
+     */
+    public void setParameters(Map<String, Object> parameters) {
+        this.parameters = parameters;
+    }
+
     @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=883648&r1=883647&r2=883648&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 Nov 24 10:31:10 2009
@@ -30,6 +30,7 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -40,11 +41,13 @@
     private static final transient Log LOG = LogFactory.getLog(JdbcProducer.class);
     private DataSource dataSource;
     private int readSize;
+    private Map<String, Object> parameters;
 
-    public JdbcProducer(JdbcEndpoint endpoint, DataSource dataSource, int readSize) throws
Exception {
+    public JdbcProducer(JdbcEndpoint endpoint, DataSource dataSource, int readSize, Map<String,
Object> parameters) throws Exception {
         super(endpoint);
         this.dataSource = dataSource;
         this.readSize = readSize;
+        this.parameters = parameters;
     }
 
     /**
@@ -58,6 +61,11 @@
         try {
             conn = dataSource.getConnection();
             stmt = conn.createStatement();
+
+            if (parameters != null && !parameters.isEmpty()) {
+                IntrospectionSupport.setProperties(stmt, parameters);
+            }
+            
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Executing JDBC statement: " + sql);
             }

Copied: camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcStatementParametersTest.java
(from r883593, 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/JdbcStatementParametersTest.java?p2=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcStatementParametersTest.java&p1=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java&r1=883593&r2=883648&rev=883648&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/JdbcStatementParametersTest.java
Tue Nov 24 10:31:10 2009
@@ -16,12 +16,9 @@
  */
 package org.apache.camel.component.jdbc;
 
-import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.List;
 import javax.sql.DataSource;
 
-import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
@@ -34,7 +31,7 @@
 /**
  * @version $Revision$
  */
-public class JdbcRouteTest extends CamelTestSupport {
+public class JdbcStatementParametersTest extends CamelTestSupport {
     protected DataSource ds;
     private String driverClass = "org.hsqldb.jdbcDriver";
     private String url = "jdbc:hsqldb:mem:camel_jdbc";
@@ -42,50 +39,41 @@
     private String password = "";
 
     @Test
-    public void testJdbcRoutes() throws Exception {
-        // START SNIPPET: invoke
-        // first we create our exchange using the endpoint
-        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");
-
-        // now we send the exchange to the endpoint, and receives the response from Camel
-        Exchange out = template.send(endpoint, exchange);
-
-        // assertions of the response
-        assertNotNull(out);
-        assertNotNull(out.getOut());
-        ArrayList<HashMap<String, Object>> data = out.getOut().getBody(ArrayList.class);
-        assertNotNull("out body could not be converted to an ArrayList - was: "
-            + out.getOut().getBody(), data);
-        assertEquals(2, data.size());
-        HashMap<String, Object> row = data.get(0);
-        assertEquals("cust1", row.get("ID"));
-        assertEquals("jstrachan", row.get("NAME"));
-        row = data.get(1);
-        assertEquals("cust2", row.get("ID"));
-        assertEquals("nsandhu", row.get("NAME"));
-        // END SNIPPET: invoke
+    public void testMax2Rows() throws Exception {
+        List rows = template.requestBody("direct:hello", "select * from customer order by
id", List.class);
+
+        assertEquals(2, rows.size());
+        assertEquals(2, context.getEndpoints().size());
+    }
+
+    @Test
+    public void testMax5Rows() throws Exception {
+        List rows = template.requestBody("jdbc:testdb?statement.maxRows=5&statement.fetchSize=50",
"select * from customer order by id", List.class);
+
+        assertEquals(3, rows.size());
+        assertEquals(3, context.getEndpoints().size());
+    }
+
+    @Test
+    public void testNoParameters() throws Exception {
+        List rows = template.requestBody("jdbc:testdb", "select * from customer order by
id", List.class);
+
+        assertEquals(3, rows.size());
+        assertEquals(3, context.getEndpoints().size());
     }
 
 
     protected JndiRegistry createRegistry() throws Exception {
-        // START SNIPPET: register
         JndiRegistry reg = super.createRegistry();
         reg.bind("testdb", ds);
         return reg;
-        // END SNIPPET: register
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
-            // START SNIPPET: route
-            // lets add simple route
             public void configure() throws Exception {
-                from("direct:hello").to("jdbc:testdb?readSize=100");
+                from("direct:hello").to("jdbc:testdb?statement.maxRows=2");
             }
-            // END SNIPPET: route
         };
     }
 
@@ -96,11 +84,10 @@
         ds = dataSource;
 
         JdbcTemplate jdbc = new JdbcTemplate(ds);
-        // START SNIPPET: setup
         jdbc.execute("create table customer (id varchar(15), name varchar(10))");
         jdbc.execute("insert into customer values('cust1','jstrachan')");
         jdbc.execute("insert into customer values('cust2','nsandhu')");
-        // END SNIPPET: setup
+        jdbc.execute("insert into customer values('cust3','willem')");
         super.setUp();
     }
 
@@ -111,4 +98,4 @@
         jdbc.execute("drop table customer");
     }
 
-}
+}
\ No newline at end of file



Mime
View raw message