diff --git a/lib/ecto/adapters/postgres/connection.ex b/lib/ecto/adapters/postgres/connection.ex index 200d8f4e..a04dd1f3 100644 --- a/lib/ecto/adapters/postgres/connection.ex +++ b/lib/ecto/adapters/postgres/connection.ex @@ -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) @@ -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" diff --git a/test/ecto/adapters/postgres_test.exs b/test/ecto/adapters/postgres_test.exs index a31096bb..6ea79ec0 100644 --- a/test/ecto/adapters/postgres_test.exs +++ b/test/ecto/adapters/postgres_test.exs @@ -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