on dev help sign in
more 9.46
commit1f4db597abb246fa929bd7afbf8df5c1a210afbe authorMike Ledger <eleventynine@gmail.com> dateThu 12 Jul 2018, 00:48 (8 years ago) parent0a95352
2 files changed+121
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+84 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+37
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+84
@@ -114,6 +114,14 @@ module Squeal.PostgreSQL.Expression
114114 , jsonbBuildArray
115115 , jsonBuildObject
116116 , jsonbBuildObject
117+ , jsonObject
118+ , jsonbObject
119+ , jsonZipObject
120+ , jsonbZipObject
121+ , jsonArrayLength
122+ , jsonbArrayLength
123+ , jsonEachAsComposite
124+ , jsonbEachAsComposite
117125 , jsonObjectKeys
118126 , jsonbObjectKeys
119127 -- ** Aggregation
@@ -1158,11 +1166,87 @@ jsonbArrayLength
11581166 -> Expression schema relations grouping params (nullity 'PGint4)
11591167jsonbArrayLength = unsafeFunction "jsonb_array_length"
11601168
1169+-- | Expands the outermost JSON object into a set of key/value pairs.
1170+--
1171+-- See also 'Squeal.PostgreSQL.Query.jsonEach'.
1172+jsonEachAsComposite
1173+ :: Expression schema relations grouping params (nullity 'PGjson)
1174+ -> Expression schema relations grouping params
1175+ (nullity ('PGcomposite '[ "key" ::: 'PGtext, "value" ::: 'PGjson ]))
1176+jsonEachAsComposite = unsafeFunction "json_each"
1177+
1178+-- | Expands the outermost JSON object into a set of key/value pairs.
1179+--
1180+-- See also 'Squeal.PostgreSQL.Query.jsonbEach'
1181+jsonbEachAsComposite
1182+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1183+ -> Expression schema relations grouping params
1184+ (nullity ('PGcomposite '[ "key" ::: 'PGtext, "value" ::: 'PGjsonb ]))
1185+jsonbEachAsComposite = unsafeFunction "jsonb_each"
1186+
1187+-- | Expands the outermost JSON object into a set of key/value pairs.
1188+--
1189+-- See also 'Squeal.PostgreSQL.Query.jsonEachText'.
1190+jsonEachTextAsComposite
1191+ :: Expression schema relations grouping params (nullity 'PGjson)
1192+ -> Expression schema relations grouping params
1193+ (nullity ('PGcomposite '[ "key" ::: 'PGtext, "value" ::: 'PGtext ]))
1194+jsonEachTextAsComposite = unsafeFunction "json_each_text"
1195+
1196+-- | Expands the outermost JSON object into a set of key/value pairs.
1197+--
1198+-- See also 'Squeal.PostgreSQL.Query.jsonbEachText'
1199+jsonbEachTextAsComposite
1200+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1201+ -> Expression schema relations grouping params
1202+ (nullity ('PGcomposite '[ "key" ::: 'PGtext, "value" ::: 'PGtext ]))
1203+jsonbEachTextAsComposite = unsafeFunction "jsonb_each_text"
1204+
1205+-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1206+jsonExtractPath
1207+ :: SListI elems
1208+ => Expression schema relations grouping params (nullity 'PGjson)
1209+ -> NP (Expression schema relations grouping params) elems
1210+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1211+jsonExtractPath x xs =
1212+ unsafeVariadicFunction "json_extract_path" (x :* xs)
1213+
1214+-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1215+jsonbExtractPath
1216+ :: SListI elems
1217+ => Expression schema relations grouping params (nullity 'PGjsonb)
1218+ -> NP (Expression schema relations grouping params) elems
1219+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1220+jsonbExtractPath x xs =
1221+ unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1222+
1223+-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1224+-- as text.
1225+jsonExtractPathText
1226+ :: SListI elems
1227+ => Expression schema relations grouping params (nullity 'PGjson)
1228+ -> NP (Expression schema relations grouping params) elems
1229+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1230+jsonExtractPathText x xs =
1231+ unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1232+
1233+-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1234+-- as text.
1235+jsonbExtractPathText
1236+ :: SListI elems
1237+ => Expression schema relations grouping params (nullity 'PGjsonb)
1238+ -> NP (Expression schema relations grouping params) elems
1239+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1240+jsonbExtractPathText x xs =
1241+ unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1242+
1243+-- | Returns set of keys in the outermost JSON object.
11611244jsonObjectKeys
11621245 :: Expression schema relations grouping params (nullity 'PGjson)
11631246 -> Expression schema relations grouping params (nullity 'PGtext)
11641247jsonObjectKeys = unsafeFunction "json_object_keys"
11651248
1249+-- | Returns set of keys in the outermost JSON object.
11661250jsonbObjectKeys
11671251 :: Expression schema relations grouping params (nullity 'PGjsonb)
11681252 -> Expression schema relations grouping params (nullity 'PGtext)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+37
@@ -48,6 +48,11 @@ module Squeal.PostgreSQL.Query
4848 , orderBy
4949 , limit
5050 , offset
51+ -- ** JSON table expressions
52+ , PGjson_each
53+ , PGjsonb_each
54+ , jsonEach
55+ , jsonbEach
5156 -- * From
5257 , FromClause (..)
5358 , table
@@ -623,6 +628,38 @@ offset
623628 -> TableExpression schema params relations grouping
624629offset off rels = rels {offsetClause = off : offsetClause rels}
625630
631+{-----------------------------------------
632+JSON stuff
633+-----------------------------------------}
634+
635+type PGjson_each j =
636+ '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson ] ]
637+
638+type PGjsonb_each j =
639+ '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb ] ]
640+
641+-- | Expands the outermost JSON object into a set of key/value pairs.
642+--
643+-- See also 'jsonEachAsComposite'.
644+jsonEach
645+ :: Aliased (Expression schema relations 'Ungrouped params) '(jt, nullity 'PGjson)
646+ -> TableExpression schema params (PGjson_each j) 'Ungrouped
647+jsonEach (As jexpr jname) = from . UnsafeFromClause
648+ $ renderExpression (jsonEachAsComposite jexpr)
649+ <+> "AS"
650+ <+> renderAlias jname
651+
652+-- | Expands the outermost JSON object into a set of key/value pairs.
653+--
654+-- See also 'jsonbEachAsComposite'.
655+jsonbEach
656+ :: Aliased (Expression schema relations 'Ungrouped params) '(jt, nullity 'PGjsonb)
657+ -> TableExpression schema params (PGjsonb_each j) 'Ungrouped
658+jsonbEach (As jexpr jname) = from . UnsafeFromClause
659+ $ renderExpression (jsonbEachAsComposite jexpr)
660+ <+> "AS"
661+ <+> renderAlias jname
662+
626663{-----------------------------------------
627664FROM clauses
628665-----------------------------------------}