ufoss-org/kotysa

Add a new query syntax for complex queries with programmatic conditions

Closed this issue · 0 comments

Current API tries to mimic the SQL syntax, but this leads to type problems when programmatic conditions are needed when buiding the query, with statements that are in if blocks.

For example the first order by clause have to use orderByDesc or orderByAsc, and the following will use andDesc or andAsc. The same syntax and type problem occurs for selects, froms, wheres ...

Here is a code example with the current DSL : I have to duplicate the order by desc on Messages.creationTime, that is always needed, because of the Kotysa type problem (OrderBy VS OrderByPart2).

val orderByQuery: CoroutinesSqlClientSelect.OrderByPart2<List<Any?>> = if (hasForUserId) {
    // Messages from subscribed Users messages are returned first
    (whereQuery orderByDesc QueryAlias<Boolean>("subscribed")
        andDesc Messages.creationTime)
} else {
    whereQuery orderByDesc Messages.creationTime
}

Kotysa can provide a new query syntax that fits better with conditions, this is the previous order by example with this new syntax

var orderByQuery = whereQuery.orderBys()

if (hasForUserId) {
    // Messages from subscribed Users messages are returned first
    orderByQuery = orderByQuery orderByDesc QueryAlias<Boolean>("subscribed")
}
orderByQuery = orderByQuery orderByDesc Messages.creationTime

This new syntax would not replace the current one, that is nice for non-conditional queries, it will add a new simple way of writing complex conditional queries !

Note : The conditional selects() will require to add a new behavior, because now Kotysa returns a single value for single column/table select, a Pair<T, U> for two values, a Triple<T, U, V> for three values and a List<Any?> for 4 and more values. It does not fit well to conditional selects, that may return any number of selected columns.
When using the new selects() syntax, Kotysa will always return a List<Any?>.

Note 2 : The new syntax that is proposed here may be a little different when implementation will be done, this issue will be updated then.