Skip to content

Commit

Permalink
fix(postgres): fix interaction between returning and exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Americas committed Oct 16, 2019
1 parent c3a8341 commit 42b37b8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class QueryGenerator {
let valueQuery = '';
let emptyQuery = '';
let outputFragment = '';
let returningFragment = '';
let identityWrapperRequired = false;
let tmpTable = ''; //tmpTable declaration for trigger

Expand All @@ -132,8 +133,7 @@ class QueryGenerator {
if (this._dialect.supports.returnValues && options.returning) {
const returnValues = this.generateReturnValues(modelAttributes, options);

valueQuery += returnValues.query;
emptyQuery += returnValues.query;
returningFragment = returnValues.query;
tmpTable = returnValues.tmpTable || '';
outputFragment = returnValues.outputFragment || '';
}
Expand Down Expand Up @@ -198,12 +198,15 @@ class QueryGenerator {

options.exception = 'WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL;';
valueQuery = `${`CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response ${quotedTable}, OUT sequelize_caught_exception text) RETURNS RECORD AS ${delimiter}` +
' BEGIN '}${valueQuery} INTO response; EXCEPTION ${options.exception} END ${delimiter
' BEGIN '}${valueQuery} RETURNING * INTO response; EXCEPTION ${options.exception} END ${delimiter
} LANGUAGE plpgsql; SELECT (testfunc.response).*, testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc()`;
} else {
options.exception = 'WHEN unique_violation THEN NULL;';
valueQuery = `CREATE OR REPLACE FUNCTION pg_temp.testfunc() RETURNS SETOF ${quotedTable} AS $body$ BEGIN RETURN QUERY ${valueQuery}; EXCEPTION ${options.exception} END; $body$ LANGUAGE plpgsql; SELECT * FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();`;
valueQuery = `CREATE OR REPLACE FUNCTION pg_temp.testfunc() RETURNS SETOF ${quotedTable} AS $body$ BEGIN RETURN QUERY ${valueQuery} RETURNING *; EXCEPTION ${options.exception} END; $body$ LANGUAGE plpgsql; SELECT * FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();`;
}
} else {
valueQuery += returningFragment;
emptyQuery += returningFragment;
}

if (this._dialect.supports['ON DUPLICATE KEY'] && options.onDuplicate) {
Expand Down

0 comments on commit 42b37b8

Please sign in to comment.