jOOQ Dynamic Number of WITH Clauses - java

I am playing around with jOOQ and nesting queries. I have a JSON payload which may contain many subqueries. I want to treat these subqueries as a variable number of Common Table Expressions (i.e. CTE's in the WITH clause). Currently I have this working example, but it is static in terms of the number of CTE's it includes. How would I accomplish a variable number of CTE's within the WITH Clause?
/*
+-----------------+
|sum_of_everything|
+-----------------+
| 100|
+-----------------+
*/
Supplier<Stream<Map<String, Object>>> resultsWith =
() ->
dslContext
.with("s1")
.as(query)
.select(sum(field("1", Integer.class)).as("sum_of_everything"))
.from(table(name("s1")))
.fetchStream()
.map(Record::intoMap);
Ultimately I will need to deserialize the JSON payload to ensure that the CTE reference hierarchy works properly, and I will need to see if jOOQ supports referencing one CTE in another CTE before selecting the final result. I will need to accomplish something like this :
/*
+-----------------+
|sum_of_everything|
+-----------------+
| 100|
+-----------------+
*/
Supplier<Stream<Map<String, Object>>> resultsWith =
() ->
dslContext
.with("s1").as(query1)
.with("s2").as(query2) // should be able to reference "s1" i.e. query1
...
.with("sNMinus1").as(queryNMinus1)
.with("sN").as(queryN) // should be able to reference any upstream CTE
.select(sum(field("1", Integer.class)).as("sum_of_everything"))
.from(table(name("sN")))
.fetchStream()
.map(Record::intoMap);

You can create a CommonTableExpression instance starting out from a Name, using Name.as(Select) e.g.
CommonTableExpression<?> s1 = name("s1").as(query1);
CommonTableExpression<?> s2 = name("s2").as(query2);
// And then (or, of course, use a Collection)
dslContext.with(s1, s2, ..., SN)

Related

Java generate queryDSL

So I have a table named BLOG_REPORTERS like:
blog_id | reporter_id
1 1
2 3
And REPORTER:
reporter_id | name | etc
1 asd etc
I need to perform JOIN operation between them. I generated a QueryDSL class for them, and tried something like:
new JPAQuery<>(entityManager)
.select(QReporter.reporter)
.from(QReporter.reporter)
.join(QBlogReporters.blogreporters)
but this is wrong because join() method accepts EntityPath<P> and QBlogReporters extends BeanPath<T>.
Is any way to do this?

Postgresql Array Functions with QueryDSL

I use the Vlad Mihalcea's library in order to map SQL arrays (Postgresql in my case) to JPA. Then let's imagine I have an Entity, ex.
#TypeDefs(
{#TypeDef(name = "string-array", typeClass =
StringArrayType.class)}
)
#Entity
public class Entity {
#Type(type = "string-array")
#Column(columnDefinition = "text[]")
private String[] tags;
}
The appropriate SQL is:
CREATE TABLE entity (
tags text[]
);
Using QueryDSL I'd like to fetch rows which tags contains all the given ones. The raw SQL could be:
SELECT * FROM entity WHERE tags #> '{"someTag","anotherTag"}'::text[];
(taken from: https://www.postgresql.org/docs/9.1/static/functions-array.html)
Is it possible to do it with QueryDSL? Something like the code bellow ?
predicate.and(entity.tags.eqAll(<whatever>));
1st step is to generate proper sql: WHERE tags #> '{"someTag","anotherTag"}'::text[];
2nd step is described by coladict (thanks a lot!): figure out the functions which are called: #> is arraycontains and ::text[] is string_to_array
3rd step is to call them properly. After hours of debug I figured out that HQL doesn't treat functions as functions unless I added an expression sign (in my case: ...=true), so the final solution looks like this:
predicate.and(
Expressions.booleanTemplate("arraycontains({0}, string_to_array({1}, ',')) = true",
entity.tags,
tagsStr)
);
where tagsStr - is a String with values separated by ,
Since you can't use custom operators, you will have to use their functional equivalents. You can look them up in the psql console with \doS+. For \doS+ #> we get several results, but this is the one you want:
List of operators
Schema | Name | Left arg type | Right arg type | Result type | Function | Description
------------+------+---------------+----------------+-------------+---------------------+-------------
pg_catalog | #> | anyarray | anyarray | boolean | arraycontains | contains
It tells us the function used is called arraycontains, so now we look-up that function to see it's parameters using \df arraycontains
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------------+------------------+---------------------+--------
pg_catalog | arraycontains | boolean | anyarray, anyarray | normal
From here, we transform the target query you're aiming for into:
SELECT * FROM entity WHERE arraycontains(tags, '{"someTag","anotherTag"}'::text[]);
You should then be able to use the builder's function call to create this condition.
ParameterExpression<String[]> tags = cb.parameter(String[].class);
Expression<Boolean> tagcheck = cb.function("Flight_.id", Boolean.class, Entity_.tags, tags);
Though I use a different array solution (might publish soon), I believe it should work, unless there are bugs in the underlying implementation.
An alternative to method would be to compile the escaped string format of the array and pass it on as the second parameter. It's easier to print if you don't treat the double-quotes as optional. In that event, you have to replace String[] with String in the ParameterExpression row above
For EclipseLink I created a function
CREATE OR REPLACE FUNCTION check_array(array_val text[], string_comma character varying ) RETURNS bool AS $$
BEGIN
RETURN arraycontains(array_val, string_to_array(string_comma, ','));
END;
$$ LANGUAGE plpgsql;
As pointed out by Serhii, then you can useExpressions.booleanTemplate("FUNCTION('check_array', {0}, {1}) = true", entity.tags, tagsStr)

Can I search by multiple fields using the Elastic Search Java API?

Example:
|studentname | maths | computers |
++++++++++++++++++++++++++++++++++
|s1 |78 |90 |
==================================
|s2 |56 |75 |
==================================
|s3 |45 |50 |
==================================
The above table represents data that is present in Elasticsearch.
Consider that the user enters 60 and above then Elasticsearch should be able to display only s1, because he is the only student who has scored more than 60 in both subjects.
How do I solve it using Java API?
NOTE: I was able to find out for individual subjects by:
QueryBuilder query = QueryBuilders.boolQuery()
.should(
QueryBuilders.rangeQuery(maths)
.from(50)
.to(100)
)
You can have multiple .should() clauses in the the bool query. So in your case:
QueryBuilder query = QueryBuilders.boolQuery()
.should(
QueryBuilders.rangeQuery(maths)
.from(61)
.to(100)
)
.should(
QueryBuilders.rangeQuery(computers)
.from(61)
.to(100)
)
Note:
RangeQueryBuilder (returned from QueryBuilders.rangeQuery()) also has the method #gte().
According to the docs: "In a boolean query with no must or filter clauses, one or more should clauses must match a document." So if you have have no filter or must, you might not get the desired behavior. The above answer assumes you are using some other clauses. The point is you can combine clauses in the bool query. With the Java API this just means using the clause repeatedly.
You might want to use filter instead of should. This will lead to faster execution and caching (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)

Stored procedures in jooq with dynamic names

I want to call stored procedures from PostgreSQL in JOOQ by name dynamically:
final Field function = function("report_" + name, Object.class, (Field[])params.toArray(new Field[params.size()]));
dsl().select(function).fetchArrays();
For example it generates:
select report_total_requests('83.84.85.3184');
Which returns:
report_total_requests
-----------------------
(3683,2111,0)
(29303,10644,1)
And in java it is array of "(3683,2111,0)" objects.
I want to generate:
select * from report_total_requests('83.84.85.3184')
To produce:
total | users | priority
------+-------+----------
3683 | 2111 | 0
29303 | 10644 | 1
That is in java array of arrays of objects
Any ideas?
The way forward is to use plain SQL as follows:
Name name = DSL.name("report_" + name);
QueryPart arguments = DSL.list(params);
dsl().select().from("{0}({1})", name, arguments).fetch();
Note, I've wrapped the function name in a DSL.name() object, to prevent SQL injection.

Order Page by different Date Properties with Spring Data

I am using Spring Data to access a SQL Database with mapped objects.
I have got an object which has multiple date properties which are relevant in different states of the object.
public class Foo {
Date deadlineOne; // relevant in State.ONE
Date deadlineTwo; // relevant in State.TWO
Date deadlineThree; // relevant in State.THREE
State state; // implemented via persisted Enum
}
Now, in the UI I want to display these objects with a property called something like 'Next Deadline' and also be able to sort by this aspect. This 'Next Deadline' should always be the earliest Date relevant by the related state. So for example, if I have the following objects:
O1{state:ONE, deadlineOne: '2016-06-14', deadlineTwo: '2016-06-16', deadlineThree: '2016-06-19'}
O2{state:TWO, deadlineOne: '2016-06-12', deadlineTwo: '2016-06-13', deadlineThree: '2016-06-20'}
O3{state:THREE, deadlineOne: '2016-06-15', deadlineTwo: '2016-06-18', deadlineThree: '2016-06-21'}
I want them to be displayed like this when sorted by 'Next Deadline'
Object | Next Deadline | State
O2 | 2016-06-13 | TWO
O1 | 2016-06-14 | ONE
O3 | 2016-06-21 | THREE
I just can not figure out how to implement this functionality with Spring Data. Up to this point I have been using a PageRequest and got basic sorting working by just passing a property of my object. But that way I can not mix the sorting-properties the way I need to.
What I've tried so far
Implementing this functionality via JPA Query Annotaion using SQL Case syntax like this in a JpaRepository interface:
#Query("SELECT f FROM Foo f ORDER BY CASE
WHEN f.state = State.ONE THEN f.deadlineOne
WHEN f.state = State.TWO THEN f.deadlineTwo
WHEN f.state = State.THREE THEN f.deadlineThree")
Page<Foo> findSortedByNextDeadline(Pageable pageable);
The interpreter just cant seem to accept this and gives no useful error information.
Question
Is there any way to make this work with Spring Data and the right Query/PageRequest or do I have to maintain a seperate nextDeadline property or something of that matter?
Alright I got it working, and here is how:
Like Dimpre Jean-Sébastien pointed out in his comment I was missing the END keyword. I also had to add an explicit #Enumerated(EnumType.ORDINAL) to my state-property (which is only relevant in my particular case, its should work with String types just aswell). Also I referenced the Enum for my state with its full package name. In the end it looked something like this:
#Query("SELECT f FROM Foo f ORDER BY CASE
WHEN f.state = com.package.State.ONE THEN f.deadlineOne
WHEN f.state = com.package.State.TWO THEN f.deadlineTwo
WHEN f.state = com.package.State.THREE THEN f.deadlineThree
ELSE f.deadlineOne END")
Page<Foo> findSortedByNextDeadline(Pageable pageable);

Categories

Resources