HQL CASE statement usage - hql

I'm using HQL and have this query
<query name="rule.myRule.query.by.name">
<![CDATA[
FROM rule.myRule as cr
WHERE cr.name LIKE :name
AND cr.type = 'myType'
CASE
WHEN COUNT(cr.childRules) > 0
THEN
cr.childRules LIKE :name
]]>
</query>
I need some help making this query correct. What I want is to have the condition cr.childeRule LIKE :name execute only when the count of cr.childRules >0 .
Where am I wrong ?

FROM rule.myRule as cr
left join cr.childRules childRule
WHERE cr.name LIKE :name
AND cr.type = 'myType'
and childRule.id is null or childRule.name = :name
should do what you want.

Related

Ruby Sequel left outer join query

I have a more complex query (does not work)
query.select_all(:table_1).select_more(:col_1)
.left_outer_join(:table_2, table_1_id: :id, col_1: 1,
Seqel.lit('col_2 > ?', 1.week.ago)
I need to compare col_2 like this:
"col_2 > '2018-01-01 10:10:10'"
it is easy to write whole query as custom sql, but I would like to chain methods and so far I couldn't succeed in adding the above.
expected result ~:
"SELECT `table_1`.*, `table_2`.`col_1` FROM `table_1`
LEFT OUTER JOIN `table_2`
ON (
`table_2`.`table_1_id` = `table_1`.`id`
AND
`table_2`.`col_1` = 1
AND
`table_2`.`col_2` > '2018-01-01 10:10:10')"
I used with_sql but it ignores other scopes.
Also I tried:
.left_outer_join("`table_2` ON (`table_2`.`table_1_id` =
`table_1`.`id` AND `table_2`.`col_1` = 1 AND `table_2`.`col_2` >
'2017-09-28 06:49:53 UTC')")
Problem in last one is that it wraps that whole string with extra ``
what worked for me:
.left_outer_join(:table_2,
Sequel.lit('table_2.table_1_id = table_1.id AND table_2.col_1 = ?
AND table_2.col_2 > ?', id, date))
but is there a way to accomplish it without using string queries?
.left_outer_join(:table_2, :table_1_id=>:id, :col_1=>id){Sequel[:table_2][:col_2] > date}
I will post my own answer, but will not accept it because I think it could be done better.
.left_outer_join(:table_2,
Sequel.lit('table_2.table_1_id = table_1.id AND table_2.col_1 = ?
AND table_2.col_2 > ?', id, date))
So waiting for other suggestions.
This is might be a bit cleaner:
.left_outer_join(:table_2,
table_2.where(table_1_id: 'table_1.id')
.where(col_1: id)
.where('col 2 > ?', date)
)

How to use ActiveRecord to do partial matches with joined tables?

In ActiveRecord, I can do partial matches by using little tidbits of SQL like so:
Foo.where("name LIKE ?", "%matchthis%")
However, I'm looking to do something a little more complicated: use string concatenation between two tables and do a partial matches on the results. In PostgreSQL, I'd do it like this:
select
foos.id
from
foos
join
bars on
bars.id = foos.bar_id
where
foos.name || '.' || bars.name like '%match.this%'
;
How can I do the above using ActiveRecord? I would greatly prefer to use only ActiveRecord syntax only, and if I can't, it would be very beneficial to have the SQL blurbs SQL-agnostic.
A bit more ActiveRecord:
Foo.joins(:bars).where("foos.name || '.' || bars.name LIKE ?", '%match.this%')
This produces a query in AR but not sure if it is exactly what you are looking for. The query produced is:
SELECT "foos".* FROM "foos"
INNER JOIN "bars" ON "bars"."foo_id" = "foos"."id"
WHERE (foos.name || '.' || bars.name LIKE '%match.this%')
You might want to approach it separately like:
Foo.where('foos.name LIKE ?', '%match%').joins(:bars).where('bars.name LIKE ?', '%this%')
SELECT "foos".* FROM "foos"
INNER JOIN "bars" ON "bars"."foos_id" = "foos"."id"
WHERE (foos.name LIKE '%match%') AND (bars.name LIKE '%this%')

Is there a way to see the raw SQL that a Sequel expression will generate?

Say I have a Sequel expression like:
db.select(:id).from(:some_table).where(:foo => 5)
Is there a way to get the SQL string that this will generate (i.e. "SELECT id FROM some_table WHERE foo = 5")? I notice that calling inspect or to_s on the result of the above expression includes that generated SQL, but not sure how to access it directly.
And how about Sequel expressions that do not return a dataset, like:
db.from(:some_table).update(:foo => 5)
Is it possible to see the SQL from this before it's executed?
You can call sql on dataset:
db.select(:id).from(:some_table).where(:foo => 5).sql # => "SELECT `id` FROM `some_table` WHERE (`foo` = 5)"
For update queries you can do this:
db.from(:some_table).update_sql(:foo => 5) # => "UPDATE `some_table` SET `foo` = 5"
Some similar useful methods:
insert_sql
delete_sql
truncate_sql

Dynamic select in query with linq

I see Dynamic linq in below link:
ScottGu
I want to use this method to select a dynamic query like this:
I have a complex select and this way not solve my problem,
this is my select:
Select sUserName,sname, sFamily,sMobail,sid,sNumberSt,sActive,sPic,sDate from Student where {0} order by sid desc";
which {0} is a complex Condition:
sname like %somthing% and susername=N'something' and sid in (select sidfk from tablex where userteacher=N'name1')
and this condition is passed to method.
I must say that:
I don's know my condition,my condition specified with user search params.this condition that I say,Is only one example of what I want to do.
How can I do this?
Only way that solve my problem:
I send tow parameters instead of one,one for student and one for tablex:
var az = db.tablex.Where(p => p.userteacher== name1)
.Select(p => p.sidfk).ToList();
var query = db.Students.Where(textSearch).Where(s=>az.Contains(s.sid)).OrderByDescending(s => s.sid)
.Select(s => new
{
s.sUserName,
s.sname,
s.sFamily,
s.sMobail,
s.sid,
s.sNumberSt,
s.sActive,
s.sPic,
s.sDate,
});
wiche textSearch is :
sname like %somthing% and susername=N'something'
with dynamic linq
any better way is exist?
You don't need to use dynamic linq for this situation.
string paramA = "", paramB = "";
var query = from x in context.table1
where x.name == paramA
where context.table2.Where(y => y.field1 == paramB).Any(y => y.id == x.id)
select x;
Dynamic Linq usually use if in query you don't know what field will be use, so in your sample you use only params for conditions with field, so you don't ned dynamic linq
you can little optimize you query like this
var query = from student in db.Students
join teacher in db.tablex on student.sid equals teacher.sidfk
where student.sname.Contains('somthing') &&
susername=='something' &&
teacher.userteacher=='name1'
orderby s.sid descending
select new
{
s.sUserName,
s.sname,
s.sFamily,
s.sMobail,
s.sid,
s.sNumberSt,
s.sActive,
s.sPic,
s.sDate,
};

select documents based on embedded document values in mongodb using linq

How can I select documents based on values within the embedded documents? For instance, I have class r that has a list of i classes. I want to select all r's that have i.name=="foo".
If you want to select all r's that have at least one i with i.Name == "foo" you can use this query:
var result = collectionOfRs.Where(r => r.ListOfIs.Any(i => i.Name == "foo"));
If you want to select all r's that have all i's with i.Name == "foo" you can use this one:
var result = collectionOfRs.Where(r => r.ListOfIs.All(i => i.Name == "foo"));

Resources