@@ -314,7 +314,95 @@ defmodule Ecto.Adapters.SQL do
314314
315315 @ timeout 15_000
316316
317- @ doc """
317+ @ query_doc """
318+ Runs a custom SQL query.
319+
320+ If the query was successful, it will return an `:ok` tuple containing
321+ a map with at least two keys:
322+ * `:num_rows` - the number of rows affected
323+ * `:rows` - the result set as a list. `nil` may be returned
324+ instead of the list if the command does not yield any row
325+ as result (but still yields the number of affected rows,
326+ like a `delete` command without returning would)
327+
328+ ## Options
329+ * `:log` - When false, does not log the query
330+ * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ @ timeout } `);
331+
332+ ## Examples
333+ iex> MyRepo.query("SELECT $1::integer + $2", [40, 2])
334+ {:ok, %{rows: [[42]], num_rows: 1}}
335+ """
336+
337+ @ query_bang_doc """
338+ Runs a custom SQL query.
339+
340+ If the query was successful, it will return an `:ok` tuple containing
341+ a map with at least two keys:
342+ * `:num_rows` - the number of rows affected
343+ * `:rows` - the result set as a list. `nil` may be returned
344+ instead of the list if the command does not yield any row
345+ as result (but still yields the number of affected rows,
346+ like a `delete` command without returning would)
347+
348+ ## Options
349+ * `:log` - When false, does not log the query
350+ * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ @ timeout } `);
351+
352+ ## Examples
353+ iex> MyRepo.query("SELECT $1::integer + $2", [40, 2])
354+ {:ok, %{rows: [[42]], num_rows: 1}}
355+ """
356+
357+ @ disconnect_all_doc """
358+ Forces all connections in the repo pool to disconnect within the given interval.
359+
360+ Once this function is called, the pool will disconnect all of its connections
361+ as they are checked in or as they are pinged. Checked in connections will be
362+ randomly disconnected within the given time interval. Pinged connections are
363+ immediately disconnected - as they are idle (according to `:idle_interval`).
364+
365+ If the connection has a backoff configured (which is the case by default),
366+ disconnecting means an attempt at a new connection will be done immediately
367+ after, without starting a new process for each connection. However, if backoff
368+ has been disabled, the connection process will terminate. In such cases,
369+ disconnecting all connections may cause the pool supervisor to restart
370+ depending on the max_restarts/max_seconds configuration of the pool,
371+ so you will want to set those carefully.
372+ """
373+
374+ @ query_many_doc """
375+ Runs a custom SQL query that returns multiple results on the given repo.
376+
377+ In case of success, it must return an `:ok` tuple containing
378+ a list of maps with at least two keys:
379+
380+ * `:num_rows` - the number of rows affected
381+
382+ * `:rows` - the result set as a list. `nil` may be returned
383+ instead of the list if the command does not yield any row
384+ as result (but still yields the number of affected rows,
385+ like a `delete` command without returning would)
386+
387+ ## Options
388+
389+ * `:log` - When false, does not log the query
390+ * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ @ timeout } `);
391+
392+ ## Examples
393+
394+ iex> MyRepo.query_many("SELECT $1; SELECT $2;", [40, 2])
395+ {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
396+
397+ iex> Ecto.Adapters.SQL.query_many(MyRepo, "SELECT $1; SELECT $2;", [40, 2])
398+ {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
399+ """
400+
401+ @ query_many_bang_doc """
402+ Same as `query_many/4` but raises on invalid queries.
403+ """
404+
405+ @ to_sql_doc """
318406 Converts the given query to SQL according to its kind and the
319407 adapter in the given repository.
320408
@@ -323,19 +411,14 @@ defmodule Ecto.Adapters.SQL do
323411 The examples below are meant for reference. Each adapter will
324412 return a different result:
325413
326- iex> Ecto.Adapters.SQL. to_sql(:all, Repo , Post)
414+ iex> MyRepo. to_sql(:all, Post)
327415 {"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []}
328416
329- iex> Ecto.Adapters.SQL.to_sql(:update_all, Repo,
330- from(p in Post, update: [set: [title: ^"hello"]]))
417+ iex> MyRepo.to_sql(:update_all, from(p in Post, update: [set: [title: ^"hello"]]))
331418 {"UPDATE posts AS p SET title = $1", ["hello"]}
332-
333- This function is also available under the repository with name `to_sql`:
334-
335- iex> Repo.to_sql(:all, Post)
336- {"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []}
337-
338419 """
420+
421+ @ doc @ to_sql_doc
339422 @ spec to_sql ( :all | :update_all | :delete_all , Ecto.Repo . t ( ) , Ecto.Queryable . t ( ) ) ::
340423 { String . t ( ) , query_params }
341424 def to_sql ( kind , repo , queryable ) do
@@ -596,9 +679,7 @@ defmodule Ecto.Adapters.SQL do
596679 sql_call ( adapter_meta , :query , [ sql ] , params , opts )
597680 end
598681
599- @ doc """
600- Same as `query_many/4` but raises on invalid queries.
601- """
682+ @ doc @ query_many_doc
602683 @ spec query_many! (
603684 Ecto.Repo . t ( ) | Ecto.Adapter . adapter_meta ( ) ,
604685 iodata ,
@@ -613,35 +694,7 @@ defmodule Ecto.Adapters.SQL do
613694 end
614695 end
615696
616- @ doc """
617- Runs a custom SQL query that returns multiple results on the given repo.
618-
619- In case of success, it must return an `:ok` tuple containing
620- a list of maps with at least two keys:
621-
622- * `:num_rows` - the number of rows affected
623-
624- * `:rows` - the result set as a list. `nil` may be returned
625- instead of the list if the command does not yield any row
626- as result (but still yields the number of affected rows,
627- like a `delete` command without returning would)
628-
629- ## Options
630-
631- * `:log` - When false, does not log the query
632- * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ @ timeout } `);
633-
634- ## Examples
635-
636- iex> Ecto.Adapters.SQL.query_many(MyRepo, "SELECT $1; SELECT $2;", [40, 2])
637- {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
638-
639- For convenience, this function is also available under the repository:
640-
641- iex> MyRepo.query_many("SELECT $1; SELECT $2;", [40, 2])
642- {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
643-
644- """
697+ @ doc @ query_many_doc
645698 @ spec query_many (
646699 pid ( ) | Ecto.Repo . t ( ) | Ecto.Adapter . adapter_meta ( ) ,
647700 iodata ,
@@ -772,107 +825,50 @@ defmodule Ecto.Adapters.SQL do
772825
773826 @ doc false
774827 def __before_compile__ ( driver , _env ) do
775- default_timeout = @ timeout
776-
777- quote bind_quoted: [ driver: driver , default_timeout: default_timeout ] do
778- @ doc """
779- Runs a custom SQL query.
780-
781- If the query was successful, it will return an `:ok` tuple containing
782- a map with at least two keys:
783- * `:num_rows` - the number of rows affected
784- * `:rows` - the result set as a list. `nil` may be returned
785- instead of the list if the command does not yield any row
786- as result (but still yields the number of affected rows,
787- like a `delete` command without returning would)
828+ disconnect_all_doc = @ disconnect_all_doc
829+ query_bang_doc = @ query_bang_doc
830+ query_doc = @ query_doc
831+ query_many_bang_doc = @ query_many_bang_doc
832+ query_many_doc = @ query_many_doc
833+ to_sql_doc = @ to_sql_doc
788834
789- ## Options
790- * `:log` - When false, does not log the query
791- * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ default_timeout } `);
792-
793- ## Examples
794- iex> MyRepo.query("SELECT $1::integer + $2", [40, 2])
795- {:ok, %{rows: [[42]], num_rows: 1}}
796- """
835+ quote do
836+ @ doc unquote ( query_doc )
797837 @ spec query ( iodata ( ) , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
798838 { :ok , Ecto.Adapters.SQL . query_result ( ) } | { :error , Exception . t ( ) }
799839 def query ( sql , params \\ [ ] , opts \\ [ ] ) do
800840 Ecto.Adapters.SQL . query ( get_dynamic_repo ( ) , sql , params , opts )
801841 end
802842
803- @ doc """
804- Runs a custom SQL query.
805-
806- Same as `query/3` but raises on invalid queries.
807- """
808- @ spec query ( iodata ( ) , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
809- { :ok , Ecto.Adapters.SQL . query_result ( ) } | { :error , Exception . t ( ) }
843+ @ doc unquote ( query_bang_doc )
844+ @ spec query! ( iodata ( ) , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
845+ Ecto.Adapters.SQL . query_result ( )
810846 def query! ( sql , params \\ [ ] , opts \\ [ ] ) do
811847 Ecto.Adapters.SQL . query! ( get_dynamic_repo ( ) , sql , params , opts )
812848 end
813849
814- @ doc """
815- Runs a custom SQL query that returns multiple results on the given repo.
816-
817- In case of success, it must return an `:ok` tuple containing
818- a list of maps with at least two keys:
819-
820- * `:num_rows` - the number of rows affected
821-
822- * `:rows` - the result set as a list. `nil` may be returned
823- instead of the list if the command does not yield any row
824- as result (but still yields the number of affected rows,
825- like a `delete` command without returning would)
826-
827- ## Options
828-
829- * `:log` - When false, does not log the query
830- * `:timeout` - Execute request timeout, accepts: `:infinity` (default: `#{ default_timeout } `);
831-
832- ## Examples
833-
834- iex> MyRepo.query_many("SELECT $1; SELECT $2;", [40, 2])
835- {:ok, [%{rows: [[40]], num_rows: 1}, %{rows: [[2]], num_rows: 1}]}
836- """
837-
850+ @ doc unquote ( query_many_doc )
838851 @ spec query_many ( iodata , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
839852 { :ok , [ Ecto.Adapters.SQL . query_result ( ) ] } | { :error , Exception . t ( ) }
840853 def query_many ( sql , params \\ [ ] , opts \\ [ ] ) do
841854 Ecto.Adapters.SQL . query_many ( get_dynamic_repo ( ) , sql , params , opts )
842855 end
843856
844- @ doc """
845- Same as `query_many/4` but raises on invalid queries.
846- """
857+ @ doc unquote ( query_many_bang_doc )
847858 @ spec query_many! ( iodata , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
848859 [ Ecto.Adapters.SQL . query_result ( ) ]
849860 def query_many! ( sql , params \\ [ ] , opts \\ [ ] ) do
850861 Ecto.Adapters.SQL . query_many! ( get_dynamic_repo ( ) , sql , params , opts )
851862 end
852863
853- @ doc """
854- Converts the given query to SQL according to its kind and the
855- adapter in the given repository.
856-
857- ## Examples
858-
859- The examples below are meant for reference. Each adapter will
860- return a different result:
861-
862- iex> MyRepo.to_sql(:all, Post)
863- {"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []}
864-
865- iex> MyRepo.to_sql(:update_all, from(p in Post, update: [set: [title: ^"hello"]]))
866- {"UPDATE posts AS p SET title = $1", ["hello"]}
867-
868- """
864+ @ doc unquote ( to_sql_doc )
869865 @ spec to_sql ( :all | :update_all | :delete_all , Ecto.Queryable . t ( ) ) ::
870866 { String . t ( ) , Ecto.Adapters.SQL . query_params ( ) }
871867 def to_sql ( operation , queryable ) do
872868 Ecto.Adapters.SQL . to_sql ( operation , get_dynamic_repo ( ) , queryable )
873869 end
874870
875- case driver do
871+ case unquote ( driver ) do
876872 :postgrex ->
877873 @ doc """
878874 Executes an EXPLAIN statement or similar for the given query according to its kind and the
@@ -978,22 +974,7 @@ defmodule Ecto.Adapters.SQL do
978974 Ecto.Adapters.SQL . explain ( get_dynamic_repo ( ) , operation , queryable , opts )
979975 end
980976
981- @ doc """
982- Forces all connections in the repo pool to disconnect within the given interval.
983-
984- Once this function is called, the pool will disconnect all of its connections
985- as they are checked in or as they are pinged. Checked in connections will be
986- randomly disconnected within the given time interval. Pinged connections are
987- immediately disconnected - as they are idle (according to `:idle_interval`).
988-
989- If the connection has a backoff configured (which is the case by default),
990- disconnecting means an attempt at a new connection will be done immediately
991- after, without starting a new process for each connection. However, if backoff
992- has been disabled, the connection process will terminate. In such cases,
993- disconnecting all connections may cause the pool supervisor to restart
994- depending on the max_restarts/max_seconds configuration of the pool,
995- so you will want to set those carefully.
996- """
977+ @ doc unquote ( disconnect_all_doc )
997978 @ spec disconnect_all ( non_neg_integer , opts :: Keyword . t ( ) ) :: :ok
998979 def disconnect_all ( interval , opts \\ [ ] ) do
999980 Ecto.Adapters.SQL . disconnect_all ( get_dynamic_repo ( ) , interval , opts )
0 commit comments