feat: stored procedures and funciton parameters support for mssql

This commit is contained in:
Nybkox
2024-11-26 17:05:12 +01:00
parent 35e9ff607d
commit 2b2ecac3ab
11 changed files with 162 additions and 14 deletions

View File

@@ -31,6 +31,18 @@ function simplifyComutedExpression(expr) {
return expr;
}
function getFullDataTypeName({ dataType, charMaxLength, numericScale, numericPrecision }) {
let fullDataType = dataType;
if (charMaxLength && isTypeString(dataType)) {
fullDataType = `${dataType}(${charMaxLength < 0 ? 'MAX' : charMaxLength})`;
}
if (numericPrecision && numericScale && isTypeNumeric(dataType)) {
fullDataType = `${dataType}(${numericPrecision},${numericScale})`;
}
return fullDataType;
}
function getColumnInfo({
isNullable,
isIdentity,
@@ -43,13 +55,12 @@ function getColumnInfo({
defaultConstraint,
computedExpression,
}) {
let fullDataType = dataType;
if (charMaxLength && isTypeString(dataType)) {
fullDataType = `${dataType}(${charMaxLength < 0 ? 'MAX' : charMaxLength})`;
}
if (numericPrecision && numericScale && isTypeNumeric(dataType)) {
fullDataType = `${dataType}(${numericPrecision},${numericScale})`;
}
const fullDataType = getFullDataTypeName({
dataType,
charMaxLength,
numericPrecision,
numericScale,
});
if (defaultValue) {
defaultValue = defaultValue.trim();
@@ -116,7 +127,11 @@ class MsSqlAnalyser extends DatabaseAnalyser {
this.feedback({ analysingMessage: 'Loading views' });
const viewsRows = await this.analyserQuery('views', ['views']);
this.feedback({ analysingMessage: 'Loading procedures & functions' });
const programmableRows = await this.analyserQuery('programmables', ['procedures', 'functions']);
const prodceureParameterRows = await this.analyserQuery('proceduresParameters');
const functionParameterRows = await this.analyserQuery('functionParameters');
this.feedback({ analysingMessage: 'Loading view columns' });
const viewColumnRows = await this.analyserQuery('viewColumns', ['views']);
@@ -157,20 +172,46 @@ class MsSqlAnalyser extends DatabaseAnalyser {
columns: viewColumnRows.rows.filter(col => col.objectId == row.objectId).map(getColumnInfo),
}));
const prodceureParameter = prodceureParameterRows.rows.map(row => ({
...row,
fullDataType: getFullDataTypeName(row),
}));
const prodceureToParameters = prodceureParameter.reduce((acc, parameter) => {
if (!acc[parameter.parentObjectId]) acc[parameter.parentObjectId] = [];
acc[parameter.parentObjectId].push(parameter);
return acc;
}, {});
const procedures = programmableRows.rows
.filter(x => x.sqlObjectType.trim() == 'P')
.map(row => ({
...row,
contentHash: row.modifyDate && row.modifyDate.toISOString(),
createSql: getCreateSql(row),
parameters: prodceureToParameters[row.objectId],
}));
const functionParameters = functionParameterRows.rows.map(row => ({
...row,
fullDataType: getFullDataTypeName(row),
}));
const functionToParameters = functionParameters.reduce((acc, parameter) => {
if (!acc[parameter.parentObjectId]) acc[parameter.parentObjectId] = [];
acc[parameter.parentObjectId].push(parameter);
return acc;
}, {});
const functions = programmableRows.rows
.filter(x => ['FN', 'IF', 'TF'].includes(x.sqlObjectType.trim()))
.map(row => ({
...row,
contentHash: row.modifyDate && row.modifyDate.toISOString(),
createSql: getCreateSql(row),
parameters: functionToParameters[row.objectId],
}));
this.feedback({ analysingMessage: null });

View File

@@ -0,0 +1,26 @@
module.exports = `
SELECT
o.object_id as parentObjectId,
p.object_id AS parameterObjectId,
CASE
WHEN p.name IS NULL OR LTRIM(RTRIM(p.name)) = '' THEN
'@Output'
ELSE
p.name
END AS pureName,
TYPE_NAME(p.user_type_id) AS dataType,
p.max_length AS charMaxLength,
p.precision AS precision,
p.scale AS scale,
p.is_output AS isOutputParameter,
p.parameter_id AS parameterIndex
FROM
sys.objects o
JOIN
sys.parameters p ON o.object_id = p.object_id
WHERE
o.type IN ('FN', 'IF', 'TF')
ORDER BY
p.object_id,
p.parameter_id;
`;

View File

@@ -7,6 +7,8 @@ const modifications = require('./modifications');
const loadSqlCode = require('./loadSqlCode');
const views = require('./views');
const programmables = require('./programmables');
const proceduresParameters = require('./proceduresParameters');
const functionParameters = require('./functionParameters');
const viewColumns = require('./viewColumns');
const indexes = require('./indexes');
const indexcols = require('./indexcols');
@@ -20,6 +22,8 @@ module.exports = {
loadSqlCode,
views,
programmables,
proceduresParameters,
functionParameters,
viewColumns,
indexes,
indexcols,

View File

@@ -0,0 +1,21 @@
module.exports = `
SELECT
o.object_id as parentObjectId,
p.object_id as objectId,
p.name AS pureName,
TYPE_NAME(p.user_type_id) AS dataType,
p.max_length AS charMaxLength,
p.precision AS precision,
p.scale AS scale,
p.is_output AS isOutputParameter,
p.parameter_id AS parameterIndex
FROM
sys.objects o
JOIN
sys.parameters p ON o.object_id = p.object_id
WHERE
o.type = 'P'
ORDER BY
o.object_id,
p.parameter_id;
`;