| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hpgsql.Query
Contents
Synopsis
- data Query
- data SingleQuery
- sql :: QuasiQuoter
- sqlPrep :: QuasiQuoter
- mkQuery :: ToPgRow a => ByteString -> a -> Query
- escapeIdentifier :: ByteString -> Query
- vALUES :: ToPgRow a => [a] -> Query
- preparedStatement :: Query -> Query
- nonPreparedStatement :: Query -> Query
- breakQueryIntoStatements :: Query -> NonEmpty SingleQuery
- mkQueryInternal :: ByteString -> [[Either ByteString (EncodingContext -> (Maybe Oid, BinaryField))]] -> Query
- encodeParam :: ToPgField a => a -> EncodingContext -> (Maybe Oid, BinaryField)
Documentation
Query is some SQL with values of query arguments inside and also whether it's a prepared statement.
It can be concatenated to other Query objects freely, and concatenating chains of Queries with
at least one prepared statement among them makes the final Query object a prepared statement, too.
You can build this with the sql and sqlPrep quasiquoters, the former for unprepared statements,
and you can use preparedStatement and nonPreparedStatement to convert between them.
data SingleQuery Source #
A single statement, not multiple, with dollar-numbered query arguments starting from $1.
Instances
| Show SingleQuery Source # | |
Defined in Hpgsql.InternalTypes Methods showsPrec :: Int -> SingleQuery -> ShowS # show :: SingleQuery -> String # showList :: [SingleQuery] -> ShowS # | |
sql :: QuasiQuoter Source #
sqlPrep :: QuasiQuoter Source #
Just like the sql quasiquoter, but every SQL statement inside is
a prepared statement.
mkQuery :: ToPgRow a => ByteString -> a -> Query Source #
Takes in a query string with query arguments as dollar-numbered arguments and returns a Query you
can run.
Examples:
- "SELECT * FROM table WHERE col1=$1 AND col2=$2"
- "SELECT * FROM table WHERE (["a", "b", "c"]'::jsonb ? b) = $1"
Question marks are interpreted literally, i.e. they have no special meaning.
Note that if the number of arguments does not match what's in the query string,
this will throw an error.
escapeIdentifier :: ByteString -> Query Source #
Escapes a database object identifier like a table name or a column name,
so it can be embedded inside a [sql|...|] quasiquote using ^{expr} syntax:
[sql| SELECT ^{escapeIdentifier "çolumñ"} FROM ^{escapeIdentifier "sChEmA"}.some_table |]vALUES :: ToPgRow a => [a] -> Query Source #
Generates a query like VALUES ($1,$2), ($3,$4) from a list of rows.
Can be embedded inside a [sql|...|] quasiquote using ^{expr} syntax:
[sql| INSERT INTO emp(id,name) ^{vALUES rows} ON CONFLICT DO NOTHING |]preparedStatement :: Query -> Query Source #
nonPreparedStatement :: Query -> Query Source #
Internal
These functions are internal implementation details and are not intended for public use. They may change or be removed without notice.
breakQueryIntoStatements :: Query -> NonEmpty SingleQuery Source #
For internal usage only. Takes a Query and breaks it up into
individual SQL statements that can be sent to a postgres backend.
mkQueryInternal :: ByteString -> [[Either ByteString (EncodingContext -> (Maybe Oid, BinaryField))]] -> Query Source #
Takes in a query string with query arguments as question marks (NOT dollar-numbered arguments).
Example:
- "SELECT * FROM table WHERE col1=? AND col2=?"
You should really use mkQuery instead of this. This is here only for hpgsql-simple-compat.
encodeParam :: ToPgField a => a -> EncodingContext -> (Maybe Oid, BinaryField) Source #