{-# LANGUAGE OverloadedStrings #-}
module Arbiter.Core.SqlLiterals
( textLiteral
, quoteIdentifier
, doubleLiteral
, intLiteral
) where
import Data.Text (Text)
import Data.Text qualified as T
textLiteral :: Text -> Text
textLiteral :: Text -> Text
textLiteral Text
text = Text
"'" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"'" Text
"''" Text
text Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'"
quoteIdentifier :: Text -> Text
quoteIdentifier :: Text -> Text
quoteIdentifier Text
ident = Text
"\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\"" Text
"\"\"" Text
ident Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\""
doubleLiteral :: Double -> Text
doubleLiteral :: Double -> Text
doubleLiteral Double
value
| Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
value Bool -> Bool -> Bool
|| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
value = Text
"'" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Double -> String
forall a. Show a => a -> String
show Double
value) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'::double precision"
| Bool
otherwise = String -> Text
T.pack (Double -> String
forall a. Show a => a -> String
show Double
value)
intLiteral :: (Integral a) => a -> Text
intLiteral :: forall a. Integral a => a -> Text
intLiteral a
value = String -> Text
T.pack (Integer -> String
forall a. Show a => a -> String
show (a -> Integer
forall a. Integral a => a -> Integer
toInteger a
value))