Skip to content

Commit

Permalink
raise on non-list params
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Jun 13, 2024
1 parent 85d82c9 commit a8ca22c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ if Code.ensure_loaded?(MyXQL) do

@impl true
def prepare_execute(conn, name, sql, params, opts) do
ensure_list_params!(params)
MyXQL.prepare_execute(conn, name, sql, params, opts)
end

@impl true
def query(conn, sql, params, opts) do
ensure_list_params!(params)
opts = Keyword.put_new(opts, :query_type, :binary_then_text)
MyXQL.query(conn, sql, params, opts)
end

@impl true
def query_many(conn, sql, params, opts) do
ensure_list_params!(params)
opts = Keyword.put_new(opts, :query_type, :text)
MyXQL.query_many(conn, sql, params, opts)
end

@impl true
def execute(conn, query, params, opts) do
ensure_list_params!(params)

case MyXQL.execute(conn, query, params, opts) do
{:ok, _, result} -> {:ok, result}
{:error, _} = error -> error
Expand All @@ -41,9 +46,16 @@ if Code.ensure_loaded?(MyXQL) do

@impl true
def stream(conn, sql, params, opts) do
ensure_list_params!(params)
MyXQL.stream(conn, sql, params, opts)
end

defp ensure_list_params!(params) do
unless is_list(params) do
raise ArgumentError, "expected params to be a list, got: #{inspect(params)}"
end
end

@quotes ~w(" ' `)

@impl true
Expand Down
12 changes: 12 additions & 0 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ if Code.ensure_loaded?(Postgrex) do

@impl true
def prepare_execute(conn, name, sql, params, opts) do
ensure_list_params!(params)

case Postgrex.prepare_execute(conn, name, sql, params, opts) do
{:error, %Postgrex.Error{postgres: %{pg_code: "22P02", message: message}} = error} ->
context = """
Expand All @@ -121,6 +123,7 @@ if Code.ensure_loaded?(Postgrex) do

@impl true
def query(conn, sql, params, opts) do
ensure_list_params!(params)
Postgrex.query(conn, sql, params, opts)
end

Expand All @@ -131,6 +134,8 @@ if Code.ensure_loaded?(Postgrex) do

@impl true
def execute(conn, %{ref: ref} = query, params, opts) do
ensure_list_params!(params)

case Postgrex.execute(conn, query, params, opts) do
{:ok, %{ref: ^ref}, result} ->
{:ok, result}
Expand All @@ -151,9 +156,16 @@ if Code.ensure_loaded?(Postgrex) do

@impl true
def stream(conn, sql, params, opts) do
ensure_list_params!(params)
Postgrex.stream(conn, sql, params, opts)
end

defp ensure_list_params!(params) do
unless is_list(params) do
raise ArgumentError, "expected params to be a list, got: #{inspect(params)}"
end
end

@parent_as __MODULE__
alias Ecto.Query.{BooleanExpr, ByExpr, JoinExpr, QueryExpr, WithExpr}

Expand Down
4 changes: 4 additions & 0 deletions lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ if Code.ensure_loaded?(Tds) do
def to_constraints(_, _opts), do: []

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

{params, _} =
Enum.map_reduce(params, 1, fn param, acc ->
case prepare_param(param) do
Expand Down

0 comments on commit a8ca22c

Please sign in to comment.