module Database.PostgreSQL.Simple.Vector.Unboxed 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.Unboxed as VU

-- | 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, VU.Unbox r) => Connection -> Query -> q -> IO (VU.Vector r)
query :: forall q r.
(ToRow q, FromRow r, Unbox r) =>
Connection -> Query -> q -> IO (Vector r)
query = forall q r.
(ToRow q, Unbox r) =>
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, VU.Unbox r) => Connection -> Query -> IO (VU.Vector r)
query_ :: forall r.
(FromRow r, Unbox r) =>
Connection -> Query -> IO (Vector r)
query_ = forall r.
Unbox 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, VU.Unbox r) => RowParser r -> Connection -> Query -> q -> IO (VU.Vector r)
queryWith :: forall q r.
(ToRow q, Unbox r) =>
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.
Unbox r =>
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithVU RowParser r
parser Connection
conn Query
template Result
result

-- | A version of 'query_' taking parser as argument
queryWith_ :: VU.Unbox r => RowParser r -> Connection -> Query -> IO (VU.Vector r)
queryWith_ :: forall r.
Unbox 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.
Unbox r =>
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithVU 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, VU.Unbox r) => Connection -> Query -> [q] -> IO (VU.Vector r)
returning :: forall q r.
(ToRow q, FromRow r, Unbox r) =>
Connection -> Query -> [q] -> IO (Vector r)
returning = forall q r.
(ToRow q, Unbox r) =>
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, VU.Unbox r) => RowParser r -> Connection -> Query -> [q] -> IO (VU.Vector r)
returningWith :: forall q r.
(ToRow q, Unbox r) =>
RowParser r -> Connection -> Query -> [q] -> IO (Vector r)
returningWith RowParser r
_ Connection
_ Query
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Unbox a => Vector a
VU.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.
Unbox r =>
RowParser r -> Connection -> Query -> Result -> IO (Vector r)
finishQueryWithVU RowParser r
parser Connection
conn Query
q Result
result