module Database.HaskRel.Relational.Variable (
Relvar (Relvar, relvarPath), relvarType,
readRel, readRelvar, hListsToRel,
writeRelvarBody, writeRelvarBody', showRelStr
) where
import Data.HList.CommonMain
import Data.Set ( Set, fromList )
import Data.Typeable ( Typeable )
import Database.HaskRel.HFWTabulation
( HFWPresent, HPresentTypedRecAttr, HPresentRecAttr, showHListSetType,
hfwPrint, hfwPrintTyped, hfwPrintTypedTS )
import Database.HaskRel.Relational.Definition ( Relation, bodyAsList )
data Relvar (a::[*]) = Relvar { relvarPath :: FilePath }
relvarType :: Relvar a -> Relation a
relvarType rv = undefined
instance Typeable a => Show ( Relvar a ) where
show a = "Relvar \"" ++ relvarPath a ++ "\" :: Relvar" ++ showHListSetType ( relvarType a )
hListsToRel
:: (Ord (HList b), RecordValues b,
HMapAux HList TaggedFn (RecordValuesR b) b) =>
[HList (RecordValuesR b)] -> Relation b
hListsToRel = fromList . map hMapTaggedFn
readRel :: forall b .
(Ord (HList b), Read (HList (RecordValuesR b)), RecordValues b,
HMapAux HList TaggedFn (RecordValuesR b) b) =>
String -> Relation b
readRel s = hListsToRel $ read $ "[" ++ s ++ "]"
readRel' :: forall b .
(Ord (HList b), Read (HList (RecordValuesR b)), RecordValues b,
HMapAux HList TaggedFn (RecordValuesR b) b) =>
String -> Relation b -> Relation b
readRel' s rt = hListsToRel $ read $ "[" ++ s ++ "]"
readRelvar
:: (Ord (HList b), Read (HList (RecordValuesR b)), RecordValues b,
HMapAux HList TaggedFn (RecordValuesR b) b) =>
Relvar b -> IO (Relation b)
readRelvar rv = do
relStr <- readFile ( relvarPath rv )
return $ readRel relStr
showRelStr :: (Show (HList (RecordValuesR r)), RecordValues r) =>
Relation r -> String
showRelStr = init . tail . show . bodyAsList
writeRelvarBody :: Show r => FilePath -> r -> IO ()
writeRelvarBody n hll = writeFile n ( init $ tail $ show hll )
writeRelvarBody' :: Show r => Relvar rv -> r -> IO ()
writeRelvarBody' rv hll = writeFile ( relvarPath rv ) ( init $ tail $ show hll )
instance (Ord (HList b), Read (HList (RecordValuesR b)), Typeable b,
RecordValues b, HMapAux HList TaggedFn (RecordValuesR b) b,
HFoldr (Mapcar HPresentTypedRecAttr) [[String]] (RecordValuesR b) [[String]],
HFoldr (Mapcar HPresentRecAttr) [[String]] (RecordValuesR b) [[String]]) =>
HFWPresent ( Relvar b )
where
hfwPrint r' = do
r <- readRelvar r'
hfwPrint r
hfwPrintTypedTS ts r' = do
r <- readRelvar r'
hfwPrintTypedTS ts r