Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-227 Slice() with empty list creates invalid SQL #1899

Merged
merged 1 commit into from
Nov 27, 2023

Conversation

bog-walk
Copy link
Member

Currently providing an empty list to slice() results in a SELECT statement without any columns defined, which returns syntax exceptions in all databases except PostgreSQL and H2 (these return empty results).

Adding a check to Query.prepareSQL() allows an early fail now.

Providing an empty list to slice() resulted in a SELECT statement without any
columns defined, which returned syntax exceptions in all databases except
PostgreSQL and H2 (these returned empty results).

Adding require() to the query prepareSQL() allows an early fail in this
situation. The check could also go directly in an init block in the Slice
class. It could not go directly in the function itself, in the edge case
that a user uses Slice directly.
@bog-walk bog-walk requested review from e5l and joc-a November 25, 2023 00:16
Comment on lines +108 to +109
require(set.fields.isNotEmpty()) { "Can't prepare SELECT statement without columns or expressions to retrieve" }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check cannot be placed directly inside the slice() because that would not prevent the edge case of a user creating a Slice instance instead of calling the method (it's odd but is possible):

MyTable.slice(emptyList()).selectAll()
// vs
Slice(MyTable, emptyList()).selectAll()

So the alternative location is to put the check inside Slice:

class Slice(override val source: ColumnSet, override val fields: List<Expression<*>>) : FieldSet {
    init {
        require(fields.isNotEmpty()) { "Can't prepare SELECT statement without columns or expressions to retrieve" }
    }
}

@bog-walk bog-walk merged commit bb2c933 into main Nov 27, 2023
3 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-slice-with-empty-list branch November 27, 2023 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants