Skip to content

Commit e45fe75

Browse files
committed
Improved UX for using statement prefixes
Signed-off-by: worksofliam <[email protected]>
1 parent 2f922da commit e45fe75

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/views/results/index.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ export function initialise(context: vscode.ExtensionContext) {
207207

208208
} else {
209209
// Otherwise... it's a bit complicated.
210+
resultSetProvider.setLoadingText(`Executing SQL statement...`);
211+
210212
const data = await JobManager.runSQL(statementDetail.content, undefined);
211213

212214
if (data.length > 0) {
@@ -226,29 +228,42 @@ export function initialise(context: vscode.ExtensionContext) {
226228
case `sql`:
227229
const keys = Object.keys(data[0]);
228230

229-
const insertStatement = [
230-
`insert into TABLE (`,
231-
` ${keys.join(`, `)}`,
232-
`) values `,
233-
data.map(
234-
row => ` (${keys.map(key => {
235-
if (row[key] === null) return `null`;
236-
if (typeof row[key] === `string`) return `'${String(row[key]).replace(/'/g, `''`)}'`;
237-
return row[key];
238-
}).join(`, `)})`
239-
).join(`,\n`),
240-
];
241-
content = insertStatement.join(`\n`);
231+
// split array into groups of 1k
232+
const insertLimit = 1000;
233+
const dataChunks = [];
234+
for (let i = 0; i < data.length; i += insertLimit) {
235+
dataChunks.push(data.slice(i, i + insertLimit));
236+
}
237+
238+
content = `-- Generated ${dataChunks.length} insert statement${dataChunks.length === 1 ? `` : `s`}\n\n`;
239+
240+
for (const data of dataChunks) {
241+
const insertStatement = [
242+
`insert into TABLE (`,
243+
` ${keys.join(`, `)}`,
244+
`) values `,
245+
data.map(
246+
row => ` (${keys.map(key => {
247+
if (row[key] === null) return `null`;
248+
if (typeof row[key] === `string`) return `'${String(row[key]).replace(/'/g, `''`)}'`;
249+
return row[key];
250+
}).join(`, `)})`
251+
).join(`,\n`),
252+
];
253+
content += insertStatement.join(`\n`) + `;\n`;
254+
}
242255
break;
243256
}
244257

245258
const textDoc = await vscode.workspace.openTextDocument({ language: statementDetail.qualifier, content });
246259
await vscode.window.showTextDocument(textDoc);
260+
resultSetProvider.setLoadingText(`Query executed with ${data.length} rows returned.`);
247261
break;
248262
}
249263

250264
} else {
251265
vscode.window.showInformationMessage(`Query executed with no data returned.`);
266+
resultSetProvider.setLoadingText(`Query executed with no data returned.`);
252267
}
253268
}
254269
}

0 commit comments

Comments
 (0)