querydsl / querydsl

Unified Queries for Java
https://querydsl.com
Apache License 2.0
4.74k stars 873 forks source link

querydsl 4 does not support calling isNull() on a SQLQuery #1561

Closed robertandrewbain closed 9 years ago

robertandrewbain commented 9 years ago

In querydsl 3 you could write a subquery and call isNull() on it. However it took me a while to figure out how to do it using querydsl 4. I think the API could be improved.

To demonstrate, I was trying to do something like this:

    private SQLQuery<String> getSomeSubquery() {
        return query()
                .select(someTable.someColumn)
                .from(someTable)
                .where(someTable.someColumn.eq("someValue"))
                .distinct();
    }

    private StringExpression getSomeCaseStatementForSomeSubquery() {
        return new CaseBuilder()
                .when(getSomeSubquery().isNull())
                .then("someValue")
                .otherwise("someOtherValue");
    }

The problem is that isNull() is not present. Looking at the isNull() method in SimpleExpression I realised it was calling Expressions.booleanOperation(Ops.IS_NULL, mixin) which led me to look at the usages of Ops.IS_NULL and discover ExpressionUtils public static Predicate isNull(Expression<?> left) method, which done the trick.

However, I think it should go in ExtendedSubQuery as BooleanExpression isNull(Expression<? extends T> expr).

timowest commented 9 years ago

What did you try? Didn't the following work?

select(select(fooExpr).from(someTable)).from(someOtherTable)
robertandrewbain commented 9 years ago

Hi Timo, in response to your question, I was completely barking up the wrong tree last night - long day. I've updated the issue and its title as I believe I've got to the bottom of the problem I was experiencing. Thanks as always for your help.

timowest commented 9 years ago

@robertandrewbain Feel free to provide a PR for that.

robertandrewbain commented 9 years ago

@timowest no problem, I'll attempt to get that done today. I'll put in isNotNull() too, for completeness.