Summary: Execute a SQL query against an external database and return all results at once.
URI Format
pfx-sql:select?sql=SELECT * FROM myTable&dataSource=myDb
Parameters
|
Parameter |
Type |
Default |
Required |
Description |
|---|---|---|---|---|
|
|
String |
|
No |
Name of the Spring DataSource bean |
|
|
String |
|
Yes |
SQL query to execute |
|
|
SQLDialect |
|
No |
SQL dialect |
|
|
String |
|
No |
Where to store results: |
|
|
String |
|
No |
Name of the header/property when |
Examples
Basic Select
<route id="selectFromDb">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:select?sql=SELECT * FROM products WHERE active = 1&dataSource=myDb&dialect=MYSQL"/>
<to uri="pfx-api:loaddata?objectType=P&mapper=productMapper"/>
</route>
Select to Header
<route id="selectToHeader">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:select?sql=SELECT code, name FROM regions&dataSource=myDb&outputTarget=header&outputTargetName=regionLookup"/>
<!-- regionLookup is now available as a message header -->
<to uri="pfx-api:fetch?objectType=P&filter=allProducts"/>
<to uri="pfx-api:loaddata?objectType=P&mapper=productWithRegion"/>
</route>
Select with PostgreSQL
<route id="selectPostgres">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:select?sql=SELECT * FROM inventory&dataSource=pgDb&dialect=POSTGRESQL"/>
<to uri="pfx-api:loaddata?objectType=P&mapper=inventoryMapper"/>
</route>
Common Pitfalls
|
Problem |
Cause |
Fix |
|---|---|---|
|
Out of memory on large tables |
|
Use |
|
Empty results |
SQL syntax error or wrong dialect |
Verify the SQL runs correctly in a database client; check |
|
Results overwrite body |
Default |
Use |
|
Special characters in SQL |
XML escaping needed |
Use |
See Also
-
pfx-sql Component — Parent reference
-
pfx-sql:selectIterator — Batched select for large datasets