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

View File

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

View File

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

View File

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