on dev help sign in
indexes and functions
commit50bf2addf1fd465f8fe835ecd02818016b2983a3 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateTue 24 Sep 2019, 08:45 (7 years ago) parentc824222
4 files changed+44−15
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−10 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+4−1
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−10
@@ -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)
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+2−2
@@ -356,7 +356,7 @@ unsafeFunction fun x = UnsafeExpression $
356356 fun <> parenthesized (renderSQL x)
357357
358358function
359− :: (Has sch schemas schema, Has fun schema ('Function '[x] ('Returns y)))
359+ :: (Has sch schemas schema, Has fun schema ('Function ('[x] '::--> 'Returns y)))
360360 => QualifiedAlias sch fun
361361 -> FunctionDB schemas x y
362362function = unsafeFunction . renderSQL
@@ -369,7 +369,7 @@ unsafeFunctionN fun xs = UnsafeExpression $
369369
370370functionN
371371 :: ( Has sch schemas schema
372− , Has fun schema ('Function xs ('Returns y))
372+ , Has fun schema ('Function (xs '::--> 'Returns y))
373373 , SListI xs )
374374 => QualifiedAlias sch fun
375375 -> FunctionNDB schemas xs y
squeal-postgresql/src/Squeal/PostgreSQL/Expression/SetOf.hs+2−2
@@ -62,7 +62,7 @@ unsafeSetOfFunction x = UnsafeFromClause $
6262
6363setOfFunction
6464 :: ( Has sch schemas schema
65− , Has fun schema ('Function '[ty] ('ReturnsTable setof)) )
65+ , Has fun schema ('Function ('[ty] '::--> 'ReturnsTable setof)) )
6666 => Alias fun
6767 -> ( forall outer commons params
6868 . Expression outer commons 'Ungrouped schemas params '[] ty
@@ -88,7 +88,7 @@ unsafeSetOfFunctionN xs = UnsafeFromClause $
8888
8989setOfFunctionN
9090 :: ( Has sch schemas schema
91− , Has fun schema ('Function tys ('ReturnsTable setof))
91+ , Has fun schema ('Function (tys '::--> 'ReturnsTable setof))
9292 , SOP.SListI tys )
9393 => Alias fun
9494 -> ( forall outer commons params
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+4−1
@@ -45,6 +45,7 @@ module Squeal.PostgreSQL.Schema
4545 , TableType
4646 , SchemumType (..)
4747 , IndexType (..)
48+ , FunctionType (..)
4849 , OperatorType (..)
4950 , ReturnsType (..)
5051 , SchemaType
@@ -428,10 +429,12 @@ data SchemumType
428429 | View RowType
429430 | Typedef PGType
430431 | Index IndexType
431− | Function [NullityType] ReturnsType
432+ | Function FunctionType
432433 | Operator OperatorType
433434 | UnsafeSchemum Symbol
434435
436+data FunctionType = (::-->) [NullityType] ReturnsType
437+
435438data IndexType
436439 = Btree
437440 | Hash