on dev help sign in
Literal class
commit4d66d978b07257363b2e25affa07abcd6fea3e08 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateWed 27 Mar 2019, 19:10 (8 years ago) parentdd4d47b
2 files changed+35−25
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+29−19 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+6−6
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+29−19
@@ -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
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+6−6
@@ -730,7 +730,7 @@ unnest = unsafeSetOfFunction "unnest"
730730
731731-- | Expands the outermost JSON object into a set of key/value pairs.
732732-- >>> 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"]))))))
734734-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
735735jsonEach
736736 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -740,7 +740,7 @@ jsonEach = unsafeSetOfFunction "json_each"
740740
741741-- | Expands the outermost binary JSON object into a set of key/value pairs.
742742-- >>> 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"]))))))
744744-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
745745jsonbEach
746746 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
@@ -750,7 +750,7 @@ jsonbEach = unsafeSetOfFunction "jsonb_each"
750750
751751-- | 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.
752752-- >>> 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"]))))))
754754-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
755755jsonEachText
756756 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -760,7 +760,7 @@ jsonEachText = unsafeSetOfFunction "json_each_text"
760760
761761-- | Expands the outermost binary JSON object into a set of key/value pairs.
762762-- >>> 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"]))))))
764764-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
765765jsonbEachText
766766 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
@@ -770,7 +770,7 @@ jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
770770
771771-- | Returns set of keys in the outermost JSON object.
772772-- >>> import Data.Aeson
773−-- >>> printSQL (jsonObjectKeys (jsonLit (object ["a" .= "foo", "b" .= "bar"])))
773+-- >>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
774774-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
775775jsonObjectKeys
776776 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjson) -- ^ json object
@@ -780,7 +780,7 @@ jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
780780
781781-- | Returns set of keys in the outermost JSON object.
782782-- >>> import Data.Aeson
783−-- >>> printSQL (jsonbObjectKeys (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))
783+-- >>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
784784-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
785785jsonbObjectKeys
786786 :: Expression outer commons 'Ungrouped schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object