Eloquent Raw where query using like condition - laravel-4

I am using this Eloquent raw query to fetch some search results combining both caption and tags column. my code goes like this
$term="Test";
$clips= \Clip::whereRaw("caption like '%?%' OR tags like '%?%' ", array($term,$term))->get();
dd($clips);
but using this I am not able to get results as the dump shows no results, where as using below code I am able to get results:
$term="Test";
$clips= \Clip::whereRaw("caption like '%$term%' OR tags like '%$term%' ")->get();
dd($clips);
and dump shows all 5 results which are expected. What am I doing wrong in first case.

If you use prepared statements, you should use a ? and nothing else. If you're adding quotes yourself, you should not be using prepared statements. So let the prepared statement take care of the quotes and add the %-sign to the variable you are inserting into the prepared statement.
$term="Test";
$clips= \Clip::whereRaw("caption like ? OR tags like ? ", array('%'.$term.'%','%'.$term.'%'))->get();
dd($clips);
By the way, you could also do this without a raw where..
$term="Test";
$clips=\Clip::where("caption","like","%".$term."%")->orWhere("tags","like","%".$term."%")->get();
dd($clips);
... and personally I would even prefer to use a scope for these kind of things.

Related

Looking for a way to view an interpolated query in golang mysql

So, I have a reasonably complicated query which I am trying to debug, but for a simple example let's say I have something like this:
q := "SELECT id FROM users WHERE timestamp > ? AND timestamp < ?"
I will do a Query() on this the usual way, e.g.
db.Query(q, 1546300800,1561127488)
And I would like to log/println/whatever (for debugging) the interpolated query, to end up with
SELECT id FROM users WHERE timestamp > 1546300800 AND timestamp < 1561127488
Wondering if anyone has a trick for me here.
Based on #mkopriva's comments,
Not in Go as it does not do the interpolation, both the query string and the argument values are sent to the db server, which does the interpolation. So if the db server doesn't provide such a feature then you're out of luck. Alternatively you could try searching for a 3rd party package that does the interpolation, however if you find one keep in mind that to be accurate it needs to keep up with the target server's version, if it doesn't do that you may see logs that don't match the actual query being executed
I cannot accomplish this using tooling, as go never has the interpolated query. Sticking to spitting out the non-interpolated query along with the pile of arguments.

How to fetch multiple cq pages using Xpath based on page's property

I have two cq page and I want to retrive these two using jcr:title property using XPATH. The below query is working fine for single.
/jcr:root/content/test//element(*, cq:Page)[((jcr:like(jcr:content/#jcr:title, dell)))]
But I want to excuate it for multiple items. I have tried with following option but it is not working
/jcr:root/content/test//element(*, cq:Page)[((jcr:like(jcr:content/#jcr:title, [dell,samusng])))]
Could anyone help me to write xpath query?
As rakhi4110 already mentioned in the comment, you can combine multiple where clauses with an or just like in SQL. Though I think you either want exact matches or use jcr:containts instead of jcr:like.
Exact match:
/jcr:root/content/test//element(*, cq:Page) [jcr:content/#jcr:title='dell' or jcr:content/#jcr:title='samsung']
Contains:
/jcr:root/content/test//element(*, cq:Page) [jcr:contains(jcr:content/#jcr:title, 'dell') or jcr:contains(jcr:content/#jcr:title, 'samsung')]
Or if you really want to use jcr:like which, as in SQL, uses % for wildcard:
/jcr:root/content/test//element(*, cq:Page) [jcr:like(jcr:content/#jcr:title, '%dell%') or jcr:like(jcr:content/#jcr:title, '%samsung%')]

Find HTML Tags in Properties

