2016-12-22 18:12:38 +00:00
|
|
|
module Database.PostgreSQL
|
2017-06-03 11:10:15 +00:00
|
|
|
( module Row
|
|
|
|
, module Value
|
2018-11-16 11:40:30 +00:00
|
|
|
, PG
|
|
|
|
, PGError(..)
|
|
|
|
, PGErrorDetail
|
2018-10-21 01:24:58 +00:00
|
|
|
, Database
|
2016-12-22 18:12:38 +00:00
|
|
|
, PoolConfiguration
|
|
|
|
, Pool
|
|
|
|
, Connection
|
2016-12-22 19:25:17 +00:00
|
|
|
, Query(..)
|
2016-12-22 18:12:38 +00:00
|
|
|
, newPool
|
|
|
|
, withConnection
|
|
|
|
, withTransaction
|
2018-10-21 01:24:58 +00:00
|
|
|
, defaultPoolConfiguration
|
2018-10-08 19:06:41 +00:00
|
|
|
, command
|
2016-12-22 18:12:38 +00:00
|
|
|
, execute
|
|
|
|
, query
|
2017-01-12 16:12:59 +00:00
|
|
|
, scalar
|
2018-11-16 11:40:30 +00:00
|
|
|
, onIntegrityError
|
2016-12-22 18:12:38 +00:00
|
|
|
) where
|
|
|
|
|
2017-12-04 21:43:36 +00:00
|
|
|
import Prelude
|
|
|
|
|
2018-11-16 11:40:30 +00:00
|
|
|
import Control.Monad.Error.Class (catchError, throwError, try)
|
|
|
|
import Control.Monad.Except.Trans (ExceptT, except, runExceptT)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
2017-06-03 11:10:15 +00:00
|
|
|
import Data.Array (head)
|
2018-11-16 11:40:30 +00:00
|
|
|
import Data.Bifunctor (lmap)
|
2016-12-22 18:12:38 +00:00
|
|
|
import Data.Either (Either(..))
|
2018-11-16 11:40:30 +00:00
|
|
|
import Data.Generic.Rep (class Generic)
|
|
|
|
import Data.Generic.Rep.Show (genericShow)
|
|
|
|
import Data.Maybe (Maybe(..), maybe)
|
2016-12-22 19:25:17 +00:00
|
|
|
import Data.Newtype (class Newtype)
|
2018-11-16 11:40:30 +00:00
|
|
|
import Data.Nullable (Nullable, toMaybe, toNullable)
|
|
|
|
import Data.String (Pattern(..))
|
|
|
|
import Data.String as String
|
2016-12-22 18:12:38 +00:00
|
|
|
import Data.Traversable (traverse)
|
2018-07-15 17:51:17 +00:00
|
|
|
import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), Row10(..), Row11(..), Row12(..), Row13(..), Row14(..), Row15(..), Row16(..), Row17(..), Row18(..), Row19(..), Row2(..), Row3(..), Row4(..), Row5(..), Row6(..), Row7(..), Row8(..), Row9(..), fromSQLRow, toSQLRow) as Row
|
2018-10-21 01:24:58 +00:00
|
|
|
import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), fromSQLRow, toSQLRow)
|
2017-06-03 11:10:15 +00:00
|
|
|
import Database.PostgreSQL.Value (class FromSQLValue)
|
2018-07-15 17:51:17 +00:00
|
|
|
import Database.PostgreSQL.Value (class FromSQLValue, class ToSQLValue, fromSQLValue, instantFromString, instantToString, null, toSQLValue, unsafeIsBuffer) as Value
|
2018-10-21 01:24:58 +00:00
|
|
|
import Effect (Effect)
|
|
|
|
import Effect.Aff (Aff, bracket)
|
|
|
|
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
|
|
|
|
import Effect.Class (liftEffect)
|
2018-11-16 11:40:30 +00:00
|
|
|
import Effect.Exception (Error)
|
2018-10-21 01:24:58 +00:00
|
|
|
import Foreign (Foreign)
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2018-10-21 01:24:58 +00:00
|
|
|
type Database = String
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2018-11-16 11:40:30 +00:00
|
|
|
-- | PostgreSQL computations run in the `PG` monad. It's just `Aff`
|
|
|
|
-- | stacked with ExceptT to provide error handling.
|
|
|
|
type PG a = ExceptT PGError Aff a
|
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | PostgreSQL connection pool configuration.
|
2016-12-22 18:12:38 +00:00
|
|
|
type PoolConfiguration =
|
2018-10-21 01:24:58 +00:00
|
|
|
{ database :: Database
|
|
|
|
, host :: Maybe String
|
|
|
|
, idleTimeoutMillis :: Maybe Int
|
|
|
|
, max :: Maybe Int
|
|
|
|
, password :: Maybe String
|
|
|
|
, port :: Maybe Int
|
|
|
|
, user :: Maybe String
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultPoolConfiguration :: Database -> PoolConfiguration
|
|
|
|
defaultPoolConfiguration database =
|
|
|
|
{ database
|
|
|
|
, host: Nothing
|
|
|
|
, idleTimeoutMillis: Nothing
|
|
|
|
, max: Nothing
|
|
|
|
, password: Nothing
|
|
|
|
, port: Nothing
|
|
|
|
, user: Nothing
|
2016-12-22 18:12:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | PostgreSQL connection pool.
|
2017-04-20 08:05:17 +00:00
|
|
|
foreign import data Pool :: Type
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | PostgreSQL connection.
|
2017-04-20 08:05:17 +00:00
|
|
|
foreign import data Connection :: Type
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | PostgreSQL query with parameter (`$1`, `$2`, …) and return types.
|
2016-12-22 19:25:17 +00:00
|
|
|
newtype Query i o = Query String
|
|
|
|
|
|
|
|
derive instance newtypeQuery :: Newtype (Query i o) _
|
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | Create a new connection pool.
|
2018-07-15 17:51:17 +00:00
|
|
|
newPool :: PoolConfiguration -> Aff Pool
|
2018-10-21 01:24:58 +00:00
|
|
|
newPool cfg =
|
|
|
|
liftEffect <<< ffiNewPool $ cfg'
|
|
|
|
where
|
|
|
|
cfg' =
|
|
|
|
{ user: toNullable cfg.user
|
|
|
|
, password: toNullable cfg.password
|
|
|
|
, host: toNullable cfg.host
|
|
|
|
, port: toNullable cfg.port
|
|
|
|
, database: cfg.database
|
|
|
|
, max: toNullable cfg.max
|
|
|
|
, idleTimeoutMillis: toNullable cfg.idleTimeoutMillis
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Configuration which we actually pass to FFI.
|
|
|
|
type PoolConfiguration' =
|
|
|
|
{ user :: Nullable String
|
|
|
|
, password :: Nullable String
|
|
|
|
, host :: Nullable String
|
|
|
|
, port :: Nullable Int
|
|
|
|
, database :: String
|
|
|
|
, max :: Nullable Int
|
|
|
|
, idleTimeoutMillis :: Nullable Int
|
|
|
|
}
|
2017-06-03 11:43:30 +00:00
|
|
|
|
|
|
|
foreign import ffiNewPool
|
2018-10-21 01:24:58 +00:00
|
|
|
:: PoolConfiguration'
|
2018-07-15 17:51:17 +00:00
|
|
|
-> Effect Pool
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2017-12-05 21:12:01 +00:00
|
|
|
-- | Run an action with a connection. The connection is released to the pool
|
|
|
|
-- | when the action returns.
|
2017-06-03 11:43:30 +00:00
|
|
|
withConnection
|
2018-07-15 17:51:17 +00:00
|
|
|
:: ∀ a
|
2016-12-22 18:12:38 +00:00
|
|
|
. Pool
|
2018-11-16 11:40:30 +00:00
|
|
|
-> (Connection -> PG a)
|
|
|
|
-> PG a
|
2017-06-03 11:43:30 +00:00
|
|
|
withConnection p k =
|
2018-11-16 11:40:30 +00:00
|
|
|
except <=< lift $ bracket (connect p) cleanup run
|
|
|
|
where
|
|
|
|
cleanup (Left _) = pure unit
|
|
|
|
cleanup (Right { done }) = liftEffect done
|
|
|
|
|
|
|
|
run (Left err) = pure $ Left err
|
|
|
|
run (Right { connection }) = runExceptT $ k connection
|
2017-06-03 11:43:30 +00:00
|
|
|
|
2017-12-04 21:43:36 +00:00
|
|
|
connect
|
2018-07-15 17:51:17 +00:00
|
|
|
:: Pool
|
2018-11-16 11:40:30 +00:00
|
|
|
-> Aff (Either PGError ConnectResult)
|
|
|
|
connect =
|
|
|
|
fromEffectFnAff
|
|
|
|
<<< ffiConnect
|
|
|
|
{ nullableLeft: toNullable <<< map Left <<< convertError
|
|
|
|
, right: Right
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ConnectResult =
|
|
|
|
{ connection :: Connection
|
|
|
|
, done :: Effect Unit
|
|
|
|
}
|
2017-12-04 21:43:36 +00:00
|
|
|
|
|
|
|
foreign import ffiConnect
|
2018-11-16 11:40:30 +00:00
|
|
|
:: ∀ a
|
|
|
|
. { nullableLeft :: Error -> Nullable (Either PGError ConnectResult)
|
|
|
|
, right :: a -> Either PGError ConnectResult
|
|
|
|
}
|
|
|
|
-> Pool
|
|
|
|
-> EffectFnAff (Either PGError ConnectResult)
|
2017-12-04 21:43:36 +00:00
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | Run an action within a transaction. The transaction is committed if the
|
|
|
|
-- | action returns, and rolled back when the action throws. If you want to
|
|
|
|
-- | change the transaction mode, issue a separate `SET TRANSACTION` statement
|
|
|
|
-- | within the transaction.
|
2016-12-22 18:12:38 +00:00
|
|
|
withTransaction
|
2018-07-15 17:51:17 +00:00
|
|
|
:: ∀ a
|
2016-12-22 18:12:38 +00:00
|
|
|
. Connection
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG a
|
|
|
|
-> PG a
|
2016-12-22 18:12:38 +00:00
|
|
|
withTransaction conn action =
|
2018-11-16 11:40:30 +00:00
|
|
|
begin *> lift (try $ runExceptT action) >>= case _ of
|
|
|
|
Left jsErr -> do
|
|
|
|
rollback
|
|
|
|
lift $ throwError jsErr
|
|
|
|
Right (Left pgErr) -> do
|
|
|
|
rollback
|
|
|
|
throwError pgErr
|
|
|
|
Right (Right value) -> do
|
|
|
|
commit
|
|
|
|
pure value
|
|
|
|
|
|
|
|
where
|
|
|
|
begin = execute conn (Query "BEGIN TRANSACTION") Row0
|
|
|
|
commit = execute conn (Query "COMMIT TRANSACTION") Row0
|
|
|
|
rollback = execute conn (Query "ROLLBACK TRANSACTION") Row0
|
|
|
|
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | Execute a PostgreSQL query and discard its results.
|
2016-12-22 18:12:38 +00:00
|
|
|
execute
|
2018-07-15 17:51:17 +00:00
|
|
|
:: ∀ i o
|
2016-12-22 18:12:38 +00:00
|
|
|
. (ToSQLRow i)
|
|
|
|
=> Connection
|
2016-12-22 19:25:17 +00:00
|
|
|
-> Query i o
|
2016-12-22 18:12:38 +00:00
|
|
|
-> i
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG Unit
|
2016-12-22 19:25:17 +00:00
|
|
|
execute conn (Query sql) values =
|
2018-11-16 11:40:30 +00:00
|
|
|
void $ unsafeQuery Rows conn sql (toSQLRow values)
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2016-12-24 12:38:36 +00:00
|
|
|
-- | Execute a PostgreSQL query and return its results.
|
2016-12-22 18:12:38 +00:00
|
|
|
query
|
2018-07-15 17:51:17 +00:00
|
|
|
:: ∀ i o
|
2017-04-20 08:05:17 +00:00
|
|
|
. ToSQLRow i
|
|
|
|
=> FromSQLRow o
|
2016-12-22 18:12:38 +00:00
|
|
|
=> Connection
|
2016-12-22 19:25:17 +00:00
|
|
|
-> Query i o
|
2016-12-22 18:12:38 +00:00
|
|
|
-> i
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG (Array o)
|
|
|
|
query conn (Query sql) values = do
|
|
|
|
unsafeQuery Rows conn sql (toSQLRow values)
|
2017-03-17 13:56:50 +00:00
|
|
|
>>= traverse (fromSQLRow >>> case _ of
|
2017-04-18 12:33:43 +00:00
|
|
|
Right row -> pure row
|
2018-11-16 11:40:30 +00:00
|
|
|
Left msg -> throwError $ ConversionError msg)
|
2016-12-22 18:12:38 +00:00
|
|
|
|
2017-06-03 11:43:30 +00:00
|
|
|
-- | Execute a PostgreSQL query and return the first field of the first row in
|
|
|
|
-- | the result.
|
2017-01-12 16:12:59 +00:00
|
|
|
scalar
|
2018-07-15 17:51:17 +00:00
|
|
|
:: ∀ i o
|
2017-04-20 08:05:17 +00:00
|
|
|
. ToSQLRow i
|
|
|
|
=> FromSQLValue o
|
2017-01-12 16:12:59 +00:00
|
|
|
=> Connection
|
2017-06-03 11:10:15 +00:00
|
|
|
-> Query i (Row1 o)
|
2017-01-12 16:12:59 +00:00
|
|
|
-> i
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG (Maybe o)
|
2017-01-12 16:12:59 +00:00
|
|
|
scalar conn sql values =
|
|
|
|
query conn sql values
|
2017-06-03 11:10:15 +00:00
|
|
|
<#> map (case _ of Row1 a -> a) <<< head
|
2017-01-12 16:12:59 +00:00
|
|
|
|
2018-10-09 06:30:12 +00:00
|
|
|
-- | Execute a PostgreSQL query and return its command tag value
|
|
|
|
-- | (how many rows were affected by the query). This may be useful
|
|
|
|
-- | for example with DELETE or UPDATE queries.
|
2018-10-08 19:06:41 +00:00
|
|
|
command
|
|
|
|
:: ∀ i
|
|
|
|
. ToSQLRow i
|
|
|
|
=> Connection
|
|
|
|
-> Query i Int
|
|
|
|
-> i
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG Int
|
|
|
|
command conn (Query sql) values = do
|
|
|
|
result <- unsafeQuery RowCount conn sql (toSQLRow values)
|
|
|
|
case result of
|
|
|
|
[[x]] -> except $ lmap ConversionError (Value.fromSQLValue x)
|
|
|
|
_ -> throwError (ConversionError "unexpected data")
|
|
|
|
|
|
|
|
data QueryMode
|
|
|
|
= Rows
|
|
|
|
| RowCount
|
|
|
|
|
|
|
|
|
|
|
|
type QueryResult
|
|
|
|
= Array (Array Foreign)
|
2018-10-08 19:06:41 +00:00
|
|
|
|
2018-11-16 11:40:30 +00:00
|
|
|
|
|
|
|
unsafeQuery
|
|
|
|
:: QueryMode
|
|
|
|
-> Connection
|
2018-10-08 19:06:41 +00:00
|
|
|
-> String
|
|
|
|
-> Array Foreign
|
2018-11-16 11:40:30 +00:00
|
|
|
-> PG QueryResult
|
|
|
|
unsafeQuery m c s =
|
|
|
|
except <=< lift <<< fromEffectFnAff <<< ffiUnsafeQuery p c s
|
|
|
|
where
|
|
|
|
p =
|
|
|
|
{ queryMode: case m of
|
|
|
|
Rows -> "rows"
|
|
|
|
RowCount -> "rowCount"
|
|
|
|
, nullableLeft: toNullable <<< map Left <<< convertError
|
|
|
|
, right: Right
|
|
|
|
}
|
2018-10-08 19:06:41 +00:00
|
|
|
|
2018-11-16 11:40:30 +00:00
|
|
|
foreign import ffiUnsafeQuery
|
|
|
|
:: { queryMode :: String
|
|
|
|
, nullableLeft :: Error -> Nullable (Either PGError QueryResult)
|
|
|
|
, right :: QueryResult -> Either PGError QueryResult
|
|
|
|
}
|
|
|
|
-> Connection
|
2018-10-08 19:06:41 +00:00
|
|
|
-> String
|
|
|
|
-> Array Foreign
|
2018-11-16 11:40:30 +00:00
|
|
|
-> EffectFnAff (Either PGError (Array (Array Foreign)))
|
|
|
|
|
|
|
|
|
|
|
|
data PGError
|
|
|
|
= ConnectionError String
|
|
|
|
| ConversionError String
|
|
|
|
| InternalError PGErrorDetail
|
|
|
|
| OperationalError PGErrorDetail
|
|
|
|
| ProgrammingError PGErrorDetail
|
|
|
|
| IntegrityError PGErrorDetail
|
|
|
|
| DataError PGErrorDetail
|
|
|
|
| NotSupportedError PGErrorDetail
|
|
|
|
| QueryCanceledError PGErrorDetail
|
|
|
|
| TransactionRollbackError PGErrorDetail
|
|
|
|
|
|
|
|
|
|
|
|
derive instance eqPGError :: Eq PGError
|
|
|
|
derive instance genericPGError :: Generic PGError _
|
|
|
|
|
|
|
|
instance showPGError :: Show PGError where
|
|
|
|
show = genericShow
|
|
|
|
|
|
|
|
type PGErrorDetail =
|
|
|
|
{ severity :: String
|
|
|
|
, code :: String
|
|
|
|
, message :: String
|
|
|
|
, detail :: String
|
|
|
|
, hint :: String
|
|
|
|
, position :: String
|
|
|
|
, internalPosition :: String
|
|
|
|
, internalQuery :: String
|
|
|
|
, where_ :: String
|
|
|
|
, schema :: String
|
|
|
|
, table :: String
|
|
|
|
, column :: String
|
|
|
|
, dataType :: String
|
|
|
|
, constraint :: String
|
|
|
|
, file :: String
|
|
|
|
, line :: String
|
|
|
|
, routine :: String
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign import ffiSQLState :: Error -> Nullable String
|
|
|
|
foreign import ffiErrorDetail :: Error -> PGErrorDetail
|
|
|
|
|
|
|
|
|
|
|
|
convertError :: Error -> Maybe PGError
|
|
|
|
convertError err =
|
|
|
|
case toMaybe $ ffiSQLState err of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just sqlState -> Just $ convert sqlState $ ffiErrorDetail err
|
|
|
|
|
|
|
|
where
|
|
|
|
convert :: String -> PGErrorDetail -> PGError
|
|
|
|
convert s =
|
|
|
|
if prefix "0A" s then NotSupportedError
|
|
|
|
else if prefix "20" s || prefix "21" s then ProgrammingError
|
|
|
|
else if prefix "22" s then DataError
|
|
|
|
else if prefix "23" s then IntegrityError
|
|
|
|
else if prefix "24" s || prefix "25" s then InternalError
|
|
|
|
else if prefix "26" s || prefix "27" s || prefix "28" s then OperationalError
|
|
|
|
else if prefix "2B" s || prefix "2D" s || prefix "2F" s then InternalError
|
|
|
|
else if prefix "34" s then OperationalError
|
|
|
|
else if prefix "38" s || prefix "39" s || prefix "3B" s then InternalError
|
|
|
|
else if prefix "3D" s || prefix "3F" s then ProgrammingError
|
|
|
|
else if prefix "40" s then TransactionRollbackError
|
|
|
|
else if prefix "42" s || prefix "44" s then ProgrammingError
|
|
|
|
else if s == "57014" then QueryCanceledError
|
|
|
|
else if prefix "5" s then OperationalError
|
|
|
|
else if prefix "F" s then InternalError
|
|
|
|
else if prefix "H" s then OperationalError
|
|
|
|
else if prefix "P" s then InternalError
|
|
|
|
else if prefix "X" s then InternalError
|
|
|
|
else const $ ConnectionError s
|
|
|
|
|
|
|
|
prefix :: String -> String -> Boolean
|
|
|
|
prefix p =
|
|
|
|
maybe false (_ == 0) <<< String.indexOf (Pattern p)
|
|
|
|
|
|
|
|
|
|
|
|
onIntegrityError :: forall a. PG a -> PG a -> PG a
|
|
|
|
onIntegrityError errorResult db =
|
|
|
|
catchError db handleError
|
|
|
|
where
|
|
|
|
handleError e =
|
|
|
|
case e of
|
|
|
|
IntegrityError _ -> errorResult
|
|
|
|
_ -> throwError e
|