-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Case
constraint which is more general than Table Expr
The `Case` constraint is used for functions like `bool`, `case_`, `maybeTable`, `nullable`, all of which ultimately compile down to a PostgreSQL `CASE` statement. `Case` has two instances: an overlapping `Table Expr a => Case a` instance, and a `Case b => Case (a -> b)` instance, that allows expressions like `maybeTable id (+)` which would not have been possible for.
- Loading branch information
1 parent
5eb5689
commit 701406b
Showing
12 changed files
with
71 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{-# language FlexibleContexts #-} | ||
{-# language FlexibleInstances #-} | ||
{-# language NamedFieldPuns #-} | ||
{-# language TypeFamilies #-} | ||
{-# language UndecidableInstances #-} | ||
{-# language ViewPatterns #-} | ||
|
||
module Rel8.Table.Case | ||
( Case | ||
, case_ | ||
, undefined | ||
) | ||
where | ||
|
||
-- base | ||
import Prelude hiding ( undefined ) | ||
|
||
-- rel8 | ||
import Rel8.Expr ( Expr ) | ||
import Rel8.Expr.Bool ( caseExpr ) | ||
import Rel8.Expr.Null ( snull, unsafeUnnullify ) | ||
import Rel8.Schema.HTable ( hfield, htabulate, hspecs ) | ||
import Rel8.Schema.Null ( Nullity( Null, NotNull ) ) | ||
import Rel8.Schema.Spec ( Spec(..) ) | ||
import Rel8.Table ( Table, fromColumns, toColumns ) | ||
|
||
|
||
class Case a where | ||
-- | Produce a table expression from a list of alternatives. Returns the | ||
-- first table where the @Expr Bool@ expression is @True@. If no | ||
-- alternatives are true, the given default is returned. | ||
case_ :: [(Expr Bool, a)] -> a -> a | ||
|
||
undefined :: a | ||
|
||
|
||
instance {-# INCOHERENT #-} Table Expr a => Case a where | ||
case_ (map (fmap toColumns) -> branches) (toColumns -> fallback) = | ||
fromColumns $ htabulate $ \field -> case hfield fallback field of | ||
fallbackExpr -> | ||
case map (fmap (`hfield` field)) branches of | ||
branchExprs -> caseExpr branchExprs fallbackExpr | ||
undefined = fromColumns $ htabulate $ \field -> case hfield hspecs field of | ||
Spec {nullity, info} -> case nullity of | ||
Null -> snull info | ||
NotNull -> unsafeUnnullify (snull info) | ||
|
||
|
||
instance Case b => Case (a -> b) where | ||
case_ branches fallback a = case_ (map (fmap ($ a)) branches) (fallback a) | ||
undefined = const undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters