@@ -46,53 +46,34 @@ iex(6)> inspect(sql)
4646" ~SQL\"\"\"\n select\n id, \n email\n from\n users\n where\n email = {{email}}\n\"\"\" "
4747```
4848
49- ### Leverage the Enumerable protocol in your repository
50-
5149``` elixir
52- defmodule MyApp .Repo do
53- use Ecto .Repo , otp_app: :myapp , adapter: Ecto .Adapters .Postgres
54- use SQL , adapter: SQL .Adapters .Postgres
50+ defmodule MyApp .Accounts do
51+ use SQL , adapter: SQL .Adapters .Postgres , repo: MyApp .Repo
5552
56- defimpl Enumerable , for: SQL do
57- def count (_enumerable ) do
58- {:error , __MODULE__ }
59- end
60- def member? (_enumerable , _element ) do
61- {:error , __MODULE__ }
62- end
63- def reduce (%SQL {} = enumerable, _acc , _fun ) do
64- {sql, params} = SQL .to_sql (enumerable)
65- result = __MODULE__ .query! (sql, params)
66- {:done , Enum .map (result.rows, & Map .new (Enum .zip (result.columns, &1 )))}
67- end
68- def slice (_enumerable ) do
69- {:error , __MODULE__ }
70- end
53+ def list_users () do
54+ ~ SQL [from users select * ]
55+ |> SQL .map (fn row, columns, repo -> repo.load (User , {columns, row}) end )
56+ |> Enum .to_list ()
7157 end
7258 end
7359
74- iex (1 )> Enum .map (~ SQL [from users select * ], & IO .inspect / 1 )
75- %{" id" => 1 , " email" => " john@example.com" }
76- %{" id" => 2 , " email" => " jane@example.com" }
77- [%{" id" => 1 , " email" => " john@example.com" }, %{" id" => 2 , " email" => " jane@example.com" }]
60+ iex (1 )> MyApp .Accounts .list_users ()
61+ [%User {id: 1 , email: " john@example.com" }, %User {id: 2 , email: " jane@example.com" }]
7862```
7963
8064## Benchmark
8165You can find benchmark results [ here] ( https://github.com/elixir-dbvisor/sql/benchmarks ) or run ` mix sql.bench `
8266
8367## Installation
8468
85- If [ available in Hex] ( https://hex.pm/docs/publish ) , the package can be installed
86- by adding ` sql ` to your list of dependencies in ` mix.exs ` :
69+ The package can be installed by adding ` sql ` to your list of dependencies in ` mix.exs ` :
8770
8871``` elixir
8972def deps do
9073 [
91- {:sql , " ~> 0.3 .0" }
74+ {:sql , " ~> 0.4 .0" }
9275 ]
9376end
9477```
9578
96- Documentation can be generated with [ ExDoc] ( https://github.com/elixir-lang/ex_doc )
97- and published on [ HexDocs] ( https://hexdocs.pm ) . Once published, the docs can
98- be found at < https://hexdocs.pm/sql > .
79+ Documentation can be found at < https://hexdocs.pm/sql > .
0 commit comments