feat(editor): configurable default sort when opening tables
This commit is contained in:
25 files changed
+417
-23
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import type { DatabaseType, QueryTab } from "@/types/database";
|
||||
import type { EditorSettings } from "@/stores/settingsStore";
|
||||
import { databaseSortSupportedForDatabase } from "@/lib/dataGrid/dataGridSort";
|
||||
import { quoteTableDataIdentifier } from "@/lib/table/tableSelectSql";
|
||||
|
||||
export function resolveTableDefaultSort(settings: Pick<EditorSettings, "tableOpenSortMode" | "tableDatabaseSortDirection" | "tableLocalSortDirection">, databaseType: DatabaseType | undefined, physicalPrimaryKeys: string[], identifierQuote?: string) {
|
||||
const mode = settings.tableOpenSortMode ?? "none";
|
||||
const direction = (mode === "local" ? settings.tableLocalSortDirection : settings.tableDatabaseSortDirection) ?? "asc";
|
||||
const columns = databaseType === "influxdb" || databaseType === "influxdb3" ? [] : physicalPrimaryKeys;
|
||||
const orderBy = mode === "database" && columns.length && databaseSortSupportedForDatabase(databaseType) ? columns.map((column) => `${databaseType === "neo4j" ? "n." : ""}${quoteTableDataIdentifier(databaseType, column, identifierQuote)} ${direction.toUpperCase()}`).join(", ") : undefined;
|
||||
return { mode, direction, columns, orderBy };
|
||||
}
|
||||
|
||||
export function applyTableDefaultSortResult(tab: QueryTab, sort: ReturnType<typeof resolveTableDefaultSort>, sortLocally: (id: string, column: string, index: number, direction: "asc" | "desc") => void) {
|
||||
if (!tab.result || tab.result.execution_error || tab.isExecuting) return;
|
||||
const column = sort.columns[0];
|
||||
const index = column ? tab.result.columns.indexOf(column) : -1;
|
||||
if (!column || index < 0) return;
|
||||
if (sort.orderBy) {
|
||||
tab.orderByInput = sort.orderBy;
|
||||
// A single-column grid indicator must not overwrite a composite ORDER BY.
|
||||
if (sort.columns.length === 1) {
|
||||
tab.resultSortColumn = column;
|
||||
tab.resultSortColumnIndex = index;
|
||||
tab.resultSortDirection = sort.direction;
|
||||
tab.resultSortMode = "database";
|
||||
}
|
||||
} else if (sort.mode === "local" && !tab.result.large_value_cells?.some((cell) => cell.column_index === index)) {
|
||||
sortLocally(tab.id, column, index, sort.direction);
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,13 @@ export function editablePrimaryKeys(databaseType: DatabaseType | undefined, colu
|
||||
return primaryKeys;
|
||||
}
|
||||
|
||||
/** Physical primary keys only; unlike editable row identifiers, never fall back to unique indexes or synthetic keys. */
|
||||
export function physicalTablePrimaryKeys(columns: readonly Pick<ColumnInfo, "name" | "is_primary_key">[], indexes: readonly Pick<IndexInfo, "columns" | "is_primary">[] = []): string[] {
|
||||
const columnPrimaryKeys = columns.filter((column) => column.is_primary_key).map((column) => column.name);
|
||||
if (columnPrimaryKeys.length > 0) return columnPrimaryKeys;
|
||||
return indexes.find((index) => index.is_primary && index.columns.length > 0)?.columns ?? [];
|
||||
}
|
||||
|
||||
export function editableRowIdentifierColumns(databaseType: DatabaseType | undefined, columns: ColumnInfo[], indexes?: IndexInfo[], tableType?: string): string[] {
|
||||
const primaryKeys = editablePrimaryKeys(databaseType, columns, tableType);
|
||||
const oracleRowIdFallback = getDatabaseCapability(databaseType).syntheticKey === "oracle-rowid" && primaryKeys.length === 1 && primaryKeys[0]?.toUpperCase() === DBX_ROWID_COLUMN;
|
||||
|
||||
Reference in new issue
Block a user