From aaff820457fe7527c0fccd0e0adf7085c0e5bdad Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Sun, 27 Oct 2024 18:13:07 +0100 Subject: [PATCH] dk_fix_missing_comments --- lib/postgrex.ex | 16 +++++++++------- lib/postgrex/protocol.ex | 20 ++++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/postgrex.ex b/lib/postgrex.ex index bee5ddfe..6c0c47ac 100644 --- a/lib/postgrex.ex +++ b/lib/postgrex.ex @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index 17d8aa05..3d681e67 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -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 @@ -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), @@ -1563,7 +1566,8 @@ 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 @@ -1571,7 +1575,7 @@ defmodule Postgrex.Protocol do [ 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), @@ -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 @@ -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) @@ -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)