Safe Haskell | None |
---|---|
Language | Haskell98 |
A MySQL backend for persistent
.
- withMySQLPool :: (MonadIO m, MonadLogger m, MonadBaseControl IO m, IsSqlBackend backend) => MySQLConnectInfo -> Int -> (Pool backend -> m a) -> m a
- withMySQLConn :: (MonadBaseControl IO m, MonadIO m, MonadLogger m, IsSqlBackend backend) => MySQLConnectInfo -> (backend -> m a) -> m a
- createMySQLPool :: (MonadBaseControl IO m, MonadIO m, MonadLogger m, IsSqlBackend backend) => MySQLConnectInfo -> Int -> m (Pool backend)
- module Database.Persist.Sql
- data MySQLConnectInfo
- mkMySQLConnectInfo :: HostName -> ByteString -> ByteString -> ByteString -> MySQLConnectInfo
- setMySQLConnectInfoPort :: PortNumber -> MySQLConnectInfo -> MySQLConnectInfo
- setMySQLConnectInfoCharset :: Word8 -> MySQLConnectInfo -> MySQLConnectInfo
- data MySQLConf
- mkMySQLConf :: MySQLConnectInfo -> Int -> MySQLConf
- mockMigration :: Migration -> IO ()
- insertOnDuplicateKeyUpdate :: (PersistEntity record, MonadIO m) => record -> [Update record] -> SqlPersistT m ()
- insertManyOnDuplicateKeyUpdate :: (PersistEntity record, MonadIO m) => [record] -> [SomeField record] -> [Update record] -> SqlPersistT m ()
- data SomeField record where
- SomeField :: EntityField record typ -> SomeField record
- copyUnlessNull :: PersistField typ => EntityField record (Maybe typ) -> SomeField record
- copyUnlessEmpty :: (Monoid typ, PersistField typ) => EntityField record typ -> SomeField record
- copyUnlessEq :: PersistField typ => EntityField record typ -> typ -> SomeField record
- setMySQLConnectInfoTLS :: ClientParams -> MySQLConnectInfo -> MySQLConnectInfo
- data TrustedCAStore :: *
- makeClientParams :: TrustedCAStore -> IO ClientParams
- makeClientParams' :: FilePath -> [FilePath] -> FilePath -> TrustedCAStore -> IO ClientParams
Documentation
:: (MonadIO m, MonadLogger m, MonadBaseControl IO m, IsSqlBackend backend) | |
=> MySQLConnectInfo | Connection information. |
-> Int | Number of connections to be kept open in the pool. |
-> (Pool backend -> m a) | Action to be executed that uses the connection pool. |
-> m a |
Create a MySQL connection pool and run the given action.
The pool is properly released after the action finishes using
it. Note that you should not use the given ConnectionPool
outside the action since it may be already been released.
:: (MonadBaseControl IO m, MonadIO m, MonadLogger m, IsSqlBackend backend) | |
=> MySQLConnectInfo | Connection information. |
-> (backend -> m a) | Action to be executed that uses the connection. |
-> m a |
Same as withMySQLPool
, but instead of opening a pool
of connections, only one connection is opened.
:: (MonadBaseControl IO m, MonadIO m, MonadLogger m, IsSqlBackend backend) | |
=> MySQLConnectInfo | Connection information. |
-> Int | Number of connections to be kept open in the pool. |
-> m (Pool backend) |
Create a MySQL connection pool. Note that it's your
responsibility to properly close the connection pool when
unneeded. Use withMySQLPool
for automatic resource control.
module Database.Persist.Sql
:: HostName | hostname |
-> ByteString | username |
-> ByteString | password |
-> ByteString | database |
-> MySQLConnectInfo |
Public constructor for MySQLConnectInfo
.
setMySQLConnectInfoPort :: PortNumber -> MySQLConnectInfo -> MySQLConnectInfo Source #
Update port number for MySQLConnectInfo
.
setMySQLConnectInfoCharset Source #
:: Word8 | Numeric ID of collation. See https://dev.mysql.com/doc/refman/5.7/en/show-collation.html. |
-> MySQLConnectInfo | Reference connectInfo to perform update on |
-> MySQLConnectInfo |
Update character set for MySQLConnectInfo
.
Information required to connect to a MySQL database
using persistent
's generic facilities. These values are the
same that are given to withMySQLPool
.
:: MySQLConnectInfo | The connection information. |
-> Int | How many connections should be held on the connection pool. |
-> MySQLConf |
Public constructor for MySQLConf
.
mockMigration :: Migration -> IO () Source #
Mock a migration even when the database is not present. This function will mock the migration for a database even when the actual database isn't already present in the system.
insertOnDuplicateKeyUpdate :: (PersistEntity record, MonadIO m) => record -> [Update record] -> SqlPersistT m () Source #
MySQL specific upsert
. This will prevent multiple queries, when one will
do.
insertManyOnDuplicateKeyUpdate Source #
:: (PersistEntity record, MonadIO m) | |
=> [record] | A list of the records you want to insert, or update |
-> [SomeField record] | A list of updates to perform based on the record being inserted. |
-> [Update record] | A list of the updates to apply that aren't dependent on the record being inserted. |
-> SqlPersistT m () |
Do a bulk insert on the given records in the first parameter. In the event that a key conflicts with a record currently in the database, the second and third parameters determine what will happen.
The second parameter is a list of fields to copy from the original value. This allows you to specify which fields to copy from the record you're trying to insert into the database to the preexisting row.
The third parameter is a list of updates to perform that are independent of the value that is provided. You can use this to increment a counter value. These updates only occur if the original record is present in the database.
More details on SomeField
usage
The [
parameter allows you to specify which fields (and
under which conditions) will be copied from the inserted rows. For
a brief example, consider the following data model and existing data set:SomeField
]
Item name Text description Text price Double Maybe quantity Int Maybe Primary name
items: +------+-------------+-------+----------+ | name | description | price | quantity | +------+-------------+-------+----------+ | foo | very good | | 3 | | bar | | 3.99 | | +------+-------------+-------+----------+
This record type has a single natural key on itemName
. Let's suppose
that we download a CSV of new items to store into the database. Here's
our CSV:
name,description,price,quantity foo,,2.50,6 bar,even better,,5 yes,wow,,
We parse that into a list of Haskell records:
records = [ Item { itemName = "foo", itemDescription = "" , itemPrice = Just 2.50, itemQuantity = Just 6 } , Item "bar" "even better" Nothing (Just 5) , Item "yes" "wow" Nothing Nothing ]
The new CSV data is partial. It only includes updates from the
upstream vendor. Our CSV library parses the missing description field as
an empty string. We don't want to override the existing description. So
we can use the copyUnlessEmpty
function to say: "Don't update when the
value is empty."
Likewise, the new row for bar
includes a quantity, but no price. We do
not want to overwrite the existing price in the database with a NULL
value. So we can use copyUnlessNull
to only copy the existing values
in.
The final code looks like this:
insertManyOnDuplicateKeyUpdate
records
[ copyUnlessEmpty
ItemDescription
, copyUnlessNull
ItemPrice
, copyUnlessNull
ItemQuantity
]
[]
Once we run that code on the datahase, the new data set looks like this:
items: +------+-------------+-------+----------+ | name | description | price | quantity | +------+-------------+-------+----------+ | foo | very good | 2.50 | 6 | | bar | even better | 3.99 | 5 | | yes | wow | | | +------+-------------+-------+----------+
data SomeField record where Source #
This type is used to determine how to update rows using MySQL's
INSERT ON DUPLICATE KEY UPDATE
functionality, exposed via
insertManyOnDuplicateKeyUpdate
in the library.
SomeField :: EntityField record typ -> SomeField record | Copy the field directly from the record. |
copyUnlessNull :: PersistField typ => EntityField record (Maybe typ) -> SomeField record Source #
Copy the field into the database only if the value in the
corresponding record is non-NULL
.
@since 2.6.2
copyUnlessEmpty :: (Monoid typ, PersistField typ) => EntityField record typ -> SomeField record Source #
Copy the field into the database only if the value in the
corresponding record is non-empty, where "empty" means the Monoid
definition for mempty
. Useful for Text
, String
, ByteString
, etc.
The resulting SomeField
type is useful for the
insertManyOnDuplicateKeyUpdate
function.
@since 2.6.2
copyUnlessEq :: PersistField typ => EntityField record typ -> typ -> SomeField record Source #
Copy the field into the database only if the field is not equal to the provided value. This is useful to avoid copying weird nullary data into the database.
The resulting SomeField
type is useful for the
insertManyOnDuplicateKeyUpdate
function.
@since 2.6.2
TLS configuration
setMySQLConnectInfoTLS Source #
:: ClientParams |
|
-> MySQLConnectInfo | Reference connectInfo to perform update on |
-> MySQLConnectInfo |
Set TLS ClientParams for MySQLConnectInfo
.
data TrustedCAStore :: * #
The whole point of TLS is that: a peer should have already trusted
some certificates, which can be used for validating other peer's certificates.
if the certificates sent by other side form a chain. and one of them is issued
by one of TrustedCAStore
, Then the peer will be trusted.
SystemCAStore | provided by your operating system. |
MozillaCAStore | provided by Mozilla. |
CustomCAStore FilePath | provided by your self, the CA file can contain multiple certificates. |
:: TrustedCAStore | trusted certificates. |
-> IO ClientParams |
make a simple tls ClientParams
that will validate server and use tls connection
without providing client's own certificate. suitable for connecting server which don't
validate clients.
we defer setting of clientServerIdentification
to connecting phase.
Note, tls's default validating method require server has v3 certificate.
you can use openssl's V3 extension to issue such a certificate. or change ClientParams
before connecting.
:: FilePath | public certificate (X.509 format). |
-> [FilePath] | chain certificates (X.509 format). the root of your certificate chain should be already trusted by server, or tls will fail. |
-> FilePath | private key associated. |
-> TrustedCAStore | trusted certificates. |
-> IO ClientParams |
make a simple tls ClientParams
that will validate server and use tls connection
while providing client's own certificate as well. suitable for connecting server which
validate clients.
Also only accept v3 certificate.