how to set input parameter in select query of anypoint - anypoint-studio

how to set input parameter in select query on anypoint-stutio
i'm newer on anypoint studio. and i'm tring to get some data from database . but i don't know how to set input parameter in anypoint.
may i know who can provide some guides or cookbook for this.
for example:
my query is "select * from ESPOSSG.xf_salesimport where xf_txdate=:xf_txdate "
i need to set input parameters in anypoint
the i can use "http://localhost:10256/sales/txdate='20190601'" to get data

Your Input parameters should be like Dataweave expression, instead you have used expression to print logger.
Try this Instead:
{
xf_txdate:message.inboundProperties.'http.query.params'.txdate
}

Related

How to define get method in Anypoint studio if queryParam is defined?

Im trying to make a get method in Anypoint studio. I have already defined raml file with get method that looks something like this:
/kupci:
get:
queryParameters:
active:
required: false
enum:
- "true"
- "false"
I want to make a get flow that returns data from mysql database with 2 options:
if i have defined queryParam return everything from the database based of that condition
if not defined, just return everything from database
You just need a simple if else condition wherever you are creating your select query, and add a where clause if active is non empty.
SELECT field1, field2, ... FROM table
++ if(!isEmpty(attributes.queryParams.active)) 'WHERE active = $(attributes.queryParams.active)'
else ''
Note: You might need to change the above query if the datatype of the column active is Boolean. For example:
WHERE active IS $(attributes.queryParams.active)
You need to wrap this around the #[] script tag when writing in db:select as mentioned in the mule database connector docs

select globalmap using tDBinput with Talend give the error: Invalid character constant

I have to remove the accents from the person's name, but I cannot apply the function in Talend while it works in SQL oracle.
this query works in my tDBInput component :
"SELECT '"+((String)globalMap.get("copyOfSORTIE.NOM"))+"' as nom_nom_compl,
'"+((String)globalMap.get("copyOfSORTIE.ENTETE"))+"' entete
FROM DUAL"
However, when I want to add the convert function, it doesn't work
this query does not work :
"SELECT '"+((String)globalMap.get(CONVERT("copyOfSORTIE.NOM",'US7ASCII')))+"' as nom_nom_compl,
'"+((String)globalMap.get("copyOfSORTIE.ENTETE"))+"' entete
FROM DUAL"
In my talend :
I am getting this error
What is the syntax for it to work?
Thank you!
Two things there :
I don't know the CONVERT method, but I can see that you are applying it to the key of your globalMap variable , and not the value (as if you wanted to convert "myKey" and not "myValue" which is attached to the key). Are you sure this is what you want to achieve ? if not, the syntax should be something similar to "SELECT CONVERT('"+((String)globalMap.get("copyOfSORTIE.NOM"))+"','US7ASCII') "
A useful java method implemented in talend is TalendString.removeAccents("") that you can apply directly on your talend variable, thus not using a SQL method.

Mule: how to send Array parameter to DB Update

