refs #6014 refactor(execute): schema required
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2023-11-14 09:17:46 +01:00
parent 59d2da24eb
commit cce61ae8cc
4 changed files with 30 additions and 33 deletions

View File

@ -1,7 +1,7 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.execute = async(ctx, query, params, options) => {
Self.execute = async(ctx, type, query, params, options) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
params = params ?? [];
@ -10,20 +10,18 @@ module.exports = Self => {
if (typeof options == 'object')
Object.assign(myOptions, options);
let [caller, chain] = query.split(' ');
if (!chain.includes('.')) chain = 'vn.' + chain;
const chain = query.split(' ')[1];
const [canExecute] = await models.ProcsPriv.rawSql(
'SELECT account.user_hasRoutinePriv(?,?,?)',
[caller == 'CALL' ? 'PROCEDURE' : 'FUNCTION', chain, userId],
[type, chain, userId],
myOptions);
if (!Object.values(canExecute)[0]) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
let argString = params.map(() => '?').join(',');
query = `${query}(${argString})`;
const argString = params.map(() => '?').join(',');
const [response] = await models.ProcsPriv.rawSql(query, params, myOptions);
const [response] = await models.ProcsPriv.rawSql(query + `(${argString})`, params, myOptions);
return response;
};
};

View File

@ -10,16 +10,17 @@ module.exports = Self => {
required: true,
http: {source: 'path'}
},
{
arg: 'schema',
type: 'string',
description: 'The routine schema',
required: true,
},
{
arg: 'params',
type: ['any'],
description: 'The params array',
},
{
arg: 'schema',
type: 'string',
description: 'The routine schema',
}
],
returns: {
type: 'any',
@ -31,12 +32,10 @@ module.exports = Self => {
}
});
Self.executeFunc = async(ctx, routine, params, schema, options) => {
if (schema)
routine = schema + '.' + routine;
Self.executeFunc = async(ctx, routine, schema, params, options) => {
const query = `SELECT ${schema}.${routine}`;
const query = `SELECT ${routine}`;
const response = await Self.execute(ctx, query, params, options);
const response = await Self.execute(ctx, 'FUNCTION', query, params, options);
return Object.values(response)[0];
};
};

View File

@ -10,16 +10,17 @@ module.exports = Self => {
required: true,
http: {source: 'path'}
},
{
arg: 'schema',
type: 'string',
description: 'The routine schema',
required: true,
},
{
arg: 'params',
type: ['any'],
description: 'The params array',
},
{
arg: 'schema',
type: 'string',
description: 'The routine schema',
}
],
returns: {
type: 'any',
@ -31,11 +32,8 @@ module.exports = Self => {
}
});
Self.executeProc = async(ctx, routine, params, schema, options) => {
if (schema)
routine = schema + '.' + routine;
const query = `CALL ${routine}`;
return Self.execute(ctx, query, params, options);
Self.executeProc = async(ctx, routine, schema, params, options) => {
const query = `CALL ${schema}.${routine}`;
return Self.execute(ctx, 'PROCEDURE', query, params, options);
};
};

View File

@ -50,7 +50,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
await models.Application.execute(
ctx,
'CALL myProcedure',
'PROCEDURE',
'CALL vn.myProcedure',
[1],
options
);
@ -71,7 +72,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
const response = await models.Application.execute(
ctx,
'CALL myProcedure',
'PROCEDURE',
'CALL vn.myProcedure',
[1],
options
);
@ -95,8 +97,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
const response = await models.Application.executeProc(
ctx,
'myProcedure',
'vn',
[1],
null,
options
);
@ -120,8 +122,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
const response = await models.Application.executeFunc(
ctx,
'myFunction',
[1],
'bs',
[1],
options
);
@ -142,8 +144,8 @@ describe('Application execute()/executeProc()/executeFunc()', () => {
const response = await models.Application.executeFunc(
ctx,
'myFunction',
[1],
'bs',
[1],
options
);