CAMEL-11319: sql-stored - Add support for function

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/239c7438
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/239c7438
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/239c7438

Branch: refs/heads/master
Commit: 239c7438a47ad8b93f75ba2de62e0580799aed69
Parents: d067f42
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed May 24 14:12:57 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed May 24 14:35:28 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/sql-stored-component.adoc     |  3 +-
 .../stored/CallableStatementWrapperFactory.java | 10 +++----
 .../component/sql/stored/SqlStoredEndpoint.java | 31 +++++++++++++++-----
 .../sql/stored/TemplateStoredProcedure.java     |  3 +-
 .../stored/CallableStatementWrapperTest.java    |  4 +--
 .../component/sql/stored/TemplateCacheTest.java |  2 +-
 6 files changed, 33 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/main/docs/sql-stored-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/docs/sql-stored-component.adoc 
b/components/camel-sql/src/main/docs/sql-stored-component.adoc
index b18c23d..453afd9 100644
--- a/components/camel-sql/src/main/docs/sql-stored-component.adoc
+++ b/components/camel-sql/src/main/docs/sql-stored-component.adoc
@@ -93,13 +93,14 @@ with the following path and query parameters:
 | **template** | *Required* Sets the StoredProcedure template to perform |  | 
String
 |=======================================================================
 
-#### Query Parameters (6 parameters):
+#### Query Parameters (7 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |=======================================================================
 | Name | Description | Default | Type
 | **batch** (producer) | Enables or disables batch mode | false | boolean
 | **dataSource** (producer) | Sets the DataSource to use to communicate with 
the database. |  | DataSource
+| **function** (producer) | Whether this call is for a function. | false | 
boolean
 | **noop** (producer) | If set will ignore the results of the template and use 
the existing IN message as the OUT message for the continuation of processing | 
false | boolean
 | **outputHeader** (producer) | Store the template result in a header instead 
of the message body. By default outputHeader == null and the template result is 
stored in the message body any existing content in the message body is 
discarded. If outputHeader is set the value is used as the name of the header 
to store the template result and the original message body is preserved. |  | 
String
 | **useMessageBodyForTemplate** (producer) | Whether to use the message body 
as the template and then headers for parameters. If this option is enabled then 
the template in the uri is not used. | false | boolean

