@@ -22,20 +22,16 @@ module Squeel.PostgreSQL.PQ where
22import Control.Exception.Lifted
22import Control.Exception.Lifted
23import Control.Monad.Base
23import Control.Monad.Base
24import Control.Monad.Except
24import Control.Monad.Except
25−import Control.Monad.Trans.Maybe
26import Control.Monad.Trans.Control
25import Control.Monad.Trans.Control
27import Data.ByteString (ByteString)
26import Data.ByteString (ByteString)
28import Data.Function ((&))
27import Data.Function ((&))
30−import Data.Proxy (Proxy)
31−import Data.Text (Text)
32import Generics.SOP
28import Generics.SOP
33import GHC.Exts hiding (fromList)
29import GHC.Exts hiding (fromList)
34import GHC.TypeLits
30import GHC.TypeLits
36import qualified Database.PostgreSQL.LibPQ as LibPQ
32import qualified Database.PostgreSQL.LibPQ as LibPQ
38−import Squeel.PostgreSQL.Binary
34+import Squeel.PostgreSQL.Binary2
39import Squeel.PostgreSQL.Statement
35import Squeel.PostgreSQL.Statement
40import Squeel.PostgreSQL.Schema
36import Squeel.PostgreSQL.Schema
@@ -104,28 +100,24 @@ pqThenExec = pqThen . pqExec
104class Monad m => MonadPQ schema m | m -> schema where
100class Monad m => MonadPQ schema m | m -> schema where
106 pqExecParams
102 pqExecParams
107− :: (ToOids ps, AllZip HasEncoding ps xs)
103+ :: ToParams x params
108− => Manipulation schema ps ys -> NP I xs -> m (Maybe (Result ys))
104+ => Manipulation schema params ys -> x -> m (Maybe (Result ys))
109 default pqExecParams
105 default pqExecParams
110 :: (MonadTrans t, MonadPQ schema m1, m ~ t m1)
106 :: (MonadTrans t, MonadPQ schema m1, m ~ t m1)
111− => (ToOids ps, AllZip HasEncoding ps xs)
107+ => ToParams x params
112− => Manipulation schema ps ys -> NP I xs -> m (Maybe (Result ys))
108+ => Manipulation schema params ys -> x -> m (Maybe (Result ys))
113 pqExecParams manipulation params = lift $ pqExecParams manipulation params
109 pqExecParams manipulation params = lift $ pqExecParams manipulation params
115 pqExecNil :: Manipulation schema '[] ys -> m (Maybe (Result ys))
111 pqExecNil :: Manipulation schema '[] ys -> m (Maybe (Result ys))
116− pqExecNil statement = pqExecParams statement Nil
112+ pqExecNil statement = pqExecParams statement ()
118instance MonadBase IO io => MonadPQ schema (PQ schema schema io) where
114instance MonadBase IO io => MonadPQ schema (PQ schema schema io) where
120− pqExecParams (UnsafeManipulation q :: Manipulation schema ps ys) params =
116+ pqExecParams (UnsafeManipulation q :: Manipulation schema ps ys) (params :: x) =
121 PQ $ \ (Connection conn) -> do
117 PQ $ \ (Connection conn) -> do
123− paramValues = encodings (Proxy :: Proxy ps) params
119+ toParam' bytes = (LibPQ.invalidOid,bytes,LibPQ.Binary)
124− oids = toOids (proxy# :: Proxy# ps)
120+ params' = fmap (fmap toParam') (hcollapse (toParams @x @ps params))
126− [ Just (oid, param', LibPQ.Binary)
127− | (oid, param') <- zip oids paramValues
129 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
121 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
130 return (Result <$> result, Connection conn)
122 return (Result <$> result, Connection conn)
@@ -190,43 +182,25 @@ instance {-# OVERLAPPABLE #-}
190 (KnownNat n, HasColumnNumber (n-1) columns column)
182 (KnownNat n, HasColumnNumber (n-1) columns column)
191 => HasColumnNumber n (column' : columns) column
183 => HasColumnNumber n (column' : columns) column
193−newtype Value pgs m x = Value { runValue :: Result pgs -> m x }
195−instance Applicative m => Applicative (Value pgs m) where
196− pure x = Value $ \ _result -> pure x
197− mf <*> mx = Value $ \ result -> runValue mf result <*> runValue mx result
198−instance Monad m => Monad (Value pgs m) where
200− mx >>= f = Value $ \ result -> do
201− x <- runValue mx result
202− runValue (f x) result
203−instance MonadTrans (Value pgs) where
204− lift m = Value $ \ _result -> m
205−instance MonadBase b m => MonadBase b (Value pgs m) where
206− liftBase = lift . liftBase
209− :: (HasDecoding pg x, MonadBase IO io)
186+ :: (FromColumnValue colty y, MonadBase IO io)
210 => RowNumber
187 => RowNumber
211− -> ColumnNumber n pgs pg
188+ -> ColumnNumber n columns colty
212− -> Value pgs (ExceptT Text (MaybeT io)) x
189+ -> Result columns
213−getValue (RowNumber r) (UnsafeColumnNumber c :: ColumnNumber n pgs pg) =
193+ (UnsafeColumnNumber c :: ColumnNumber n columns colty)
214− Value $ \ (Result result) -> do
194+ (Result result)
195+ = fromColumnValue @colty . K <$>
215− maybeBytestring <- liftBase $ LibPQ.getvalue result r c
196+ liftBase (LibPQ.getvalue result r c)
216− case maybeBytestring of
218− Just bytestring -> case decodeValue (Proxy :: Proxy pg) bytestring of
219− Left err -> throwError err
220− Right val -> return val
223− :: (AllZip HasDecoding pgs xs, MonadBase IO io)
199+ :: (FromRow columns y, MonadBase IO io)
224− => RowNumber -> Value pgs (ExceptT Text (MaybeT io)) (NP I xs)
200+ => RowNumber -> Result columns -> io y
225−getRow (RowNumber r) = Value $ \ (Result result :: Result pgs) -> do
201+getRow (RowNumber r) (Result result :: Result columns) = do
202+ let len = fromIntegral (lengthSList (Proxy @columns))
226− maybeBytestrings <- traverse (liftBase . LibPQ.getvalue result r)
203+ row' <- traverse (liftBase . LibPQ.getvalue result r) [0 .. len - 1]
227− [0 .. fromIntegral (lengthSList (Proxy :: Proxy pgs)) - 1]
228− case fromList (catMaybes maybeBytestrings) of
204+ case fromList row' of
229− Nothing -> mzero
205+ Nothing -> error "getRow: found unexpected length"
230− Just bytestrings -> case decodings (Proxy :: Proxy pgs) bytestrings of
231− Left err -> throwError err
232− Right row -> return row
206+ Just row -> return $ fromRow @columns row