-- | 'query' variants returning 'V.Vector'.
module Database.PostgreSQL.Simple.Vector where

import           Database.PostgreSQL.Simple (Connection, formatQuery, formatMany)
import           Database.PostgreSQL.Simple.FromRow (FromRow(..))
import           Database.PostgreSQL.Simple.ToRow (ToRow(..))
import           Database.PostgreSQL.Simple.Internal (RowParser, exec)
import           Database.PostgreSQL.Simple.Internal.PQResultUtils
import           Database.PostgreSQL.Simple.Types ( Query (..) )

import qualified Data.Vector as V

-- | Perform a @SELECT@ or other SQL query that is expected to return
-- results. All results are retrieved and converted before this
-- function returns.
query :: (ToRow q, FromRow r) => Connection -> Query -> q -> IO (V.Vector r)
query :: forall q r.
(ToRow q, FromRow r) =>
Connection -> Query -> q -> IO (Vector r)
query = forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> q -> IO (Vector r)
queryWith forall a. FromRow a => RowParser a
fromRow

-- | A version of 'query' that does not perform query substitution.
query_ :: (FromRow r) => Connection -> Query -> IO (V.Vector r)
query_ :: forall r. FromRow r => Connection -> Query -> IO (Vector r)
query_ = forall r. RowParser r -> Connection -> Query -> IO (Vector r)
queryWith_ forall a. FromRow a => RowParser a
fromRow

-- | A version of 'query' taking parser as argument
queryWith :: ToRow q => RowParser r -> Connection -> Query -> q -> IO (V.Vector r)
queryWith :: forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> q -> IO (Vector r)
queryWith RowParser r
parser Connection
conn Query
template q
qs = do
  Result
result <- Connection -> ByteString -> IO Result
exec Connection
conn forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall q. ToRow q => Connection -> Query -> q -> IO ByteString
formatQuery Connection
conn Query
template q
qs
  forall r.
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithV RowParser r
parser Connection
conn Query
template Result
result

-- | A version of 'query_' taking parser as argument
queryWith_ :: RowParser r -> Connection -> Query -> IO (V.Vector r)
queryWith_ :: forall r. RowParser r -> Connection -> Query -> IO (Vector r)
queryWith_ RowParser r
parser Connection
conn q :: Query
q@(Query ByteString
que) = do
  Result
result <- Connection -> ByteString -> IO Result
exec Connection
conn ByteString
que
  forall r.
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithV RowParser r
parser Connection
conn Query
q Result
result

-- | Execute @INSERT ... RETURNING@, @UPDATE ... RETURNING@, or other SQL
-- query that accepts multi-row input and is expected to return results.
returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO (V.Vector r)
returning :: forall q r.
(ToRow q, FromRow r) =>
Connection -> Query -> [q] -> IO (Vector r)
returning = forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> [q] -> IO (Vector r)
returningWith forall a. FromRow a => RowParser a
fromRow

-- | A version of 'returning' taking parser as argument
returningWith :: (ToRow q) => RowParser r -> Connection -> Query -> [q] -> IO (V.Vector r)
returningWith :: forall q r.
ToRow q =>
RowParser r -> Connection -> Query -> [q] -> IO (Vector r)
returningWith RowParser r
_ Connection
_ Query
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Vector a
V.empty
returningWith RowParser r
parser Connection
conn Query
q [q]
qs = do
  Result
result <- Connection -> ByteString -> IO Result
exec Connection
conn forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall q. ToRow q => Connection -> Query -> [q] -> IO ByteString
formatMany Connection
conn Query
q [q]
qs
  forall r.
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithV RowParser r
parser Connection
conn Query
q Result
result