Skip to content

Commit e445a84

Browse files
committed
Update to allow csv without headers
1 parent 8acd56b commit e445a84

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

src/views/schemaBrowser/index.ts

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,17 @@ export default class schemaBrowser {
452452
}
453453

454454
let rows: any[] = [];
455-
455+
let hasHeaders = true;
456456
if (ext === `csv`) {
457+
hasHeaders = (await vscode.window.showQuickPick(['Yes','No'], { placeHolder: 'Does the file have headers?' })) === `Yes` ? true : false;
457458
rows = parse(data, {
458-
columns: true,
459+
columns: hasHeaders,
459460
cast: true
460461
});
461462
if (!rows.length) {
462463
vscode.window.showWarningMessage('No rows found.');
463464
return;
464465
}
465-
466-
// Get the headers and types
467-
// Using the first two rows, first row is header names, second row will tell us the type
468-
469466
} else if (ext === `json`) {
470467
rows = JSON.parse(data);
471468
if (!Array.isArray(rows)) {
@@ -478,28 +475,47 @@ export default class schemaBrowser {
478475
return;
479476
}
480477

481-
// Get headers using the first row of data
482-
const colNames = Object.keys(rows[0]);
483-
const cols = colNames.join(', ');
484-
485-
// Generate the INSERT statement
486-
let content: string = `INSERT INTO SYSIBM.SYSDUMMY1 (${cols}) \nVALUES\n`;
487-
for (let i = 0; i < rows.length; i++) {
488-
const row = rows[i];
489-
let allValues = [];
490-
for(const col of colNames) {
491-
const val = row[col];
492-
if (typeof val === `string`) {
493-
allValues.push(`'${val}'`);
494-
} else
495-
allValues.push(val);
496-
}
497-
content += ` (${allValues.join(', ')})`;
498-
499-
// If not at last item yet, append a comma
500-
if (i != rows.length - 1) {
501-
content += `,\n`;
502-
}
478+
let content: string = ``;
479+
if(hasHeaders) {
480+
// Get headers using the first row of data
481+
const colNames = Object.keys(rows[0]);
482+
const cols = colNames.join(', ');
483+
484+
// Generate the INSERT statement
485+
content = `INSERT INTO SYSIBM.SYSDUMMY1 (${cols}) \nVALUES\n`;
486+
const allRowValues = [];
487+
for (let i = 0; i < rows.length; i++) {
488+
const row = rows[i];
489+
let allValues = [];
490+
for(const col of colNames) {
491+
const val = row[col];
492+
if (typeof val === `string`) {
493+
allValues.push(`'${val}'`);
494+
} else {
495+
allValues.push(val);
496+
}
497+
}
498+
allRowValues.push(` (${allValues.join(', ')})`);
499+
}
500+
content += allRowValues.join(`,\n`);
501+
} else {
502+
// Generate the INSERT statement
503+
content = `INSERT INTO SYSIBM.SYSDUMMY1 \nVALUES\n`;
504+
const allRowValues = [];
505+
for (let i = 0; i < rows.length; i++) {
506+
const row = rows[i];
507+
let allValues = [];
508+
for(let j = 0; j < row.length; j++) {
509+
const val = row[j];
510+
if (typeof val === `string`) {
511+
allValues.push(`'${val}'`);
512+
} else {
513+
allValues.push(val);
514+
}
515+
}
516+
allRowValues.push(` (${allValues.join(', ')})`);
517+
}
518+
content += allRowValues.join(`,\n`);
503519
}
504520

505521
content += `;`;

0 commit comments

Comments
 (0)