@@ -19,6 +19,7 @@ Squeal data definition language.
1919 , GADTs
2020 , LambdaCase
2121 , MultiParamTypeClasses
22+ , OverloadedLabels
2223 , OverloadedStrings
2324 , RankNTypes
2425 , ScopedTypeVariables
@@ -48,6 +49,13 @@ module Squeal.PostgreSQL.Definition
4849 , createDomain
4950 , createTypeRange
5051 , createIndex
52+ , createFunction
53+ , createOrReplaceFunction
54+ , createSetFunction
55+ , createOrReplaceSetFunction
56+ , createBinaryOp
57+ , createLeftOp
58+ , createRightOp
5159 , TableConstraintExpression (..)
5260 , check
5361 , unique
@@ -56,12 +64,17 @@ module Squeal.PostgreSQL.Definition
5664 , ForeignKeyed
5765 , OnDeleteClause (..)
5866 , OnUpdateClause (..)
67+ , FunctionDefinition(..)
68+ , languageSqlExpr
69+ , languageSqlQuery
5970 -- ** Drop
6071 , dropSchema
6172 , dropTable
6273 , dropView
6374 , dropType
6475 , dropIndex
76+ , dropFunction
77+ , dropOperator
6578 -- ** Alter
6679 , alterTable
6780 , alterTableRename
@@ -576,6 +589,7 @@ DROP statements
576589dropSchema
577590 :: Has sch schemas schema
578591 => Alias sch
592+ -- ^ user defined schema
579593 -> Definition schemas (Drop sch schemas)
580594dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
581595
@@ -1124,13 +1138,169 @@ instance RenderSQL IndexMethod where
11241138 Gin -> "gin"
11251139 Brin -> "brin"
11261140
1141+createFunction
1142+ :: ( Has sch schemas schema
1143+ , KnownSymbol fun
1144+ , SOP.SListI args )
1145+ => QualifiedAlias sch fun
1146+ -> NP (TypeExpression schemas) args
1147+ -> TypeExpression schemas ret
1148+ -> FunctionDefinition schemas args ('Returns ret)
1149+ -> Definition schemas (Alter sch (Create fun ('Function args ('Returns ret)) schema) schemas)
1150+createFunction fun args ret fundef = UnsafeDefinition $
1151+ "CREATE" <+> "FUNCTION" <+> renderSQL fun
1152+ <+> parenthesized (renderCommaSeparated renderSQL args)
1153+ <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1154+
1155+createOrReplaceFunction
1156+ :: ( Has sch schemas schema
1157+ , Has fun schema ('Function args0 ret0)
1158+ , SOP.SListI args )
1159+ => QualifiedAlias sch fun
1160+ -> NP (TypeExpression schemas) args
1161+ -> TypeExpression schemas ret
1162+ -> FunctionDefinition schemas args ('Returns ret)
1163+ -> Definition schemas (Alter sch (Alter fun ('Function args ('Returns ret)) schema) schemas)
1164+createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1165+ "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1166+ <+> parenthesized (renderCommaSeparated renderSQL args)
1167+ <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1168+
1169+languageSqlExpr
1170+ :: Expression '[] '[] 'Ungrouped schemas args '[] ret
1171+ -> FunctionDefinition schemas args ('Returns ret)
1172+languageSqlExpr expr = UnsafeFunctionDefinition $
1173+ "language sql as"
1174+ <+> "$$" <+> renderSQL (values_ (expr `as` #ret)) <+> "$$"
1175+
1176+languageSqlQuery
1177+ :: Query '[] '[] schemas args rets
1178+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1179+languageSqlQuery qry = UnsafeFunctionDefinition $
1180+ "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1181+
1182+createSetFunction
1183+ :: ( Has sch schemas schema
1184+ , KnownSymbol fun
1185+ , SOP.SListI args
1186+ , SOP.SListI rets )
1187+ => QualifiedAlias sch fun
1188+ -> NP (TypeExpression schemas) args
1189+ -> NP (Aliased (TypeExpression schemas)) rets
1190+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1191+ -> Definition schemas (Alter sch (Create fun ('Function args ('ReturnsTable rets)) schema) schemas)
1192+createSetFunction fun args rets fundef = UnsafeDefinition $
1193+ "CREATE" <+> "FUNCTION" <+> renderSQL fun
1194+ <+> parenthesized (renderCommaSeparated renderSQL args)
1195+ <+> "RETURNS" <+> "TABLE"
1196+ <+> parenthesized (renderCommaSeparated renderRet rets)
1197+ <+> renderSQL fundef <> ";"
1198+ where
1199+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1200+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1201+
1202+createOrReplaceSetFunction
1203+ :: ( Has sch schemas schema
1204+ , Has fun schema ('Function args0 ret0)
1205+ , SOP.SListI args
1206+ , SOP.SListI rets )
1207+ => QualifiedAlias sch fun
1208+ -> NP (TypeExpression schemas) args
1209+ -> NP (Aliased (TypeExpression schemas)) rets
1210+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1211+ -> Definition schemas (Alter sch (Alter fun ('Function args ('ReturnsTable rets)) schema) schemas)
1212+createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1213+ "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1214+ <+> parenthesized (renderCommaSeparated renderSQL args)
1215+ <+> "RETURNS" <+> "TABLE"
1216+ <+> parenthesized (renderCommaSeparated renderRet rets)
1217+ <+> renderSQL fundef <> ";"
1218+ where
1219+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1220+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1221+
1222+createBinaryOp
1223+ :: forall op fun sch schemas schema x y z.
1224+ ( Has sch schemas schema
1225+ , Has fun schema ('Function '[x,y] ('Returns z))
1226+ , KnownSymbol op )
1227+ => Alias fun
1228+ -> TypeExpression schemas x
1229+ -> TypeExpression schemas y
1230+ -> Definition schemas
1231+ (Alter sch (Create op ('Operator ('BinaryOp x y z)) schema) schemas)
1232+createBinaryOp fun x y = UnsafeDefinition $
1233+ "CREATE" <+> "OPERATOR" <+> renderSymbol @op
1234+ <+> parenthesized (commaSeparated opdef)
1235+ where
1236+ opdef =
1237+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1238+ , "LEFTARG" <+> "=" <+> renderSQL x
1239+ , "RIGHTARG" <+> "=" <+> renderSQL y ]
1240+
1241+createLeftOp
1242+ :: forall op fun sch schemas schema x y.
1243+ ( Has sch schemas schema
1244+ , Has fun schema ('Function '[x] ('Returns y))
1245+ , KnownSymbol op )
1246+ => Alias fun
1247+ -> TypeExpression schemas x
1248+ -> Definition schemas
1249+ (Alter sch (Create op ('Operator ('LeftOp x y)) schema) schemas)
1250+createLeftOp fun x = UnsafeDefinition $
1251+ "CREATE" <+> "OPERATOR" <+> renderSymbol @op
1252+ <+> parenthesized (commaSeparated opdef)
1253+ where
1254+ opdef =
1255+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1256+ , "RIGHTARG" <+> "=" <+> renderSQL x ]
1257+
1258+createRightOp
1259+ :: forall op fun sch schemas schema x y.
1260+ ( Has sch schemas schema
1261+ , Has fun schema ('Function '[x] ('Returns y))
1262+ , KnownSymbol op )
1263+ => Alias fun
1264+ -> TypeExpression schemas x
1265+ -> Definition schemas
1266+ (Alter sch (Create op ('Operator ('RightOp x y)) schema) schemas)
1267+createRightOp fun x = UnsafeDefinition $
1268+ "CREATE" <+> "OPERATOR" <+> renderSymbol @op
1269+ <+> parenthesized (commaSeparated opdef)
1270+ where
1271+ opdef =
1272+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1273+ , "LEFTARG" <+> "=" <+> renderSQL x ]
1274+
1275+dropFunction
1276+ :: (Has sch schemas schema, Has fun schema ('Function args ret))
1277+ => QualifiedAlias sch fun
1278+ -- ^ name of the user defined function
1279+ -> Definition schemas (Alter sch (Drop fun schema) schemas)
1280+dropFunction fun = UnsafeDefinition $
1281+ "DROP" <+> "FUNCTION" <+> renderSQL fun <> ";"
1282+
1283+dropOperator
1284+ :: (Has sch schemas schema, Has op schema ('Operator oper))
1285+ => QualifiedAlias sch op
1286+ -- ^ name of the user defined operator
1287+ -> Definition schemas (Alter sch (Drop op schema) schemas)
1288+dropOperator op = UnsafeDefinition $
1289+ "DROP" <+> "OPERATOR" <+> renderSQL op <> ";"
1290+
1291+newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
1292+ { renderFunctionDefinition :: ByteString }
1293+ deriving (Eq,Show,GHC.Generic,NFData)
1294+instance RenderSQL (FunctionDefinition schemas args ret) where
1295+ renderSQL = renderFunctionDefinition
1296+
11271297-- |
11281298-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
11291299-- DROP INDEX "ix";
11301300dropIndex
11311301 :: (Has sch schemas schema, Has ix schema 'Index)
11321302 => QualifiedAlias sch ix
1133− -- ^ name of the user defined type
1303+ -- ^ name of the user defined index
11341304 -> Definition schemas (Alter sch (Drop ix schema) schemas)
11351305dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
11361306