@@ -53,6 +53,12 @@ module Squeal.PostgreSQL.Query
5353 , PGjsonb_each
5454 , jsonEach
5555 , jsonbEach
56+ , jsonEachAsText
57+ , jsonbEachAsText
58+ , jsonPopulateRecordAs
59+ , jsonbPopulateRecordAs
60+ , jsonPopulateRecordSetAs
61+ , jsonbPopulateRecordSetAs
5662 -- * From
5763 , FromClause (..)
5864 , table
@@ -632,33 +638,103 @@ offset off rels = rels {offsetClause = off : offsetClause rels}
632638JSON stuff
633639-----------------------------------------}
634640
635−type PGjson_each j =
636− '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson ] ]
641+type PGjson_each_variant val tab =
642+ '[ tab ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull val ] ]
637643
638−type PGjsonb_each j =
639− '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb ] ]
644+type PGjson_each tab = PGjson_each_variant 'PGjson tab
645+type PGjsonb_each tab = PGjson_each_variant 'PGjsonb tab
646+type PGjson_each_text tab = PGjson_each_variant 'PGtext tab
647+type PGjsonb_each_text tab = PGjson_each_variant 'PGtext tab
640648
641−-- | Expands the outermost JSON object into a set of key/value pairs.
642−--
643−-- See also 'jsonEachAsComposite'.
649+unsafeAliasedFromClauseExpression
650+ :: Aliased (Expression schema' relations' grouping' params') ty'
651+ -> FromClause schema params relations
652+unsafeAliasedFromClauseExpression aliasedExpr = UnsafeFromClause
653+ (renderAliasedAs renderExpression aliasedExpr)
654+
655+-- | Expands the outermost JSON object into a set of key/value pairs.
644656jsonEach
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'.
657+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjson)
658+ -> FromClause schema params (PGjson_each tab)
659+jsonEach (As jexpr jname) = unsafeAliasedFromClauseExpression
660+ (As (unsafeFunction "json_each" jexpr) jname)
661+
662+-- | Expands the outermost binary JSON object into a set of key/value pairs.
655663jsonbEach
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
664+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjsonb)
665+ -> FromClause schema params (PGjsonb_each tab)
666+jsonbEach (As jexpr jname) = unsafeAliasedFromClauseExpression
667+ (As (unsafeFunction "jsonb_each" jexpr) jname)
668+
669+-- | Expands the outermost JSON object into a set of key/value pairs.
670+jsonEachAsText
671+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjson)
672+ -> FromClause schema params (PGjson_each_text tab)
673+jsonEachAsText (As jexpr jname) = unsafeAliasedFromClauseExpression
674+ (As (unsafeFunction "json_each" jexpr) jname)
675+
676+-- | Expands the outermost binary JSON object into a set of key/value pairs.
677+jsonbEachAsText
678+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjsonb)
679+ -> FromClause schema params (PGjsonb_each_text tab)
680+jsonbEachAsText (As jexpr jname) = unsafeAliasedFromClauseExpression
681+ (As (unsafeFunction "jsonb_each" jexpr) jname)
682+
683+nullRow
684+ :: Has tab schema ('Table table)
685+ => Alias tab
686+ -> Expression schema relations grouping params
687+ (nullity ('PGcomposite (RelationToRowType (TableToRelation table))))
688+nullRow tab = UnsafeExpression ("null::"<+>renderAlias tab)
689+
690+-- | Expands the JSON expression to a row whose columns match the record
691+-- type defined by the given table.
692+jsonPopulateRecordAs
693+ :: (KnownSymbol alias, Has tab schema ('Table table))
694+ => Alias tab
695+ -> Expression schema '[] 'Ungrouped params (nullity 'PGjson)
696+ -> Alias alias
697+ -> FromClause schema params '[alias ::: TableToRelation table]
698+jsonPopulateRecordAs tableName expr alias = unsafeAliasedFromClauseExpression
699+ (unsafeVariadicFunction "json_populate_record"
700+ (nullRow tableName :* expr :* Nil) `As` alias)
701+
702+-- | Expands the binary JSON expression to a row whose columns match the record
703+-- type defined by the given table.
704+jsonbPopulateRecordAs
705+ :: (KnownSymbol alias, Has tab schema ('Table table))
706+ => Alias tab
707+ -> Expression schema '[] 'Ungrouped params (nullity 'PGjsonb)
708+ -> Alias alias
709+ -> FromClause schema params '[alias ::: TableToRelation table]
710+jsonbPopulateRecordAs tableName expr alias = unsafeAliasedFromClauseExpression
711+ (unsafeVariadicFunction "jsonb_populate_record"
712+ (nullRow tableName :* expr :* Nil) `As` alias)
713+
714+-- | Expands the outermost array of objects in the given binary JSON expression
715+-- to a set of rows whose columns match the record type defined by the given
716+-- table.
717+jsonbPopulateRecordSetAs
718+ :: (KnownSymbol alias, Has tab schema ('Table table))
719+ => Alias tab
720+ -> Expression schema '[] 'Ungrouped params (nullity 'PGjsonb)
721+ -> Alias alias
722+ -> FromClause schema params '[alias ::: TableToRelation table]
723+jsonbPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpression
724+ (unsafeVariadicFunction "jsonb_populate_record_set"
725+ (nullRow tableName :* expr :* Nil) `As` alias)
726+
727+-- | Expands the outermost array of objects in the given JSON expression to a
728+-- set of rows whose columns match the record type defined by the given table.
729+jsonPopulateRecordSetAs
730+ :: (KnownSymbol alias, Has tab schema ('Table table))
731+ => Alias tab
732+ -> Expression schema '[] 'Ungrouped params (nullity 'PGjson)
733+ -> Alias alias
734+ -> FromClause schema params '[alias ::: TableToRelation table]
735+jsonPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpression
736+ (unsafeVariadicFunction "json_populate_record_set"
737+ (nullRow tableName :* expr :* Nil) `As` alias)
662738
663739{-----------------------------------------
664740FROM clauses