How to select similar data using SQL in JCR/jackrabbit? - sql

how do we write query in JCR/Jackrabbit to select data using:
"where title like '%News%".
here is my structure in repository
/rootNode and under root node I have many child nodes and under those child nodes i have data in which I want to search if a string matches the name or is similar to that name.
please pardon me if i am not able to explain. I m new to JCR/jackrabbit.
I know how to do in database example
(SELECT * FROM Customers
WHERE City LIKE 'anystring%';)
i want to accomplish similar thing in JCR.
Thanks

If you are planning to use XPATH query syntax, then the jcr:like function can be used to achieve your requirements.
/jcr:root//*[jcr:like(#jcr:title, '%News%')]
For SQL grammar, the LIKE operator can be used for the same.
select * from nt:base where jcr:title like '%News%'
For SQL2, the LIKE operator can be used again.
SELECT * FROM [nt:base] AS s WHERE s.[jcr:title] LIKE '%News%'
All the above queries are for reference only, they would run on the entire repository. But when using them for achieving the desired functionality, kindly restrict them to query within the required subtree and not the entire repo.

Related

Does anyone know what this regexp_replace do?

I received this snippet from someone who doesn't work for my current company anymore, and I can't figure out what this regex do.
The objective for him was to scan through sql query strings and rearrange table info
regexp_replace(query, ' ("*?)(analytics_internal|arr|bizops_analytics|cloud_api|cloud_backend_raw|data_science|eloqua|fb_ads|google_ads|information_schema|intricately|legacy_sfdc|marketing_analytics|ns__analytics_postprocessing|ns__global_write|product_analytics|raw_bing_ads|raw_cloud_api|raw_compass|raw_coveo|raw_eloqua|raw_g_search_console|raw_gainsight|raw_google_ads|raw_intercom|raw_linkedin_ads|raw_mightysignal|raw_realm|raw_sfdc|remodel_cloud|remodel_test|sales_analytics|sales_ops|sampledb|segment|sfdc|ts_analytics|university_platform_analytics|upstream_gainsight|usage|xform_cloud|xform_etl|xform_finance|xform_marketing|xform_reference|xform_sales|xform_tables)("*?)\.("*?)(.+?)("*?)(\s|$)', ' awsdatacatalog.$1$2$3.$4$5$6$7') as queryString
The regex you've provided attempts to match any of the many provided strings in the second capture group and modify that part of the query to be prefixed with awsdatacatalog.. This is most likely an attempt to modify queries to occur on a new database, in particular a database named awsdatacatalog. For example, the consider the following query:
SELECT * FROM "analytics_internal".foo.table
Your regex_replace should produce a new query that looks like
SELECT * FROM awsdatacatalog."analytics_internal".foo.table

HQL to SQL translation - defining meanful alias for columns

I have tricki problem:
When I translate a HQL to SQL (using org.hibernate.hql.spi.QueryTranslator) i got a valid SQL.
Hibernate: Parse/Translate HQL FROM part to get pairs class alias, class name
It works as expected!
But, my problem is the transalation of the column aliases!
HQL to SQL
*) HQL for entity:Base
SELECT Base FROM Base Base
leads into:
*) SQL for entity:Base
select base0_.iD as id1_0_, base0_.comment as comment2_0_, base0_.creationDate as creation3_0_ from ...
You can see my problem:
The Alias of the columns are not intutive names:
base0_.creationDate --> creation3_0_
Expected:
base0_.creationDate --> creationDate
UseCases:
Creating Views for each entity, automatically
Better readabillity for our Database adminitrators
I have debugged hours and hours to find a solution to influence the mechnanism.
I hope some one has an idea how to solve this problem (whithout hacking)!
I know this is a not conventional question, so i would be glad, someone has an idea ;-)
Thanks, in advance
Andy
The problem you faced is known as Hibernate naming stragety.
Here https://www.baeldung.com/hibernate-naming-strategy you can find a deeper explanation on how to customize the generated column names by implementing the interface PhysicalNamingStrategy.
Thanks for your help, but it was not the solution, because now I can rename every column, table or schema, but not the alias in a select query.
I still get queries like this:
select base0_.i_d as i_d1_0_, ....
from base base0_
left outer join campaign base0_1_ on base0_.i_d = base0_1_.i_d
but I would like to have a query without all this "1_0" etc.
select base.i_d as i_d, ....
from base base
left outer join campaign base on base.i_d = campaign.i_d ...
I want to use the translated queries to create views for all entities:
resulting view and columns
You can see, column names are not very useful ;-)
Does anyone have any idea, without string substitution or modifying the SQL AST?

