Subselect in JPQL - jpql

Looking to perform a possible subselect in JPQL. I use SQL often and looking to perform a query like this:
Select ((Select SUM(e.hours.hours) from app$Time e
where e.type = 'Purchased')
-
(Select SUM(f.hours.hours) from app$Time f
where f.type = 'Used'))
I have not seen many examples of JPQL performing a query like this, hoping that this is possible.

Jay,
JPA 2.1 specification doesn't support subquery in the select list.
From the specification:
The SELECT clause can contain one or more of the following elements: an identification variable that
ranges over an abstract schema type, a single-valued path expression, a scalar expression, an aggregate
expression, a constructor expression.
Workaround: you can perform two JPQL queries and subtract the Used from the Purchased in the java code

Related

How do I select token value using com.datastax.oss.driver.api.querybuilder.QueryBuilder?

How to rewrite
select token(id) as t, id from example
using com.datastax.oss.driver.api.querybuilder.QueryBuilder class?
TOKEN() is a native CQL function which you cannot call with the QueryBuilder. It also doesn't make sense to do so.
If you already have a static CQL query, you should just execute it. The general proposition for the Query builder in the Java driver is to generate dynamic CQL queries. It isn't a replacement for static statements. Cheers!
It should be possible to do as described in docs, something like this:
selectFrom("example").function("token", Selector.column("id")).column("id");

What are the differences between a JPQL-Injection and SQL-Injection

I have read about JPQL injection and SQL injection. In many sites it has been said that ORM injection is almost as same as SQL injection in a testers point of view. So what basically i want to know is the major differences between JPQL and SQL injections.
Both JPQL injection and SQL injection are examples of the broader category of Code Injection.
Any language that is parsed at runtime is susceptible to Code Injection.
JPQL or Java Persistence Query Language is similar to SQL in syntax, and in the fact that it is written as strings and parsed at runtime.
Building queries by passing JPQL query strings directly to the createQuery method, as shown above, is referred to in JPA as dynamic query construction because the query string can be built dynamically at runtime.
When the description says "built dynamically at runtime" they mean your code formats the JPQL query as a Java string, then submits the string to be parsed and executed. Therefore your code has an opportunity to combine fixed strings with variable content.
Here's an example of using parameters safely to combine a variable with a JPQL statement. This comes from https://www.objectdb.com/java/jpa/query/parameter
SAFE:
TypedQuery<Country> query = em.createQuery(
"SELECT c FROM Country c WHERE c.name = :name", Country.class);
return query.setParameter("name", name).getSingleResult();
Here's the same query written in an unsafe way, combining the variable directly into the string.
UNSAFE:
TypedQuery<Country> query = em.createQuery(
"SELECT c FROM Country c WHERE c.name = '" + name + "'", Country.class);
Don't use string concatenation to form JPQL queries if you can avoid it. That's how unsafe content sneaks into your JPQL.

Pass Dynamic values to the rules in ANTLR4 grammar

I am newbie to ANTLR4
I want to write a grammar that would parse the syntax using the values which it reads dynamically.
Say my grammar is as follows in image
I need help such the HANDLERID not only takes the values mentioned,but a list of values based on a function call,dynamic values. For example a function return list containing {'ACD','GHY','XYZ' ..}. Not to confuse with identifier,these values are names of some defined set of objects, so writing a grammar for IDENTIFIER is not solution.
Any help is appeciated.
Maybe actions are a viable solution? These are written in the target language and allow to do all kind of processing. Formulated as a predicate (appending a ? to the action block) they can even be used to guide the parser what path to take.
Here's a typical form:
decl: type ID ';' { System.out.println("found a decl"); };
or as a predicate:
HANDLERID: ID { isSpecialWord($ID.text) }?;
which will only be matched for IDs that your internal function isSpecialWord is returning true for. So essentially, you are not passing the lexer rule some values, but you do the evaluation in internal code.

Mapping IQueryable class from Automapper

Does Automapper works with IQueryable?
I have 2 Query
IQueryable<V_ImageUpload> Query1;
IQueryable<V_ImageUpload> Query2;
Mapper.CreateMap<IQueryable<V_ImageUpload_WithReceiptBackup>, IQueryable<V_ImageUpload>>();
Query1 = Mapper.Map<IQueryable<V_ImageUpload_WithReceiptBackup>, IQueryable<V_ImageUpload>>(Query2);
Exception occured is:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary
I've not used AutoMapper (so this was an excuse to try it out!), but there are queryable extensions available. Both your queryables are of the same type, so I'm not exactly sure what you are trying to achieve, but perhaps something like this is what you want, which takes an IQueryable<V_ImageUpload_WithReceiptBackup> and converts it to an IQueryable<V_ImageUpload>:
IQueryable<V_ImageUpload_WithReceiptBackup> query1;
IQueryable<V_ImageUpload> query2;
// Only map the actual type, not the queryable types
Mapper.CreateMap<V_ImageUpload_WithReceiptBackup, V_ImageUpload>();
query2 = query1.Project().To<V_ImageUpload>();
The .Project().To<V_ImageUpload>() keeps it as IQueryable, while Mapper.Map would end up with a List/IEnumerable. I only tested this out with LINQ to Objects, but hopefully it works with Entity Framework, or whatever you are using.

How to Inner Join an UDF Function with parameters using SubSonic

I need to query a table using FreeTextTable (because I need ranking), with SubSonic. AFAIK, Subsonic doesn't support FullText, so I ended up creating a simple UDF function (Table Function) which takes 2 params (keywords to search and max number of results).
Now, how can I inner join the main table with this FreeTextTable?
InlineQuery is not an option.
Example:
table ARTICLE with fields Id, ArticleName, Author, ArticleStatus.
The search can be done by one of more of the following fields: ArticleName (fulltext), Author (another FullText but with different search keywords), ArticleStatus (an int).
Actually the query is far more complex and has other joins (depending on user choice).
If SubSonic cannot handle this situation, probably the best solution is good old plain sql (so there would be no need to create an UDF, too).
Thanks for your help
ps: will SubSonic 3.0 handle this situation?
3.0 can do this for you but you'd need to make a template for it since we don't handle functions (yet) out of the box. I'll be working on this in the coming weeks - for now I don't think 2.2 will do this for you.
I realize your question is more complex than this, but you can get results from a table valued function via SubSonic 2.2 with a little massaging.
Copy the .cs file from one of your generated views into a safe folder, and then change all the properties to match the columns returned by your UDF.
Then, on your collection, add a constructor method with your parameters and have it execute an InlineQuery.
public partial class UDFSearchCollection
{
public UDFSearchCollection(){}
public UDFSearchCollection(string keyword, int maxResults)
{
UDFSearchCollection coll = new InlineQuery().ExecuteAsCollection<UDFSearchCollection>("select resultID, resultColumn from dbo.udfSearch(#keyword, #maxResults)",keyword,maxResults);
coll.CopyTo(this);
coll = null;
}
}
public partial class UDFSearch : ReadOnlyRecord<UDFSearch>, IReadOnlyRecord
{
//all the methods for read only record go here
...
}
An inner join would be a little more difficult because the table object doesn't have it's own parameters collection. But it could...

Resources