{-# LANGUAGE Strict #-}
module Database.PostgreSQL.Entity.Internal
(
isNotNull
, isNull
, inParens
, quoteName
, getTableName
, expandFields
, expandQualifiedFields
, expandQualifiedFields'
, qualifyFields
, placeholder
, generatePlaceholders
, textToQuery
, queryToText
, intercalateVector
) 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 Database.PostgreSQL.Entity.Internal.Unsafe (Field (Field))
import Database.PostgreSQL.Entity.Types
inParens :: Text -> Text
inParens :: Text -> Text
inParens Text
t = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
quoteName :: Text -> Text
quoteName :: Text -> Text
quoteName Text
n = Text
"\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\""
getTableName :: forall e. Entity e => Text
getTableName :: Text
getTableName = Text -> Text
quoteName (Entity e => Text
forall e. Entity e => Text
tableName @e)
getFieldName :: Field -> Text
getFieldName :: Field -> Text
getFieldName = Text -> Text
quoteName (Text -> Text) -> (Field -> Text) -> Field -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Text
fieldName
expandFields :: forall e. Entity e => Text
expandFields :: Text
expandFields = (Text -> Text -> Text) -> Vector Text -> Text
forall a. (a -> a -> a) -> Vector a -> a
V.foldl1' (\Text
element Text
acc -> Text
element Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
acc) (Field -> Text
getFieldName (Field -> Text) -> Vector Field -> Vector Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Entity e => Vector Field
forall e. Entity e => Vector Field
fields @e)
expandQualifiedFields :: forall e. Entity e => Text
expandQualifiedFields :: Text
expandQualifiedFields = Vector Field -> Text -> Text
expandQualifiedFields' (Entity e => Vector Field
forall e. Entity e => Vector Field
fields @e) Text
prefix
where
prefix :: Text
prefix = Entity e => Text
forall e. Entity e => Text
tableName @e
expandQualifiedFields' :: Vector Field -> Text -> Text
expandQualifiedFields' :: Vector Field -> Text -> Text
expandQualifiedFields' Vector Field
fs Text
prefix = (Text -> Text -> Text) -> Vector Text -> Text
forall a. (a -> a -> a) -> Vector a -> a
V.foldl1' (\Text
element Text
acc -> Text
element Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
acc) Vector Text
fs'
where
fs' :: Vector Text
fs' = Field -> Text
fieldName (Field -> Text) -> Vector Field -> Vector Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Vector Field -> Vector Field
qualifyFields Text
prefix Vector Field
fs
qualifyFields :: Text -> Vector Field -> Vector Field
qualifyFields :: Text -> Vector Field -> Vector Field
qualifyFields Text
p Vector Field
fs = (Field -> Field) -> Vector Field -> Vector Field
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 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> 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 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" = ?"
placeholder (Field Text
f (Just Text
t)) = Text -> Text
quoteName Text
f Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" = ?::" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t
generatePlaceholders :: Vector Field -> Text
generatePlaceholders :: Vector Field -> Text
generatePlaceholders Vector Field
vf = Vector Text -> Text
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Vector Text -> Text) -> Vector Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
", " (Vector Text -> Vector Text) -> Vector Text -> Vector Text
forall a b. (a -> b) -> a -> b
$ (Field -> Text) -> Vector Field -> Vector Text
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) = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"?" (\Text
t' -> Text
"?::" Text -> Text -> 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' = Vector Text -> Text
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Vector Text -> Text) -> Vector Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
" AND " ((Text -> Text) -> Vector Text -> Vector Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
process Vector Text
fieldNames)
where
fieldNames :: Vector Text
fieldNames = (Field -> Text) -> Vector Field -> Vector Text
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 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" IS NOT NULL"
isNull :: Vector Field -> Text
isNull :: Vector Field -> Text
isNull Vector Field
fs' = Vector Text -> Text
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Vector Text -> Text) -> Vector Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Vector Text -> Vector Text
intercalateVector Text
" AND " ((Text -> Text) -> Vector Text -> Vector Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
process Vector Text
fieldNames)
where
fieldNames :: Vector Text
fieldNames = (Field -> Text) -> Vector Field -> Vector Text
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 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" IS NULL"
textToQuery :: Text -> Query
textToQuery :: Text -> Query
textToQuery = String -> Query
forall a. IsString a => String -> a
fromString (String -> Query) -> (Text -> String) -> Text -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack
queryToText :: Query -> Text
queryToText :: Query -> Text
queryToText = ByteString -> Text
decodeUtf8 (ByteString -> Text) -> (Query -> ByteString) -> Query -> Text
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 | Vector Text -> Bool
forall a. Vector a -> Bool
V.null Vector Text
vt = Vector Text
vt
| Bool
otherwise = Text -> Vector Text -> Vector Text
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) = (Vector Text -> Text
forall a. Vector a -> a
V.head Vector Text
vt, Vector Text -> Vector Text
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 | Vector Text -> Bool
forall a. Vector a -> Bool
V.null Vector Text
ys = Vector Text
ys
| Bool
otherwise = Text -> Vector Text -> Vector Text
forall a. a -> Vector a -> Vector a
V.cons Text
sep (Text -> Vector Text -> Vector Text
forall a. a -> Vector a -> Vector a
V.cons (Vector Text -> Text
forall a. Vector a -> a
V.head Vector Text
ys) (Vector Text -> Vector Text
go (Vector Text -> Vector Text
forall a. Vector a -> Vector a
V.tail Vector Text
ys)))