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 authored and josevalim committed Aug 21, 2023
1 parent 3888004 commit b201ac2
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 @@ -674,6 +674,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 @@ -590,6 +590,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 b201ac2

Please sign in to comment.