Skip to content

Commit

Permalink
chore: Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
joc-a committed Jul 2, 2024
1 parent 90ad9bb commit 212353a
Showing 1 changed file with 13 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,7 @@ interface ISqlExpressionBuilder {
/** Checks if this [EntityID] expression is less than some [t] value. */
@JvmName("lessEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.less(t: T): LessOp =
LessOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
LessOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is less than some [other] expression. */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.less(
Expand All @@ -428,18 +417,7 @@ interface ISqlExpressionBuilder {
/** Checks if this [EntityID] expression is less than or equal to some [t] value */
@JvmName("lessEqEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.lessEq(t: T): LessEqOp =
LessEqOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
LessEqOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is less than or equal to some [other] expression */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.lessEq(
Expand All @@ -463,18 +441,7 @@ interface ISqlExpressionBuilder {
/** Checks if this [EntityID] expression is greater than some [t] value. */
@JvmName("greaterEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.greater(t: T): GreaterOp =
GreaterOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
GreaterOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is greater than some [other] expression. */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.greater(
Expand All @@ -498,18 +465,7 @@ interface ISqlExpressionBuilder {
/** Checks if this [EntityID] expression is greater than or equal to some [t] value */
@JvmName("greaterEqEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.greaterEq(t: T): GreaterEqOp =
GreaterEqOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
GreaterEqOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is greater than or equal to some [other] expression */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.greaterEq(
Expand All @@ -528,27 +484,7 @@ interface ISqlExpressionBuilder {

/** Returns `true` if this [EntityID] expression is between the values [from] and [to], `false` otherwise. */
fun <T : Comparable<T>, E : EntityID<T>?> Column<E>.between(from: T, to: T): Between =
Between(
this,
wrap(
EntityID(
from,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
),
wrap(
EntityID(
to,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
Between(this, wrap(EntityID(from, this.idTable())), wrap(EntityID(to, this.idTable())))

/** Returns `true` if this expression is null, `false` otherwise. */
fun <T> Expression<T>.isNull(): IsNullOp = IsNullOp(this)
Expand All @@ -566,18 +502,7 @@ interface ISqlExpressionBuilder {
/** Checks if this expression is equal to some [t] value, with `null` treated as a comparable value */
@JvmName("isNotDistinctFromEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.isNotDistinctFrom(t: T): IsNotDistinctFromOp =
IsNotDistinctFromOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
IsNotDistinctFromOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is equal to some [other] expression */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.isNotDistinctFrom(
Expand All @@ -599,18 +524,7 @@ interface ISqlExpressionBuilder {
/** Checks if this expression is not equal to some [t] value, with `null` treated as a comparable value */
@JvmName("isDistinctFromEntityID")
infix fun <T : Comparable<T>> Column<EntityID<T>>.isDistinctFrom(t: T): IsDistinctFromOp =
IsDistinctFromOp(
this,
wrap(
EntityID(
t,
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
)
)
)
IsDistinctFromOp(this, wrap(EntityID(t, this.idTable())))

/** Checks if this [EntityID] expression is not equal to some [other] expression */
infix fun <T : Comparable<T>, E : EntityID<T>?, V : T?> ExpressionWithColumnType<E>.isDistinctFrom(
Expand Down Expand Up @@ -1015,6 +929,12 @@ interface ISqlExpressionBuilder {

fun ExpressionWithColumnType<Int>.intToDecimal(): NoOpConversion<Int, BigDecimal> =
NoOpConversion(this, DecimalColumnType(precision = 15, scale = 0))

private fun <T : Comparable<T>, E : EntityID<T>> Column<out E?>.idTable(): IdTable<T> =
when (val table = this.foreignKey?.targetTable ?: this.table) {
is Alias<*> -> table.delegate
else -> table
} as IdTable<T>
}

/**
Expand Down

0 comments on commit 212353a

Please sign in to comment.