]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Query/Join.hs
Merge branch 'dev' into dev-docs-download
[gargantext.git] / src / Gargantext / Database / Query / Join.hs
1 {-|
2 Module : Gargantext.Database.Query.Join
3 Description : Main Join queries (using Opaleye)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Multiple Join functions with Opaleye.
11
12 -}
13
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
15
16
17 {-# LANGUAGE Arrows #-}
18 {-# LANGUAGE FunctionalDependencies #-}
19 {-# LANGUAGE QuasiQuotes #-}
20 {-# LANGUAGE NoMonomorphismRestriction #-}
21 {-# LANGUAGE TemplateHaskell #-}
22
23 ------------------------------------------------------------------------
24
25 module Gargantext.Database.Query.Join ( leftJoin2
26 , leftJoin3
27 , leftJoin4
28 , leftJoin5
29 , leftJoin6
30 , leftJoin7
31 , leftJoin8
32 , leftJoin9
33 )
34 where
35
36 import Control.Arrow ((>>>), returnA)
37 import Data.Profunctor.Product.Default
38 import Gargantext.Prelude
39 import Opaleye hiding (keepWhen)
40 import Opaleye.Internal.Join (NullMaker(..))
41 import qualified Opaleye.Internal.Unpackspec()
42
43
44 keepWhen :: (a -> Field SqlBool) -> SelectArr a a
45 keepWhen p = proc a -> do
46 restrict -< p a
47 returnA -< a
48
49 ------------------------------------------------------------------------
50 leftJoin2 :: (Default Unpackspec fieldsL fieldsL,
51 Default Unpackspec fieldsR fieldsR,
52 Default NullMaker fieldsR nullableFieldsR) =>
53 Select fieldsL
54 -> Select fieldsR
55 -> ((fieldsL, fieldsR) -> Column SqlBool)
56 -> Select (fieldsL, nullableFieldsR)
57 leftJoin2 = leftJoin
58
59 ------------------------------------------------------------------------
60 -- | LeftJoin3 in two ways to write it
61 _leftJoin3 :: Select columnsA -> Select columnsB -> Select columnsC
62 -> ((columnsA, columnsB, columnsC) -> Column SqlBool)
63 -> Select (columnsA, columnsB, columnsC)
64 _leftJoin3 q1 q2 q3 cond = ((,,) <$> q1 <*> q2 <*> q3) >>> keepWhen cond
65
66
67 leftJoin3 :: ( Default Unpackspec b2 b2
68 , Default Unpackspec b3 b3
69 , Default Unpackspec fieldsL fieldsL
70 , Default Unpackspec fieldsR fieldsR
71 , Default NullMaker b3 b4
72 , Default NullMaker b2 b5
73 , Default NullMaker fieldsR b2) =>
74 Select fieldsR
75 -> Select b3
76 -> Select fieldsL
77 -> ((b3, fieldsR) -> Column SqlBool)
78 -> ((fieldsL, (b3, b2)) -> Column SqlBool)
79 -> Select (fieldsL, (b4, b5))
80
81 leftJoin3 q1 q2 q3
82 cond12 cond23 =
83 leftJoin q3 ( leftJoin q2 q1 cond12) cond23
84
85
86 leftJoin4 :: (Default Unpackspec b2 b2,
87 Default Unpackspec fieldsL fieldsL, Default Unpackspec b3 b3,
88 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
89 Default Unpackspec b6 b6, Default Unpackspec fieldsR fieldsR,
90 Default NullMaker b2 b7, Default NullMaker b5 b8,
91 Default NullMaker b6 b9, Default NullMaker b3 b5,
92 Default NullMaker b4 b6, Default NullMaker fieldsR b4) =>
93 Select fieldsR
94 -> Select b3
95 -> Select b2
96 -> Select fieldsL
97 -> ((b3, fieldsR) -> Column SqlBool)
98 -> ((b2, (b3, b4)) -> Column SqlBool)
99 -> ((fieldsL, (b2, (b5, b6))) -> Column SqlBool)
100 -> Select (fieldsL, (b7, (b8, b9)))
101 leftJoin4 q1 q2 q3 q4
102 cond12 cond23 cond34 =
103 leftJoin q4 ( leftJoin q3
104 ( leftJoin q2 q1
105 cond12
106 ) cond23
107 ) cond34
108
109
110 leftJoin5 :: (Default Unpackspec b2 b2, Default Unpackspec b3 b3,
111 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
112 Default Unpackspec b6 b6, Default Unpackspec b7 b7,
113 Default Unpackspec fieldsL fieldsL, Default Unpackspec b8 b8,
114 Default Unpackspec b9 b9, Default Unpackspec b10 b10,
115 Default Unpackspec fieldsR fieldsR, Default NullMaker b7 b6,
116 Default NullMaker b6 b11, Default NullMaker b8 b12,
117 Default NullMaker b3 b13, Default NullMaker b2 b14,
118 Default NullMaker b9 b3, Default NullMaker b10 b2,
119 Default NullMaker b5 b9, Default NullMaker b4 b10,
120 Default NullMaker fieldsR b4) =>
121 Select fieldsR
122 -> Select b5
123 -> Select b7
124 -> Select b8
125 -> Select fieldsL
126 -> ((b5, fieldsR) -> Column SqlBool)
127 -> ((b7, (b5, b4)) -> Column SqlBool)
128 -> ((b8, (b7, (b9, b10))) -> Column SqlBool)
129 -> ((fieldsL, (b8, (b6, (b3, b2)))) -> Column SqlBool)
130 -> Select (fieldsL, (b12, (b11, (b13, b14))))
131 leftJoin5 q1 q2 q3 q4 q5
132 cond12 cond23 cond34 cond45 =
133 leftJoin q5 ( leftJoin q4
134 ( leftJoin q3
135 ( leftJoin q2 q1
136 cond12
137 ) cond23
138 ) cond34
139 ) cond45
140
141
142 leftJoin6 :: (Default Unpackspec b2 b2, Default Unpackspec b3 b3,
143 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
144 Default Unpackspec fieldsL fieldsL, Default Unpackspec b6 b6,
145 Default Unpackspec b7 b7, Default Unpackspec b8 b8,
146 Default Unpackspec b9 b9, Default Unpackspec b10 b10,
147 Default Unpackspec b11 b11, Default Unpackspec b12 b12,
148 Default Unpackspec b13 b13, Default Unpackspec b14 b14,
149 Default Unpackspec b15 b15, Default Unpackspec fieldsR fieldsR,
150 Default NullMaker b5 b4, Default NullMaker b4 b16,
151 Default NullMaker b6 b17, Default NullMaker b2 b18,
152 Default NullMaker b7 b2, Default NullMaker b3 b7,
153 Default NullMaker b12 b19, Default NullMaker b13 b20,
154 Default NullMaker b10 b12, Default NullMaker b11 b13,
155 Default NullMaker b14 b10, Default NullMaker b15 b11,
156 Default NullMaker b8 b14, Default NullMaker b9 b15,
157 Default NullMaker fieldsR b9) =>
158 Select fieldsR
159 -> Select b8
160 -> Select b3
161 -> Select b5
162 -> Select b6
163 -> Select fieldsL
164 -> ((b8, fieldsR) -> Column SqlBool)
165 -> ((b3, (b8, b9)) -> Column SqlBool)
166 -> ((b5, (b3, (b14, b15))) -> Column SqlBool)
167 -> ((b6, (b5, (b7, (b10, b11)))) -> Column SqlBool)
168 -> ((fieldsL, (b6, (b4, (b2, (b12, b13))))) -> Column SqlBool)
169 -> Select (fieldsL, (b17, (b16, (b18, (b19, b20)))))
170 leftJoin6 q1 q2 q3 q4 q5 q6
171 cond12 cond23 cond34 cond45 cond56 =
172 leftJoin q6 ( leftJoin q5
173 ( leftJoin q4
174 ( leftJoin q3
175 ( leftJoin q2 q1
176 cond12
177 ) cond23
178 ) cond34
179 ) cond45
180 ) cond56
181
182
183 leftJoin7 :: (Default Unpackspec b2 b2, Default Unpackspec b3 b3,
184 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
185 Default Unpackspec b6 b6, Default Unpackspec b7 b7,
186 Default Unpackspec b8 b8, Default Unpackspec b9 b9,
187 Default Unpackspec b10 b10, Default Unpackspec b11 b11,
188 Default Unpackspec b12 b12, Default Unpackspec b13 b13,
189 Default Unpackspec fieldsL fieldsL, Default Unpackspec b14 b14,
190 Default Unpackspec b15 b15, Default Unpackspec b16 b16,
191 Default Unpackspec b17 b17, Default Unpackspec b18 b18,
192 Default Unpackspec b19 b19, Default Unpackspec b20 b20,
193 Default Unpackspec b21 b21, Default Unpackspec fieldsR fieldsR,
194 Default NullMaker b11 b8, Default NullMaker b8 b10,
195 Default NullMaker b10 b9, Default NullMaker b9 b22,
196 Default NullMaker b16 b12, Default NullMaker b12 b17,
197 Default NullMaker b17 b23, Default NullMaker b13 b24,
198 Default NullMaker b15 b25, Default NullMaker b14 b15,
199 Default NullMaker b3 b26, Default NullMaker b2 b27,
200 Default NullMaker b18 b3, Default NullMaker b19 b2,
201 Default NullMaker b5 b18, Default NullMaker b4 b19,
202 Default NullMaker b20 b5, Default NullMaker b21 b4,
203 Default NullMaker b7 b20, Default NullMaker b6 b21,
204 Default NullMaker fieldsR b6) =>
205 Select fieldsR
206 -> Select b7
207 -> Select b11
208 -> Select b16
209 -> Select b14
210 -> Select b13
211 -> Select fieldsL
212 -> ((b7, fieldsR) -> Column SqlBool)
213 -> ((b11, (b7, b6)) -> Column SqlBool)
214 -> ((b16, (b11, (b20, b21))) -> Column SqlBool)
215 -> ((b14, (b16, (b8, (b5, b4)))) -> Column SqlBool)
216 -> ((b13, (b14, (b12, (b10, (b18, b19))))) -> Column SqlBool)
217 -> ((fieldsL, (b13, (b15, (b17, (b9, (b3, b2))))))
218 -> Column SqlBool)
219 -> Select (fieldsL, (b24, (b25, (b23, (b22, (b26, b27))))))
220 leftJoin7 q1 q2 q3 q4 q5 q6 q7
221 cond12 cond23 cond34 cond45 cond56 cond67 =
222 leftJoin q7 ( leftJoin q6
223 ( leftJoin q5
224 ( leftJoin q4
225 ( leftJoin q3
226 ( leftJoin q2 q1
227 cond12
228 ) cond23
229 ) cond34
230 ) cond45
231 ) cond56
232 ) cond67
233
234
235 leftJoin8 :: (Default Unpackspec b2 b2, Default Unpackspec b3 b3,
236 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
237 Default Unpackspec b6 b6, Default Unpackspec b7 b7,
238 Default Unpackspec b8 b8, Default Unpackspec b9 b9,
239 Default Unpackspec b10 b10, Default Unpackspec fieldsL fieldsL,
240 Default Unpackspec b11 b11, Default Unpackspec b12 b12,
241 Default Unpackspec b13 b13, Default Unpackspec b14 b14,
242 Default Unpackspec b15 b15, Default Unpackspec b16 b16,
243 Default Unpackspec b17 b17, Default Unpackspec b18 b18,
244 Default Unpackspec b19 b19, Default Unpackspec b20 b20,
245 Default Unpackspec b21 b21, Default Unpackspec b22 b22,
246 Default Unpackspec b23 b23, Default Unpackspec b24 b24,
247 Default Unpackspec b25 b25, Default Unpackspec b26 b26,
248 Default Unpackspec b27 b27, Default Unpackspec b28 b28,
249 Default Unpackspec fieldsR fieldsR, Default NullMaker b8 b5,
250 Default NullMaker b5 b7, Default NullMaker b7 b6,
251 Default NullMaker b6 b29, Default NullMaker b13 b9,
252 Default NullMaker b9 b14, Default NullMaker b14 b30,
253 Default NullMaker b10 b31, Default NullMaker b12 b32,
254 Default NullMaker b11 b12, Default NullMaker b2 b33,
255 Default NullMaker b15 b2, Default NullMaker b3 b15,
256 Default NullMaker b16 b3, Default NullMaker b4 b16,
257 Default NullMaker b23 b34, Default NullMaker b24 b35,
258 Default NullMaker b21 b23, Default NullMaker b22 b24,
259 Default NullMaker b25 b21, Default NullMaker b26 b22,
260 Default NullMaker b19 b25, Default NullMaker b20 b26,
261 Default NullMaker b27 b19, Default NullMaker b28 b20,
262 Default NullMaker b17 b27, Default NullMaker b18 b28,
263 Default NullMaker fieldsR b18) =>
264 Select fieldsR
265 -> Select b17
266 -> Select b4
267 -> Select b8
268 -> Select b13
269 -> Select b11
270 -> Select b10
271 -> Select fieldsL
272 -> ((b17, fieldsR) -> Column SqlBool)
273 -> ((b4, (b17, b18)) -> Column SqlBool)
274 -> ((b8, (b4, (b27, b28))) -> Column SqlBool)
275 -> ((b13, (b8, (b16, (b19, b20)))) -> Column SqlBool)
276 -> ((b11, (b13, (b5, (b3, (b25, b26))))) -> Column SqlBool)
277 -> ((b10, (b11, (b9, (b7, (b15, (b21, b22)))))) -> Column SqlBool)
278 -> ((fieldsL, (b10, (b12, (b14, (b6, (b2, (b23, b24)))))))
279 -> Column SqlBool)
280 -> Select (fieldsL, (b31, (b32, (b30, (b29, (b33, (b34, b35)))))))
281 leftJoin8 q1 q2 q3 q4 q5 q6 q7 q8
282 cond12 cond23 cond34 cond45 cond56 cond67 cond78 =
283 leftJoin q8 ( leftJoin q7
284 ( leftJoin q6
285 ( leftJoin q5
286 ( leftJoin q4
287 ( leftJoin q3
288 ( leftJoin q2 q1
289 cond12
290 ) cond23
291 ) cond34
292 ) cond45
293 ) cond56
294 ) cond67
295 ) cond78
296
297
298
299 leftJoin9 :: (Default Unpackspec b2 b2, Default Unpackspec b3 b3,
300 Default Unpackspec b4 b4, Default Unpackspec b5 b5,
301 Default Unpackspec b6 b6, Default Unpackspec b7 b7,
302 Default Unpackspec b8 b8, Default Unpackspec b9 b9,
303 Default Unpackspec b10 b10, Default Unpackspec b11 b11,
304 Default Unpackspec b12 b12, Default Unpackspec b13 b13,
305 Default Unpackspec b14 b14, Default Unpackspec b15 b15,
306 Default Unpackspec b16 b16, Default Unpackspec b17 b17,
307 Default Unpackspec b18 b18, Default Unpackspec b19 b19,
308 Default Unpackspec b20 b20, Default Unpackspec b21 b21,
309 Default Unpackspec fieldsL fieldsL, Default Unpackspec b22 b22,
310 Default Unpackspec b23 b23, Default Unpackspec b24 b24,
311 Default Unpackspec b25 b25, Default Unpackspec b26 b26,
312 Default Unpackspec b27 b27, Default Unpackspec b28 b28,
313 Default Unpackspec b29 b29, Default Unpackspec b30 b30,
314 Default Unpackspec b31 b31, Default Unpackspec b32 b32,
315 Default Unpackspec b33 b33, Default Unpackspec b34 b34,
316 Default Unpackspec b35 b35, Default Unpackspec b36 b36,
317 Default Unpackspec fieldsR fieldsR, Default NullMaker b15 b10,
318 Default NullMaker b10 b14, Default NullMaker b14 b11,
319 Default NullMaker b11 b13, Default NullMaker b13 b12,
320 Default NullMaker b12 b37, Default NullMaker b28 b16,
321 Default NullMaker b16 b29, Default NullMaker b29 b17,
322 Default NullMaker b17 b30, Default NullMaker b30 b38,
323 Default NullMaker b21 b20, Default NullMaker b20 b39,
324 Default NullMaker b22 b40, Default NullMaker b18 b41,
325 Default NullMaker b23 b18, Default NullMaker b19 b23,
326 Default NullMaker b26 b42, Default NullMaker b25 b26,
327 Default NullMaker b27 b25, Default NullMaker b24 b27,
328 Default NullMaker b3 b43, Default NullMaker b2 b44,
329 Default NullMaker b31 b3, Default NullMaker b32 b2,
330 Default NullMaker b5 b31, Default NullMaker b4 b32,
331 Default NullMaker b33 b5, Default NullMaker b34 b4,
332 Default NullMaker b7 b33, Default NullMaker b6 b34,
333 Default NullMaker b35 b7, Default NullMaker b36 b6,
334 Default NullMaker b9 b35, Default NullMaker b8 b36,
335 Default NullMaker fieldsR b8) =>
336 Select fieldsR
337 -> Select b9
338 -> Select b15
339 -> Select b28
340 -> Select b24
341 -> Select b19
342 -> Select b21
343 -> Select b22
344 -> Select fieldsL
345 -> ((b9, fieldsR) -> Column SqlBool)
346 -> ((b15, (b9, b8)) -> Column SqlBool)
347 -> ((b28, (b15, (b35, b36))) -> Column SqlBool)
348 -> ((b24, (b28, (b10, (b7, b6)))) -> Column SqlBool)
349 -> ((b19, (b24, (b16, (b14, (b33, b34))))) -> Column SqlBool)
350 -> ((b21, (b19, (b27, (b29, (b11, (b5, b4)))))) -> Column SqlBool)
351 -> ((b22, (b21, (b23, (b25, (b17, (b13, (b31, b32)))))))
352 -> Column SqlBool)
353 -> ((fieldsL, (b22, (b20, (b18, (b26, (b30, (b12, (b3, b2))))))))
354 -> Column SqlBool)
355 -> Select
356 (fieldsL, (b40, (b39, (b41, (b42, (b38, (b37, (b43, b44))))))))
357 leftJoin9 q1 q2 q3 q4 q5 q6 q7 q8 q9
358 cond12 cond23 cond34 cond45 cond56 cond67 cond78 cond89 =
359 leftJoin q9 ( leftJoin q8
360 ( leftJoin q7
361 ( leftJoin q6
362 ( leftJoin q5
363 ( leftJoin q4
364 ( leftJoin q3
365 ( leftJoin q2 q1
366 cond12
367 ) cond23
368 ) cond34
369 ) cond45
370 ) cond56
371 ) cond67
372 ) cond78
373 ) cond89
374