on dev help sign in
indexes and functions
50bf2adEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−10
@@ -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"
1163
1164
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+
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 $
1193
1219
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 $
1240
1266
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 ]
1313
1339
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)
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+2−2
@@ -356,7 +356,7 @@ unsafeFunction fun x = UnsafeExpression $
356 fun <> parenthesized (renderSQL x)
356 fun <> parenthesized (renderSQL x)
357
357
358function
358function
359− :: (Has sch schemas schema, Has fun schema ('Function '[x] ('Returns y)))
359+ :: (Has sch schemas schema, Has fun schema ('Function ('[x] '::--> 'Returns y)))
360 => QualifiedAlias sch fun
360 => QualifiedAlias sch fun
361 -> FunctionDB schemas x y
361 -> FunctionDB schemas x y
362function = unsafeFunction . renderSQL
362function = unsafeFunction . renderSQL
@@ -369,7 +369,7 @@ unsafeFunctionN fun xs = UnsafeExpression $
369
369
370functionN
370functionN
371 :: ( Has sch schemas schema
371 :: ( Has sch schemas schema
372− , Has fun schema ('Function xs ('Returns y))
372+ , Has fun schema ('Function (xs '::--> 'Returns y))
373 , SListI xs )
373 , SListI xs )
374 => QualifiedAlias sch fun
374 => QualifiedAlias sch fun
375 -> FunctionNDB schemas xs y
375 -> FunctionNDB schemas xs y
squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+2−2
@@ -62,7 +62,7 @@ unsafeSetOfFunction x = UnsafeFromClause $
62
62
63setOfFunction
63setOfFunction
64 :: ( Has sch schemas schema
64 :: ( Has sch schemas schema
65− , Has fun schema ('Function '[ty] ('ReturnsTable setof)) )
65+ , Has fun schema ('Function ('[ty] '::--> 'ReturnsTable setof)) )
66 => Alias fun
66 => Alias fun
67 -> ( forall outer commons params
67 -> ( forall outer commons params
68 . Expression outer commons 'Ungrouped schemas params '[] ty
68 . Expression outer commons 'Ungrouped schemas params '[] ty
@@ -88,7 +88,7 @@ unsafeSetOfFunctionN xs = UnsafeFromClause $
88
88
89setOfFunctionN
89setOfFunctionN
90 :: ( Has sch schemas schema
90 :: ( Has sch schemas schema
91− , Has fun schema ('Function tys ('ReturnsTable setof))
91+ , Has fun schema ('Function (tys '::--> 'ReturnsTable setof))
92 , SOP.SListI tys )
92 , SOP.SListI tys )
93 => Alias fun
93 => Alias fun
94 -> ( forall outer commons params
94 -> ( forall outer commons params
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+4−1
@@ -45,6 +45,7 @@ module Squeal.PostgreSQL.Schema
45 , TableType
45 , TableType
46 , SchemumType (..)
46 , SchemumType (..)
47 , IndexType (..)
47 , IndexType (..)
48+ , FunctionType (..)
48 , OperatorType (..)
49 , OperatorType (..)
49 , ReturnsType (..)
50 , ReturnsType (..)
50 , SchemaType
51 , SchemaType
@@ -428,10 +429,12 @@ data SchemumType
428 | View RowType
429 | View RowType
429 | Typedef PGType
430 | Typedef PGType
430 | Index IndexType
431 | Index IndexType
431− | Function [NullityType] ReturnsType
432+ | Function FunctionType
432 | Operator OperatorType
433 | Operator OperatorType
433 | UnsafeSchemum Symbol
434 | UnsafeSchemum Symbol
434
435
436+data FunctionType = (::-->) [NullityType] ReturnsType
437+
435data IndexType
438data IndexType
436 = Btree
439 = Btree
437 | Hash
440 | Hash