Skip to content

Commit 93d8c68

Browse files
introduce_identifier_constant
1 parent e000147 commit 93d8c68

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

lib/ecto/adapters/myxql/connection.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,18 @@ if Code.ensure_loaded?(MyXQL) do
750750
quote_name(literal)
751751
end
752752

753+
defp expr({:identifier, _, [literal]}, _sources, _query) do
754+
quote_name(literal)
755+
end
756+
757+
defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
758+
[?', escape_string(literal), ?']
759+
end
760+
761+
defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
762+
[to_string(literal)]
763+
end
764+
753765
defp expr({:splice, _, [{:^, _, [_, length]}]}, _sources, _query) do
754766
Enum.intersperse(List.duplicate(??, length), ?,)
755767
end

lib/ecto/adapters/postgres/connection.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,18 @@ if Code.ensure_loaded?(Postgrex) do
991991
quote_name(literal)
992992
end
993993

994+
defp expr({:identifier, _, [literal]}, _sources, _query) do
995+
quote_name(literal)
996+
end
997+
998+
defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
999+
[?', escape_string(literal), ?']
1000+
end
1001+
1002+
defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
1003+
[to_string(literal)]
1004+
end
1005+
9941006
defp expr({:splice, _, [{:^, _, [idx, length]}]}, _sources, _query) do
9951007
Enum.map_join(1..length, ",", &"$#{idx + &1}")
9961008
end

lib/ecto/adapters/tds/connection.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,18 @@ if Code.ensure_loaded?(Tds) do
830830
quote_name(literal)
831831
end
832832

833+
defp expr({:identifier, _, [literal]}, _sources, _query) do
834+
quote_name(literal)
835+
end
836+
837+
defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
838+
[?', escape_string(literal), ?']
839+
end
840+
841+
defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
842+
[to_string(literal)]
843+
end
844+
833845
defp expr({:splice, _, [{:^, _, [idx, length]}]}, _sources, _query) do
834846
list_param_to_args(idx, length)
835847
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ defmodule EctoSQL.MixProject do
7676
if path = System.get_env("ECTO_PATH") do
7777
{:ecto, path: path}
7878
else
79-
{:ecto, github: "elixir-ecto/ecto"}
79+
{:ecto, git: "https://github.com/greg-rychlewski/ecto.git", branch: "literals_take_2"}
8080
end
8181
end
8282

test/ecto/adapters/myxql_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,15 @@ defmodule Ecto.Adapters.MyXQLTest do
658658
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
659659
assert all(query) == ~s{SELECT s0.`x` COLLATE `es_ES` FROM `schema` AS s0}
660660

661+
query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
662+
assert all(query) == ~s{SELECT s0.`x` COLLATE `es_ES` FROM `schema` AS s0}
663+
664+
query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
665+
assert all(query) == ~s{SELECT s0.`x` FROM `schema` AS s0 LIMIT 1}
666+
667+
query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
668+
assert all(query) == ~s{SELECT 'let''s escape' FROM `schema` AS s0}
669+
661670
query =
662671
Schema
663672
|> select([r], r.x)

test/ecto/adapters/postgres_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,15 @@ defmodule Ecto.Adapters.PostgresTest do
838838
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
839839
assert all(query) == ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0}
840840

841+
query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
842+
assert all(query) == ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0}
843+
844+
query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
845+
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 LIMIT 1}
846+
847+
query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
848+
assert all(query) == ~s{SELECT 'let''s escape' FROM "schema" AS s0}
849+
841850
query =
842851
Schema
843852
|> select([r], r.x)

test/ecto/adapters/tds_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,15 @@ defmodule Ecto.Adapters.TdsTest do
694694
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
695695
assert all(query) == ~s{SELECT s0.[x] COLLATE [es_ES] FROM [schema] AS s0}
696696

697+
query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
698+
assert all(query) == ~s{SELECT s0.[x] COLLATE [es_ES] FROM [schema] AS s0}
699+
700+
query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
701+
assert all(query) == ~s{SELECT TOP(1) s0.[x] FROM [schema] AS s0}
702+
703+
query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
704+
assert all(query) == ~s{SELECT 'let''s escape' FROM [schema] AS s0}
705+
697706
query =
698707
Schema
699708
|> select([r], r.x)

0 commit comments

Comments
 (0)