From 18783938097b728f4656a8c463d23133a3adf2bd Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Sun, 13 Oct 2024 20:13:21 +0200 Subject: [PATCH] cr suggestions --- lib/postgrex.ex | 7 ++----- lib/postgrex/protocol.ex | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/postgrex.ex b/lib/postgrex.ex index 845f947d..8f53b5ba 100644 --- a/lib/postgrex.ex +++ b/lib/postgrex.ex @@ -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)} @@ -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 @@ -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 diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index 6aa18795..664a2678 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -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 @@ -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 @@ -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