Skip to content

Commit

Permalink
Fix for casting boolean values in MySQL (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1dnight committed Jul 19, 2023
1 parent 9069a4c commit cfb3dad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit cfb3dad

Please sign in to comment.