on dev help sign in
even more 9.46
commit2f10823410525365972d894b7411a647b991a240 authorMike Ledger <eleventynine@gmail.com> dateThu 12 Jul 2018, 13:04 (8 years ago) parent1f4db59
3 files changed+129−66
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+24−43 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+99−23 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+24−43
@@ -120,10 +120,14 @@ module Squeal.PostgreSQL.Expression
120120 , jsonbZipObject
121121 , jsonArrayLength
122122 , jsonbArrayLength
123− , jsonEachAsComposite
124− , jsonbEachAsComposite
123+ , jsonExtractPath
124+ , jsonbExtractPath
125+ , jsonExtractPathAsText
126+ , jsonbExtractPathAsText
125127 , jsonObjectKeys
126128 , jsonbObjectKeys
129+ , jsonTypeof
130+ , jsonbTypeof
127131 -- ** Aggregation
128132 , unsafeAggregate, unsafeAggregateDistinct
129133 , sum_, sumDistinct
@@ -181,7 +185,6 @@ import Data.Semigroup
181185import Data.Ratio
182186import Data.String
183187import Generics.SOP hiding (from)
184−import GHC.Exts (Constraint)
185188import GHC.OverloadedLabels
186189import GHC.TypeLits
187190import Prelude hiding (id, (.))
@@ -1166,42 +1169,6 @@ jsonbArrayLength
11661169 -> Expression schema relations grouping params (nullity 'PGint4)
11671170jsonbArrayLength = unsafeFunction "jsonb_array_length"
11681171
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−
12051172-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
12061173jsonExtractPath
12071174 :: SListI elems
@@ -1222,22 +1189,22 @@ jsonbExtractPath x xs =
12221189
12231190-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
12241191-- as text.
1225−jsonExtractPathText
1192+jsonExtractPathAsText
12261193 :: SListI elems
12271194 => Expression schema relations grouping params (nullity 'PGjson)
12281195 -> NP (Expression schema relations grouping params) elems
12291196 -> Expression schema relations grouping params (nullity 'PGjsonb)
1230−jsonExtractPathText x xs =
1197+jsonExtractPathAsText x xs =
12311198 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
12321199
12331200-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
12341201-- as text.
1235−jsonbExtractPathText
1202+jsonbExtractPathAsText
12361203 :: SListI elems
12371204 => Expression schema relations grouping params (nullity 'PGjsonb)
12381205 -> NP (Expression schema relations grouping params) elems
12391206 -> Expression schema relations grouping params (nullity 'PGjsonb)
1240−jsonbExtractPathText x xs =
1207+jsonbExtractPathAsText x xs =
12411208 unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
12421209
12431210-- | Returns set of keys in the outermost JSON object.
@@ -1252,6 +1219,20 @@ jsonbObjectKeys
12521219 -> Expression schema relations grouping params (nullity 'PGtext)
12531220jsonbObjectKeys = unsafeFunction "jsonb_object_keys"
12541221
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+
12551236{-----------------------------------------
12561237aggregation
12571238-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+99−23
@@ -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
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
@@ -103,6 +103,7 @@ module Squeal.PostgreSQL.Schema
103103 , ColumnsToRelation
104104 , TableToColumns
105105 , TableToRelation
106+ , RelationToRowType
106107 , ConstraintInvolves
107108 , DropIfConstraintsInvolve
108109 -- ** JSON support
@@ -602,6 +603,11 @@ type family NullifyRelations (tables :: RelationsType) :: RelationsType where
602603 NullifyRelations (table ::: columns ': tables) =
603604 table ::: NullifyRelation columns ': NullifyRelations tables
604605
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+
605611-- | `Join` is simply promoted `++` and is used in @JOIN@s in
606612-- `Squeal.PostgreSQL.Query.FromClause`s.
607613type family Join xs ys where