@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
50 , createIndex
50 , createIndex
51 , createFunction
51 , createFunction
52 , createOrReplaceFunction
52 , createOrReplaceFunction
54+ , createOrReplaceSetFunction
53 , TableConstraintExpression (..)
55 , TableConstraintExpression (..)
@@ -60,6 +62,7 @@ module Squeal.PostgreSQL.Definition
60 , OnUpdateClause (..)
62 , OnUpdateClause (..)
61 , FunctionDefinition(..)
63 , FunctionDefinition(..)
62 , languageSql
64 , languageSql
63 -- ** Drop
66 -- ** Drop
64 , dropSchema
67 , dropSchema
65 , dropTable
68 , dropTable
@@ -1136,8 +1139,8 @@ createFunction
1136 => QualifiedAlias sch fun
1139 => QualifiedAlias sch fun
1137 -> NP (TypeExpression schemas) args
1140 -> NP (TypeExpression schemas) args
1138 -> TypeExpression schemas ret
1141 -> TypeExpression schemas ret
1139− -> FunctionDefinition schemas ('Fun args ret)
1142+ -> FunctionDefinition schemas args ('Returns ret)
1140− -> Definition schemas (Alter sch (Create fun ('Function ('Fun args ret)) schema) schemas)
1143+ -> Definition schemas (Alter sch (Create fun ('Function args ('Returns ret)) schema) schemas)
1141createFunction fun args ret fundef = UnsafeDefinition $
1144createFunction fun args ret fundef = UnsafeDefinition $
1142 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1145 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1143 <+> parenthesized (renderCommaSeparated renderSQL args)
1146 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1145,13 +1148,13 @@ createFunction fun args ret fundef = UnsafeDefinition $
1146createOrReplaceFunction
1149createOrReplaceFunction
1147 :: ( Has sch schemas schema
1150 :: ( Has sch schemas schema
1148− , Has fun schema ('Function ('Fun args0 ret0))
1151+ , Has fun schema ('Function args0 ret0)
1149 , SOP.SListI args )
1152 , SOP.SListI args )
1150 => QualifiedAlias sch fun
1153 => QualifiedAlias sch fun
1151 -> NP (TypeExpression schemas) args
1154 -> NP (TypeExpression schemas) args
1152 -> TypeExpression schemas ret
1155 -> TypeExpression schemas ret
1153− -> FunctionDefinition schemas ('Fun args ret)
1156+ -> FunctionDefinition schemas args ('Returns ret)
1154− -> Definition schemas (Alter sch (Alter fun ('Function ('Fun args ret)) schema) schemas)
1157+ -> Definition schemas (Alter sch (Alter fun ('Function args ('Returns ret)) schema) schemas)
1155createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1158createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1156 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1159 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1157 <+> parenthesized (renderCommaSeparated renderSQL args)
1160 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1159,22 +1162,69 @@ createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1160languageSql
1163languageSql
1161 :: Query '[] '[] schemas args '[col ::: ret]
1164 :: Query '[] '[] schemas args '[col ::: ret]
1162− -> FunctionDefinition schemas ('Fun args ret)
1165+ -> FunctionDefinition schemas args ('Returns ret)
1163languageSql qry = UnsafeFunctionDefinition $
1166languageSql qry = UnsafeFunctionDefinition $
1164 "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1167 "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1170+ :: Query '[] '[] schemas args rets
1171+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1172+languageSqlSet qry = UnsafeFunctionDefinition $
1173+ "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1177+ :: ( Has sch schemas schema
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 <> ";"
1193+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1194+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1196+createOrReplaceSetFunction
1197+ :: ( Has sch schemas schema
1198+ , Has fun schema ('Function args0 ret0)
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 <> ";"
1213+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1214+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1166dropFunction
1216dropFunction
1167− :: (Has sch schemas schema, Has fun schema ('Function funty))
1217+ :: (Has sch schemas schema, Has fun schema ('Function args ret))
1168 => QualifiedAlias sch fun
1218 => QualifiedAlias sch fun
1169 -- ^ name of the user defined function
1219 -- ^ name of the user defined function
1170 -> Definition schemas (Alter sch (Drop ix schema) schemas)
1220 -> Definition schemas (Alter sch (Drop ix schema) schemas)
1171dropFunction fun = UnsafeDefinition $
1221dropFunction fun = UnsafeDefinition $
1172 "DROP" <+> "Function" <+> renderSQL fun <> ";"
1222 "DROP" <+> "Function" <+> renderSQL fun <> ";"
1174−newtype FunctionDefinition schemas funty = UnsafeFunctionDefinition
1224+newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
1175 { renderFunctionDefinition :: ByteString }
1225 { renderFunctionDefinition :: ByteString }
1176 deriving (Eq,Show,GHC.Generic,NFData)
1226 deriving (Eq,Show,GHC.Generic,NFData)
1177−instance RenderSQL (FunctionDefinition schemas funty) where
1227+instance RenderSQL (FunctionDefinition schemas args ret) where
1178 renderSQL = renderFunctionDefinition
1228 renderSQL = renderFunctionDefinition