/
repo.ex
424 lines (313 loc) · 12.5 KB
/
repo.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
defmodule Ecto.Repo do
@moduledoc """
Defines a repository.
A repository maps to a data store, for example an SQL database.
A repository must implement `conf/0` and set an adapter (see `Ecto.Adapter`)
to be used for the repository.
When used, the following options are allowed:
* `:adapter` - the adapter to be used for the repository
* `:env` - configures the repository to support environments
## Example
defmodule MyRepo do
use Ecto.Repo, adapter: Ecto.Adapters.Postgres
def conf do
parse_url "ecto://postgres:postgres@localhost/postgres"
end
end
Most of the time, we want the repository to work with different
environments. In such cases, we can pass an `:env` option:
defmodule MyRepo do
use Ecto.Repo, adapter: Ecto.Adapters.Postgres, env: Mix.env
def conf(env), do: parse_url url(env)
defp url(:dev), do: "ecto://postgres:postgres@localhost/postgres_dev"
defp url(:test), do: "ecto://postgres:postgres@localhost/postgres_test?size=1"
defp url(:prod), do: "ecto://postgres:postgres@localhost/postgres_prod"
end
Notice that, when using the environment, developers should implement
`conf/1` which automatically passes the environment instead of `conf/0`.
Note the environment is only used at compilation time. That said, make
sure the `:build_per_environment` option is set to true (the default)
in your Mix project configuration.
"""
use Behaviour
@type t :: module
@doc false
defmacro __using__(opts) do
adapter = Macro.expand(Keyword.fetch!(opts, :adapter), __CALLER__)
env = Keyword.get(opts, :env)
unless Code.ensure_loaded?(adapter) do
raise ArgumentError, message: "Adapter #{inspect adapter} was not compiled, " <>
"ensure its driver is included as a dependency of your project"
end
quote do
use unquote(adapter)
@behaviour Ecto.Repo
@env unquote(env)
require Logger
import Ecto.Utils, only: [parse_url: 1]
import Application, only: [app_dir: 2]
if @env do
def conf do
conf(@env)
end
defoverridable conf: 0
end
def start_link do
unquote(adapter).start_link(__MODULE__, conf)
end
def stop do
unquote(adapter).stop(__MODULE__)
end
def transaction(opts \\ [], fun) do
unquote(adapter).transaction(__MODULE__, opts, fun)
end
def rollback(value) do
unquote(adapter).rollback(__MODULE__, value)
end
def all(queryable, opts \\ []) do
Ecto.Repo.Queryable.all(__MODULE__, unquote(adapter), queryable, opts)
end
def get(queryable, id, opts \\ []) do
Ecto.Repo.Queryable.get(__MODULE__, unquote(adapter), queryable, id, opts)
end
def get!(queryable, id, opts \\ []) do
Ecto.Repo.Queryable.get!(__MODULE__, unquote(adapter), queryable, id, opts)
end
def one(queryable, opts \\ []) do
Ecto.Repo.Queryable.one(__MODULE__, unquote(adapter), queryable, opts)
end
def one!(queryable, opts \\ []) do
Ecto.Repo.Queryable.one!(__MODULE__, unquote(adapter), queryable, opts)
end
defmacro update_all(queryable, values, opts \\ []) do
Ecto.Repo.Queryable.update_all(__MODULE__, unquote(adapter), queryable,
values, opts)
end
def delete_all(queryable, opts \\ []) do
Ecto.Repo.Queryable.delete_all(__MODULE__, unquote(adapter), queryable, opts)
end
def insert(model, opts \\ []) do
Ecto.Repo.Model.insert(__MODULE__, unquote(adapter), model, opts)
end
def update(model, opts \\ []) do
Ecto.Repo.Model.update(__MODULE__, unquote(adapter), model, opts)
end
def delete(model, opts \\ []) do
Ecto.Repo.Model.delete(__MODULE__, unquote(adapter), model, opts)
end
def preload(model_or_models, preloads) do
Ecto.Repo.Preloader.preload(model_or_models, __MODULE__, preloads)
end
def adapter do
unquote(adapter)
end
def __repo__ do
true
end
# TODO: Should we keep this as overridable?
def log({:query, sql}, fun) do
{time, result} = :timer.tc(fun)
Logger.debug fn -> [sql, " (", inspect(time), "µs)"] end
result
end
def log(_arg, fun) do
fun.()
end
defoverridable [log: 2]
end
end
@doc """
Returns the adapter tied to the repository.
"""
defcallback adapter() :: Ecto.Adapter.t
@doc """
Simply returns true to mark this module as a repository.
"""
defcallback __repo__ :: true
@doc """
Should return the database options that will be given to the adapter. Often
used in conjunction with `parse_url/1`. This function must be implemented by
the user.
"""
defcallback conf() :: Keyword.t
@doc """
Starts any connection pooling or supervision and return `{:ok, pid}`
or just `:ok` if nothing needs to be done.
Returns `{:error, {:already_started, pid}}` if the repo already
started or `{:error, term}` in case anything else goes wrong.
"""
defcallback start_link() :: {:ok, pid} | :ok |
{:error, {:already_started, pid}} |
{:error, term}
@doc """
Stops any connection pooling or supervision started with `start_link/1`.
"""
defcallback stop() :: :ok
@doc """
Fetches a single model from the data store where the primary key matches the
given id.
Returns `nil` if no result was found. If the model in the queryable
has no primary key `Ecto.NoPrimaryKeyError` will be raised.
`Ecto.AdapterError` will be raised if there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
"""
defcallback get(Ecto.Queryable.t, term, Keyword.t) :: Ecto.Model.t | nil | no_return
@doc """
Similar to `get/3` but raises `Ecto.NotSingleResult` if no record was found.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
"""
defcallback get!(Ecto.Queryable.t, term, Keyword.t) :: Ecto.Model.t | nil | no_return
@doc """
Fetches a single result from the query.
Returns `nil` if no result was found.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
"""
defcallback one(Ecto.Queryable.t, Keyword.t) :: Ecto.Model.t | nil | no_return
@doc """
Similar to `one/3` but raises `Ecto.NotSingleResult` if no record was found.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
"""
defcallback one!(Ecto.Queryable.t, Keyword.t) :: Ecto.Model.t | nil | no_return
@doc """
Preloads all associations on the givne model or models.
`preloads` is a list of associations that can be nested in rose
tree structure:
node :: atom | {atom, node} | [node]
"""
defcallback preload([Ecto.Model.t] | Ecto.Model.t, preloads :: term) ::
[Ecto.Model.t] | Ecto.Model.t
@doc """
Fetches all results from the data store based on the given query. May raise
`Ecto.QueryError` if query validation fails. `Ecto.AdapterError` will be
raised if there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Example
# Fetch all post titles
query = from p in Post,
select: p.title
MyRepo.all(query)
"""
defcallback all(Ecto.Query.t, Keyword.t) :: [Ecto.Model.t] | no_return
@doc """
Updates all entities matching the given query with the given values.
`Ecto.AdapterError` will be raised if there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Examples
MyRepo.update_all(Post, title: "New title")
MyRepo.update_all(p in Post, visits: p.visits + 1)
from(p in Post, where: p.id < 10)
|> MyRepo.update_all(title: "New title")
"""
defmacrocallback update_all(Macro.t, Keyword.t, Keyword.t) :: integer | no_return
@doc """
Deletes all entities matching the given query with the given values.
`Ecto.AdapterError` will be raised if there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Examples
MyRepo.delete_all(Post)
from(p in Post, where: p.id < 10) |> MyRepo.delete_all
"""
defcallback delete_all(Ecto.Queryable.t, Keyword.t) :: integer | no_return
@doc """
Stores a single new model in the data store and returns its stored
representation. May raise `Ecto.AdapterError` if there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Example
post = %Post{title: "Ecto is great", text: "really, it is"}
|> MyRepo.insert
"""
defcallback insert(Ecto.Model.t, Keyword.t) :: Ecto.Model.t | no_return
@doc """
Updates an model using the primary key as key. If the model has no primary
key `Ecto.NoPrimaryKeyError` will be raised. `Ecto.AdapterError` will be raised if
there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Example
[post] = from p in Post, where: p.id == 42
post = %{post | title: "New title"}
MyRepo.update(post)
"""
defcallback update(Ecto.Model.t, Keyword.t) :: Ecto.Model.t | no_return
@doc """
Deletes an model using the primary key as key. If the model has no primary
key `Ecto.NoPrimaryKeyError` will be raised. `Ecto.AdapterError` will be raised if
there is an adapter error.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Example
[post] = MyRepo.all(from(p in Post, where: p.id == 42))
MyRepo.delete(post)
"""
defcallback delete(Ecto.Model.t, Keyword.t) :: Ecto.Model.t | no_return
@doc """
Runs the given function inside a transaction. If an unhandled error occurs the
transaction will be rolled back. If no error occurred the transaction will be
commited when the function returns. A transaction can be explicitly rolled
back by calling `rollback`, this will immediately leave the function and
return the value given to `rollback` as `{:error, value}`. A successful
transaction returns the value returned by the function wrapped in a tuple as
`{:ok, value}`. Transactions can be nested.
## Options
`:timeout` - The time in milliseconds to wait for the call to finish,
`:infinity` will wait indefinitely (default: 5000);
## Examples
MyRepo.transaction(fn ->
MyRepo.update(%{alice | balance: alice.balance - 10})
MyRepo.update(%{bob | balance: bob.balance + 10})
end)
# In the following example only the comment will be rolled back
MyRepo.transaction(fn ->
MyRepo.insert(%Post{})
MyRepo.transaction(fn ->
MyRepo.insert(%Comment{})
raise "error"
end)
end)
# Roll back a transaction explicitly
MyRepo.transaction(fn ->
p = MyRepo.insert(%Post{})
if not Editor.post_allowed?(p) do
MyRepo.rollback(:posting_not_allowed)
end
end)
"""
defcallback transaction(Keyword.t, fun) :: {:ok, any} | {:error, any}
@doc """
Rolls back the current transaction.
The transaction will return the value given as `{:error, value}`.
"""
defcallback rollback(any) :: no_return
@doc """
Enables logging and debugging of adapter actions such as sending queries to
the database. By default writes to Logger but can be overriden to customize
behaviour.
You must return the result of calling the passed in function.
## Examples
def log({:query, sql}, fun) do
{time, result} = :timer.tc(fun)
Logger.debug inspect{sql, time}
result
end
def log(_arg, fun), do: fun.()
"""
defcallback log({:query, String.t} | :begin | :commit | :rollback, (() -> any)) :: any
end