apache camel sql : request with like - java

hello i'm stuck in this problem i have a query that take in setting the value of $ {body}
<to uri="sql:SELECT distinct substr(cust_account,4,3) as TypeCompte from bnaservice.customer_accounts where cust_account like '#${body[0]}%' order by cust_account?dataSource=moodleDB"/>
<to uri="bean:tn.ngtrend.CompteClientRest.Transformer?method=ToXml(Exchange)"/>
the value of $ {body} equal 001 but each time I execute the query it gives me a result null
I think the concatenation between $ {body} and% is wrong
is there anyone who can help me solve this problem,thanks

Can you try to use a colon? Because body is like a named parameter.
like ':#${body[0]}%'
If this still does not work, I would try to concatenate the expression and the % in an Exchange property and then call the property in the SQL statement

Finally I get this working with this snippet:
(It's scala but it's practically the same)
transform("%" + _.in[String] + "%")
to("sql:SELECT * FROM quotes WHERE quote LIKE :#${body} ORDER BY RANDOM() LIMIT 1")

Related

Unable to capture next line character in Java

I have a requirement of parsing through an python file which contains multiple sql queries and get the start and end positions of the query to get only the query part using JAVA
I am using .contains function to check for sql(''' as my opening character for the query and now for the closing character I have ''') but there are some cases where ''') comes in between the query when there is a variable involved which should not be detected as an end of the query.
Something like this :
spark.sql(''' SELECT .......
FROM.....
WHERE xxx IN ('''+ Variable +''')
''')
here the last but one line also gets detected as end of line if I use line.contains(" ''') ") which is wrong.
All I can think of is to check for next line character as the end of the query as each query is separated by two empty lines. So tried these if (line.contains(" ''')\n") & if (line.contains(" ''')\r\n") but none of them work for me.
Kindly let me know of any other way to do this.
Note that I do not have the privilege to change the query file.
Thanks
I believe simple contains won't solve this problem.
You will have to use Pattern if you are looking to match \n.
String query = "spark.sql(''' SELECT .......\n" +
"FROM..... \n" +
"WHERE xxx IN ('''+ Variable +''')\n" +
"''')";
Pattern pattern = Pattern.compile("^spark.sql\\('''(.*)'''\\)$", Pattern.DOTALL);
System.out.println(pattern.matcher(query).find());
Output:
true
Pattern.DOTALL tells Java to allow the dot to match newline characters, too.

How to print unmatched parameters in Drools

In the following Drools file, I join two queries in the when expression, and print matched results.
import com.demo.drools.*;
rule "demo"
when
$book: BlockTrade()
$buys : Trade(type=="buy") from $book.trades
$sells : Trade(type=="sell", $buys.id==id,
$buys.price==price,
$buys.trader==trader) from $book.trades
then
System.out.println("buys: " + $buys);
System.out.println("sells: " + $sells);
end
It works okay, but I want to log all unmatched trades with an unmatch reason.
For example:
Trade id=1 doesn't match because $buys.type="both" doesn't match any trades in $buys or $sells
// or
Trade id=2 doesn't match because $buys.price=50, and $buys.trader="John" doesn't match any $sells
How can it be implemented?
See this other answer. If you want to log the unmatched trades, you will need to create the rules for that.
Hope it helps,

JPA Select query not returning results with one letter word

I have a query that when given a word that starts with a one-letter word followed by space character and then another word (ex: "T Distribution"), does not return results. While given "Distribution" alone returns results including the results for "T Distribution". It is the same behavior with all search terms beginning with a one-letter word followed by space character and then another word.
The problem appears when the search term is of this pattern:
"[one-letter][space][letter/word]". example: "o ring".
What would be the problem that the LIKE operator not working correctly in this case?
Here is my query:
#Cacheable(value = "filteredConcept")
#Query("SELECT NEW sina.backend.data.model.ConceptSummaryVer04(s.id, s.arabicGloss, s.englishGloss, s.example, s.dataSourceId,
s.synsetFrequnecy, s.arabicWordsCache, s.englishWordsCache, s.superId, s.categoryId, s.dataSourceCacheAr, s.dataSourceCacheEn,
s.superTypeCasheAr, s.superTypeCasheEn, s.area, s.era, s.rank, s.undiacritizedArabicWordsCache, s.normalizedEnglishWordsCache,
s.isTranslation, s.isGloss, s.arabicSynonymsCount, s.englishSynonymsCount) FROM Concept s
where s.undiacritizedArabicWordsCache LIKE %:searchTerm% AND data_source_id != 200 AND data_source_id != 31")
List<ConceptSummaryVer04> findByArabicWordsCacheAndNotConcept(#Param("searchTerm") String searchTerm, Sort sort);
the result of the query on the database itself:
link to screenshot
results on the database are returned no matter the letters case:
link to screenshot
I solved this problem.
It was due to the default configuration of the Full-text index on mysql database which is by default set to 2 (ft_min_word_len = 2).
I changed that and rebuilt the index. Then, one-letter words were returned by the query.
12.9.6 Fine-Tuning MySQL Full-Text Search
Use some quotes:
LIKE '%:searchTerm%';
Set searchTerm="%your_word%" and use it on query like this :
... s.undiacritizedArabicWordsCache LIKE :searchTerm ...

Fetching data from xml Using Xquery with starts with function

I want to fetch a data from xml Using Xquery with starts with function.
data.xml
<data><employee id=\"1\"><name value=\"vA-12\">A</name> <title id=\"2\">Manager</title></employee>
<employee id=\"2\"><name value=\"vC-12\">C</name><title id=\"2\">Manager</title></employee>
<employee id=\"2\"><name value=\"vB-12\">B</name><title id=\"2\">Manager</title></employee>
</data>
Now I want to fetch that name which has employee#id=title#id and name#value starts with 'vC'.
I have written this xquery for the same.Please see below but getting error-
for $x in /data/employee where $x/#id=$x/title/#id and [fn:starts-with($x/name/#value,vC)] return data($x/name)
this is error-
Error on line 1 column 55
XPST0003 XQuery syntax error near #.../title/#id and [fn:starts-with#:
Unexpected token "[" in path expression
net.sf.saxon.s9api.SaxonApiException: Unexpected token "[" in path expression
at net.sf.saxon.s9api.XQueryCompiler.compile(XQueryCompiler.java:544)
at Xml.process(Xml.java:46)
at Xml.main(Xml.java:30)
Caused by: net.sf.saxon.trans.XPathException: Unexpected token "[" in path expression
at net.sf.saxon.query.XQueryParser.grumble(XQueryParser.java:479)
at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:221)
starts-with() function expects two parameters. I'm not familiar with Saxon specifically, but in general xquery you can do this way :
for $x in /data/employee[#id=title/#id and name[starts-with(#value,'vC')]]
return data($x/name)
or using where clause instead of predicate in for clause, as in your attempted query :
for $x in /data/employee
where $x/#id=$x/title/#id and $x/name/starts-with(#value,'vC')
return data($x/name)
Just use this shorter and simpler XPath 2.0 one-liner:
/*/employee[#id eq title/#id and starts-with(name/#value, 'vC')]/data(name)
Depending on your exact requirements, you might need this instead (I let you go through the differences, but basically it selects all names with #value starting with vC, where the other solutions here select all names of all employees with at least a name starting with vC):
/data/employee[#id eq title/#id]/name[starts-with(#value, 'vC')]/data(.)

String split function with hql

I have following hql query,
from Channe where ip='1.11.6.0';
But in the db the IP is saving as 1.11.6.0:8080 .
So I need to modify the query in a way that, split the ip with a delimiter ':' and take the firstcome value. I do not wish to modify the search with value 1.11.6.0:8080.
See this page in the Hibernate docs. On the page below there is a section called 14.10. Expressions
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html
It says, among other things:
string concatenation ...||... or concat(...,...) current_date(),
...
Any function or operator defined by EJB-QL 3.0: substring(), trim(), lower(), upper(),
length(), locate(), abs(), sqrt(), bit_length(), mod()
But you are actually better off doing as #Hansraj suggests in the comments and appending a wildcard to your search term
String query = "from Channe where ip like :term";
entityManager.createQuery(query).setParameter("term",ipString + "%");
This assumes that your data type is string, of course.
Try the following:
Say variable ip had the address
ip = "10.131.56.40:8080";
var ipSplit = ip.Split(':');
var ipStart = ipSplit[0];
ipStart will store only 10.131.56.40
This could solve your problem
Try this:
SPLIT(".", FIELDNAME)

Categories