2013-05-13 10:16:00 +04:00
|
|
|
{-# LANGUAGE MonadComprehensions #-}
|
2013-05-20 02:45:45 +04:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
|
|
|
import Database.Record
|
2013-05-13 10:16:00 +04:00
|
|
|
|
|
|
|
import Database.Relational.Query
|
2013-05-20 02:45:45 +04:00
|
|
|
import Database.HDBC (IConnection, SqlValue)
|
2013-05-27 13:56:16 +04:00
|
|
|
import Data.Int (Int32)
|
2013-05-13 10:16:00 +04:00
|
|
|
|
|
|
|
import qualified User
|
|
|
|
import User (User, user)
|
|
|
|
import qualified Group
|
|
|
|
import Group (Group, group)
|
|
|
|
import Membership (Membership, groupId', userId', membership)
|
|
|
|
|
|
|
|
import PgTestDataSource (connect)
|
|
|
|
import Database.HDBC.Record.Query (runQuery)
|
2013-05-30 05:35:59 +04:00
|
|
|
import Database.HDBC.Session (withConnectionIO, handleSqlError')
|
2013-05-13 10:16:00 +04:00
|
|
|
|
2013-05-20 02:45:45 +04:00
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
groupMemberShip :: Relation () (Maybe Membership, Group)
|
2013-05-20 02:45:45 +04:00
|
|
|
groupMemberShip =
|
2013-05-21 05:56:08 +04:00
|
|
|
relation $
|
2013-05-21 11:44:17 +04:00
|
|
|
[ m >< g
|
2013-05-20 02:45:45 +04:00
|
|
|
| m <- queryMaybe membership
|
|
|
|
, g <- query group
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ m ?! groupId' .=. just (g ! Group.id')
|
2013-05-20 02:45:45 +04:00
|
|
|
]
|
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
userGroup0 :: Relation () (Maybe User, Maybe Group)
|
2013-05-20 02:45:45 +04:00
|
|
|
userGroup0 =
|
|
|
|
relation $
|
2013-05-30 12:13:45 +04:00
|
|
|
[ u >< mg ?! snd'
|
2013-05-20 02:45:45 +04:00
|
|
|
| u <- queryMaybe user
|
2013-05-25 22:22:26 +04:00
|
|
|
, mg <- queryMaybe groupMemberShip
|
2013-05-20 02:45:45 +04:00
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ u ?! User.id' .=. mg ?!? fst' ?! userId'
|
2013-05-20 02:45:45 +04:00
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- asc $ u ?! User.id'
|
2013-05-20 02:45:45 +04:00
|
|
|
]
|
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
userGroup1 :: Relation () (Maybe User, Maybe Group)
|
2013-05-27 13:56:16 +04:00
|
|
|
userGroup1 =
|
2013-05-27 17:32:51 +04:00
|
|
|
relation $
|
|
|
|
[ u >< g
|
|
|
|
| umg <- query $
|
2013-05-30 12:13:45 +04:00
|
|
|
user `left` membership `on'` [\ u m -> just (u ! User.id') .=. m ?! userId' ]
|
|
|
|
`full` group `on'` [ \ um g -> um ?!? snd' ?! groupId' .=. g ?! Group.id' ]
|
2013-05-27 17:32:51 +04:00
|
|
|
, let um = umg ! fst'
|
2013-05-30 12:13:45 +04:00
|
|
|
u = um ?! fst'
|
2013-05-27 17:32:51 +04:00
|
|
|
g = umg ! snd'
|
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- asc $ u ?! User.id'
|
2013-05-27 17:32:51 +04:00
|
|
|
]
|
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
userGroup2 :: Relation () (Maybe User, Maybe Group)
|
2013-05-30 05:35:59 +04:00
|
|
|
userGroup2 =
|
|
|
|
relation $
|
2013-05-30 12:13:45 +04:00
|
|
|
[ u >< mg ?! snd'
|
2013-05-30 05:35:59 +04:00
|
|
|
| u <- queryMaybe user
|
|
|
|
, mg <- queryMaybe . relation $
|
|
|
|
[ m >< g
|
|
|
|
| m <- queryMaybe membership
|
|
|
|
, g <- query group
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ m ?! groupId' .=. just (g ! Group.id')
|
2013-05-30 05:35:59 +04:00
|
|
|
]
|
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ u ?! User.id' .=. mg ?!? fst' ?! userId'
|
2013-05-30 05:35:59 +04:00
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- asc $ u ?! User.id'
|
2013-05-30 05:35:59 +04:00
|
|
|
]
|
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
userGroup0Aggregate :: Relation () ((Maybe String, Int32), Maybe Bool)
|
2013-05-27 17:32:51 +04:00
|
|
|
userGroup0Aggregate =
|
2013-05-27 13:56:16 +04:00
|
|
|
aggregateRelation $
|
2013-05-30 12:13:45 +04:00
|
|
|
[ g >< c >< every (uid .<. just (value 3))
|
2013-05-27 13:56:16 +04:00
|
|
|
| ug <- query userGroup0
|
2013-05-30 12:13:45 +04:00
|
|
|
, g <- groupBy (ug ! snd' ?!? Group.name')
|
|
|
|
, let uid = ug ! fst' ?! User.id'
|
2013-05-27 13:56:16 +04:00
|
|
|
, let c = count uid
|
|
|
|
, () <- having $ c .<. value 3
|
2013-05-27 20:23:10 +04:00
|
|
|
, () <- asc $ c
|
2013-05-27 13:56:16 +04:00
|
|
|
]
|
2013-05-20 02:45:45 +04:00
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
userGroup2Fail :: Relation () (Maybe User, Maybe Group)
|
2013-05-30 05:35:59 +04:00
|
|
|
userGroup2Fail =
|
|
|
|
relation $
|
2013-05-30 12:13:45 +04:00
|
|
|
[ u >< mg ?! snd'
|
2013-05-30 05:35:59 +04:00
|
|
|
| u <- queryMaybe user
|
|
|
|
, mg <- queryMaybe . relation $
|
|
|
|
[ m >< g
|
|
|
|
| m <- queryMaybe membership
|
|
|
|
, g <- query group
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ m ?! groupId' .=. just (g ! Group.id')
|
|
|
|
, () <- wheres $ u ?! User.id' .>. just (value 0) -- bad line
|
2013-05-30 05:35:59 +04:00
|
|
|
]
|
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- on $ u ?! User.id' .=. mg ?!? fst' ?! userId'
|
2013-05-30 05:35:59 +04:00
|
|
|
|
2013-05-30 12:13:45 +04:00
|
|
|
, () <- asc $ u ?! User.id'
|
2013-05-30 05:35:59 +04:00
|
|
|
]
|
|
|
|
|
2013-06-05 05:48:32 +04:00
|
|
|
runAndPrint :: (Show a, IConnection conn, FromSql SqlValue a) => conn -> Relation () a -> IO ()
|
2013-05-20 02:45:45 +04:00
|
|
|
runAndPrint conn rel = do
|
2013-05-24 05:46:08 +04:00
|
|
|
putStrLn $ "SQL: " ++ sqlFromRelation rel
|
2013-05-20 02:45:45 +04:00
|
|
|
records <- runQuery conn () (fromRelation rel)
|
|
|
|
mapM_ print records
|
|
|
|
putStrLn ""
|
2013-05-13 10:16:00 +04:00
|
|
|
|
|
|
|
run :: IO ()
|
2013-05-30 05:35:59 +04:00
|
|
|
run = handleSqlError' $ withConnectionIO connect
|
2013-05-13 10:16:00 +04:00
|
|
|
(\conn -> do
|
2013-06-05 05:48:32 +04:00
|
|
|
let run' :: (Show a, FromSql SqlValue a) => Relation () a -> IO ()
|
2013-05-30 05:35:59 +04:00
|
|
|
run' = runAndPrint conn
|
|
|
|
run' userGroup0
|
|
|
|
run' userGroup1
|
|
|
|
run' userGroup2
|
|
|
|
run' userGroup0Aggregate
|
|
|
|
run' userGroup2Fail
|
2013-05-20 02:45:45 +04:00
|
|
|
)
|
2013-05-13 10:16:00 +04:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = run
|