on dev help sign in
set of stuff
commit3113a7af4111f1a93e7810be94b50a9be68b00a0 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateFri 12 Apr 2019, 20:35 (8 years ago) parent1086cfc
3 files changed+63−53
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Collection.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Expression/Json.hs+37−30 squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+25−22
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Collection.hs+1−1
@@ -114,7 +114,7 @@ index n expr = UnsafeExpression $
114114
115115-- | Expand an array to a set of rows
116116unnest :: SetOfFunction "unnest" (null ('PGvararray ty)) '["unnest" ::: ty]
117−unnest = unsafeSetOfFunction #unnest
117+unnest = unsafeSetOfFunction
118118
119119-- | A row constructor is an expression that builds a row value
120120-- (also called a composite value) using values for its member fields.
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Json.hs+37−30
@@ -85,6 +85,7 @@ import qualified Generics.SOP as SOP
8585
8686-- $setup
8787-- >>> import Squeal.PostgreSQL
88+-- >>> import Data.Aeson
8889
8990{-----------------------------------------
9091 -- json and jsonb support
@@ -379,58 +380,64 @@ jsonbPretty :: null 'PGjsonb :--> null 'PGtext
379380jsonbPretty = unsafeFunction "jsonb_pretty"
380381
381382
382−-- | Expands the outermost JSON object into a set of key/value pairs.
383−-- >>> import Data.Aeson
384−-- >>> printSQL (select Star (from (jsonEach (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
385−-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
383+{- | Expands the outermost JSON object into a set of key/value pairs.
384+
385+>>> printSQL (select Star (from (jsonEach (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
386+SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
387+-}
386388jsonEach :: SetOfFunction "json_each" (null 'PGjson)
387389 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
388−jsonEach = unsafeSetOfFunction #json_each
390+jsonEach = unsafeSetOfFunction
389391
390−-- | Expands the outermost binary JSON object into a set of key/value pairs.
391−-- >>> import Data.Aeson
392−-- >>> printSQL (select Star (from (jsonbEach (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
393−-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
392+{- | Expands the outermost binary JSON object into a set of key/value pairs.
393+
394+>>> printSQL (select Star (from (jsonbEach (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
395+SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
396+-}
394397jsonbEach
395398 :: SetOfFunction "jsonb_each" (nullity 'PGjsonb)
396399 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
397−jsonbEach = unsafeSetOfFunction #jsonb_each
400+jsonbEach = unsafeSetOfFunction
398401
399−-- | 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.
400−-- >>> import Data.Aeson
401−-- >>> printSQL (select Star (from (jsonEachText (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
402−-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
402+{- | Expands the outermost JSON object into a set of key/value pairs.
403+
404+>>> printSQL (select Star (from (jsonEachText (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
405+SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
406+-}
403407jsonEachText
404408 :: SetOfFunction "json_each_text" (null 'PGjson)
405409 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
406−jsonEachText = unsafeSetOfFunction #json_each_text
410+jsonEachText = unsafeSetOfFunction
407411
408−-- | Expands the outermost binary JSON object into a set of key/value pairs.
409−-- >>> import Data.Aeson
410−-- >>> printSQL (select Star (from (jsonbEachText (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
411−-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
412+{- | Expands the outermost binary JSON object into a set of key/value pairs.
413+
414+>>> printSQL (select Star (from (jsonbEachText (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
415+SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
416+-}
412417jsonbEachText
413418 :: SetOfFunction "jsonb_each_text" (null 'PGjsonb)
414419 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
415−jsonbEachText = unsafeSetOfFunction #jsonb_each_text
420+jsonbEachText = unsafeSetOfFunction
416421
417−-- | Returns set of keys in the outermost JSON object.
418−-- >>> import Data.Aeson
419−-- >>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
420−-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
422+{- | Returns set of keys in the outermost JSON object.
423+
424+>>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
425+json_object_keys(('{"a":"foo","b":"bar"}' :: json))
426+-}
421427jsonObjectKeys
422428 :: SetOfFunction "json_object_keys" (nullity 'PGjson)
423429 '["json_object_keys" ::: 'NotNull 'PGtext]
424−jsonObjectKeys = unsafeSetOfFunction #json_object_keys
430+jsonObjectKeys = unsafeSetOfFunction
425431
426−-- | Returns set of keys in the outermost JSON object.
427−-- >>> import Data.Aeson
428−-- >>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
429−-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
432+{- | Returns set of keys in the outermost JSON object.
433+
434+>>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
435+jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
436+-}
430437jsonbObjectKeys
431438 :: SetOfFunction "jsonb_object_keys" (null 'PGjsonb)
432439 '["jsonb_object_keys" ::: 'NotNull 'PGtext]
433−jsonbObjectKeys = unsafeSetOfFunction #jsonb_object_keys
440+jsonbObjectKeys = unsafeSetOfFunction
434441
435442unsafePopulateFunction
436443 :: forall fun schemas null outer commons params ty row
squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+25−22
@@ -30,7 +30,6 @@ module Squeal.PostgreSQL.Expression.SetOf
3030 , unsafeSetOfFunctionN
3131 ) where
3232
33−import Data.ByteString (ByteString)
3433import GHC.TypeLits
3534
3635import qualified Generics.SOP as SOP
@@ -48,56 +47,60 @@ A @RankNType@ for set returning functions with 1 argument.
4847type SetOfFunction fun ty setof
4948 = forall outer commons schemas params
5049 . Expression outer commons 'Ungrouped schemas params '[] ty
50+ -- ^ input
5151 -> FromClause outer commons schemas params '[fun ::: setof]
52+ -- ^ output
5253
5354-- | Escape hatch for a set returning function with 1 argument.
5455unsafeSetOfFunction
55− :: forall fun ty setof
56− . KnownSymbol fun
57− => Alias fun
58− -> SetOfFunction fun ty setof
59−unsafeSetOfFunction _fun x = UnsafeFromClause $
56+ :: forall fun ty setof. KnownSymbol fun
57+ => SetOfFunction fun ty setof -- ^ set returning function
58+unsafeSetOfFunction x = UnsafeFromClause $
6059 renderSymbol @fun <> parenthesized (renderSQL x)
6160
6261{- |
6362A @RankNType@ for set returning functions with multiple argument.
6463-}
65−type SetOfFunctionN tys setof
64+type SetOfFunctionN fun tys setof
6665 = forall outer commons schemas params
6766 . NP (Expression outer commons 'Ungrouped schemas params '[]) tys
68− -> FromClause outer commons schemas params setof
67+ -- ^ inputs
68+ -> FromClause outer commons schemas params '[fun ::: setof]
69+ -- ^ output
6970
7071-- | Escape hatch for a set returning function with multiple argument.
7172unsafeSetOfFunctionN
72− :: SOP.SListI tys
73− => ByteString
74− -> SetOfFunctionN tys setof
75−unsafeSetOfFunctionN fun xs = UnsafeFromClause $
76− fun <> parenthesized (renderCommaSeparated renderSQL xs)
73+ :: forall fun tys setof. (SOP.SListI tys, KnownSymbol fun)
74+ => SetOfFunctionN fun tys setof -- ^ set returning function
75+unsafeSetOfFunctionN xs = UnsafeFromClause $
76+ renderSymbol @fun <> parenthesized (renderCommaSeparated renderSQL xs)
7777
7878{- | @generateSeries (start *: stop)@
79+
7980Generate a series of values, from @start@ to @stop@ with a step size of one
8081-}
8182generateSeries
8283 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
83− => SetOfFunctionN '[ null ty, null ty]
84− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
85−generateSeries = unsafeSetOfFunctionN "generate_series"
84+ => SetOfFunctionN "generate_series" '[ null ty, null ty]
85+ '["generate_series" ::: null ty] -- ^ set returning function
86+generateSeries = unsafeSetOfFunctionN
8687
8788{- | @generateSeries (start :* stop *: step)@
89+
8890Generate a series of values, from @start@ to @stop@ with a step size of @step@
8991-}
9092generateSeriesStep
9193 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
92− => SetOfFunctionN '[null ty, null ty, null ty]
93− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
94−generateSeriesStep = unsafeSetOfFunctionN "generate_series"
94+ => SetOfFunctionN "generate_series" '[null ty, null ty, null ty]
95+ '["generate_series" ::: null ty] -- ^ set returning function
96+generateSeriesStep = unsafeSetOfFunctionN
9597
9698{- | @generateSeries (start :* stop *: step)@
99+
97100Generate a series of values, from @start@ to @stop@ with a step size of @step@
98101-}
99102generateSeriesTimestamp
100103 :: ty `In` '[ 'PGtimestamp, 'PGtimestamptz]
101− => SetOfFunctionN '[null ty, null ty, null 'PGinterval]
102− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
103−generateSeriesTimestamp = unsafeSetOfFunctionN "generate_series"
104+ => SetOfFunctionN "generate_series" '[null ty, null ty, null 'PGinterval]
105+ '["generate_series" ::: null ty] -- ^ set returning function
106+generateSeriesTimestamp = unsafeSetOfFunctionN