forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (174 loc) · 4.58 KB
/
index.js
File metadata and controls
188 lines (174 loc) · 4.58 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const mssql = require('mssql');
const { formatSchemaQueryResults } = require('../utils');
const id = 'sqlserver';
const name = 'SQL Server';
const SCHEMA_SQL = `
SELECT
t.table_schema,
t.table_name,
c.column_name,
c.data_type
FROM
INFORMATION_SCHEMA.TABLES t
JOIN INFORMATION_SCHEMA.COLUMNS c ON t.table_schema = c.table_schema AND t.table_name = c.table_name
WHERE
t.table_schema NOT IN ('information_schema')
ORDER BY
t.table_schema,
t.table_name,
c.ordinal_position
`;
/**
* Run query for connection
* Should return { rows, incomplete }
* @param {string} query
* @param {object} connection
*/
function runQuery(query, connection) {
const config = {
user: connection.username,
password: connection.password,
server: connection.host,
port: connection.port ? parseInt(connection.port, 10) : 1433,
database: connection.database,
domain: connection.domain,
// Set timeout to 1 hour for long running query support
requestTimeout: 1000 * 60 * 60,
options: {
appName: 'SQLPad',
encrypt: Boolean(connection.sqlserverEncrypt),
multiSubnetFailover: connection.sqlserverMultiSubnetFailover,
readOnlyIntent: connection.readOnlyIntent,
// Set enableArithAbort to avoid following log message:
// tedious deprecated The default value for `config.options.enableArithAbort`
// will change from `false` to `true` in the next major version of `tedious`.
// Set the value to `true` or `false` explicitly to silence this message. ../../node_modules/mssql/lib/tedious/connection-pool.js:61:23
enableArithAbort: true,
},
pool: {
max: 1,
min: 0,
idleTimeoutMillis: 1000,
},
};
let incomplete;
const rows = [];
return new Promise((resolve, reject) => {
const pool = new mssql.ConnectionPool(config, (err) => {
if (err) {
return reject(err);
}
const request = new mssql.Request(pool);
// Stream set a config level doesn't seem to work
request.stream = true;
request.query(query);
request.on('row', (row) => {
// Special handling if columns were not given names
if (row[''] && row[''].length) {
for (let i = 0; i < row[''].length; i++) {
row['UNNAMED COLUMN ' + (i + 1)] = row[''][i];
}
delete row[''];
}
if (rows.length < connection.maxRows) {
return rows.push(row);
}
// If reached it means we received a row event for more than maxRows
// If we haven't flagged incomplete yet, flag it,
// Resolve what we have and cancel request
// Note that this will yield a cancel error
if (!incomplete) {
incomplete = true;
resolve({ rows, incomplete });
request.cancel();
}
});
// Error events may fire multiple times
// If we get an ECANCEL error and too many rows were handled it was intentional
request.on('error', (err) => {
if (err.code === 'ECANCEL' && incomplete) {
return;
}
return reject(err);
});
// Always emitted as the last one
request.on('done', () => {
resolve({ rows, incomplete });
pool.close();
});
});
pool.on('error', (err) => reject(err));
});
}
/**
* Test connectivity of connection
* @param {*} connection
*/
function testConnection(connection) {
const query = "SELECT 'success' AS TestQuery;";
return runQuery(query, connection);
}
/**
* Get schema for connection
* @param {*} connection
*/
function getSchema(connection) {
return runQuery(SCHEMA_SQL, connection).then((queryResult) =>
formatSchemaQueryResults(queryResult)
);
}
const fields = [
{
key: 'host',
formType: 'TEXT',
label: 'Host/Server/IP Address',
},
{
key: 'port',
formType: 'TEXT',
label: 'Port (optional)',
},
{
key: 'database',
formType: 'TEXT',
label: 'Database',
},
{
key: 'username',
formType: 'TEXT',
label: 'Database Username',
},
{
key: 'password',
formType: 'PASSWORD',
label: 'Database Password',
},
{
key: 'domain',
formType: 'TEXT',
label: 'Domain',
},
{
key: 'sqlserverEncrypt',
formType: 'CHECKBOX',
label: 'Encrypt (necessary for Azure)',
},
{
key: 'sqlserverMultiSubnetFailover',
formType: 'CHECKBOX',
label: 'MultiSubnetFailover',
},
{
key: 'readOnlyIntent',
formType: 'CHECKBOX',
label: 'ReadOnly Application Intent',
},
];
module.exports = {
id,
name,
fields,
getSchema,
runQuery,
testConnection,
};