Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): support geometry and WKT preview by selection

lxk9550459bec9bab1
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index e6c82d4973..957813652b 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -172,6 +172,7 @@ import { displayCellValue, firstLineCellDisplayValue, gridCellDisplayValue, limi
import { cellExternalUrl } from "@/lib/dataGrid/cellExternalUrl";
import { getApplicablePreviewActions, type PreviewAction } from "@/lib/dataGrid/resultPreviewRegistry";
import "@/lib/dataGrid/geometryMapPreview";
+import { isHexGeometry, looksLikeWkt, parseWktGeometry } from "@/lib/dataGrid/geometryPreview";
import {
BINARY_CELL_DOWNLOAD_MODES,
BinaryCellImportTooLargeError,
@@ -6668,7 +6669,7 @@ function restoreTabSwitchViewSnapshot() {
const multiRowCount = computed(() => {
if (hasRowSelection.value) return selectedRowCount.value;
const range = selectedRange.value;
- if (range && range.startRow !== range.endRow) return range.endRow - range.startRow + 1;
+ if (range && range.startRow !== range.endRow) return Math.abs(range.endRow - range.startRow) + 1;
return 1;
});
@@ -6848,8 +6849,10 @@ function affectedRowIds(): number[] {
}
const range = selectedRange.value;
if (range && range.startRow !== range.endRow) {
+ const minRow = Math.min(range.startRow, range.endRow);
+ const maxRow = Math.max(range.startRow, range.endRow);
return displayRowRefs.value
- .slice(range.startRow, range.endRow + 1)
+ .slice(minRow, maxRow + 1)
.filter((ref) => !("isDraft" in ref && ref.isDraft) && transposeIncludesRow(ref.id))
.map((ref) => ref.id);
}
@@ -7068,10 +7071,57 @@ function exportSelectedCellsTxt() {
return exportTxt(target);
}
+// Preview actions index columns by actual data position; the cell selection
+// matrix and column selection track visible grid positions.
+function cellSelectionTargetWithActualColumns(cellTarget: { rowIds: number[]; columnIndexes: number[] }): { rowIds: number[]; columnIndexes: number[] } {
+ return { rowIds: cellTarget.rowIds, columnIndexes: cellTarget.columnIndexes.map(actualColumnIndex) };
+}
+
+function previewActionTargetSelection(): { rowIds: number[]; columnIndexes?: number[] } {
+ if (hasRowSelection.value && selectedRowCount.value > 0) {
+ const rowIds = [...selectedRowIds.value].filter((rowId) => !getRowItem(rowId)?.isDraft);
+ const columnIndexes = hasSelectedColumns.value ? targetColumnIndexes().map(actualColumnIndex) : undefined;
+ return { rowIds, columnIndexes };
+ }
+ if (contextCell.value && contextCell.value.rowId !== undefined) {
+ const item = getRowItem(contextCell.value.rowId);
+ const colIdx = contextCell.value.col >= 0 ? contextCell.value.col : undefined;
+ if (item && !item.isDraft) {
+ const cellTarget = activeCellSelectionTarget();
+ if (cellTarget && cellTarget.rowIds.includes(item.id)) {
+ return cellSelectionTargetWithActualColumns(cellTarget);
+ }
+ return { rowIds: [item.id], columnIndexes: colIdx !== undefined ? [colIdx] : undefined };
+ }
+ }
+ const cellTarget = activeCellSelectionTarget();
+ if (cellTarget && cellTarget.rowIds.length > 0) {
+ return cellSelectionTargetWithActualColumns(cellTarget);
+ }
+ const range = selectedRange.value;
+ if (range) {
+ const minRow = Math.min(range.startRow, range.endRow);
+ const maxRow = Math.max(range.startRow, range.endRow);
+ const rowIds = displayRowRefs.value
+ .slice(minRow, maxRow + 1)
+ .filter((ref) => !("isDraft" in ref && ref.isDraft) && transposeIncludesRow(ref.id))
+ .map((ref) => ref.id);
+ const minCol = Math.min(range.startCol, range.endCol);
+ const maxCol = Math.max(range.startCol, range.endCol);
+ const columnIndexes = Array.from({ length: maxCol - minCol + 1 }, (_, i) => actualColumnIndex(minCol + i));
+ if (rowIds.length > 0) {
+ return { rowIds, columnIndexes };
+ }
+ }
+ return { rowIds: [] };
+}
+
function executePreviewAction(action: PreviewAction) {
+ const target = previewActionTargetSelection();
const config = action.execute({
result: props.result,
- selectedRowIds: affectedRowIds(),
+ selectedRowIds: target.rowIds,
+ selectedColumnIndexes: target.columnIndexes,
displayRowRefs: displayRowRefs.value.filter((ref) => "sourceIndex" in ref),
});
if (config) {
@@ -14385,8 +14435,13 @@ const gridContextMenuItems = computed<ContextMenuItem[]>(() => {
const gridSnapshotContext = contextHeaderColumn.value && hasColumnSelection.value ? "columns" : contextCell.value?.col === -1 && affectedRowIds().length > 0 ? "rows" : contextCell.value && hasCellSelection.value && selectedCellMatrix.value ? "cells" : null;
const previewItems: ContextMenuItem[] = [];
if (!contextHeaderColumn.value && contextCell.value) {
- const colType = props.result.column_types?.[contextCell.value.col];
- if (colType && isGeometryColumnType(colType)) {
+ const colIdx = contextCell.value.col;
+ const colType = props.result.column_types?.[colIdx];
+ const isSpatial = props.result.spatial_columns?.some((sc) => sc.column_index === colIdx);
+ const item = contextCell.value.rowId !== undefined ? getRowItem(contextCell.value.rowId) : undefined;
+ const rawVal = item && colIdx >= 0 ? item.data[colIdx] : undefined;
+ const isWkt = typeof rawVal === "string" && !isHexGeometry(rawVal) && looksLikeWkt(rawVal) && !!parseWktGeometry(rawVal);
+ if ((colType && isGeometryColumnType(colType)) || isSpatial || isWkt) {
const actions = previewActions.value;
if (actions.length > 0) {
previewItems.push({ label: "", separator: true });
diff --git a/apps/desktop/src/components/grid/DataGridCellDetailDialog.vue b/apps/desktop/src/components/grid/DataGridCellDetailDialog.vue
index fb3645572a..4069608e34 100644
--- a/apps/desktop/src/components/grid/DataGridCellDetailDialog.vue
+++ b/apps/desktop/src/components/grid/DataGridCellDetailDialog.vue
@@ -12,8 +12,8 @@ import { useCellDetailEditor, type UseCellDetailEditorReturn } from "@/composabl
import { useTheme } from "@/composables/useTheme";
import { useSettingsStore } from "@/stores/settingsStore";
import { BINARY_CELL_DOWNLOAD_MODES, isBinaryCellColumnType, binaryCellUtf8Text, isBlobCellColumnType, type BinaryCellDownloadMode } from "@/lib/dataGrid/binaryCellDownload";
-import { cellDetailPreviewText, isGeometryColumnType } from "@/lib/dataGrid/cellDetailPresentation";
-import { isHexGeometry, renderWktOnCanvas } from "@/lib/dataGrid/geometryPreview";
+import { canPreviewCellGeometry, cellDetailPreviewText } from "@/lib/dataGrid/cellDetailPresentation";
+import { renderWktOnCanvas } from "@/lib/dataGrid/geometryPreview";
import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
import type { DatabaseType } from "@/types/database";
@@ -216,7 +216,7 @@ watch(
</DropdownMenuContent>
</DropdownMenu>
<!-- Unsupported geometry values can arrive as a hex fallback that cannot be rendered. -->
- <Popover v-if="isGeometryColumnType(detail.type) && detail.value !== null && !isHexGeometry(detail.value as string)" v-model:open="geometryPreviewOpen">
+ <Popover v-if="canPreviewCellGeometry(detail.value, detail.type)" v-model:open="geometryPreviewOpen">
<PopoverTrigger as-child>
<Button variant="ghost" size="icon" class="h-6 w-6" :title="t('grid.geometryPreview')">
<Eye class="h-3 w-3" />
diff --git a/apps/desktop/src/components/grid/DataGridCellDetailPanel.vue b/apps/desktop/src/components/grid/DataGridCellDetailPanel.vue
index c8de5521ed..222ff5415e 100644
--- a/apps/desktop/src/components/grid/DataGridCellDetailPanel.vue
+++ b/apps/desktop/src/components/grid/DataGridCellDetailPanel.vue
@@ -11,8 +11,7 @@ import { TabsContent } from "@/components/ui/tabs";
import TemporalCellEditor from "@/components/grid/TemporalCellEditor.vue";
import { useDataGridCellDetail } from "@/composables/useDataGridCellDetail";
import { BINARY_CELL_DOWNLOAD_MODES, isBinaryCellColumnType, binaryCellUtf8Text, isBlobCellColumnType, type BinaryCellDownloadMode } from "@/lib/dataGrid/binaryCellDownload";
-import { cellDetailPreviewText, isGeometryColumnType } from "@/lib/dataGrid/cellDetailPresentation";
-import { isHexGeometry } from "@/lib/dataGrid/geometryPreview";
+import { canPreviewCellGeometry, cellDetailPreviewText } from "@/lib/dataGrid/cellDetailPresentation";
import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
import type { TemporalCellEditorConfig } from "@/lib/dataGrid/dataGridTemporalEditor";
import type { DatabaseType } from "@/types/database";
@@ -194,7 +193,7 @@ defineExpose({ openSearch });
><DropdownMenuItem v-for="mode in BINARY_CELL_DOWNLOAD_MODES" :key="mode" @click="downloadBinaryValue(detail, mode)">{{ t(`grid.binaryDownload.${mode}`) }}</DropdownMenuItem></DropdownMenuContent
></DropdownMenu
>
- <Popover v-if="isGeometryColumnType(detail.type) && detail.value !== null && !editing && !isHexGeometry(detail.value as string)" v-model:open="geometryPreviewOpen"
+ <Popover v-if="!editing && canPreviewCellGeometry(detail.value, detail.type)" v-model:open="geometryPreviewOpen"
><PopoverTrigger as-child
><Button variant="ghost" size="icon" class="h-5 w-5" :title="t('grid.geometryPreview')"><Eye class="h-3 w-3" /></Button></PopoverTrigger
><PopoverContent class="w-auto p-1.5" align="end"><canvas v-show="geometryPreviewOpen" ref="geometryCanvas" width="400" height="280" class="block rounded" /></PopoverContent
diff --git a/apps/desktop/src/components/grid/__tests__/DataGridSurfaces.spec.ts b/apps/desktop/src/components/grid/__tests__/DataGridSurfaces.spec.ts
index b6807c0c38..bd26196d0b 100644
--- a/apps/desktop/src/components/grid/__tests__/DataGridSurfaces.spec.ts
+++ b/apps/desktop/src/components/grid/__tests__/DataGridSurfaces.spec.ts
@@ -84,7 +84,7 @@ vi.mock("@/stores/settingsStore", async () => {
};
return { ...actual, useSettingsStore: () => settingsMock.store };
});
-vi.mock("@/lib/dataGrid/geometryPreview", () => ({ isHexGeometry: () => false, renderWktOnCanvas: mocks.renderWkt }));
+vi.mock("@/lib/dataGrid/geometryPreview", () => ({ isHexGeometry: () => false, looksLikeWkt: () => false, parseWktGeometry: () => null, renderWktOnCanvas: mocks.renderWkt }));
vi.mock("@/lib/common/clipboard", () => ({ copyToClipboard: mocks.copyToClipboard }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
vi.mock("@/composables/useDataGridCellDetail", async () => {
diff --git a/apps/desktop/src/lib/__tests__/dataGrid/cellDetailPresentation.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/cellDetailPresentation.spec.ts
index 824587a629..0d9041bb79 100644
--- a/apps/desktop/src/lib/__tests__/dataGrid/cellDetailPresentation.spec.ts
+++ b/apps/desktop/src/lib/__tests__/dataGrid/cellDetailPresentation.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { compactJsonText, formatJsonText, looksLikeJsonContainerText, valueEditorActions } from "@/lib/dataGrid/cellDetailPresentation";
+import { canPreviewCellGeometry, compactJsonText, formatJsonText, isGeometryColumnType, looksLikeJsonContainerText, valueEditorActions } from "@/lib/dataGrid/cellDetailPresentation";
describe("cellDetailPresentation", () => {
it("offers compact JSON beside format JSON for editable JSON values", () => {
@@ -16,4 +16,34 @@ describe("cellDetailPresentation", () => {
// Scalar string in quotes is not treated as a container
expect(looksLikeJsonContainerText('"hello"')).toBe(false);
});
+
+ it("detects geometry values for preview from column type or WKT content", () => {
+ expect(isGeometryColumnType("geometry")).toBe(true);
+ expect(isGeometryColumnType("GEOGRAPHY")).toBe(true);
+ expect(isGeometryColumnType("geometry(Point, 4326)")).toBe(true);
+ expect(isGeometryColumnType("text")).toBe(false);
+
+ // Geometry column with valid WKT
+ expect(canPreviewCellGeometry("POINT(1 2)", "geometry")).toBe(true);
+ // Text column with WKT string (e.g. PostgreSQL ST_AsText or raw WKT)
+ expect(canPreviewCellGeometry("POINT(116.397 39.908)", "text")).toBe(true);
+ expect(canPreviewCellGeometry("POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", "varchar")).toBe(true);
+ expect(canPreviewCellGeometry("SRID=4326;POINT(1 2)", "text")).toBe(true);
+ expect(canPreviewCellGeometry("MULTIPOLYGON EMPTY", "text")).toBe(true);
+ expect(canPreviewCellGeometry("LINESTRING Z (1 2 3, 4 5 6)", "text")).toBe(true);
+ expect(canPreviewCellGeometry("point(1 2)", "text")).toBe(true);
+
+ // Non-geometry strings or null
+ expect(canPreviewCellGeometry("just a normal string", "text")).toBe(false);
+ expect(canPreviewCellGeometry(null, "geometry")).toBe(false);
+ expect(canPreviewCellGeometry(undefined, "text")).toBe(false);
+ expect(canPreviewCellGeometry(123, "text")).toBe(false);
+
+ // Ordinary text that the lenient WKT parser alone would accept
+ expect(canPreviewCellGeometry("No records (0)", "text")).toBe(false);
+ expect(canPreviewCellGeometry("FY report (2024)", "text")).toBe(false);
+ expect(canPreviewCellGeometry("result is empty", "text")).toBe(false);
+ expect(canPreviewCellGeometry("empty", "text")).toBe(false);
+ expect(canPreviewCellGeometry("Meeting notes (see attached 3 items)", "text")).toBe(false);
+ });
});
diff --git a/apps/desktop/src/lib/__tests__/dataGrid/geometryMapPreview.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/geometryMapPreview.spec.ts
index c323b65f57..8af3353edf 100644
--- a/apps/desktop/src/lib/__tests__/dataGrid/geometryMapPreview.spec.ts
+++ b/apps/desktop/src/lib/__tests__/dataGrid/geometryMapPreview.spec.ts
@@ -136,4 +136,61 @@ describe("buildGeometryMapFeatureCollection", () => {
expect(collection?.features.map((feature) => feature.properties._srid)).toEqual([3857, 4326]);
expect(collection?.features.map((feature) => feature.properties.name)).toEqual(["mercator", "wgs84"]);
});
+
+ it("filters preview features according to selectedRowIds", () => {
+ const ctx = context();
+ // displayRowRefs: [{ id: 20, sourceIndex: 1 }, { id: 10, sourceIndex: 0 }, { id: 30, sourceIndex: 2 }]
+ ctx.selectedRowIds = [10];
+
+ const collection = buildGeometryMapFeatureCollection(ctx);
+ expect(collection?.features).toHaveLength(1);
+ expect(collection?.features[0].properties.name).toBe("wgs84");
+ });
+
+ it("filters preview features according to selectedColumnIndexes", () => {
+ const ctx = context();
+ ctx.result.columns = ["shape_a", "shape_b", "name"];
+ ctx.result.column_types = ["geometry", "geometry", "text"];
+ ctx.result.rows = [["POINT(1 1)", "POINT(2 2)", "first"]];
+ ctx.displayRowRefs = [{ id: 1, sourceIndex: 0, isNew: false }];
+ ctx.selectedColumnIndexes = [1];
+
+ const collection = buildGeometryMapFeatureCollection(ctx);
+ expect(collection?.features).toHaveLength(1);
+ expect(collection?.features[0].properties._column).toBe("shape_b");
+ });
+
+ it("interprets selectedColumnIndexes in the actual data-column space", () => {
+ const ctx = context();
+ // Hidden leading id column: "shape" is visible grid position 0 but actual
+ // data column 1; the caller maps visible positions before passing them in.
+ ctx.result.columns = ["id", "shape", "name"];
+ ctx.result.column_types = ["int", "geometry", "text"];
+ ctx.result.rows = [[7, "POINT(1 1)", "first"]];
+ ctx.displayRowRefs = [{ id: 1, sourceIndex: 0, isNew: false }];
+ ctx.selectedColumnIndexes = [1];
+
+ const collection = buildGeometryMapFeatureCollection(ctx);
+ expect(collection?.features).toHaveLength(1);
+ expect(collection?.features[0].properties._column).toBe("shape");
+ });
+
+ it("returns null when the column selection contains no geometry column", () => {
+ const ctx = context();
+ ctx.result.columns = ["shape_a", "shape_b", "name"];
+ ctx.result.column_types = ["geometry", "geometry", "text"];
+ ctx.result.rows = [["POINT(1 1)", "POINT(2 2)", "first"]];
+ ctx.displayRowRefs = [{ id: 1, sourceIndex: 0, isNew: false }];
+ ctx.selectedColumnIndexes = [2];
+
+ expect(buildGeometryMapFeatureCollection(ctx)).toBeNull();
+ });
+
+ it("recognizes spatial_columns when column_types is empty or omitted", () => {
+ const result = {
+ column_types: [],
+ spatial_columns: [{ column_index: 0, srid: 4326 }],
+ };
+ expect(hasGeometryMapPreviewColumns(result)).toBe(true);
+ });
});
diff --git a/apps/desktop/src/lib/__tests__/dataGrid/geometryPreview.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/geometryPreview.spec.ts
index 4b61061abc..292bd99eab 100644
--- a/apps/desktop/src/lib/__tests__/dataGrid/geometryPreview.spec.ts
+++ b/apps/desktop/src/lib/__tests__/dataGrid/geometryPreview.spec.ts
@@ -1,5 +1,24 @@
import { describe, expect, it } from "vitest";
-import { parseWktGeometry, renderGeometryOnCanvas, wktToGeoJson } from "@/lib/dataGrid/geometryPreview";
+import { looksLikeWkt, parseWktGeometry, renderGeometryOnCanvas, wktToGeoJson } from "@/lib/dataGrid/geometryPreview";
+
+describe("looksLikeWkt", () => {
+ it("accepts real WKT shapes including SRID prefixes, EMPTY and Z variants", () => {
+ expect(looksLikeWkt("POINT(1 2)")).toBe(true);
+ expect(looksLikeWkt("SRID=4326;POINT(1 2)")).toBe(true);
+ expect(looksLikeWkt("MULTIPOLYGON EMPTY")).toBe(true);
+ expect(looksLikeWkt("LINESTRING Z (1 2 3, 4 5 6)")).toBe(true);
+ expect(looksLikeWkt("point(1 2)")).toBe(true);
+ });
+
+ it("rejects ordinary text that the lenient parser would accept", () => {
+ expect(parseWktGeometry("No records (0)")).not.toBeNull();
+ expect(looksLikeWkt("No records (0)")).toBe(false);
+ expect(looksLikeWkt("FY report (2024)")).toBe(false);
+ expect(looksLikeWkt("result is empty")).toBe(false);
+ expect(looksLikeWkt("empty")).toBe(false);
+ expect(looksLikeWkt("Meeting notes (see attached 3 items)")).toBe(false);
+ });
+});
describe("wktToGeoJson", () => {
it("preserves polygon interior rings", () => {
diff --git a/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts b/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
index 95af0d3b73..4c433506e1 100644
--- a/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
+++ b/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
@@ -1,5 +1,6 @@
import { safeJsonFormat } from "@/lib/common/safeJsonFormat";
import { isBooleanColumnType } from "@/lib/dataGrid/dataGridBooleanColumn";
+import { isHexGeometry, looksLikeWkt, parseWktGeometry } from "@/lib/dataGrid/geometryPreview";
import type { DatabaseType } from "@/types/database";
export type CellDetailTab = "details" | "hexViewer" | "valueEditor";
@@ -87,6 +88,20 @@ export function isGeometryColumnType(columnType: string | undefined): boolean {
return base === "geometry" || base === "geography";
}
+export function canPreviewCellGeometry(value: unknown, columnType?: string): boolean {
+ if (value === null || value === undefined) return false;
+ if (typeof value === "string") {
+ const trimmed = value.trim();
+ if (!trimmed) return false;
+ if (isHexGeometry(trimmed)) {
+ return isGeometryColumnType(columnType);
+ }
+ if (isGeometryColumnType(columnType)) return true;
+ return looksLikeWkt(trimmed) && !!parseWktGeometry(trimmed);
+ }
+ return isGeometryColumnType(columnType);
+}
+
export function canFormatCellDetailJson(value: unknown, columnType?: string): boolean {
if (value === null || value === undefined) return false;
const text = cellDetailRawEditorText(value);
diff --git a/apps/desktop/src/lib/dataGrid/geometryMapPreview.ts b/apps/desktop/src/lib/dataGrid/geometryMapPreview.ts
index e5b71e0f53..83cfcd0b8b 100644
--- a/apps/desktop/src/lib/dataGrid/geometryMapPreview.ts
+++ b/apps/desktop/src/lib/dataGrid/geometryMapPreview.ts
@@ -37,14 +37,22 @@ registerPreviewAction({
},
});
-export function hasGeometryMapPreviewColumns(result: Pick<QueryResult, "column_types">): boolean {
- return geometryColumnIndices(result.column_types).length > 0;
+export function hasGeometryMapPreviewColumns(result: Pick<QueryResult, "column_types"> & Partial<Pick<QueryResult, "spatial_columns">>): boolean {
+ return geometryColumnIndices(result.column_types, result.spatial_columns).length > 0;
}
export function buildGeometryMapFeatureCollection(ctx: PreviewActionContext): GeometryMapFeatureCollection | null {
- const geomIndices = geometryColumnIndices(ctx.result.column_types);
+ let geomIndices = geometryColumnIndices(ctx.result.column_types, ctx.result.spatial_columns);
if (geomIndices.length === 0) return null;
+ if (ctx.selectedColumnIndexes && ctx.selectedColumnIndexes.length > 0) {
+ const selectedGeomIndices = geomIndices.filter((idx) => ctx.selectedColumnIndexes!.includes(idx));
+ // A column selection without geometry columns is not previewable; only an
+ // empty/absent selection falls back to every geometry column.
+ if (selectedGeomIndices.length === 0) return null;
+ geomIndices = selectedGeomIndices;
+ }
+
const features: GeometryMapFeature[] = [];
const seen = new Set<string>();
const sridByColumn = new Map<number, number | null>((ctx.result.spatial_columns ?? []).map((entry) => [entry.column_index, entry.srid]));
@@ -86,15 +94,22 @@ export function buildGeometryMapFeatureCollection(ctx: PreviewActionContext): Ge
return features.length > 0 ? { type: "FeatureCollection", features, detectedSrid } : null;
}
-function geometryColumnIndices(columnTypes: readonly string[] | undefined): number[] {
- if (!columnTypes) return [];
- const indices: number[] = [];
- for (let i = 0; i < columnTypes.length; i++) {
- const base = (columnTypes[i] ?? "")
- .trim()
- .toLowerCase()
- .split(/[(:\s]/)[0];
- if (base === "geometry" || base === "geography") indices.push(i);
+export function geometryColumnIndices(columnTypes: readonly string[] | undefined, spatialColumns?: readonly { column_index: number }[]): number[] {
+ const indices = new Set<number>();
+ if (columnTypes && columnTypes.length > 0) {
+ for (let i = 0; i < columnTypes.length; i++) {
+ const base = (columnTypes[i] ?? "")
+ .trim()
+ .toLowerCase()
+ .split(/[(:\s]/)[0];
+ if (base === "geometry" || base === "geography") indices.add(i);
+ }
+ } else if (spatialColumns) {
+ for (const entry of spatialColumns) {
+ if (typeof entry.column_index === "number" && entry.column_index >= 0) {
+ indices.add(entry.column_index);
+ }
+ }
}
- return indices;
+ return [...indices].sort((a, b) => a - b);
}
diff --git a/apps/desktop/src/lib/dataGrid/geometryPreview.ts b/apps/desktop/src/lib/dataGrid/geometryPreview.ts
index b24795292c..f410302b5d 100644
--- a/apps/desktop/src/lib/dataGrid/geometryPreview.ts
+++ b/apps/desktop/src/lib/dataGrid/geometryPreview.ts
@@ -217,6 +217,20 @@ export function isHexGeometry(value: string): boolean {
return value.startsWith("0x");
}
+const WKT_GEOMETRY_KEYWORDS = ["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION", "TRIANGLE", "TIN", "POLYHEDRALSURFACE", "CIRCULARSTRING", "COMPOUNDCURVE", "CURVEPOLYGON", "MULTICURVE", "MULTISURFACE"].join("|");
+
+const WKT_SHAPE_RE = new RegExp(`^(?:SRID=\\d+\\s*;\\s*)?(?:${WKT_GEOMETRY_KEYWORDS})\\s*(?:Z|M|ZM)?\\s*(?:\\(|EMPTY\\b)`, "i");
+
+/**
+ * Strict WKT gate for geometry-sniffing UI: the value must start with a known
+ * geometry keyword followed by coordinates or EMPTY. `parseWktGeometry` alone
+ * is too loose for detection — it accepts any text containing a parenthesized
+ * number or the word "empty".
+ */
+export function looksLikeWkt(value: string): boolean {
+ return WKT_SHAPE_RE.test(value.trim());
+}
+
export function parseWktGeometry(wkt: string): ParsedWktGeometry | null {
const cleaned = cleanWktInput(wkt);
if (!cleaned || isHexGeometry(cleaned)) return null;
diff --git a/apps/desktop/src/lib/dataGrid/resultPreviewRegistry.ts b/apps/desktop/src/lib/dataGrid/resultPreviewRegistry.ts
index 983869a696..53696fd851 100644
--- a/apps/desktop/src/lib/dataGrid/resultPreviewRegistry.ts
+++ b/apps/desktop/src/lib/dataGrid/resultPreviewRegistry.ts
@@ -17,6 +17,7 @@ export interface PreviewActionContext {
result: QueryResult;
selectedRowIds: readonly number[];
displayRowRefs: readonly { id: number; sourceIndex: number; isNew: boolean }[];
+ selectedColumnIndexes?: readonly number[];
}
/**