Make getColumnTable report missing columns

This commit is contained in:
2023-09-16 04:58:23 +02:00
parent 591f4cddfa
commit ab396b9db6
3 changed files with 28 additions and 28 deletions
+12 -14
View File
@@ -62,16 +62,16 @@ fetchResult result = do
runExceptT $ mapM (ExceptT . flip fromRow result) [0..nRows - 1]
class FromRow a where
getColumnTable :: Proxy a -> Result -> IO [LibPQ.Column]
default getColumnTable :: (Generic a, GetColumnTable' (Rep a)) => Proxy a -> Result -> IO [LibPQ.Column]
getColumnTable Proxy = getColumnTable' @(Rep a) Proxy
getColumnTable :: Proxy a -> Result -> IO (Either Error [Column])
default getColumnTable :: (Generic a, GetColumnTable' (Rep a)) => Proxy a -> Result -> IO (Either Error [Column])
getColumnTable Proxy = runExceptT . getColumnTable' @(Rep a) Proxy
fromRow :: Row -> Result -> IO (Either Error a)
default fromRow :: (Generic a, FromRow' (Rep a)) => Row -> Result -> IO (Either Error a)
fromRow row result = fmap to <$> fromRow' row result
class GetColumnTable' f where
getColumnTable' :: Proxy (f p) -> Result -> IO [LibPQ.Column]
getColumnTable' :: Proxy (f p) -> Result -> ExceptT Error IO [Column]
instance GetColumnTable' f => GetColumnTable' (M1 D c f) where
getColumnTable' Proxy = getColumnTable' @f Proxy
@@ -83,17 +83,15 @@ instance (GetColumnTable' f, GetColumnTable' g) => GetColumnTable' (f :*: g) whe
getColumnTable' Proxy result =
(++) <$> getColumnTable' @f Proxy result <*> getColumnTable' @g Proxy result
checkColumn :: FromField f => Proxy f -> String -> Result -> IO [Column]
checkColumn Proxy nameStr result = LibPQ.fnumber result name >>= \case
Just column -> do
-- TODO: Rewrite FromField to check whether oid works for decoding t
_oid <- LibPQ.ftype result column
pure [column]
Nothing -> do
-- TODO: Return ErrorMissingColumn
undefined
checkColumn :: FromField f => Proxy f -> String -> Result -> ExceptT Error IO [Column]
checkColumn Proxy nameStr result = do
column <- ExceptT $ maybe (Left $ ErrorMissingColumn nameText) Right <$> LibPQ.fnumber result name
-- TODO: Rewrite FromField to check whether oid works for decoding t
_oid <- ExceptT $ Right <$> LibPQ.ftype result column
pure [column]
where
name = Encoding.encodeUtf8 $ Text.pack nameStr
nameText = Text.pack nameStr
name = Encoding.encodeUtf8 nameText
instance {-# OVERLAPPABLE #-} (FromField t, KnownSymbol nameSym) => GetColumnTable' (M1 S ('MetaSel ('Just nameSym) nu ns dl) (Rec0 t)) where
getColumnTable' Proxy = checkColumn @t Proxy $ symbolVal @nameSym Proxy
@@ -29,6 +29,7 @@ p \/ q = \x -> p x || q x
data FieldError
= FieldErrorUnexpectedNull
-- TODO: Move this to the normal Error
| FieldErrorInvalidOid Oid
| FieldErrorInvalidField Oid Text String
deriving (Eq, Show)