query package:mysql-simple

Perform a SELECT or other SQL query that is expected to return results. All results are retrieved and converted before this function returns. When processing large results, this function will consume a lot of client-side memory. Consider using fold instead. Exceptions that may be thrown:
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 ?"
The underlying type is a ByteString, and literal Haskell strings that contain Unicode characters will be correctly transformed to UTF-8.
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 ?"
The underlying type is a ByteString, and literal Haskell strings that contain Unicode characters will be correctly transformed to UTF-8.
A version of query that does not perform query substitution.
Exception thrown if query is used to perform an INSERT-like operation, or execute is used to perform a SELECT-like operation.
The QueryParams typeclass, for rendering a collection of parameters to a SQL query. Predefined instances are provided for tuples containing up to ten elements.
A collection type that can be turned into a list of rendering Actions. Instances should use the render method of the Param class to perform conversion of each element of the collection.
The QueryResults typeclass, for converting a row of results returned by a SQL query into a more useful Haskell representation. Predefined instances are provided for tuples containing up to ten elements.
A collection type that can be converted from a list of strings. Instances should use the convert method of the Result class to perform conversion of each element of the collection. This example instance demonstrates how to convert a two-column row into a Haskell pair. Each field in the metadata is paired up with each value from the row, and the two are passed to convert.
instance (Result a, Result b) => QueryResults (a,b) where
convertResults [fa,fb] [va,vb] = (a,b)
where !a = convert fa va
!b = convert fb vb
convertResults fs vs  = convertError fs vs 2
Notice that this instance evaluates each element to WHNF before constructing the pair. By doing this, we guarantee two important properties:
  • Keep resource usage under control by preventing the construction of potentially long-lived thunks.
  • Ensure that any ResultError that might arise is thrown immediately, rather than some place later in application code that cannot handle it.
You can also declare Haskell types of your own to be instances of QueryResults.
data User = User { firstName :: String, lastName :: String }

instance QueryResults User where
convertResults [fa,fb] [va,vb] = User $ a * b
where !a = convert fa va
!b = convert fb vb
convertResults fs vs  = convertError fs vs 2

Format a query string. This function is exposed to help with debugging and logging. Do not use it to prepare queries for execution. 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.
Split a query into fragments separated by ? characters. Does not break a fragment if the question mark is in a string literal.