diff --git a/lib/ecto/adapters/myxql/connection.ex b/lib/ecto/adapters/myxql/connection.ex index 6e6224f7..6cf3b0f9 100644 --- a/lib/ecto/adapters/myxql/connection.ex +++ b/lib/ecto/adapters/myxql/connection.ex @@ -689,6 +689,10 @@ if Code.ensure_loaded?(MyXQL) do [expr(other, sources, query), " + 0"] end + defp expr(%Ecto.Query.Tagged{value: other, type: :boolean}, sources, query) do + ["IF(", expr(other, sources, query), ", TRUE, FALSE)"] + end + defp expr(%Ecto.Query.Tagged{value: other, type: type}, sources, query) do ["CAST(", expr(other, sources, query), " AS ", ecto_cast_to_db(type, query), ?)] end diff --git a/test/ecto/adapters/myxql_test.exs b/test/ecto/adapters/myxql_test.exs index 0020d2b6..33fc9274 100644 --- a/test/ecto/adapters/myxql_test.exs +++ b/test/ecto/adapters/myxql_test.exs @@ -612,6 +612,16 @@ defmodule Ecto.Adapters.MyXQLTest do assert all(query) == ~s{SELECT CAST(? AS char) FROM `schema` AS s0} end + test "boolean type true is cast with an if" do + query = Schema |> select([], type(^true, :boolean)) |> plan() + assert all(query) == ~s{SELECT IF(?, TRUE, FALSE) FROM `schema` AS s0} + end + + test "boolean type false is cast with an if" do + query = Schema |> select([], type(^false, :boolean)) |> plan() + assert all(query) == ~s{SELECT IF(?, TRUE, FALSE) FROM `schema` AS s0} + end + test "json_extract_path" do query = Schema |> select([s], json_extract_path(s.meta, [0, 1])) |> plan() assert all(query) == ~s{SELECT json_extract(s0.`meta`, '$[0][1]') FROM `schema` AS s0}