-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Dead simple storage interface to PostgreSQL -- -- Dead simple storage interface to PostgreSQL @package pg-store @version 0.0.1 module Database.PostgreSQL.Store.Columns -- | Query parameter or value of a column - see pack on how to -- generate Values manually but conveniently. data Value Value :: Oid -> ByteString -> Format -> Value -- | Type object identifier [valueType] :: Value -> Oid -- | Data value [valueData] :: Value -> ByteString -- | Data format [valueFormat] :: Value -> Format NullValue :: Value -- | Description of a column data ColumnDescription ColumnDescription :: String -> Bool -> ColumnDescription -- | Type name (e.g. bool, integer) [columnTypeName] :: ColumnDescription -> String -- | Can the column be null? [columnTypeNull] :: ColumnDescription -> Bool -- | Generate column description in SQL. Think CREATE TABLE. makeColumnDescription :: ColumnDescription -> String -- | Generate the sanitized representation of a name. sanitizeName :: Name -> String -- | Similiar to "sanitizeName" but encloses the name in quotes. sanitizeName' :: Name -> String -- | Generate the name for the identifying field. identField :: Name -> String -- | Similiar to "identField" but encloses the name in quotes. identField' :: Name -> String -- | Column type class Column a -- | Pack column value. pack :: Column a => a -> Value -- | Unpack column value. unpack :: Column a => Value -> Maybe a -- | Descripe the column type. describeColumn :: Column a => Proxy a -> ColumnDescription instance GHC.Classes.Ord Database.PostgreSQL.Store.Columns.ColumnDescription instance GHC.Classes.Eq Database.PostgreSQL.Store.Columns.ColumnDescription instance GHC.Show.Show Database.PostgreSQL.Store.Columns.ColumnDescription instance GHC.Classes.Ord Database.PostgreSQL.Store.Columns.Value instance GHC.Classes.Eq Database.PostgreSQL.Store.Columns.Value instance GHC.Show.Show Database.PostgreSQL.Store.Columns.Value instance Database.PostgreSQL.Store.Columns.Column a => Database.PostgreSQL.Store.Columns.Column (GHC.Base.Maybe a) instance Database.PostgreSQL.Store.Columns.Column GHC.Types.Bool instance Database.PostgreSQL.Store.Columns.Column GHC.Types.Int instance Database.PostgreSQL.Store.Columns.Column GHC.Int.Int8 instance Database.PostgreSQL.Store.Columns.Column GHC.Int.Int16 instance Database.PostgreSQL.Store.Columns.Column GHC.Int.Int32 instance Database.PostgreSQL.Store.Columns.Column GHC.Int.Int64 instance Database.PostgreSQL.Store.Columns.Column [GHC.Types.Char] instance Database.PostgreSQL.Store.Columns.Column Data.Text.Internal.Text instance Database.PostgreSQL.Store.Columns.Column Data.Text.Internal.Lazy.Text instance Database.PostgreSQL.Store.Columns.Column Data.ByteString.Internal.ByteString instance Database.PostgreSQL.Store.Columns.Column Data.ByteString.Lazy.Internal.ByteString module Database.PostgreSQL.Store.Result -- | Error that occured during result processing data ResultError ColumnMissing :: ByteString -> ResultError ValueError :: Row -> Column -> Oid -> Format -> ColumnDescription -> ResultError -- | Result processor type ResultProcessor = ReaderT Result (ExceptT ResultError IO) -- | Column information type ColumnInfo = (Column, Oid, Format) -- | Process the result. runResultProcessor :: Result -> ResultProcessor a -> ExceptT ResultError IO a -- | Get the column number for a column name. columnNumber :: ByteString -> ResultProcessor Column -- | Get the type of a column. columnType :: Column -> ResultProcessor Oid -- | Get the format of a column. columnFormat :: Column -> ResultProcessor Format -- | Get information about a column. columnInfo :: ByteString -> ResultProcessor ColumnInfo -- | Do something for each row. foreachRow :: (Row -> ResultProcessor a) -> ResultProcessor [a] -- | Get cell value. cellValue :: Row -> ColumnInfo -> ResultProcessor Value -- | Unpack cell value. unpackCellValue :: (Column a) => Row -> ColumnInfo -> ResultProcessor a -- | Result row class Result a -- | Extract rows from the given result. resultProcessor :: Result a => ResultProcessor [a] -- | A row containing all a list of all column values. newtype RawRow RawRow :: [Value] -> RawRow instance GHC.Classes.Ord Database.PostgreSQL.Store.Result.RawRow instance GHC.Classes.Eq Database.PostgreSQL.Store.Result.RawRow instance GHC.Show.Show Database.PostgreSQL.Store.Result.RawRow instance GHC.Classes.Eq Database.PostgreSQL.Store.Result.ResultError instance GHC.Show.Show Database.PostgreSQL.Store.Result.ResultError instance Database.PostgreSQL.Store.Result.Result () instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b) => Database.PostgreSQL.Store.Result.Result (a, b) instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b, Database.PostgreSQL.Store.Result.Result c) => Database.PostgreSQL.Store.Result.Result (a, b, c) instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b, Database.PostgreSQL.Store.Result.Result c, Database.PostgreSQL.Store.Result.Result d) => Database.PostgreSQL.Store.Result.Result (a, b, c, d) instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b, Database.PostgreSQL.Store.Result.Result c, Database.PostgreSQL.Store.Result.Result d, Database.PostgreSQL.Store.Result.Result e) => Database.PostgreSQL.Store.Result.Result (a, b, c, d, e) instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b, Database.PostgreSQL.Store.Result.Result c, Database.PostgreSQL.Store.Result.Result d, Database.PostgreSQL.Store.Result.Result e, Database.PostgreSQL.Store.Result.Result f) => Database.PostgreSQL.Store.Result.Result (a, b, c, d, e, f) instance (Database.PostgreSQL.Store.Result.Result a, Database.PostgreSQL.Store.Result.Result b, Database.PostgreSQL.Store.Result.Result c, Database.PostgreSQL.Store.Result.Result d, Database.PostgreSQL.Store.Result.Result e, Database.PostgreSQL.Store.Result.Result f, Database.PostgreSQL.Store.Result.Result g) => Database.PostgreSQL.Store.Result.Result (a, b, c, d, e, f, g) instance Database.PostgreSQL.Store.Result.Result Database.PostgreSQL.Store.Result.RawRow module Database.PostgreSQL.Store.Query -- | Description of a table type data TableDescription TableDescription :: String -> String -> TableDescription -- | Table name [tableName] :: TableDescription -> String -- | Identifier column name [tableIdentifier] :: TableDescription -> String -- | Attach meta data to a table type class DescribableTable a where describeTable proxy = TableDescription {tableName = describeTableName proxy, tableIdentifier = describeTableIdentifier proxy} describeTableName proxy = tableName (describeTable proxy) describeTableIdentifier proxy = tableIdentifier (describeTable proxy) -- | Describe the table. describeTable :: DescribableTable a => Proxy a -> TableDescription -- | Describe table name. describeTableName :: DescribableTable a => Proxy a -> String -- | Describe table identifier. describeTableIdentifier :: DescribableTable a => Proxy a -> String -- | Query including statement and parameters. Use the pgsq -- quasi-quoter to conveniently create queries. data Query Query :: !ByteString -> ![Value] -> Query -- | Statement [queryStatement] :: Query -> !ByteString -- | Parameters [queryParams] :: Query -> ![Value] -- | Generate a Query from a SQL statement. -- --
-- {-# LANGUAGE QuasiQuotes #-}
-- module MyModule where
--
-- ...
--
-- data Table = Table { myField :: Int }
-- mkTable ''Table []
--
-- myQuery :: Query
-- myQuery = [pgsq| SELECT * FROM Table WHERE myField > 1337 |]
--
--
-- The SQL statement associated with myQuery will be:
--
-- -- SELECT * FROM "MyModule.Table" WHERE "MyModule.myField" > 1337 ---- --
-- magicNumber :: Int -- magicNumber = 1337 -- -- myQuery :: Query -- myQuery = [pgsq| SELECT * FROM Table WHERE myField > $magicNumber |] ---- --
-- [pgsq| SELECT * -- FROM TableA, TableB -- WHERE refToB = &TableB |] ---- -- Note refToB is a field of TableA. In different -- circumstances one would write such query as follows. -- --
-- SELECT * -- FROM TableA a, Table b -- WHERE a.refToB = b.id --pgsq :: QuasiQuoter instance GHC.Classes.Ord Database.PostgreSQL.Store.Query.Query instance GHC.Classes.Eq Database.PostgreSQL.Store.Query.Query instance GHC.Show.Show Database.PostgreSQL.Store.Query.Query instance GHC.Classes.Ord Database.PostgreSQL.Store.Query.TableDescription instance GHC.Classes.Eq Database.PostgreSQL.Store.Query.TableDescription instance GHC.Show.Show Database.PostgreSQL.Store.Query.TableDescription module Database.PostgreSQL.Store.Errand -- | Error during errand data ErrandError NoResult :: ErrandError ExecError :: ExecStatus -> (Maybe ByteString) -> ErrandError ResultError :: ResultError -> ErrandError UnexpectedEmptyResult :: ErrandError UserError :: String -> ErrandError -- | An interaction with the database type Errand = ReaderT Connection (ExceptT ErrandError IO) -- | Run an errand. runErrand :: Connection -> Errand a -> IO (Either ErrandError a) -- | Raise an error. raiseErrandError :: ErrandError -> Errand a -- | Execute a query and return its result. executeQuery :: Query -> Errand Result -- | Execute a query and process its result set. It is essential that all -- fields required by the underlying result parser are present. query :: (Result a) => Query -> Errand [a] -- | Execute a query. query_ :: Query -> Errand () instance GHC.Classes.Eq Database.PostgreSQL.Store.Errand.ErrandError instance GHC.Show.Show Database.PostgreSQL.Store.Errand.ErrandError module Database.PostgreSQL.Store.Table -- | Description of a table type data TableDescription TableDescription :: String -> String -> TableDescription -- | Table name [tableName] :: TableDescription -> String -- | Identifier column name [tableIdentifier] :: TableDescription -> String class (DescribableTable a) => Table a -- | Insert a row into the table and return a Reference to the -- inserted row. insert :: Table a => a -> Errand (Reference a) -- | Find the row identified by the given reference. find :: (Table a, HasID i) => i a -> Errand (Row a) -- | Update an existing row. update :: (Table a, HasID i) => i a -> a -> Errand () -- | Delete a row from the table. delete :: (Table a, HasID i) => i a -> Errand () -- | Generate the query which creates this table inside the database. Use -- mkCreateQuery for convenience. createQuery :: Table a => Proxy a -> Query -- | Extract rows from a result set. tableResultProcessor :: Table a => ResultProcessor [Row a] -- | Extract only a Reference to each row. tableRefResultProcessor :: Table a => ResultProcessor [Reference a] -- | Resolved row data Row a Row :: !Int64 -> !a -> Row a -- | Identifier [rowID] :: Row a -> !Int64 -- | Value [rowValue] :: Row a -> !a -- | Reference to a row newtype Reference a Reference :: Int64 -> Reference a -- | A value of that type contains an ID. class HasID a -- | Retrieve the underlying ID. referenceID :: HasID a => a b -> Int64 -- | Options to mkTable. data TableConstraint -- | A combination of fields must be unique. Unique ['name1, 'name2, -- ...] works analogous to the following table constraint: -- UNIQUE (name1, name2, ...) Unique :: [Name] -> TableConstraint -- | A combination of fields references another combination of fields from -- a different table. ForeignKey ['name1, 'name2, ...] ''RefTable -- ['refname1, 'refname2, ...] works like this table constraint in -- SQL: FOREIGN KEY (name1, name2, ...) REFERENCES RefTable(refname1, -- refname2, ...) ForeignKey :: [Name] -> Name -> [Name] -> TableConstraint -- | Implement Table for a data type. The given type must fulfill -- these requirements: -- --
-- {-# LANGUAGE TemplateHaskell #-}
-- module Movies where
--
-- ...
--
-- data Movie = Movie {
-- movieTitle :: String,
-- movieYear :: Int
-- } deriving Show
--
-- mkTable ''Movie []
--
-- data Actor = Actor {
-- actorName :: String,
-- actorAge :: Int
-- } deriving Show
--
-- mkTable ''Actor []
--
-- data MovieCast = MovieCast {
-- movieCastMovie :: Reference Movie,
-- movieCastActor :: Reference Actor
-- } deriving Show
--
-- mkTable ''MovieCast []
--
mkTable :: Name -> [TableConstraint] -> Q [Dec]
-- | Generate a Query which will create the table described my the
-- given type.
--
-- Example:
--
--
-- data Table = Table { myField :: Int }
-- mkTable ''Table []
-- ...
-- query_ $(mkCreateQuery ''Table)
--
mkCreateQuery :: Name -> Q Exp
instance GHC.Classes.Ord Database.PostgreSQL.Store.Table.TableConstraint
instance GHC.Classes.Eq Database.PostgreSQL.Store.Table.TableConstraint
instance GHC.Show.Show Database.PostgreSQL.Store.Table.TableConstraint
instance GHC.Classes.Ord (Database.PostgreSQL.Store.Table.Reference a)
instance GHC.Classes.Eq (Database.PostgreSQL.Store.Table.Reference a)
instance GHC.Show.Show (Database.PostgreSQL.Store.Table.Reference a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (Database.PostgreSQL.Store.Table.Row a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Database.PostgreSQL.Store.Table.Row a)
instance GHC.Show.Show a => GHC.Show.Show (Database.PostgreSQL.Store.Table.Row a)
instance Database.PostgreSQL.Store.Table.Table a => Database.PostgreSQL.Store.Columns.Column (Database.PostgreSQL.Store.Table.Reference a)
instance Database.PostgreSQL.Store.Table.Table a => Database.PostgreSQL.Store.Result.Result (Database.PostgreSQL.Store.Table.Row a)
instance Database.PostgreSQL.Store.Table.Table a => Database.PostgreSQL.Store.Result.Result (Database.PostgreSQL.Store.Table.Reference a)
instance Database.PostgreSQL.Store.Table.HasID Database.PostgreSQL.Store.Table.Row
instance Database.PostgreSQL.Store.Table.HasID Database.PostgreSQL.Store.Table.Reference
module Database.PostgreSQL.Store
-- | Options to mkTable.
data TableConstraint
-- | A combination of fields must be unique. Unique ['name1, 'name2,
-- ...] works analogous to the following table constraint:
-- UNIQUE (name1, name2, ...)
Unique :: [Name] -> TableConstraint
-- | A combination of fields references another combination of fields from
-- a different table. ForeignKey ['name1, 'name2, ...] ''RefTable
-- ['refname1, 'refname2, ...] works like this table constraint in
-- SQL: FOREIGN KEY (name1, name2, ...) REFERENCES RefTable(refname1,
-- refname2, ...)
ForeignKey :: [Name] -> Name -> [Name] -> TableConstraint
-- | Implement Table for a data type. The given type must fulfill
-- these requirements:
--
--
-- {-# LANGUAGE TemplateHaskell #-}
-- module Movies where
--
-- ...
--
-- data Movie = Movie {
-- movieTitle :: String,
-- movieYear :: Int
-- } deriving Show
--
-- mkTable ''Movie []
--
-- data Actor = Actor {
-- actorName :: String,
-- actorAge :: Int
-- } deriving Show
--
-- mkTable ''Actor []
--
-- data MovieCast = MovieCast {
-- movieCastMovie :: Reference Movie,
-- movieCastActor :: Reference Actor
-- } deriving Show
--
-- mkTable ''MovieCast []
--
mkTable :: Name -> [TableConstraint] -> Q [Dec]
-- | Resolved row
data Row a
Row :: !Int64 -> !a -> Row a
-- | Identifier
[rowID] :: Row a -> !Int64
-- | Value
[rowValue] :: Row a -> !a
-- | Reference to a row
newtype Reference a
Reference :: Int64 -> Reference a
-- | Query including statement and parameters. Use the pgsq
-- quasi-quoter to conveniently create queries.
data Query
Query :: !ByteString -> ![Value] -> Query
-- | Statement
[queryStatement] :: Query -> !ByteString
-- | Parameters
[queryParams] :: Query -> ![Value]
-- | Generate a Query from a SQL statement.
--
--
-- {-# LANGUAGE QuasiQuotes #-}
-- module MyModule where
--
-- ...
--
-- data Table = Table { myField :: Int }
-- mkTable ''Table []
--
-- myQuery :: Query
-- myQuery = [pgsq| SELECT * FROM Table WHERE myField > 1337 |]
--
--
-- The SQL statement associated with myQuery will be:
--
-- -- SELECT * FROM "MyModule.Table" WHERE "MyModule.myField" > 1337 ---- --
-- magicNumber :: Int -- magicNumber = 1337 -- -- myQuery :: Query -- myQuery = [pgsq| SELECT * FROM Table WHERE myField > $magicNumber |] ---- --
-- [pgsq| SELECT * -- FROM TableA, TableB -- WHERE refToB = &TableB |] ---- -- Note refToB is a field of TableA. In different -- circumstances one would write such query as follows. -- --
-- SELECT * -- FROM TableA a, Table b -- WHERE a.refToB = b.id --pgsq :: QuasiQuoter -- | Generate a Query which will create the table described my the -- given type. -- -- Example: -- --
-- data Table = Table { myField :: Int }
-- mkTable ''Table []
-- ...
-- query_ $(mkCreateQuery ''Table)
--
mkCreateQuery :: Name -> Q Exp
-- | Error that occured during result processing
data ResultError
ColumnMissing :: ByteString -> ResultError
ValueError :: Row -> Column -> Oid -> Format -> ColumnDescription -> ResultError
-- | Error during errand
data ErrandError
NoResult :: ErrandError
ExecError :: ExecStatus -> (Maybe ByteString) -> ErrandError
ResultError :: ResultError -> ErrandError
UnexpectedEmptyResult :: ErrandError
UserError :: String -> ErrandError
-- | An interaction with the database
type Errand = ReaderT Connection (ExceptT ErrandError IO)
-- | Run an errand.
runErrand :: Connection -> Errand a -> IO (Either ErrandError a)
-- | Execute a query and process its result set. It is essential that all
-- fields required by the underlying result parser are present.
query :: (Result a) => Query -> Errand [a]
-- | Execute a query.
query_ :: Query -> Errand ()
-- | Insert a row into the table and return a Reference to the
-- inserted row.
insert :: Table a => a -> Errand (Reference a)
-- | Find the row identified by the given reference.
find :: (Table a, HasID i) => i a -> Errand (Row a)
-- | Update an existing row.
update :: (Table a, HasID i) => i a -> a -> Errand ()
-- | Delete a row from the table.
delete :: (Table a, HasID i) => i a -> Errand ()