http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/CallableStatementWrapperFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/CallableStatementWrapperFactory.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/CallableStatementWrapperFactory.java
index 541192d..9e98449 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/CallableStatementWrapperFactory.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/CallableStatementWrapperFactory.java
@@ -29,21 +29,19 @@ import org.springframework.jdbc.core.JdbcTemplate;
 public class CallableStatementWrapperFactory extends ServiceSupport {
 
     public static final int TEMPLATE_CACHE_DEFAULT_SIZE = 200;
-
     public static final int BATCH_TEMPLATE_CACHE_DEFAULT_SIZE = 200;
 
     final JdbcTemplate jdbcTemplate;
-
     final TemplateParser templateParser;
+    boolean function;
 
     private final LRUCache<String, TemplateStoredProcedure> templateCache = 
new LRUCache<>(TEMPLATE_CACHE_DEFAULT_SIZE);
-
     private final LRUCache<String, BatchCallableStatementCreatorFactory> 
batchTemplateCache = new LRUCache<>(BATCH_TEMPLATE_CACHE_DEFAULT_SIZE);
 
-    public CallableStatementWrapperFactory(JdbcTemplate jdbcTemplate, 
TemplateParser
-            templateParser) {
+    public CallableStatementWrapperFactory(JdbcTemplate jdbcTemplate, 
TemplateParser templateParser, boolean function) {
         this.jdbcTemplate = jdbcTemplate;
         this.templateParser = templateParser;
+        this.function = function;
     }
 
     public StatementWrapper create(String sql) throws SQLException {
@@ -68,7 +66,7 @@ public class CallableStatementWrapperFactory extends 
ServiceSupport {
             return templateStoredProcedure;
         }
 
-        templateStoredProcedure = new TemplateStoredProcedure(jdbcTemplate, 
templateParser.parseTemplate(sql));
+        templateStoredProcedure = new TemplateStoredProcedure(jdbcTemplate, 
templateParser.parseTemplate(sql), function);
 
         this.templateCache.put(sql, templateStoredProcedure);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
index e8fff4a..5204ae8 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
@@ -34,7 +34,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
 @UriEndpoint(firstVersion = "2.17.0", scheme = "sql-stored", title = "SQL 
Stored Procedure", syntax = "sql-stored:template", producerOnly = true, label = 
"database,sql")
 public class SqlStoredEndpoint extends DefaultPollingEndpoint {
 
-    private final CallableStatementWrapperFactory wrapperFactory;
+    private CallableStatementWrapperFactory wrapperFactory;
     private JdbcTemplate jdbcTemplate;
 
     @UriParam(description = "Sets the DataSource to use to communicate with 
the database.")
@@ -43,28 +43,28 @@ public class SqlStoredEndpoint extends 
DefaultPollingEndpoint {
     @UriPath(description = "Sets the StoredProcedure template to perform")
     @Metadata(required = "true")
     private String template;
-    @UriParam(label = "producer", description = "Enables or disables batch 
mode")
+    @UriParam(description = "Enables or disables batch mode")
     private boolean batch;
-    @UriParam(label = "producer", description = "Whether to use the message 
body as the template and then headers for parameters. If this option is enabled 
then the template in the uri is not used.")
+    @UriParam(description = "Whether to use the message body as the template 
and then headers for parameters. If this option is enabled then the template in 
the uri is not used.")
     private boolean useMessageBodyForTemplate;
-    @UriParam(label = "producer", description = "If set, will ignore the 
results of the template and use the existing IN message as the OUT message for 
the continuation of processing")
+    @UriParam(description = "If set, will ignore the results of the template 
and use the existing IN message as the OUT message for the continuation of 
processing")
     private boolean noop;
     @UriParam(description = "Store the template result in a header instead of 
the message body. By default, outputHeader == null and the template result is 
stored"
             + " in the message body, any existing content in the message body 
is discarded. If outputHeader is set, the value is used as the name of the 
header"
             + " to store the template result and the original message body is 
preserved.")
     private String outputHeader;
+    @UriParam(description = "Whether this call is for a function.")
+    private boolean function;
 
     public SqlStoredEndpoint(String uri, SqlStoredComponent component, 
JdbcTemplate jdbcTemplate) {
         super(uri, component);
         setJdbcTemplate(jdbcTemplate);
-        wrapperFactory = new CallableStatementWrapperFactory(jdbcTemplate, new 
TemplateParser());
     }
 
     public Producer createProducer() throws Exception {
         return new SqlStoredProducer(this);
     }
 
-
     @Override
     protected String createEndpointUri() {
         // Make sure it's properly encoded
@@ -72,10 +72,17 @@ public class SqlStoredEndpoint extends 
DefaultPollingEndpoint {
     }
 
     @Override
+    protected void doStart() throws Exception {
+        this.wrapperFactory = new 
CallableStatementWrapperFactory(jdbcTemplate, new TemplateParser(), 
isFunction());
+        super.doStart();
+    }
+
+    @Override
     protected void doStop() throws Exception {
         super.doStop();
-        this.wrapperFactory.shutdown();
-
+        if (this.wrapperFactory != null) {
+            this.wrapperFactory.shutdown();
+        }
     }
 
     public JdbcTemplate getJdbcTemplate() {
@@ -134,6 +141,14 @@ public class SqlStoredEndpoint extends 
DefaultPollingEndpoint {
         this.dataSource = dataSource;
     }
 
+    public boolean isFunction() {
+        return function;
+    }
+
+    public void setFunction(boolean function) {
+        this.function = function;
+    }
+
     @Override
     public boolean isSingleton() {
         return false;

http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/TemplateStoredProcedure.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/TemplateStoredProcedure.java
 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/TemplateStoredProcedure.java
index a71ec9b..c1bec3c 100644
--- 
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/TemplateStoredProcedure.java
+++ 
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/TemplateStoredProcedure.java
@@ -40,8 +40,9 @@ public class TemplateStoredProcedure extends StoredProcedure {
 
     private List<InputParameter> inputParameterList = new ArrayList<>();
 
-    public TemplateStoredProcedure(JdbcTemplate jdbcTemplate, Template 
template) {
+    public TemplateStoredProcedure(JdbcTemplate jdbcTemplate, Template 
template, boolean function) {
         this.template = template;
+        setFunction(function);
         setDataSource(jdbcTemplate.getDataSource());
 
         setSql(template.getProcedureName());

http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
index 6aa372c..0bec974 100644
--- 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
@@ -45,14 +45,13 @@ public class CallableStatementWrapperTest extends 
CamelTestSupport {
                 
.setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
         jdbcTemplate = new JdbcTemplate(db);
         templateParser = new TemplateParser();
-        this.factory = new CallableStatementWrapperFactory(jdbcTemplate, 
templateParser);
+        this.factory = new CallableStatementWrapperFactory(jdbcTemplate, 
templateParser, false);
         super.setUp();
     }
 
 
     @Test
     public void shouldExecuteStoredProcedure() throws Exception {
-
         CallableStatementWrapper wrapper = new 
CallableStatementWrapper("SUBNUMBERS"
                 + "(INTEGER ${header.v1},INTEGER ${header.v2},OUT INTEGER 
resultofsub)", factory);
 
@@ -75,7 +74,6 @@ public class CallableStatementWrapperTest extends 
CamelTestSupport {
     public void shouldExecuteNilacidProcedure() throws Exception {
         CallableStatementWrapper wrapper = new 
CallableStatementWrapper("NILADIC()", factory);
 
-
         wrapper.call(new WrapperExecuteCallback() {
             @Override
             public void execute(StatementWrapper statementWrapper) throws 
SQLException, DataAccessException {

http://git-wip-us.apache.org/repos/asf/camel/blob/239c7438/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
index 4aee75c..2c43b16 100644
--- 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
@@ -46,7 +46,7 @@ public class TemplateCacheTest extends CamelTestSupport {
     @Test
     public void shouldCacheTemplateFunctions() throws InterruptedException {
         JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
-        CallableStatementWrapperFactory fac = new 
CallableStatementWrapperFactory(jdbcTemplate, new TemplateParser());
+        CallableStatementWrapperFactory fac = new 
CallableStatementWrapperFactory(jdbcTemplate, new TemplateParser(), false);
 
         BatchCallableStatementCreatorFactory batchFactory1 = 
fac.getTemplateForBatch("FOO()");
         BatchCallableStatementCreatorFactory batchFactory2 = 
fac.getTemplateForBatch("FOO()");

Reply via email to