Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.querydsl.kotlin

import com.querydsl.core.types.Expression
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.core.types.dsl.ComparableExpression

/**
* Null-safe greater-than against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this > value, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.gt(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.gt(value)

/**
* Null-safe greater-than against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this > expr, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.gt(expr: Expression<T>?): BooleanExpression? =
if (this == null || expr == null) null else this.gt(expr)

/**
* Null-safe greater-than-or-equal against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this >= value, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.goe(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.goe(value)

/**
* Null-safe greater-than-or-equal against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this >= expr, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.goe(expr: Expression<T>?): BooleanExpression? =
if (this == null || expr == null) null else this.goe(expr)

/**
* Null-safe less-than against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this < value, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.lt(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.lt(value)

/**
* Null-safe less-than against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this < expr, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.lt(expr: Expression<T>?): BooleanExpression? =
if (this == null || expr == null) null else this.lt(expr)

/**
* Null-safe less-than-or-equal against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this <= value, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.loe(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.loe(value)

/**
* Null-safe less-than-or-equal against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this <= expr, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.loe(expr: Expression<T>?): BooleanExpression? =
if (this == null || expr == null) null else this.loe(expr)

/**
* Null-safe BETWEEN with partial-range support, judging each bound independently.
*
* Both bounds present yields BETWEEN, lower-only yields `>=`, upper-only yields `<=`,
* and neither is skipped.
*
* @param range a `from to to` pair where either bound may be null
* @return this BETWEEN from AND to (or a one-sided `>=` / `<=`), or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.between(range: Pair<T?, T?>): BooleanExpression? {
if (this == null) return null
val (from, to) = range
return when {
from != null && to != null -> this.between(from, to)
from != null -> this.goe(from)
to != null -> this.loe(to)
else -> null
}
}

/**
* Null-safe BETWEEN over a Kotlin [ClosedRange], where both bounds are always present.
*
* @param range the closed range (`from..to`)
* @return this BETWEEN from AND to, or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.between(range: ClosedRange<T>): BooleanExpression? =
this?.between(range.start, range.endInclusive)

/**
* Null-safe NOT BETWEEN, mirroring [between] with partial-range support.
*
* Both bounds present yields NOT BETWEEN, lower-only yields `<`, upper-only yields `>`,
* and neither is skipped.
*
* @param range a `from to to` pair where either bound may be null
* @return this NOT BETWEEN from AND to (or a one-sided `<` / `>`), or null
*/
infix fun <T : Comparable<T>> ComparableExpression<T>?.notBetween(range: Pair<T?, T?>): BooleanExpression? {
if (this == null) return null
val (from, to) = range
return when {
from != null && to != null -> this.notBetween(from, to)
from != null -> this.lt(from)
to != null -> this.gt(to)
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,66 @@ import com.querydsl.core.types.dsl.*
import com.querydsl.core.types.dsl.Expressions.constant

/**
* Get a negation of this boolean expression
* Null-safe negation that skips when this is null.
*
* @return !this
* @return NOT this, or null
*/
operator fun Expression<Boolean>.not() : BooleanExpression {
return Expressions.booleanOperation(Ops.NOT, this)
operator fun Expression<Boolean>?.not() : BooleanExpression? {
return if (this == null) null else Expressions.booleanOperation(Ops.NOT, this)
}

/**
* Get an intersection of this and the given expression
* Null-safe intersection. A null side is skipped: both present -> AND, one present -> that side,
* both null -> null (dropped by where()).
*
* @param predicate right hand side of the union
* @return this and right
* @param predicate right-hand side, or null to skip
* @return this AND predicate (or the present side), or null
*/
infix fun Expression<Boolean>.and(predicate: Expression<Boolean>) : BooleanExpression {
return Expressions.booleanOperation(Ops.AND, this, predicate);
infix fun Expression<Boolean>?.and(predicate: Expression<Boolean>?) : BooleanExpression? {
return when {
this != null && predicate != null -> Expressions.booleanOperation(Ops.AND, this, predicate)
this != null -> Expressions.asBoolean(this)
predicate != null -> Expressions.asBoolean(predicate)
else -> null
}
}

/**
* Get a union of this and the given expression
* Null-safe union. A null side is skipped: both present -> OR, one present -> that side,
* both null -> null.
*
* @param predicate right hand side of the union
* @return this || right
* @param predicate right-hand side, or null to skip
* @return this OR predicate (or the present side), or null
*/
infix fun Expression<Boolean>.or(predicate: Expression<Boolean>) : BooleanExpression {
return Expressions.booleanOperation(Ops.OR, this, predicate);
infix fun Expression<Boolean>?.or(predicate: Expression<Boolean>?) : BooleanExpression? {
return when {
this != null && predicate != null -> Expressions.booleanOperation(Ops.OR, this, predicate)
this != null -> Expressions.asBoolean(this)
predicate != null -> Expressions.asBoolean(predicate)
else -> null
}
}

/**
* Get a union of this and the given expression
* Null-safe XOR that skips when either side is null (XOR has no meaningful one-sided form).
*
* @param predicate right hand side of the union
* @return this || right
* @param predicate right-hand side, or null to skip
* @return this XOR predicate, or null
*/
infix fun Expression<Boolean>.xor(predicate: Expression<Boolean>) : BooleanExpression {
return Expressions.booleanOperation(Ops.XOR, this, predicate);
infix fun Expression<Boolean>?.xor(predicate: Expression<Boolean>?) : BooleanExpression? {
return if (this == null || predicate == null) null
else Expressions.booleanOperation(Ops.XOR, this, predicate)
}

/**
* Get a union of this and the given expression
* Null-safe XNOR that skips when either side is null.
*
* @param predicate right hand side of the union
* @return this || right
* @param predicate right-hand side, or null to skip
* @return this XNOR predicate, or null
*/
infix fun Expression<Boolean>.xnor(predicate: Expression<Boolean>) : BooleanExpression {
return Expressions.booleanOperation(Ops.XNOR, this, predicate);
infix fun Expression<Boolean>?.xnor(predicate: Expression<Boolean>?) : BooleanExpression? {
return if (this == null || predicate == null) null
else Expressions.booleanOperation(Ops.XNOR, this, predicate)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.querydsl.kotlin

import com.querydsl.core.types.Expression
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.core.types.dsl.NumberExpression

// NumberExpression does not extend ComparableExpression, so these comparison helpers are declared
// separately from the ComparableExpression extensions.

/**
* Null-safe greater-than against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this > value, or null
*/
infix fun <T> NumberExpression<T>?.gt(value: T?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || value == null) null else this.gt(value)

/**
* Null-safe greater-than against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this > expr, or null
*/
infix fun <T> NumberExpression<T>?.gt(expr: Expression<T>?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || expr == null) null else this.gt(expr)

/**
* Null-safe greater-than-or-equal against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this >= value, or null
*/
infix fun <T> NumberExpression<T>?.goe(value: T?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || value == null) null else this.goe(value)

/**
* Null-safe greater-than-or-equal against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this >= expr, or null
*/
infix fun <T> NumberExpression<T>?.goe(expr: Expression<T>?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || expr == null) null else this.goe(expr)

/**
* Null-safe less-than against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this < value, or null
*/
infix fun <T> NumberExpression<T>?.lt(value: T?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || value == null) null else this.lt(value)

/**
* Null-safe less-than against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this < expr, or null
*/
infix fun <T> NumberExpression<T>?.lt(expr: Expression<T>?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || expr == null) null else this.lt(expr)

/**
* Null-safe less-than-or-equal against a value, skipping when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this <= value, or null
*/
infix fun <T> NumberExpression<T>?.loe(value: T?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || value == null) null else this.loe(value)

/**
* Null-safe less-than-or-equal against another expression, skipping when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this <= expr, or null
*/
infix fun <T> NumberExpression<T>?.loe(expr: Expression<T>?): BooleanExpression? where T : Number, T : Comparable<T> =
if (this == null || expr == null) null else this.loe(expr)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.querydsl.kotlin

import com.querydsl.core.types.Expression
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.core.types.dsl.SimpleExpression

/**
* Null-safe equality against a value, skipping the condition when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this = value, or null
*/
infix fun <T> SimpleExpression<T>?.eq(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.eq(value)

/**
* Null-safe equality against another expression, skipping the condition when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this = expr, or null
*/
infix fun <T> SimpleExpression<T>?.eq(expr: Expression<in T>?): BooleanExpression? =
if (this == null || expr == null) null else this.eq(expr)

/**
* Null-safe inequality against a value, skipping the condition when either side is null.
*
* @param value the value to compare against, or null to skip
* @return this != value, or null
*/
infix fun <T> SimpleExpression<T>?.ne(value: T?): BooleanExpression? =
if (this == null || value == null) null else this.ne(value)

/**
* Null-safe inequality against another expression, skipping the condition when either side is null.
*
* @param expr the expression to compare against, or null to skip
* @return this != expr, or null
*/
infix fun <T> SimpleExpression<T>?.ne(expr: Expression<in T>?): BooleanExpression? =
if (this == null || expr == null) null else this.ne(expr)

/**
* Null-safe IN that skips the condition when this is null or the collection is null or empty.
*
* @param values the values to match, or null/empty to skip
* @return this IN values, or null
*/
infix fun <T> SimpleExpression<T>?.`in`(values: Collection<T>?): BooleanExpression? =
if (this == null || values.isNullOrEmpty()) null else this.`in`(values)
Loading
Loading