on dev help sign in
complete 9.46
commitb515229f4e9d6e26ac1b6732e51792874c04a6a6 authorMike Ledger <eleventynine@gmail.com> dateThu 12 Jul 2018, 19:38 (8 years ago) parent2f10823
2 files changed+162−17
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+67−5 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+95−12
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+67−5
@@ -128,6 +128,11 @@ module Squeal.PostgreSQL.Expression
128128 , jsonbObjectKeys
129129 , jsonTypeof
130130 , jsonbTypeof
131+ , jsonStripNulls
132+ , jsonbStripNulls
133+ , jsonbSet
134+ , jsonbInsert
135+ , jsonbPretty
131136 -- ** Aggregation
132137 , unsafeAggregate, unsafeAggregateDistinct
133138 , sum_, sumDistinct
@@ -1171,7 +1176,7 @@ jsonbArrayLength = unsafeFunction "jsonb_array_length"
11711176
11721177-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
11731178jsonExtractPath
1174− :: SListI elems
1179+ :: SListI elems
11751180 => Expression schema relations grouping params (nullity 'PGjson)
11761181 -> NP (Expression schema relations grouping params) elems
11771182 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1180,7 +1185,7 @@ jsonExtractPath x xs =
11801185
11811186-- | Returns JSON value pointed to by path_elems (equivalent to #> operator).
11821187jsonbExtractPath
1183− :: SListI elems
1188+ :: SListI elems
11841189 => Expression schema relations grouping params (nullity 'PGjsonb)
11851190 -> NP (Expression schema relations grouping params) elems
11861191 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1190,17 +1195,17 @@ jsonbExtractPath x xs =
11901195-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
11911196-- as text.
11921197jsonExtractPathAsText
1193− :: SListI elems
1198+ :: SListI elems
11941199 => Expression schema relations grouping params (nullity 'PGjson)
11951200 -> NP (Expression schema relations grouping params) elems
1196− -> Expression schema relations grouping params (nullity 'PGjsonb)
1201+ -> Expression schema relations grouping params (nullity 'PGjson)
11971202jsonExtractPathAsText x xs =
11981203 unsafeVariadicFunction "json_extract_path_text" (x :* xs)
11991204
12001205-- | Returns JSON value pointed to by path_elems (equivalent to #> operator),
12011206-- as text.
12021207jsonbExtractPathAsText
1203− :: SListI elems
1208+ :: SListI elems
12041209 => Expression schema relations grouping params (nullity 'PGjsonb)
12051210 -> NP (Expression schema relations grouping params) elems
12061211 -> Expression schema relations grouping params (nullity 'PGjsonb)
@@ -1233,6 +1238,63 @@ jsonbTypeof
12331238 -> Expression schema relations grouping params (nullity 'PGtext)
12341239jsonbTypeof = unsafeFunction "jsonb_typeof"
12351240
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+
12361298{-----------------------------------------
12371299aggregation
12381300-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+95−12
@@ -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