on dev help sign in
setof and more
commit6be23e22b5787412b480aab4e41a06a87e39b2be authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 2 Sep 2019, 11:40 (7 years ago) parent79e6b1c
4 files changed+95−18
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+59−9 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+10−4 squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+21 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+5−5
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+59−9
@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
5050 , createIndex
5151 , createFunction
5252 , createOrReplaceFunction
53+ , createSetFunction
54+ , createOrReplaceSetFunction
5355 , TableConstraintExpression (..)
5456 , check
5557 , unique
@@ -60,6 +62,7 @@ module Squeal.PostgreSQL.Definition
6062 , OnUpdateClause (..)
6163 , FunctionDefinition(..)
6264 , languageSql
65+ , languageSqlSet
6366 -- ** Drop
6467 , dropSchema
6568 , dropTable
@@ -1136,8 +1139,8 @@ createFunction
11361139 => QualifiedAlias sch fun
11371140 -> NP (TypeExpression schemas) args
11381141 -> TypeExpression schemas ret
1139− -> FunctionDefinition schemas ('Fun args ret)
1140− -> Definition schemas (Alter sch (Create fun ('Function ('Fun args ret)) schema) schemas)
1142+ -> FunctionDefinition schemas args ('Returns ret)
1143+ -> Definition schemas (Alter sch (Create fun ('Function args ('Returns ret)) schema) schemas)
11411144createFunction fun args ret fundef = UnsafeDefinition $
11421145 "CREATE" <+> "FUNCTION" <+> renderSQL fun
11431146 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1145,13 +1148,13 @@ createFunction fun args ret fundef = UnsafeDefinition $
11451148
11461149createOrReplaceFunction
11471150 :: ( Has sch schemas schema
1148− , Has fun schema ('Function ('Fun args0 ret0))
1151+ , Has fun schema ('Function args0 ret0)
11491152 , SOP.SListI args )
11501153 => QualifiedAlias sch fun
11511154 -> NP (TypeExpression schemas) args
11521155 -> TypeExpression schemas ret
1153− -> FunctionDefinition schemas ('Fun args ret)
1154− -> Definition schemas (Alter sch (Alter fun ('Function ('Fun args ret)) schema) schemas)
1156+ -> FunctionDefinition schemas args ('Returns ret)
1157+ -> Definition schemas (Alter sch (Alter fun ('Function args ('Returns ret)) schema) schemas)
11551158createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
11561159 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
11571160 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1159,22 +1162,69 @@ createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
11591162
11601163languageSql
11611164 :: Query '[] '[] schemas args '[col ::: ret]
1162− -> FunctionDefinition schemas ('Fun args ret)
1165+ -> FunctionDefinition schemas args ('Returns ret)
11631166languageSql qry = UnsafeFunctionDefinition $
11641167 "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
11651168
1169+languageSqlSet
1170+ :: Query '[] '[] schemas args rets
1171+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1172+languageSqlSet qry = UnsafeFunctionDefinition $
1173+ "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1174+
1175+
1176+createSetFunction
1177+ :: ( Has sch schemas schema
1178+ , KnownSymbol fun
1179+ , SOP.SListI args
1180+ , SOP.SListI rets )
1181+ => QualifiedAlias sch fun
1182+ -> NP (TypeExpression schemas) args
1183+ -> NP (Aliased (TypeExpression schemas)) rets
1184+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1185+ -> Definition schemas (Alter sch (Create fun ('Function args ('ReturnsTable rets)) schema) schemas)
1186+createSetFunction fun args rets fundef = UnsafeDefinition $
1187+ "CREATE" <+> "FUNCTION" <+> renderSQL fun
1188+ <+> parenthesized (renderCommaSeparated renderSQL args)
1189+ <+> "RETURNS" <+> "TABLE"
1190+ <+> parenthesized (renderCommaSeparated renderRet rets)
1191+ <+> renderSQL fundef <> ";"
1192+ where
1193+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1194+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1195+
1196+createOrReplaceSetFunction
1197+ :: ( Has sch schemas schema
1198+ , Has fun schema ('Function args0 ret0)
1199+ , SOP.SListI args
1200+ , SOP.SListI rets )
1201+ => QualifiedAlias sch fun
1202+ -> NP (TypeExpression schemas) args
1203+ -> NP (Aliased (TypeExpression schemas)) rets
1204+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1205+ -> Definition schemas (Alter sch (Alter fun ('Function args ('ReturnsTable rets)) schema) schemas)
1206+createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1207+ "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1208+ <+> parenthesized (renderCommaSeparated renderSQL args)
1209+ <+> "RETURNS" <+> "TABLE"
1210+ <+> parenthesized (renderCommaSeparated renderRet rets)
1211+ <+> renderSQL fundef <> ";"
1212+ where
1213+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1214+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1215+
11661216dropFunction
1167− :: (Has sch schemas schema, Has fun schema ('Function funty))
1217+ :: (Has sch schemas schema, Has fun schema ('Function args ret))
11681218 => QualifiedAlias sch fun
11691219 -- ^ name of the user defined function
11701220 -> Definition schemas (Alter sch (Drop ix schema) schemas)
11711221dropFunction fun = UnsafeDefinition $
11721222 "DROP" <+> "Function" <+> renderSQL fun <> ";"
11731223
1174−newtype FunctionDefinition schemas funty = UnsafeFunctionDefinition
1224+newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
11751225 { renderFunctionDefinition :: ByteString }
11761226 deriving (Eq,Show,GHC.Generic,NFData)
1177−instance RenderSQL (FunctionDefinition schemas funty) where
1227+instance RenderSQL (FunctionDefinition schemas args ret) where
11781228 renderSQL = renderFunctionDefinition
11791229
11801230-- |
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+10−4
@@ -324,9 +324,11 @@ unsafeFunction fun x = UnsafeExpression $
324324 fun <> parenthesized (renderSQL x)
325325
326326function
327− :: (Has sch schemas schema, Has fun schema ('Function ('Fun '[x] y)))
327+ :: (Has sch schemas schema, Has fun schema ('Function '[x] ('Returns y)))
328328 => QualifiedAlias sch fun
329− -> x :--> y
329+ -> ( forall outer commons grp params from
330+ . Expression outer commons grp schemas params from x
331+ -> Expression outer commons grp schemas params from y )
330332function = unsafeFunction . renderSQL
331333
332334-- | >>> printSQL $ unsafeFunctionN "f" (currentTime :* localTimestamp :* false *: literal 'a')
@@ -336,9 +338,13 @@ unsafeFunctionN fun xs = UnsafeExpression $
336338 fun <> parenthesized (renderCommaSeparated renderSQL xs)
337339
338340functionN
339− :: (Has sch schemas schema, Has fun schema ('Function ('Fun xs y)), SListI xs)
341+ :: ( Has sch schemas schema
342+ , Has fun schema ('Function xs ('Returns y))
343+ , SListI xs )
340344 => QualifiedAlias sch fun
341− -> FunctionN xs y
345+ -> ( forall outer commons grp params from
346+ . NP (Expression outer commons grp schemas params from) xs
347+ -> Expression outer commons grp schemas params from y )
342348functionN = unsafeFunctionN . renderSQL
343349
344350instance ty `In` PGNum
squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+21
@@ -26,8 +26,10 @@ module Squeal.PostgreSQL.Expression.SetOf
2626 , generateSeriesTimestamp
2727 , SetOfFunction
2828 , unsafeSetOfFunction
29+ , setOfFunction
2930 , SetOfFunctionN
3031 , unsafeSetOfFunctionN
32+ , setOfFunctionN
3133 ) where
3234
3335import GHC.TypeLits
@@ -58,6 +60,15 @@ unsafeSetOfFunction
5860unsafeSetOfFunction x = UnsafeFromClause $
5961 renderSymbol @fun <> parenthesized (renderSQL x)
6062
63+setOfFunction
64+ :: ( Has sch schemas schema
65+ , Has fun schema ('Function '[ty] ('ReturnsTable setof)) )
66+ => Alias fun
67+ -> ( forall outer commons params
68+ . Expression outer commons 'Ungrouped schemas params '[] ty
69+ -> FromClause outer commons schemas params '[fun ::: setof] )
70+setOfFunction _ = unsafeSetOfFunction
71+
6172{- |
6273A @RankNType@ for set returning functions with multiple argument.
6374-}
@@ -75,6 +86,16 @@ unsafeSetOfFunctionN
7586unsafeSetOfFunctionN xs = UnsafeFromClause $
7687 renderSymbol @fun <> parenthesized (renderCommaSeparated renderSQL xs)
7788
89+setOfFunctionN
90+ :: ( Has sch schemas schema
91+ , Has fun schema ('Function tys ('ReturnsTable setof))
92+ , SOP.SListI tys )
93+ => Alias fun
94+ -> ( forall outer commons params
95+ . NP (Expression outer commons 'Ungrouped schemas params '[]) tys
96+ -> FromClause outer commons schemas params '[fun ::: setof] )
97+setOfFunctionN _ = unsafeSetOfFunctionN
98+
7899{- | @generateSeries (start *: stop)@
79100
80101Generate a series of values, from @start@ to @stop@ with a step size of one
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+5−5
@@ -45,7 +45,7 @@ module Squeal.PostgreSQL.Schema
4545 , TableType
4646 , SchemumType (..)
4747 , OperatorType (..)
48− , FunctionType (..)
48+ , ReturnsType (..)
4949 , SchemaType
5050 , SchemasType
5151 , Public
@@ -369,13 +369,13 @@ data SchemumType
369369 | View RowType
370370 | Typedef PGType
371371 | Index
372− | Function FunctionType
372+ | Function [NullityType] ReturnsType
373373 | Operator OperatorType
374374 | UnsafeSchemum Symbol
375375
376−data FunctionType
377− = Fun [NullityType] NullityType
378− | SetFun [NullityType] [NullityType]
376+data ReturnsType
377+ = Returns NullityType
378+ | ReturnsTable RowType
379379
380380data OperatorType
381381 = BinaryOp NullityType NullityType NullityType