Skip to content

Commit b2d6cea

Browse files
authored
Add suport for NULLS FIRST and NULLS LAST syntax in ORDER BY clauses (#44)
1 parent 5f5cd89 commit b2d6cea

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,21 @@ defmodule Ecto.Adapters.SQLite3.Connection do
10071007
:asc ->
10081008
str
10091009

1010+
:asc_nulls_last ->
1011+
[str | " ASC NULLS LAST"]
1012+
1013+
:asc_nulls_first ->
1014+
[str | " ASC NULLS FIRST"]
1015+
10101016
:desc ->
10111017
[str | " DESC"]
10121018

1019+
:desc_nulls_last ->
1020+
[str | " DESC NULLS LAST"]
1021+
1022+
:desc_nulls_first ->
1023+
[str | " DESC NULLS FIRST"]
1024+
10131025
_ ->
10141026
raise Ecto.QueryError,
10151027
query: query,

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -534,19 +534,23 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
534534

535535
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
536536

537-
for dir <- [:asc_nulls_first, :asc_nulls_last, :desc_nulls_first, :desc_nulls_last] do
538-
assert_raise(
539-
Ecto.QueryError,
540-
~r"#{dir} is not supported in ORDER BY in SQLite3",
541-
fn ->
542-
Schema
543-
|> order_by([r], [{^dir, r.x}])
544-
|> select([r], r.x)
545-
|> plan()
546-
|> all()
547-
end
548-
)
549-
end
537+
query =
538+
Schema
539+
|> order_by([r], asc_nulls_first: r.x, desc_nulls_first: r.y)
540+
|> select([r], r.x)
541+
|> plan()
542+
543+
assert all(query) ==
544+
~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x" ASC NULLS FIRST, s0."y" DESC NULLS FIRST}
545+
546+
query =
547+
Schema
548+
|> order_by([r], asc_nulls_last: r.x, desc_nulls_last: r.y)
549+
|> select([r], r.x)
550+
|> plan()
551+
552+
assert all(query) ==
553+
~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x" ASC NULLS LAST, s0."y" DESC NULLS LAST}
550554
end
551555

552556
test "union and union all" do

0 commit comments

Comments
 (0)