Skip to content

Commit

Permalink
Rearrange documentation added in #717
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-rouse committed Nov 25, 2017
1 parent 0f1f552 commit 900d4d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion persistent/Database/Persist/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Database.Persist.Sql
, rawQueryRes
, rawExecute
, rawExecuteCount
, rawSql
, sqlQQ
, executeQQ
, rawSql
, deleteWhereCount
, updateWhereCount
, transactionSave
Expand Down
74 changes: 45 additions & 29 deletions persistent/Database/Persist/Sql/Raw/QQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,14 @@ Module: module Database.Persist.Sql.Raw.QQ
Description: QuasiQuoters for performing raw sql queries
This module exports convenient QuasiQuoters to perform raw SQL queries.
All QuasiQuoters follow them same pattern and are analogous to the similar named
All QuasiQuoters follow the same pattern and are analogous to the similar named
functions exported from 'Database.Persist.Sql.Raw'. Neither the quoted
function's behaviour, nor it's return value is altered during the translation
and all documentation provided with it holds.
The QuasiQuoters in this module perform a simple substitution on the query text,
that allows value substitutions, table name substitutions as well as column name
substitutions.
Here is a small example:
Given the following simple model:
@
Category
rgt Int
lft Int
@
We can now execute this raw query:
@
let lft = 10 :: Int
rgt = 20 :: Int
width = rgt - lft
in [sqlQQ|
DELETE FROM ^{Category} WHERE @{CategoryLft} BETWEEN #{lft} AND #{rgt};
UPDATE category SET @{CategoryRgt} = @{CategoryRgt} - #{width} WHERE @{CategoryRgt} > #{rgt};
UPDATE category SET @{CategoryLft} = @{CategoryLft} - #{width} WHERE @{CategoryLft} > #{rgt};
|]
@
@^{TableName}@ looks up the table's name and escapes it, @\@{ColumnName}@ looks
up the column's name and properly escapes it and @#{value}@ inserts the value
via the usual parameter substitution mechanism.
-}

{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -154,27 +127,70 @@ makeQQ x = QuasiQuoter
(error "Cannot use qc as a type")
(error "Cannot use qc as a dec")

-- | Analoguous to 'Database.Persist.Sql.Raw.rawSql'
-- | QuasiQuoter for performing raw sql queries, analoguous to
-- 'Database.Persist.Sql.Raw.rawSql'
--
-- This and the following are convenient QuasiQuoters to perform raw SQL
-- queries. They each follow the same pattern and are analogous to
-- the similarly named @raw@ functions. Neither the quoted function's
-- behaviour, nor it's return value is altered during the translation and
-- all documentation provided with it holds.
--
-- These QuasiQuoters perform a simple substitution on the query text, that
-- allows value substitutions, table name substitutions as well as column name
-- substitutions.
--
-- Here is a small example:
--
-- Given the following simple model:
--
-- @
-- Category
-- rgt Int
-- lft Int
-- @
--
-- We can now execute this raw query:
--
-- @
-- let lft = 10 :: Int
-- rgt = 20 :: Int
-- width = rgt - lft
-- in [sqlQQ|
-- DELETE FROM ^{Category} WHERE @{CategoryLft} BETWEEN #{lft} AND #{rgt};
-- UPDATE category SET @{CategoryRgt} = @{CategoryRgt} - #{width} WHERE @{CategoryRgt} > #{rgt};
-- UPDATE category SET @{CategoryLft} = @{CategoryLft} - #{width} WHERE @{CategoryLft} > #{rgt};
-- |]
-- @
--
-- @^{TableName}@ looks up the table's name and escapes it, @\@{ColumnName}@
-- looks up the column's name and properly escapes it and @#{value}@ inserts
-- the value via the usual parameter substitution mechanism.
--
-- @since 2.7.2
sqlQQ :: QuasiQuoter
sqlQQ = makeQQ [| rawSql |]

-- | Analoguous to 'Database.Persist.Sql.Raw.rawExecute'
--
-- @since 2.7.2
executeQQ :: QuasiQuoter
executeQQ = makeQQ [| rawExecute |]

-- | Analoguous to 'Database.Persist.Sql.Raw.rawExecuteCount'
--
-- @since 2.7.2
executeCountQQ :: QuasiQuoter
executeCountQQ = makeQQ [| rawExecuteCount |]

-- | Analoguous to 'Database.Persist.Sql.Raw.rawQuery'
--
-- @since 2.7.2
queryQQ :: QuasiQuoter
queryQQ = makeQQ [| rawQuery |]

-- | Analoguous to 'Database.Persist.Sql.Raw.rawQueryRes'
--
-- @since 2.7.2
queryResQQ :: QuasiQuoter
queryResQQ = makeQQ [| rawQueryRes |]

0 comments on commit 900d4d3

Please sign in to comment.