Skip to content

Commit

Permalink
WIP #261 Add a SQL query console to allow user to query data directly…
Browse files Browse the repository at this point in the history
… from file.
  • Loading branch information
cugarteblair committed Feb 5, 2025
1 parent b2304f2 commit 84b528f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions web-client/src/components/SrDuckDbShell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
<Button :disabled="isLoading" @click="executeQuery">
{{ isLoading ? "Running..." : "Run" }}
</Button>
<Button
v-if="rows.length > 0"
@click="exportToCSV"
style="margin-left: 1rem;"
>
Export CSV
</Button>

<!-- Error Display -->
<p v-if="error" class="error">
Expand Down Expand Up @@ -77,6 +84,43 @@ function getSql(){
return sqlStmnt.trim();
}
function exportToCSV(): void {
// If no rows, do nothing
if (!rows.value || rows.value.length === 0) {
return;
}
// 1. Create CSV header from columns
let csvContent = columns.value.join(',') + '\n';
// 2. Append rows
rows.value.forEach(row => {
// Convert each cell to string, handle commas, quotes if needed
const rowData = columns.value.map(col => {
// Convert "undefined" or null values to empty string
const cellValue = row[col] == null ? '' : String(row[col]);
// Optional: handle quotes, escape commas, etc. if you want more robust CSV
return `"${cellValue.replace(/"/g, '""')}"`;
});
csvContent += rowData.join(',') + '\n';
});
// 3. Create a Blob object for the CSV data
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
// 4. Create a temporary link to download the Blob
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', 'export.csv');
link.style.visibility = 'hidden';
// 5. Trigger the download
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
onMounted(async () => {
try {
duckDbClient = await createDuckDbClient();
Expand Down

0 comments on commit 84b528f

Please sign in to comment.