Skip to content

Commit

Permalink
cr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Oct 13, 2024
1 parent a5121fb commit 1878393
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
7 changes: 2 additions & 5 deletions lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ defmodule Postgrex do
@spec query(conn, iodata, list, [execute_option]) ::
{:ok, Postgrex.Result.t()} | {:error, Exception.t()}
def query(conn, statement, params, opts \\ []) do
prepare? = !Keyword.get(opts, :comment)
opts = Keyword.put(opts, :postgrex_prepare, prepare?)

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

Expand Down Expand Up @@ -366,7 +363,7 @@ defmodule Postgrex do
{:ok, Postgrex.Query.t()} | {:error, Exception.t()}
def prepare(conn, name, statement, opts \\ []) do
query = %Query{name: name, statement: statement}
prepare? = !Keyword.get(opts, :comment)
prepare? = Keyword.get(opts, :comment) == nil
opts = Keyword.put(opts, :postgrex_prepare, prepare?)
DBConnection.prepare(conn, query, opts)
end
Expand All @@ -377,7 +374,7 @@ defmodule Postgrex do
"""
@spec prepare!(conn, iodata, iodata, [option]) :: Postgrex.Query.t()
def prepare!(conn, name, statement, opts \\ []) do
prepare? = !Keyword.get(opts, :comment)
prepare? = Keyword.get(opts, :comment) == nil
opts = Keyword.put(opts, :postgrex_prepare, prepare?)
DBConnection.prepare!(conn, %Query{name: name, statement: statement}, opts)
end
Expand Down
32 changes: 17 additions & 15 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,21 @@ defmodule Postgrex.Protocol do
end

def handle_prepare(%Query{name: ""} = query, opts, s) do
prepare? = Keyword.get(opts, :postgrex_prepare, false)
status = new_status(opts, prepare: prepare?)
prepare = Keyword.get(opts, :postgrex_prepare, false)
status = new_status(opts, prepare: prepare)

if prepare? do
parse_describe_close(s, status, query)
else
comment = Keyword.get(opts, :comment)
case prepare do
true ->
parse_describe_close(s, status, query)

if is_binary(comment) && String.contains?(comment, "*/") do
raise @comment_validation_error
else
parse_describe_flush(s, status, query, comment)
end
false ->
comment = Keyword.get(opts, :comment)

if is_binary(comment) && String.contains?(comment, "*/") do
raise @comment_validation_error
else
parse_describe_flush(s, status, query, comment)
end
end
end

Expand All @@ -371,11 +373,11 @@ defmodule Postgrex.Protocol do
if new_query = cached_query(s, query) do
{:ok, new_query, s}
else
prepare? = Keyword.get(opts, :postgrex_prepare, false)
status = new_status(opts, prepare: prepare?)
prepare = Keyword.get(opts, :postgrex_prepare, false)
status = new_status(opts, prepare: prepare)

result =
case prepare? do
case prepare do
true -> close_parse_describe(s, status, query)
false -> close_parse_describe_flush(s, status, query)
end
Expand Down Expand Up @@ -1605,7 +1607,7 @@ defmodule Postgrex.Protocol do
end

defp parse_describe_comment_msgs(query, comment, tail) when is_binary(comment) do
statement = query.statement <> "/* #{comment} */"
statement = query.statement <> "/*#{comment}*/"
query = %{query | statement: statement}
parse_describe_msgs(query, tail)
end
Expand Down

0 comments on commit 1878393

Please sign in to comment.