fix(tdengine): support table row editing

This commit is contained in:
zipg authored and GitHub committed 2026-07-11 13:01:33 +08:00
1 parent 1a833714ba
commit 8d14a056b7
10 files changed
+732 -17

No files matched your search

@@ -1,5 +1,19 @@
import { describe, expect, it } from "vitest";
import { DBX_ROWID_COLUMN, canEditExistingTableRows, canUseKeylessRowPredicate, editablePrimaryKeys, editableRowIdentifierColumns, isClickHouseExistingRowReadonlyColumn, isTableDataEditable, supportsDataGridTransaction, usesSyntheticRowIdKey } from "@/lib/table/tableEditing";
import {
DBX_ROWID_COLUMN,
DBX_TDENGINE_TBNAME_COLUMN,
canDeleteExistingTdengineRows,
canEditExistingTableRows,
canUseKeylessRowPredicate,
editablePrimaryKeys,
editableRowIdentifierColumns,
hasCompleteTdengineRowIdentity,
isClickHouseExistingRowReadonlyColumn,
isTdengineExistingRowReadonlyColumn,
isTableDataEditable,
supportsDataGridTransaction,
usesSyntheticRowIdKey,
} from "@/lib/table/tableEditing";
import type { ColumnInfo, IndexInfo } from "@/types/database";
function column(name: string, isPrimaryKey = false): ColumnInfo {
@@ -59,6 +73,31 @@ describe("tableEditing", () => {
expect(canEditExistingTableRows("clickhouse", undefined, [])).toBe(false);
});
it("uses tbname only when editing TDengine stable rows", () => {
const columns = [column("ts", true), column("seq", true), column("voltage")];
expect(editablePrimaryKeys("tdengine", columns, "STABLE")).toEqual([DBX_TDENGINE_TBNAME_COLUMN, "ts", "seq"]);
expect(editablePrimaryKeys("tdengine", columns, "TABLE")).toEqual(["ts", "seq"]);
expect(canEditExistingTableRows("tdengine", undefined, ["ts", "seq"])).toBe(true);
expect(canEditExistingTableRows("tdengine", undefined, [])).toBe(false);
expect(isTdengineExistingRowReadonlyColumn("tdengine", "seq", columns)).toBe(true);
});
it("requires every TDengine row identifier in editable results", () => {
const stableKeys = [DBX_TDENGINE_TBNAME_COLUMN, "ts", "seq"];
expect(hasCompleteTdengineRowIdentity("tdengine", stableKeys, ["tbname", "ts", "seq", "voltage"])).toBe(true);
expect(hasCompleteTdengineRowIdentity("tdengine", stableKeys, ["tbname", "ts", "voltage"])).toBe(false);
expect(hasCompleteTdengineRowIdentity("tdengine", ["ts", "seq"], ["ts", "seq", "voltage"])).toBe(true);
expect(hasCompleteTdengineRowIdentity("postgres", ["id"], [])).toBe(true);
});
it("disables existing-row deletion for TDengine composite keys", () => {
expect(canDeleteExistingTdengineRows("tdengine", ["ts"])).toBe(true);
expect(canDeleteExistingTdengineRows("tdengine", [DBX_TDENGINE_TBNAME_COLUMN, "ts"])).toBe(true);
expect(canDeleteExistingTdengineRows("tdengine", ["ts", "seq"])).toBe(false);
expect(canDeleteExistingTdengineRows("tdengine", [DBX_TDENGINE_TBNAME_COLUMN, "ts", "seq"])).toBe(false);
expect(canDeleteExistingTdengineRows("postgres", ["id", "tenant_id"])).toBe(true);
});
it("treats ClickHouse row identifier cells as readonly on existing rows", () => {
expect(isClickHouseExistingRowReadonlyColumn("clickhouse", "ID", ["id"])).toBe(true);
expect(isClickHouseExistingRowReadonlyColumn("clickhouse", "name", ["id"])).toBe(false);
+19 -2
View File
@@ -9,10 +9,15 @@ function isViewTableType(tableType?: string): boolean {
return tableType?.toUpperCase().includes("VIEW") === true;
}
function isTdengineStableTableType(tableType?: string): boolean {
const normalized = tableType?.trim().toUpperCase();
return normalized === "STABLE" || normalized === "SUPER TABLE" || normalized === "SUPERTABLE";
}
export function editablePrimaryKeys(databaseType: DatabaseType | undefined, columns: ColumnInfo[], tableType?: string): string[] {
const primaryKeys = columns.filter((column) => column.is_primary_key).map((column) => column.name);
if (isViewTableType(tableType)) return primaryKeys;
if (databaseType === "tdengine" && primaryKeys.length > 0) return [DBX_TDENGINE_TBNAME_COLUMN, ...primaryKeys];
if (databaseType === "tdengine" && primaryKeys.length > 0 && isTdengineStableTableType(tableType)) return [DBX_TDENGINE_TBNAME_COLUMN, ...primaryKeys];
const syntheticKey = getDatabaseCapability(databaseType).syntheticKey;
if (syntheticKey === "oracle-rowid" && primaryKeys.length === 0) return [DBX_ROWID_COLUMN];
if (syntheticKey === "neo4j-element-id" && primaryKeys.length === 0) return [DBX_NEO4J_ELEMENT_ID_COLUMN];
@@ -55,11 +60,23 @@ export function canEditExistingTableRows(databaseType: DatabaseType | undefined,
if (tableData.readonly) return false;
if (tableData.existingRowsReadonly) return false;
if (tableData.requiresTransactionalTableForExistingRows && hiveTableTransactional !== true) return false;
if (databaseType === "tdengine" && !primaryKeys?.some((key) => key.toLowerCase() === DBX_TDENGINE_TBNAME_COLUMN)) return false;
if (tableData.updateRequiresPrimaryKey && primaryKeys && primaryKeys.length === 0) return false;
return true;
}
export function hasCompleteTdengineRowIdentity(databaseType: DatabaseType | undefined, primaryKeys: readonly string[], resultColumns: readonly (string | undefined)[]): boolean {
if (databaseType !== "tdengine") return true;
if (primaryKeys.length === 0) return false;
const availableColumns = new Set(resultColumns.filter((column): column is string => !!column).map((column) => column.toLowerCase()));
return primaryKeys.every((primaryKey) => availableColumns.has(primaryKey.toLowerCase()));
}
export function canDeleteExistingTdengineRows(databaseType: DatabaseType | undefined, primaryKeys: readonly string[]): boolean {
if (databaseType !== "tdengine") return true;
const rowPrimaryKeys = primaryKeys.filter((primaryKey) => primaryKey.toLowerCase() !== DBX_TDENGINE_TBNAME_COLUMN);
return rowPrimaryKeys.length <= 1;
}
export function hiveTablePropertiesIndicateTransactional(result: { rows: readonly (readonly unknown[])[] }): boolean {
return result.rows.some((row) => {
const name = String(row[0] ?? "")