I have a PG table with a field of type char(10)[].
I need to update a record in the table with values from a Mule flow.
So, i did something like this:
flowVars.test=['aaa', 'bbb',ccc'];
Then, I'm trying to submit an update statement like this:
update tab1 set fld1=#[flowVars.test]
it's failing with the error:
Cannot cast an instance of java.util.ArrayList to type Types.ARRAY
My understanding is that SQL array should be used in this scenario but I can't figure out how to get an instance of such an array in a flow and how to work with it in MEL.
Can someone please advise?
Thank you,
There are many sources that suggest to use the Connection#createArrayOf(). But I don't know how to use it in the Database connector.
However, for this purpose I will do this solution:
Convert the ArrayList to a String. It should be formed as: {value1, value2, ...}
Change the Database Query Type from Parameterized into Dynamic
Update the SQL Query become: update tab1 set fld1 = '#[flowVars.test]'. The additional single quote is required for this query type.
Finally, by using the following configuration I can update field of type character(10)[]:
<expression-transformer expression="#[flowVars.test = ['aaa', 'bbb', 'ccc'].toString().replace('[', '{').replace(']', '}')]" doc:name="Expression"/>
<db:update config-ref="Postgre_Database_Configuration" doc:name="Database">
<db:dynamic-query><![CDATA[update tab1 set fld1 = '#[flowVars.test]']]></db:dynamic-query>
</db:update>
Ok, I've found an answer in MuleSoft doc.
Starting from version 3.6 DB connector supports custom types and allows defining mapping between SQL arrays and structures and custom user classes.
It's documented here .

How to Extract the value of resultSet returned from JDBC response (Via MEL) Mule ESB

I have JDBC where I'm calling the stored Procedure, It is returning the response as below, But I'm pretty not sure how to extract the value of result set
Please find the response from DB
{updateCount1=4,resultSet1=[{XML_F5RYI-11YTR=<Customers><Customer1>John<Customer1><Customer2>Ganesh<Customer2><Customers>}],resultSet2[{SequenceNumber=94}],updateCount2=1, updateCount3=4}
I have used the this expression #[message.payload.get(0)], It has return the ResultSet as below, But not exactly value required. I need to take the xml value of XML_F5RYI-11YTR.
{XML_F5RYI-11YTR=<Customers><Customer1>John<Customer1><Customer2>Ganesh<Customer2><Customers>}
Also tried like below
#[message.payload.get(0).XML_F5RYI-11YTR] but getting error , not able to extract the xml.
Could you please suggest how can I extract the xml from the ResultSet1
In most cases, the way you did it should work. I think what is happening here is that the hyphen in the column name is interpreted by the MEL parser as a subtraction. So you could change yours to this syntax, and it should work:
#[message.payload.get(0)['XML_F5RYI-11YTR']]
Also you can omit "message", as payload is resolvable directly:
#[payload.get(0)['XML_F5RYI-11YTR']]
You could use array bracket syntax to access the first row in the result set, instead of the get method:
#[payload[0]['XML_F5RYI-11YTR']]
Finally, you might want to do something for each row returned from the database. If you use a collection-splitter or a for-each, your payload will be the map that represents the row, instead of a list of maps representing the whole result set:
<collection-splitter />
<logger message="#[payload['XML_F5RYI-11YTR']]" />
EDIT
To access the result set in the payload shown in the question, you would need to access it like so:
#[payload.resultSet1[0]['XML_F5RYI-11YTR']]
The database connector gives you a list of maps. The map keys will be the name of the columns. Therefore if you want to get updateCount1, you can use something like this:
#[payload.get('updateCount1')]"
Thump rule - you database connector gives you list of map, not sure what format does it is carry, if you want XML_F5RYI.. value then do the below
[message.payload.get(0)] convert it to json or map from which #[message.payload.get("XML_F5RYI-11YTR")]

How to concatenate 2 values in mule?

Can someone please let me know how to concatenate multiple values in mule?
Something like,
#[payload.getPayload()].concat(#[getSubject()])
I assume you are using Mule 3.3.x or above. If so you can use Mule Expression Language(MEL).
One example using MEL is:
#['Hello' + 'World']
Or MEL also allows you to use standard Java method invocation:
#[message.payload.concat(' Another String')]
Cheat sheet on MEL
MULE 4 Update
For Mule 4. Dataweave 2.0 is the main expression language:
Simple concat:
#['Hello' ++ ' World']
Other alternative is to use Mule Design plugin :
Drop an "Append String" operation as many times as you need.
This operation takes the message payload of the previous step and concats a specified string to it.
Not sure about performance details, but it will be surely more easy to maintain.
Append to String - MuleSoft
you can declare a string buffer using expression component
<expression-component doc:name="Expression"><![CDATA[StringBuffer sb = new
StringBuffer();
flowVars.stBuffer=sb;
]]></expression-component>
and then append use append on string buffer any where in the flow.
flowVars.stBuffer.append("string to append")
Once done use #[flowVars.stBuffer] to access the concatenated string
If you want to add two different values received through payload in the mule flow then we can use concat() method.
For example below we have received values through arraylist where i am adding two diffrent fields i.e. FirstName and the LastName -
concat(#[payload[0].'firstname']," " #[payload[0].'lastname']