Skip to content

Commit b97f50a

Browse files
authored
Merge pull request #737 from paul-rouse/master
Rearrange documentation added in #717
2 parents 0f1f552 + 900d4d3 commit b97f50a

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

Diff for: persistent/Database/Persist/Sql.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Database.Persist.Sql
99
, rawQueryRes
1010
, rawExecute
1111
, rawExecuteCount
12+
, rawSql
1213
, sqlQQ
1314
, executeQQ
14-
, rawSql
1515
, deleteWhereCount
1616
, updateWhereCount
1717
, transactionSave

Diff for: persistent/Database/Persist/Sql/Raw/QQ.hs

+45-29
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,14 @@ Module: module Database.Persist.Sql.Raw.QQ
55
Description: QuasiQuoters for performing raw sql queries
66
77
This module exports convenient QuasiQuoters to perform raw SQL queries.
8-
All QuasiQuoters follow them same pattern and are analogous to the similar named
8+
All QuasiQuoters follow the same pattern and are analogous to the similar named
99
functions exported from 'Database.Persist.Sql.Raw'. Neither the quoted
1010
function's behaviour, nor it's return value is altered during the translation
1111
and all documentation provided with it holds.
1212
1313
The QuasiQuoters in this module perform a simple substitution on the query text,
1414
that allows value substitutions, table name substitutions as well as column name
1515
substitutions.
16-
17-
Here is a small example:
18-
19-
Given the following simple model:
20-
21-
@
22-
Category
23-
rgt Int
24-
lft Int
25-
@
26-
27-
We can now execute this raw query:
28-
29-
@
30-
let lft = 10 :: Int
31-
rgt = 20 :: Int
32-
width = rgt - lft
33-
in [sqlQQ|
34-
DELETE FROM ^{Category} WHERE @{CategoryLft} BETWEEN #{lft} AND #{rgt};
35-
UPDATE category SET @{CategoryRgt} = @{CategoryRgt} - #{width} WHERE @{CategoryRgt} > #{rgt};
36-
UPDATE category SET @{CategoryLft} = @{CategoryLft} - #{width} WHERE @{CategoryLft} > #{rgt};
37-
|]
38-
@
39-
40-
@^{TableName}@ looks up the table's name and escapes it, @\@{ColumnName}@ looks
41-
up the column's name and properly escapes it and @#{value}@ inserts the value
42-
via the usual parameter substitution mechanism.
4316
-}
4417

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

157-
-- | Analoguous to 'Database.Persist.Sql.Raw.rawSql'
130+
-- | QuasiQuoter for performing raw sql queries, analoguous to
131+
-- 'Database.Persist.Sql.Raw.rawSql'
132+
--
133+
-- This and the following are convenient QuasiQuoters to perform raw SQL
134+
-- queries. They each follow the same pattern and are analogous to
135+
-- the similarly named @raw@ functions. Neither the quoted function's
136+
-- behaviour, nor it's return value is altered during the translation and
137+
-- all documentation provided with it holds.
138+
--
139+
-- These QuasiQuoters perform a simple substitution on the query text, that
140+
-- allows value substitutions, table name substitutions as well as column name
141+
-- substitutions.
142+
--
143+
-- Here is a small example:
144+
--
145+
-- Given the following simple model:
146+
--
147+
-- @
148+
-- Category
149+
-- rgt Int
150+
-- lft Int
151+
-- @
152+
--
153+
-- We can now execute this raw query:
154+
--
155+
-- @
156+
-- let lft = 10 :: Int
157+
-- rgt = 20 :: Int
158+
-- width = rgt - lft
159+
-- in [sqlQQ|
160+
-- DELETE FROM ^{Category} WHERE @{CategoryLft} BETWEEN #{lft} AND #{rgt};
161+
-- UPDATE category SET @{CategoryRgt} = @{CategoryRgt} - #{width} WHERE @{CategoryRgt} > #{rgt};
162+
-- UPDATE category SET @{CategoryLft} = @{CategoryLft} - #{width} WHERE @{CategoryLft} > #{rgt};
163+
-- |]
164+
-- @
165+
--
166+
-- @^{TableName}@ looks up the table's name and escapes it, @\@{ColumnName}@
167+
-- looks up the column's name and properly escapes it and @#{value}@ inserts
168+
-- the value via the usual parameter substitution mechanism.
169+
--
158170
-- @since 2.7.2
159171
sqlQQ :: QuasiQuoter
160172
sqlQQ = makeQQ [| rawSql |]
161173

162174
-- | Analoguous to 'Database.Persist.Sql.Raw.rawExecute'
175+
--
163176
-- @since 2.7.2
164177
executeQQ :: QuasiQuoter
165178
executeQQ = makeQQ [| rawExecute |]
166179

167180
-- | Analoguous to 'Database.Persist.Sql.Raw.rawExecuteCount'
181+
--
168182
-- @since 2.7.2
169183
executeCountQQ :: QuasiQuoter
170184
executeCountQQ = makeQQ [| rawExecuteCount |]
171185

172186
-- | Analoguous to 'Database.Persist.Sql.Raw.rawQuery'
187+
--
173188
-- @since 2.7.2
174189
queryQQ :: QuasiQuoter
175190
queryQQ = makeQQ [| rawQuery |]
176191

177192
-- | Analoguous to 'Database.Persist.Sql.Raw.rawQueryRes'
193+
--
178194
-- @since 2.7.2
179195
queryResQQ :: QuasiQuoter
180196
queryResQQ = makeQQ [| rawQueryRes |]

0 commit comments

Comments
 (0)