@@ -50,6 +50,7 @@ module Squeal.PostgreSQL.Definition
50 , createDomain
50 , createDomain
51 , createTypeRange
51 , createTypeRange
52 , createIndex
52 , createIndex
53+ , createIndexIfNotExists
53 , IndexMethod (..)
54 , IndexMethod (..)
54 , btree, hash, gist, spgist, gin, brin
55 , btree, hash, gist, spgist, gin, brin
55 , createFunction
56 , createFunction
@@ -1161,6 +1162,31 @@ createIndex ix tab method cols = UnsafeDefinition $
1161 DescNullsLast expression -> parenthesized (renderSQL expression)
1162 DescNullsLast expression -> parenthesized (renderSQL expression)
1162 <+> "DESC NULLS LAST"
1163 <+> "DESC NULLS LAST"
1165+createIndexIfNotExists
1166+ :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol 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))
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"
1164newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
1190newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
1165 deriving stock (Eq, Ord, Show, GHC.Generic)
1191 deriving stock (Eq, Ord, Show, GHC.Generic)
1166instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
1192instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
@@ -1185,7 +1211,7 @@ createFunction
1185 -> NP (TypeExpression schemas) args
1211 -> NP (TypeExpression schemas) args
1186 -> TypeExpression schemas ret
1212 -> TypeExpression schemas ret
1187 -> FunctionDefinition schemas args ('Returns ret)
1213 -> 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)
1189createFunction fun args ret fundef = UnsafeDefinition $
1215createFunction fun args ret fundef = UnsafeDefinition $
1190 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1216 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1191 <+> parenthesized (renderCommaSeparated renderSQL args)
1217 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1193,13 +1219,13 @@ createFunction fun args ret fundef = UnsafeDefinition $
1194createOrReplaceFunction
1220createOrReplaceFunction
1195 :: ( Has sch schemas schema
1221 :: ( Has sch schemas schema
1196− , Has fun schema ('Function args0 ret0)
1222+ , KnownSymbol fun
1197 , SOP.SListI args )
1223 , SOP.SListI args )
1198 => QualifiedAlias sch fun
1224 => QualifiedAlias sch fun
1199 -> NP (TypeExpression schemas) args
1225 -> NP (TypeExpression schemas) args
1200 -> TypeExpression schemas ret
1226 -> TypeExpression schemas ret
1201 -> FunctionDefinition schemas args ('Returns ret)
1227 -> 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)
1203createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1229createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1204 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1230 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1205 <+> parenthesized (renderCommaSeparated renderSQL args)
1231 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1227,7 +1253,7 @@ createSetFunction
1227 -> NP (TypeExpression schemas) args
1253 -> NP (TypeExpression schemas) args
1228 -> NP (Aliased (TypeExpression schemas)) rets
1254 -> NP (Aliased (TypeExpression schemas)) rets
1229 -> FunctionDefinition schemas args ('ReturnsTable rets)
1255 -> 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)
1231createSetFunction fun args rets fundef = UnsafeDefinition $
1257createSetFunction fun args rets fundef = UnsafeDefinition $
1232 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1258 "CREATE" <+> "FUNCTION" <+> renderSQL fun
1233 <+> parenthesized (renderCommaSeparated renderSQL args)
1259 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1240,14 +1266,14 @@ createSetFunction fun args rets fundef = UnsafeDefinition $
1241createOrReplaceSetFunction
1267createOrReplaceSetFunction
1242 :: ( Has sch schemas schema
1268 :: ( Has sch schemas schema
1243− , Has fun schema ('Function args0 ret0)
1269+ , Has fun schema ('Function (args0 '::--> ret0))
1244 , SOP.SListI args
1270 , SOP.SListI args
1245 , SOP.SListI rets )
1271 , SOP.SListI rets )
1246 => QualifiedAlias sch fun
1272 => QualifiedAlias sch fun
1247 -> NP (TypeExpression schemas) args
1273 -> NP (TypeExpression schemas) args
1248 -> NP (Aliased (TypeExpression schemas)) rets
1274 -> NP (Aliased (TypeExpression schemas)) rets
1249 -> FunctionDefinition schemas args ('ReturnsTable rets)
1275 -> 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)
1251createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1277createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1252 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1278 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1253 <+> parenthesized (renderCommaSeparated renderSQL args)
1279 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1261,7 +1287,7 @@ createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1261createBinaryOp
1287createBinaryOp
1262 :: forall op fun sch schemas schema x y z.
1288 :: forall op fun sch schemas schema x y z.
1263 ( Has sch schemas schema
1289 ( Has sch schemas schema
1264− , Has fun schema ('Function '[x,y] ('Returns z))
1290+ , Has fun schema ('Function ('[x,y] '::--> 'Returns z))
1265 , KnownSymbol op )
1291 , KnownSymbol op )
1266 => Alias fun
1292 => Alias fun
1267 -> TypeExpression schemas x
1293 -> TypeExpression schemas x
@@ -1280,7 +1306,7 @@ createBinaryOp fun x y = UnsafeDefinition $
1280createLeftOp
1306createLeftOp
1281 :: forall op fun sch schemas schema x y.
1307 :: forall op fun sch schemas schema x y.
1282 ( Has sch schemas schema
1308 ( Has sch schemas schema
1283− , Has fun schema ('Function '[x] ('Returns y))
1309+ , Has fun schema ('Function ('[x] '::--> 'Returns y))
1284 , KnownSymbol op )
1310 , KnownSymbol op )
1285 => Alias fun
1311 => Alias fun
1286 -> TypeExpression schemas x
1312 -> TypeExpression schemas x
@@ -1297,7 +1323,7 @@ createLeftOp fun x = UnsafeDefinition $
1297createRightOp
1323createRightOp
1298 :: forall op fun sch schemas schema x y.
1324 :: forall op fun sch schemas schema x y.
1299 ( Has sch schemas schema
1325 ( Has sch schemas schema
1300− , Has fun schema ('Function '[x] ('Returns y))
1326+ , Has fun schema ('Function ('[x] '::--> 'Returns y))
1301 , KnownSymbol op )
1327 , KnownSymbol op )
1302 => Alias fun
1328 => Alias fun
1303 -> TypeExpression schemas x
1329 -> TypeExpression schemas x
@@ -1312,7 +1338,7 @@ createRightOp fun x = UnsafeDefinition $
1312 , "LEFTARG" <+> "=" <+> renderSQL x ]
1338 , "LEFTARG" <+> "=" <+> renderSQL x ]
1314dropFunction
1340dropFunction
1315− :: (Has sch schemas schema, Has fun schema ('Function args ret))
1341+ :: (Has sch schemas schema, Has fun schema ('Function (args '::--> ret)))
1316 => QualifiedAlias sch fun
1342 => QualifiedAlias sch fun
1317 -- ^ name of the user defined function
1343 -- ^ name of the user defined function
1318 -> Definition schemas (Alter sch (Drop fun schema) schemas)
1344 -> Definition schemas (Alter sch (Drop fun schema) schemas)