/
money_ecto_query_api.ex
294 lines (240 loc) · 8.72 KB
/
money_ecto_query_api.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
if Code.ensure_loaded?(Ecto.Query.API) do
defmodule Money.Ecto.Query.API do
@moduledoc """
Provides several helpers to query DB for the `Money` type.
Under the hood it delegates to
[`Ecto.Query.API.fragment/1`](https://hexdocs.pm/ecto/Ecto.Query.API.html#fragment/1-defining-custom-functions-using-macros-and-fragment),
but might be helpful for compile-type sanity check for typos and
better language server support.
It is also designed to be an implementation-agnostic, meaning one can use
these helpers without a necessity to explicitly specify a backing type.
The default implementation recommends a `Composite` adapter, which is used by default.
To use it with, say, `MySQL`, one should implement this behaviour for `MySQL` and declare
the implementation as `use Money.Ecto.Query.API, adapter: MyImpl.MySQL.Adapter`
Although the library provides the MySQL adapter too (`Money.Ecto.Query.API.Map.MySQL`)
but it is not actively maintained, so use it on your own.
If for some reason you use `Map` type with `Postgres`, helpers are still available
with `use Money.Ecto.Query.API, adapter: Money.Ecto.Query.API.Map.Postgres`
"""
@doc """
Native implementation of how to retrieve `amount` from the DB.
For `Postgres`, it delegates to the function on the composite type,
for other implementation it should return a `Ecto.Query.API.fragment/1`.
"""
@macrocallback amount(Macro.t()) :: Macro.t()
@doc """
Native implementation of how to retrieve `currency_code` from the DB.
For `Postgres`, it delegates to the function on the composite type,
for other implementation it should return a `Ecto.Query.API.fragment/1`.
"""
@macrocallback currency_code(Macro.t()) :: Macro.t()
@doc """
Native implementation of how to `sum` several records having a field
of the type `Money` in the DB.
For `Postgres`, it delegates to the function on the composite type,
for other implementation it should return a `Ecto.Query.API.fragment/1`.
"""
@macrocallback sum(Macro.t(), cast? :: boolean()) :: Macro.t()
@doc """
Cast decimal to the value accepted by the database.
"""
@callback cast_decimal(Decimal.t()) :: any()
@doc false
defmacro __using__(opts \\ [])
@doc false
defmacro __using__([]),
do: do_using(Money.Ecto.Query.API.Composite)
@doc false
defmacro __using__(adapter: adapter),
do: do_using(adapter)
defp do_using(adapter) do
quote do
import unquote(adapter)
import Money.Ecto.Query.API
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the same currency.
_Example:_
```elixir
iex> Organization
...> |> where([o], currency_eq(o.payroll, :AUD))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:AUD, "50"), Money.new(:AUD, "70")
```
"""
defmacro currency_eq(field, currency) when is_atom(currency) do
currency = currency |> to_string() |> String.upcase()
do_currency_eq(field, currency)
end
defmacro currency_eq(field, currency) when is_binary(currency) do
do_currency_eq(field, currency)
end
defp do_currency_eq(field, <<_::binary-size(3)>> = currency) do
quote do: currency_code(unquote(field)) == ^unquote(currency)
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the same amount.
_Example:_
```elixir
iex> Organization
...> |> where([o], amount_eq(o.payroll, 100))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:EUR, "100"), Money.new(:USD, "100")
```
"""
defmacro amount_eq(field, amount) when is_integer(amount) do
quote do
amount(unquote(field)) == ^unquote(amount)
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the same amount and currency.
_Example:_
```elixir
iex> Organization
...> |> where([o], money_eq(o.payroll, Money.new!(100, :USD)))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:USD, "100"), Money.new(:USD, "100")]
```
"""
defmacro money_eq(field, money) do
quote do
amount(unquote(field)) == ^cast_decimal(unquote(money).amount) and
currency_code(unquote(field)) == ^to_string(unquote(money).currency)
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having one
of currencies given as an argument.
_Example:_
```elixir
iex> Organization
...> |> where([o], currency_in(o.payroll, [:USD, :EUR]))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:EUR, "100"), Money.new(:USD, "100")]
```
"""
defmacro currency_in(field, currencies) when is_list(currencies) do
currencies = currencies |> Enum.map(&to_string/1) |> Enum.map(&String.upcase/1)
quote do
currency_code(unquote(field)) in ^unquote(currencies)
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the amount
in the range given as an argument.
Accepts `[min, max]`, `{min. max}`, and `min..max` as a range.
_Example:_
```elixir
iex> Organization
...> |> where([o], amount_in(o.payroll, 90..110))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:EUR, "100"), Money.new(:USD, "100")]
```
"""
defmacro amount_in(field, [min, max]),
do: do_amount_in(field, min, max)
defmacro amount_in(field, {min, max}),
do: do_amount_in(field, min, max)
defmacro amount_in(field, {:.., _, [min, max]}),
do: do_amount_in(field, min, max)
defmacro amount_in(field, {:"..//", _, [min, max, 1]}),
do: do_amount_in(field, min, max)
defmacro amount_in(field, {:"..//", _, [min, max, {:-, _, [1]}]}),
do: do_amount_in(field, max, min)
defmacro amount_in(field, {:"..//", _, [_min, _max, step]}) do
raise CompileError,
file: __CALLER__.file,
line: __CALLER__.line,
description:
"Ranges with a step (#{step}) are not supported for [#{Macro.to_string(field)}]"
end
defp do_amount_in(field, min, max) do
quote do
amount_ge(unquote(field), unquote(min)) and amount_le(unquote(field), unquote(max))
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the amount
greater than or equal to the one given as an argument.
_Example:_
```elixir
iex> Organization
...> |> where([o], amount_ge(o.payroll, 90))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:AUD, "90"), Money.new(:USD, "100")]
```
"""
defmacro amount_ge(field, num) do
quote do
amount(unquote(field)) >= ^unquote(num)
end
end
@doc """
`Ecto.Query.API` helper, allowing to filter records having the amount
less than or equal to the one given as an argument.
_Example:_
```elixir
iex> Organization
...> |> where([o], amount_le(o.payroll, 110))
...> |> select([o], o.payroll)
...> |> Repo.all()
[Money.new(:EUR, "100"), Money.new(:USD, "110")]
```
"""
defmacro amount_le(field, num) do
quote do
amount(unquote(field)) <= ^unquote(num)
end
end
@doc """
`Ecto.Query.API` helper, allowing to aggregate by currency, suming amount.
For more sophisticated aggregation, resort to raw `fragment`.
_Example:_
```elixir
iex> Organization
...> |> where([o], o.name == ^"Lemon Inc.")
...> |> total_by([o], o.payroll)
...> |> Repo.all()
[Money.new(:EUR, "100"), Money.new(:USD, "210")]
```
"""
defmacro total_by(query, binding, field) do
quote do
unquote(query)
|> where(unquote(binding), not is_nil(unquote(field)))
|> group_by(unquote(binding), [currency_code(unquote(field))])
|> select(unquote(binding), sum(unquote(field), true))
end
end
@doc """
`Ecto.Query.API` helper, allowing to aggregate by currency, suming amount.
Same as `total_by/3`, but for the single currency only.
_Example:_
```elixir
iex> Organization
...> |> where([o], o.name == ^"Lemon Inc.")
...> |> total_by([o], o.payroll, :USD)
...> |> Repo.one()
[Money.new(:USD, "210")]
```
"""
defmacro total_by(query, binding, field, currency) do
currency = currency |> to_string() |> String.upcase() |> List.wrap()
quote do
unquote(query)
|> where(unquote(binding), not is_nil(unquote(field)))
|> where(unquote(binding), currency_in(unquote(field), unquote(currency)))
|> group_by(unquote(binding), [currency_code(unquote(field))])
|> select(unquote(binding), sum(unquote(field), true))
end
end
end
end