-
Notifications
You must be signed in to change notification settings - Fork 4
/
where_or.ex
104 lines (79 loc) · 3.41 KB
/
where_or.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
defmodule FatEcto.FatQuery.WhereOr do
alias FatEcto.FatQuery.{FatDynamics, FatNotDynamics}
alias FatEcto.FatHelper
import Ecto.Query
def or_condition(queryable, nil, _build_options, _options), do: queryable
def or_condition(queryable, where_map, build_options, options) do
dynamics =
Enum.reduce(where_map, false, fn {k, map_cond}, dynamics ->
FatHelper.params_valid(queryable, k, build_options)
map_condition(k, dynamics, map_cond, options)
end)
from(q in queryable, where: ^dynamics)
end
def map_condition(k, dynamics, map_cond, opts) when is_map(map_cond) do
Enum.reduce(map_cond, dynamics, fn {key, value}, dynamics ->
case key do
"$like" ->
FatDynamics.like_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_like" ->
FatNotDynamics.not_like_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$ilike" ->
FatDynamics.ilike_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_ilike" ->
FatNotDynamics.not_ilike_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$lt" ->
FatDynamics.lt_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$lte" ->
FatDynamics.lte_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$gt" ->
FatDynamics.gt_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$gte" ->
FatDynamics.gte_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$between" ->
FatDynamics.between_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$between_equal" ->
FatDynamics.between_equal_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_between" ->
FatNotDynamics.not_between_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_between_equal" ->
FatNotDynamics.not_between_equal_dynamic(
k,
value,
dynamics,
opts ++ [dynamic_type: :or]
)
"$in" ->
FatDynamics.in_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_in" ->
FatNotDynamics.not_in_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$equal" ->
FatDynamics.eq_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
"$not_equal" ->
FatDynamics.not_eq_dynamic(k, value, dynamics, opts ++ [dynamic_type: :or])
_ ->
dynamics
end
end)
end
def map_condition(k, dynamics, map_cond, opts) when is_nil(map_cond) do
FatDynamics.is_nil_dynamic(k, dynamics, opts ++ [dynamic_type: :or])
end
def map_condition(k, dynamics, map_cond, opts)
when map_cond == "$not_null" do
FatNotDynamics.not_is_nil_dynamic(k, dynamics, opts ++ [dynamic_type: :or])
end
def map_condition(k, dynamics, map_cond, opts) when not is_list(map_cond) do
FatDynamics.eq_dynamic(k, map_cond, dynamics, opts ++ [dynamic_type: :or])
end
def map_condition(k, dynamics, map_cond, opts)
when is_list(map_cond) and k == "$not_null" do
Enum.reduce(map_cond, dynamics, fn key, dynamics ->
FatNotDynamics.not_is_nil_dynamic(key, dynamics, opts ++ [dynamic_type: :or])
end)
end
def map_condition(k, dynamics, map_cond, opts)
when is_list(map_cond) do
FatDynamics.eq_dynamic(k, map_cond, dynamics, opts ++ [dynamic_type: :or])
end
end