@@ -50,6 +50,7 @@ module Squeal.PostgreSQL.Definition
5050 , createDomain
5151 , createTypeRange
5252 , createIndex
53+ , createIndexIfNotExists
5354 , IndexMethod (..)
5455 , btree, hash, gist, spgist, gin, brin
5556 , createFunction
@@ -1161,6 +1162,31 @@ createIndex ix tab method cols = UnsafeDefinition $
11611162 DescNullsLast expression -> parenthesized (renderSQL expression)
11621163 <+> "DESC NULLS LAST"
11631164
1165+createIndexIfNotExists
1166+ :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1167+ => Alias ix
1168+ -> QualifiedAlias sch tab
1169+ -> IndexMethod method
1170+ -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1171+ -> Definition schemas (Alter sch (CreateIfNotExists ix ('Index method) schema) schemas)
1172+createIndexIfNotExists ix tab method cols = UnsafeDefinition $
1173+ "CREATE INDEX IF NOT EXISTS" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1174+ <+> "USING" <+> renderSQL method
1175+ <+> parenthesized (commaSeparated (renderIndex <$> cols))
1176+ <> ";"
1177+ where
1178+ renderIndex = \case
1179+ Asc expression -> parenthesized (renderSQL expression) <+> "ASC"
1180+ Desc expression -> parenthesized (renderSQL expression) <+> "DESC"
1181+ AscNullsFirst expression -> parenthesized (renderSQL expression)
1182+ <+> "ASC NULLS FIRST"
1183+ DescNullsFirst expression -> parenthesized (renderSQL expression)
1184+ <+> "DESC NULLS FIRST"
1185+ AscNullsLast expression -> parenthesized (renderSQL expression)
1186+ <+> "ASC NULLS LAST"
1187+ DescNullsLast expression -> parenthesized (renderSQL expression)
1188+ <+> "DESC NULLS LAST"
1189+
11641190newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
11651191 deriving stock (Eq, Ord, Show, GHC.Generic)
11661192instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
@@ -1185,7 +1211,7 @@ createFunction
11851211 -> NP (TypeExpression schemas) args
11861212 -> TypeExpression schemas ret
11871213 -> FunctionDefinition schemas args ('Returns ret)
1188− -> Definition schemas (Alter sch (Create fun ('Function args ('Returns ret)) schema) schemas)
1214+ -> Definition schemas (Alter sch (Create fun ('Function (args '::--> 'Returns ret)) schema) schemas)
11891215createFunction fun args ret fundef = UnsafeDefinition $
11901216 "CREATE" <+> "FUNCTION" <+> renderSQL fun
11911217 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1193,13 +1219,13 @@ createFunction fun args ret fundef = UnsafeDefinition $
11931219
11941220createOrReplaceFunction
11951221 :: ( Has sch schemas schema
1196− , Has fun schema ('Function args0 ret0)
1222+ , KnownSymbol fun
11971223 , SOP.SListI args )
11981224 => QualifiedAlias sch fun
11991225 -> NP (TypeExpression schemas) args
12001226 -> TypeExpression schemas ret
12011227 -> FunctionDefinition schemas args ('Returns ret)
1202− -> Definition schemas (Alter sch (CreateOrReplace fun ('Function args ('Returns ret)) schema) schemas)
1228+ -> Definition schemas (Alter sch (CreateOrReplace fun ('Function (args '::--> 'Returns ret)) schema) schemas)
12031229createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
12041230 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
12051231 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1227,7 +1253,7 @@ createSetFunction
12271253 -> NP (TypeExpression schemas) args
12281254 -> NP (Aliased (TypeExpression schemas)) rets
12291255 -> FunctionDefinition schemas args ('ReturnsTable rets)
1230− -> Definition schemas (Alter sch (Create fun ('Function args ('ReturnsTable rets)) schema) schemas)
1256+ -> Definition schemas (Alter sch (Create fun ('Function (args '::--> 'ReturnsTable rets)) schema) schemas)
12311257createSetFunction fun args rets fundef = UnsafeDefinition $
12321258 "CREATE" <+> "FUNCTION" <+> renderSQL fun
12331259 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1240,14 +1266,14 @@ createSetFunction fun args rets fundef = UnsafeDefinition $
12401266
12411267createOrReplaceSetFunction
12421268 :: ( Has sch schemas schema
1243− , Has fun schema ('Function args0 ret0)
1269+ , Has fun schema ('Function (args0 '::--> ret0))
12441270 , SOP.SListI args
12451271 , SOP.SListI rets )
12461272 => QualifiedAlias sch fun
12471273 -> NP (TypeExpression schemas) args
12481274 -> NP (Aliased (TypeExpression schemas)) rets
12491275 -> FunctionDefinition schemas args ('ReturnsTable rets)
1250− -> Definition schemas (Alter sch (CreateOrReplace fun ('Function args ('ReturnsTable rets)) schema) schemas)
1276+ -> Definition schemas (Alter sch (CreateOrReplace fun ('Function (args '::--> 'ReturnsTable rets)) schema) schemas)
12511277createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
12521278 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
12531279 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1261,7 +1287,7 @@ createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
12611287createBinaryOp
12621288 :: forall op fun sch schemas schema x y z.
12631289 ( Has sch schemas schema
1264− , Has fun schema ('Function '[x,y] ('Returns z))
1290+ , Has fun schema ('Function ('[x,y] '::--> 'Returns z))
12651291 , KnownSymbol op )
12661292 => Alias fun
12671293 -> TypeExpression schemas x
@@ -1280,7 +1306,7 @@ createBinaryOp fun x y = UnsafeDefinition $
12801306createLeftOp
12811307 :: forall op fun sch schemas schema x y.
12821308 ( Has sch schemas schema
1283− , Has fun schema ('Function '[x] ('Returns y))
1309+ , Has fun schema ('Function ('[x] '::--> 'Returns y))
12841310 , KnownSymbol op )
12851311 => Alias fun
12861312 -> TypeExpression schemas x
@@ -1297,7 +1323,7 @@ createLeftOp fun x = UnsafeDefinition $
12971323createRightOp
12981324 :: forall op fun sch schemas schema x y.
12991325 ( Has sch schemas schema
1300− , Has fun schema ('Function '[x] ('Returns y))
1326+ , Has fun schema ('Function ('[x] '::--> 'Returns y))
13011327 , KnownSymbol op )
13021328 => Alias fun
13031329 -> TypeExpression schemas x
@@ -1312,7 +1338,7 @@ createRightOp fun x = UnsafeDefinition $
13121338 , "LEFTARG" <+> "=" <+> renderSQL x ]
13131339
13141340dropFunction
1315− :: (Has sch schemas schema, Has fun schema ('Function args ret))
1341+ :: (Has sch schemas schema, Has fun schema ('Function (args '::--> ret)))
13161342 => QualifiedAlias sch fun
13171343 -- ^ name of the user defined function
13181344 -> Definition schemas (Alter sch (Drop fun schema) schemas)