@@ -314,7 +314,95 @@ defmodule Ecto.Adapters.SQL do
314
314
315
315
@ timeout 15_000
316
316
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 """
318
406
Converts the given query to SQL according to its kind and the
319
407
adapter in the given repository.
320
408
@@ -323,19 +411,14 @@ defmodule Ecto.Adapters.SQL do
323
411
The examples below are meant for reference. Each adapter will
324
412
return a different result:
325
413
326
- iex> Ecto.Adapters.SQL. to_sql(:all, Repo , Post)
414
+ iex> MyRepo. to_sql(:all, Post)
327
415
{"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []}
328
416
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"]]))
331
418
{"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
-
338
419
"""
420
+
421
+ @ doc @ to_sql_doc
339
422
@ spec to_sql ( :all | :update_all | :delete_all , Ecto.Repo . t ( ) , Ecto.Queryable . t ( ) ) ::
340
423
{ String . t ( ) , query_params }
341
424
def to_sql ( kind , repo , queryable ) do
@@ -596,9 +679,7 @@ defmodule Ecto.Adapters.SQL do
596
679
sql_call ( adapter_meta , :query , [ sql ] , params , opts )
597
680
end
598
681
599
- @ doc """
600
- Same as `query_many/4` but raises on invalid queries.
601
- """
682
+ @ doc @ query_many_doc
602
683
@ spec query_many! (
603
684
Ecto.Repo . t ( ) | Ecto.Adapter . adapter_meta ( ) ,
604
685
iodata ,
@@ -613,35 +694,7 @@ defmodule Ecto.Adapters.SQL do
613
694
end
614
695
end
615
696
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
645
698
@ spec query_many (
646
699
pid ( ) | Ecto.Repo . t ( ) | Ecto.Adapter . adapter_meta ( ) ,
647
700
iodata ,
@@ -772,107 +825,50 @@ defmodule Ecto.Adapters.SQL do
772
825
773
826
@ doc false
774
827
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
788
834
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 )
797
837
@ spec query ( iodata ( ) , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
798
838
{ :ok , Ecto.Adapters.SQL . query_result ( ) } | { :error , Exception . t ( ) }
799
839
def query ( sql , params \\ [ ] , opts \\ [ ] ) do
800
840
Ecto.Adapters.SQL . query ( get_dynamic_repo ( ) , sql , params , opts )
801
841
end
802
842
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 ( )
810
846
def query! ( sql , params \\ [ ] , opts \\ [ ] ) do
811
847
Ecto.Adapters.SQL . query! ( get_dynamic_repo ( ) , sql , params , opts )
812
848
end
813
849
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 )
838
851
@ spec query_many ( iodata , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
839
852
{ :ok , [ Ecto.Adapters.SQL . query_result ( ) ] } | { :error , Exception . t ( ) }
840
853
def query_many ( sql , params \\ [ ] , opts \\ [ ] ) do
841
854
Ecto.Adapters.SQL . query_many ( get_dynamic_repo ( ) , sql , params , opts )
842
855
end
843
856
844
- @ doc """
845
- Same as `query_many/4` but raises on invalid queries.
846
- """
857
+ @ doc unquote ( query_many_bang_doc )
847
858
@ spec query_many! ( iodata , Ecto.Adapters.SQL . query_params ( ) , Keyword . t ( ) ) ::
848
859
[ Ecto.Adapters.SQL . query_result ( ) ]
849
860
def query_many! ( sql , params \\ [ ] , opts \\ [ ] ) do
850
861
Ecto.Adapters.SQL . query_many! ( get_dynamic_repo ( ) , sql , params , opts )
851
862
end
852
863
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 )
869
865
@ spec to_sql ( :all | :update_all | :delete_all , Ecto.Queryable . t ( ) ) ::
870
866
{ String . t ( ) , Ecto.Adapters.SQL . query_params ( ) }
871
867
def to_sql ( operation , queryable ) do
872
868
Ecto.Adapters.SQL . to_sql ( operation , get_dynamic_repo ( ) , queryable )
873
869
end
874
870
875
- case driver do
871
+ case unquote ( driver ) do
876
872
:postgrex ->
877
873
@ doc """
878
874
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
978
974
Ecto.Adapters.SQL . explain ( get_dynamic_repo ( ) , operation , queryable , opts )
979
975
end
980
976
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 )
997
978
@ spec disconnect_all ( non_neg_integer , opts :: Keyword . t ( ) ) :: :ok
998
979
def disconnect_all ( interval , opts \\ [ ] ) do
999
980
Ecto.Adapters.SQL . disconnect_all ( get_dynamic_repo ( ) , interval , opts )
0 commit comments