SQL Component
The sql: component allows you to work with databases using JDBC queries. The difference between this component and JDBC component is that in case of SQL the query is a property of the endpoint and it uses message payload as parameters passed to the query.
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:
<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 can only be used to define producer endpoints. In other words, you cannot define an SQL endpoint in a from() statement. |
The SQL component uses the following endpoint URI notation:
sql:select * from table where id=# order by name[?options]
Notice that the standard ? symbol that denotes the parameters to an SQL query is substituted with the # symbol, because the ? symbol is used to specify options for the endpoint. The ? symbol replacement can be configured on endpoint basis.
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Option |
Type |
Default |
Description |
dataSourceRef |
String |
null |
Camel 1.5.1/2.0: Reference to a DataSource to look up in the registry. |
placeholder |
String |
# |
Camel 2.4: Specifies a character that will be replaced to ? in SQL query. Notice, that it is simple String.replaceAll() operation and no SQL parsing is involved (quoted strings will also change) |
template.<xxx> |
|
null |
Sets additional options on the Spring JdbcTemplate that is used behind the scenes to execute the queries. For instance, template.maxRows=10. For detailed documentation, see the JdbcTemplate javadoc documentation. |
Treatment of the message body
The SQL component tries to convert the message body to an object of java.util.Iterator type and then uses this iterator to fill the query parameters (where each query parameter is represented by a # symbol (or configured placeholder) in the endpoint URI). If the message body is not an array or collection, the conversion results in an iterator that iterates over only one object, which is the body itself.
For example, if the message body is an instance of java.util.List, the first item in the list is substituted into the first occurrence of # in the SQL query, the second item in the list is substituted into the second occurrence of #, and so on.
Result of the query
For select operations, the result is an instance of List<Map<String, Object>> type, as returned by the JdbcTemplate.queryForList() method. For update operations, the result is the number of updated rows, returned as an Integer.
Header values
When performing update operations, the SQL Component stores the update count in the following message headers:
Header |
Description |
SqlProducer.UPDATE_COUNT |
Camel 1.x: The number of rows updated for update operations, returned as an Integer object. |
CamelSqlUpdateCount |
Camel 2.0: The number of rows updated for update operations, returned as an Integer object. |
CamelSqlRowCount |
Camel 2.0: The number of rows returned for select operations, returned as an Integer object. |
Configuration in Camel 1.5.0 or lower
The SQL component must be configured before it can be used. In Spring, you can configure it as follows:
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="myDS"/>
</bean>
<bean id="myDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ds" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
Configuration in Camel 1.5.1 or higher
You can now set a reference to a DataSource in the URI directly:
sql:select * from table where id=# order by name?dataSourceRef=myDS
Sample
In the sample below we execute a query and retrieve the result as a List of rows, where each row is a Map<String, Object and the key is the column name.
First, we set up a table to use for our sample. As this is based on an unit test, we do it java code:
jdbcTemplate.execute("create table projects (id integer primary key,"
+ "project varchar(10), license varchar(5))");
jdbcTemplate.execute("insert into projects values (1, 'Camel', 'ASF')");
jdbcTemplate.execute("insert into projects values (2, 'AMQ', 'ASF')");
jdbcTemplate.execute("insert into projects values (3, 'Linux', 'XXX')");
Then we configure our route and our sql component. Notice that we use a direct endpoint in front of the sql endpoint. This allows us to send an exchange to the direct endpoint with the URI, direct:simple, which is much easier for the client to use than the long sql: URI. Note that the DataSource is looked up up in the registry, so we can use standard Spring XML to configure our DataSource.
from("direct:simple")
.to("sql:select * from projects where license = # order by id?dataSourceRef=jdbc/myDataSource")
.to("mock:result");
And then we fire the message into the direct endpoint that will route it to our sql component that queries the database.
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
template.sendBody("direct:simple", "XXX");
mock.assertIsSatisfied();
List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());
Map row = assertIsInstanceOf(Map.class, received.get(0));
assertEquals("Linux", row.get("PROJECT"));
We could configure the DataSource in Spring XML as follows:
<jee:jndi-lookup id="myDS" jndi-name="jdbc/myDataSource"/>
Using a JDBC based idempotent repository
In this section we will use the JDBC based idempotent repository.
First we need to setup a javax.sql.DataSource in the spring XML file:
Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
And finally we can create our JDBC idempotent repository in the spring XML file as well:
Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
See Also