This repository was archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathscriptSPAs.js
More file actions
131 lines (100 loc) · 3.59 KB
/
scriptSPAs.js
File metadata and controls
131 lines (100 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const sqlUtils = require('./scriptSqlUtils.js');
const colNameOrdinal = 0;
const defaultNoQueryText = "[FAILED TO RESOLVE QUERY TEXT]";
const defaultNoParamText = "[FAILED TO RESOLVE PARAM TEXT]";
async function getSqlScriptAsDropAndCreateStoredProcedureAsync(
connectionProfile,
tableCatalog,
tableSchema,
routineName
) {
let provider = connectionProfile.providerName;
let queryText = defaultNoQueryText;
let paramText = defaultNoParamText;
if (provider === "MSSQL")
{
queryText = sqlUtils.getRoutineInfoQuerySql(tableCatalog, tableSchema, routineName);
}
else if (provider === "MySQL")
{
queryText = sqlUtils.getRoutineInfoQueryMySql(tableCatalog, tableSchema, routineName);
paramText = sqlUtils.getRoutineParamsQueryMySql(tableCatalog, tableSchema, routineName);
}
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, provider, queryText);
if (!results || results.rowCount === 0) {
throw "No query results returned";
}
let updateSqlScript = "...";
if (provider === "MSSQL")
{
updateSqlScript = buildFinalScriptMSSQL(results, tableCatalog, tableSchema, routineName);
}
else if (provider === "MySQL")
{
const paramResults = await sqlUtils.getResultsFromQuerySql(connectionProfile, provider, paramText);
updateSqlScript = buildFinalScriptMySQL(results, paramResults, tableCatalog, tableSchema, routineName);
}
return updateSqlScript;
}
function buildFinalScriptMSSQL(results, tableCatalog, tableSchema, routineName) {
let fullScript = [];
let columsScriptPart = [];
fullScript.push(`DROP PROCEDURE IF EXISTS [${tableSchema}].[${routineName}];\n`);
fullScript.push(`GO \n\n`);
fullScript.push(`SET ANSI_NULLS ON;\n`);
fullScript.push(`SET QUOTED_IDENTIFIER ON;\n`);
fullScript.push(`GO \n`);
for (let i = 0; i !== results.rowCount; i++) {
let rowData = results.rows[i];
columsScriptPart.push(
rowData[colNameOrdinal].displayValue
);
}
return fullScript
.concat(columsScriptPart)
.join("");
}
function buildFinalScriptMySQL(results, paramResults, tableCatalog, tableSchema, routineName) {
const fullScript = [];
const columsScriptPart = [];
const firstRow = results.rows[0];
const definerRaw = firstRow[3].rawObject;
const dataAccessRaw = firstRow[5].rawObject;
const commentRaw = firstRow[8].rawObject;
fullScript.push(`DROP PROCEDURE IF EXISTS \`${routineName}\`;\n\n`);
fullScript.push(`CREATE DEFINER=\`${definerRaw}\` PROCEDURE \`${routineName}\`(`);
for (let i = 0; i !== paramResults.rowCount; i++)
{
const rowData = paramResults.rows[i];
const charMaxLenRaw = rowData[3].rawObject;
const charMaxLenPart = charMaxLenRaw == null || charMaxLenRaw.toLowerCase() == "null" ? "" : `(${charMaxLenRaw})`;
const isFirstRow = i === 0;
const isLastRow = i === paramResults.rowCount - 1;
const comma = isLastRow ? "" : ",";
if(isFirstRow){
columsScriptPart.push(`\n`);
}
columsScriptPart.push(
` ${rowData[0].rawObject} ${rowData[1].rawObject} ${rowData[2].rawObject}${charMaxLenPart}${comma} \n`
);
}
fullScript.push(`)`);
if(dataAccessRaw){
fullScript.push(`\n${dataAccessRaw}\n`);
}
if(commentRaw){
fullScript.push(`\nCOMMENT '${commentRaw}'\n`);
}
for (let i = 0; i !== results.rowCount; i++) {
let rowData = results.rows[i];
columsScriptPart.push(
rowData[colNameOrdinal].displayValue
);
}
return fullScript
.concat(columsScriptPart)
.join("");
}
module.exports.getSqlScriptAsDropAndCreateStoredProcedureAsync =
getSqlScriptAsDropAndCreateStoredProcedureAsync;