git.iko.soy/typeable/postgresql-simple
on master help sign in
Attempt at support for Day and TimeOfDay result instances
f0bc301Leon P Smith
src/Database/PostgreSQL/Simple/Result.hs+19−17
@@ -166,9 +166,6 @@ instance Result (Binary LB.ByteString) where
166
166
167instance Result ST.Text where
167instance Result ST.Text where
168 convert f = doConvert f okText $ (either left Right . ST.decodeUtf8')
168 convert f = doConvert f okText $ (either left Right . ST.decodeUtf8')
169−{- | isText f = doConvert f okText $ (Right . ST.decodeUtf8)
170− | otherwise = incompatible f (typeOf ST.empty)
171− "attempt to mix binary and text" -}
172 -- FIXME: check character encoding
169 -- FIXME: check character encoding
173
170
174instance Result LT.Text where
171instance Result LT.Text where
@@ -183,30 +180,24 @@ instance Result UTCTime where
183 Just t -> Right t
180 Just t -> Right t
184 Nothing -> returnError ConversionFailed f "could not parse"
181 Nothing -> returnError ConversionFailed f "could not parse"
185 where ok = mkCompats [TimestampWithTimeZone]
182 where ok = mkCompats [TimestampWithTimeZone]
186−{--
183+
187instance Result Day where
184instance Result Day where
188− convert f = flip (atto ok) f $ case fieldType f of
185+ convert f = atto ok date f
189− Year -> year
190− _ -> date
191− where ok = mkCompats [Year,Date,NewDate]
186+ where ok = mkCompats [Date]
192− year = fromGregorian <$> decimal <*> pure 1 <*> pure 1
193 date = fromGregorian <$> (decimal <* char '-')
187 date = fromGregorian <$> (decimal <* char '-')
194 <*> (decimal <* char '-')
188 <*> (decimal <* char '-')
195 <*> decimal
189 <*> decimal
196
190
197instance Result TimeOfDay where
191instance Result TimeOfDay where
198− convert f = flip (atto ok) f $ do
192+ convert f = atto' ok time f
193+ where ok = mkCompats [Time]
194+ time = do
199 hours <- decimal <* char ':'
195 hours <- decimal <* char ':'
200 mins <- decimal <* char ':'
196 mins <- decimal <* char ':'
201 secs <- decimal :: Parser Int
197 secs <- decimal :: Parser Int
202 case makeTimeOfDayValid hours mins (fromIntegral secs) of
198 case makeTimeOfDayValid hours mins (fromIntegral secs) of
203− Just t -> return t
199+ Just t -> return (pure t)
204− _ -> conversionFailed f "TimeOfDay" "could not parse"
200+ _ -> return (returnError ConversionFailed f "could not parse")
205− where ok = mkCompats [Time]
206−
207−isText :: Field -> Bool
208−isText f = fieldCharSet f /= 63
209−--}
210newtype Compat = Compat Word64
201newtype Compat = Compat Word64
211
202
212mkCompats :: [BuiltinType] -> Compat
203mkCompats :: [BuiltinType] -> Compat
@@ -259,5 +250,16 @@ atto types p0 f dat = doConvert f types (go p0) dat
259 Left err -> returnError ConversionFailed f err
250 Left err -> returnError ConversionFailed f err
260 Right v -> Right v
251 Right v -> Right v
261
252
253+atto' :: forall a. (Typeable a)
254+ => Compat -> Parser (Either SomeException a) -> Field -> Maybe ByteString
255+ -> Either SomeException a
256+atto' types p0 f dat = doConvert f types (go p0) dat
257+ where
258+ go :: Parser (Either SomeException a) -> ByteString -> Either SomeException a
259+ go p s =
260+ case parseOnly p s of
261+ Left err -> returnError ConversionFailed f err
262+ Right v -> v
263+
262instance Result RawResult where
264instance Result RawResult where
263 convert field rawData = Right (RawResult field rawData)
265 convert field rawData = Right (RawResult field rawData)