@@ -47,6 +47,7 @@ module Squeal.PostgreSQL.Expression
47 , unsafeFunctionHet
47 , unsafeFunctionHet
48 , HasParameter (parameter)
48 , HasParameter (parameter)
50 -- ** Null
51 -- ** Null
@@ -75,7 +76,6 @@ module Squeal.PostgreSQL.Expression
75 , ceiling_
76 , ceiling_
76 , greatest
77 , greatest
79 -- ** Conditions
79 -- ** Conditions
@@ -136,8 +136,6 @@ module Squeal.PostgreSQL.Expression
138 -- *** Functions
138 -- *** Functions
142 , toJsonb
140 , toJsonb
143 , arrayToJson
141 , arrayToJson
@@ -262,7 +260,9 @@ import Data.Function ((&))
262import Data.Kind
260import Data.Kind
263import Data.Semigroup hiding (All)
261import Data.Semigroup hiding (All)
264import qualified Data.Aeson as JSON
262import qualified Data.Aeson as JSON
265import Data.String
264import Data.String
265+import Data.Text (Text)
266import Data.Word
266import Data.Word
267import Generics.SOP hiding (All, from)
267import Generics.SOP hiding (All, from)
268import GHC.OverloadedLabels
268import GHC.OverloadedLabels
@@ -271,9 +271,13 @@ import Numeric
271import Prelude hiding (id, (.))
271import Prelude hiding (id, (.))
273import qualified Data.ByteString as ByteString
273import 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
274import qualified GHC.Generics as GHC
277import qualified GHC.Generics as GHC
275import qualified Generics.SOP as SOP
278import qualified Generics.SOP as SOP
280+import Squeal.PostgreSQL.Binary
277import Squeal.PostgreSQL.Render
281import Squeal.PostgreSQL.Render
278import Squeal.PostgreSQL.Schema
282import Squeal.PostgreSQL.Schema
@@ -735,8 +739,8 @@ unsafeFunction :: ByteString {- ^ function -} -> x :--> y
735unsafeFunction fun x = UnsafeExpression $
739unsafeFunction fun x = UnsafeExpression $
736 fun <> parenthesized (renderSQL x)
740 fun <> parenthesized (renderSQL x)
738−-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: litChar 'a')
742+-- | >>> printSQL $ unsafeFunctionHet "f" (currentTime :* localTimestamp :* false *: literal 'a')
739−-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
743+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, (E'a' :: char(1)))
740unsafeFunctionHet
744unsafeFunctionHet
741 :: SListI xs
745 :: SListI xs
742 => ByteString {- ^ function -}
746 => ByteString {- ^ function -}
@@ -1313,16 +1317,6 @@ infixl 6 #-.
1313Table 9.45: JSON creation functions
1317Table 9.45: JSON creation functions
1314-----------------------------------------}
1318-----------------------------------------}
1316−-- | Literal binary JSON
1317−jsonbLit :: JSON.ToJSON x => x -> Expr (null 'PGjsonb)
1318−jsonbLit = cast jsonb . UnsafeExpression
1319− . singleQuotedUtf8 . toStrict . JSON.encode
1322−jsonLit :: JSON.ToJSON x => x -> Expr (null 'PGjson)
1323−jsonLit = cast json . UnsafeExpression
1324− . singleQuotedUtf8 . toStrict . JSON.encode
1326-- | Returns the value as json. Arrays and composites are converted
1320-- | Returns the value as json. Arrays and composites are converted
1327-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1321-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1328-- type to json, the cast function will be used to perform the conversion;
1322-- type to json, the cast function will be used to perform the conversion;
@@ -1577,10 +1571,6 @@ setWeight
1577 :: FunctionHet '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
1571 :: FunctionHet '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
1578setWeight = unsafeFunctionHet "set_weight"
1572setWeight = unsafeFunctionHet "set_weight"
1580−litChar :: Char -> Expr (null ('PGchar 1))
1581−litChar chr = UnsafeExpression $
1582− "E\'" <> fromString (escape =<< [chr]) <> "\'"
1584strip :: null 'PGtsvector :--> null 'PGtsvector
1574strip :: null 'PGtsvector :--> null 'PGtsvector
1585strip = unsafeFunction "strip"
1575strip = unsafeFunction "strip"
@@ -2556,3 +2546,23 @@ instance RenderSQL TimeUnit where
2556interval_ :: Double -> TimeUnit -> Expr (null 'PGinterval)
2546interval_ :: Double -> TimeUnit -> Expr (null 'PGinterval)
2557interval_ num unit = UnsafeExpression . parenthesized $ "INTERVAL" <+>
2547interval_ num unit = UnsafeExpression . parenthesized $ "INTERVAL" <+>
2558 "'" <> fromString (show num) <+> renderSQL unit <> "'"
2548 "'" <> fromString (show num) <+> renderSQL unit <> "'"
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