Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Allow numbers in literal/1 (#651)" and add support for identifier/1 and constant/1 #652

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
introduce_identifier_constant
greg-rychlewski committed Jan 9, 2025
commit 93d8c681803e83a489389a443e2e33e5a7e144ba
12 changes: 12 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
@@ -750,6 +750,18 @@ if Code.ensure_loaded?(MyXQL) do
quote_name(literal)
end

defp expr({:identifier, _, [literal]}, _sources, _query) do
quote_name(literal)
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
[?', escape_string(literal), ?']
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
[to_string(literal)]
end

defp expr({:splice, _, [{:^, _, [_, length]}]}, _sources, _query) do
Enum.intersperse(List.duplicate(??, length), ?,)
end
12 changes: 12 additions & 0 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
@@ -991,6 +991,18 @@ if Code.ensure_loaded?(Postgrex) do
quote_name(literal)
end

defp expr({:identifier, _, [literal]}, _sources, _query) do
quote_name(literal)
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
[?', escape_string(literal), ?']
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
[to_string(literal)]
end

defp expr({:splice, _, [{:^, _, [idx, length]}]}, _sources, _query) do
Enum.map_join(1..length, ",", &"$#{idx + &1}")
end
12 changes: 12 additions & 0 deletions lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
@@ -830,6 +830,18 @@ if Code.ensure_loaded?(Tds) do
quote_name(literal)
end

defp expr({:identifier, _, [literal]}, _sources, _query) do
quote_name(literal)
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_binary(literal) do
[?', escape_string(literal), ?']
end

defp expr({:constant, _, [literal]}, _sources, _query) when is_number(literal) do
[to_string(literal)]
end

defp expr({:splice, _, [{:^, _, [idx, length]}]}, _sources, _query) do
list_param_to_args(idx, length)
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ defmodule EctoSQL.MixProject do
if path = System.get_env("ECTO_PATH") do
{:ecto, path: path}
else
{:ecto, github: "elixir-ecto/ecto"}
{:ecto, git: "https://github.com/greg-rychlewski/ecto.git", branch: "literals_take_2"}
end
end

9 changes: 9 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
@@ -658,6 +658,15 @@ defmodule Ecto.Adapters.MyXQLTest do
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0.`x` COLLATE `es_ES` FROM `schema` AS s0}

query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0.`x` COLLATE `es_ES` FROM `schema` AS s0}

query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
assert all(query) == ~s{SELECT s0.`x` FROM `schema` AS s0 LIMIT 1}

query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
assert all(query) == ~s{SELECT 'let''s escape' FROM `schema` AS s0}

query =
Schema
|> select([r], r.x)
9 changes: 9 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
@@ -838,6 +838,15 @@ defmodule Ecto.Adapters.PostgresTest do
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0}

query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0}

query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 LIMIT 1}

query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
assert all(query) == ~s{SELECT 'let''s escape' FROM "schema" AS s0}

query =
Schema
|> select([r], r.x)
9 changes: 9 additions & 0 deletions test/ecto/adapters/tds_test.exs
Original file line number Diff line number Diff line change
@@ -694,6 +694,15 @@ defmodule Ecto.Adapters.TdsTest do
query = Schema |> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0.[x] COLLATE [es_ES] FROM [schema] AS s0}

query = Schema |> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES"))) |> plan()
assert all(query) == ~s{SELECT s0.[x] COLLATE [es_ES] FROM [schema] AS s0}

query = Schema |> select([r], r.x) |> limit(fragment("?", constant(^1))) |> plan()
assert all(query) == ~s{SELECT TOP(1) s0.[x] FROM [schema] AS s0}

query = Schema |> select(fragment("?", constant(^"let's escape"))) |> plan()
assert all(query) == ~s{SELECT 'let''s escape' FROM [schema] AS s0}

query =
Schema
|> select([r], r.x)