forked from bluzky/querie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.ex
247 lines (198 loc) · 5.92 KB
/
filter.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
defmodule Querie.Filter do
import Ecto.Query
import Kernel, except: [apply: 2, apply: 3]
@doc """
Apply filter on multiple column
Example:
%{
id: 10,
not: %{
type: "work"
},
or: %{
team_id: 10,
and: %{
team_id: 11,
role: "manager"
}
}
}
# id = 10 and not (type = "work") and (team_id = 10 or (team_id = 11 and role = "manager" ))
"""
def apply(query, filters, opts \\ [])
def apply(query, filters, opts) when is_map(filters) do
filters = Map.to_list(filters)
apply(query, filters, opts)
end
def apply(query, filters, opts) when is_list(filters) do
{sort, filters} = Keyword.pop(filters, :_sort, [])
# skip field starts with underscore
grouped_by_type =
filters
|> Enum.reject(fn
{_, {column, _}} -> String.starts_with?(to_string(column), "_")
{column, _} -> String.starts_with?(to_string(column), "_")
_ -> false
end)
|> Enum.group_by(fn
{_, {:ref, _}} -> :ref
_ -> :filter
end)
column_filter = grouped_by_type[:filter] || []
column_filter =
if opts[:skip_nil] do
Enum.reject(column_filter, fn
{_, nil} -> true
{_, {_, nil}} -> true
_ -> false
end)
else
column_filter
end
d_query = filter(:and, column_filter)
query
|> where([q], ^d_query)
|> join_ref(grouped_by_type[:ref])
|> sort(sort)
end
# add single column query condition to existing query
def filter(query, column, params) do
d_query = filter(column, params)
query
|> where([q], ^d_query)
end
@doc """
Apply filter on single column
If filter value is list, filter row that match any value in the list
"""
def filter(:and, filters) when is_map(filters) or is_list(filters) do
Enum.reduce(filters, true, fn {key, val}, acc ->
ft = filter(key, val)
cond do
ft && acc == true -> dynamic([q], ^ft)
ft -> dynamic([q], ^acc and ^ft)
true -> acc
end
end)
end
def filter(:or, filters) when is_map(filters) or is_list(filters) do
Enum.reduce(filters, false, fn {key, val}, acc ->
ft = filter(key, val)
cond do
ft && acc == false -> dynamic([q], ^ft)
ft -> dynamic([q], ^acc or ^ft)
true -> acc
end
end)
end
def filter(:not, filters) when is_map(filters) or is_list(filters) do
d_query = filter(:and, filters)
dynamic([q], not (^d_query))
end
def filter(_column, {:in, nil}) do
nil
end
def filter(column, {:in, values}) do
dynamic([q], field(q, ^column) in ^values)
end
def filter(column, {:is, nil}) do
dynamic([q], is_nil(field(q, ^column)))
end
def filter(column, {:is, value}) do
dynamic([q], field(q, ^column) == ^value)
end
def filter(_column, {_op, nil}) do
nil
end
def filter(column, {:gt, value}) do
dynamic([q], field(q, ^column) > ^value)
end
def filter(column, {:lt, value}) do
dynamic([q], field(q, ^column) < ^value)
end
def filter(column, {:ge, value}) do
dynamic([q], field(q, ^column) >= ^value)
end
def filter(column, {:le, value}) do
dynamic([q], field(q, ^column) <= ^value)
end
def filter(column, {:ne, value}) do
dynamic([q], field(q, ^column) != ^value)
end
def filter(column, {:not, value}) do
filter(column, {:ne, value})
end
def filter(column, {:between, [lower, upper]}) do
case [lower, upper] do
[nil, nil] -> nil
[nil, upper] -> dynamic([q], field(q, ^column) < ^upper)
[lower, nil] -> dynamic([q], field(q, ^column) > ^lower)
_ -> dynamic([q], field(q, ^column) > ^lower and field(q, ^column) < ^upper)
end
end
@doc """
between inclusive
"""
def filter(column, {:ibetween, [lower, upper]}) do
case [lower, upper] do
[nil, nil] -> nil
[nil, upper] -> dynamic([q], field(q, ^column) <= ^upper)
[lower, nil] -> dynamic([q], field(q, ^column) >= ^lower)
_ -> dynamic([q], field(q, ^column) >= ^lower and field(q, ^column) <= ^upper)
end
end
def filter(_, {:between, _}), do: nil
def filter(_, {:ibetween, _}), do: nil
def filter(column, {:has, value}) do
dynamic([q], ^value in field(q, ^column))
end
def filter(_, {_, nil}), do: nil
def filter(column, {:contains, value}) do
dynamic([q], like(field(q, ^column), ^"%#{value}%"))
end
def filter(column, {:icontains, value}) do
dynamic([q], ilike(field(q, ^column), ^"%#{value}%"))
end
def filter(column, {:like, value}) do
dynamic([q], like(field(q, ^column), ^"%#{value}%"))
end
def filter(column, {:ilike, value}) do
dynamic([q], ilike(field(q, ^column), ^"%#{value}%"))
end
def filter(column, values) when is_list(values) do
filter(column, {:in, values})
end
def filter(column, value) do
filter(column, {:is, value})
end
def sort(query, fields) do
if is_nil(fields) or Enum.empty?(fields) do
query
else
order =
fields
|> Enum.map(fn {column, direction} ->
{direction, column}
end)
order_by(query, ^order)
end
end
def join_ref(query, ref, opts \\ [])
def join_ref(query, nil, _), do: query
def join_ref(query, refs, query_opts) when is_list(refs) do
Enum.reduce(refs, query, fn {column, {:ref, ref_filter}}, query ->
join_ref(query, {column, ref_filter}, query_opts)
end)
end
def join_ref(query, {column, {model, filter, opts}}, query_opts) do
foreign_key = Keyword.get(opts, :foreign_key, :"#{column}_id")
references = Keyword.get(opts, :references, :id)
ref_query = __MODULE__.apply(model, filter, query_opts)
join(query, :inner, [a], b in ^ref_query, on: field(a, ^foreign_key) == field(b, ^references))
end
def join_ref(query, _, _), do: query
def search(query, fields, term) when is_list(fields) do
filters = %{or: Enum.map(fields, fn field -> {field, {:ilike, term}} end)}
apply(query, filters, [])
end
end