Safe Haskell | None |
---|---|
Language | Haskell2010 |
Database.MySQL.JSONTable
Description
Interface to use a MySQL table with a very specific format, where each row consists of a row identifier - used for lookups - and a JSON-encoded value.
id | data |
---|---|
Row identifier (type Id ) | JSON-encoded value |
Synopsis
- data Id a
- data Row a = Row {}
- data JSONTable a = JSONTable {}
- data ConnectInfo = ConnectInfo {}
- defaultConnectInfo :: ConnectInfo
- data Connection
- withSQL :: (MonadMask m, MonadIO m) => ConnectInfo -> (Connection -> m a) -> m a
- createTable :: Connection -> Bool -> String -> IO (JSONTable a)
- deleteTable :: Connection -> Bool -> JSONTable a -> IO ()
- insert :: ToJSON a => Connection -> JSONTable a -> a -> IO (Id a)
- lookup :: (Typeable a, FromJSON a) => Connection -> JSONTable a -> Id a -> IO (Maybe a)
- adjust :: (Typeable a, FromJSON a, ToJSON a) => Connection -> JSONTable a -> (a -> IO a) -> Id a -> IO ()
- delete :: Connection -> JSONTable a -> Id a -> IO ()
- replace :: ToJSON a => Connection -> JSONTable a -> Id a -> a -> IO ()
- sourceRows :: (Typeable a, FromJSON a) => Connection -> JSONTable a -> ConduitT i (Row a) (ResourceT IO) ()
- data IdTable key a = IdTable {}
- createIdTable :: FromField key => Connection -> Bool -> String -> IO (IdTable key a)
- deleteIdTable :: Connection -> Bool -> IdTable key a -> IO ()
- insertId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO ()
- lookupId :: (ToField key, Typeable a) => Connection -> IdTable key a -> key -> IO (Maybe (Id a))
- adjustId :: (ToField key, Typeable a) => Connection -> IdTable key a -> (Id a -> IO (Id a)) -> key -> IO ()
- alterId :: (ToField key, Typeable a) => Connection -> IdTable key a -> (Maybe (Id a) -> IO (Maybe (Id a))) -> key -> IO ()
- deleteId :: ToField key => Connection -> IdTable key a -> key -> IO ()
- replaceId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO ()
- moveId :: (ToField key, Typeable a) => Connection -> IdTable key a -> key -> key -> IO ()
- sourceIds :: (Typeable key, FromField key, Typeable a) => Connection -> IdTable key a -> ConduitT i (key, Id a) (ResourceT IO) ()
JSON tables
Types
Row identifier used for table lookups. The type parameter indicates the type of data stored in the table.
Instances
A single row.
A MySQL table with two columns:
id | data |
---|---|
Row identifier (type Id ) | JSON data |
The type parameter indicates the type of data stored in the table.
Connections
data ConnectInfo #
Constructors
ConnectInfo | |
Fields
|
Instances
Read ConnectInfo | |
Defined in Database.MySQL.Base Methods readsPrec :: Int -> ReadS ConnectInfo # readList :: ReadS [ConnectInfo] # readPrec :: ReadPrec ConnectInfo # readListPrec :: ReadPrec [ConnectInfo] # | |
Show ConnectInfo | |
Defined in Database.MySQL.Base Methods showsPrec :: Int -> ConnectInfo -> ShowS # show :: ConnectInfo -> String # showList :: [ConnectInfo] -> ShowS # | |
Eq ConnectInfo | |
Defined in Database.MySQL.Base |
defaultConnectInfo :: ConnectInfo #
Default information for setting up a connection.
Defaults are as follows:
- Server on
localhost
- User
root
- No password
- Database
test
- Character set
utf8
Use as in the following example:
connect defaultConnectInfo { connectHost = "db.example.com" }
data Connection #
Connection to a MySQL database.
withSQL :: (MonadMask m, MonadIO m) => ConnectInfo -> (Connection -> m a) -> m a Source #
Open a connection to a MySQL server and apply a function to it. The connection is closed both when the function completes or throws an exception.
Table operations
Arguments
:: Connection | MySQL database connection. |
-> Bool | Fail if table already exists. |
-> String | Table name. |
-> IO (JSONTable a) |
Create a new JSON table in a MySQL database.
Arguments
:: Connection | MySQL database connection. |
-> Bool | Fail if table doesn't exist. |
-> JSONTable a | |
-> IO () |
Delete a JSON table from a MySQL database, together with all of its content.
Row operations
Arguments
:: ToJSON a | |
=> Connection | MySQL database connection. |
-> JSONTable a | Table to insert the new row. |
-> a | Data for the new row. |
-> IO (Id a) | Identifier of the new row. |
Insert a new row into a table.
Warning: It is recommended not to call insert
with the same Connection
argument from multiple threads. The Id
s returned might get mixed up.
If you need to call insert
from multiple threads, use a different
Connection
on each thread.
Arguments
:: (Typeable a, FromJSON a) | |
=> Connection | MySQL database connection. |
-> JSONTable a | Table for lookup. |
-> Id a | Identifier to use for the table lookup. |
-> IO (Maybe a) |
Lookup a row in a table.
Arguments
:: (Typeable a, FromJSON a, ToJSON a) | |
=> Connection | MySQL database connection. |
-> JSONTable a | |
-> (a -> IO a) | Update function. |
-> Id a | |
-> IO () |
Update a row by applying the supplied function. If the row doesn't exist, it does nothing.
Arguments
:: Connection | MySQL database connection. |
-> JSONTable a | Table to delete the row from. |
-> Id a | Identifier of the row to delete. |
-> IO () |
Delete a row from a table. It does nothing if the row doesn't exist.
Arguments
:: ToJSON a | |
=> Connection | MySQL database connection. |
-> JSONTable a | |
-> Id a | Row identifier. |
-> a | New value. |
-> IO () |
Replace the current value of a row. It does nothing if the row doesn't exist.
Streaming
Arguments
:: (Typeable a, FromJSON a) | |
=> Connection | MySQL database connection. |
-> JSONTable a | Table to stream rows from. |
-> ConduitT i (Row a) (ResourceT IO) () |
Stream all rows using a conduit.
Id tables
Types
Table that stores a map from keys to row identifiers from
some JSONTable
. It has the following shape:
key | id |
---|---|
User-provided key type | Row identifier (type Id ) |
Constructors
IdTable | |
Fields
|
Table operations
Arguments
:: FromField key | |
=> Connection | MySQL database connection. |
-> Bool | Fail if table already exists. |
-> String | Table name. |
-> IO (IdTable key a) |
Create a new Id table in a MySQL database.
The type of the key
column will be set to the first type listed in
fromField
.
Arguments
:: Connection | MySQL database connection. |
-> Bool | Fail if table doesn't exist. |
-> IdTable key a | |
-> IO () |
Delete an Id table from a MySQL database, together with all of its content.
Row operations
insertId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO () Source #
Insert a new Id into an Id table.
lookupId :: (ToField key, Typeable a) => Connection -> IdTable key a -> key -> IO (Maybe (Id a)) Source #
Id table lookup.
Arguments
:: (ToField key, Typeable a) | |
=> Connection | MySQL database connection. |
-> IdTable key a | |
-> (Id a -> IO (Id a)) | Update function. |
-> key | |
-> IO () |
Update an Id
by applying the supplied function. If the key is not found,
it does nothing.
Arguments
:: (ToField key, Typeable a) | |
=> Connection | MySQL database connection. |
-> IdTable key a | |
-> (Maybe (Id a) -> IO (Maybe (Id a))) | Update function. |
-> key | |
-> IO () |
Alter an Id
by applying the supplied function, either inserting it, removing
it, or updating it.
deleteId :: ToField key => Connection -> IdTable key a -> key -> IO () Source #
Delete an Id from and Id table. It does nothing if the key is not found.
replaceId :: ToField key => Connection -> IdTable key a -> key -> Id a -> IO () Source #
Replace the Id
associated to the given key. It does nothing if the key
isn't found.
Arguments
:: (ToField key, Typeable a) | |
=> Connection | |
-> IdTable key a | |
-> key | Original key |
-> key | New key |
-> IO () |
Move an Id from one key to another. This fails if the original key doesn't exist or the target key already exists.