Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski committed Nov 6, 2024
1 parent 18c030f commit 69cf80c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
12 changes: 6 additions & 6 deletions lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ defmodule Postgrex do
"""
@spec query(conn, iodata, list, [execute_option]) ::
{:ok, Postgrex.Result.t()} | {:error, Exception.t()}
def query(conn, statement, params, opts \\ []) do
def query(conn, statement, params, opts \\ []) when is_list(params) do
name = Keyword.get(opts, :cache_statement)

if comment_not_present!(opts) && name do
Expand Down Expand Up @@ -349,7 +349,7 @@ defmodule Postgrex do
there was an error. See `query/3`.
"""
@spec query!(conn, iodata, list, [execute_option]) :: Postgrex.Result.t()
def query!(conn, statement, params, opts \\ []) do
def query!(conn, statement, params, opts \\ []) when is_list(params) do
case query(conn, statement, params, opts) do
{:ok, result} -> result
{:error, err} -> raise err
Expand Down Expand Up @@ -434,7 +434,7 @@ defmodule Postgrex do
"""
@spec prepare_execute(conn, iodata, iodata, list, [execute_option]) ::
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def prepare_execute(conn, name, statement, params, opts \\ []) do
def prepare_execute(conn, name, statement, params, opts \\ []) when is_list(params) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute(conn, query, params, opts)
Expand All @@ -446,7 +446,7 @@ defmodule Postgrex do
"""
@spec prepare_execute!(conn, iodata, iodata, list, [execute_option]) ::
{Postgrex.Query.t(), Postgrex.Result.t()}
def prepare_execute!(conn, name, statement, params, opts \\ []) do
def prepare_execute!(conn, name, statement, params, opts \\ []) when is_list(params) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute!(conn, query, params, opts)
Expand Down Expand Up @@ -482,7 +482,7 @@ defmodule Postgrex do
"""
@spec execute(conn, Postgrex.Query.t(), list, [execute_option]) ::
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def execute(conn, query, params, opts \\ []) do
def execute(conn, query, params, opts \\ []) when is_list(params) do
DBConnection.execute(conn, query, params, opts)
end

Expand All @@ -492,7 +492,7 @@ defmodule Postgrex do
"""
@spec execute!(conn, Postgrex.Query.t(), list, [execute_option]) ::
Postgrex.Result.t()
def execute!(conn, query, params, opts \\ []) do
def execute!(conn, query, params, opts \\ []) when is_list(params) do
DBConnection.execute!(conn, query, params, opts)
end

Expand Down
6 changes: 1 addition & 5 deletions lib/postgrex/type_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,10 @@ defmodule Postgrex.TypeModule do
unquote(encodes)

@doc false
def encode_params(params, types) when is_list(params) do
def encode_params(params, types) do
encode_params(params, types, [])
end

def encode_params(params, _types) do
raise ArgumentError, "expected params to be a list, got: #{inspect(params)}"
end

defp encode_params([param | params], [type | types], encoded) do
encode_params(params, types, [encode_value(param, type) | encoded])
end
Expand Down
24 changes: 21 additions & 3 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,10 +1929,28 @@ defmodule QueryTest do
end

test "raise a nice message if params is not a list", context do
msg = ~r"expected params to be a list"

assert_raise ArgumentError, msg, fn ->
assert_raise FunctionClauseError, fn ->
query("SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.query!(context[:pid], "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
prepare_execute("name", "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.prepare_execute!(context[:pid], "name", "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
execute("name", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.execute!(context[:pid], "name", "postgrex")
end
end
end

0 comments on commit 69cf80c

Please sign in to comment.