Is it possible to put a subquery inside the FROM statement in my HQL Querys? - hql

Here is my query.
SELECT letter
FROM
Letter AS letter,
(evaluateDisplayName) AS displayName
WHERE
letter.id =: someID
AND displayName =: someDisplayName
// AND etc etc...
The Subquery in this part:
(Do some subquery here) AS displayName
I don't know how to form. But the logic is something like this:
private String evaluateDisplayName(Letter letter) {
def username = letter?.sender?.username
def lastName = letter?.sender?.lastName
def emailAddress = letter?.sender?.emailAddress
return username ?: lastName ?: emailAddress
}
How to turn this into a subquery?

You don't need a subquery, the logic of evaluateDisplayName seems to be the same of the coalesce function: return the first not null value. Try with this:
SELECT letter
FROM
Letter AS letter LEFT JOIN letter.sender AS sender
WHERE
letter.id = :someID
AND COALESCE(sender.username, sender.lastName, sender.emailAddress) = :someDisplayName
// AND etc etc...

Related

How does Room use sql statement keywords as query parameters?

#Query(
"""
SELECT * FROM t_article ORDER BY id :order
"""
)
fun pagingSource(order: String): PagingSource<Int, Article>
I want order set ASC or DESC, but the compiler prompts
'(', '.', ASC, BETWEEN, COLLATE, DESC, IN, LIMIT, comma or semicolon expected, got ':order'
How to solve this problem?
The issue is that you can only bind (replace ? with a value) literal values, not KEYWORDS or component names. In short if bound then the values are suitably enclosed so binding the string ASC would result in 'ASC'
You basically have the following options.
a) Multiple #Querys with selection of the appropriate Query (ascending or descending) e.g.
#Query("SELECT * FROM t_article ORDER BY id ASC")
fun pagingSourceAsc(): PagingSource<Int, Article>
#Query("SELECT * FROM t_article ORDER BY id DESC")
fun pagingSourceDesc(): PagingSource<Int, Article>
fun pagingSource(order: String): PagingSource<Int, Article> {
if(order.equals
}
b) Manipulation according to a value passed to the query where the keywords are selected according to the selection criteria e.g.
#Query("SELECT * FROM t_articale ORDER BY WHEN :order = 'ASC' THEN id ASC ELSE id DESC END")
c) By using a #RawQuery, which is very flexible BUT not validated at compile time e.g.
#RawQuery
fun pagingSourceRawQuery(rawQuery: SupportSQLiteQuery): PagingSource<Int, Article>
fun pagingSource(order: String): PagingSource<Int,Article> {
return pagingSourceRawQuery(SimpleSQLiteQuery("SELECT * FROM t_artical ORDER BY $order"))
}
Noting that Room will not validate the SQL at compile time and also that as the order is not bound then there is the potential for SQL injection.
this option can eliminate complex SQL but for the sake of the complexity being moved to the programming code (and thus often more understandable).

HQL query returing null value on use of like operator for search

