on dev help sign in
more 9.46
1f4db59Mike Ledger
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+84
@@ -114,6 +114,14 @@ module Squeal.PostgreSQL.Expression
114 , jsonbBuildArray
114 , jsonbBuildArray
115 , jsonBuildObject
115 , jsonBuildObject
116 , jsonbBuildObject
116 , jsonbBuildObject
117+ , jsonObject
118+ , jsonbObject
119+ , jsonZipObject
120+ , jsonbZipObject
121+ , jsonArrayLength
122+ , jsonbArrayLength
123+ , jsonEachAsComposite
124+ , jsonbEachAsComposite
117 , jsonObjectKeys
125 , jsonObjectKeys
118 , jsonbObjectKeys
126 , jsonbObjectKeys
119 -- ** Aggregation
127 -- ** Aggregation
@@ -1158,11 +1166,87 @@ jsonbArrayLength
1158 -> Expression schema relations grouping params (nullity 'PGint4)
1166 -> Expression schema relations grouping params (nullity 'PGint4)
1159jsonbArrayLength = unsafeFunction "jsonb_array_length"
1167jsonbArrayLength = unsafeFunction "jsonb_array_length"
1160
1168
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.
1161jsonObjectKeys
1244jsonObjectKeys
1162 :: Expression schema relations grouping params (nullity 'PGjson)
1245 :: Expression schema relations grouping params (nullity 'PGjson)
1163 -> Expression schema relations grouping params (nullity 'PGtext)
1246 -> Expression schema relations grouping params (nullity 'PGtext)
1164jsonObjectKeys = unsafeFunction "json_object_keys"
1247jsonObjectKeys = unsafeFunction "json_object_keys"
1165
1248
1249+-- | Returns set of keys in the outermost JSON object.
1166jsonbObjectKeys
1250jsonbObjectKeys
1167 :: Expression schema relations grouping params (nullity 'PGjsonb)
1251 :: Expression schema relations grouping params (nullity 'PGjsonb)
1168 -> Expression schema relations grouping params (nullity 'PGtext)
1252 -> Expression schema relations grouping params (nullity 'PGtext)
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+37
@@ -48,6 +48,11 @@ module Squeal.PostgreSQL.Query
48 , orderBy
48 , orderBy
49 , limit
49 , limit
50 , offset
50 , offset
51+ -- ** JSON table expressions
52+ , PGjson_each
53+ , PGjsonb_each
54+ , jsonEach
55+ , jsonbEach
51 -- * From
56 -- * From
52 , FromClause (..)
57 , FromClause (..)
53 , table
58 , table
@@ -623,6 +628,38 @@ offset
623 -> TableExpression schema params relations grouping
628 -> TableExpression schema params relations grouping
624offset off rels = rels {offsetClause = off : offsetClause rels}
629offset off rels = rels {offsetClause = off : offsetClause rels}
625
630
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+
626{-----------------------------------------
663{-----------------------------------------
627FROM clauses
664FROM clauses
628-----------------------------------------}
665-----------------------------------------}