Skip to content

Commit db2be09

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
refactor: move dumpQi to Config.hs and clarify quoteQi
The `dumpQi` function is only used in the `Config.hs` module, so it is moved there. This also adds a doctest for `quoteQi` and clarifies its usage. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent a688878 commit db2be09

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/PostgREST/Config.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import PostgREST.Config.JSPath (FilterExp (..), JSPath,
6363
pRoleClaimKey)
6464
import PostgREST.Config.Proxy (Proxy (..),
6565
isMalformedProxyUri, toURI)
66-
import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier, dumpQi,
66+
import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..),
6767
toQi)
6868

6969
import Protolude hiding (Proxy, toList)
@@ -198,6 +198,10 @@ toText conf =
198198
-- quote strings and replace " with \"
199199
q s = "\"" <> T.replace "\"" "\\\"" s <> "\""
200200

201+
dumpQi :: QualifiedIdentifier -> Text
202+
dumpQi (QualifiedIdentifier s i) =
203+
(if T.null s then mempty else s <> ".") <> i
204+
201205
showTxEnd c = case (configDbTxRollbackAll c, configDbTxAllowOverride c) of
202206
( False, False ) -> "commit"
203207
( False, True ) -> "commit-allow-override"

src/PostgREST/SchemaCache/Identifiers.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module PostgREST.SchemaCache.Identifiers
77
, RelIdentifier(..)
88
, Schema
99
, TableName
10-
, dumpQi
1110
, escapeIdent
1211
, isAnyElement
1312
, quoteQi
@@ -38,10 +37,12 @@ instance Hashable QualifiedIdentifier
3837
isAnyElement :: QualifiedIdentifier -> Bool
3938
isAnyElement y = QualifiedIdentifier "pg_catalog" "anyelement" == y
4039

41-
dumpQi :: QualifiedIdentifier -> Text
42-
dumpQi (QualifiedIdentifier s i) =
43-
(if T.null s then mempty else s <> ".") <> i
44-
40+
-- |
41+
-- Quote the qualified identifier when preparing the SQL. This avoids parse
42+
-- errors by postgres, for example on pg reserved words like "true" or "select".
43+
--
44+
-- >>> quoteQi (QualifiedIdentifier "" "true")
45+
-- "\"true\""
4546
quoteQi :: QualifiedIdentifier -> Text
4647
quoteQi (QualifiedIdentifier s i) =
4748
(if T.null s then mempty else escapeIdent s <> ".") <> escapeIdent i

test/doc/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ main =
2323
, "src/PostgREST/Query/SqlFragment.hs"
2424
, "src/PostgREST/Response.hs"
2525
, "src/PostgREST/Response/Performance.hs"
26+
, "src/PostgREST/SchemaCache/Identifiers.hs"
2627
]

0 commit comments

Comments
 (0)