diff --git a/src/components/grid/DataGrid.vue b/src/components/grid/DataGrid.vue index 8de26282f..6999d57ff 100644 --- a/src/components/grid/DataGrid.vue +++ b/src/components/grid/DataGrid.vue @@ -1402,6 +1402,7 @@ const { databaseType: computed(() => props.databaseType), hasCellSelection, selectedCells, + selectedRange, contextCell: exportContextCell, getRowItem: (rowId: number) => visibleDisplayItems.value.find((item) => item.id === rowId), quoteIdent, diff --git a/src/composables/useDataGridExport.ts b/src/composables/useDataGridExport.ts index 0c9a9f002..ab9624207 100644 --- a/src/composables/useDataGridExport.ts +++ b/src/composables/useDataGridExport.ts @@ -7,6 +7,7 @@ import { formatSelectionAsJson, formatSelectionAsSqlInList, formatSelectionAsTsv, + type CellSelectionRange, type SelectionData, } from "@/lib/gridSelection"; import { useToast } from "@/composables/useToast"; @@ -31,6 +32,7 @@ export interface UseDataGridExportOptions { databaseType: ComputedRef; hasCellSelection: ComputedRef; selectedCells: ComputedRef; + selectedRange: ComputedRef; contextCell: | Ref<{ rowId: number; rowIndex: number; col: number } | null> | ComputedRef<{ rowId: number; rowIndex: number; col: number } | null>; @@ -50,6 +52,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) { tableMeta, hasCellSelection, selectedCells, + selectedRange, contextCell, getRowItem, quoteIdent, @@ -102,14 +105,24 @@ export function useDataGridExport(options: UseDataGridExportOptions) { } function copyRowAsInsert() { - if (!contextCell.value) return; - const item = getRowItem(contextCell.value.rowId); - if (!item) return; - const cols = columns.value.map((c) => quoteIdent(c)).join(", "); - const vals = item.data.map((v) => escapeVal(v)).join(", "); const table = tableMeta.value ? (tableMeta.value.schema ? `${quoteIdent(tableMeta.value.schema)}.` : "") + quoteIdent(tableMeta.value.tableName) : "table_name"; + const cols = columns.value.map((c) => quoteIdent(c)).join(", "); + const range = selectedRange.value; + if (range && range.startRow !== range.endRow) { + const items = displayItems.value.slice(range.startRow, range.endRow + 1); + const statements = items.map((item) => { + const vals = item.data.map((v) => escapeVal(v)).join(", "); + return `INSERT INTO ${table} (${cols}) VALUES (${vals});`; + }); + copyText(statements.join("\n")); + return; + } + if (!contextCell.value) return; + const item = getRowItem(contextCell.value.rowId); + if (!item) return; + const vals = item.data.map((v) => escapeVal(v)).join(", "); copyText(`INSERT INTO ${table} (${cols}) VALUES (${vals});`); }