mirror of
https://github.com/khibino/haskell-relational-record.git
synced 2025-01-07 13:46:41 +03:00
Update example.
This commit is contained in:
parent
da42195194
commit
f9f12e2ab3
@ -21,6 +21,7 @@ Executable sample1
|
|||||||
, names-th
|
, names-th
|
||||||
, DB-record
|
, DB-record
|
||||||
, relational-join
|
, relational-join
|
||||||
|
, HDBC
|
||||||
, HDBC-session
|
, HDBC-session
|
||||||
, HDBC-postgresql
|
, HDBC-postgresql
|
||||||
, relational-query-HDBC
|
, relational-query-HDBC
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
{-# LANGUAGE MonadComprehensions #-}
|
{-# LANGUAGE MonadComprehensions #-}
|
||||||
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
|
|
||||||
|
import Database.Record
|
||||||
|
|
||||||
import Database.Relational.Query
|
import Database.Relational.Query
|
||||||
|
import Database.HDBC (IConnection, SqlValue)
|
||||||
|
|
||||||
import qualified User
|
import qualified User
|
||||||
import User (User, user)
|
import User (User, user)
|
||||||
@ -12,27 +16,58 @@ import PgTestDataSource (connect)
|
|||||||
import Database.HDBC.Record.Query (runQuery)
|
import Database.HDBC.Record.Query (runQuery)
|
||||||
import Database.HDBC.Session (withConnectionIO)
|
import Database.HDBC.Session (withConnectionIO)
|
||||||
|
|
||||||
um :: Relation (User, Maybe Membership)
|
|
||||||
um = relation $
|
|
||||||
[ u >*< m
|
|
||||||
| u <- query user
|
|
||||||
, m <- queryMaybe membership
|
|
||||||
, () <- on $ just (u ! User.id') .=. m !? userId'
|
|
||||||
]
|
|
||||||
|
|
||||||
r0 :: Relation (Maybe User, Maybe Group)
|
groupMemberShip :: QueryJoin (Projection (Maybe Membership, Group))
|
||||||
r0 = relation $
|
groupMemberShip =
|
||||||
[ (um' !? fst') >*< g
|
[ m >*< g
|
||||||
| um' <- queryMaybe um
|
| m <- queryMaybe membership
|
||||||
, g <- queryMaybe group
|
, g <- query group
|
||||||
, () <- on $ flatten (um' !? snd') !? groupId' .=. g !? Group.id'
|
, () <- on $ m !? groupId' .=. just (g ! Group.id')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
userGroup0 :: Relation (Maybe User, Maybe Group)
|
||||||
|
userGroup0 =
|
||||||
|
relation $
|
||||||
|
[ u >*< mg !? snd'
|
||||||
|
| u <- queryMaybe user
|
||||||
|
, mg <- queryMaybe $ relation groupMemberShip
|
||||||
|
-- Call one subquery via relation layer
|
||||||
|
-- Simple implementation.
|
||||||
|
-- Complex SQL. Nested table form joins.
|
||||||
|
|
||||||
|
, () <- on $ u !? User.id' .=. flatten (mg !? fst') !? userId'
|
||||||
|
|
||||||
|
, () <- asc $ u !? User.id'
|
||||||
|
]
|
||||||
|
|
||||||
|
userGroup1 :: Relation (Maybe User, Maybe Group)
|
||||||
|
userGroup1 =
|
||||||
|
relation $
|
||||||
|
[ u >*< mg !? snd'
|
||||||
|
| u <- queryMaybe user
|
||||||
|
, mg <- queryMergeMaybe groupMemberShip
|
||||||
|
-- Directly merge another QueryJoin monad.
|
||||||
|
-- Complex implementation.
|
||||||
|
-- Simple SQL. Flat table form joins.
|
||||||
|
|
||||||
|
, () <- on $ u !? User.id' .=. flatten (mg !? fst') !? userId'
|
||||||
|
|
||||||
|
, () <- asc $ u !? User.id'
|
||||||
|
]
|
||||||
|
|
||||||
|
runAndPrint :: (Show a, IConnection conn, FromSql SqlValue a) => conn -> Relation a -> IO ()
|
||||||
|
runAndPrint conn rel = do
|
||||||
|
putStrLn $ "SQL: " ++ show rel
|
||||||
|
records <- runQuery conn () (fromRelation rel)
|
||||||
|
mapM_ print records
|
||||||
|
putStrLn ""
|
||||||
|
|
||||||
run :: IO ()
|
run :: IO ()
|
||||||
run = withConnectionIO connect
|
run = withConnectionIO connect
|
||||||
(\conn -> do
|
(\conn -> do
|
||||||
records <- runQuery conn () (fromRelation r0)
|
runAndPrint conn userGroup0
|
||||||
mapM_ print records)
|
runAndPrint conn userGroup1
|
||||||
|
)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = run
|
main = run
|
||||||
|
Loading…
Reference in New Issue
Block a user