Skip to content

Commit f5d29e3

Browse files
authored
fix: myxql type cast of :integer should be signed integer (instead of unsigned) (#609)
1 parent 1ced585 commit f5d29e3

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

integration_test/sql/sql.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ defmodule Ecto.Integration.SQLTest do
2121
assert [123] = TestRepo.all(from p in "posts", select: type(fragment("visits"), :integer))
2222
end
2323

24+
test "type casting negative integers" do
25+
TestRepo.insert!(%Post{visits: -42})
26+
assert [-42] = TestRepo.all(from(p in Post, select: type(p.visits, :integer)))
27+
end
28+
2429
@tag :array_type
2530
test "fragment array types" do
2631
text1 = "foo"

lib/ecto/adapters/myxql/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ if Code.ensure_loaded?(MyXQL) do
14451445
end
14461446

14471447
defp ecto_cast_to_db(:id, _query), do: "unsigned"
1448-
defp ecto_cast_to_db(:integer, _query), do: "unsigned"
1448+
defp ecto_cast_to_db(:integer, _query), do: "signed"
14491449
defp ecto_cast_to_db(:string, _query), do: "char"
14501450
defp ecto_cast_to_db(:utc_datetime_usec, _query), do: "datetime(6)"
14511451
defp ecto_cast_to_db(:naive_datetime_usec, _query), do: "datetime(6)"

test/ecto/adapters/myxql_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ defmodule Ecto.Adapters.MyXQLTest do
161161
~s{UNION ALL } <>
162162
~s{(SELECT sc0.`id`, st1.`depth` + 1 FROM `categories` AS sc0 } <>
163163
~s{INNER JOIN `tree` AS st1 ON st1.`id` = sc0.`parent_id`)) } <>
164-
~s{SELECT s0.`x`, t1.`id`, CAST(t1.`depth` AS unsigned) } <>
164+
~s{SELECT s0.`x`, t1.`id`, CAST(t1.`depth` AS signed) } <>
165165
~s{FROM `schema` AS s0 } <>
166166
~s{INNER JOIN `tree` AS t1 ON t1.`id` = s0.`category_id`}
167167
end
@@ -1475,7 +1475,7 @@ defmodule Ecto.Adapters.MyXQLTest do
14751475
|> plan()
14761476
|> all()
14771477

1478-
cast_types = %{bid: "binary(16)", num: "unsigned"}
1478+
cast_types = %{bid: "binary(16)", num: "signed"}
14791479
from_values_text = values_text(values, cast_types)
14801480
join_values_text = values_text(values, cast_types)
14811481
select_fields = Enum.map_join(types, ", ", fn {field, _} -> "v1.`#{field}`" end)
@@ -1498,7 +1498,7 @@ defmodule Ecto.Adapters.MyXQLTest do
14981498
|> plan(:delete_all)
14991499
|> delete_all()
15001500

1501-
cast_types = %{bid: "binary(16)", num: "unsigned"}
1501+
cast_types = %{bid: "binary(16)", num: "signed"}
15021502
values_text = values_text(values, cast_types)
15031503
fields = Enum.map_join(types, ",", fn {field, _} -> "`#{field}`" end)
15041504

@@ -1523,7 +1523,7 @@ defmodule Ecto.Adapters.MyXQLTest do
15231523
|> plan(:update_all)
15241524
|> update_all()
15251525

1526-
cast_types = %{bid: "binary(16)", num: "unsigned"}
1526+
cast_types = %{bid: "binary(16)", num: "signed"}
15271527
values_text = values_text(values, cast_types)
15281528
fields = Enum.map_join(types, ",", fn {field, _} -> "`#{field}`" end)
15291529

0 commit comments

Comments
 (0)