ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

Query.limit() should honor 0 arguments for offset and limit

Open ragnese opened this issue 3 years ago • 2 comments

Ktorm Version: 3.5.0 Source File: org/ktorm/dsl/Query.kt Line: 398 Function definition:

public fun Query.limit(offset: Int?, limit: Int?): Query {
    return this.withExpression(
        when (expression) {
            is SelectExpression -> expression.copy(
                offset = offset?.takeIf { it > 0 } ?: expression.offset,
                limit = limit?.takeIf { it > 0 } ?: expression.limit
            )
            is UnionExpression -> expression.copy(
                offset = offset?.takeIf { it > 0 } ?: expression.offset,
                limit = limit?.takeIf { it > 0 } ?: expression.limit
            )
        }
    )
}

I think the takeIf predicates should be { it >= 0 } instead of strictly >, especially for offset. I believe it is valid (at least in MySQL) to have limit and/or offset equal to 0.

When using business logic to algorithmically build a query to be executed, I ended up calling this function with zeros, but received unexpected results.

ragnese avatar Aug 23 '22 14:08 ragnese

Yes, I agree with you, but this change is incompatible, it should be made in the next major version, not 3.x.x.

vincentlauvlwj avatar Aug 31 '22 12:08 vincentlauvlwj

Any news regarding this? Do I need to make a hack for it currently?

mjovanc avatar Mar 04 '23 21:03 mjovanc