Dynamic Spring #Query with principal and ternary operator - jpql

According to this https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions I should be able construct a conditional query:
#Query("select o from BusinessObject o where o.owner.emailAddress like "+
"?#{hasRole('ROLE_ADMIN') ? '%' : principal.emailAddress}")
List<BusinessObject> findBusinessObjectsForCurrentUser();
What I'm trying to achieve is to change a larger part of a query. This is entirely synthetic example, but it demonstrates the idea:
#Query("select p from Project p where " + "?#{hasRole('ROLE_ADMIN') ? 'p.status='PRIVATE'':p.'status='PUBLIC''}")
public Page<Project> findAll(Pageable pageable);
The query above gives org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ? near line 1
I have tried different combinations with and without ?, ', :, # but still no luck. I suspect that it's either not possible due to the limitations of #Query or I misunderstand the syntax. Any help is appreciated.

Related

Not able to use for loop in ternary operator in arangodb

How do we write conditions in arango, that includes for loops. I can elaborate the requirement below.
My requirement is if a particular attribute(array type) exists in the arango collection, i would read data from the collection(that requires a loop) or else, might do the following :
return null
return empty string ""
do nothing.
Is this possible to achieve in arango?
The helping methods could be -->
-- has(collectionname, attributename)
-- The ternary operator ?:
let attribute1 = has(doc,"attribute1") ?(
for name in doc.attribute1.names
filter name.language == "xyz"
return name.name
) : ""
But this dosent work. Seems like arango compiler first attempts to compile the for loop, finds nulls and reports error as below. Instead, it should have compiled "has" function first for the ternary operator being used.
collection or array expected as operand to FOR loop; you provided a value of type 'null' (while executing)
If there is a better way of doing it, would appreciate the advice!!
Thanks in advance!
Nilotpal
Fakhrany here from ArangoDB.
Regarding your question, this is a known limitation.
From https://www.arangodb.com/docs/3.8/aql/fundamentals-limitations.html:
The following other limitations are known for AQL queries:
Subqueries that are used inside expressions are pulled out of these
expressions and executed beforehand. That means that subqueries do not
participate in lazy evaluation of operands, for example in the ternary
operator. Also see evaluation of subqueries.
Also noted here for the ternary operator:
https://www.arangodb.com/docs/3.8/aql/operators.html#ternary-operator.
An answer to the question what to do may be to use a FILTER before enumerating over the attributes:
FOR doc IN collection
/* the following filter will only let those documents passed in which "attribute1.names" is an array */
FILTER IS_ARRAY(doc.attribute1.names)
FOR name IN doc.attribute1.names
FILTER name.language == "xyz"
RETURN name.name
Other solutions are also possible. Depends a bit on the use case.

Antlr no viable alternative at input when the keyword is POINT

import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.sql.parser.core.parser.SQLParserExecutor;
import org.junit.Test;
import javax.xml.bind.SchemaOutputResolver;
public class T1 {
#Test
public void t1() {
ParseTree parseTree = new SQLParserExecutor("MySQL", "insert into T_NAME (POINT) values (?)").execute().getRootNode();
}
}
This code will report the following error:
line 1:20 no viable alternative at input '(POINT'
When I use other column names, it’s all right, but POINT doesn’t work. Why?
Java project, pom.xml:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.1.1</version>
</dependency>
I suspect that all (MySQL), or more, keywords would trigger this error (POLYGON probably also produces this error). The grammar probably is trying to match an identifier, but since the input POINTS is already matched as a keyword, it fails to match it properly.
Something like this:
insert_stat
: INSERT INTO? table_name '(' column_names ')' ...
;
column_names
: IDENTIFIER ( ',' IDENTIFIER )*
;
Looking at the Github issue, one of the maintainers indicated that the 5.0.0-alpha properly handles this, meaning they probably did something like this to fix it:
insert_stat
: INSERT INTO? table_name '(' column_names ')' ...
;
column_names
: identifier ( ',' identifier )*
;
identifier
: IDENTIFIER
| POINT
| POLYGON
| ...
;
I.e.: they extended the set of valid identifiers inside the parser.
Thanks for #Bart's comment Here. This bug has been fixed from 5.0.0-alpha.
BTW, ShardingSphere supports many mainstream RDBMS, like MySQL, PostgreSQL, SQLServer and Oracle. Therefore you will see a great many g4 files in this repo. Ideally, these definitions in Antlr g4 file are expected to be aligned with the corresponding official SQL docs. So if there is any mismatch, please report that at ISSUES. Thanks a lot.

What is the difference between these Peewee query filter forms

In the description of filtering records in Peewee, there are examples of two alternative syntaxes: using commas to separate multiple conditions, such as the following example,
Tweet.select().where(Tweet.user == user, Tweet.is_published == True)
and using bitwise operators. I cannot figure out (and cannot find a description of) the difference between using the comma syntax and using bitwise operators. What does the comma syntax actually do? From the (single) documented example of using a comma, it seems like it might be equivalent to using &, as in
Tweet.select().where( (Tweet.user == user) & (Tweet.is_published == True) )
Is that the case?
Yes, they are equivalent, as per the code:
def where(self, *expressions):
if self._where is not None:
expressions = (self._where,) + expressions
self._where = reduce(operator.and_, expressions)

What's the mistake in the following query?

I have the following query:
strsql = """SELECT referencia, concepto, sfamilia, pvp, puc FROM almacen2 WHERE concepto like '*YATEKOMO*'"""
I use this sentence in python with the Access Driver.
When I execute this in my code:
strsql = """SELECT referencia, concepto, sfamilia, pvp, puc FROM almacen2"""
I get a lot of results.
And when I try to execute the query with the tag LIKE in Access directly, I get 8 results.
What's my mistake so I get results out of my code and not in it?
Use % instead of *.
Or do RegEx with concepto ~ 'YATEKOMO'
Oops, I can see that this is about ACCESS. My answer fits more to PostgreSQL.

How to group "And" & "Or" in a single query statement using subsonic 2x

I am having some trouble in converting the following
SELECT * FROM foo f WHERE
(f.name = 'name' AND f.date = '1980/02/2001')
OR
(f.name = 'another name' AND f.date = '1990/02/2001')
to
new Select().From(Foo.Schema.TableName)
.Where(Foo.Columns.Name).IsEqualTo('name')
.And(Foo.Columns.Date).IsEqualTo('1980/02/2001')
.Or(Foo.Columns.Name).IsEqualTo('another name')
.And(Foo.Columns.Date).IsEqualTo('1990/02/2001')
as you can see I am not aware of the method which can isolate two groups of "AND" and put each of them in an "OR" statement.
I would really appreciate your help in this context.
You need to wrap the parts in barckets using the .AndExpression() and .CloseExpression()
See: http://biasecurities.com/2008/07/complex-sql-conditional-statements-with-subsonic-2-1/ for examples

Resources