SQL Multi Where Conditions using QUERY_STRING in WSO2 Dataservice - sql

I have SQL Select statement with optional filter that i would like to exposed as an API . I used the dynamic SQL Query to get the parameters to filter with like this :
<query id="selectprofile" useConfig="default">
<sql>SELECT BADLOGINS,EMAIL,FRAMED_ROUTE,FULLNAME FROM USERS :filterQuery </sql>
<result outputType="json">{
"accounts":
{ "account" :
[{
"BADLOGINS":"$BADLOGINS",
"EMAIL":"$EMAIL",
"FRAMED_ROUTE":"$FRAMED_ROUTE",
"FULLNAME":"$FULLNAME"
}]
}
}</result>
<param name="filterQuery" sqlType="QUERY_STRING"/>
</query>
but it didn't work for me and it give me an error
DS Code: INCOMPATIBLE_PARAMETERS_ERROR\nNested
Exception:-\njavax.xml.stream.XMLStreamException: DS Fault Message:
Error in 'Query.extractParams', cannot find query param with
name:filterQuery\nDS Code:
i invoked the API like this with get method :
localhost:8290/services/MYSQLDataService/selectprofile?FULLNAME="AHMED"
NB : I followed this documentation https://apim.docs.wso2.com/en/latest/reference/synapse-properties/data-services/sample-queries/#defining-a-dynamic-sql-query

The resource path should only contain the resource name if you need to use a query param. In your case, the resource element should be as follows,
<resource method="GET" path="selectprofile">
<call-query href="selectprofile">
<with-param name="filterQuery" query-param="filterQuery"/>
</call-query>
</resource>
According to the above example, when invoking the data service you will need to pass a query parameter named filterQuery. This parameter should contain the complete query you need to use(It seems you have missed the WHERE clause).
localhost:8290/services/MYSQLDataService/selectprofile?filterQuery=WHERE FULLNAME="AHMED"
For more information check Exposing Data as a REST Resource

Related

BigQuery cacheHit property

I'm using the BigQuery API to run a query with the following code:
query = (
'SELEC ...'
)
# API request - starts the query
query_job = client.query(
query,
location='US'
)
results = query_job.result()
The query works and outputs expected results.
However, I am not able to verify use of the cache.
Docs:
If you are using the BigQuery API, the cacheHit property in the query
result is set to true.
I am trying to access results.cacheHit, but it does work out.
AttributeError: 'RowIterator' object has no attribute 'cacheHit'
What am I doing wrong? How can I see the use of cache with my query?
The quote you are using from docs refers to the REST API (cacheHit is in the response of the getQueryResults method).
What you need instead is query_job.cache_hit

binding parameters into sql query in eclipse.persistence.logging

I am using eclipse.persistence.logging to log SQL queries. I want to bind the parameters into the query on the right place where they belong, so I could just copy+paste from log file into database development environment and run that query. For example I want "SELECT * FROM table WHERE id = 5" instead of "SELECT * FROM table WHERE id = ? bind => [5]" to be logged.
In the persistance.xml I have these properties already:
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.logger" value=my cutom logger class/>
And I made a custom logger class to override method public void log(SessionLogEntry logEntry), where I am replacing the '?' with parameters from the 'bind => [...]' part of the message using regex and string functions.
It all works fine but I have one problem, which I don't know how to solve.
Parameters in the '[...]' are separated by comma, so when I split the '[...]' part by commas to get an array of parameters, the string/varchar parameters containing commas mess up the whole thing and are splitted into multiple parts by those commas. The parameters are not quoted to mark the start and the end of the string/varchar parameter, so I don't see any solution to prevent the splitting.
At first I thought the parameters for the message would be accesible by getParameters() method of SessionLogEntry class and easier to handle, but this method returns null and the parameters are only in the message, therefore I used the regex and string functions.
Can anyone help me with this? Is there any other way to achieve the result?

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")]

Using a querystring parameter in an XSLT 2.0 SQL Query

