6014-executeRoutineMethod #1809
|
@ -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
|
||||
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
jgallego
commented
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
jgallego
commented
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;
|
||||
|
|
|
@ -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
jgallego
commented
quitar quitar
|
||||
routine = schema + '.' + routine;
|
||||
|
||||
const query = `SELECT ${routine}`;
|
||||
const response = await Self.execute(ctx, query, params, options);
|
||||
return Object.values(response)[0];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
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