Portability | portable |
---|---|
Stability | experimental |
Maintainer | Bryan O'Sullivan <bos@mailrank.com> |
A mid-level client library for the MySQL database, aimed at ease of use and high performance.
- data ConnectInfo = ConnectInfo {}
- data Connection
- data Query
- newtype In a = In a
- newtype Only a = Only {
- fromOnly :: a
- data FormatError
- data QueryError
- data ResultError
- connect :: ConnectInfo -> IO Connection
- defaultConnectInfo :: ConnectInfo
- close :: Connection -> IO ()
- query :: (QueryParams q, QueryResults r) => Connection -> Query -> q -> IO [r]
- query_ :: QueryResults r => Connection -> Query -> IO [r]
- execute :: QueryParams q => Connection -> Query -> q -> IO Int64
- execute_ :: Connection -> Query -> IO Int64
- executeMany :: QueryParams q => Connection -> Query -> [q] -> IO Int64
- insertID :: Connection -> IO Word64
- withTransaction :: Connection -> IO a -> IO a
- autocommit :: Connection -> Bool -> IO ()
- commit :: Connection -> IO ()
- rollback :: Connection -> IO ()
- formatMany :: QueryParams q => Connection -> Query -> [q] -> IO ByteString
- formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteString
Types
data ConnectInfo
ConnectInfo | |
|
data Connection
Connection to a MySQL database.
A query string. This type is intended to make it difficult to construct a SQL query by concatenating string fragments, as that is an extremely common way to accidentally introduce SQL injection vulnerabilities into an application.
This type is an instance of IsString
, so the easiest way to
construct a query is to enable the OverloadedStrings
language
extension and then simply write the query in double quotes.
{-# LANGUAGE OverloadedStrings #-} import Database.MySQL.Simple q :: Query q = "select ?"
Wrap a list of values for use in an IN
clause. Replaces a
single "?
" character with a parenthesized list of rendered
values.
Example:
query "select * from whatever where id in ?" (In [3,4,5])
In a |
A single-value collection.
This can be handy if you need to supply a single parameter to a SQL query.
Example:
query "select x from scores where x > ?" (Only
(42::Int))
Exceptions
data FormatError Source
Exception thrown if a Query
could not be formatted correctly.
This may occur if the number of '?
' characters in the query
string does not match the number of parameters provided.
data QueryError Source
data ResultError Source
Exception thrown if conversion from a SQL value to a Haskell value fails.
Connection management
connect :: ConnectInfo -> IO Connection
Connect to a database.
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" }
close :: Connection -> IO ()
Close a connection, and mark any outstanding Result
as
invalid.
Queries that return results
query :: (QueryParams q, QueryResults r) => Connection -> Query -> q -> IO [r]Source
Perform a SELECT
or other SQL query that is expected to return
results.
All results are retrieved and converted before this function returns.
Exceptions that may be thrown:
-
FormatError
: the query string could not be formatted correctly. -
QueryError
: the result contains no columns (i.e. you should be usingexecute
instead ofquery
). -
ResultError
: result conversion failed.
query_ :: QueryResults r => Connection -> Query -> IO [r]Source
A version of query
that does not perform query substitution.
Statements that do not return results
execute :: QueryParams q => Connection -> Query -> q -> IO Int64Source
Execute an INSERT
, UPDATE
, or other SQL query that is not
expected to return results.
Returns the number of rows affected.
Throws FormatError
if the query could not be formatted correctly.
execute_ :: Connection -> Query -> IO Int64Source
A version of execute
that does not perform query substitution.
executeMany :: QueryParams q => Connection -> Query -> [q] -> IO Int64Source
Execute a multi-row INSERT
, UPDATE
, or other SQL query that is not
expected to return results.
Returns the number of rows affected.
Throws FormatError
if the query could not be formatted correctly.
insertID :: Connection -> IO Word64
Return the value generated for an AUTO_INCREMENT
column by the
previous INSERT
or UPDATE
statement.
See http://dev.mysql.com/doc/refman/5.5/en/mysql-insert-id.html
Transaction handling
withTransaction :: Connection -> IO a -> IO aSource
Execute an action inside a SQL transaction.
You are assumed to have started the transaction yourself.
If your action succeeds, the transaction will be commit
ted
before this function returns.
If your action throws any exception (not just a SQL exception), the
transaction will be rolled back rollback
before the
exception is propagated.
autocommit :: Connection -> Bool -> IO ()
Turn autocommit on or off.
By default, MySQL runs with autocommit mode enabled. In this mode, as soon as you modify a table, MySQL stores your modification permanently.
commit :: Connection -> IO ()
Commit the current transaction.
rollback :: Connection -> IO ()
Roll back the current transaction.
Helper functions
formatMany :: QueryParams q => Connection -> Query -> [q] -> IO ByteStringSource
Format a query string with a variable number of rows.
The query string must contain exactly one substitution group,
identified by the SQL keyword "VALUES
" (case insensitive)
followed by an "(
" character, a series of one or more "?
"
characters separated by commas, and a ")
" character. White
space in a substitution group is permitted.
Throws FormatError
if the query string could not be formatted
correctly.
formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteStringSource
Format a query string.
String parameters are escaped according to the character set in use
on the Connection
.
Throws FormatError
if the query string could not be formatted
correctly.