@@ -608,11 +608,11 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -608,11 +608,11 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
if(!functionName||!returnType||!language||!body)thrownewError('createFunction missing some parameters. Did you pass functionName, returnType, language and body?');
if(!functionName||!returnType||!language||!body)thrownewError('createFunction missing some parameters. Did you pass functionName, returnType, language and body?');
conststatement=options&&options.force?'CREATE OR REPLACE FUNCTION':'CREATE FUNCTION';
conststatement=options&&options.force?'CREATE OR REPLACE FUNCTION':'CREATE FUNCTION';
return`${statement}${functionName}(${paramList}) RETURNS ${returnType} AS $func$ BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;
return`${statement}${functionName}(${paramList}) RETURNS ${returnType} AS $func$ ${variableList}BEGIN ${body} END; $func$ language '${language}'${expandedOptionsArray};`;
}
}
dropFunction(functionName,params){
dropFunction(functionName,params){
...
@@ -667,6 +667,25 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -667,6 +667,25 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
returnparamList.join(', ');
returnparamList.join(', ');
}
}
expandFunctionVariableList(variables){
if(!Array.isArray(variables)){
thrownewError('expandFunctionVariableList: function variables must be an array');
}
constvariableDefinitions=[];
variables.forEach(variable=>{
if(!variable.name||!variable.type){
thrownewError('function variable must have a name and type');
* @param {string} functionName Name of SQL function to create
* @param {string} functionName Name of SQL function to create
...
@@ -1242,6 +1249,7 @@ class QueryInterface {
...
@@ -1242,6 +1249,7 @@ class QueryInterface {
* @param {Array} optionsArray Extra-options for creation
* @param {Array} optionsArray Extra-options for creation
* @param {Object} [options] query options
* @param {Object} [options] query options
* @param {boolean} options.force If force is true, any existing functions with the same parameters will be replaced. For postgres, this means using `CREATE OR REPLACE FUNCTION` instead of `CREATE FUNCTION`. Default is false
* @param {boolean} options.force If force is true, any existing functions with the same parameters will be replaced. For postgres, this means using `CREATE OR REPLACE FUNCTION` instead of `CREATE FUNCTION`. Default is false
* @param {Array<Object>} options.variables List of declared variables. Each variable should be an object with string fields `type` and `name`, and optionally having a `default` field as well.