on dev help sign in
even more 9.46
2f10823Mike Ledger
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+24−43
@@ -120,10 +120,14 @@ module Squeal.PostgreSQL.Expression
120 , jsonbZipObject
120 , jsonbZipObject
121 , jsonArrayLength
121 , jsonArrayLength
122 , jsonbArrayLength
122 , jsonbArrayLength
123− , jsonEachAsComposite
123+ , jsonExtractPath
124− , jsonbEachAsComposite
124+ , jsonbExtractPath
125+ , jsonExtractPathAsText
126+ , jsonbExtractPathAsText
125 , jsonObjectKeys
127 , jsonObjectKeys
126 , jsonbObjectKeys
128 , jsonbObjectKeys
129+ , jsonTypeof
130+ , jsonbTypeof
127 -- ** Aggregation
131 -- ** Aggregation
128 , unsafeAggregate, unsafeAggregateDistinct
132 , unsafeAggregate, unsafeAggregateDistinct
129 , sum_, sumDistinct
133 , sum_, sumDistinct
@@ -181,7 +185,6 @@ import Data.Semigroup
181import Data.Ratio
185import Data.Ratio
182import Data.String
186import Data.String
183import Generics.SOP hiding (from)
187import Generics.SOP hiding (from)
184−import GHC.Exts (Constraint)
185import GHC.OverloadedLabels
188import GHC.OverloadedLabels
186import GHC.TypeLits
189import GHC.TypeLits
187import Prelude hiding (id, (.))
190import Prelude hiding (id, (.))
@@ -1166,42 +1169,6 @@ jsonbArrayLength
1166 -> Expression schema relations grouping params (nullity 'PGint4)
1169 -> Expression schema relations grouping params (nullity 'PGint4)
1167jsonbArrayLength = unsafeFunction "jsonb_array_length"
1170jsonbArrayLength = unsafeFunction "jsonb_array_length"
1168
1171
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).
1172-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1206jsonExtractPath
1173jsonExtractPath
1207 :: SListI elems
1174 :: SListI elems
@@ -1222,22 +1189,22 @@ jsonbExtractPath x xs =
1222
1189
1223-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1190-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1224-- as text.
1191-- as text.
1225−jsonExtractPathText
1192+jsonExtractPathAsText
1226 :: SListI elems
1193 :: SListI elems
1227 => Expression schema relations grouping params (nullity 'PGjson)
1194 => Expression schema relations grouping params (nullity 'PGjson)
1228 -> NP (Expression schema relations grouping params) elems
1195 -> NP (Expression schema relations grouping params) elems
1229 -> Expression schema relations grouping params (nullity 'PGjsonb)
1196 -> Expression schema relations grouping params (nullity 'PGjsonb)
1230−jsonExtractPathText x xs =
1197+jsonExtractPathAsText x xs =
1231 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1198 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1232
1199
1233-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1200-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1234-- as text.
1201-- as text.
1235−jsonbExtractPathText
1202+jsonbExtractPathAsText
1236 :: SListI elems
1203 :: SListI elems
1237 => Expression schema relations grouping params (nullity 'PGjsonb)
1204 => Expression schema relations grouping params (nullity 'PGjsonb)
1238 -> NP (Expression schema relations grouping params) elems
1205 -> NP (Expression schema relations grouping params) elems
1239 -> Expression schema relations grouping params (nullity 'PGjsonb)
1206 -> Expression schema relations grouping params (nullity 'PGjsonb)
1240−jsonbExtractPathText x xs =
1207+jsonbExtractPathAsText x xs =
1241 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1208 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1242
1209
1243-- | Returns set of keys in the outermost JSON object.
1210-- | Returns set of keys in the outermost JSON object.
@@ -1252,6 +1219,20 @@ jsonbObjectKeys
1252 -> Expression schema relations grouping params (nullity 'PGtext)
1219 -> Expression schema relations grouping params (nullity 'PGtext)
1253jsonbObjectKeys = unsafeFunction "jsonb_object_keys"
1220jsonbObjectKeys = unsafeFunction "jsonb_object_keys"
1254
1221
1222+-- | Returns the type of the outermost JSON value as a text string. Possible
1223+-- types are object, array, string, number, boolean, and null.
1224+jsonTypeof
1225+ :: Expression schema relations grouping params (nullity 'PGjson)
1226+ -> Expression schema relations grouping params (nullity 'PGtext)
1227+jsonTypeof = unsafeFunction "json_typeof"
1228+
1229+-- | Returns the type of the outermost binary JSON value as a text string.
1230+-- Possible types are object, array, string, number, boolean, and null.
1231+jsonbTypeof
1232+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1233+ -> Expression schema relations grouping params (nullity 'PGtext)
1234+jsonbTypeof = unsafeFunction "jsonb_typeof"
1235+
1255{-----------------------------------------
1236{-----------------------------------------
1256aggregation
1237aggregation
1257-----------------------------------------}
1238-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+99−23
@@ -53,6 +53,12 @@ module Squeal.PostgreSQL.Query
53 , PGjsonb_each
53 , PGjsonb_each
54 , jsonEach
54 , jsonEach
55 , jsonbEach
55 , jsonbEach
56+ , jsonEachAsText
57+ , jsonbEachAsText
58+ , jsonPopulateRecordAs
59+ , jsonbPopulateRecordAs
60+ , jsonPopulateRecordSetAs
61+ , jsonbPopulateRecordSetAs
56 -- * From
62 -- * From
57 , FromClause (..)
63 , FromClause (..)
58 , table
64 , table
@@ -632,33 +638,103 @@ offset off rels = rels {offsetClause = off : offsetClause rels}
632JSON stuff
638JSON stuff
633-----------------------------------------}
639-----------------------------------------}
634
640
635−type PGjson_each j =
641+type PGjson_each_variant val tab =
636− '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson ] ]
642+ '[ tab ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull val ] ]
637
643
638−type PGjsonb_each j =
644+type PGjson_each tab = PGjson_each_variant 'PGjson tab
639− '[ j ::: '[ "key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb ] ]
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
640
648
649+unsafeAliasedFromClauseExpression
650+ :: Aliased (Expression schema' relations' grouping' params') ty'
651+ -> FromClause schema params relations
652+unsafeAliasedFromClauseExpression aliasedExpr = UnsafeFromClause
653+ (renderAliasedAs renderExpression aliasedExpr)
654+
641−-- | Expands the outermost JSON object into a set of key/value pairs.
655+-- | Expands the outermost JSON object into a set of key/value pairs.
642−--
643−-- See also 'jsonEachAsComposite'.
644jsonEach
656jsonEach
645− :: Aliased (Expression schema relations 'Ungrouped params) '(jt, nullity 'PGjson)
657+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjson)
646− -> TableExpression schema params (PGjson_each j) 'Ungrouped
658+ -> FromClause schema params (PGjson_each tab)
647−jsonEach (As jexpr jname) = from . UnsafeFromClause
659+jsonEach (As jexpr jname) = unsafeAliasedFromClauseExpression
648− $ renderExpression (jsonEachAsComposite jexpr)
660+ (As (unsafeFunction "json_each" jexpr) jname)
649− <+> "AS"
661+
650− <+> renderAlias jname
651−
652−-- | Expands the outermost JSON object into a set of key/value pairs.
662+-- | Expands the outermost binary JSON object into a set of key/value pairs.
653−--
654−-- See also 'jsonbEachAsComposite'.
655jsonbEach
663jsonbEach
656− :: Aliased (Expression schema relations 'Ungrouped params) '(jt, nullity 'PGjsonb)
664+ :: Aliased (Expression schema relations 'Ungrouped params) '(tab, nullity 'PGjsonb)
657− -> TableExpression schema params (PGjsonb_each j) 'Ungrouped
665+ -> FromClause schema params (PGjsonb_each tab)
658−jsonbEach (As jexpr jname) = from . UnsafeFromClause
666+jsonbEach (As jexpr jname) = unsafeAliasedFromClauseExpression
659− $ renderExpression (jsonbEachAsComposite jexpr)
667+ (As (unsafeFunction "jsonb_each" jexpr) jname)
660− <+> "AS"
668+
661− <+> renderAlias jname
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)
662
738
663{-----------------------------------------
739{-----------------------------------------
664FROM clauses
740FROM clauses
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
@@ -103,6 +103,7 @@ module Squeal.PostgreSQL.Schema
103 , ColumnsToRelation
103 , ColumnsToRelation
104 , TableToColumns
104 , TableToColumns
105 , TableToRelation
105 , TableToRelation
106+ , RelationToRowType
106 , ConstraintInvolves
107 , ConstraintInvolves
107 , DropIfConstraintsInvolve
108 , DropIfConstraintsInvolve
108 -- ** JSON support
109 -- ** JSON support
@@ -602,6 +603,11 @@ type family NullifyRelations (tables :: RelationsType) :: RelationsType where
602 NullifyRelations (table ::: columns ': tables) =
603 NullifyRelations (table ::: columns ': tables) =
603 table ::: NullifyRelation columns ': NullifyRelations tables
604 table ::: NullifyRelation columns ': NullifyRelations tables
604
605
606+-- | 'RelationToRowType' drops the nullity constraints of its argument relations.
607+type family RelationToRowType (tables :: RelationType) :: [(Symbol, PGType)] where
608+ RelationToRowType (nullity x : xs) = x : RelationToRowType xs
609+ RelationToRowType '[] = '[]
610+
605-- | `Join` is simply promoted `++` and is used in @JOIN@s in
611-- | `Join` is simply promoted `++` and is used in @JOIN@s in
606-- `Squeal.PostgreSQL.Query.FromClause`s.
612-- `Squeal.PostgreSQL.Query.FromClause`s.
607type family Join xs ys where
613type family Join xs ys where