Change Relation type definition.

This commit is contained in:
Kei Hibino 2013-06-05 10:48:32 +09:00
parent 61fb2876a6
commit 0def28fa95
7 changed files with 81 additions and 83 deletions

View File

@ -18,7 +18,7 @@ import Database.HDBC.Record.Query (runQuery)
import Database.HDBC.Session (withConnectionIO, handleSqlError')
groupMemberShip :: Relation (Maybe Membership, Group)
groupMemberShip :: Relation () (Maybe Membership, Group)
groupMemberShip =
relation $
[ m >< g
@ -27,7 +27,7 @@ groupMemberShip =
, () <- on $ m ?! groupId' .=. just (g ! Group.id')
]
userGroup0 :: Relation (Maybe User, Maybe Group)
userGroup0 :: Relation () (Maybe User, Maybe Group)
userGroup0 =
relation $
[ u >< mg ?! snd'
@ -39,7 +39,7 @@ userGroup0 =
, () <- asc $ u ?! User.id'
]
userGroup1 :: Relation (Maybe User, Maybe Group)
userGroup1 :: Relation () (Maybe User, Maybe Group)
userGroup1 =
relation $
[ u >< g
@ -53,7 +53,7 @@ userGroup1 =
, () <- asc $ u ?! User.id'
]
userGroup2 :: Relation (Maybe User, Maybe Group)
userGroup2 :: Relation () (Maybe User, Maybe Group)
userGroup2 =
relation $
[ u >< mg ?! snd'
@ -70,7 +70,7 @@ userGroup2 =
, () <- asc $ u ?! User.id'
]
userGroup0Aggregate :: Relation ((Maybe String, Int32), Maybe Bool)
userGroup0Aggregate :: Relation () ((Maybe String, Int32), Maybe Bool)
userGroup0Aggregate =
aggregateRelation $
[ g >< c >< every (uid .<. just (value 3))
@ -82,7 +82,7 @@ userGroup0Aggregate =
, () <- asc $ c
]
userGroup2Fail :: Relation (Maybe User, Maybe Group)
userGroup2Fail :: Relation () (Maybe User, Maybe Group)
userGroup2Fail =
relation $
[ u >< mg ?! snd'
@ -100,7 +100,7 @@ userGroup2Fail =
, () <- asc $ u ?! User.id'
]
runAndPrint :: (Show a, IConnection conn, FromSql SqlValue a) => conn -> Relation a -> IO ()
runAndPrint :: (Show a, IConnection conn, FromSql SqlValue a) => conn -> Relation () a -> IO ()
runAndPrint conn rel = do
putStrLn $ "SQL: " ++ sqlFromRelation rel
records <- runQuery conn () (fromRelation rel)
@ -110,7 +110,7 @@ runAndPrint conn rel = do
run :: IO ()
run = handleSqlError' $ withConnectionIO connect
(\conn -> do
let run' :: (Show a, FromSql SqlValue a) => Relation a -> IO ()
let run' :: (Show a, FromSql SqlValue a) => Relation () a -> IO ()
run' = runAndPrint conn
run' userGroup0
run' userGroup1

View File

@ -15,7 +15,7 @@ import qualified Database.Relational.Query.Table as Table
import Database.Relational.Query.Projectable (placeholder, (.=.))
import Database.Relational.Query.ProjectableExtended ((!))
import Database.Relational.Query.Monad.Class (wheres)
import Database.Relational.Query.Relation (Relation, PrimeRelation, relation, query)
import Database.Relational.Query.Relation (Relation, relation, query)
import Database.Relational.Query.Constraint
(Key, Primary, Unique, projectionKey, uniqueKey,
HasConstraintKey(constraintKey))
@ -24,8 +24,8 @@ import Database.Relational.Query.Type (Update, typedSingleKeyUpdate)
unique :: PersistableWidth p
=> Key Unique a p
-> Relation a
-> PrimeRelation p a
-> Relation () a
-> Relation p a
unique uk rel = relation $ do
q <- query rel
wheres $ q ! projectionKey uk .=. placeholder
@ -33,13 +33,13 @@ unique uk rel = relation $ do
primary' :: PersistableWidth p
=> Key Primary a p
-> Relation a
-> PrimeRelation p a
-> Relation () a
-> Relation p a
primary' pc = unique $ Constraint.uniqueKey pc
primary :: (PersistableWidth p, HasConstraintKey Primary a p)
=> Relation a
-> PrimeRelation p a
=> Relation () a
-> Relation p a
primary = primary' constraintKey

View File

@ -6,7 +6,7 @@ module Database.Relational.Query.Relation (
query, query', queryMaybe, queryMaybe', from,
PrimeRelation, Relation,
Relation,
inner', left', right', full',
inner, left, right, full,
@ -44,59 +44,57 @@ import Database.Relational.Query.Sub (SubQuery)
import qualified Database.Relational.Query.Sub as SubQuery
data PrimeRelation p r = SubQuery SubQuery
data Relation p r = SubQuery SubQuery
| SimpleRel (SimpleQuery r)
| AggregateRel (AggregatedQuery r)
type Relation r = PrimeRelation () r
table :: Table r -> Relation r
table :: Table r -> Relation () r
table = SubQuery . SubQuery.fromTable
from :: Table r -> Relation r
from :: Table r -> Relation () r
from = table
subQueryQualifyFromRelation :: PrimeRelation p r -> Qualify SubQuery
subQueryQualifyFromRelation :: Relation p r -> Qualify SubQuery
subQueryQualifyFromRelation = d where
d (SubQuery sub) = return $ sub
d (SimpleRel qp) = Simple.toSubQuery qp
d (AggregateRel qp) = Aggregate.toSubQuery qp
subQueryFromRelation :: PrimeRelation p r -> SubQuery
subQueryFromRelation :: Relation p r -> SubQuery
subQueryFromRelation = evalQualifyPrime . subQueryQualifyFromRelation
queryWithAttr :: MonadQuery m
=> NodeAttr -> PrimeRelation p r -> m (PlaceHolders p, Projection r)
=> NodeAttr -> Relation p r -> m (PlaceHolders p, Projection r)
queryWithAttr attr = addPlaceHolders . q where
q = UnsafeMonadQuery.unsafeSubQuery attr . subQueryQualifyFromRelation
-- d (PrimeRelation q) = UnsafeMonadQuery.unsafeMergeAnotherQuery attr q
-- d (Relation q) = UnsafeMonadQuery.unsafeMergeAnotherQuery attr q
query' :: MonadQuery m => PrimeRelation p r -> m (PlaceHolders p, Projection r)
query' :: MonadQuery m => Relation p r -> m (PlaceHolders p, Projection r)
query' = queryWithAttr Just'
query :: MonadQuery m => Relation r -> m (Projection r)
query :: MonadQuery m => Relation () r -> m (Projection r)
query = fmap snd . query'
queryMaybe' :: MonadQuery m => PrimeRelation p r -> m (PlaceHolders p, Projection (Maybe r))
queryMaybe' :: MonadQuery m => Relation p r -> m (PlaceHolders p, Projection (Maybe r))
queryMaybe' pr = do
(ph, pj) <- queryWithAttr Maybe pr
return (ph, Projection.just pj)
queryMaybe :: MonadQuery m => PrimeRelation p r -> m (Projection (Maybe r))
queryMaybe :: MonadQuery m => Relation () r -> m (Projection (Maybe r))
queryMaybe = fmap snd . queryMaybe'
relation :: QuerySimple (Projection r) -> PrimeRelation p r
relation :: QuerySimple (Projection r) -> Relation p r
relation = SimpleRel
relation' :: QuerySimple (PlaceHolders p, Projection r) -> PrimeRelation p r
relation' :: QuerySimple (PlaceHolders p, Projection r) -> Relation p r
relation' = SimpleRel . fmap snd
aggregateRelation :: QueryAggregate (Aggregation r) -> PrimeRelation p r
aggregateRelation :: QueryAggregate (Aggregation r) -> Relation p r
aggregateRelation = AggregateRel
aggregateRelation' :: QueryAggregate (PlaceHolders p, Aggregation r) -> PrimeRelation p r
aggregateRelation' :: QueryAggregate (PlaceHolders p, Aggregation r) -> Relation p r
aggregateRelation' = AggregateRel . fmap snd
@ -108,7 +106,7 @@ join' :: ProjectableGeneralizedZip pa pb pc
-> qa
-> qb
-> [JoinRestriction a b]
-> PrimeRelation pc (a, b)
-> Relation pc (a, b)
join' qL qR r0 r1 ons = relation' $ do
(ph0, pj0) <- qL r0
(ph1, pj1) <- qR r1
@ -116,31 +114,31 @@ join' qL qR r0 r1 ons = relation' $ do
return $ (ph0 `generalizedZip` ph1, pj0 `projectZip` pj1)
inner' :: ProjectableGeneralizedZip pa pb pc
=> PrimeRelation pa a
-> PrimeRelation pb b
=> Relation pa a
-> Relation pb b
-> [JoinRestriction a b]
-> PrimeRelation pc (a, b)
-> Relation pc (a, b)
inner' = join' query' query'
left' :: ProjectableGeneralizedZip pa pb pc
=> PrimeRelation pa a
-> PrimeRelation pb b
=> Relation pa a
-> Relation pb b
-> [JoinRestriction a (Maybe b)]
-> PrimeRelation pc (a, Maybe b)
-> Relation pc (a, Maybe b)
left' = join' query' queryMaybe'
right' :: ProjectableGeneralizedZip pa pb pc
=> PrimeRelation pa a
-> PrimeRelation pb b
=> Relation pa a
-> Relation pb b
-> [JoinRestriction (Maybe a) b]
-> PrimeRelation pc(Maybe a, b)
-> Relation pc(Maybe a, b)
right' = join' queryMaybe' query'
full' :: ProjectableGeneralizedZip pa pb pc
=> PrimeRelation pa a
-> PrimeRelation pb b
=> Relation pa a
-> Relation pb b
-> [JoinRestriction (Maybe a) (Maybe b)]
-> PrimeRelation pc (Maybe a, Maybe b)
-> Relation pc (Maybe a, Maybe b)
full' = join' queryMaybe' queryMaybe'
join :: (qa -> QuerySimple (Projection a))
@ -148,59 +146,59 @@ join :: (qa -> QuerySimple (Projection a))
-> qa
-> qb
-> [JoinRestriction a b]
-> Relation (a, b)
-> Relation () (a, b)
join qL qR r0 r1 ons = relation $ do
pj0 <- qL r0
pj1 <- qR r1
sequence_ $ zipWith3 (\f a b -> on $ f a b) ons (repeat pj0) (repeat pj1)
return $ pj0 `projectZip` pj1
inner :: Relation a
-> Relation b
inner :: Relation () a
-> Relation () b
-> [JoinRestriction a b]
-> Relation (a, b)
-> Relation () (a, b)
inner = join query query
left :: Relation a
-> Relation b
left :: Relation () a
-> Relation () b
-> [JoinRestriction a (Maybe b)]
-> Relation (a, Maybe b)
-> Relation () (a, Maybe b)
left = join query queryMaybe
right :: Relation a
-> Relation b
right :: Relation () a
-> Relation () b
-> [JoinRestriction (Maybe a) b]
-> Relation (Maybe a, b)
-> Relation () (Maybe a, b)
right = join queryMaybe query
full :: Relation a
-> Relation b
full :: Relation () a
-> Relation () b
-> [JoinRestriction (Maybe a) (Maybe b)]
-> Relation (Maybe a, Maybe b)
-> Relation () (Maybe a, Maybe b)
full = join queryMaybe queryMaybe
on' :: ([JoinRestriction a b] -> PrimeRelation pc (a, b))
on' :: ([JoinRestriction a b] -> Relation pc (a, b))
-> [JoinRestriction a b]
-> PrimeRelation pc (a, b)
-> Relation pc (a, b)
on' = ($)
infixl 8 `inner'`, `left'`, `right'`, `full'`, `inner`, `left`, `right`, `full`, `on'`
sqlQualifyFromRelation :: PrimeRelation p r -> Qualify String
sqlQualifyFromRelation :: Relation p r -> Qualify String
sqlQualifyFromRelation = d where
d (SubQuery sub) = return $ SubQuery.toSQL sub
d (SimpleRel qp) = Simple.toSQL qp
d (AggregateRel qp) = Aggregate.toSQL qp
sqlFromRelation :: PrimeRelation p r -> String
sqlFromRelation :: Relation p r -> String
sqlFromRelation = evalQualifyPrime . sqlQualifyFromRelation
instance Show (PrimeRelation p r) where
instance Show (Relation p r) where
show = sqlFromRelation
width :: PrimeRelation p r -> Int
width :: Relation p r -> Int
width = SubQuery.width . subQueryFromRelation
nested :: PrimeRelation p r -> PrimeRelation p r
nested :: Relation p r -> Relation p r
nested = SubQuery . subQueryFromRelation

View File

@ -48,7 +48,7 @@ import Database.Record.TH
defineHasKeyConstraintInstance)
import Database.Relational.Query
(Table, Pi, Relation, PrimeRelation,
(Table, Pi, Relation,
sqlFromRelation, Query, fromRelation, Update, Insert, typedInsert,
HasConstraintKey(constraintKey), projectionKey, Primary, NotNull)
import qualified Database.Relational.Query as Query
@ -142,7 +142,7 @@ defineTableTypes tableVar' relVar' recordType table columns = do
tableDs <- simpleValD tableVar [t| Table $(recordType) |]
[| Table.table $(stringE table) $(listE $ map stringE (map (fst . fst) columns)) |]
let relVar = varName relVar'
relDs <- simpleValD relVar [t| Relation $(recordType) |]
relDs <- simpleValD relVar [t| Relation () $(recordType) |]
[| Query.table $(toVarExp tableVar') |]
return $ tableDs ++ relDs
@ -280,17 +280,17 @@ defineTableDefault schema table fields derives mayPrimaryIdx mayNotNullIdx = do
return $ tblD ++ primD ++ nnD
inlineQuery :: VarName -- ^ Top-level variable name which has 'PrimeRelation' type
-> PrimeRelation p r -- ^ Object which has 'PrimeRelation' type
-> VarName -- ^ Variable name for inlined query
-> Q [Dec] -- ^ Result declarations
inlineQuery :: VarName -- ^ Top-level variable name which has 'PrimeRelation' type
-> Relation p r -- ^ Object which has 'PrimeRelation' type
-> VarName -- ^ Variable name for inlined query
-> Q [Dec] -- ^ Result declarations
inlineQuery relVar' rel qVar' = do
let relVar = varName relVar'
qVar = varName qVar'
relInfo <- reify relVar
case relInfo of
VarI _ (AppT (AppT (ConT prn) p) r) _ _
| prn == ''PrimeRelation -> do
| prn == ''Relation -> do
simpleValD qVar
[t| Query $(return p) $(return r) |]
[| unsafeTypedQuery $(stringE . sqlFromRelation $ rel) |]

View File

@ -6,7 +6,7 @@ module Database.Relational.Query.Type (
Insert(untypeInsert), unsafeTypedInsert, typedInsert
) where
import Database.Relational.Query.Relation (PrimeRelation, sqlFromRelation)
import Database.Relational.Query.Relation (Relation, sqlFromRelation)
import Database.Relational.Query.Table (Table)
import Database.Relational.Query.SQL (singleKeyUpdateSQL, insertSQL)
@ -19,7 +19,7 @@ unsafeTypedQuery = Query
instance Show (Query p a) where
show = untypeQuery
fromRelation :: PrimeRelation p r -> Query p r
fromRelation :: Relation p r -> Query p r
fromRelation = unsafeTypedQuery . sqlFromRelation

View File

@ -18,7 +18,7 @@ import Database.Record.Instances ()
import Database.Relational.Query.Type (fromRelation)
import Database.Relational.Query
(Query, PrimeRelation, query, relation,
(Query, Relation, query, relation,
wheres, (.=.), (!), placeholder, asc, value)
import Control.Applicative ((<|>))
@ -61,7 +61,7 @@ getType mapFromSql rec = do
then typ
else [t| Maybe $(typ) |]
columnsRelationFromTable :: PrimeRelation (String, String) Columns
columnsRelationFromTable :: Relation (String, String) Columns
columnsRelationFromTable = relation $ do
c <- query columns
wheres $ c ! Columns.tabschema' .=. placeholder
@ -73,7 +73,7 @@ columnsQuerySQL :: Query (String, String) Columns
columnsQuerySQL = fromRelation columnsRelationFromTable
primaryKeyRelation :: PrimeRelation (String, String) String
primaryKeyRelation :: Relation (String, String) String
primaryKeyRelation = relation $ do
cons <- query tabconst
key <- query keycoluse

View File

@ -24,7 +24,7 @@ import Database.Record.Instances ()
import Database.Relational.Query.Type (fromRelation)
import Database.Relational.Query
(Query, PrimeRelation, query, relation, query', relation', expr,
(Query, Relation, query, relation, query', relation', expr,
wheres, (.=.), (.>.), in', values, (!), just,
placeholder, asc, value, unsafeProjectSql, (><))
@ -95,7 +95,7 @@ getType mapFromSql column@(pgAttr, pgTyp) = do
then typ
else [t| Maybe $typ |]
relOidRelation :: PrimeRelation (String, String) Int32
relOidRelation :: Relation (String, String) Int32
relOidRelation = relation $ do
nsp <- query pgNamespace
cls <- query pgClass
@ -106,7 +106,7 @@ relOidRelation = relation $ do
return $ cls ! Class.oid'
attributeRelation :: PrimeRelation (String, String) PgAttribute
attributeRelation :: Relation (String, String) PgAttribute
attributeRelation = relation' $ do
(ph, reloid) <- query' relOidRelation
att <- query pgAttribute
@ -116,7 +116,7 @@ attributeRelation = relation' $ do
return (ph, att)
columnRelation :: PrimeRelation (String, String) Column
columnRelation :: Relation (String, String) Column
columnRelation = relation' $ do
(ph, att) <- query' attributeRelation
typ <- query pgType
@ -138,7 +138,7 @@ columnRelation = relation' $ do
columnQuerySQL :: Query (String, String) Column
columnQuerySQL = fromRelation columnRelation
primaryKeyRelation :: PrimeRelation (String, String) String
primaryKeyRelation :: Relation (String, String) String
primaryKeyRelation = relation' $ do
(ph, att) <- query' attributeRelation
con <- query pgConstraint