-
Notifications
You must be signed in to change notification settings - Fork 4
/
order_by.ex
251 lines (216 loc) · 9.38 KB
/
order_by.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
defmodule FatEcto.FatQuery.FatOrderBy do
import Ecto.Query
alias FatEcto.FatHelper
@moduledoc """
Builds query with asc or desc order.
## => $asc
### Parameters
- `queryable` - Ecto Queryable that represents your schema name, table name or query.
- `query_opts` - Include query options as a map.
### Example
iex> query_opts = %{
...> "$select" => %{
...> "$fields" => ["name", "location", "rating"],
...> "fat_rooms" => ["floor", "name"]
...> },
...> "$where" => %{"name" => "saint claire"},
...> "$group" => ["rating", "total_staff"],
...> "$order" => %{"total_staff" => "$asc"},
...> "$include" => %{
...> "fat_doctors" => %{
...> "$include" => ["fat_patients"],
...> "$where" => %{"rating" => %{"$gt" => 5}},
...> "$order" => %{"experience_years" => "$asc"},
...> "$select" => ["name", "designation", "phone"]
...> }
...> },
...> "$right_join" => %{
...> "fat_rooms" => %{
...> "$on_field" => "id",
...> "$on_table_field" => "hospital_id",
...> "$select" => ["floor", "name", "is_active"],
...> "$where" => %{"floor" => 10},
...> "$order" => %{"name" => "$asc"}
...> }
...> }
...> }
iex> #{MyApp.Query}.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in \"fat_rooms\", on: f0.id == f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.name == ^\"saint claire\" and ^true, where: f1.floor == ^10 and ^true, where: f2.rating > ^5 and ^true, group_by: [f0.rating], group_by: [f0.total_staff], order_by: [asc: f1.name], order_by: [asc: f2.experience_years], order_by: [asc: f0.total_staff], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:floor, :name]}]), %{^\"fat_rooms\" => map(f1, [:floor, :name, :is_active])}), preload: [[fat_doctors: [:fat_patients]]]>
### Options
- `$select` - Select the fields from `hospital` and `rooms`.
- `$right_join: :$select`- Select the fields from `rooms`.
- `$include: :$select` - Select the fields from `doctors`.
- `$right_join` - Right join the table `rooms`.
- `$include` - Include the assoication model `doctors` and `patients`.
- `$gt` - Added the greaterthan attribute in the where query inside include .
- `$order` - Sort the result based on the order attribute.
- `$right_join: :$order` - Sort the result based on the order attribute inside join.
- `$include: :$order` - Sort the result based on the order attribute inside include.
- `$group` - Added the group_by attribute in the query.
## => $desc
### Parameters
- `queryable` - Ecto Queryable that represents your schema name, table name or query.
- `query_opts` - Include query options as a map.
### Example
iex> query_opts = %{
...> "$select" => %{
...> "$fields" => ["name", "location", "rating"],
...> "fat_rooms" => ["floor", "name"]
...> },
...> "$where" => %{"name" => "saint claire"},
...> "$group" => ["rating", "total_staff"],
...> "$order" => %{"rating" => "$desc"},
...> "$include" => %{
...> "fat_doctors" => %{
...> "$include" => ["fat_patients"],
...> "$where" => %{"rating" => %{"$gt" => 5}},
...> "$order" => %{"experience_years" => "$asc"},
...> "$select" => ["name", "designation", "phone"]
...> }
...> },
...> "$right_join" => %{
...> "fat_rooms" => %{
...> "$on_field" => "id",
...> "$on_table_field" => "hospital_id",
...> "$select" => ["name", "floor", "is_active"],
...> "$where" => %{"floor" => 10},
...> "$order" => %{"floor" => "$desc"}
...> }
...> }
...> }
iex> #{MyApp.Query}.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in \"fat_rooms\", on: f0.id == f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.name == ^\"saint claire\" and ^true, where: f1.floor == ^10 and ^true, where: f2.rating > ^5 and ^true, group_by: [f0.rating], group_by: [f0.total_staff], order_by: [desc: f1.floor], order_by: [asc: f2.experience_years], order_by: [desc: f0.rating], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, fat_rooms: [:floor, :name]]), %{\n ^\"fat_rooms\" => map(f1, [:name, :floor, :is_active])\n}), preload: [fat_doctors: [:fat_patients]]>
### Options
- `$select` - Select the fields from `hospital` and `rooms`.
- `$right_join: :$select`- Select the fields from `rooms`.
- `$include: :$select` - Select the fields from `doctors`.
- `$right_join` - Right join the table `rooms`.
- `$include` - Include the assoication model `doctors` and `patients`.
- `$gt` - Added the greaterthan attribute in the where query inside include .
- `$order` - Sort the result based on the order attribute.
- `$right_join: :$order` - Sort the result based on the order attribute inside join.
- `$include: :$order` - Sort the result based on the order attribute inside include.
- `$group` - Added the group_by attribute in the query.
"""
alias FatEcto.FatHelper
def build_order_by(queryable, group_params, build_options, opts \\ [])
def build_order_by(queryable, nil, _build_options, _opts) do
queryable
end
@doc """
Order the results with respect to order_by clause in the params.
### Parameters
- `queryable` - Ecto Queryable that represents your schema name, table name or query.
- `order_by_params` - Order_By query options as a map.
- `opts` - Pass options related to query bindings.
- `build_options` - Pass options related to otp_app.
### Examples
iex> query_opts = %{
...> "$select" => %{
...> "$fields" => ["name", "location", "rating"],
...> "fat_rooms" => ["name", "floor"]
...> },
...> "$order" => %{"id" => "$asc"},
...> "$where" => %{"rating" => 4},
...> "$include" => %{
...> "fat_doctors" => %{
...> "$include" => ["fat_patients"],
...> "$where" => %{"designation" => "ham"},
...> "$order" => %{"id" => "$desc"}
...> }
...> }
...> }
iex> #{__MODULE__}.build_order_by(FatEcto.FatHospital, query_opts["$order"], [], [])
#Ecto.Query<from f0 in FatEcto.FatHospital, order_by: [asc: f0.id]>
"""
def build_order_by(queryable, order_by_params, build_options, opts) do
Enum.reduce(order_by_params, queryable, fn {field, format}, queryable ->
FatHelper.check_params_validity(build_options, queryable, field)
if opts[:binding] == :last do
case format do
"$desc" ->
from([q, ..., c] in queryable,
order_by: [desc: field(c, ^FatHelper.string_to_existing_atom(field))]
)
"$asc" ->
from(
[q, ..., c] in queryable,
order_by: [
asc: field(c, ^FatHelper.string_to_existing_atom(field))
]
)
"$asc_nulls_first" ->
from(
[q, ..., c] in queryable,
order_by: [
asc_nulls_first: field(c, ^FatHelper.string_to_existing_atom(field))
]
)
"$asc_nulls_last" ->
from(
[q, ..., c] in queryable,
order_by: [
asc_nulls_last: field(c, ^FatHelper.string_to_existing_atom(field))
]
)
"$desc_nulls_first" ->
from(
[q, ..., c] in queryable,
order_by: [
desc_nulls_first: field(c, ^FatHelper.string_to_existing_atom(field))
]
)
"$desc_nulls_last" ->
from(
[q, ..., c] in queryable,
order_by: [
desc_nulls_last: field(c, ^FatHelper.string_to_existing_atom(field))
]
)
end
else
case format do
"$desc" ->
from(queryable,
order_by: [desc: ^FatHelper.string_to_existing_atom(field)]
)
"$asc" ->
from(
queryable,
order_by: [
asc: ^FatHelper.string_to_existing_atom(field)
]
)
"$asc_nulls_first" ->
from(
queryable,
order_by: [
asc_nulls_first: ^FatHelper.string_to_existing_atom(field)
]
)
"$asc_nulls_last" ->
from(
queryable,
order_by: [
asc_nulls_last: ^FatHelper.string_to_existing_atom(field)
]
)
"$desc_nulls_first" ->
from(
queryable,
order_by: [
desc_nulls_first: ^FatHelper.string_to_existing_atom(field)
]
)
"$desc_nulls_last" ->
from(
queryable,
order_by: [
desc_nulls_last: ^FatHelper.string_to_existing_atom(field)
]
)
end
end
end)
end
end