Copyright | (c) Eitan Chatav 2019 |
---|---|
Maintainer | eitan@morphism.tech |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
create and drop functions
Synopsis
- createFunction :: (Has sch db schema, KnownSymbol fun, SListI args) => QualifiedAlias sch fun -> NP (TypeExpression db) args -> TypeExpression db ret -> FunctionDefinition db args ('Returns ret) -> Definition db (Alter sch (Create fun ('Function (args :=> 'Returns ret)) schema) db)
- createOrReplaceFunction :: (Has sch db schema, KnownSymbol fun, SListI args) => QualifiedAlias sch fun -> NP (TypeExpression db) args -> TypeExpression db ret -> FunctionDefinition db args ('Returns ret) -> Definition db (Alter sch (CreateOrReplace fun ('Function (args :=> 'Returns ret)) schema) db)
- createSetFunction :: (Has sch db schema, KnownSymbol fun, SListI args, SListI rets) => QualifiedAlias sch fun -> NP (TypeExpression db) args -> NP (Aliased (TypeExpression db)) rets -> FunctionDefinition db args ('ReturnsTable rets) -> Definition db (Alter sch (Create fun ('Function (args :=> 'ReturnsTable rets)) schema) db)
- createOrReplaceSetFunction :: (Has sch db schema, KnownSymbol fun, SListI args, SListI rets) => QualifiedAlias sch fun -> NP (TypeExpression db) args -> NP (Aliased (TypeExpression db)) rets -> FunctionDefinition db args ('ReturnsTable rets) -> Definition db (Alter sch (CreateOrReplace fun ('Function (args :=> 'ReturnsTable rets)) schema) db)
- dropFunction :: (Has sch db schema, KnownSymbol fun) => QualifiedAlias sch fun -> Definition db (Alter sch (DropSchemum fun 'Function schema) db)
- dropFunctionIfExists :: (Has sch db schema, KnownSymbol fun) => QualifiedAlias sch fun -> Definition db (Alter sch (DropSchemumIfExists fun 'Function schema) db)
- newtype FunctionDefinition db args ret = UnsafeFunctionDefinition {}
- languageSqlExpr :: Expression 'Ungrouped '[] '[] db args '[] ret -> FunctionDefinition db args ('Returns ret)
- languageSqlQuery :: Query '[] '[] db args rets -> FunctionDefinition db args ('ReturnsTable rets)
Create
:: (Has sch db schema, KnownSymbol fun, SListI args) | |
=> QualifiedAlias sch fun | function alias |
-> NP (TypeExpression db) args | arguments |
-> TypeExpression db ret | return type |
-> FunctionDefinition db args ('Returns ret) | function definition |
-> Definition db (Alter sch (Create fun ('Function (args :=> 'Returns ret)) schema) db) |
Create a function.
>>>
type Fn = 'Function ( '[ 'Null 'PGint4, 'Null 'PGint4] :=> 'Returns ( 'Null 'PGint4))
>>>
:{
let definition :: Definition (Public '[]) (Public '["fn" ::: Fn]) definition = createFunction #fn (int4 *: int4) int4 $ languageSqlExpr (param @1 * param @2 + 1) in printSQL definition :} CREATE FUNCTION "fn" (int4, int4) RETURNS int4 language sql as $$ SELECT * FROM (VALUES (((($1 :: int4) * ($2 :: int4)) + (1 :: int4)))) AS t ("ret") $$;
createOrReplaceFunction Source #
:: (Has sch db schema, KnownSymbol fun, SListI args) | |
=> QualifiedAlias sch fun | function alias |
-> NP (TypeExpression db) args | arguments |
-> TypeExpression db ret | return type |
-> FunctionDefinition db args ('Returns ret) | function definition |
-> Definition db (Alter sch (CreateOrReplace fun ('Function (args :=> 'Returns ret)) schema) db) |
Create or replace a function. It is not possible to change the name or argument types or return type of a function this way.
>>>
type Fn = 'Function ( '[ 'Null 'PGint4, 'Null 'PGint4] :=> 'Returns ( 'Null 'PGint4))
>>>
:{
let definition :: Definition (Public '["fn" ::: Fn]) (Public '["fn" ::: Fn]) definition = createOrReplaceFunction #fn (int4 *: int4) int4 $ languageSqlExpr (param @1 @('Null 'PGint4) * param @2 @('Null 'PGint4) + 1) in printSQL definition :} CREATE OR REPLACE FUNCTION "fn" (int4, int4) RETURNS int4 language sql as $$ SELECT * FROM (VALUES (((($1 :: int4) * ($2 :: int4)) + (1 :: int4)))) AS t ("ret") $$;
:: (Has sch db schema, KnownSymbol fun, SListI args, SListI rets) | |
=> QualifiedAlias sch fun | function alias |
-> NP (TypeExpression db) args | arguments |
-> NP (Aliased (TypeExpression db)) rets | return type |
-> FunctionDefinition db args ('ReturnsTable rets) | function definition |
-> Definition db (Alter sch (Create fun ('Function (args :=> 'ReturnsTable rets)) schema) db) |
Create a set function.
>>>
type Tab = 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])
>>>
type Fn = 'Function ('[ 'Null 'PGint4, 'Null 'PGint4] :=> 'ReturnsTable '["ret" ::: 'Null 'PGint4])
>>>
:{
let definition :: Definition (Public '["tab" ::: Tab]) (Public '["tab" ::: Tab, "fn" ::: Fn]) definition = createSetFunction #fn (int4 *: int4) (int4 `as` #ret) $ languageSqlQuery (select_ ((param @1 * param @2 + #col) `as` #ret) (from (table #tab))) in printSQL definition :} CREATE FUNCTION "fn" (int4, int4) RETURNS TABLE ("ret" int4) language sql as $$ SELECT ((($1 :: int4) * ($2 :: int4)) + "col") AS "ret" FROM "tab" AS "tab" $$;
createOrReplaceSetFunction Source #
:: (Has sch db schema, KnownSymbol fun, SListI args, SListI rets) | |
=> QualifiedAlias sch fun | function alias |
-> NP (TypeExpression db) args | arguments |
-> NP (Aliased (TypeExpression db)) rets | return type |
-> FunctionDefinition db args ('ReturnsTable rets) | function definition |
-> Definition db (Alter sch (CreateOrReplace fun ('Function (args :=> 'ReturnsTable rets)) schema) db) |
Create or replace a set function.
>>>
type Tab = 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])
>>>
type Fn = 'Function ('[ 'Null 'PGint4, 'Null 'PGint4] :=> 'ReturnsTable '["ret" ::: 'Null 'PGint4])
>>>
:{
let definition :: Definition (Public '["tab" ::: Tab, "fn" ::: Fn]) (Public '["tab" ::: Tab, "fn" ::: Fn]) definition = createOrReplaceSetFunction #fn (int4 *: int4) (int4 `as` #ret) $ languageSqlQuery (select_ ((param @1 * param @2 + #col) `as` #ret) (from (table #tab))) in printSQL definition :} CREATE OR REPLACE FUNCTION "fn" (int4, int4) RETURNS TABLE ("ret" int4) language sql as $$ SELECT ((($1 :: int4) * ($2 :: int4)) + "col") AS "ret" FROM "tab" AS "tab" $$;
Drop
:: (Has sch db schema, KnownSymbol fun) | |
=> QualifiedAlias sch fun | function alias |
-> Definition db (Alter sch (DropSchemum fun 'Function schema) db) |
Drop a function.
>>>
type Fn = 'Function ( '[ 'Null 'PGint4, 'Null 'PGint4] :=> 'Returns ( 'Null 'PGint4))
>>>
:{
let definition :: Definition (Public '["fn" ::: Fn]) (Public '[]) definition = dropFunction #fn in printSQL definition :} DROP FUNCTION "fn";
:: (Has sch db schema, KnownSymbol fun) | |
=> QualifiedAlias sch fun | function alias |
-> Definition db (Alter sch (DropSchemumIfExists fun 'Function schema) db) |
Drop a function.
>>>
type Fn = 'Function ( '[ 'Null 'PGint4, 'Null 'PGint4] :=> 'Returns ( 'Null 'PGint4))
>>>
:{
let definition :: Definition (Public '[]) (Public '[]) definition = dropFunctionIfExists #fn in printSQL definition :} DROP FUNCTION IF EXISTS "fn";
Function Definition
newtype FunctionDefinition db args ret Source #
Body of a user defined function
Instances
:: Expression 'Ungrouped '[] '[] db args '[] ret | function body |
-> FunctionDefinition db args ('Returns ret) |
Use a parameterized Expression
as a function body
:: Query '[] '[] db args rets | function body |
-> FunctionDefinition db args ('ReturnsTable rets) |
Use a parametrized Query
as a function body