on dev help sign in
complete 9.46
b515229Mike Ledger
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+67−5
@@ -128,6 +128,11 @@ module Squeal.PostgreSQL.Expression
128 , jsonbObjectKeys
128 , jsonbObjectKeys
129 , jsonTypeof
129 , jsonTypeof
130 , jsonbTypeof
130 , jsonbTypeof
131+ , jsonStripNulls
132+ , jsonbStripNulls
133+ , jsonbSet
134+ , jsonbInsert
135+ , jsonbPretty
131 -- ** Aggregation
136 -- ** Aggregation
132 , unsafeAggregate, unsafeAggregateDistinct
137 , unsafeAggregate, unsafeAggregateDistinct
133 , sum_, sumDistinct
138 , sum_, sumDistinct
@@ -1171,7 +1176,7 @@ jsonbArrayLength = unsafeFunction "jsonb_array_length"
1171
1176
1172-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1177-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1173jsonExtractPath
1178jsonExtractPath
1174− :: SListI elems
1179+ :: SListI elems
1175 => Expression schema relations grouping params (nullity 'PGjson)
1180 => Expression schema relations grouping params (nullity 'PGjson)
1176 -> NP (Expression schema relations grouping params) elems
1181 -> NP (Expression schema relations grouping params) elems
1177 -> Expression schema relations grouping params (nullity 'PGjsonb)
1182 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1180,7 +1185,7 @@ jsonExtractPath x xs =
1180
1185
1181-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1186-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
1182jsonbExtractPath
1187jsonbExtractPath
1183− :: SListI elems
1188+ :: SListI elems
1184 => Expression schema relations grouping params (nullity 'PGjsonb)
1189 => Expression schema relations grouping params (nullity 'PGjsonb)
1185 -> NP (Expression schema relations grouping params) elems
1190 -> NP (Expression schema relations grouping params) elems
1186 -> Expression schema relations grouping params (nullity 'PGjsonb)
1191 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1190,17 +1195,17 @@ jsonbExtractPath x xs =
1190-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1195-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1191-- as text.
1196-- as text.
1192jsonExtractPathAsText
1197jsonExtractPathAsText
1193− :: SListI elems
1198+ :: SListI elems
1194 => Expression schema relations grouping params (nullity 'PGjson)
1199 => Expression schema relations grouping params (nullity 'PGjson)
1195 -> NP (Expression schema relations grouping params) elems
1200 -> NP (Expression schema relations grouping params) elems
1196− -> Expression schema relations grouping params (nullity 'PGjsonb)
1201+ -> Expression schema relations grouping params (nullity 'PGjson)
1197jsonExtractPathAsText x xs =
1202jsonExtractPathAsText x xs =
1198 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1203 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1199
1204
1200-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1205-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
1201-- as text.
1206-- as text.
1202jsonbExtractPathAsText
1207jsonbExtractPathAsText
1203− :: SListI elems
1208+ :: SListI elems
1204 => Expression schema relations grouping params (nullity 'PGjsonb)
1209 => Expression schema relations grouping params (nullity 'PGjsonb)
1205 -> NP (Expression schema relations grouping params) elems
1210 -> NP (Expression schema relations grouping params) elems
1206 -> Expression schema relations grouping params (nullity 'PGjsonb)
1211 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1233,6 +1238,63 @@ jsonbTypeof
1233 -> Expression schema relations grouping params (nullity 'PGtext)
1238 -> Expression schema relations grouping params (nullity 'PGtext)
1234jsonbTypeof = unsafeFunction "jsonb_typeof"
1239jsonbTypeof = unsafeFunction "jsonb_typeof"
1235
1240
1241+-- | Returns its argument with all object fields that have null values omitted.
1242+-- Other null values are untouched.
1243+jsonStripNulls
1244+ :: Expression schema relations grouping params (nullity 'PGjson)
1245+ -> Expression schema relations grouping params (nullity 'PGjson)
1246+jsonStripNulls = unsafeFunction "json_strip_nulls"
1247+
1248+-- | Returns its argument with all object fields that have null values omitted.
1249+-- Other null values are untouched.
1250+jsonbStripNulls
1251+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1252+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1253+jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1254+
1255+-- | @ jsonbSet target path new_value create_missing @
1256+--
1257+-- Returns target with the section designated by path replaced by new_value,
1258+-- or with new_value added if create_missing is true ( default is true) and the
1259+-- item designated by path does not exist. As with the path orientated
1260+-- operators, negative integers that appear in path count from the end of JSON
1261+-- arrays.
1262+jsonbSet
1263+ :: PGtextArray "jsonbSet" arr
1264+ => Expression schema relations grouping params (nullity 'PGjsonb)
1265+ -> Expression schema relations grouping params (nullity arr)
1266+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1267+ -> Maybe (Expression schema relations grouping params (nullity 'PGbool))
1268+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1269+jsonbSet tgt path val createMissing = case createMissing of
1270+ Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1271+ Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1272+
1273+-- | @ jsonbInsert target path new_value insert_after @
1274+--
1275+-- Returns target with new_value inserted. If target section designated by
1276+-- path is in a JSONB array, new_value will be inserted before target or after
1277+-- if insert_after is true (default is false). If target section designated by
1278+-- path is in JSONB object, new_value will be inserted only if target does not
1279+-- exist. As with the path orientated operators, negative integers that appear
1280+-- in path count from the end of JSON arrays.
1281+jsonbInsert
1282+ :: PGtextArray "jsonbInsert" arr
1283+ => Expression schema relations grouping params (nullity 'PGjsonb)
1284+ -> Expression schema relations grouping params (nullity arr)
1285+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1286+ -> Maybe (Expression schema relations grouping params (nullity 'PGbool))
1287+ -> Expression schema relations grouping params (nullity 'PGjsonb)
1288+jsonbInsert tgt path val insertAfter = case insertAfter of
1289+ Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1290+ Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1291+
1292+-- | Returns its argument as indented JSON text.
1293+jsonbPretty
1294+ :: Expression schema relations grouping params (nullity 'PGjsonb)
1295+ -> Expression schema relations grouping params (nullity 'PGtext)
1296+jsonbPretty = unsafeFunction "jsonb_pretty"
1297+
1236{-----------------------------------------
1298{-----------------------------------------
1237aggregation
1299aggregation
1238-----------------------------------------}
1300-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+95−12
@@ -9,7 +9,8 @@ Squeal queries.
9-}
9-}
10
10
11{-# LANGUAGE
11{-# LANGUAGE
12+ ConstraintKinds
12− DeriveGeneric
13+ , DeriveGeneric
13 , FlexibleContexts
14 , FlexibleContexts
14 , GADTs
15 , GADTs
15 , GeneralizedNewtypeDeriving
16 , GeneralizedNewtypeDeriving
@@ -18,6 +19,7 @@ Squeal queries.
18 , StandaloneDeriving
19 , StandaloneDeriving
19 , TypeInType
20 , TypeInType
20 , TypeOperators
21 , TypeOperators
22+ , RankNTypes
21#-}
23#-}
22
24
23module Squeal.PostgreSQL.Query
25module Squeal.PostgreSQL.Query
@@ -59,6 +61,10 @@ module Squeal.PostgreSQL.Query
59 , jsonbPopulateRecordAs
61 , jsonbPopulateRecordAs
60 , jsonPopulateRecordSetAs
62 , jsonPopulateRecordSetAs
61 , jsonbPopulateRecordSetAs
63 , jsonbPopulateRecordSetAs
64+ , jsonToRecordAs
65+ , jsonbToRecordAs
66+ , jsonToRecordSetAs
67+ , jsonbToRecordSetAs
62 -- * From
68 -- * From
63 , FromClause (..)
69 , FromClause (..)
64 , table
70 , table
@@ -711,6 +717,18 @@ jsonbPopulateRecordAs tableName expr alias = unsafeAliasedFromClauseExpression
711 (unsafeVariadicFunction "jsonb_populate_record"
717 (unsafeVariadicFunction "jsonb_populate_record"
712 (nullRow tableName :* expr :* Nil) `As` alias)
718 (nullRow tableName :* expr :* Nil) `As` alias)
713
719
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+
714-- | Expands the outermost array of objects in the given binary JSON expression
732-- | 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
733-- to a set of rows whose columns match the record type defined by the given
716-- table.
734-- table.
@@ -724,17 +742,82 @@ jsonbPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpressio
724 (unsafeVariadicFunction "jsonb_populate_record_set"
742 (unsafeVariadicFunction "jsonb_populate_record_set"
725 (nullRow tableName :* expr :* Nil) `As` alias)
743 (nullRow tableName :* expr :* Nil) `As` alias)
726
744
727−-- | Expands the outermost array of objects in the given JSON expression to a
745+renderTableTypeExpression
728−-- set of rows whose columns match the record type defined by the given table.
746+ :: All Top types -- ^ always satisfiable
729−jsonPopulateRecordSetAs
747+ => Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
730− :: (KnownSymbol alias, Has tab schema ('Table table))
748+ -> ByteString
731− => Alias tab
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
732− -> Expression schema '[] 'Ungrouped params (nullity 'PGjson)
814+ => Expression schema '[] 'Ungrouped params (nullity 'PGjsonb)
733− -> Alias alias
815+ -> Aliased (NP (Aliased (TypeExpression schema))) (tab ::: types)
734− -> FromClause schema params '[alias ::: TableToRelation table]
816+ -> FromClause schema params '[tab ::: types]
735−jsonPopulateRecordSetAs tableName expr alias = unsafeAliasedFromClauseExpression
817+jsonbToRecordSetAs expr types =
736− (unsafeVariadicFunction "json_populate_record_set"
818+ unsafeTableAliasedFromClauseExpression
737− (nullRow tableName :* expr :* Nil) `As` alias)
819+ (unsafeFunction "jsonb_to_recordset" expr)
820+ types
738
821
739{-----------------------------------------
822{-----------------------------------------
740FROM clauses
823FROM clauses