on dev help sign in
Literal class
4d66d97Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+29−19
@@ -47,6 +47,7 @@ module Squeal.PostgreSQL.Expression
47 , unsafeFunctionHet
47 , unsafeFunctionHet
48 , HasParameter (parameter)
48 , HasParameter (parameter)
49 , param
49 , param
50+ , Literal (..)
50 -- ** Null
51 -- ** Null
51 , null_
52 , null_
52 , notNull
53 , notNull
@@ -75,7 +76,6 @@ module Squeal.PostgreSQL.Expression
75 , ceiling_
76 , ceiling_
76 , greatest
77 , greatest
77 , least
78 , least
78− , litChar
79 -- ** Conditions
79 -- ** Conditions
80 , true
80 , true
81 , false
81 , false
@@ -136,8 +136,6 @@ module Squeal.PostgreSQL.Expression
136 , (.-.)
136 , (.-.)
137 , (#-.)
137 , (#-.)
138 -- *** Functions
138 -- *** Functions
139− , jsonLit
140− , jsonbLit
141 , toJson
139 , toJson
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
263+import Data.Int
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, (.))
272
272
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
276
279
280+import Squeal.PostgreSQL.Binary
277import Squeal.PostgreSQL.Render
281import Squeal.PostgreSQL.Render
278import Squeal.PostgreSQL.Schema
282import Squeal.PostgreSQL.Schema
279
283
@@ -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)
737
741
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-----------------------------------------}
1315
1319
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−
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"
1579
1573
1580−litChar :: Char -> Expr (null ('PGchar 1))
1581−litChar chr = UnsafeExpression $
1582− "E\'" <> fromString (escape =<< [chr]) <> "\'"
1583−
1584strip :: null 'PGtsvector :--> null 'PGtsvector
1574strip :: null 'PGtsvector :--> null 'PGtsvector
1585strip = unsafeFunction "strip"
1575strip = unsafeFunction "strip"
1586
1576
@@ -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 <> "'"
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
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+6−6
@@ -730,7 +730,7 @@ unnest = unsafeSetOfFunction "unnest"
730
730
731-- | Expands the outermost JSON object into a set of key/value pairs.
731-- | Expands the outermost JSON object into a set of key/value pairs.
732-- >>> import Data.Aeson
732-- >>> import Data.Aeson
733−-- >>> printSQL (select Star (from (jsonEach (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
733+-- >>> printSQL (select Star (from (jsonEach (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
734-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
734-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
735jsonEach
735jsonEach
736 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
736 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -740,7 +740,7 @@ jsonEach = unsafeSetOfFunction "json_each"
740
740
741-- | Expands the outermost binary JSON object into a set of key/value pairs.
741-- | Expands the outermost binary JSON object into a set of key/value pairs.
742-- >>> import Data.Aeson
742-- >>> import Data.Aeson
743−-- >>> printSQL (select Star (from (jsonbEach (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
743+-- >>> printSQL (select Star (from (jsonbEach (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
744-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
744-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
745jsonbEach
745jsonbEach
746 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
746 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
@@ -750,7 +750,7 @@ jsonbEach = unsafeSetOfFunction "jsonb_each"
750
750
751-- | Expands the outermost JSON object into a set of key/value pairs.-- | Expands the outermost binary JSON object into a set of key/value pairs.
751-- | Expands the outermost JSON object into a set of key/value pairs.-- | Expands the outermost binary JSON object into a set of key/value pairs.
752-- >>> import Data.Aeson
752-- >>> import Data.Aeson
753−-- >>> printSQL (select Star (from (jsonEachText (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
753+-- >>> printSQL (select Star (from (jsonEachText (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
754-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
754-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
755jsonEachText
755jsonEachText
756 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
756 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -760,7 +760,7 @@ jsonEachText = unsafeSetOfFunction "json_each_text"
760
760
761-- | Expands the outermost binary JSON object into a set of key/value pairs.
761-- | Expands the outermost binary JSON object into a set of key/value pairs.
762-- >>> import Data.Aeson
762-- >>> import Data.Aeson
763−-- >>> printSQL (select Star (from (jsonbEachText (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
763+-- >>> printSQL (select Star (from (jsonbEachText (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
764-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
764-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
765jsonbEachText
765jsonbEachText
766 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
766 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
@@ -770,7 +770,7 @@ jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
770
770
771-- | Returns set of keys in the outermost JSON object.
771-- | Returns set of keys in the outermost JSON object.
772-- >>> import Data.Aeson
772-- >>> import Data.Aeson
773−-- >>> printSQL (jsonObjectKeys (jsonLit (object ["a" .= "foo", "b" .= "bar"])))
773+-- >>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
774-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
774-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
775jsonObjectKeys
775jsonObjectKeys
776 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
776 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -780,7 +780,7 @@ jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
780
780
781-- | Returns set of keys in the outermost JSON object.
781-- | Returns set of keys in the outermost JSON object.
782-- >>> import Data.Aeson
782-- >>> import Data.Aeson
783−-- >>> printSQL (jsonbObjectKeys (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))
783+-- >>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
784-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
784-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
785jsonbObjectKeys
785jsonbObjectKeys
786 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
786 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object