@@ -19,6 +19,7 @@ Squeal data definition language.
20 , LambdaCase
20 , LambdaCase
21 , MultiParamTypeClasses
21 , MultiParamTypeClasses
22 , OverloadedStrings
23 , OverloadedStrings
23 , RankNTypes
24 , RankNTypes
24 , ScopedTypeVariables
25 , ScopedTypeVariables
@@ -48,6 +49,13 @@ module Squeal.PostgreSQL.Definition
48 , createDomain
49 , createDomain
49 , createTypeRange
50 , createTypeRange
50 , createIndex
51 , createIndex
53+ , createOrReplaceFunction
55+ , createOrReplaceSetFunction
51 , TableConstraintExpression (..)
59 , TableConstraintExpression (..)
@@ -56,12 +64,17 @@ module Squeal.PostgreSQL.Definition
56 , ForeignKeyed
64 , ForeignKeyed
57 , OnDeleteClause (..)
65 , OnDeleteClause (..)
58 , OnUpdateClause (..)
66 , OnUpdateClause (..)
67+ , FunctionDefinition(..)
59 -- ** Drop
70 -- ** Drop
60 , dropSchema
71 , dropSchema
61 , dropTable
72 , dropTable
62 , dropView
73 , dropView
63 , dropType
74 , dropType
64 , dropIndex
75 , dropIndex
65 -- ** Alter
78 -- ** Alter
66 , alterTable
79 , alterTable
67 , alterTableRename
80 , alterTableRename
@@ -576,6 +589,7 @@ DROP statements
576dropSchema
589dropSchema
577 :: Has sch schemas schema
590 :: Has sch schemas schema
578 => Alias sch
591 => Alias sch
592+ -- ^ user defined schema
579 -> Definition schemas (Drop sch schemas)
593 -> Definition schemas (Drop sch schemas)
580dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
594dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
@@ -1124,13 +1138,169 @@ instance RenderSQL IndexMethod where
1124 Gin -> "gin"
1138 Gin -> "gin"
1125 Brin -> "brin"
1139 Brin -> "brin"
1142+ :: ( Has sch schemas schema
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 <> ";"
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 <> ";"
1170+ :: Expression '[] '[] 'Ungrouped schemas args '[] ret
1171+ -> FunctionDefinition schemas args ('Returns ret)
1172+languageSqlExpr expr = UnsafeFunctionDefinition $
1174+ <+> "$$" <+> renderSQL (values_ (expr `as` #ret)) <+> "$$"
1177+ :: Query '[] '[] schemas args rets
1178+ -> FunctionDefinition schemas args ('ReturnsTable rets)
1179+languageSqlQuery qry = UnsafeFunctionDefinition $
1180+ "language sql as" <+> "$$" <+> renderSQL qry <+> "$$"
1183+ :: ( Has sch schemas schema
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 <> ";"
1199+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1200+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1202+createOrReplaceSetFunction
1203+ :: ( Has sch schemas schema
1204+ , Has fun schema ('Function args0 ret0)
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 <> ";"
1219+ renderRet :: Aliased (TypeExpression s) r -> ByteString
1220+ renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1223+ :: forall op fun sch schemas schema x y z.
1224+ ( Has sch schemas schema
1225+ , Has fun schema ('Function '[x,y] ('Returns z))
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)
1237+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1238+ , "LEFTARG" <+> "=" <+> renderSQL x
1239+ , "RIGHTARG" <+> "=" <+> renderSQL y ]
1242+ :: forall op fun sch schemas schema x y.
1243+ ( Has sch schemas schema
1244+ , Has fun schema ('Function '[x] ('Returns y))
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)
1255+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1256+ , "RIGHTARG" <+> "=" <+> renderSQL x ]
1259+ :: forall op fun sch schemas schema x y.
1260+ ( Has sch schemas schema
1261+ , Has fun schema ('Function '[x] ('Returns y))
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)
1272+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1273+ , "LEFTARG" <+> "=" <+> renderSQL x ]
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 <> ";"
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 <> ";"
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
1128-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1298-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1129-- DROP INDEX "ix";
1299-- DROP INDEX "ix";
1130dropIndex
1300dropIndex
1131 :: (Has sch schemas schema, Has ix schema 'Index)
1301 :: (Has sch schemas schema, Has ix schema 'Index)
1132 => QualifiedAlias sch ix
1302 => QualifiedAlias sch ix
1133− -- ^ name of the user defined type
1303+ -- ^ name of the user defined index
1134 -> Definition schemas (Alter sch (Drop ix schema) schemas)
1304 -> Definition schemas (Alter sch (Drop ix schema) schemas)
1135dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1305dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"