Reputation: 1
This is my first time experience with Mule & trying to define a flow with DB connector. I am defining a parameterized simple select query with 3 parameters but somehow not able to figure it out. Even I tried putting entire/partial query in Variable before passing the variable value to query but could not succeed. Any help is highly appreciated.
select * from employees where employee_country='UK' limit 1,10;
Here UK,1 & 10 are dynamic values which will come from UI as below, http://localhost:9090/employee?country=UK&start=1&limit=10
My try:
select * from employees where employee_country=#[message.inboundProperties.'http.query.params'.country] limit #[message.inboundProperties.'http.query.params'.start],#[message.inboundProperties.'http.query.params'.limit]
Thanx, -Swapnil
Upvotes: 0
Views: 4150
Reputation: 128
#[message.inboundProperties.'http.query.params']
Use groovy script to capture those variables and seperate them via set invocation properties ..
def inputRequest = message.getInvocationProperty('originalQueryParams');
message.setInvocationProperty('country',inputRequest.country);
return payload;
Now Country is you flow variable and pass it in the query .
select * from employees where employee_country = '#[flowVars.country]'
Hope this helps
Upvotes: 0
Reputation: 11606
You are trying to create a dynamic query. For a parameterised query, you should separate the parameter values with placeholders. Parameterized won't replace expressions.
Here is the difference between the two options:
<db:select config-ref="DBConfig" doc:name="Database">
<db:parameterized-query><![CDATA[select * from employees where employee_country= :country limit 1,10;]]></db:parameterized-query>
<db:in-param name="country" type="VARCHAR" value="#[message.inboundProperties.'http.query.params'.country]" />
</db:select>
Dynamic:
<db:select config-ref="DBConfig" doc:name="Database">
<db:dynamic-query><![CDATA[select * from employees where employee_country=#[message.inboundProperties.'http.query.params'.country] limit #[message.inboundProperties.'http.query.params'.start],#[message.inboundProperties.'http.query.params'.limit]]]></db:dynamic-query>
</db:select>
Here is the documentation explaining the difference:
https://docs.mulesoft.com/mule-runtime/3.7/database-connector#query-types
Parameterized is the recommended approach as the disadvantage of using dynamic query statements is security as it leaves the statement open for SQL injection.
Upvotes: 1