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

dk_fix_missing_comments #712

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 9 additions & 7 deletions lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ defmodule Postgrex do
@spec query(conn, iodata, list, [execute_option]) ::
{:ok, Postgrex.Result.t()} | {:error, Exception.t()}
def query(conn, statement, params, opts \\ []) do
validate_comment!(opts)
name = Keyword.get(opts, :cache_statement)

if name = Keyword.get(opts, :cache_statement) do
if comment_not_present!(opts) && name do
query = %Query{name: name, cache: :statement, statement: IO.iodata_to_binary(statement)}

case DBConnection.prepare_execute(conn, query, params, opts) do
Expand Down Expand Up @@ -330,16 +330,16 @@ defmodule Postgrex do
end
end

defp validate_comment!(opts) do
defp comment_not_present!(opts) do
case Keyword.get(opts, :comment) do
nil ->
false
true

comment when is_binary(comment) ->
if String.contains?(comment, "*/") do
raise @comment_validation_error
else
true
false
end
end
end
Expand Down Expand Up @@ -388,7 +388,7 @@ defmodule Postgrex do
{:ok, Postgrex.Query.t()} | {:error, Exception.t()}
def prepare(conn, name, statement, opts \\ []) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, not validate_comment!(opts))
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare(conn, query, opts)
end

Expand All @@ -398,7 +398,7 @@ defmodule Postgrex do
"""
@spec prepare!(conn, iodata, iodata, [option]) :: Postgrex.Query.t()
def prepare!(conn, name, statement, opts \\ []) do
opts = Keyword.put(opts, :postgrex_prepare, not validate_comment!(opts))
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare!(conn, %Query{name: name, statement: statement}, opts)
end

Expand Down Expand Up @@ -436,6 +436,7 @@ defmodule Postgrex do
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def prepare_execute(conn, name, statement, params, opts \\ []) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute(conn, query, params, opts)
end

Expand All @@ -447,6 +448,7 @@ defmodule Postgrex do
{Postgrex.Query.t(), Postgrex.Result.t()}
def prepare_execute!(conn, name, statement, params, opts \\ []) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute!(conn, query, params, opts)
end

Expand Down
20 changes: 12 additions & 8 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,12 @@ defmodule Postgrex.Protocol do
else
prepare = Keyword.get(opts, :postgrex_prepare, false)
status = new_status(opts, prepare: prepare)
comment = Keyword.get(opts, :comment)

result =
case prepare do
true -> close_parse_describe(s, status, query)
false -> close_parse_describe_flush(s, status, query)
false -> close_parse_describe_flush(s, status, query, comment)
end

with {:ok, query, s} <- result do
Expand Down Expand Up @@ -1535,11 +1536,13 @@ defmodule Postgrex.Protocol do
transaction_error(s, postgres)
end

defp close_parse_describe_flush(s, %{mode: :transaction} = status, query) do
defp close_parse_describe_flush(s, %{mode: :transaction} = status, query, comment) do
%Query{name: name} = query
%{buffer: buffer} = s

msgs = [msg_close(type: :statement, name: name)] ++ parse_describe_msgs(query, [msg_flush()])
msgs =
[msg_close(type: :statement, name: name)] ++
parse_describe_comment_msgs(query, comment, [msg_flush()])

with :ok <- msg_send(%{s | buffer: nil}, msgs, buffer),
{:ok, s, buffer} <- recv_close(s, status, buffer),
Expand All @@ -1563,15 +1566,16 @@ defmodule Postgrex.Protocol do
defp close_parse_describe_flush(
%{postgres: :transaction, buffer: buffer} = s,
%{mode: :savepoint} = status,
query
query,
comment
) do
%Query{name: name} = query

msgs =
[
msg_query(statement: "SAVEPOINT postgrex_query"),
msg_close(type: :statement, name: name)
] ++ parse_describe_msgs(query, [msg_flush()])
] ++ parse_describe_comment_msgs(query, comment, [msg_flush()])

with :ok <- msg_send(%{s | buffer: nil}, msgs, buffer),
{:ok, _, %{buffer: buffer} = s} <- recv_transaction(s, status, buffer),
Expand All @@ -1593,7 +1597,7 @@ defmodule Postgrex.Protocol do
end
end

defp close_parse_describe_flush(%{postgres: postgres} = s, %{mode: :savepoint}, _)
defp close_parse_describe_flush(%{postgres: postgres} = s, %{mode: :savepoint}, _, _)
when postgres in [:idle, :error] do
transaction_error(s, postgres)
end
Expand Down Expand Up @@ -2132,7 +2136,7 @@ defmodule Postgrex.Protocol do
defp handle_prepare_execute(%Query{} = query, params, opts, s) do
status = new_status(opts)

case close_parse_describe_flush(s, status, query) do
case close_parse_describe_flush(s, status, query, nil) do
{:ok, query, s} ->
bind_execute(s, status, query, params)

Expand Down Expand Up @@ -2423,7 +2427,7 @@ defmodule Postgrex.Protocol do
defp handle_prepare_bind(%Query{} = query, params, res, opts, s) do
status = new_status(opts)

case close_parse_describe_flush(s, status, query) do
case close_parse_describe_flush(s, status, query, nil) do
{:ok, query, s} ->
bind(s, status, query, params, res)

Expand Down
Loading