feat(grid): support multi-row Copy as INSERT

When multiple rows are selected, copyRowAsInsert now generates an
INSERT statement for each selected row instead of only the
right-clicked row.

Closes #166
This commit is contained in:
t8y2 2026-05-12 23:39:26 +08:00
parent e1a07aae4a
commit 004b5645c5
2 changed files with 19 additions and 5 deletions

View File

@ -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,

View File

@ -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<string | undefined>;
hasCellSelection: ComputedRef<boolean>;
selectedCells: ComputedRef<SelectionData>;
selectedRange: ComputedRef<CellSelectionRange | null>;
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});`);
}