Copyright | 2013 Kei Hibino |
---|---|
License | BSD3 |
Maintainer | ex8k.hibino@gmail.com |
Stability | experimental |
Portability | unknown |
Safe Haskell | None |
Language | Haskell2010 |
This is integrated module which contains types to represent table constraints and interfaces to bind between SQL database values and Haskell records.
Synopsis
- class HasKeyConstraint c a where
- keyConstraint :: KeyConstraint c a
- type PrimaryConstraint = KeyConstraint Primary
- type UniqueConstraint = KeyConstraint Unique
- data KeyConstraint c r
- class HasColumnConstraint c a where
- columnConstraint :: ColumnConstraint c a
- type PrimaryColumnConstraint = ColumnConstraint Primary
- type NotNullColumnConstraint = ColumnConstraint NotNull
- type UniqueColumnConstraint = ColumnConstraint Unique
- data Primary
- data NotNull
- data Unique
- data ColumnConstraint c r
- uniqueColumn :: PrimaryColumnConstraint r -> UniqueColumnConstraint r
- notNullColumn :: PrimaryColumnConstraint r -> NotNullColumnConstraint r
- derivedUniqueColumnConstraint :: HasColumnConstraint Primary r => UniqueColumnConstraint r
- derivedNotNullColumnConstraint :: HasColumnConstraint Primary r => NotNullColumnConstraint r
- deriveComposite :: ColumnConstraint c r -> KeyConstraint c r
- unique :: PrimaryConstraint r -> UniqueConstraint r
- derivedCompositePrimary :: HasColumnConstraint Primary r => PrimaryConstraint r
- derivedUniqueConstraint :: HasKeyConstraint Primary r => UniqueConstraint r
- class PersistableWidth a where
- class Eq q => PersistableType q where
- type PersistableRecordWidth a = ProductConst (Sum Int) a
- data PersistableSqlType q
- sqlNullValue :: PersistableType q => q
- derivedWidth :: PersistableWidth a => (PersistableRecordWidth a, Int)
- class FromSql q a where
- recordFromSql :: RecordFromSql q a
- data RecordFromSql q a
- takeRecord :: FromSql q a => [q] -> (a, [q])
- toRecord :: FromSql q a => [q] -> a
- valueRecordFromSql :: (q -> a) -> RecordFromSql q a
- class PersistableWidth a => ToSql q a where
- recordToSql :: RecordToSql q a
- data RecordToSql q a
- type ToSqlM q a = Writer (DList q) a
- putRecord :: ToSql q a => a -> ToSqlM q ()
- putEmpty :: () -> ToSqlM q ()
- fromRecord :: ToSql q a => a -> [q]
- valueRecordToSql :: (a -> q) -> RecordToSql q a
- updateValuesByUnique :: ToSql q ra => KeyConstraint Unique ra -> ra -> [q]
- updateValuesByPrimary :: (HasKeyConstraint Primary ra, ToSql q ra) => ra -> [q]
Concepts
On most drivers for SQL database, we need to write or read untyped SQL value sequence when accessing databases.
This library maps between list of untyped SQL type and Haskell record type using type classes.
Binding between SQL values and Haskell records
You will need to implement instances of FromSql
and ToSql
class
to bind between SQL database values and Haskell records.
You can use Database.Record.TH module in this package to generate instances from SQL database record column names and types.
Constraints used for RecordFromSql
inference
You will need to implement instances of
HasColumnConstraint
NotNull
which is a premise
to infer RecordFromSql
proof object using ToSql
q
(Maybe
a) instance.
This proof object cat convert from SQL type into Maybe
typed record
when dealing with outer joined query.
Modules which provide proof objects
Table constraint specified by keys
class HasKeyConstraint c a where Source #
Interface of inference rule for KeyConstraint
proof object.
keyConstraint :: KeyConstraint c a Source #
Infer ColumnConstraint
proof object.
type PrimaryConstraint = KeyConstraint Primary Source #
Specialized primary constraint.
type UniqueConstraint = KeyConstraint Unique Source #
Specialized unique constraint.
data KeyConstraint c r Source #
Proof object to specify table constraint
for table record type r
and constraint c
.
Constraint is specified by composite key.
class HasColumnConstraint c a where Source #
Interface of inference rule for ColumnConstraint
proof object.
columnConstraint :: ColumnConstraint c a Source #
Infer ColumnConstraint
proof object.
Instances
HasColumnConstraint NotNull Bool Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Char Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int8 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int16 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int32 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int64 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull String Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull a => HasColumnConstraint NotNull (a, b) Source # | Inference rule of |
Defined in Database.Record.KeyConstraint columnConstraint :: ColumnConstraint NotNull (a, b) Source # |
type PrimaryColumnConstraint = ColumnConstraint Primary Source #
Specialized primary constraint.
type NotNullColumnConstraint = ColumnConstraint NotNull Source #
Specialized not-null constraint.
type UniqueColumnConstraint = ColumnConstraint Unique Source #
Specialized unique constraint.
Constraint type. Not-null key.
Instances
HasColumnConstraint NotNull Bool Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Char Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int8 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int16 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int32 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull Int64 Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull String Source # | |
Defined in Database.Record.Instances | |
HasColumnConstraint NotNull a => HasColumnConstraint NotNull (a, b) Source # | Inference rule of |
Defined in Database.Record.KeyConstraint columnConstraint :: ColumnConstraint NotNull (a, b) Source # |
data ColumnConstraint c r Source #
Proof object to specify table constraint
for table record type r
and constraint c
specified by a single column.
uniqueColumn :: PrimaryColumnConstraint r -> UniqueColumnConstraint r Source #
Derivation rule for UniqueColumnConstraint
. Derive Unique from Primary.
notNullColumn :: PrimaryColumnConstraint r -> NotNullColumnConstraint r Source #
Derivation rule for NotNullColumnConstraint
. Derive NotNull from Primary.
derivedUniqueColumnConstraint :: HasColumnConstraint Primary r => UniqueColumnConstraint r Source #
Inferred UniqueColumnConstraint
proof object.
Record type r
has unique key which is derived r
has primary key.
derivedNotNullColumnConstraint :: HasColumnConstraint Primary r => NotNullColumnConstraint r Source #
Inferred NotNullColumnConstraint
proof object.
Record type r
has not-null key which is derived r
has primary key.
deriveComposite :: ColumnConstraint c r -> KeyConstraint c r Source #
Derivation rule for KeyConstraint
. Derive from ColumnConstraint
.
unique :: PrimaryConstraint r -> UniqueConstraint r Source #
Derivation rule for UniqueConstraint
.
derivedCompositePrimary :: HasColumnConstraint Primary r => PrimaryConstraint r Source #
Inferred PrimaryConstraint
proof object.
Record type r
has composite primary key which is derived r
has single column primary key.
derivedUniqueConstraint :: HasKeyConstraint Primary r => UniqueConstraint r Source #
Inferred UniqueConstraint
proof object.
Record type r
has unique key which is derived r
has primary key.
Convert between Haskell type and list of SQL type
class PersistableWidth a where Source #
PersistableWidth
a
is implicit rule to derive PersistableRecordWidth
a
width proposition for type a
.
Generic programming (https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#generic-programming)
with default signature is available for PersistableWidth
class,
so you can make instance like below:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) -- data Foo = Foo { ... } deriving Generic instance PersistableWidth Foo
Nothing
persistableWidth :: PersistableRecordWidth a Source #
persistableWidth :: (Generic a, GFieldWidthList (Rep a)) => PersistableRecordWidth a Source #
Instances
class Eq q => PersistableType q where Source #
Interface of derivation rule for PersistableSqlType
.
type PersistableRecordWidth a = ProductConst (Sum Int) a Source #
Proposition to specify width of Haskell type a
.
The width is length of database value list which is converted from Haskell type a
.
data PersistableSqlType q Source #
Proposition to specify type q
is database value type, contains null value
sqlNullValue :: PersistableType q => q Source #
Implicitly derived null value of database value type.
derivedWidth :: PersistableWidth a => (PersistableRecordWidth a, Int) Source #
Pass type parameter and inferred width value.
Convert from list of SQL type
class FromSql q a where Source #
FromSql
q
a
is implicit rule to derive RecordFromSql
q
a
record parser function against type a
.
Generic programming (https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#generic-programming)
with default signature is available for FromSql
class,
so you can make instance like below:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) import Database.HDBC (SqlValue) -- data Foo = Foo { ... } deriving Generic instance FromSql SqlValue Foo
Nothing
recordFromSql :: RecordFromSql q a Source #
RecordFromSql
q
a
record parser function.
recordFromSql :: (Generic a, GFromSql q (Rep a)) => RecordFromSql q a Source #
RecordFromSql
q
a
record parser function.
Instances
data RecordFromSql q a Source #
RecordFromSql
q
a
is data-type wrapping function
to convert from list of database value type (to receive from database) [q
] into Haskell type a
This structure is similar to parser.
While running RecordFromSql
behavior is the same as non-fail-able parser
which parse list of database value type [q
] stream.
So, RecordFromSql
q
is Monad
and Applicative
instance like parser monad.
When, you have data constructor and objects like below.
data MyRecord = MyRecord Foo Bar Baz
foo ::RecordFromSql
SqlValue Foo foo = ... bar ::RecordFromSql
SqlValue Bar bar = ... baz ::RecordFromSql
SqlValue Baz baz = ...
You can get composed RecordFromSql
like below.
myRecord :: RecordFromSql SqlValue MyRecord myRecord = MyRecord <$> foo <*> bar <*> baz
Instances
Monad (RecordFromSql q) Source # | |
Defined in Database.Record.FromSql (>>=) :: RecordFromSql q a -> (a -> RecordFromSql q b) -> RecordFromSql q b # (>>) :: RecordFromSql q a -> RecordFromSql q b -> RecordFromSql q b # return :: a -> RecordFromSql q a # fail :: String -> RecordFromSql q a # | |
Functor (RecordFromSql q) Source # | |
Defined in Database.Record.FromSql fmap :: (a -> b) -> RecordFromSql q a -> RecordFromSql q b # (<$) :: a -> RecordFromSql q b -> RecordFromSql q a # | |
Applicative (RecordFromSql q) Source # | Derived |
Defined in Database.Record.FromSql pure :: a -> RecordFromSql q a # (<*>) :: RecordFromSql q (a -> b) -> RecordFromSql q a -> RecordFromSql q b # liftA2 :: (a -> b -> c) -> RecordFromSql q a -> RecordFromSql q b -> RecordFromSql q c # (*>) :: RecordFromSql q a -> RecordFromSql q b -> RecordFromSql q b # (<*) :: RecordFromSql q a -> RecordFromSql q b -> RecordFromSql q a # |
takeRecord :: FromSql q a => [q] -> (a, [q]) Source #
Run implicit RecordFromSql
parser function object.
Convert from list of database value type [q
] into haskell type a
and rest of list [q
].
toRecord :: FromSql q a => [q] -> a Source #
Run implicit RecordFromSql
parser function object.
Convert from list of database value type [q
] into haskell type a
.
valueRecordFromSql :: (q -> a) -> RecordFromSql q a Source #
Derivation rule of RecordFromSql
parser function object for value convert function.
Convert into list of SQL type
class PersistableWidth a => ToSql q a where Source #
ToSql
q
a
is implicit rule to derive RecordToSql
q
a
record printer function for type a
.
Generic programming (https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#generic-programming)
with default signature is available for ToSql
class,
so you can make instance like below:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) import Database.HDBC (SqlValue) -- data Foo = Foo { ... } deriving Generic instance ToSql SqlValue Foo
To make instances of ToSql
manually,
ToSql
q
a
and RecordToSql
'q a
are composable with monadic context.
When, you have data constructor and objects like below.
data MyRecord = MyRecord Foo Bar Baz
instance ToSql SqlValue Foo where ... instance ToSql SqlValue Bar where ... instance ToSql SqlValue Baz where ...
You can get composed ToSql
implicit rule like below.
instance ToSql SqlValue MyRecord where recordToSql = recordToSql = wrapToSql $ \ (MyRecord x y z) -> do putRecord x putRecord y putRecord z
Nothing
recordToSql :: RecordToSql q a Source #
Derived RecordToSql
printer function object.
recordToSql :: (Generic a, GToSql q (Rep a)) => RecordToSql q a Source #
Derived RecordToSql
printer function object.
Instances
ToSql q () Source # | Implicit derivation rule of |
Defined in Database.Record.ToSql recordToSql :: RecordToSql q () Source # | |
(PersistableType q, ToSql q a) => ToSql q (Maybe a) Source # | Implicit derivation rule of |
Defined in Database.Record.ToSql recordToSql :: RecordToSql q (Maybe a) Source # | |
(ToSql q a, ToSql q b) => ToSql q (a, b) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b) Source # | |
(ToSql q a, ToSql q b, ToSql q c) => ToSql q (a, b, c) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b, c) Source # | |
(ToSql q a, ToSql q b, ToSql q c, ToSql q d) => ToSql q (a, b, c, d) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b, c, d) Source # | |
(ToSql q a, ToSql q b, ToSql q c, ToSql q d, ToSql q e) => ToSql q (a, b, c, d, e) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b, c, d, e) Source # | |
(ToSql q a, ToSql q b, ToSql q c, ToSql q d, ToSql q e, ToSql q f) => ToSql q (a, b, c, d, e, f) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b, c, d, e, f) Source # | |
(ToSql q a, ToSql q b, ToSql q c, ToSql q d, ToSql q e, ToSql q f, ToSql q g) => ToSql q (a, b, c, d, e, f, g) Source # | |
Defined in Database.Record.TupleInstances recordToSql :: RecordToSql q (a, b, c, d, e, f, g) Source # |
data RecordToSql q a Source #
RecordToSql
q
a
is data-type wrapping function
to convert from Haskell type a
into list of database value type (to send to database) [q
].
This structure is similar to printer.
While running RecordToSql
behavior is the same as list printer.
which appends list of database value type [q
] stream.
putRecord :: ToSql q a => a -> ToSqlM q () Source #
Run implicit RecordToSql
printer function object.
Context to convert haskell record type a
into lib of database value type [q
].
putEmpty :: () -> ToSqlM q () Source #
Run RecordToSql
empty printer.
fromRecord :: ToSql q a => a -> [q] Source #
Run implicit RecordToSql
printer function object.
Convert from haskell type a
into list of database value type [q
].
valueRecordToSql :: (a -> q) -> RecordToSql q a Source #
Derivation rule of RecordToSql
printer function object for value convert function.
:: ToSql q ra | |
=> KeyConstraint Unique ra | Unique key table constraint printer function object. |
-> ra | |
-> [q] |
Convert from Haskell type ra
into database value q
list expected by update form like
UPDATE table SET c0 = ?, c1 = ?, c2 = ? ... WHERE key0 = ? AND key1 = ? AND key2 = ? ...
using printer function object infered by ToSql ra q.
updateValuesByPrimary :: (HasKeyConstraint Primary ra, ToSql q ra) => ra -> [q] Source #
Convert like updateValuesByUnique'
using implicit RecordToSql
and ColumnConstraint
.