on dev help sign in
remove stuff

simplify and remove stuff

commit96f20f0aa3bede6a89405c4e1dfe5f34f69b4b03 authorEitan Chatav <eitan.chatav@gmail.com> dateWed 21 Mar 2018, 16:47 (9 years ago) parent6aa5168
5 files changed+25−70
squeal-postgresql/exe/Example.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+5−5 squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+17−62 squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+1−1
squeal-postgresql/exe/Example.hs+1−1
@@ -99,7 +99,7 @@ session :: (MonadBase IO pq, MonadPQ Schema pq) => pq ()
9999session = do
100100 liftBase $ Char8.putStrLn "manipulating"
101101 idResults <- traversePrepared insertUser ([(userName user, userVec user) | user <- users])
102− ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
102+ ids <- traverse (fmap fromOnly . getRow 0) idResults
103103 traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
104104 liftBase $ Char8.putStrLn "querying"
105105 usersResult <- runQuery getUsers
squeal-postgresql/src/Squeal/PostgreSQL.hs+1−1
@@ -185,7 +185,7 @@
185185-- session :: PQ Schema Schema IO ()
186186-- session = do
187187-- idResults <- traversePrepared insertUser (Only . userName <$> users)
188−-- ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
188+-- ids <- traverse (fmap fromOnly . getRow 0) idResults
189189-- traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
190190-- usersResult <- runQuery getUsers
191191-- usersRows <- getRows usersResult
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+5−5
@@ -72,7 +72,7 @@ let
7272 => PQ schema schema IO ()
7373 numMigrations = do
7474 result <- runQuery (selectStar (from (table (#schema_migrations `As` #m))))
75− RowNumber num <- ntuples result
75+ num <- ntuples result
7676 liftBase $ print num
7777:}
7878
@@ -187,7 +187,7 @@ migrateUp migration =
187187 io ()
188188 upMigration step =
189189 queryExecuted step
190− & pqBind (\ (RowNumber executed) ->
190+ & pqBind (\ executed ->
191191 if executed == 1 -- up step has already been executed
192192 then PQ (\ _ -> return (K ())) -- unsafely switch schemas
193193 else
@@ -201,7 +201,7 @@ migrateUp migration =
201201 => Migration io schema0 schema1 -> PQ
202202 ("schema_migrations" ::: MigrationsTable ': schema0)
203203 ("schema_migrations" ::: MigrationsTable ': schema0)
204− io RowNumber
204+ io Row
205205 queryExecuted step = do
206206 result <- runQueryParams selectMigration (Only (name step))
207207 okResult result
@@ -242,7 +242,7 @@ migrateDown migrations =
242242 io ()
243243 downMigration step =
244244 queryExecuted step
245− & pqBind (\ (RowNumber executed) ->
245+ & pqBind (\ executed ->
246246 if executed == 0 -- up step has not been executed
247247 then PQ (\ _ -> return (K ())) -- unsafely switch schemas
248248 else
@@ -256,7 +256,7 @@ migrateDown migrations =
256256 => Migration io schema0 schema1 -> PQ
257257 ("schema_migrations" ::: MigrationsTable ': schema1)
258258 ("schema_migrations" ::: MigrationsTable ': schema1)
259− io RowNumber
259+ io Row
260260 queryExecuted step = do
261261 result <- runQueryParams selectMigration (Only (name step))
262262 okResult result
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+17−62
@@ -33,7 +33,8 @@ a `MonadPQ` constraint for running a `Manipulation` or `Query`.
3333
3434module Squeal.PostgreSQL.PQ
3535 ( -- * Connection
36− connectdb
36+ LibPQ.Connection
37+ , connectdb
3738 , finish
3839 , withConnection
3940 , lowerConnection
@@ -47,22 +48,17 @@ module Squeal.PostgreSQL.PQ
4748 , PQRun
4849 , pqliftWith
4950 -- * Result
50− , RowNumber (RowNumber, unRowNumber)
51− , ColumnNumber (UnsafeColumnNumber, getColumnNumber)
52− , HasColumnNumber (columnNumber)
53− , getValue
51+ , LibPQ.Result
52+ , LibPQ.Row
5453 , getRow
5554 , getRows
56− , ntuples
5755 , nextRow
5856 , firstRow
5957 , liftResult
58+ , ntuples
59+ , LibPQ.ExecStatus (..)
6060 , resultStatus
6161 , resultErrorMessage
62− -- * Re-export
63− , LibPQ.Connection
64− , LibPQ.Result
65− , LibPQ.ExecStatus (..)
6662 ) where
6763
6864import Control.Exception.Lifted
@@ -76,8 +72,6 @@ import Data.Kind
7672import Data.Monoid
7773import Data.Traversable
7874import Generics.SOP
79−import GHC.Exts hiding (fromList)
80−import GHC.TypeLits
8175
8276import qualified Database.PostgreSQL.LibPQ as LibPQ
8377
@@ -504,54 +498,15 @@ instance MonadBaseControl b m => MonadBaseControl b (PQ schema schema m) where
504498 pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
505499 restoreM = PQ . const . restoreM
506500
507−-- | Just newtypes around a `CInt`
508−newtype RowNumber = RowNumber { unRowNumber :: LibPQ.Row }
509−
510−-- | In addition to being newtypes around a `CInt`, a `ColumnNumber` is
511−-- parameterized by a `Nat`ural number and acts as an index into a row.
512−newtype ColumnNumber (n :: Nat) (cs :: [k]) (c :: k) =
513− UnsafeColumnNumber { getColumnNumber :: LibPQ.Column }
514−
515−-- | >>> getColumnNumber (columnNumber @5 @'[_,_,_,_,_,_])
516−-- Col 5
517−class KnownNat n => HasColumnNumber n columns column
518− | n columns -> column where
519− columnNumber :: ColumnNumber n columns column
520− columnNumber =
521− UnsafeColumnNumber . fromIntegral $ natVal' (proxy# :: Proxy# n)
522−instance {-# OVERLAPPING #-} HasColumnNumber 0 (column1:columns) column1
523−instance {-# OVERLAPPABLE #-}
524− (KnownNat n, HasColumnNumber (n-1) columns column)
525− => HasColumnNumber n (column' : columns) column
526−
527−-- | Get a single value corresponding to a given row and column number
528−-- from a `Result`.
529−getValue
530− :: (FromColumnValue colty y, MonadBase IO io)
531− => RowNumber -- ^ row
532− -> ColumnNumber n columns colty -- ^ col
533− -> K LibPQ.Result columns -- ^ result
534− -> io y
535−getValue
536− (RowNumber r)
537− (UnsafeColumnNumber c :: ColumnNumber n columns colty)
538− (K result)
539− = fmap (fromColumnValue @colty . K) $ liftBase $ do
540− numRows <- LibPQ.ntuples result
541− when (numRows < r) $ error $
542− "getValue: expected at least " <> show r <> "rows but only saw "
543− <> show numRows
544− LibPQ.getvalue result r c
545−
546501-- | Get a row corresponding to a given row number from a `Result`.
547502getRow
548503 :: (FromRow columns y, MonadBase IO io)
549− => RowNumber
504+ => LibPQ.Row
550505 -- ^ row
551506 -> K LibPQ.Result columns
552507 -- ^ result
553508 -> io y
554−getRow (RowNumber r) (K result :: K LibPQ.Result columns) = liftBase $ do
509+getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
555510 numRows <- LibPQ.ntuples result
556511 when (numRows < r) $ error $
557512 "getRow: expected at least " <> show r <> "rows but only saw "
@@ -562,27 +517,23 @@ getRow (RowNumber r) (K result :: K LibPQ.Result columns) = liftBase $ do
562517 Nothing -> error "getRow: found unexpected length"
563518 Just row -> return $ fromRow @columns row
564519
565−-- | Returns the number of rows (tuples) in the query result.
566−ntuples :: MonadBase IO io => K LibPQ.Result columns -> io RowNumber
567−ntuples (K result) = liftBase $ RowNumber <$> LibPQ.ntuples result
568−
569520-- | Intended to be used for unfolding in streaming libraries, `nextRow`
570521-- takes a total number of rows (which can be found with `ntuples`)
571522-- and a `Result` and given a row number if it's too large returns `Nothing`,
572523-- otherwise returning the row along with the next row number.
573524nextRow
574525 :: (FromRow columns y, MonadBase IO io)
575− => RowNumber -- ^ total number of rows
526+ => LibPQ.Row -- ^ total number of rows
576527 -> K LibPQ.Result columns -- ^ result
577− -> RowNumber -- ^ row number
578− -> io (Maybe (RowNumber,y))
579−nextRow (RowNumber total) (K result :: K LibPQ.Result columns) (RowNumber r)
528+ -> LibPQ.Row -- ^ row number
529+ -> io (Maybe (LibPQ.Row,y))
530+nextRow total (K result :: K LibPQ.Result columns) r
580531 = liftBase $ if r >= total then return Nothing else do
581532 let len = fromIntegral (lengthSList (Proxy @columns))
582533 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
583534 case fromList row' of
584535 Nothing -> error "nextRow: found unexpected length"
585− Just row -> return $ Just (RowNumber (r+1), fromRow @columns row)
536+ Just row -> return $ Just (r+1, fromRow @columns row)
586537
587538-- | Get all rows from a `Result`.
588539getRows
@@ -619,6 +570,10 @@ liftResult
619570 -> K LibPQ.Result results -> io x
620571liftResult f (K result) = liftBase $ f result
621572
573+-- | Returns the number of rows (tuples) in the query result.
574+ntuples :: MonadBase IO io => K LibPQ.Result columns -> io LibPQ.Row
575+ntuples = liftResult LibPQ.ntuples
576+
622577-- | Returns the result status of the command.
623578resultStatus :: MonadBase IO io => K LibPQ.Result results -> io LibPQ.ExecStatus
624579resultStatus = liftResult LibPQ.resultStatus
squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+1−1
@@ -22,7 +22,7 @@ Squeal transaction control language.
2222#-}
2323
2424module Squeal.PostgreSQL.Transaction
25− ( -- * Transactions
25+ ( -- * Transaction
2626 transactionally
2727 , transactionally_
2828 , begin