Skip to content

Commit 66f84c5

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
refactor: move escapeIdent function to Identifiers.hs
Moves the functions `escapeIdent` and `trimNullChars` to SchemaCache/Identifiers.hs module. Signed-off-by: Taimoor Zaeem <[email protected]>
1 parent 5d9b169 commit 66f84c5

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/PostgREST/Query/SqlFragment.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ import PostgREST.RangeQuery (NonnegRange, allRange,
9090
rangeLimit, rangeOffset)
9191
import PostgREST.SchemaCache.Identifiers (FieldName,
9292
QualifiedIdentifier (..),
93-
RelIdentifier (..))
93+
RelIdentifier (..),
94+
escapeIdent, trimNullChars)
9495
import PostgREST.SchemaCache.Routine (MediaHandler (..),
9596
Routine (..),
9697
funcReturnsScalar,
@@ -163,9 +164,6 @@ pgBuildArrayLiteral vals =
163164
pgFmtIdent :: Text -> SQL.Snippet
164165
pgFmtIdent x = SQL.sql . encodeUtf8 $ escapeIdent x
165166

166-
escapeIdent :: Text -> Text
167-
escapeIdent x = "\"" <> T.replace "\"" "\"\"" (trimNullChars x) <> "\""
168-
169167
-- Only use it if the input comes from the database itself, like on `jsonb_build_object('column_from_a_table', val)..`
170168
pgFmtLit :: Text -> Text
171169
pgFmtLit x =
@@ -176,9 +174,6 @@ pgFmtLit x =
176174
then "E" <> slashed
177175
else slashed
178176

179-
trimNullChars :: Text -> Text
180-
trimNullChars = T.takeWhile (/= '\x0')
181-
182177
-- |
183178
-- Format a list of identifiers and separate them by commas.
184179
--

src/PostgREST/SchemaCache.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ import NeatInterpolation (trimming)
4242
import PostgREST.Config (AppConfig (..))
4343
import PostgREST.Config.Database (TimezoneNames,
4444
toIsolationLevel)
45-
import PostgREST.Query.SqlFragment (escapeIdent)
4645
import PostgREST.SchemaCache.Identifiers (FieldName,
4746
QualifiedIdentifier (..),
4847
RelIdentifier (..),
49-
Schema, isAnyElement)
48+
Schema, escapeIdent,
49+
isAnyElement)
5050
import PostgREST.SchemaCache.Relationship (Cardinality (..),
5151
Junction (..),
5252
Relationship (..),

src/PostgREST/SchemaCache/Identifiers.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ module PostgREST.SchemaCache.Identifiers
88
, Schema
99
, TableName
1010
, dumpQi
11+
, escapeIdent
1112
, isAnyElement
1213
, toQi
14+
, trimNullChars
1315
) where
1416

1517
import qualified Data.Aeson as JSON
@@ -46,6 +48,12 @@ toQi txt = case T.drop 1 <$> T.breakOn "." txt of
4648
(i, "") -> QualifiedIdentifier mempty i
4749
(s, i) -> QualifiedIdentifier s i
4850

51+
escapeIdent :: Text -> Text
52+
escapeIdent x = "\"" <> T.replace "\"" "\"\"" (trimNullChars x) <> "\""
53+
54+
trimNullChars :: Text -> Text
55+
trimNullChars = T.takeWhile (/= '\x0')
56+
4957
type Schema = Text
5058
type TableName = Text
5159
type FieldName = Text

0 commit comments

Comments
 (0)