on master help sign in
add simple Relation->CSV conversion
8a9ad96AgentM
ProjectM36/Relation/Show/CSV.hsnew file+23
@@ -0,0 +1,23 @@
1+module ProjectM36.Relation.Show.CSV where
2+import ProjectM36.Base
3+import Data.Csv
4+import qualified Data.Map as M
5+import qualified Data.HashSet as HS
6+import qualified Data.ByteString.Lazy as BS
7+
8+--maybe support a header in the future
9+relationAsCSV :: Relation -> BS.ByteString
10+relationAsCSV (Relation _ tupleSet) = tupleSetAsCSV tupleSet
11+
12+tupleSetAsCSV :: RelationTupleSet -> BS.ByteString
13+tupleSetAsCSV tupleSet = encode $ HS.toList tupleSet
14+
15+instance ToRecord RelationTuple where
16+ toRecord (RelationTuple tupMap) = toRecord $ map toField (M.elems tupMap)
17+
18+instance ToField Atom where
19+ toField (StringAtom atom) = toField atom
20+ toField (IntAtom atom) = toField atom
21+ toField (RelationAtom atom) = undefined -- CSV does not support nested relations- this should likely be detected and rejected
22+
23+
project-m36.cabal+10−3
@@ -7,7 +7,7 @@ Category: Relational Algebra
7Cabal-Version: >= 1.2
7Cabal-Version: >= 1.2
8
8
9Library
9Library
10− Build-Depends: base,mtl,containers,unordered-containers,parsec,hashable,haskeline,directory,uuid
10+ Build-Depends: base,mtl,containers,unordered-containers,parsec,hashable,haskeline,directory,uuid,cassava,text,bytestring
11 Exposed-Modules: ProjectM36.Error,
11 Exposed-Modules: ProjectM36.Error,
12 ProjectM36.Transaction,
12 ProjectM36.Transaction,
13 ProjectM36.Transaction.Show,
13 ProjectM36.Transaction.Show,
@@ -19,7 +19,8 @@ Library
19 ProjectM36.RelationalExpression,
19 ProjectM36.RelationalExpression,
20 ProjectM36.Relation.Show.HTML,
20 ProjectM36.Relation.Show.HTML,
21 ProjectM36.StaticOptimizer,
21 ProjectM36.StaticOptimizer,
22− ProjectM36.Relation.Show.Term
22+ ProjectM36.Relation.Show.Term,
23+ ProjectM36.Relation.Show.CSV
23 GHC-Options: -Wall
24 GHC-Options: -Wall
24
25
25Executable tutd
26Executable tutd
@@ -47,4 +48,10 @@ Test-Suite test-static-optimizer
47 type: exitcode-stdio-1.0
48 type: exitcode-stdio-1.0
48 main-is: test/Relation/StaticOptimizer.hs
49 main-is: test/Relation/StaticOptimizer.hs
49 Build-Depends: base,HUnit,Cabal
50 Build-Depends: base,HUnit,Cabal
50− GHC-Options: -Wall
51+ GHC-Options: -Wall
52+
53+benchmark bench
54+ type: exitcode-stdio-1.0
55+ main-is: benchmark/Relation.hs
56+ build-depends: base,criterion
57+ GHC-Options: -Wall -rtsopts