on dev help sign in
set of stuff
3113a7aEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Collection.hs+1−1
@@ -114,7 +114,7 @@ index n expr = UnsafeExpression $
114
114
115-- | Expand an array to a set of rows
115-- | Expand an array to a set of rows
116unnest :: SetOfFunction "unnest" (null ('PGvararray ty)) '["unnest" ::: ty]
116unnest :: SetOfFunction "unnest" (null ('PGvararray ty)) '["unnest" ::: ty]
117−unnest = unsafeSetOfFunction #unnest
117+unnest = unsafeSetOfFunction
118
118
119-- | A row constructor is an expression that builds a row value
119-- | A row constructor is an expression that builds a row value
120-- (also called a composite value) using values for its member fields.
120-- (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
85
85
86-- $setup
86-- $setup
87-- >>> import Squeal.PostgreSQL
87-- >>> import Squeal.PostgreSQL
88+-- >>> import Data.Aeson
88
89
89{-----------------------------------------
90{-----------------------------------------
90 -- json and jsonb support
91 -- json and jsonb support
@@ -379,58 +380,64 @@ jsonbPretty :: null 'PGjsonb :--> null 'PGtext
379jsonbPretty = unsafeFunction "jsonb_pretty"
380jsonbPretty = unsafeFunction "jsonb_pretty"
380
381
381
382
382−-- | Expands the outermost JSON object into a set of key/value pairs.
383+{- | Expands the outermost JSON object into a set of key/value pairs.
383−-- >>> import Data.Aeson
384+
384−-- >>> printSQL (select Star (from (jsonEach (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
385+>>> printSQL (select Star (from (jsonEach (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
385−-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
386+SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
387+-}
386jsonEach :: SetOfFunction "json_each" (null 'PGjson)
388jsonEach :: SetOfFunction "json_each" (null 'PGjson)
387 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
389 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
388−jsonEach = unsafeSetOfFunction #json_each
390+jsonEach = unsafeSetOfFunction
389
391
390−-- | Expands the outermost binary JSON object into a set of key/value pairs.
392+{- | Expands the outermost binary JSON object into a set of key/value pairs.
391−-- >>> import Data.Aeson
393+
392−-- >>> printSQL (select Star (from (jsonbEach (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
394+>>> printSQL (select Star (from (jsonbEach (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
393−-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
395+SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
396+-}
394jsonbEach
397jsonbEach
395 :: SetOfFunction "jsonb_each" (nullity 'PGjsonb)
398 :: SetOfFunction "jsonb_each" (nullity 'PGjsonb)
396 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
399 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
397−jsonbEach = unsafeSetOfFunction #jsonb_each
400+jsonbEach = unsafeSetOfFunction
398
401
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.
402+{- | Expands the outermost JSON object into a set of key/value pairs.
400−-- >>> import Data.Aeson
403+
401−-- >>> printSQL (select Star (from (jsonEachText (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
404+>>> printSQL (select Star (from (jsonEachText (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))))
402−-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
405+SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
406+-}
403jsonEachText
407jsonEachText
404 :: SetOfFunction "json_each_text" (null 'PGjson)
408 :: SetOfFunction "json_each_text" (null 'PGjson)
405 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
409 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
406−jsonEachText = unsafeSetOfFunction #json_each_text
410+jsonEachText = unsafeSetOfFunction
407
411
408−-- | Expands the outermost binary JSON object into a set of key/value pairs.
412+{- | Expands the outermost binary JSON object into a set of key/value pairs.
409−-- >>> import Data.Aeson
413+
410−-- >>> printSQL (select Star (from (jsonbEachText (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
414+>>> printSQL (select Star (from (jsonbEachText (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))))
411−-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
415+SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
416+-}
412jsonbEachText
417jsonbEachText
413 :: SetOfFunction "jsonb_each_text" (null 'PGjsonb)
418 :: SetOfFunction "jsonb_each_text" (null 'PGjsonb)
414 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
419 '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
415−jsonbEachText = unsafeSetOfFunction #jsonb_each_text
420+jsonbEachText = unsafeSetOfFunction
416
421
417−-- | Returns set of keys in the outermost JSON object.
422+{- | Returns set of keys in the outermost JSON object.
418−-- >>> import Data.Aeson
423+
419−-- >>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
424+>>> printSQL (jsonObjectKeys (literal (Json (object ["a" .= "foo", "b" .= "bar"]))))
420−-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
425+json_object_keys(('{"a":"foo","b":"bar"}' :: json))
426+-}
421jsonObjectKeys
427jsonObjectKeys
422 :: SetOfFunction "json_object_keys" (nullity 'PGjson)
428 :: SetOfFunction "json_object_keys" (nullity 'PGjson)
423 '["json_object_keys" ::: 'NotNull 'PGtext]
429 '["json_object_keys" ::: 'NotNull 'PGtext]
424−jsonObjectKeys = unsafeSetOfFunction #json_object_keys
430+jsonObjectKeys = unsafeSetOfFunction
425
431
426−-- | Returns set of keys in the outermost JSON object.
432+{- | Returns set of keys in the outermost JSON object.
427−-- >>> import Data.Aeson
433+
428−-- >>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
434+>>> printSQL (jsonbObjectKeys (literal (Jsonb (object ["a" .= "foo", "b" .= "bar"]))))
429−-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
435+jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
436+-}
430jsonbObjectKeys
437jsonbObjectKeys
431 :: SetOfFunction "jsonb_object_keys" (null 'PGjsonb)
438 :: SetOfFunction "jsonb_object_keys" (null 'PGjsonb)
432 '["jsonb_object_keys" ::: 'NotNull 'PGtext]
439 '["jsonb_object_keys" ::: 'NotNull 'PGtext]
433−jsonbObjectKeys = unsafeSetOfFunction #jsonb_object_keys
440+jsonbObjectKeys = unsafeSetOfFunction
434
441
435unsafePopulateFunction
442unsafePopulateFunction
436 :: forall fun schemas null outer commons params ty row
443 :: 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
30 , unsafeSetOfFunctionN
30 , unsafeSetOfFunctionN
31 ) where
31 ) where
32
32
33−import Data.ByteString (ByteString)
34import GHC.TypeLits
33import GHC.TypeLits
35
34
36import qualified Generics.SOP as SOP
35import qualified Generics.SOP as SOP
@@ -48,56 +47,60 @@ A @RankNType@ for set returning functions with 1 argument.
48type SetOfFunction fun ty setof
47type SetOfFunction fun ty setof
49 = forall outer commons schemas params
48 = forall outer commons schemas params
50 . Expression outer commons 'Ungrouped schemas params '[] ty
49 . Expression outer commons 'Ungrouped schemas params '[] ty
50+ -- ^ input
51 -> FromClause outer commons schemas params '[fun ::: setof]
51 -> FromClause outer commons schemas params '[fun ::: setof]
52+ -- ^ output
52
53
53-- | Escape hatch for a set returning function with 1 argument.
54-- | Escape hatch for a set returning function with 1 argument.
54unsafeSetOfFunction
55unsafeSetOfFunction
55− :: forall fun ty setof
56+ :: forall fun ty setof. KnownSymbol fun
56− . KnownSymbol fun
57− => Alias fun
58− -> SetOfFunction fun ty setof
57+ => SetOfFunction fun ty setof -- ^ set returning function
59−unsafeSetOfFunction _fun x = UnsafeFromClause $
58+unsafeSetOfFunction x = UnsafeFromClause $
60 renderSymbol @fun <> parenthesized (renderSQL x)
59 renderSymbol @fun <> parenthesized (renderSQL x)
61
60
62{- |
61{- |
63A @RankNType@ for set returning functions with multiple argument.
62A @RankNType@ for set returning functions with multiple argument.
64-}
63-}
65−type SetOfFunctionN tys setof
64+type SetOfFunctionN fun tys setof
66 = forall outer commons schemas params
65 = forall outer commons schemas params
67 . NP (Expression outer commons 'Ungrouped schemas params '[]) tys
66 . NP (Expression outer commons 'Ungrouped schemas params '[]) tys
67+ -- ^ inputs
68− -> FromClause outer commons schemas params setof
68+ -> FromClause outer commons schemas params '[fun ::: setof]
69+ -- ^ output
69
70
70-- | Escape hatch for a set returning function with multiple argument.
71-- | Escape hatch for a set returning function with multiple argument.
71unsafeSetOfFunctionN
72unsafeSetOfFunctionN
72− :: SOP.SListI tys
73+ :: forall fun tys setof. (SOP.SListI tys, KnownSymbol fun)
73− => ByteString
74− -> SetOfFunctionN tys setof
74+ => SetOfFunctionN fun tys setof -- ^ set returning function
75−unsafeSetOfFunctionN fun xs = UnsafeFromClause $
75+unsafeSetOfFunctionN xs = UnsafeFromClause $
76− fun <> parenthesized (renderCommaSeparated renderSQL xs)
76+ renderSymbol @fun <> parenthesized (renderCommaSeparated renderSQL xs)
77
77
78{- | @generateSeries (start *: stop)@
78{- | @generateSeries (start *: stop)@
79+
79Generate a series of values, from @start@ to @stop@ with a step size of one
80Generate a series of values, from @start@ to @stop@ with a step size of one
80-}
81-}
81generateSeries
82generateSeries
82 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
83 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
83− => SetOfFunctionN '[ null ty, null ty]
84+ => SetOfFunctionN "generate_series" '[ null ty, null ty]
84− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
85+ '["generate_series" ::: null ty] -- ^ set returning function
85−generateSeries = unsafeSetOfFunctionN "generate_series"
86+generateSeries = unsafeSetOfFunctionN
86
87
87{- | @generateSeries (start :* stop *: step)@
88{- | @generateSeries (start :* stop *: step)@
89+
88Generate a series of values, from @start@ to @stop@ with a step size of @step@
90Generate a series of values, from @start@ to @stop@ with a step size of @step@
89-}
91-}
90generateSeriesStep
92generateSeriesStep
91 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
93 :: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
92− => SetOfFunctionN '[null ty, null ty, null ty]
94+ => SetOfFunctionN "generate_series" '[null ty, null ty, null ty]
93− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
95+ '["generate_series" ::: null ty] -- ^ set returning function
94−generateSeriesStep = unsafeSetOfFunctionN "generate_series"
96+generateSeriesStep = unsafeSetOfFunctionN
95
97
96{- | @generateSeries (start :* stop *: step)@
98{- | @generateSeries (start :* stop *: step)@
99+
97Generate a series of values, from @start@ to @stop@ with a step size of @step@
100Generate a series of values, from @start@ to @stop@ with a step size of @step@
98-}
101-}
99generateSeriesTimestamp
102generateSeriesTimestamp
100 :: ty `In` '[ 'PGtimestamp, 'PGtimestamptz]
103 :: ty `In` '[ 'PGtimestamp, 'PGtimestamptz]
101− => SetOfFunctionN '[null ty, null ty, null 'PGinterval]
104+ => SetOfFunctionN "generate_series" '[null ty, null ty, null 'PGinterval]
102− '["generate_series" ::: '["generate_series" ::: '[null ty]]]
105+ '["generate_series" ::: null ty] -- ^ set returning function
103−generateSeriesTimestamp = unsafeSetOfFunctionN "generate_series"
106+generateSeriesTimestamp = unsafeSetOfFunctionN