@@ -9,7 +9,8 @@ Squeal queries.
99-}
1010
1111{-# LANGUAGE
12− DeriveGeneric
12+ ConstraintKinds
13+ , DeriveGeneric
1314 , FlexibleContexts
1415 , GADTs
1516 , GeneralizedNewtypeDeriving
@@ -18,6 +19,7 @@ Squeal queries.
1819 , StandaloneDeriving
1920 , TypeInType
2021 , TypeOperators
22+ , RankNTypes
2123#-}
2224
2325module Squeal.PostgreSQL.Query
@@ -59,6 +61,10 @@ module Squeal.PostgreSQL.Query
5961 , jsonbPopulateRecordAs
6062 , jsonPopulateRecordSetAs
6163 , jsonbPopulateRecordSetAs
64+ , jsonToRecordAs
65+ , jsonbToRecordAs
66+ , jsonToRecordSetAs
67+ , jsonbToRecordSetAs
6268 -- * From
6369 , FromClause (..)
6470 , table
@@ -711,6 +717,18 @@ jsonbPopulateRecordAs tableName expr alias = unsafeAliasedFromClauseExpression
711717 (unsafeVariadicFunction "jsonb_populate_record"
712718 (nullRow tableName :* expr :* Nil) `As` alias)
713719
720+-- | Expands the outermost array of objects in the given JSON expression to a
721+-- set of rows whose columns match the record type defined by the given table.
722+jsonPopulateRecordSetAs
723+ :: (KnownSymbol alias, Has tab schema ('Table table))
724+ => Alias tab
725+ -> Expression schema '[] 'Ungrouped params (nullity 'PGjson)
726+ -> Alias alias
727+ -> FromClause schema params '[alias ::: TableToRelation table]
728+jsonPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpression
729+ (unsafeVariadicFunction "json_populate_record_set"
730+ (nullRow tableName :* expr :* Nil) `As` alias)
731+
714732-- | Expands the outermost array of objects in the given binary JSON expression
715733-- to a set of rows whose columns match the record type defined by the given
716734-- table.
@@ -724,17 +742,82 @@ jsonbPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpressio
724742 (unsafeVariadicFunction "jsonb_populate_record_set"
725743 (nullRow tableName :* expr :* Nil) `As` alias)
726744
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)
745+renderTableTypeExpression
746+ :: All Top types -- ^ always satisfiable
747+ => Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
748+ -> ByteString
749+renderTableTypeExpression (hc `As` tab) =
750+ (renderAlias tab <>)
751+ . parenthesized
752+ . commaSeparated
753+ . flip appEndo []
754+ . hcfoldMap
755+ (Proxy :: Proxy Top)
756+ (\(ty `As` name) -> Endo
757+ ((renderAlias name <+> renderTypeExpression ty):))
758+ $ hc
759+
760+unsafeTableAliasedFromClauseExpression
761+ :: All Top types
762+ => Expression schema' relations' grouping' params' ty'
763+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
764+ -> FromClause schema params '[alias ::: types]
765+unsafeTableAliasedFromClauseExpression expr types = UnsafeFromClause
766+ (renderExpression expr
767+ <+> "AS"
768+ <+> renderTableTypeExpression types)
769+
770+-- | Builds an arbitrary record from a JSON object. As with all functions
771+-- returning record, the caller must explicitly define the structure of the
772+-- record with an AS clause.
773+jsonToRecordAs
774+ :: All Top types
775+ => Expression schema '[] 'Ungrouped params (nullity 'PGjson)
776+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
777+ -> FromClause schema params '[tab ::: types]
778+jsonToRecordAs expr types =
779+ unsafeTableAliasedFromClauseExpression
780+ (unsafeFunction "json_to_record" expr)
781+ types
782+
783+-- | Builds an arbitrary record from a binary JSON object. As with all functions
784+-- returning record, the caller must explicitly define the structure of the
785+-- record with an AS clause.
786+jsonbToRecordAs
787+ :: All Top types
788+ => Expression schema '[] 'Ungrouped params (nullity 'PGjsonb)
789+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
790+ -> FromClause schema params '[tab ::: types]
791+jsonbToRecordAs expr types =
792+ unsafeTableAliasedFromClauseExpression
793+ (unsafeFunction "jsonb_to_record" expr)
794+ types
795+
796+-- | Builds an arbitrary set of records from a JSON array of objects (see note
797+-- below). As with all functions returning record, the caller must explicitly
798+-- define the structure of the record with an AS clause.
799+jsonToRecordSetAs
800+ :: All Top types
801+ => Expression schema '[] 'Ungrouped params (nullity 'PGjson)
802+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
803+ -> FromClause schema params '[tab ::: types]
804+jsonToRecordSetAs expr types =
805+ unsafeTableAliasedFromClauseExpression
806+ (unsafeFunction "json_to_recordset" expr)
807+ types
808+
809+-- | Builds an arbitrary set of records from a binary JSON array of objects (see
810+-- note below). As with all functions returning record, the caller must
811+-- explicitly define the structure of the record with an AS clause.
812+jsonbToRecordSetAs
813+ :: All Top types
814+ => Expression schema '[] 'Ungrouped params (nullity 'PGjsonb)
815+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
816+ -> FromClause schema params '[tab ::: types]
817+jsonbToRecordSetAs expr types =
818+ unsafeTableAliasedFromClauseExpression
819+ (unsafeFunction "jsonb_to_recordset" expr)
820+ types
738821
739822{-----------------------------------------
740823FROM clauses