on master help sign in
add date and double atom types
commitc121f1661e7c91953d05773fb81c149bd23682a1 authorAgentM <agentm@themactionfaction.com> dateFri 24 Jul 2015, 15:53 (11 years ago) parent7e4246e
7 files changed+50−12
ProjectM36/Atom.hs+8 ProjectM36/Base.hs+13−1 ProjectM36/Relation/Show/CSV.hs+1−1 ProjectM36/Relation/Show/Term.hs+2 TutorialD/Interpreter/Base.hs+6−1 TutorialD/Interpreter/Import/Base.hs−2 TutorialD/Interpreter/RelationalExpr.hs+20−7
ProjectM36/Atom.hs+8
@@ -17,6 +17,8 @@ atomTypeForAtom (IntAtom _) = IntAtomType
1717atomTypeForAtom (RelationAtom (Relation attributes _)) = RelationAtomType attributes
1818atomTypeForAtom (BoolAtom _) = BoolAtomType
1919atomTypeForAtom (DateTimeAtom _) = DateTimeAtomType
20+atomTypeForAtom (DateAtom _) = DateAtomType
21+atomTypeForAtom (DoubleAtom _) = DoubleAtomType
2022
2123{- a generic string constructor for atoms
2224used by CSV relation generation
@@ -36,4 +38,10 @@ atomFromString DateTimeAtomType strIn =
3638 case parseTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" strIn of
3739 Just utctime -> Right $ DateTimeAtom utctime
3840 Nothing -> Left $ ParseError "Failed to parse datetime"
41+atomFromString DoubleAtomType strIn = case readMaybe strIn of
42+ Just d -> Right $ DoubleAtom d
43+ Nothing -> Left (ParseError "Failed to parse double")
44+atomFromString DateAtomType strIn = case parseTime defaultTimeLocale "%Y-%m-%d" strIn of
45+ Just date -> Right $ DateTimeAtom date
46+ Nothing -> Left $ ParseError "Failed to parse datetime"
3947atomFromString AnyAtomType _ = Left $ ParseError "Parsing AnyAtomType is not supported"
ProjectM36/Base.hs+13−1
@@ -17,7 +17,9 @@ import Data.Vector.Binary()
1717import ProjectM36.HashSetBinary ()
1818import Data.Time.Clock
1919import Data.Time.Clock.POSIX
20+import Data.Time.Calendar (Day,toGregorian,fromGregorian)
2021import Data.Hashable.Time ()
22+import Control.Monad (liftM)
2123
2224type StringType = T.Text
2325
@@ -25,7 +27,9 @@ data Atom = StringAtom StringType |
2527 IntAtom Int |
2628 BoolAtom Bool |
2729 RelationAtom Relation |
28− DateTimeAtom UTCTime
30+ DateTimeAtom UTCTime |
31+ DateAtom Day |
32+ DoubleAtom Double
2933 deriving (Show, Eq, Generic)
3034
3135--not so great orphan instance
@@ -34,6 +38,12 @@ instance Binary UTCTime where
3438 get = do
3539 r <- get :: Get Rational
3640 return (posixSecondsToUTCTime (fromRational r))
41+
42+instance Binary Day where
43+ put day = put $ toGregorian day
44+ get = do
45+ (y,m,d) <- get :: Get (Integer, Int, Int)
46+ return $ fromGregorian y m d
3747
3848instance NFData Atom where rnf = genericRnf
3949
@@ -46,6 +56,8 @@ data AtomType = StringAtomType |
4656 BoolAtomType |
4757 RelationAtomType Attributes |
4858 DateTimeAtomType |
59+ DateAtomType |
60+ DoubleAtomType |
4961 AnyAtomType --AnyAtomType is used as a wildcard
5062 deriving (Eq, Show, Generic)
5163
ProjectM36/Relation/Show/CSV.hs+1−1
@@ -11,7 +11,7 @@ relationAsCSV :: Relation -> BS.ByteString
1111relationAsCSV (Relation _ tupleSet) = tupleSetAsCSV tupleSet
1212
1313tupleSetAsCSV :: RelationTupleSet -> BS.ByteString
14−tupleSetAsCSV tupleSet = encode $ V.fromList (HS.toList tupleSet)
14+tupleSetAsCSV tupleSet = encode (HS.toList tupleSet)
1515
1616instance ToRecord RelationTuple where
1717 toRecord tuple = toRecord $ map toField (V.toList $ tupleAtoms tuple)
ProjectM36/Relation/Show/Term.hs+2
@@ -97,6 +97,8 @@ showAtom (IntAtom atom) = T.pack $ show atom
9797showAtom (RelationAtom rel) = renderTable $ relationAsTable rel
9898showAtom (BoolAtom bool) = T.pack $ show bool
9999showAtom (DateTimeAtom dt) = T.pack $ show dt
100+showAtom (DateAtom d) = T.pack $ show d
101+showAtom (DoubleAtom d) = T.pack $ show d
100102
101103renderTable :: Table -> StringType
102104renderTable table = renderHeader table (fst cellLocs) `T.append` renderBody (snd table) cellLocs
TutorialD/Interpreter/Base.hs+6−1
@@ -45,14 +45,19 @@ semi = Token.semi lexer
4545whiteSpace :: Parser ()
4646whiteSpace = Token.whiteSpace lexer
4747
48−integer :: Parser (Integer)
48+integer :: Parser Integer
4949integer = Token.integer lexer
5050
51+float :: Parser Double
52+float = Token.float lexer
53+
5154atomTypeToTutDType :: AtomType -> Maybe T.Text
5255atomTypeToTutDType atomType = case atomType of
5356 StringAtomType -> Just "char"
5457 IntAtomType -> Just "int"
5558 DateTimeAtomType -> Just "datetime"
59+ DoubleAtomType -> Just "double"
60+ DateAtomType -> Just "date"
5661 RelationAtomType attrs -> Just $ "relation" `T.append` showRelationAttributes attrs
5762 _ -> Nothing
5863
TutorialD/Interpreter/Import/Base.hs−2
@@ -1,8 +1,6 @@
11module TutorialD.Interpreter.Import.Base where
22import ProjectM36.Base
33import ProjectM36.Error
4−import ProjectM36.Relation
5−import qualified Data.Map as M
64
75data DataImportOperator = DataImportOperator FilePath (FilePath -> DatabaseContext -> IO (Either RelationalError DatabaseExpr))
86
TutorialD/Interpreter/RelationalExpr.hs+20−7
@@ -223,7 +223,7 @@ nakedAtomExprP :: Parser AtomExpr
223223nakedAtomExprP = NakedAtomExpr <$> atomP
224224
225225atomP :: Parser Atom
226−atomP = dateTimeAtomP <|> stringAtomP <|> intAtomP <|> boolAtomP <|> relationAtomP
226+atomP = dateTimeAtomP <|> dateAtomP <|> stringAtomP <|> doubleAtomP <|> intAtomP <|> boolAtomP <|> relationAtomP
227227
228228functionAtomExprP :: Parser AtomExpr
229229functionAtomExprP = do
@@ -239,15 +239,28 @@ stringAtomP = liftA (StringAtom . T.pack) quotedString
239239
240240dateTimeAtomP :: Parser Atom
241241dateTimeAtomP = do
242− dateString' <- try $ do
243− dateString <- quotedString
244− reserved "::dt"
245− return dateString
242+ dateTimeString' <- try $ do
243+ dateTimeString <- quotedString
244+ reserved "::datetime"
245+ return dateTimeString
246246 --deprecated in time 1.5- needs update for GHC 7.10
247− case parseTime defaultTimeLocale "%Y-%m-%d %H:%M" dateString' of
247+ case parseTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" dateTimeString' of
248248 Just utctime -> return $ DateTimeAtom utctime
249249 Nothing -> fail "Failed to parse datetime"
250−
250+
251+dateAtomP :: Parser Atom
252+dateAtomP = do
253+ dateString' <- try $ do
254+ dateString <- quotedString
255+ reserved "::date"
256+ return dateString
257+ case parseTime defaultTimeLocale "%Y-%m-%d" dateString' of
258+ Just utctime -> return $ DateAtom utctime
259+ Nothing -> fail "Failed to parse date"
260+
261+doubleAtomP :: Parser Atom
262+doubleAtomP = DoubleAtom <$> (try float)
263+
251264intAtomP :: Parser Atom
252265intAtomP = (IntAtom . fromIntegral) <$> integer
253266