camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SQL Component
Date Mon, 13 Jul 2009 09:04:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/SQL+Component">SQL
Component</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~fbolton">Fintan
Bolton</a>
    </h4>
     Minor copy edits and re-wrote the section, 'Rules of treating message body' (renamed
to 'Treatment of the message body')
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Minor copy edits and re-wrote the section, 'Rules of treating message body' (renamed
to 'Treatment of the message body')<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <h2><a name="SQLComponent-SQLComponent"></a>SQL Component</h2>

<p>The <b>sql:</b> component allows you to work with databases using JDBC
queries. The difference between this component and <a href="/confluence/display/CAMEL/JDBC"
title="JDBC">JDBC</a> 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.</p>

<p>This component uses <tt>spring-jdbc</tt> behind the scenes for the actual
SQL handling.</p>

<h3><a name="SQLComponent-URIformat"></a>URI format</h3>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><p>The SQL component
can only be used to define producer endpoints. In other words, you cannot define an SQL endpoint
in a <tt>from()</tt> statement.</p></td></tr></table></div>

<p>The SQL component uses the following endpoint URI notation:</p>

<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">sql:select
* from table where id=# order by name[?options]</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>Notice that the standard <tt>?</tt> symbol that denotes the parameters
to an SQL query is substituted with the <tt>#</tt> symbol, because the <tt>?</tt>
symbol is used to specify options for the endpoint.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="SQLComponent-Options"></a>Options</h3>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>dataSourceRef</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.5.1/2.0:</b> Reference to a <tt>DataSource</tt>
to look up in the registry. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>template.&lt;xxx&gt;</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Sets additional options on the Spring <tt>JdbcTemplate</tt>
that is used behind the scenes to execute the queries. For instance, <tt>template.maxRows=10</tt>.
For detailed documentation, see the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html"
rel="nofollow">JdbcTemplate javadoc</a> documentation. </td>
</tr>
</tbody></table>

<h3><a name="SQLComponent-Treatmentofthemessagebody"></a>Treatment of the
message body</h3>

<p>The SQL component tries to convert the message body to an object of <tt>java.util.Iterator</tt>
type and then uses this iterator to fill the query parameters (where each query parameter
is represented by a <tt>#</tt> symbol 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.</p>

<p>For example, if the message body is an instance of <tt>java.util.List</tt>,
the first item in the list is substituted into the first occurrence of <tt>#</tt>
in the SQL query, the second item in the list is substituted into the second occurrence of
<tt>#</tt>, and so on.</p>

<h3><a name="SQLComponent-Resultofthequery"></a>Result of the query</h3>

<p>For <tt>select</tt> operations, the result is an instance of <tt>List&lt;Map&lt;String,
Object&gt;&gt;</tt> type, as returned by the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList(java.lang.String,%20java.lang.Object%91%93)"
rel="nofollow">JdbcTemplate.queryForList()</a> method. For <tt>update</tt>
operations, the result is the number of updated rows, returned as an <tt>Integer</tt>.</p>

<h3><a name="SQLComponent-Headervalues"></a>Header values</h3>

<p>When performing <tt>update</tt> operations, the SQL Component stores
the update count in the following message headers:</p>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>SqlProducer.UPDATE_COUNT</tt> </td>
<td class='confluenceTd'> Camel 1.x: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlUpdateCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlRowCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows returned for <tt>select</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
</tbody></table>

<h3><a name="SQLComponent-ConfigurationinCamel1.5.0orlower"></a>Configuration
in Camel 1.5.0 or lower</h3>

<p>The SQL component must be configured before it can be used. In Spring, you can configure
it as follows:</p>

<div class="code">
<textarea name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;bean
id="sql" class="org.apache.camel.component.sql.SqlComponent"&gt;
    &lt;property name="dataSource" ref="myDS"/&gt;
&lt;/bean&gt;

&lt;bean id="myDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
    &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
    &lt;property name="url" value="jdbc:mysql://localhost:3306/ds" /&gt;
    &lt;property name="username" value="username" /&gt;
    &lt;property name="password" value="password" /&gt;
&lt;/bean&gt;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<h3><a name="SQLComponent-ConfigurationinCamel1.5.1orhigher"></a>Configuration
in Camel 1.5.1 or higher</h3>
<p>You can now set a reference to a <tt>DataSource</tt> in the URI directly:</p>

<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">sql:select
* from table where id=# order by name?dataSourceRef=myDS</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>



<h3><a name="SQLComponent-Sample"></a>Sample</h3>
<p>In the sample below we execute a query and retrieve the result as a <tt>List</tt>
of rows, where each row is a <tt>Map&lt;String, Object</tt> and the key is
the column name. </p>

<p>First, we set up a table to use for our sample. As this is based on an unit test,
we do it java code:</p>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">//
this is the database we create with some initial data for our unit test
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', 'GPL')");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>Then we configure our route and our <tt>sql</tt> component. Notice that
we use a <tt>direct</tt> endpoint in front of the <tt>sql</tt> endpoint.
This allows us to send an exchange to the <tt>direct</tt> endpoint with the URI,
<tt>direct:simple</tt>, which is much easier for the client to use than the long
<tt>sql:</tt> URI. Note that the <tt>DataSource</tt> is looked up
up in the registry, so we can use standard Spring XML to configure our <tt>DataSource</tt>.</p>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("direct:simple")
    .to("sql:select * from projects where license = # order by id?dataSourceRef=jdbc/myDataSource")
    .to("mock:result");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>And then we fire the message into the <tt>direct</tt> endpoint that will
route it to our <tt>sql</tt> component that queries the database.</p>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">MockEndpoint
mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);

// send the query to direct that will route it to the sql where we will execute the query
// and bind the parameters with the data from the body. The body only contains one value
// in this case (GPL) but if we should use multi values then the body will be iterated
// so we could supply a List&lt;String&gt; instead containing each binding value.
template.sendBody("direct:simple", "GPL");

mock.assertIsSatisfied();

// the result is a List
List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());

// and each row in the list is a Map
Map row = assertIsInstanceOf(Map.class, received.get(0));

// and we should be able the get the project from the map that should be Linux
assertEquals("Linux", row.get("PROJECT"));</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<p>We could configure the <tt>DataSource</tt> in Spring XML as follows:</p>
<div class="code">
<textarea name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;jee:jndi-lookup
id="myDS" jndi-name="jdbc/myDataSource"/&gt;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>


<h3><a name="SQLComponent-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/JDBC" title="JDBC">JDBC</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/SQL+Component">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=82932&revisedVersion=11&originalVersion=10">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/SQL+Component?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message