query = genericDAOImpl.getHibernateSession().createQuery("select t from ticket t where createdBy=:user and t.subject like concat("%", :summary, "%")
.setParameter("user", userId)
.setParameter("summary", inputBean.getSummary);
this is my query when I search for some values it returns the proper output but when I give null value for the search field it returns empty list actually it should return all the values when the search field is empty
You should just use a plain named placeholder for the predicate of the LIKE expression, and then bind the wildcard string from the Java side:
String sql = "select t from ticket t where createdBy = :user and t.subject like :summary";
query = genericDAOImpl.getHibernateSession().createQuery(sql);
query.setParameter("user", userId);
query.setParameter("summary", "%" + inputBean.getSummary + "%");

Accessing array property in "named query" in hyperledger-composer

In my hyperledger composer app I want to write a named query that returns all persons with a certain hobby.
The model for "Person" is the following:
participant Person identified by id {
o String id
o String firstName
o String lastName
o String email
--> Hobby[] hobbies optional
}
The model for "Hobby" is the following:
asset Hobby identified by name {
o String name
}
The named query has the following structure:
query selectPersonsByHobby {
description: "Select all persons with a certain hobby."
statement:
SELECT org.comp.myapp.Person
WHERE //don't know what to put here//
}
I don't know what to put after the "WHERE" operator in order to achieve what I want.
I want something like the following:
query selectPersonsByHobby {
description: "Select all persons with a certain hobby."
statement:
SELECT org.comp.myapp.Person
WHERE (hobbies.contains(_$hobby))
}
Is this even possible ?
The short answer is that this query should work:
query selectConsultantsBySkill {
description: "Select all persons with a certain hobby."
statement:
SELECT org.comp.myapp.Person
WHERE (hobbies CONTAINS _$targetHobby)
}
But note that because your hobbies is an array of Relationships the targetHobby parameter will have to be something like resource:org.acme.Hobby#cycling . In a production scenario you would be 'calling' the query from a UI program so you could pre-pend the relationship syntax.
I guess this is just a test example, but I wonder if Hobby needs to be a relationship? It would be easier if not.
You could alternatively use a concept (and even an enumerated type within the concept!). Here is an example of a modified model and query with a concept:
participant Person identified by id {
o String id
o String firstName
o String lastName
o String email
o Interest[] passTime
}
concept Interest {
o String name
o String description
}
query selectConsultantsBySkill {
description: "Select all persons with a certain hobby."
statement:
SELECT org.comp.myapp.Person
WHERE (passTime CONTAINS (name == _$targetHobby ))
}

Linq conversion

I am using the following code to return an IList:
public IList<string> FindCodesByCountry(string country)
{
var query = from q in session.Linq<Store>()
where q.Country == country
orderby q.Code
select new {q.Code};
return (IList<string>) query.ToList();
}
However I keep getting this error:
Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType01[System.String]]' to type 'System.Collections.Generic.IList`1[System.String]'.
What I am supposed to return here?
as long as q.code is a string this should work:
note that it is not creating an anonymous object, just the string is being selected.
public IList<string> FindCodesByCountry(string country)
{
var query = from q in session.Linq<Store>()
where q.Country == country
orderby q.Code
select q.Code;
return query.ToList();
}
Is there a reason you were selecting an anonymous type? If not try this...
var query = from q in session.Linq<Store>()
where q.Country == country
orderby q.Code
select q.Code;
How about
query.Select(s => s.ToString()).ToList();
Or
query.Cast<String>().ToList();
But I'm assuming that q.Code is a string? In which case you just want to change your LINQ expression:
var query = from q in session.Linq<Store>()
where q.Country == country
orderby q.Code
select q.Code;
In the query, instead of selecting an anonymous class containing a string, just select the string itself:
var query = from q in session.Linq<Store>()
where q.Country == country
orderby q.Code
select q.Code;
You can't cast a list of custom types to a list of strings like that. The easiest way would be to have your query object begin it's life as an iEnumerable list of strings, rather than a custom type. Change your select line to:
select new q.Code.toString();
and you'll be good. If q.Code is itself a string to begin with, then the .ToString() won't be necessary.
Try this:
return query.ToList<string>();

Conditional Multiple Fields Searching and Filtering in LINQ

Assuming that we have the following table:
Person:
PersonID,
Name,
Age,
Gender
And we are providing a search function that allows users to search the table according to the name and/or the age.
The tricky part in writing the SQL ( or LINQ) query is that the users can choose to search for both field, or any one field, or no field. If he wants to search for all then he would just have to leave the textbox blank.
The logic to do this can be written as follows:
var p;
if(Name_TextBox=='')
{
p=from row in person
select row ;
}
else
{
p= from row in person
where row.Name=Name_TextBox
select row ;
}
// repeat the same for age
Now after a while the code gets very long and messy... How can I compress the above into a single query with no if-else?
Try code like this
string personName = txtPersonName.Text;
int personAge = Convert.ToInt32(txtAge.Text);
var opportunites = from p in this.DataContext.Persons
select new
{
p.PersonID,
p.Name,
p.Age,
p.Gender
};
if (personsID != 0)
opportunites = opportunites.Where(p => p.PersonID == personID);
if (personName != string.Empty)
opportunites = opportunites.Where(p => p.Name.StartsWith(personName));
if (personAge != 0)
opportunites = opportunites.Where(p => p.Age == personAge);
This will work fine. If personName is not given it will be not add to where, and if given then it will added.
One alternative which I have used in SQL which could be implemented in Linq too is
var p = from p in Person
where p.Name == Name_TextBox || Name_TextBox == String.Empty
select p;
(Note that your 'linq' is using SQL syntax, which won't compile. Also you can't declare a var as you are doing without directly assigning a value)
why not use the null coalescing operator? eg.
var products = from a in context.products
where a.ID == (productID ?? a.ID)
select a;
This works really well on my system

Resources