@@ -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-- |