Here is the query that I am using. Let's assume that the URL which was used to access this page is http://www.domain.com/default.aspx?id=3
<query name="SimpleQuery" rowElementName="Table" runif="showpage">
<sql>
<![CDATA[
select * from Table where ID = #QueryStringID
]]>
</sql>
<queryparam paramname="#QueryStringID" paramtype="request" requestparamname="id" sqlDataType="int" defvalue="0" validationpattern="" />
</query>
So this should translate this query: select * from Table where ID = 3, but it is not getting the querystring parameter properly.
According to the AspDotNetStorefront documentation (http://manual.aspdotnetstorefront.com/p-157-xml-packages.aspx), the paramtype request can be used to access querystring variables, but I cannot figure out how.
Any help would be greatly appreciated :)
That looks right. Your query, however, might not be executing due to your runif attribute (runif="showpage").
Exert from the link you referenced:
"The runif attribute can be used to run the query under only certain circumstances. The value should be either a querystring/form/cookie param or an appconfig param. If the specified querystring/form/cookie param or an appconfig param doesn't exist or is an empty the query will not be executed. This could be used for a page where the query should not be run until the page is submitted with a form field."

OData $filter with items in a $expand

I have given some web services to access informations.
The first thing that i have tries to expand a node . And i have done that successfully with following code
http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings
Now i want to filter ServiceOfferingID that i will get when expanding ServiceOfferings .
How can use filter option against a expanded collection
http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings&$filter=ServiceOfferings.ServiceOfferingID eq 127
But its not working. What is right way to do the same
The query you'll need to write depends on the cardinality of the expanded collection.
Here are some examples that use the public sample OData Northwind service, provided by odata.org.
An order is always done by exactly one customer.
Find the orders made by a customer with a specific name:
http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/CompanyName eq 'Vins et alcools Chevalier'. This is equivalent to the answer of Dhawal.
A customer can issue many orders.
Use the quantifiers all or any to specify whether you want at least one, or all of the orders to obey your conditions.
Find customers for which one or more orders have been processed by a specific employee:
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/any(o: o/EmployeeID eq 9)
Find customers that haven't ordered anything for a long time:
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/all(o: o/OrderDate lt DateTime'1997-01-01')
You can call http://services.odata.org/V3/Northwind/Northwind.svc/$metadata and inspect the NavigationProperty elements, to see which relations exist.
<NavigationProperty Name="Orders"
Relationship="NorthwindModel.FK_Orders_Customers"
ToRole="Orders"
FromRole="Customers"/>
Then, look for an association with that name and you'll find the cardinality:
<Association Name="FK_Orders_Customers">
<End
Type="NorthwindModel.Customer"
Role="Customers"
Multiplicity="0..1"/>
<End
Type="NorthwindModel.Order"
Role="Orders"
Multiplicity="*"/>
...
Navigating a one-to-many relationship like this: http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/EmployeeID eq 9, will give you: "The parent value for a property access of a property 'EmployeeID' is not a single value. Property access can only be applied to a single value."
Navigating a many-to-one relationship with all or any, like http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/any(c: c/CompanyName eq 'Vins et alcools Chevalier'), will give you:
"Any/All may only be used following a collection."
By the way, all() and any() are actually the Universal quantifier, ∀() and the existential quantifier, ∃(), respectively, which you may remember from math class.
Filtering by child object's properties is supported in oData.
Here is an example:
http://services.odata.org/Northwind/Northwind.svc/Orders?$filter=Customer/Country eq 'Germany'
Might be helpful for someone
GET serviceRoot/People?$expand=Trips($filter=Name eq 'Trip in US')
In OData the Filter command only works on the top level element. For your filter to work you would need to have the following URL
http://www.example.com/ODataService/WorkService.svc/CaseStudies(x)/ServiceOfferings?format=json&$filter=ServiceOfferingID eq 127
Obviously this isn't the query you are trying to write, but behind the scenes your query is being converted to an expression tree which has a root expression based on the top level element.
If you really required to filter the data you could potentially intercept the query and write your own expression as below:
[QueryInterceptor("CaseStudies")]
public Expression<Func<CaseStudie, bool>> CaseStudieFilter()
{
<Expression here>
}
You can also accomplish this through a webget on the service. I have had to do something similar to filter by properties of properties.