|
| 1 | +module Database.Persist.Sql.Lifted.Expression.ArrayAggregate.PostgreSQL |
| 2 | + ( AggMode (..) |
| 3 | + , arrayAggDistinct |
| 4 | + , arrayAgg |
| 5 | + , arrayAggWith |
| 6 | + , arrayRemove |
| 7 | + , arrayRemoveNull |
| 8 | + , maybeArray |
| 9 | + , arrayAggById |
| 10 | + , arrayAggByIdMaybe |
| 11 | + , arrayAggByMaybe |
| 12 | + , arrayAggBy |
| 13 | + ) where |
| 14 | + |
| 15 | +import Prelude |
| 16 | + |
| 17 | +import Database.Esqueleto.Experimental |
| 18 | + ( Entity |
| 19 | + , EntityField |
| 20 | + , PersistEntity |
| 21 | + , PersistField |
| 22 | + , SqlExpr |
| 23 | + , Value |
| 24 | + , asc |
| 25 | + , persistIdField |
| 26 | + , (^.) |
| 27 | + ) |
| 28 | +import Database.Esqueleto.Internal.Internal |
| 29 | + ( (??.) |
| 30 | + ) |
| 31 | +import Database.Esqueleto.PostgreSQL |
| 32 | + ( AggMode (..) |
| 33 | + , arrayAgg |
| 34 | + , arrayAggDistinct |
| 35 | + , arrayAggWith |
| 36 | + , arrayRemove |
| 37 | + , arrayRemoveNull |
| 38 | + , maybeArray |
| 39 | + ) |
| 40 | + |
| 41 | +-- | Aggregrate the given column with stable ordering (by ID) |
| 42 | +-- |
| 43 | +-- This ensures that if you aggregrate two columns: |
| 44 | +-- |
| 45 | +-- @ |
| 46 | +-- pure |
| 47 | +-- ( 'arrayAggById' students StudentFirstName |
| 48 | +-- , 'arrayAggById' students StudentLastName |
| 49 | +-- ) |
| 50 | +-- @ |
| 51 | +-- |
| 52 | +-- The list, if zipped, will be as expected. |
| 53 | +-- |
| 54 | +-- See <https://stackoverflow.com/a/7317520>. |
| 55 | +-- |
| 56 | +-- Also replaces the 'Maybe' result with an empty list, because really that's |
| 57 | +-- what you always want. |
| 58 | +arrayAggById |
| 59 | + :: (PersistEntity val, PersistField [typ], PersistField typ) |
| 60 | + => SqlExpr (Entity val) |
| 61 | + -> EntityField val typ |
| 62 | + -> SqlExpr (Value [typ]) |
| 63 | +arrayAggById es f = arrayAggBy (es ^. f) (es ^. persistIdField) |
| 64 | + |
| 65 | +-- | 'arrayAggById' but for a left-outer-joined entity |
| 66 | +-- |
| 67 | +-- If you're using '(?.)' instead of '(^.)', use this instead of 'arrayAggById'. |
| 68 | +arrayAggByIdMaybe |
| 69 | + :: (PersistEntity val, PersistField typ) |
| 70 | + => SqlExpr (Maybe (Entity val)) |
| 71 | + -> EntityField val typ |
| 72 | + -> SqlExpr (Value [typ]) |
| 73 | +arrayAggByIdMaybe es f = |
| 74 | + arrayRemoveNull $ arrayAggBy (es ??. f) (es ??. persistIdField) |
| 75 | + |
| 76 | +-- | 'arrayAggBy' but for a left-outer-joined entity |
| 77 | +-- |
| 78 | +-- If you're using '(?.)' instead of '(^.)', use this instead of 'arrayAggBy'. |
| 79 | +arrayAggByMaybe |
| 80 | + :: (PersistField a, PersistField b) |
| 81 | + => SqlExpr (Value (Maybe a)) |
| 82 | + -> SqlExpr (Value b) |
| 83 | + -> SqlExpr (Value [a]) |
| 84 | +arrayAggByMaybe a = arrayRemoveNull . arrayAggBy a |
| 85 | + |
| 86 | +arrayAggBy |
| 87 | + :: (PersistField [a], PersistField a, PersistField b) |
| 88 | + => SqlExpr (Value a) |
| 89 | + -> SqlExpr (Value b) |
| 90 | + -> SqlExpr (Value [a]) |
| 91 | +arrayAggBy a b = maybeArray $ arrayAggWith AggModeAll a [asc b] |
0 commit comments