on dev help sign in
change set of functions from queries to from clauses
7669916Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+78−60
@@ -17,6 +17,7 @@ Squeal queries.
17 , GeneralizedNewtypeDeriving
17 , GeneralizedNewtypeDeriving
18 , LambdaCase
18 , LambdaCase
19 , MultiParamTypeClasses
19 , MultiParamTypeClasses
20+ , OverloadedLabels
20 , OverloadedStrings
21 , OverloadedStrings
21 , QuantifiedConstraints
22 , QuantifiedConstraints
22 , ScopedTypeVariables
23 , ScopedTypeVariables
@@ -57,8 +58,8 @@ module Squeal.PostgreSQL.Query
57 -- ** Json
58 -- ** Json
58 , jsonEach
59 , jsonEach
59 , jsonbEach
60 , jsonbEach
60− , jsonEachAsText
61+ , jsonEachText
61− , jsonbEachAsText
62+ , jsonbEachText
62 , jsonObjectKeys
63 , jsonObjectKeys
63 , jsonbObjectKeys
64 , jsonbObjectKeys
64 , jsonPopulateRecord
65 , jsonPopulateRecord
@@ -717,89 +718,108 @@ JSON stuff
717unsafeSetOfFunction
718unsafeSetOfFunction
718 :: ByteString
719 :: ByteString
719 -> Expression outer 'Ungrouped commons schemas params '[] ty
720 -> Expression outer 'Ungrouped commons schemas params '[] ty
720− -> Query outer commons schemas params row
721+ -> FromClause outer commons schemas params from
721−unsafeSetOfFunction fun expr = UnsafeQuery $
722+unsafeSetOfFunction fun expr = UnsafeFromClause $
722− "SELECT * FROM " <> fun <> "(" <> renderSQL expr <> ")"
723+ fun <> "(" <> renderSQL expr <> ")"
723
724
724-- | Expand an array to a set of rows
725-- | Expand an array to a set of rows
725unnest
726unnest
726 :: Expression outer 'Ungrouped commons schemas params '[] (nullity ('PGvararray ty))
727 :: Expression outer 'Ungrouped commons schemas params '[] (nullity ('PGvararray ty))
727− -> Query outer commons schemas params '[ "unnest" ::: ty ]
728+ -> FromClause outer commons schemas params '["unnest" ::: '["unnest" ::: ty]]
728unnest = unsafeSetOfFunction "unnest"
729unnest = unsafeSetOfFunction "unnest"
729
730
730-- | Expands the outermost JSON object into a set of key/value pairs.
731-- | Expands the outermost JSON object into a set of key/value pairs.
732+-- >>> import Data.Aeson
733+-- >>> printSQL (select Star (from (jsonEach (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
734+-- SELECT * FROM json_each(('{"a":"foo","b":"bar"}' :: json))
731jsonEach
735jsonEach
732− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
736+ :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
733− -> Query outer commons schemas params
737+ -> FromClause outer commons schemas params
734− '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]
738+ '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
735jsonEach = unsafeSetOfFunction "json_each"
739jsonEach = unsafeSetOfFunction "json_each"
736
740
737-- | Expands the outermost binary JSON object into a set of key/value pairs.
741-- | Expands the outermost binary JSON object into a set of key/value pairs.
742+-- >>> import Data.Aeson
743+-- >>> printSQL (select Star (from (jsonbEach (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
744+-- SELECT * FROM jsonb_each(('{"a":"foo","b":"bar"}' :: jsonb))
738jsonbEach
745jsonbEach
739− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
746+ :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
740− -> Query outer commons schemas params
747+ -> FromClause outer commons schemas params
741− '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjsonb]
748+ '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGjson]]
742jsonbEach = unsafeSetOfFunction "jsonb_each"
749jsonbEach = unsafeSetOfFunction "jsonb_each"
743
750
744−-- | Expands the outermost JSON object into a set of key/value pairs.
751+-- | Expands the outermost JSON object into a set of key/value pairs.-- | Expands the outermost binary JSON object into a set of key/value pairs.
745−jsonEachAsText
752+-- >>> import Data.Aeson
753+-- >>> printSQL (select Star (from (jsonEachText (jsonLit (object ["a" .= "foo", "b" .= "bar"])))))
754+-- SELECT * FROM json_each_text(('{"a":"foo","b":"bar"}' :: json))
755+jsonEachText
746− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
756+ :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
747− -> Query outer commons schemas params
757+ -> FromClause outer commons schemas params
748− '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
758+ '["json_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
749−jsonEachAsText = unsafeSetOfFunction "json_each_text"
759+jsonEachText = unsafeSetOfFunction "json_each_text"
750
760
751-- | Expands the outermost binary JSON object into a set of key/value pairs.
761-- | Expands the outermost binary JSON object into a set of key/value pairs.
752−jsonbEachAsText
762+-- >>> import Data.Aeson
763+-- >>> printSQL (select Star (from (jsonbEachText (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))))
764+-- SELECT * FROM jsonb_each_text(('{"a":"foo","b":"bar"}' :: jsonb))
765+jsonbEachText
753− :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
766+ :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
754− -> Query outer commons schemas params
767+ -> FromClause outer commons schemas params
755− '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]
768+ '["jsonb_each" ::: '["key" ::: 'NotNull 'PGtext, "value" ::: 'NotNull 'PGtext]]
756−jsonbEachAsText = unsafeSetOfFunction "jsonb_each_text"
769+jsonbEachText = unsafeSetOfFunction "jsonb_each_text"
757
770
758-- | Returns set of keys in the outermost JSON object.
771-- | Returns set of keys in the outermost JSON object.
772+-- >>> import Data.Aeson
773+-- >>> printSQL (jsonObjectKeys (jsonLit (object ["a" .= "foo", "b" .= "bar"])))
774+-- json_object_keys(('{"a":"foo","b":"bar"}' :: json))
759jsonObjectKeys
775jsonObjectKeys
760 :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
776 :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
777+ -> FromClause outer commons schemas params
761− -> Query outer commons schemas params '["json_object_keys" ::: 'NotNull 'PGtext]
778+ '["json_object_keys" ::: '["json_object_keys" ::: 'NotNull 'PGtext]]
762jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
779jsonObjectKeys = unsafeSetOfFunction "json_object_keys"
763
780
764-- | Returns set of keys in the outermost JSON object.
781-- | Returns set of keys in the outermost JSON object.
782+-- >>> import Data.Aeson
783+-- >>> printSQL (jsonbObjectKeys (jsonbLit (object ["a" .= "foo", "b" .= "bar"])))
784+-- jsonb_object_keys(('{"a":"foo","b":"bar"}' :: jsonb))
765jsonbObjectKeys
785jsonbObjectKeys
766 :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
786 :: Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
787+ -> FromClause outer commons schemas params
767− -> Query outer commons schemas params '["jsonb_object_keys" ::: 'NotNull 'PGtext]
788+ '["jsonb_object_keys" ::: '["jsonb_object_keys" ::: 'NotNull 'PGtext]]
768jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
789jsonbObjectKeys = unsafeSetOfFunction "jsonb_object_keys"
769
790
770unsafePopulateFunction
791unsafePopulateFunction
771− :: ByteString
792+ :: KnownSymbol fun
793+ => Alias fun
772 -> TypeExpression schemas (nullity ('PGcomposite row))
794 -> TypeExpression schemas (nullity ('PGcomposite row))
773 -> Expression outer 'Ungrouped commons schemas params '[] ty
795 -> Expression outer 'Ungrouped commons schemas params '[] ty
774− -> Query outer commons schemas params row
796+ -> FromClause outer commons schemas params '[fun ::: row]
775−unsafePopulateFunction fun ty expr = UnsafeQuery $
797+unsafePopulateFunction fun ty expr = UnsafeFromClause $ renderSQL fun
776− "SELECT * FROM " <> fun <> "("
777− <> "null::" <> renderSQL ty <> ", "
798+ <> parenthesized ("null::" <> renderSQL ty <> ", " <> renderSQL expr)
778− <> renderSQL expr <> ")"
779
799
780-- | Expands the JSON expression to a row whose columns match the record
800-- | Expands the JSON expression to a row whose columns match the record
781-- type defined by the given table.
801-- type defined by the given table.
782jsonPopulateRecord
802jsonPopulateRecord
783 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
803 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
784 -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
804 -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
785− -> Query outer commons schemas params row
805+ -> FromClause outer commons schemas params '["json_populate_record" ::: row]
786−jsonPopulateRecord = unsafePopulateFunction "json_populate_record"
806+jsonPopulateRecord = unsafePopulateFunction #json_populate_record
787
807
788-- | Expands the binary JSON expression to a row whose columns match the record
808-- | Expands the binary JSON expression to a row whose columns match the record
789-- type defined by the given table.
809-- type defined by the given table.
790jsonbPopulateRecord
810jsonbPopulateRecord
791 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
811 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
792− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
812+ -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
793− -> Query outer commons schemas params row
813+ -> FromClause outer commons schemas params '["jsonb_populate_record" ::: row]
794−jsonbPopulateRecord = unsafePopulateFunction "jsonb_populate_record"
814+jsonbPopulateRecord = unsafePopulateFunction #jsonb_populate_record
795
815
796-- | Expands the outermost array of objects in the given JSON expression to a
816-- | Expands the outermost array of objects in the given JSON expression to a
797-- set of rows whose columns match the record type defined by the given table.
817-- set of rows whose columns match the record type defined by the given table.
798jsonPopulateRecordSet
818jsonPopulateRecordSet
799 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
819 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
800− -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
820+ -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
801− -> Query outer commons schemas params row
821+ -> FromClause outer commons schemas params '["json_populate_record_set" ::: row]
802−jsonPopulateRecordSet = unsafePopulateFunction "json_populate_record_set"
822+jsonPopulateRecordSet = unsafePopulateFunction #json_populate_record_set
803
823
804-- | Expands the outermost array of objects in the given binary JSON expression
824-- | Expands the outermost array of objects in the given binary JSON expression
805-- to a set of rows whose columns match the record type defined by the given
825-- to a set of rows whose columns match the record type defined by the given
@@ -807,54 +827,52 @@ jsonPopulateRecordSet = unsafePopulateFunction "json_populate_record_set"
807jsonbPopulateRecordSet
827jsonbPopulateRecordSet
808 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
828 :: TypeExpression schemas (nullity ('PGcomposite row)) -- ^ row type
809 -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
829 -> Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
810− -> Query outer commons schemas params row
830+ -> FromClause outer commons schemas params '["jsonb_populate_record_set" ::: row]
811−jsonbPopulateRecordSet = unsafePopulateFunction "jsonb_populate_record_set"
831+jsonbPopulateRecordSet = unsafePopulateFunction #jsonb_populate_record_set
812
832
813unsafeRecordFunction
833unsafeRecordFunction
814 :: (SListI record, json `In` PGJsonType)
834 :: (SListI record, json `In` PGJsonType)
815 => ByteString
835 => ByteString
816− -> Expression outer 'Ungrouped commons schemas params '[] (nullity json)
836+ -> Expression outer 'Ungrouped commons schemas params '[] (nullity json)
817− -> NP (Aliased (TypeExpression schemas)) record
837+ -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
818− -> Query outer commons schemas params record
838+ -> FromClause outer commons schemas params '[tab ::: record]
819−unsafeRecordFunction fun expr types = UnsafeQuery $
839+unsafeRecordFunction fun expr (types `As` tab) = UnsafeFromClause $
820− "SELECT * FROM " <> fun <> "("
821− <> renderSQL expr <> ")"
840+ fun <> parenthesized (renderSQL expr) <+> "AS" <+> renderSQL tab
822− <+> "AS" <+> "x" <> parenthesized (renderCommaSeparated renderTy types)
841+ <> parenthesized (renderCommaSeparated renderTy types)
823 where
842 where
824 renderTy :: Aliased (TypeExpression schemas) ty -> ByteString
843 renderTy :: Aliased (TypeExpression schemas) ty -> ByteString
825− renderTy (ty `As` alias) =
826− renderSQL alias <+> renderSQL ty
844+ renderTy (ty `As` alias) = renderSQL alias <+> renderSQL ty
827
845
828-- | Builds an arbitrary record from a JSON object.
846-- | Builds an arbitrary record from a JSON object.
829jsonToRecord
847jsonToRecord
830 :: SListI record
848 :: SListI record
831− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
849+ => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json object
832− -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
850+ -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
833− -> Query outer commons schemas params record
851+ -> FromClause outer commons schemas params '[tab ::: record]
834jsonToRecord = unsafeRecordFunction "json_to_record"
852jsonToRecord = unsafeRecordFunction "json_to_record"
835
853
836-- | Builds an arbitrary record from a binary JSON object.
854-- | Builds an arbitrary record from a binary JSON object.
837jsonbToRecord
855jsonbToRecord
838 :: SListI record
856 :: SListI record
839− => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
857+ => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb object
840− -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
858+ -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
841− -> Query outer commons schemas params record
859+ -> FromClause outer commons schemas params '[tab ::: record]
842jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
860jsonbToRecord = unsafeRecordFunction "jsonb_to_record"
843
861
844-- | Builds an arbitrary set of records from a JSON array of objects.
862-- | Builds an arbitrary set of records from a JSON array of objects.
845jsonToRecordSet
863jsonToRecordSet
846 :: SListI record
864 :: SListI record
847 => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
865 => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjson) -- ^ json array
848− -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
866+ -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
849− -> Query outer commons schemas params record
867+ -> FromClause outer commons schemas params '[tab ::: record]
850jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
868jsonToRecordSet = unsafeRecordFunction "json_to_record_set"
851
869
852-- | Builds an arbitrary set of records from a binary JSON array of objects.
870-- | Builds an arbitrary set of records from a binary JSON array of objects.
853jsonbToRecordSet
871jsonbToRecordSet
854 :: SListI record
872 :: SListI record
855 => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
873 => Expression outer 'Ungrouped commons schemas params '[] (nullity 'PGjsonb) -- ^ jsonb array
856− -> NP (Aliased (TypeExpression schemas)) record -- ^ record types
874+ -> Aliased (NP (Aliased (TypeExpression schemas))) (tab ::: record)
857− -> Query outer commons schemas params record
875+ -> FromClause outer commons schemas params '[tab ::: record]
858jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
876jsonbToRecordSet = unsafeRecordFunction "jsonb_to_record_set"
859
877
860{-----------------------------------------
878{-----------------------------------------