SQL DB2 - How to SELECT or compare columns based on their name?

Thank you for checking my question out!
I'm trying to write a query for a very specific problem we're having at my workplace and I can't seem to get my head around it.
Short version: I need to be able to target columns by their name, and more specifically by a part of their name that will be consistent throughout all the columns I need to combine or compare.
More details:
We have (for example), 5 different surveys. They have many questions each, but SOME of the questions are part of the same metric, and we need to create a generic field that keeps it. There's more background to the "why" of that, but it's pretty important for us at this point.
We were able to kind of solve this with either COALESCE() or CASE statements but the challenge is that, as more surveys/survey versions continue to grow, our vendor inevitably generates new columns for each survey and its questions.
Take this example, which is what we do currently and works well enough:
CASE
WHEN SURVEY_NAME = 'Service1' THEN SERV1_REC
WHEN SURVEY_NAME = 'Notice1' THEN FNOL1_REC
WHEN SURVEY_NAME = 'Status1' THEN STAT1_REC
WHEN SURVEY_NAME = 'Sales1' THEN SALE1_REC
WHEN SURVEY_NAME = 'Transfer1' THEN Null
ELSE Null
END REC
And also this alternative which works well:
COALESCE(SERV1_REC, FNOL1_REC, STAT1_REC, SALE1_REC) as REC
But as I mentioned, eventually we will have a "SALE2_REC" for example, and we'll need them BOTH on this same statement. I want to create something where having to come into the SQL and make changes isn't needed. Given that the columns will ALWAYS be named "something#_REC" for this specific metric, is there any way to achieve something like:
COALESCE(all columns named LIKE '%_REC') as REC
Bonus! Related, might be another way around this same problem:
Would there also be a way to achieve this?
SELECT (columns named LIKE '%_REC') FROM ...
Thank you very much in advance for all your time and attention.
-Kendall
Table and column information in Db2 are managed in the system catalog. The relevant views are SYSCAT.TABLES and SYSCAT.COLUMNS. You could write:
select colname, tabname from syscat.tables
where colname like some_expression
and syscat.tabname='MYTABLE
Note that the LIKE predicate supports expressions based on a variable or the result of a scalar function. So you could match it against some dynamic input.
Have you considered storing the more complicated properties in JSON or XML values? Db2 supports both and you can query those values with regular SQL statements.

SQL selecting results from multiple tables

Hello I want to display results from unrelated tables where a text string exists in a column which is common to all tables in the database.
I can get the desired result with this:
SELECT *
FROM Table1
WHERE Title LIKE '%Text%'
UNION
SELECT *
FROM Table2
WHERE Title LIKE '%Text%'`
However my question is is there a more efficient way to go about this as I need to search dozens of tbls. Thanks for any help you can give!
ps the system I am using supports most dialects but would prefer to keep it simple with SQL Server as that is what I am used to.
There is a SP script you can find online called SearchAllTables (http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm).
When you call it pass in the string, it will return the tables and columns as well as the full string.
You can modify it to work with other datatypes quite easily. It's a fantastic resource for tasks exactly like yours.

Placing index columns on the left of a mysql WHERE statement?

I was curious since i read it in a doc. Does writing
select * from CONTACTS where id = ‘098’ and name like ‘Tom%’;
speed up the query as oppose to
select * from CONTACTS where name like ‘Tom%’ and id = ‘098’;
The first has an indexed column on the left side. Does it actually speed things up or is it superstition?
Using php and mysql
Check the query plans with explain. They should be exactly the same.
This is purely superstition. I see no reason that either query would differ in speed. If it was an OR query rather than an AND query however, then I could see that having it on the left may spped things up.
interesting question, i tried this once. query plans are the same (using EXPLAIN).
but considering short-circuit-evaluation i was wondering too why there is no difference (or does mysql fully evaluate boolean statements?)
You may be mis-remembering or mis-reading something else, regarding which side the wildcards are on a string literal in a Like predicate. Putting the wildcard on the right (as in yr example), allows the query engine to use any indices that might exist on the table column you are searching (in this case - name). But if you put the wildcard on the left,
select * from CONTACTS where name like ‘%Tom’ and id = ‘098’;
then the engine cannot use any existing index and must do a complete table scan.