Convert SQL TO DQL ( doctrine) - doctrine

I want to convert SQL request TO DQL using Doctrine "Query Builder"
SELECT e1.id, e1.name, (SELECT COUNT() FROM (SELECT id FROM element e2 WHERE
e2.parent_id = e1.id LIMIT 1000) as e3 )
FROM element e1
WHERE
e1.parent_id = xxx
AND
e1.element_type_id = xxx
is it possible ?

You can write DQL for above SQL as below
SELECT e1.id, e1.name, COUNT(*) somealias
FROM YourBundle:Element e1
LEFT JOIN YourBundle:Element e2 WITH e1.id = e2.parent_id
WHERE e1.parent_id = xxx
AND e1.element_type_id = xxx
GROUP BY e1.id
I don't see the logic of using LIMIT in sub select.
It would be easier if you have defined the parent_id association in your entity as childs with i guess ManyToOne for unidirectional relation and doctrine will do the join part so you don't have to use WITH clause and your DQL will become like
SELECT e1.id, e1.name, COUNT(*) somealias
FROM YourBundle:Element e1
LEFT JOIN e1.childs e2
WHERE e1.parent_id = xxx
AND e1.element_type_id = xxx
GROUP BY e1.id

Related

JPQL query with having oracle db

I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)

How can I solve this CRUD set_relation_n_n issue

I have three tables:
aauth_user: id,email,pass,name,banned,last_login
aauth_user_to_groups: user_id,group_id
aauth_groups: id,name,definition
Here I want to set relation table auth_user_to_groups with auth_groups.
Here auth_user is basic table and auth_user_to_groups is relational table and auth_groups is selection table
try this:
SELECT *
FROM aauth_user_to_groups a
JOIN aauth_user b ON a.user_id = b.id
JOIN aauth_groups c ON a.group_id = c.id ;

Convert oracle to JPA createQuery

SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
In the above oracle query i have to got the follupnum and folluptype in the combination commnumber and commtype. But is it possible to write this query in JPA?
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery.
You could probably also reword your query in terms of JPQL.
If you are using EclipseLink, it does support sub-selects in the FROM clause in JPQL,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery
once click this link
Assuming you have Foreign Keys in place between CUSTOMER_MASTER and FOLLOWUP_GUIDES
i.e. Assuming
CUSTOMER_MASTER.FOLL_UP_NUM is FK to FOLLOWUP_GUIDES.COMMNUMBER
CUSTOMER_MASTER.FOLL_UP_TYPE is FK to FOLLOWUP_GUIDES.COMMTYPE
Then your query
SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1
WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
is equivalent to
SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND t1.STAGE_ID = '5'
This can be done in JPA quite easily and looks very similar to this SQL (only using object names and field names instead of table names and attribute names).
JPA JPQL query will look something like this:
SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND t1.stageId = "5"
Can also do with JPA subquery:
SELECT t0
FROM CustomerMaster t0
WHERE t0.companyId = "C001"
AND EXISTS (SELECT 1
FROM FollowupGuides t1
WHERE t1.commnumber = t0.follUpNum
AND t1.commtype = t0.FollUpType
AND t1.stageId = "5")
=B)

Join statement in Linq to Sql

I need to write Join statment after writing query in linq
example :
var Query = (from Tab in Db.Employees
select Tab)
as i have some cases to perform join operation so
i need to do it on this Query Query.Join(Join with another Table like Department); I need the Syntax
if (DeptID != -1){ Query.Join(Join with table Department where FkDeptID = DeptID); }
Consider the usage of join in the LINQ 'query syntax':
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
Something like this?
var results = (from q in Query
join m in myList on q.SomeID = m.SomeID
select unknown);
Try using this query:
var Query =
from e in Db.Employees
join d in Db.Departments on e.FkDeptID equals d.DeptID into departments
select new
{
Employee = e,
Department = departments.SingleOrDefault(),
};
This works assuming that when e.FkDeptID == -1 that there is no record in the Departments table and in that case Department would be assigned null.
You should never have more than one department for an employee so I've used SingleOrDefault rather than FirstOrDefault.

collation conflict

Is there any one who knows how we can solve collation issue in select linq query?
I'm getting this error when I want to select data in linq.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation
var lstData = from s in dataTrackDB.datas
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join m in dataTrackDB.mktDatas on s.mktcode equals m.mktcode
select new dataView {
Account=m.account,
brandcode=b.brandcode,
commodity=s.commodity,
date=s.date,
daysvalid=s.daysvalid,
mfrcode=b.mfrcode,
mktcode=s.mktcode,
price=s.price,
prodid=s.prodid,
statecode=s.statecode,
subcommodity=s.subcommodity,
supprecode=s.supprecode,
units =s.units
};
lstData = lstData.AsQueryable().Where(x => x.mfrcode == mfr );
return lstData.Take(100).ToList();
The problem is not in Linq but in your database
you can for example create a view that joins that way and the select the data in linq from the view
SELECT * FROM T1
INNER JOIN T2 ON
T1.Name COLLATE Latin1_General_CI_AS = T2.Name COLLATE Latin1_General_CI_AS
or select the data first in linq2sql separately for each table and then join it with linq2object
add COLLATE DATABASE_DEFAULT at the end of the query

Resources