My current issue is to find HTML-Tags inside of property values. I thought it would be easy to search with a query like /jcr:root/content/xgermany//*[jcr:contains(., '<strong>')] order by #jcr:score
It looks like there is a problem with the chars < and > because this query finds everything which has strong in it's property. It finds <strong>Some Text</strong> but also This is a strong man.
Also the Query Builder API didn't helped me.
Is there a possibility to solve it with a XPath or SQL Query or do I have to iterate through the whole content?
I don't fully understand why it finds This is a strong man as a result for '<strong>', but it sounds like the unexpected behavior comes from the "simple search-engine syntax" for the second argument to jcr:contains(). Apparently the < > are just being ignored as "meaningless" punctuation.
You could try quoting the search term:
/jcr:root/content/xgermany//*[jcr:contains(., '"<strong>"')]
though you may have to tweak that if your whole XPath expression is enclosed in double quotes.
Of course this will not be very robust even if it works, since you're trying to find HTML elements by searching for fixed strings, instead of actually parsing the HTML.
If you have an specific jcr:primaryType and the targeted properties you can do something like this
select * from nt:unstructured where text like '%<strong>%'
I tested it , but you need to know the properties you are intererested in.
This is jcr-sql syntax
Start using predicates like a champ this way all of this will make sense to you!
HTML Encode <strong>
HTML Decimal <strong>
Query builder is your friend:
Predicates: (like a CHAMP!)
path=/content/geometrixx
type=nt:unstructured
property=text
property.operation=like
property.value=%<strong>%
Have go here:
http://localhost:4502/libs/cq/search/content/querydebug.html?charset=UTF-8&query=path%3D%2Fcontent%2Fgeometrixx%0D%0Atype%3Dnt%3Aunstructured%0D%0Aproperty%3Dtext%0D%0Aproperty.operation%3Dlike%0D%0Aproperty.value%3D%25%3Cstrong%3E%25
Predicates: (like a CHAMP!)
path=/content/geometrixx
type=nt:unstructured
property=text
property.operation=like
property.value=%<strong>%
Have a go here:
http://localhost:4502/libs/cq/search/content/querydebug.html?charset=UTF-8&query=path%3D%2Fcontent%2Fgeometrixx%0D%0Atype%3Dnt%3Aunstructured%0D%0Aproperty%3Dtext%0D%0Aproperty.operation%3Dlike%0D%0Aproperty.value%3D%25%26lt%3Bstrong%26gt%3B%25
XPath:
/jcr:root/content/geometrixx//element(*, nt:unstructured)
[
jcr:like(#text, '%<strong>%')
]
SQL2 (already covered... NASTY YUK..)
SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/content/geometrixx]) and text like '%<strong>%'
Although I'm sure it's entirely possible with a string of predicates, it's possibly heading down the wrong route. Ideally it would be better to parse the HTML when it is stored or published.
The required information would be stored on simple properties on the node in question. The query will then be a lot simpler with just a property = value query, than lots of overly complex query syntax.
It will probably be faster too.
So if you read in your HTML with something like HTMLClient and then parse it with a OSGI service, that can accurately save these properties for you. Every time the HTML is changed the process would update these properties as necessary. Just some thoughts if your SQL is getting too much.

XPATH - how to use multiple operators on one query

I am scraping a web URL that looks like this:
www.example.com/pages/popup/popup_report_review.aspx?bikeReviewID=6582049&PageTypeID=9&width=900&height=500
I only want the ID number from it. I tried to run this query first to start the parsing after the "=" and it works perfectly:
normalize-space(substring-after(//a[#id='webpage_url']/#href, '='))
So now I have: 6582049&PageTypeID=9&width=900&height=500
I now want to run another query to just leave me with the ID number so I think this is the one:
substring(//a[#id='webpage_url']/#href,1,7)
This leaves me with only 7 characters. They both work perfectly independently, but I cannot get them to run together. I tried using 'and' but that returns me a number 1.. This is what I did:
normalize-space(substring-after(//a[#id='webpage_url']/#href, '=')) and substring(//a[#id='webpage_url']/#href,1,7)
Does anyone know how I can combine both queries or is there a better way to get this ID? Thanks in advance doe any pointers.
I should use this query:
substring-before(substring-after(//a[#id='webpage_url']/#href, '='), '&')

LINQ - OR two SqlMethods.Like clauses

I need to OR two SqlMethods.Like statements in LINQ, and I'm not sure how to accomplish it (or if it's the right way to go about it).
I've got vendor ID and vendor name fields, but I've only got a generic vendor search that allows a user to search for a vendor based on their name or ID. I also allow wildcards in the search, so I need to find vendors whose ID or name is like the user's input.
I want to do something like below, but obviously it's not correct. (EDIT: It does work as written.)
results = results.Where(p => SqlMethods.Like(p.VendorId, inputVendor.Replace("*", "%") ||
SqlMethods.Like(p.VendorName, inputVendor.Replace("*", "%"));
Background: I add where statements depending on the search parameters entered by the user, hence the results = results.Where part.
Any help would be appreciated!
It's not clear to me why this is "obviously" not correct. Presumably it's not working, otherwise you wouldn't have posted, but it's not obvious how it's not working.
I would suggest performing the replacement before the query, like this:
string vendorPattern = inputVendor.Replace("*", "%");
But then I'd expect this to work:
results = results.Where(p => SqlMethods.Like(p.VendorId, vendorPattern) ||
SqlMethods.Like(p.VendorName, vendorPattern));
Of course you're limited to where wildcards can appear in a SQL LIKE query, but that's a separate problem. (I'm not sure of the behaviour offhand if it's not at the start or end.)
If that doesn't help, please update the question with what happens when you try this.

Resources