| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hasql.Query
Contents
- data Query a b = Query !ByteString !(Params a) !(Result b) !Bool
- data ResultsError
- data ResultError
- data RowError
- run :: Query a b -> a -> Connection -> IO (Either ResultsError b)
Documentation
A specification of a strictly single-statement query, which can be parameterized and prepared.
Consists of the following:
- SQL template,
- params encoder,
- result decoder,
- a flag, determining whether it should be prepared.
The SQL template must be formatted according to Postgres' standard,
with any non-ASCII characters of the template must be encoded using UTF-8.
According to the format,
parameters must be referred to using the positional notation, as in the following:
$1, $2, $3 and etc.
Those references must be used to refer to the values of the Params encoder.
Following is an example of the declaration of a prepared statement with its associated codecs.
selectSum :: Hasql.Query(Int64, Int64) Int64 selectSum = Hasql.Querysql encoder decoder True where sql = "select ($1 + $2)" encoder =contramapfst(Hasql.Encoding.valueHasql.Encoding.int8)<>contramapsnd(Hasql.Encoding.valueHasql.Encoding.int8) decoder = Hasql.Decoding.singleRow(Hasql.Decoding.valueHasql.Decoding.int8)
The statement above accepts a product of two parameters of type Int64
and results in a single result of type Int64.
Constructors
| Query !ByteString !(Params a) !(Result b) !Bool |
Execution
data ResultsError Source
An error of the result-decoder.
Constructors
| ClientError !(Maybe ByteString) | An error on the client-side, with a message generated by the "libpq" library. Usually indicates problems with connection. |
| ResultError !ResultError | Decoder error details. |
Instances
data ResultError Source
Decoder error details.
Constructors
| ServerError !ByteString !ByteString !(Maybe ByteString) !(Maybe ByteString) | An error reported by the DB. Consists of the following: Code, message, details, hint.
|
| UnexpectedResult !Text | The database returned an unexpected result. Indicates an improper statement or a schema mismatch. |
| RowError !Int !RowError | An error of the row reader, preceded by the index of the row. |
| UnexpectedAmountOfRows !Int | An unexpected amount of rows. |
Instances
An error during the decoding of a specific row.
Constructors
| EndOfInput | Appears on the attempt to parse more columns than there are in the result. |
| UnexpectedNull | Appears on the attempt to parse a |
| ValueError !Text | Appears when a wrong value parser is used. Comes with the error details. |
run :: Query a b -> a -> Connection -> IO (Either ResultsError b) Source
Execute the query, producing either a deserialization failure or a successful result.