Skip to content

Commit

Permalink
Added a way to have nested PRIOR conditions when using connect by
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes92 committed Jun 27, 2022
1 parent c068ced commit d414707
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 6 deletions.
5 changes: 4 additions & 1 deletion querydsl-core/src/main/java/com/querydsl/core/types/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public enum Ops implements Operator {
NULLIF(Object.class),

// subquery operations
EXISTS(Boolean.class);
EXISTS(Boolean.class),

// Oracle specific
PRIOR(Object.class);

private final Class<?> type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public String escapeForLike(String str) {
add(Ops.SINGLETON, "{0}", Precedence.LIST);
add(Ops.WRAPPED, "({0})");
add(Ops.ORDER, "order()");
add(Ops.PRIOR, "{0}", Precedence.NOT_HIGH);

// boolean
add(Ops.AND, "{0} && {1}", Precedence.AND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,13 @@ public CaseForEqBuilder<T> when(Expression<? extends T> other) {
return new CaseForEqBuilder<T>(mixin, other);
}

/**
* Create a {@code PRIOR this} expression
*
* @return PRIOR this
*/
public SimpleExpression<T> prior() {
return Expressions.operation(this.getType(), Ops.PRIOR, this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void test() throws IllegalArgumentException, IllegalAccessException {
Ops.COALESCE,
Ops.ORDINAL, // TODO: add support
Ops.MATCHES_IC,
Ops.PRIOR,

// aggregation
Ops.AggOps.AVG_AGG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public OracleTemplates(char escape, boolean quote) {
setCountViaAnalytics(true);
setListMaxSize(1000);

setPrecedence(Precedence.HIGHEST, Ops.PRIOR);
setPrecedence(Precedence.COMPARISON, Ops.EQ, Ops.EQ_IGNORE_CASE, Ops.NE);
setPrecedence(Precedence.COMPARISON + 1, Ops.IS_NULL, Ops.IS_NOT_NULL, Ops.LIKE, Ops.LIKE_ESCAPE, Ops.BETWEEN,
Ops.IN, Ops.NOT_IN, Ops.EXISTS);
Expand All @@ -88,6 +89,9 @@ public OracleTemplates(char escape, boolean quote) {
add(Ops.ALIAS, "{0} {1}");
add(SQLOps.NEXTVAL, "{0s}.nextval");

// Oracle specific
add(Ops.PRIOR, "prior {0}");

// String
add(Ops.INDEX_OF, "instrb({0},{1})-1", Precedence.ARITH_LOW);
add(Ops.INDEX_OF_2ARGS, "instrb({0},{1},{2+'1'})-1", Precedence.ARITH_LOW);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public AbstractOracleQuery(Supplier<Connection> connProvider, Configuration conf
* @param cond condition
* @return the current object
*/

public C connectByPrior(Predicate cond) {
return addFlag(Position.BEFORE_ORDER, CONNECT_BY_PRIOR, cond);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
*/
package com.querydsl.sql.oracle;

import java.util.Date;

import com.querydsl.core.types.dsl.DateExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberExpression;
import java.util.Date;

/**
* Convenience functions and constants for Oracle DB usage
Expand All @@ -35,5 +34,4 @@ private OracleGrammar() { }
public static final NumberExpression<Integer> rowid = Expressions.numberTemplate(Integer.class, "rowid");

public static final DateExpression<Date> sysdate = Expressions.dateTemplate(Date.class, "sysdate");

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void constants() {
assertNotNull(OracleGrammar.level);
assertNotNull(OracleGrammar.rownum);
assertNotNull(OracleGrammar.sysdate);
assertNotNull(OracleGrammar.rowid);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,22 @@ public void connectByPrior() {
toString(query));
}

@Test
public void connectByPriorNew() {
query.connectBy(survey.name.prior().isNull());
assertEquals("from SURVEY survey connect by prior survey.NAME is null order by survey.NAME asc", toString(query));
}

@Test
public void connectByPriorWithSelect() {
query.select(survey.name.prior().as("parent_name")).connectBy(survey.name.prior().isNull().and(survey.name2.isNotNull()));
assertEquals("select prior survey.NAME parent_name from SURVEY survey connect by prior survey.NAME is null and survey.NAME2 is not null order by survey.NAME asc", toString(query));
}

@Test
public void connectBy() {
query.connectByPrior(survey.name.isNull());
assertEquals("from SURVEY survey connect by prior survey.NAME is null order by survey.NAME asc",
query.connectBy(survey.name.isNull());
assertEquals("from SURVEY survey connect by survey.NAME is null order by survey.NAME asc",
toString(query));
}

Expand Down

0 comments on commit d414707

Please sign in to comment.