Christiano
01/21/2024, 12:35 PMifnull
.
SELECT * FROM Projects
WHERE (
id = IFNULL((SELECT max(id) FROM Projects WHERE id < 4),(SELECT id FROM Projects ORDER BY id DESC LIMIT 1))
)
Is there something else I can do, to still obtain the same result? 🤔Jaap Beetstra
01/22/2024, 2:53 PMclass IfNull<T>(private val nullable: Expression<out T?>, private val alternative: Expression<out T>) :
Expression<T>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
append("IFNULL(", nullable, ",", alternative, ")")
}
}
Depending on your exact need, you may also need to wrap a subquery to an expression.Christiano
01/22/2024, 10:30 PMval query = """
SELECT * FROM Projects
WHERE (
id = IFNULL((SELECT max(id) FROM Projects WHERE id < $id),(SELECT id FROM Projects ORDER BY id DESC LIMIT 1))
)
""".trimIndent()
val newId = transaction {
var newTempId: Int? = null
exec(query, explicitStatementType = StatementType.SELECT) {
while (it.next()) {
newTempId = it.getInt("id")
}
}
newTempId
}
Chantal Loncle
01/24/2024, 7:18 PMIFNULL
. The closest out-of-the-box option might be coalesce()
, but if that's not appropriate for your use case, then a custom expression is the best option.
If you really only need to compare subqueries, here's an alternative that should produce expected results:
class IfNull<T>(
private val nullable: AbstractQuery<*>,
private val alternative: AbstractQuery<*>
) : Expression<T>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder {
append("IFNULL((")
nullable.prepareSQL(this)
append("), (")
alternative.prepareSQL(this)
append("))")
}
}
fun <T> ifNull(nullable: AbstractQuery<*>, query: AbstractQuery<*>): IfNull<T> = IfNull(nullable, query)
// example use
projects.selectAll().where { projects.id eq ifNull(query1, query2) }
Christiano
01/26/2024, 8:43 PMIFNULL
because that was the first thing I found, but by deploying my backend to Render (which has a PostgreSQL database) I had to use coalesce
as the syntax does not include ifnull
.
I might try what you suggested, but for now it's working fine with a raw query 😄