@@ -30,6 +30,12 @@ defmodule Ecto.Adapters.SQL do
3030 * `query!(sql, params, options \\ [])` -
3131 shortcut for `Ecto.Adapters.SQL.query!/4`
3232
33+ * `query_many(sql, params, options \\ [])` -
34+ shortcut for `Ecto.Adapters.SQL.query_many/4`
35+
36+ * `query_many!(sql, params, options \\ [])` -
37+ shortcut for `Ecto.Adapters.SQL.query_many!/4`
38+
3339 * `to_sql(type, query, options \\ [])` -
3440 shortcut for `Ecto.Adapters.SQL.to_sql/4`
3541
@@ -429,7 +435,7 @@ defmodule Ecto.Adapters.SQL do
429435 end
430436
431437 @ doc """
432- Runs custom SQL query on given repo.
438+ Runs a custom SQL query on the given repo.
433439
434440 In case of success, it must return an `:ok` tuple containing
435441 a map with at least two keys:
@@ -471,6 +477,63 @@ defmodule Ecto.Adapters.SQL do
471477 sql_call ( adapter_meta , :query , [ sql ] , params , opts )
472478 end
473479
480+ @ doc """
481+ Same as `query_many/4` but raises on invalid queries.
482+ """
483+ @ spec query_many! ( Ecto.Repo . t | Ecto.Adapter . adapter_meta , iodata , [ term ] , Keyword . t ) ::
484+ [ % { :rows => nil | [ [ term ] | binary ] ,
485+ :num_rows => non_neg_integer ,
486+ optional ( atom ) => any } ]
487+ def query_many! ( repo , sql , params \\ [ ] , opts \\ [ ] ) do
488+ case query_many ( repo , sql , params , opts ) do
489+ { :ok , result } -> result
490+ { :error , err } -> raise_sql_call_error err
491+ end
492+ end
493+
494+ @ doc """
495+ Runs a custom SQL query that returns multiple results on the given repo.
496+
497+ In case of success, it must return an `:ok` tuple containing
498+ a list of maps with at least two keys:
499+
500+ * `:num_rows` - the number of rows affected
501+
502+ * `:rows` - the result set as a list. `nil` may be returned
503+ instead of the list if the command does not yield any row
504+ as result (but still yields the number of affected rows,
505+ like a `delete` command without returning would)
506+
507+ ## Options
508+
509+ * `:log` - When false, does not log the query
510+
511+ ## Examples
512+
513+ iex> Ecto.Adapters.SQL.query_many(MyRepo, "SELECT $1; SELECT $2;", [40, 2])
514+ {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
515+
516+ For convenience, this function is also available under the repository:
517+
518+ iex> MyRepo.query_many(SELECT $1; SELECT $2;", [40, 2])
519+ {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
520+
521+ """
522+ @ spec query_many ( pid ( ) | Ecto.Repo . t | Ecto.Adapter . adapter_meta , iodata , [ term ] , Keyword . t ) ::
523+ { :ok , [ % { :rows => nil | [ [ term ] | binary ] ,
524+ :num_rows => non_neg_integer ,
525+ optional ( atom ) => any } ] }
526+ | { :error , Exception . t }
527+ def query_many ( repo , sql , params \\ [ ] , opts \\ [ ] )
528+
529+ def query_many ( repo , sql , params , opts ) when is_atom ( repo ) or is_pid ( repo ) do
530+ query_many ( Ecto.Adapter . lookup_meta ( repo ) , sql , params , opts )
531+ end
532+
533+ def query_many ( adapter_meta , sql , params , opts ) do
534+ sql_call ( adapter_meta , :query_many , [ sql ] , params , opts )
535+ end
536+
474537 defp sql_call ( adapter_meta , callback , args , params , opts ) do
475538 % { pid: pool , telemetry: telemetry , sql: sql , opts: default_opts } = adapter_meta
476539 conn = get_conn_or_pool ( pool )
@@ -611,6 +674,24 @@ defmodule Ecto.Adapters.SQL do
611674 Ecto.Adapters.SQL . query! ( get_dynamic_repo ( ) , sql , params , opts )
612675 end
613676
677+ @ doc """
678+ A convenience function for SQL-based repositories that executes the given multi-result query.
679+
680+ See `Ecto.Adapters.SQL.query_many/4` for more information.
681+ """
682+ def query_many ( sql , params \\ [ ] , opts \\ [ ] ) do
683+ Ecto.Adapters.SQL . query_many ( get_dynamic_repo ( ) , sql , params , opts )
684+ end
685+
686+ @ doc """
687+ A convenience function for SQL-based repositories that executes the given multi-result query.
688+
689+ See `Ecto.Adapters.SQL.query_many!/4` for more information.
690+ """
691+ def query_many! ( sql , params \\ [ ] , opts \\ [ ] ) do
692+ Ecto.Adapters.SQL . query_many! ( get_dynamic_repo ( ) , sql , params , opts )
693+ end
694+
614695 @ doc """
615696 A convenience function for SQL-based repositories that translates the given query to SQL.
616697
0 commit comments