Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions reverse_engineering/databaseService/databaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ const getDatabaseProcedures = async ({ client, dbName, logger }) => {
dbName,
meta: {
action: 'getting procedures query',
objects: ['sys.procedures', 'sys.schemas', 'sys.sql_modules'],
objects: ['sys.procedures', 'sys.schemas', 'sys.sql_modules', 'sys.extended_properties'],
skip: true,
},
logger,
Expand All @@ -945,19 +945,26 @@ const getDatabaseProcedures = async ({ client, dbName, logger }) => {
logger.log('info', { message: `Get '${dbName}' database procedures.` }, 'Reverse Engineering');

const response = await currentDbConnectionClient.query(`
SELECT
s.name AS schema_name,
p.name AS procedure_name,
sm.definition AS procedure_body
FROM sys.procedures p
JOIN sys.schemas s ON p.schema_id = s.schema_id
LEFT JOIN sys.sql_modules sm ON p.object_id = sm.object_id
ORDER BY s.name, p.name;
SELECT
s.name AS schema_name,
p.name AS procedure_name,
sm.definition AS procedure_body,
ep.value AS description
FROM sys.procedures p
JOIN sys.schemas s
ON p.schema_id = s.schema_id
LEFT JOIN sys.sql_modules sm
ON p.object_id = sm.object_id
LEFT JOIN sys.extended_properties ep
ON ep.major_id = p.object_id
AND ep.minor_id = 0
AND ep.name = 'MS_Description'
ORDER BY s.name, p.name;
`);

const rawProcedures = await mapResponse(response);

return rawProcedures.map(parseProcedure);
return rawProcedures.map(parseProcedure(logger));
};

module.exports = {
Expand Down
51 changes: 35 additions & 16 deletions reverse_engineering/helpers/parsers/parseProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @property {string} schema_name
* @property {string} procedure_name
* @property {string|null} procedure_body
* @property {string} [description]
*
* @typedef {object} Procedure
* @property {string} name
Expand All @@ -14,10 +15,11 @@
* @property {string} [recompile]
* @property {string} [forReplication]
* @property {string} [executeAs]
* @property {string} [description]
*/

const createProcedureRegexp =
/CREATE(?<orReplace>\s*OR\s*ALTER)?\s*(?:PROC|PROCEDURE)\s*(?:[^\s(]+)\s*(?<inputArgs>\((?:[^()']+|'[^']*'|\([^()]*\))*\)|(?:\s*@\w+[^@]*?)*?)?\s*(?<parameters>(?:WITH\s*(?:ENCRYPTION|RECOMPILE|,\s*|EXECUTE\s*AS\s*(?:OWNER|CALLER|SELF|'[^']+'))+)(?:\s*FOR\s*REPLICATION)?)?\s*AS\s*(?<body>[\s\S]*)/im;
/CREATE(?<orReplace>\s*OR\s*ALTER)?\s*(?:PROC|PROCEDURE)\s*(?:[^\s(]+)\s*(?<inputArgs>\((?:[^()']+|'[^']*'|\([^()]*\))*\)|(?:\s*@\w+[^@]*?)*?)?\s*(?<parameters>(?:WITH\s*(?:ENCRYPTION|RECOMPILE|,\s*|EXECUTE\s*AS\s*(?:OWNER|CALLER|SELF|'[^']+'))+)?(?:\s*FOR\s*REPLICATION)?)?\s*AS\s*(?<body>[\s\S]*)/im;

Check warning on line 22 in reverse_engineering/helpers/parsers/parseProcedure.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rework this part of the regex to not match the empty string.

See more on https://sonarcloud.io/project/issues?id=hackolade_SQLServer&issues=AZ0uuUy4Ta3Fdhbhb-Mi&open=AZ0uuUy4Ta3Fdhbhb-Mi&pullRequest=189

Check warning on line 22 in reverse_engineering/helpers/parsers/parseProcedure.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Simplify this regular expression to reduce its complexity from 113 to the 20 allowed.

See more on https://sonarcloud.io/project/issues?id=hackolade_SQLServer&issues=AZ0uuUy4Ta3Fdhbhb-Mh&open=AZ0uuUy4Ta3Fdhbhb-Mh&pullRequest=189

Check warning on line 22 in reverse_engineering/helpers/parsers/parseProcedure.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rework this part of the regex to not match the empty string.

See more on https://sonarcloud.io/project/issues?id=hackolade_SQLServer&issues=AZ0uuUy4Ta3Fdhbhb-Mj&open=AZ0uuUy4Ta3Fdhbhb-Mj&pullRequest=189

const encryptionRegexp = /WITH[\s\S]*\b(?<value>ENCRYPTION)/i;
const recompileRegexp = /WITH[\s\S]*\b(?<value>RECOMPILE)/i;
Expand Down Expand Up @@ -48,31 +50,48 @@
};
/**
*
* @param {RawProcedure} rawProcedure
* @returns {Procedure}
* @param {{ log: (logType: string, error: Error, message: string) => void }} logger
* @returns {(rawProcedure: RawProcedure) => Procedure}
*/
const parseProcedure = rawProcedure => {
const { schema_name, procedure_name, procedure_body } = rawProcedure;
const parseProcedure = logger => rawProcedure => {
const { schema_name, procedure_name, procedure_body, description } = rawProcedure;

if (!procedure_body) {
return {
name: procedure_name,
schemaName: schema_name,
encryption: 'ENCRYPTION',
description,
};
}
const result = createProcedureRegexp.exec(procedure_body);
const { orReplace, inputArgs, parameters, body } = result.groups;
const procedureParameters = parseParameters(parameters);

return {
name: procedure_name,
schemaName: schema_name,
orReplace: !!orReplace,
inputArgs,
body,
...procedureParameters,
};
try {
const result = createProcedureRegexp.exec(procedure_body);
const { orReplace, inputArgs, parameters, body } = result.groups;
const procedureParameters = parseParameters(parameters);

return {
name: procedure_name,
schemaName: schema_name,
orReplace: !!orReplace,
inputArgs,
body,
description,
...procedureParameters,
};
} catch (error) {
logger.log(
'error',
{ message: error.message, stack: error.stack, error },
`Error parsing procedure ${schema_name}.${procedure_name}`,
);

return {
name: procedure_name,
schemaName: schema_name,
description,
};
}
};

module.exports = {
Expand Down
Loading