Skip to content

Add support for comparing jsonb @> with boolean values #399

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ if Code.ensure_loaded?(Postgrex) do
defp expr({:count, _, []}, _sources, _query), do: "count(*)"

defp expr({:==, _, [{:json_extract_path, _, [expr, path]} = left, right]}, sources, query)
when is_binary(right) or is_integer(right) do
when is_binary(right) or is_integer(right) or is_boolean(right) do
case Enum.split(path, -1) do
{path, [last]} when is_binary(last) ->
extracted = json_extract_path(expr, path, sources, query)
Expand Down Expand Up @@ -1354,6 +1354,9 @@ if Code.ensure_loaded?(Postgrex) do
Integer.to_string(value)
end

defp escape_json(true), do: ["true"]
defp escape_json(false), do: ["false"]

defp ecto_to_db({:array, t}), do: [ecto_to_db(t), ?[, ?]]
defp ecto_to_db(:id), do: "integer"
defp ecto_to_db(:identity), do: "bigint"
Expand Down
14 changes: 10 additions & 4 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,22 @@ defmodule Ecto.Adapters.PostgresTest do

test "optimized json_extract_path" do
query = Schema |> where([s], s.meta["id"] == 123) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0."meta"@>'{"id": 123}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"id": 123}'))|

query = Schema |> where([s], s.meta["id"] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0."meta"@>'{"id": "123"}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"id": "123"}'))|

query = Schema |> where([s], s.meta["tags"][0]["name"] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE (((s0."meta"#>'{"tags",0}')@>'{"name": "123"}'))|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE (((s0."meta"#>'{"tags",0}')@>'{"name": "123"}'))|

query = Schema |> where([s], s.meta[0] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM \"schema\" AS s0 WHERE ((s0.\"meta\"#>'{0}') = '123')|
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0.\"meta\"#>'{0}') = '123')|

query = Schema |> where([s], s.meta["enabled"] == true) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"enabled": true}'))|

query = Schema |> where([s], s.meta["extra"][0]["enabled"] == false) |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE (((s0."meta"#>'{"extra",0}')@>'{"enabled": false}'))|
end

test "nested expressions" do
Expand Down