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

Raise a nice error message of params is not a list #714

Merged
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
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
26 changes: 26 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1927,4 +1927,30 @@ defmodule QueryTest do
{:ok, pid} = P.start_link(database: "postgrex_test", search_path: ["public", "test_schema"])
%{rows: [[1, "foo"]]} = P.query!(pid, "SELECT * from test_table", [])
end

test "raise a nice message if params is not a list", context do
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
Loading