From 939061baff8d35e1f92d9ce8f8fcd4e97cf822c7 Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Tue, 28 Oct 2025 11:49:04 +0500 Subject: [PATCH] refactor: move escapeIdent function to Identifiers.hs Moves the functions `escapeIdent` and `trimNullChars` to SchemaCache/Identifiers.hs module. Signed-off-by: Taimoor Zaeem (cherry picked from commit 66f84c59030b3d19638d84dbda0721f4fb2fdb29) --- src/PostgREST/Query/SqlFragment.hs | 9 ++------- src/PostgREST/SchemaCache.hs | 4 ++-- src/PostgREST/SchemaCache/Identifiers.hs | 8 ++++++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/PostgREST/Query/SqlFragment.hs b/src/PostgREST/Query/SqlFragment.hs index 0f42b6778..461d4e827 100644 --- a/src/PostgREST/Query/SqlFragment.hs +++ b/src/PostgREST/Query/SqlFragment.hs @@ -90,7 +90,8 @@ import PostgREST.RangeQuery (NonnegRange, allRange, rangeLimit, rangeOffset) import PostgREST.SchemaCache.Identifiers (FieldName, QualifiedIdentifier (..), - RelIdentifier (..)) + RelIdentifier (..), + escapeIdent, trimNullChars) import PostgREST.SchemaCache.Routine (MediaHandler (..), Routine (..), funcReturnsScalar, @@ -163,9 +164,6 @@ pgBuildArrayLiteral vals = pgFmtIdent :: Text -> SQL.Snippet pgFmtIdent x = SQL.sql . encodeUtf8 $ escapeIdent x -escapeIdent :: Text -> Text -escapeIdent x = "\"" <> T.replace "\"" "\"\"" (trimNullChars x) <> "\"" - -- Only use it if the input comes from the database itself, like on `jsonb_build_object('column_from_a_table', val)..` pgFmtLit :: Text -> Text pgFmtLit x = @@ -176,9 +174,6 @@ pgFmtLit x = then "E" <> slashed else slashed -trimNullChars :: Text -> Text -trimNullChars = T.takeWhile (/= '\x0') - -- | -- Format a list of identifiers and separate them by commas. -- diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index 13cced246..528d7905f 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -42,11 +42,11 @@ import NeatInterpolation (trimming) import PostgREST.Config (AppConfig (..)) import PostgREST.Config.Database (TimezoneNames, toIsolationLevel) -import PostgREST.Query.SqlFragment (escapeIdent) import PostgREST.SchemaCache.Identifiers (FieldName, QualifiedIdentifier (..), RelIdentifier (..), - Schema, isAnyElement) + Schema, escapeIdent, + isAnyElement) import PostgREST.SchemaCache.Relationship (Cardinality (..), Junction (..), Relationship (..), diff --git a/src/PostgREST/SchemaCache/Identifiers.hs b/src/PostgREST/SchemaCache/Identifiers.hs index ac1bb2a3b..a73d970cd 100644 --- a/src/PostgREST/SchemaCache/Identifiers.hs +++ b/src/PostgREST/SchemaCache/Identifiers.hs @@ -8,8 +8,10 @@ module PostgREST.SchemaCache.Identifiers , Schema , TableName , dumpQi + , escapeIdent , isAnyElement , toQi + , trimNullChars ) where import qualified Data.Aeson as JSON @@ -46,6 +48,12 @@ toQi txt = case T.drop 1 <$> T.breakOn "." txt of (i, "") -> QualifiedIdentifier mempty i (s, i) -> QualifiedIdentifier s i +escapeIdent :: Text -> Text +escapeIdent x = "\"" <> T.replace "\"" "\"\"" (trimNullChars x) <> "\"" + +trimNullChars :: Text -> Text +trimNullChars = T.takeWhile (/= '\x0') + type Schema = Text type TableName = Text type FieldName = Text