@@ -47,6 +47,7 @@ module Squeal.PostgreSQL.Expression
4747 , unsafeFunctionHet
4848 , HasParameter (parameter)
4949 , param
50+ , Literal (..)
5051 -- ** Null
5152 , null_
5253 , notNull
@@ -75,7 +76,6 @@ module Squeal.PostgreSQL.Expression
7576 , ceiling_
7677 , greatest
7778 , least
78− , litChar
7979 -- ** Conditions
8080 , true
8181 , false
@@ -136,8 +136,6 @@ module Squeal.PostgreSQL.Expression
136136 , (.-.)
137137 , (#-.)
138138 -- *** Functions
139− , jsonLit
140− , jsonbLit
141139 , toJson
142140 , toJsonb
143141 , arrayToJson
@@ -262,7 +260,9 @@ import Data.Function ((&))
262260import Data.Kind
263261import Data.Semigroup hiding (All)
264262import qualified Data.Aeson as JSON
263+import Data.Int
265264import Data.String
265+import Data.Text (Text)
266266import Data.Word
267267import Generics.SOP hiding (All, from)
268268import GHC.OverloadedLabels
@@ -271,9 +271,13 @@ import Numeric
271271import Prelude hiding (id, (.))
272272
273273import qualified Data.ByteString as ByteString
274+import qualified Data.Text as Text
275+import qualified Data.Text.Lazy as Lazy (Text)
276+import qualified Data.Text.Lazy as Lazy.Text
274277import qualified GHC.Generics as GHC
275278import qualified Generics.SOP as SOP
276279
280+import Squeal.PostgreSQL.Binary
277281import Squeal.PostgreSQL.Render
278282import Squeal.PostgreSQL.Schema
279283
@@ -735,8 +739,8 @@ unsafeFunction :: ByteString {- ^ function -} -> x :--> y
735739unsafeFunction fun x = UnsafeExpression $
736740 fun <> parenthesized (renderSQL x)
737741
738−-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: litChar 'a')
739−-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
742+-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: literal 'a')
743+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, (E'a' :: char(1)))
740744unsafeFunctionHet
741745 :: SListI xs
742746 => ByteString {- ^ function -}
@@ -1313,16 +1317,6 @@ infixl 6 #-.
13131317Table 9.45: JSON creation functions
13141318-----------------------------------------}
13151319
1316−-- | Literal binary JSON
1317−jsonbLit :: JSON.ToJSON x => x -> Expr (null 'PGjsonb)
1318−jsonbLit = cast jsonb . UnsafeExpression
1319− . singleQuotedUtf8 . toStrict . JSON.encode
1320−
1321−-- | Literal JSON
1322−jsonLit :: JSON.ToJSON x => x -> Expr (null 'PGjson)
1323−jsonLit = cast json . UnsafeExpression
1324− . singleQuotedUtf8 . toStrict . JSON.encode
1325−
13261320-- | Returns the value as json. Arrays and composites are converted
13271321-- (recursively) to arrays and objects; otherwise, if there is a cast from the
13281322-- type to json, the cast function will be used to perform the conversion;
@@ -1577,10 +1571,6 @@ setWeight
15771571 :: FunctionHet '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
15781572setWeight = unsafeFunctionHet "set_weight"
15791573
1580−litChar :: Char -> Expr (null ('PGchar 1))
1581−litChar chr = UnsafeExpression $
1582− "E\'" <> fromString (escape =<< [chr]) <> "\'"
1583−
15841574strip :: null 'PGtsvector :--> null 'PGtsvector
15851575strip = unsafeFunction "strip"
15861576
@@ -2556,3 +2546,23 @@ instance RenderSQL TimeUnit where
25562546interval_ :: Double -> TimeUnit -> Expr (null 'PGinterval)
25572547interval_ num unit = UnsafeExpression . parenthesized $ "INTERVAL" <+>
25582548 "'" <> fromString (show num) <+> renderSQL unit <> "'"
2549+
2550+class Literal hask where
2551+ literal :: hask -> Expr (null (PG hask))
2552+instance JSON.ToJSON hask => Literal (Json hask) where
2553+ literal = cast json . UnsafeExpression
2554+ . singleQuotedUtf8 . toStrict . JSON.encode . getJson
2555+instance JSON.ToJSON hask => Literal (Jsonb hask) where
2556+ literal = cast jsonb . UnsafeExpression
2557+ . singleQuotedUtf8 . toStrict . JSON.encode . getJsonb
2558+instance Literal Char where
2559+ literal chr = cast (char @1) . UnsafeExpression $
2560+ "E\'" <> fromString (escape chr) <> "\'"
2561+instance Literal String where literal = fromString
2562+instance Literal Int16 where literal = fromIntegral
2563+instance Literal Int32 where literal = fromIntegral
2564+instance Literal Int64 where literal = fromIntegral
2565+instance Literal Float where literal = fromRational . toRational
2566+instance Literal Double where literal = fromRational . toRational
2567+instance Literal Text where literal = fromString . Text.unpack
2568+instance Literal Lazy.Text where literal = fromString . Lazy.Text.unpack