Safe Haskell | None |
---|---|
Language | Haskell98 |
Database.PostgreSQL.Typed.Query
- class PGQuery q a | q -> a where
- pgRunQuery :: PGConnection -> q -> IO (Int, [a])
- unsafeModifyQuery :: q -> (ByteString -> ByteString) -> q
- type PGSimpleQuery = QueryParser SimpleQuery
- type PGPreparedQuery = QueryParser PreparedQuery
- rawPGSimpleQuery :: ByteString -> PGSimpleQuery PGValues
- rawPGPreparedQuery :: ByteString -> PGValues -> PGPreparedQuery PGValues
- data QueryFlags = QueryFlags {
- flagQuery :: Bool
- flagNullable :: Maybe Bool
- flagPrepare :: Maybe [String]
- simpleQueryFlags :: QueryFlags
- parseQueryFlags :: String -> (QueryFlags, String)
- makePGQuery :: QueryFlags -> String -> ExpQ
- pgSQL :: QuasiQuoter
- pgExecute :: PGQuery q () => PGConnection -> q -> IO Int
- pgQuery :: PGQuery q a => PGConnection -> q -> IO [a]
- pgLazyQuery :: PGConnection -> PGPreparedQuery a -> Word32 -> IO [a]
Documentation
class PGQuery q a | q -> a where Source
Methods
pgRunQuery :: PGConnection -> q -> IO (Int, [a]) Source
Execute a query and return the number of rows affected (or -1 if not known) and a list of results.
unsafeModifyQuery :: q -> (ByteString -> ByteString) -> q Source
Change the raw SQL query stored within this query.
This is unsafe because the query has already been type-checked, so any change must not change the number or type of results or placeholders (so adding additional static WHERE or ORDER BY clauses is generally safe).
This is useful in cases where you need to construct some part of the query dynamically, but still want to infer the result types.
If you want to add dynamic values to the query, it's best to use pgSafeLiteral
.
For example:
[pgSQL|SELECT a FROM t|] `unsafeModifyQuery` (<> (" WHERE a = " <> pgSafeLiteral x))
Instances
type PGSimpleQuery = QueryParser SimpleQuery Source
A simple one-shot query that simply substitutes literal representations of parameters for placeholders.
type PGPreparedQuery = QueryParser PreparedQuery Source
A prepared query that automatically is prepared in the database the first time it is run and bound with new parameters each subsequent time.
rawPGSimpleQuery :: ByteString -> PGSimpleQuery PGValues Source
Make a simple query directly from a query string, with no type inference
rawPGPreparedQuery :: ByteString -> PGValues -> PGPreparedQuery PGValues Source
Make a prepared query directly from a query string and bind parameters, with no type inference
data QueryFlags Source
Flags affecting how and what type of query to build with makePGQuery
.
Constructors
QueryFlags | |
Fields
|
simpleQueryFlags :: QueryFlags Source
QueryFlags
for a default (simple) query.
parseQueryFlags :: String -> (QueryFlags, String) Source
Parse flags off the beginning of a query string, returning the flags and the remaining string.
makePGQuery :: QueryFlags -> String -> ExpQ Source
Construct a PGQuery
from a SQL string.
A quasi-quoter for PGSQL queries.
Used in expression context, it may contain any SQL statement [pgSQL|SELECT ...|]
.
The statement may contain PostgreSQL-style placeholders ($1
, $2
, ...) or in-line placeholders (${1+1}
) containing any valid Haskell expression (except {}
).
It will be replaced by a PGQuery
object that can be used to perform the SQL statement.
If there are more $N
placeholders than expressions, it will instead be a function accepting the additional parameters and returning a PGQuery
.
Note that all occurrences of $N
or ${
will be treated as placeholders, regardless of their context in the SQL (e.g., even within SQL strings or other places placeholders are disallowed by PostgreSQL), which may cause invalid SQL or other errors.
If you need to pass a literal $
through in these contexts, you may double it to escape it as $$N
or $${
.
The statement may start with one of more special flags affecting the interpretation:
?
- To disable nullability inference, treating all result values as nullable, thus returning
Maybe
values regardless of inferred nullability. This makes unexpected NULL errors impossible. !
- To disable nullability inference, treating all result values as not nullable, thus only returning
Maybe
where requested. This is makes unexpected NULL errors more likely. $
- To create a
PGPreparedQuery
(using placeholder parameters) rather than the defaultPGSimpleQuery
(using literal substitution). $(type,...)
- To specify specific types for a prepared query (see http://www.postgresql.org/docs/current/static/sql-prepare.html for details), rather than inferring parameter types by default.
#
- Only do literal
${}
substitution usingpgSubstituteLiterals
and return a string, not a query.
pgSQL
can also be used at the top-level to execute SQL statements at compile-time (without any parameters and ignoring results).
Here the query can only be prefixed with !
to make errors non-fatal.
pgExecute :: PGQuery q () => PGConnection -> q -> IO Int Source
Execute a query that does not return results. Return the number of rows affected (or -1 if not known).
pgQuery :: PGQuery q a => PGConnection -> q -> IO [a] Source
Run a query and return a list of row results.
Arguments
:: PGConnection | |
-> PGPreparedQuery a | |
-> Word32 | Chunk size (1 is common, 0 is all-or-nothing) |
-> IO [a] |
Run a prepared query in lazy mode, where only chunk size rows are requested at a time.
If you eventually retrieve all the rows this way, it will be far less efficient than using pgQuery
, since every chunk requires an additional round-trip.
Although you may safely stop consuming rows early, currently you may not interleave any other database operation while reading rows. (This limitation could theoretically be lifted if required.)