This repository has been 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
/
Copy pathscriptSelectAs.js
81 lines (57 loc) · 2.36 KB
/
scriptSelectAs.js
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
'use strict';
const sqlUtils = require('./scriptSqlUtils.js');
const colNameOrdinal = 0;
async function getSqlScriptAsSelectAsync(connectionProfile, tableCatalog, tableSchema, tableName)
{
let provider = connectionProfile.providerName;
let queryText = "[FAILED TO RESOLVE QUERY TEXT]";
if (provider === "MSSQL") {
queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
}
else if (provider === "MySQL") {
queryText = sqlUtils.getColumnInfoQueryMySql(tableCatalog, tableSchema, tableName);
}
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, provider, queryText, tableCatalog);
if (!results || results.rowCount === 0) {
throw "No results";
}
let selectSqlScript = "...";
if (provider === "MSSQL") {
selectSqlScript = buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName);
}
else if (provider === "MySQL") {
selectSqlScript = buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName);
}
return selectSqlScript;
}
function buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName)
{
let fullScript = [];
let columsScriptPart = [];
fullScript.push("SELECT ");
let columnIndex = 0;
for (let i= 0; i !== results.rowCount; i++)
{
let rowData = results.rows[i];
const separator = (columnIndex === 0) ? " " : ",";
columsScriptPart.push("\t\t" + separator + "[" + rowData[colNameOrdinal].displayValue + "]");
columnIndex += 1;
}
return fullScript.concat(columsScriptPart).concat([`FROM [${tableCatalog}].[${tableSchema}].[${tableName}] `]).join('\n');
}
function buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName)
{
let fullScript = [];
let columsScriptPart = [];
fullScript.push("SELECT ");
let columnIndex = 0;
for (let i= 0; i !== results.rowCount; i++)
{
let rowData = results.rows[i];
const separator = (columnIndex === 0) ? " " : ",";
columsScriptPart.push("\t\t" + separator + "`" + rowData[colNameOrdinal].displayValue + "`");
columnIndex += 1;
}
return fullScript.concat(columsScriptPart).concat([`FROM \`${tableSchema}\`.\`${tableName}\` `, `LIMIT 1000`, `;`]).join('\n');
}
module.exports.getSqlScriptAsSelectAsync = getSqlScriptAsSelectAsync;