Skip to content

Commit 99fa62b

Browse files
committed
comments in query
Signed-off-by: Daniel Kukula <[email protected]>
1 parent 45fd270 commit 99fa62b

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

lib/ecto/adapters/postgres/connection.ex

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ if Code.ensure_loaded?(Postgrex) do
187187
limit = limit(query, sources)
188188
offset = offset(query, sources)
189189
lock = lock(query, sources)
190+
comment = comment(query)
190191

191192
[
193+
comment,
192194
cte,
193195
select,
194196
from,
@@ -200,7 +202,8 @@ if Code.ensure_loaded?(Postgrex) do
200202
combinations,
201203
order_by,
202204
limit,
203-
offset | lock
205+
offset
206+
| lock
204207
]
205208
end
206209

@@ -214,8 +217,17 @@ if Code.ensure_loaded?(Postgrex) do
214217
fields = update_fields(query, sources)
215218
{join, wheres} = using_join(query, :update_all, "FROM", sources)
216219
where = where(%{query | wheres: wheres ++ query.wheres}, sources)
220+
comment = comment(query)
217221

218-
[cte, prefix, fields, join, where | returning(query, sources)]
222+
[
223+
comment,
224+
cte,
225+
prefix,
226+
fields,
227+
join,
228+
where
229+
| returning(query, sources)
230+
]
219231
end
220232

221233
@impl true
@@ -226,8 +238,19 @@ if Code.ensure_loaded?(Postgrex) do
226238

227239
{join, wheres} = using_join(query, :delete_all, "USING", sources)
228240
where = where(%{query | wheres: wheres ++ query.wheres}, sources)
241+
comment = comment(query)
229242

230-
[cte, "DELETE FROM ", from, " AS ", name, join, where | returning(query, sources)]
243+
[
244+
comment,
245+
cte,
246+
"DELETE FROM ",
247+
from,
248+
" AS ",
249+
name,
250+
join,
251+
where
252+
| returning(query, sources)
253+
]
231254
end
232255

233256
@impl true
@@ -880,6 +903,17 @@ if Code.ensure_loaded?(Postgrex) do
880903
defp lock(%{lock: binary}, _sources) when is_binary(binary), do: [?\s | binary]
881904
defp lock(%{lock: expr} = query, sources), do: [?\s | expr(expr, sources, query)]
882905

906+
defp comment(%{comments: []}), do: []
907+
908+
defp comment(%{comments: comments}) do
909+
comment =
910+
Enum.map_intersperse(comments, ",", fn
911+
comment when is_binary(comment) -> comment
912+
end)
913+
914+
["/*", comment, "*/\n"]
915+
end
916+
883917
defp boolean(_name, [], _sources, _query), do: []
884918

885919
defp boolean(name, [%{expr: expr, op: op} | query_exprs], sources, query) do

test/ecto/adapters/postgres_test.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,57 @@ defmodule Ecto.Adapters.PostgresTest do
29852985
assert SQL.ddl_logs(result) == [{:error, ~s(table "foo" exists, skipping), []}]
29862986
end
29872987

2988+
describe "comments" do
2989+
test "comments before query" do
2990+
query =
2991+
from(m in Schema, update: [set: [x: 0]])
2992+
|> comment("comment before query")
2993+
|> plan(:update_all)
2994+
2995+
assert update_all(query) == ~s{/*comment before query*/\nUPDATE "schema" AS s0 SET "x" = 0}
2996+
end
2997+
2998+
test "with multiple comments" do
2999+
query =
3000+
Schema
3001+
|> select([r], r.x)
3002+
|> comment("comptime")
3003+
|> comment(:atom)
3004+
|> plan()
3005+
3006+
assert all(query) =~ "/*comptime,atom*/"
3007+
end
3008+
3009+
test "with comments in subquery" do
3010+
subquery =
3011+
Schema
3012+
|> select([r], r.x)
3013+
|> comment("subquery")
3014+
3015+
query =
3016+
subquery(subquery)
3017+
|> select([r], r.x)
3018+
|> comment("query")
3019+
|> plan()
3020+
3021+
assert all(query) ==
3022+
~s'/*query*/\nSELECT s0."x" FROM ' <>
3023+
~s'(/*subquery*/\nSELECT ss0."x" AS "x" FROM "schema" AS ss0 AS s0;'
3024+
end
3025+
3026+
test "comments in delete_all" do
3027+
query = Schema |> select([r], r.x) |> comment("comment") |> plan()
3028+
3029+
assert delete_all(query) ==
3030+
~s'/*comment*/\nDELETE FROM "schema" AS s0 RETURNING s0."x"'
3031+
end
3032+
3033+
test "comments in update_all" do
3034+
query = from(m in Schema, update: [set: [x: 0]]) |> comment("comment") |> plan(:update_all)
3035+
assert update_all(query) == ~s{/*comment*/\nUPDATE "schema" AS s0 SET "x" = 0}
3036+
end
3037+
end
3038+
29883039
defp make_result(level) do
29893040
%Postgrex.Result{
29903041
messages: [

0 commit comments

Comments
 (0)