6014-executeRoutineMethod #1809

Merged
alexm merged 8 commits from 6014-executeRoutineMethod into dev 2023-11-14 08:45:07 +00:00
4 changed files with 21 additions and 21 deletions
Showing only changes of commit 59d2da24eb - Show all commits

View File

@ -1,32 +1,27 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.execute = async(ctx, routine, params, schema, type, options) => {
Self.execute = async(ctx, query, params, options) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
let caller = 'CALL';
params = params ?? [];
schema = schema ?? 'vn';
type = type ?? 'procedure';
const myOptions = {userId: ctx.req.accessToken.userId};
alexm marked this conversation as resolved
Review

tota la part comu va en esta funcio.
La part de proc o de func en les respectives.
El resultat es que en esta funció no tindrás ningun if relatiu al tipo

tota la part comu va en esta funcio. La part de proc o de func en les respectives. El resultat es que en esta funció no tindrás ningun if relatiu al tipo
if (typeof options == 'object')
Object.assign(myOptions, options);
const chain = `${schema}.${routine}`;
let [caller, chain] = query.split(' ');
if (!chain.includes('.')) chain = 'vn.' + chain;
alexm marked this conversation as resolved
Review

quitar esquema por defecto

quitar esquema por defecto
const [canExecute] = await models.ProcsPriv.rawSql(
'SELECT account.user_hasRoutinePriv(?,?,?)',
[type.toUpperCase(), chain, userId],
[caller == 'CALL' ? 'PROCEDURE' : 'FUNCTION', chain, userId],
myOptions);
if (!Object.values(canExecute)[0]) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
const isFunction = type == 'function';
let argString = params.map(() => '?').join(',');
alexm marked this conversation as resolved
Review

const

const
if (isFunction)
caller = 'SELECT';
const query = `${caller} ${chain}(${argString})`;
query = `${query}(${argString})`;
const [response] = await models.ProcsPriv.rawSql(query, params, myOptions);
return response;

View File

@ -32,7 +32,11 @@ module.exports = Self => {
});
Self.executeFunc = async(ctx, routine, params, schema, options) => {
const response = await Self.execute(ctx, routine, params, schema, 'function', options);
if (schema)
alexm marked this conversation as resolved Outdated

quitar

quitar
routine = schema + '.' + routine;
const query = `SELECT ${routine}`;
const response = await Self.execute(ctx, query, params, options);
return Object.values(response)[0];
};
};

View File

@ -31,6 +31,11 @@ module.exports = Self => {
}
});
Self.executeProc = async(ctx, routine, params, schema, options) =>
Self.execute(ctx, routine, params, schema, 'procedure', options);
Self.executeProc = async(ctx, routine, params, schema, options) => {
if (schema)
routine = schema + '.' + routine;
const query = `CALL ${routine}`;
return Self.execute(ctx, query, params, options);
};
};

View File

@ -50,10 +50,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
await models.Application.execute(
ctx,
'myProcedure',
'CALL myProcedure',
[1],
null,
null,
options
);
@ -73,10 +71,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
const response = await models.Application.execute(
ctx,
'myProcedure',
'CALL myProcedure',
[1],
null,
null,
options
);