{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_HADDOCK not-home #-}
module Arbiter.Core.SqlLiterals
( textLiteral
, quoteIdentifier
, doubleLiteral
, intLiteral
, defaultMaxAttemptsSQL
, attemptsLeftSQL
, attemptsSpentSQL
, minMaxAttemptsSQL
, maxIdentifierBytes
, sharedTable
, lockGroupKeyOnConflictSQL
, dollarQuote
) where
import Data.Text (Text)
import Data.Text qualified as T
import NeatInterpolation (text)
import Arbiter.Core.Enum (showText)
import Arbiter.Core.Job.Types (defaultMaxAttempts, minMaxAttempts)
textLiteral :: Text -> Text
textLiteral :: Text -> Text
textLiteral Text
raw = 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
raw 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
<> Double -> Text
forall a. Show a => a -> Text
showText Double
value Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'::double precision"
| Bool
otherwise = Double -> Text
forall a. Show a => a -> Text
showText Double
value
intLiteral :: (Integral a) => a -> Text
intLiteral :: forall a. Integral a => a -> Text
intLiteral a
value = Integer -> Text
forall a. Show a => a -> Text
showText (a -> Integer
forall a. Integral a => a -> Integer
toInteger a
value)
defaultMaxAttemptsSQL :: Text
defaultMaxAttemptsSQL :: Text
defaultMaxAttemptsSQL = Int32 -> Text
forall a. Integral a => a -> Text
intLiteral Int32
defaultMaxAttempts
attemptsLeftSQL :: Text -> Text
attemptsLeftSQL :: Text -> Text
attemptsLeftSQL Text
col = [text|${col}attempts < COALESCE(${col}max_attempts, ${defaultMaxAttemptsSQL})|]
attemptsSpentSQL :: Text -> Text
attemptsSpentSQL :: Text -> Text
attemptsSpentSQL Text
col = [text|${col}attempts >= COALESCE(${col}max_attempts, ${defaultMaxAttemptsSQL})|]
minMaxAttemptsSQL :: Text
minMaxAttemptsSQL :: Text
minMaxAttemptsSQL = Int32 -> Text
forall a. Integral a => a -> Text
intLiteral Int32
minMaxAttempts
maxIdentifierBytes :: Int
maxIdentifierBytes :: Int
maxIdentifierBytes = Int
63
sharedTable :: Text -> Text -> Text
sharedTable :: Text -> Text -> Text
sharedTable Text
schemaName Text
tableName = Text -> Text
quoteIdentifier Text
schemaName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tableName
lockGroupKeyOnConflictSQL :: Text
lockGroupKeyOnConflictSQL :: Text
lockGroupKeyOnConflictSQL = Text
"ON CONFLICT (group_key) DO UPDATE SET group_key = EXCLUDED.group_key WHERE FALSE"
dollarQuote :: Text
dollarQuote :: Text
dollarQuote = Text
"$$"