forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute-batch.js
More file actions
194 lines (177 loc) · 5.97 KB
/
Copy pathexecute-batch.js
File metadata and controls
194 lines (177 loc) · 5.97 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
189
190
191
192
193
194
/* eslint-disable no-await-in-loop */
const ConnectionClient = require('./connection-client');
/**
* Execute a query using batch/statement infrastructure
* Batch must already be created.
* @param {Object} config
* @param {import('../models/index')} models
* @param {import('./webhooks')} webhooks
* @param {string} batchId
*/
async function executeBatch(config, models, webhooks, batchId) {
const batch = await models.batches.findOneById(batchId);
const user = await models.users.findOneById(batch.userId);
const connection = await models.connections.findOneById(batch.connectionId);
// Get existing connectionClient if one was specified to use per batch
// Otherwise create a new one and connect it if the ConnectionClient supports it.
// If a new connectionClient was created *and* connected, take note to disconnect later
let connectionClient;
let disconnectOnFinish = false;
if (batch.connectionClientId) {
connectionClient = models.connectionClients.getOneById(
batch.connectionClientId
);
if (!connectionClient) {
throw new Error('Connection client disconnected');
}
} else {
connectionClient = new ConnectionClient(connection, user);
// If connectionClient supports the "Client" driver,
// and it is not connected, connect it
if (connectionClient.Client && !connectionClient.isConnected()) {
await connectionClient.connect();
disconnectOnFinish = true;
}
}
// run statements
const batchStartTime = new Date();
let stopTime;
let queryResult;
let statementError;
let statementStartTime;
for (const statement of batch.statements) {
statementStartTime = new Date();
const input = connectionClient.driver['asynchronous']
? statement.executionId
: statement.statementText;
try {
await models.statements.updateStarted(statement.id, statementStartTime);
queryResult = await connectionClient.runQuery(input);
stopTime = new Date();
await models.statements.updateFinished(
statement.id,
queryResult,
stopTime,
stopTime - statementStartTime
);
webhooks.statementFinished(user, connection, batch, statement.id);
} catch (error) {
statementError = error;
stopTime = new Date();
await models.statements.updateErrored(
statement.id,
{
title: error.message,
},
stopTime,
stopTime - statementStartTime
);
const updatedBatch = await models.batches.update(batch.id, {
status: 'error',
stopTime,
durationMs: stopTime - batchStartTime,
});
webhooks.statementFinished(user, connection, updatedBatch, statement.id);
break;
}
}
stopTime = new Date();
if (statementError) {
await models.statements.updateErrorQueuedToCancelled(batchId);
}
if (!statementError) {
await models.batches.update(batch.id, {
status: 'finished',
stopTime,
durationMs: stopTime - batchStartTime,
});
}
if (disconnectOnFinish) {
await connectionClient.disconnect();
}
const finalBatch = await models.batches.findOneById(batchId);
webhooks.batchFinished(user, connection, finalBatch);
}
/**
* Execute a query using batch/statement infrastructure
* Batch must already be created.
* @param {Object} config
* @param {import('../models/index')} models
* @param {import('./webhooks')} webhooks
* @param {string} batchId
*/
async function executeCancellableBatch(config, models, webhooks, batchId) {
const batch = await models.batches.findOneById(batchId);
const user = await models.users.findOneById(batch.userId);
const connection = await models.connections.findOneById(batch.connectionId);
// Get existing connectionClient if one was specified to use per batch
// Otherwise create a new one and connect it if the ConnectionClient supports it.
// If a new connectionClient was created *and* connected, take note to disconnect later
let connectionClient;
let disconnectOnFinish = false;
if (batch.connectionClientId) {
connectionClient = models.connectionClients.getOneById(
batch.connectionClientId
);
if (!connectionClient) {
throw new Error('Connection client disconnected');
}
} else {
connectionClient = new ConnectionClient(connection, user);
// If connectionClient supports the "Client" driver,
// and it is not connected, connect it
if (connectionClient.Client && !connectionClient.isConnected()) {
await connectionClient.connect();
disconnectOnFinish = true;
}
}
// run statements
const batchStartTime = new Date();
let executionId;
let statementStartTime;
let stopTime;
let statementError;
for (const statement of batch.statements) {
try {
statementStartTime = new Date();
await models.statements.updateStarted(statement.id, statementStartTime);
if (connectionClient.driver['asynchronous']) {
// Set execution ID ASAP to enable cancellation
executionId = await connectionClient.startQueryExecution(
statement.statementText
);
await models.statements.updateExecutionId(statement.id, executionId);
}
} catch (error) {
statementError = error;
stopTime = new Date();
await models.statements.updateErrored(
statement.id,
{
title: error.message,
},
stopTime,
stopTime - statementStartTime
);
const updatedBatch = await models.batches.update(batch.id, {
status: 'error',
stopTime,
durationMs: stopTime - batchStartTime,
});
webhooks.statementFinished(user, connection, updatedBatch, statement.id);
break;
}
}
if (statementError) {
await models.statements.updateErrorQueuedToCancelled(batchId);
}
if (disconnectOnFinish) {
await connectionClient.disconnect();
}
const finalBatch = await models.batches.findOneById(batchId);
webhooks.batchFinished(user, connection, finalBatch);
}
module.exports = {
executeBatch,
executeCancellableBatch,
};