{-# LANGUAGE Strict #-}
module Database.PostgreSQL.Entity.Internal
(
isNotNull
, isNull
, isIn
, inParens
, quoteName
, literal
, getTableName
, getFieldName
, getPrimaryKey
, prefix
, expandFields
, expandQualifiedFields
, expandQualifiedFields'
, qualifyField
, qualifyFields
, placeholder
, placeholder'
, generatePlaceholders
, textToQuery
, queryToText
, intercalateVector
, renderSortExpression
)
where
import Data.String (fromString)
import Data.Text (Text, unpack)
import Data.Text.Encoding (decodeUtf8)
import Data.Vector (Vector)
import qualified Data.Vector as V
import Database.PostgreSQL.Simple.Types (Query (..))
import Data.Foldable (fold)
import qualified Data.Text as T
import Data.Text.Display (display)
import Database.PostgreSQL.Entity.Internal.Unsafe (Field (Field))
import Database.PostgreSQL.Entity.Types
inParens :: Text -> Text
inParens :: Text -> Text
inParens Text
t = Text
"(" forall a. Semigroup a => a -> a -> a
<> Text
t forall a. Semigroup a => a -> a -> a
<> Text
")"
quoteName :: Text -> Text
quoteName :: Text -> Text
quoteName Text
n = Text
"\"" forall a. Semigroup a => a -> a -> a
<> Text
n forall a. Semigroup a => a -> a -> a
<> Text
"\""
literal :: Text -> Text
literal :: Text -> Text
literal Text
n = Text
"\'" forall a. Semigroup a => a -> a -> a
<> Text -> Text
escapeSingleQuotes Text
n forall a. Semigroup a => a -> a -> a
<> Text
"\'"
where
escapeSingleQuotes :: Text -> Text
escapeSingleQuotes Text
x = HasCallStack => Text -> Text -> Text -> Text
T.replace Text
"'" Text
"''" Text
x
getTableName :: forall e. Entity e => Text
getTableName :: forall e. Entity e => Text
getTableName = Maybe Text -> Text
prefix (forall e. Entity e => Maybe Text
schema @e) forall a. Semigroup a => a -> a -> a
<> Text -> Text
quoteName (forall e. Entity e => Text
tableName @e)
getPrimaryKey :: forall e. Entity e => Text
getPrimaryKey :: forall e. Entity e => Text
getPrimaryKey = Field -> Text
getFieldName forall a b. (a -> b) -> a -> b
$ forall e. Entity e => Field
primaryKey @e
prefix :: Maybe Text -> Text
prefix :: Maybe Text -> Text
prefix = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (forall a. Semigroup a => a -> a -> a
<> Text
".")
getFieldName :: Field -> Text
getFieldName :: Field -> Text
getFieldName = Text -> Text
quoteName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Text
fieldName
expandFields :: forall e. Entity e => Text
expandFields :: forall e. Entity e => Text
expandFields = forall a. (a -> a -> a) -> Vector a -> a
V.foldl1' (\Text
element Text
acc -> Text
element forall a. Semigroup a => a -> a -> a
<> Text
", " forall a. Semigroup a => a -> a -> a
<> Text
acc) (Field -> Text
getFieldName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall e. Entity e => Vector Field
fields @e)
expandQualifiedFields :: forall e. Entity e => Text
expandQualifiedFields :: forall e. Entity e => Text
expandQualifiedFields = Vector Field -> Text -> Text
expandQualifiedFields' (forall e. Entity e => Vector Field
fields @e) Text
prefixName
where
prefixName :: Text
prefixName = forall e. Entity e => Text
tableName @e
expandQualifiedFields' :: Vector Field -> Text -> Text
expandQualifiedFields' :: Vector Field -> Text -> Text
expandQualifiedFields' Vector Field
fs Text
prefixName = forall a. (a -> a -> a) -> Vector a -> a
V.foldl1' (\Text
element Text
acc -> Text
element forall a. Semigroup a => a -> a -> a
<> Text
", " forall a. Semigroup a => a -> a -> a
<> Text
acc) Vector Text
fs'
where
fs' :: Vector Text
fs' = Field -> Text
fieldName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Vector Field -> Vector Field
qualifyFields Text
prefixName Vector Field
fs
qualifyField :: forall e. Entity e => Field -> Text
qualifyField :: forall e. Entity e => Field -> Text
qualifyField Field
f = (\(Field Text
fName Maybe Text
_) -> Text
p forall a. Semigroup a => a -> a -> a
<> Text
"." forall a. Semigroup a => a -> a -> a
<> Text -> Text
quoteName Text
fName) Field
f
where
p :: Text
p = forall e. Entity e => Text
tableName @e
qualifyFields :: Text -> Vector Field -> Vector Field
qualifyFields :: Text -> Vector Field -> Vector Field
qualifyFields Text
p Vector Field
fs = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Field Text
f Maybe Text
t) -> Text -> Maybe Text -> Field
Field (Text
p forall a. Semigroup a => a -> a -> a
<> Text
"." forall a. Semigroup a => a -> a -> a
<> Text -> Text
quoteName Text
f) Maybe Text
t) Vector Field
fs
placeholder :: Field -> Text
placeholder :: Field -> Text
placeholder (Field Text
f Maybe Text
Nothing) = Text -> Text
quoteName Text
f forall a. Semigroup a => a -> a -> a
<> Text
" = ?"
placeholder (Field Text
f (Just Text
t)) = Text -> Text
quoteName Text
f forall a. Semigroup a => a -> a -> a
<> Text
" = ?::" forall a. Semigroup a => a -> a -> a
<> Text
t
placeholder' :: forall e. Entity e => Field -> Text
placeholder' :: forall e. Entity e => Field -> Text
placeholder' f :: Field
f@(Field Text
_ (Just Text
t)) = forall e. Entity e => Field -> Text
qualifyField @e Field
f forall a. Semigroup a => a -> a -> a
<> Text
" = ?::" forall a. Semigroup a => a -> a -> a
<> Text
t
placeholder' Field
f = forall e. Entity e => Field -> Text
qualifyField @e Field
f forall a. Semigroup a => a -> a -> a
<> Text
" = ?"
generatePlaceholders :: Vector Field -> Text
generatePlaceholders :: Vector Field -> Text
generatePlaceholders Vector Field
vf = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
", " forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Field -> Text
ph Vector Field
vf
where
ph :: Field -> Text
ph (Field Text
_ Maybe Text
t) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"?" (\Text
t' -> Text
"?::" forall a. Semigroup a => a -> a -> a
<> Text
t') Maybe Text
t
isNotNull :: Vector Field -> Text
isNotNull :: Vector Field -> Text
isNotNull Vector Field
fs' = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
" AND " (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
process Vector Text
fieldNames)
where
fieldNames :: Vector Text
fieldNames = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Field -> Text
fieldName Vector Field
fs'
process :: Text -> Text
process Text
f = Text -> Text
quoteName Text
f forall a. Semigroup a => a -> a -> a
<> Text
" IS NOT NULL"
isNull :: Vector Field -> Text
isNull :: Vector Field -> Text
isNull Vector Field
fs' = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
" AND " (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
process Vector Text
fieldNames)
where
fieldNames :: Vector Text
fieldNames = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Field -> Text
fieldName Vector Field
fs'
process :: Text -> Text
process Text
f = Text -> Text
quoteName Text
f forall a. Semigroup a => a -> a -> a
<> Text
" IS NULL"
isIn :: Field -> Vector Text -> Text
isIn :: Field -> Vector Text -> Text
isIn Field
f Vector Text
values = Field -> Text
process Field
f forall a. Semigroup a => a -> a -> a
<> Text
" IN (" forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Text -> Vector Text -> Vector Text
intercalateVector Text
", " Vector Text
vals) forall a. Semigroup a => a -> a -> a
<> Text
")"
where
vals :: Vector Text
vals = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
literal Vector Text
values
process :: Field -> Text
process Field
f' = Text -> Text
quoteName forall a b. (a -> b) -> a -> b
$ Field -> Text
fieldName Field
f'
textToQuery :: Text -> Query
textToQuery :: Text -> Query
textToQuery = forall a. IsString a => String -> a
fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack
queryToText :: Query -> Text
queryToText :: Query -> Text
queryToText = ByteString -> Text
decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> ByteString
fromQuery
intercalateVector :: Text -> Vector Text -> Vector Text
intercalateVector :: Text -> Vector Text -> Vector Text
intercalateVector Text
sep Vector Text
vt
| forall a. Vector a -> Bool
V.null Vector Text
vt = Vector Text
vt
| Bool
otherwise = forall a. a -> Vector a -> Vector a
V.cons Text
x (Vector Text -> Vector Text
go Vector Text
xs)
where
(Text
x, Vector Text
xs) = (forall a. Vector a -> a
V.head Vector Text
vt, forall a. Vector a -> Vector a
V.tail Vector Text
vt)
go :: Vector Text -> Vector Text
go :: Vector Text -> Vector Text
go Vector Text
ys
| forall a. Vector a -> Bool
V.null Vector Text
ys = Vector Text
ys
| Bool
otherwise = forall a. a -> Vector a -> Vector a
V.cons Text
sep (forall a. a -> Vector a -> Vector a
V.cons (forall a. Vector a -> a
V.head Vector Text
ys) (Vector Text -> Vector Text
go (forall a. Vector a -> Vector a
V.tail Vector Text
ys)))
renderSortExpression :: (Field, SortKeyword) -> Text
renderSortExpression :: (Field, SortKeyword) -> Text
renderSortExpression (Field
f, SortKeyword
sort) = (Text -> Text
quoteName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Text
fieldName) Field
f forall a. Semigroup a => a -> a -> a
<> Text
" " forall a. Semigroup a => a -> a -> a
<> forall a. Display a => a -> Text
display SortKeyword
sort