erlangbureau / jamdb_oracle

Oracle Database driver for Erlang
MIT License
106 stars 48 forks source link

Maybe build query into DB error with limit and offset #109

Closed thachtan closed 2 years ago

thachtan commented 2 years ago

Hi team,

I have unexpected result when I call query with limit offset. Particularly, I can not get true limit, and offset not match too.

I have built repo to check .

Example:

When i call function get User with limit 5, page 5 (limit 5, offset 20) and I get 20 user from current offset 5 (I think it may be swap when build)

Result when I call.

Screen Shot 2022-04-07 at 10 55 57 Screen Shot 2022-04-07 at 10 56 10
vstavskyi commented 2 years ago

Ecto.Query has limit anf offset functions. Where is offset in your code? example from(p in Post, limit: 10, offset: 30)

thachtan commented 2 years ago

You can check in module UserService and list_users function.

Screen Shot 2022-04-07 at 13 00 54

I put offset in criteria via page when build query.

vstavskyi commented 2 years ago

... OFFSET :2 ... NEXT :1 ... Ecto.Query.Builder generates sql in that order. :( Well, sadly, you need to use constant value for offset or limit for now.

There is no test cases, btw, for postgres or mysql with parameters for offset and limit, they test constant values only.

thachtan commented 2 years ago

I used limit and offset function to build dynamic query.

Screen Shot 2022-04-07 at 16 40 41

Can you show me example to change them?

vstavskyi commented 2 years ago

It looks good.

https://github.com/erlangbureau/jamdb_oracle/blob/d39753d331aacaea8d5df2cb2ecfef0db702f053/test/jamdb_oracle_test.exs#L459

    query = Schema |> offset([r], ^5) |> limit([r], ^3) |> select([], true) |> plan()
    assert all(query) == ~s{SELECT 1 FROM schema s0 OFFSET :2 ROWS FETCH NEXT :1 ROWS ONLY}

    query = Schema |> offset([r], 5) |> limit([r], ^3) |> select([], true) |> plan()
    assert all(query) == ~s{SELECT 1 FROM schema s0 OFFSET 5 ROWS FETCH NEXT :1 ROWS ONLY}

    query = Schema |> offset([r], ^5) |> limit([r], 3) |> select([], true) |> plan()
    assert all(query) == ~s{SELECT 1 FROM schema s0 OFFSET :1 ROWS FETCH NEXT 3 ROWS ONLY}
thachtan commented 2 years ago

Thank you for help. I hope I can use dynamic soon.

Now I swap limit, offset to work around ^^

Screen Shot 2022-04-07 at 17 37 02