Skip to content

Commit

Permalink
Add bitwise function and better handling parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigitsu committed Nov 25, 2023
1 parent df5f1ab commit b640437
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@ if Code.ensure_loaded?(Postgrex) do
and: " AND ",
or: " OR ",
ilike: " ILIKE ",
like: " LIKE "
like: " LIKE ",
band: " & ",
bor: " | ",
bxor: " # ",
bsl: " << ",
bsr: " >> "
]

@binary_ops Keyword.keys(binary_ops)
Expand Down Expand Up @@ -880,8 +885,8 @@ if Code.ensure_loaded?(Postgrex) do
["NOT (", expr(expr, sources, query), ?)]
end

defp expr({:~~~, _, [expr]}, sources, query) do
["~(", expr(expr, sources, query), ?)]
defp expr({bnot, _, [expr]}, sources, query) when bnot in ~w(~~~ bnot)a do
["~" | maybe_paren(expr, sources, query)]
end

defp expr(%Ecto.SubQuery{query: query}, sources, parent_query) do
Expand Down
34 changes: 31 additions & 3 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,7 @@ defmodule Ecto.Adapters.PostgresTest do

query = Schema |> select([r], r.x &&& 2) |> plan()
assert all(query) == ~s{SELECT s0."x" & 2 FROM "schema" AS s0}
end

test "bitwise ops" do
query = Schema |> select([r], r.x ||| 2) |> plan()
assert all(query) == ~s{SELECT s0."x" | 2 FROM "schema" AS s0}

Expand All @@ -789,8 +787,38 @@ defmodule Ecto.Adapters.PostgresTest do
query = Schema |> select([r], r.x >>> 2) |> plan()
assert all(query) == ~s{SELECT s0."x" >> 2 FROM "schema" AS s0}

end

test "unary ops" do
query = Schema |> select([r], ~~~r.x) |> plan()
assert all(query) == ~s{SELECT ~(s0."x") FROM "schema" AS s0}
assert all(query) == ~s{SELECT ~s0."x" FROM "schema" AS s0}
end

test "bitwise functions" do
query = Schema |> select([r], band(r.x, 2)) |> plan()
assert all(query) == ~s{SELECT s0."x" & 2 FROM "schema" AS s0}

query = Schema |> select([r], bor(r.x, 2)) |> plan()
assert all(query) == ~s{SELECT s0."x" | 2 FROM "schema" AS s0}

query = Schema |> select([r], bxor(r.x, 2)) |> plan()
assert all(query) == ~s{SELECT s0."x" # 2 FROM "schema" AS s0}

query = Schema |> select([r], bsl(r.x, 2)) |> plan()
assert all(query) == ~s{SELECT s0."x" << 2 FROM "schema" AS s0}

query = Schema |> select([r], bsr(r.x, 2)) |> plan()
assert all(query) == ~s{SELECT s0."x" >> 2 FROM "schema" AS s0}

query = Schema |> select([r], bnot(r.x)) |> plan()
assert all(query) == ~s{SELECT ~s0."x" FROM "schema" AS s0}

# test parenthesis
query = Schema |> select([r], band(r.x ||| 1, 2)) |> plan()
assert all(query) == ~s{SELECT (s0."x" | 1) & 2 FROM "schema" AS s0}

query = Schema |> select([r], bnot(r.x &&& 1)) |> plan()
assert all(query) == ~s{SELECT ~(s0."x" & 1) FROM "schema" AS s0}
end

test "is_nil" do
Expand Down

0 comments on commit b640437

Please sign in to comment.