| = SQL Stored Procedure Component |
| :doctitle: SQL Stored Procedure |
| :shortname: sql-stored |
| :artifactid: camel-sql |
| :description: Perform SQL queries as a JDBC Stored Procedures using Spring JDBC. |
| :since: 2.17 |
| :supportlevel: Stable |
| :tabs-sync-option: |
| :component-header: Only producer is supported |
| //Manually maintained attributes |
| :camel-spring-boot-name: sql |
| |
| *Since Camel {since}* |
| |
| *{component-header}* |
| |
| The SQL Stored component allows you to work with databases using JDBC |
| Stored Procedure queries. This component is an extension to |
| the xref:sql-component.adoc[SQL Component] but specialized for calling |
| stored procedures. |
| |
| This component uses `spring-jdbc` behind the scenes for the actual SQL |
| handling. |
| |
| Maven users will need to add the following dependency to their `pom.xml` |
| for this component: |
| |
| [source,xml] |
| ---- |
| <dependency> |
| <groupId>org.apache.camel</groupId> |
| <artifactId>camel-sql</artifactId> |
| <version>x.x.x</version> |
| <!-- use the same version as your Camel core version --> |
| </dependency> |
| ---- |
| |
| == URI format |
| |
| The SQL component uses the following endpoint URI notation: |
| |
| ---- |
| sql-stored:template[?options] |
| ---- |
| |
| Where template is the stored procedure template, where you declare the |
| name of the stored procedure and the IN, INOUT, and OUT arguments. |
| |
| You can also refer to the template in an external file on the file system |
| or classpath such as: |
| |
| ---- |
| sql-stored:classpath:sql/myprocedure.sql[?options] |
| ---- |
| |
| Where `sql/myprocedure.sql` is a plain text file in the classpath with the |
| template, as show: |
| |
| [source,sql] |
| ---- |
| SUBNUMBERS( |
| INTEGER :#num, |
| INTEGER :#num2, |
| INOUT INTEGER :#num3 out1, |
| OUT INTEGER out2 |
| ) |
| ---- |
| |
| |
| |
| // component options: START |
| include::partial$component-configure-options.adoc[] |
| include::partial$component-endpoint-options.adoc[] |
| include::partial$component-endpoint-headers.adoc[] |
| // component options: END |
| |
| |
| == Usage |
| |
| === Declaring the stored procedure template |
| |
| The template is declared using a syntax that would be similar to a Java |
| method signature. The name of the stored procedure, and then the |
| arguments enclosed in parentheses. An example explains this well: |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:STOREDSAMPLE(INTEGER ${headers.num1},INTEGER ${headers.num2},INOUT INTEGER ${headers.num3} result1,OUT INTEGER result2)"/> |
| ---- |
| |
| The arguments are declared by a type and then a mapping to the Camel |
| message using simple expression. So, in this example, the first two |
| parameters are `IN` values of `INTEGER` type, mapped to the message |
| headers. The third parameter is `INOUT`, meaning it accepts an `INTEGER` |
| and then returns a different `INTEGER` result. The last parameter is |
| the `OUT` value, also an `INTEGER` type. |
| |
| In SQL terms, the stored procedure could be declared as: |
| |
| [source,sql] |
| ---- |
| CREATE PROCEDURE STOREDSAMPLE(VALUE1 INTEGER, VALUE2 INTEGER, INOUT RESULT1 INTEGER, OUT RESULT2 INTEGER) |
| ---- |
| |
| ==== IN Parameters |
| |
| IN parameters take four parts separated by a space: parameter name, SQL type (with scale), type name, and value source. |
| |
| Parameter name is optional and will be auto generated if not provided. It must be given between quotes('). |
| |
| SQL type is required and can be an integer (positive or negative) or reference to integer field in some class. |
| If SQL type contains a dot, then the component tries to resolve that class and read the given field. For example, |
| SQL type `com.Foo.INTEGER` is read from the field `INTEGER` of class `com.Foo`. If the type doesn't |
| contain comma then class to resolve the integer value will be `java.sql.Types`. |
| Type can be postfixed by scale for example `DECIMAL(10)` would mean `java.sql.Types.DECIMAL` with scale 10. |
| |
| Type name is optional and must be given between quotes('). |
| |
| Value source is required. Value source populates the parameter value from the Exchange. |
| It can be either a Simple expression or header location i.e. `:#<header name>`. For example, |
| the Simple expression `${header.val}` would mean that parameter value will be read from the header `val`. |
| Header location expression `:#val` would have identical effect. |
| |
| When using named parameters, Camel will look up the names in the given precedence: |
| |
| 1. from a xref:languages:simple-language.adoc[Simple] expressions |
| 2. from message body if it is a `java.util.Map` |
| 3. from message headers |
| 4. from exchange variables |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:MYFUNC('param1' org.example.Types.INTEGER(10) ${header.srcValue})"/> |
| ---- |
| |
| URI means that the stored procedure will be called with parameter name `param1`, |
| it's SQL type is read from field `INTEGER` of class `org.example.Types` and scale will be set to 10. |
| The input value for the parameter is passed from the header `srcValue`. |
| |
| [source,xml] |
| ---------------------------------------------------------------------------------------------------------- |
| <to uri="sql-stored:MYFUNC('param1' 100 'mytypename' ${header.srcValue})"/> |
| ---------------------------------------------------------------------------------------------------------- |
| URI is identical to previous on except SQL-type is 100 and type name is _mytypename_. |
| |
| Actual call will be done using org.springframework.jdbc.core.SqlParameter. |
| |
| ==== OUT Parameters |
| |
| OUT parameters work similarly IN parameters and contain three parts: SQL type(with scale), type name, and output parameter name. |
| |
| SQL type works the same as IN parameters. |
| |
| Type name is optional and also works the same as IN parameters. |
| |
| Output parameter name is used for the OUT parameter name, as well as the header name where the result will be stored. |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:MYFUNC(OUT org.example.Types.DECIMAL(10) outheader1)"/> |
| ---- |
| |
| URI means that the OUT parameter's name is `outheader1` and result will be but into header `outheader1`. |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:MYFUNC(OUT org.example.Types.NUMERIC(10) 'mytype' outheader1)"/> |
| ---- |
| |
| This is identical to previous one but type name will be `mytype`. |
| |
| Actual call will be done using `org.springframework.jdbc.core.SqlOutParameter`. |
| |
| ==== INOUT Parameters |
| |
| INOUT parameters are a combination of all of the above. They receive a value from the exchange, as well as store a |
| result as a message header. The only caveat is that the IN parameter's "name" is skipped. Instead, the OUT |
| parameter's _name_ defines both the SQL parameter name, and the result header name. |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:MYFUNC(INOUT DECIMAL(10) ${headers.inheader} outheader)"/> |
| ---- |
| |
| Actual call will be done using org.springframework.jdbc.core.SqlInOutParameter. |
| |
| ==== Query Timeout |
| |
| You can configure query timeout (via `template.queryTimeout`) on statements used for query processing as shown: |
| |
| [source,xml] |
| ---- |
| <to uri="sql-stored:MYFUNC(INOUT DECIMAL(10) ${headers.inheader} outheader)?template.queryTimeout=5000"/> |
| ---- |
| |
| This will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level. |
| |
| |
| === Camel SQL Starter |
| |
| A starter module is available to spring-boot users. When using the starter, |
| the `DataSource` can be directly configured using spring-boot properties. |
| |
| [source,properties] |
| ---- |
| # Example for a mysql datasource |
| spring.datasource.url=jdbc:mysql://localhost/test |
| spring.datasource.username=dbuser |
| spring.datasource.password=dbpass |
| spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
| ---- |
| |
| To use this feature, add the following dependencies to your spring boot pom.xml file: |
| |
| [source,xml] |
| ---- |
| <dependency> |
| <groupId>org.apache.camel.springboot</groupId> |
| <artifactId>camel-sql-starter</artifactId> |
| <version>${camel.version}</version> <!-- use the same version as your Camel core version --> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-jdbc</artifactId> |
| <version>${spring-boot-version}</version> |
| </dependency> |
| ---- |
| |
| You should also include the specific database driver, if needed. |
| |
| |
| |
| include::spring-boot:partial$starter.adoc[] |