Files
dbx/apps/desktop/src/lib/database/databaseFeatureSupport.ts
T

203 lines
8.8 KiB
TypeScript

import type { ConnectionConfig, DatabaseType, TreeNodeType } from "@/types/database";
import { supportsDatabaseFeature } from "@/lib/database/databaseDriverManifest";
import { canEditTableStructure } from "@/lib/table/tableStructureCapabilities";
import { CLEARABLE_QUERY_SCHEMA_TYPES, DATABASE_OBJECT_TREE_TYPES, DATABASE_SCHEMA_QUALIFIED_TYPES, FETCH_FIRST_TYPES, PG_LIKE_STRUCTURE_TYPES, SCHEMA_AWARE_TYPES, SINGLE_DATABASE_TYPES, TREE_SCHEMA_TYPES } from "@/lib/database/databaseCapabilitySets";
import { supportsRegisteredConnectionScopedQueryExecution, supportsRegisteredQueryTargetDatabaseListing, usesRegisteredConnectionOnlyQueryTarget } from "@/lib/database/sqlExecutionTargetRegistry";
export function isSchemaAware(dbType?: DatabaseType): boolean {
return !!dbType && SCHEMA_AWARE_TYPES.has(dbType);
}
export function supportsDatabaseSchemaQualifier(dbType?: DatabaseType): boolean {
return !!dbType && DATABASE_SCHEMA_QUALIFIED_TYPES.has(dbType);
}
export function supportsDatabaseNameCompletion(dbType?: DatabaseType): boolean {
return !!dbType && ((!isSchemaAware(dbType) && !isSingleDatabase(dbType)) || dbType === "sqlserver");
}
/**
* Doris-family engines that support multi-catalog federation (`SHOW CATALOGS`):
* Doris (incl. SelectDB) and StarRocks. Manticore Search shares the MySQL code
* path but has no catalog concept, so it is excluded.
*/
export function isDorisFamilyCatalogCapable(dbType?: DatabaseType, driverProfile?: string | null): boolean {
if (dbType === "doris" || dbType === "starrocks") return true;
return driverProfile === "doris" || driverProfile === "selectdb" || driverProfile === "starrocks";
}
export function connectionIsDorisFamilyCatalogCapable(connection: Pick<ConnectionConfig, "db_type" | "driver_profile"> | undefined): boolean {
if (!connection) return false;
return isDorisFamilyCatalogCapable(connection.db_type, connection.driver_profile);
}
/**
* Whether a Doris/StarRocks catalog is the engine's built-in (non-federated)
* catalog. Doris names it `internal` (Type=`internal`); StarRocks names it
* `default_catalog` (Type=`Internal`). The `catalogType` column is the
* cross-engine signal, so it is matched case-insensitively, falling back to the
* canonical Doris name `internal` when the type is absent (very old / proxied
* deployments). Mirrors `CatalogInfo::is_internal` on the backend.
*/
export function isInternalDorisCatalog(catalogType?: string | null, catalogName?: string | null): boolean {
const type = (catalogType ?? "").trim().toLowerCase();
if (type) return type === "internal";
return (catalogName ?? "").trim() === "internal";
}
export function usesTreeSchemaMode(dbType?: DatabaseType): boolean {
return !!dbType && TREE_SCHEMA_TYPES.has(dbType);
}
export function canConfigureVisibleSchemasForTreeNode(dbType: DatabaseType | undefined, nodeType: TreeNodeType, database?: string | null): boolean {
if (!isSchemaAware(dbType)) return false;
if (nodeType === "database") return database != null;
return nodeType === "connection" && !usesTreeSchemaMode(dbType);
}
export function usesDatabaseObjectTreeMode(dbType?: DatabaseType): boolean {
return !!dbType && DATABASE_OBJECT_TREE_TYPES.has(dbType);
}
export function databaseObjectTreeQuerySchema(dbType: DatabaseType | undefined, database: string, schema?: string): string {
if (usesDatabaseObjectTreeMode(dbType)) return "";
return schema || database;
}
export function databaseObjectTreeNodeSchema(dbType: DatabaseType | undefined, database: string, schema?: string): string | undefined {
if (usesDatabaseObjectTreeMode(dbType)) return undefined;
if (schema) return schema;
return isSchemaAware(dbType) ? database : undefined;
}
export function isSingleDatabase(dbType?: DatabaseType): boolean {
return !!dbType && SINGLE_DATABASE_TYPES.has(dbType);
}
export function supportsClearableQuerySchema(dbType?: DatabaseType): boolean {
return !!dbType && CLEARABLE_QUERY_SCHEMA_TYPES.has(dbType);
}
export function supportsConnectionQueryActions(dbType?: DatabaseType): boolean {
return dbType !== "nacos" && dbType !== "consul" && dbType !== "hbase";
}
/**
* Whether the current product surface exposes a query execution path for the
* database type. This is intentionally distinct from sqlFileExecution:
* document/vector/key-value editors can execute commands from a query tab
* even when importing a SQL file is not supported.
*/
export function supportsQueryExecution(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "queryExecution");
}
export function supportsConnectionScopedQueryExecution(dbType?: DatabaseType): boolean {
return supportsRegisteredConnectionScopedQueryExecution(dbType);
}
/**
* Query surfaces whose target is the connection itself and which do not expose
* a database namespace to select. Keep this separate from
* supportsConnectionScopedQueryExecution: document/vector stores may execute
* without a selected database while still exposing database-like namespaces
* (for example indexes or collections) for browsing and target selection.
*/
export function usesConnectionOnlyQueryTarget(dbType?: DatabaseType): boolean {
return usesRegisteredConnectionOnlyQueryTarget(dbType);
}
/**
* Database-like namespaces exposed by connection-scoped document/vector
* query surfaces. This is the extension point for drivers whose query target
* is still selected from a database/index list rather than from SQL metadata.
*/
export function supportsQueryTargetDatabaseListing(dbType?: DatabaseType): boolean {
return supportsRegisteredQueryTargetDatabaseListing(dbType);
}
export function usesFetchFirst(dbType?: DatabaseType): boolean {
return !!dbType && FETCH_FIRST_TYPES.has(dbType);
}
export function supportsSqlFileExecution(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "sqlFileExecution");
}
const NON_SQL_IN_LIST_PASTE_TYPES = new Set<DatabaseType>(["neo4j"]);
export function supportsSqlInListPaste(dbType?: DatabaseType): boolean {
if (!dbType) return true;
return supportsSqlFileExecution(dbType) && !NON_SQL_IN_LIST_PASTE_TYPES.has(dbType);
}
export function supportsQueryEditorBlockComments(dbType?: DatabaseType): boolean {
if (!dbType) return true;
return supportsSqlFileExecution(dbType);
}
export function supportsSchemaDiagram(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "diagram");
}
export function supportsDatabaseSearch(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "schemaSearch");
}
export function supportsTableImport(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "tableImport");
}
export function supportsTableStructureEditing(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "tableStructureEdit") && canEditTableStructure(dbType);
}
export function supportsDatabaseCreation(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "databaseCreate");
}
export function supportsFieldLineage(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "fieldLineage");
}
export function supportsTransfer(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "dataTransfer");
}
export function supportsDriverManagement(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "driverManagement");
}
export function supportsObjectBrowser(dbType?: DatabaseType): boolean {
return supportsDatabaseFeature(dbType, "objectBrowser");
}
export function supportsConnectionDatabaseBrowser(dbType?: DatabaseType): boolean {
return supportsObjectBrowser(dbType);
}
export function supportsObjectBrowserTreeNode(dbType: DatabaseType | undefined, nodeType: TreeNodeType): boolean {
if (!supportsObjectBrowser(dbType)) return false;
if (nodeType === "database" && usesDatabaseObjectTreeMode(dbType)) return true;
if (nodeType === "database" && isSchemaAware(dbType) && dbType !== "sqlserver") return false;
return nodeType === "database" || nodeType === "schema" || nodeType === "object-browser";
}
export function supportsTableTruncate(dbType?: DatabaseType): boolean {
return !!dbType && dbType !== "impala" && dbType !== "sqlite" && dbType !== "rqlite" && dbType !== "turso" && dbType !== "cloudflare-d1" && dbType !== "duckdb" && dbType !== "influxdb" && dbType !== "victoriametrics" && dbType !== "manticoresearch";
}
export function usesPostgresLikeStructureCopy(dbType?: DatabaseType): boolean {
return !!dbType && PG_LIKE_STRUCTURE_TYPES.has(dbType);
}
const TRANSACTION_SUPPORTED_TYPES: readonly string[] = ["postgres", "mysql"];
/**
* Returns true if the given database type supports explicit transaction control
* (i.e. toggling between auto-commit and manual transaction mode via BEGIN/COMMIT).
*/
export function supportsTransaction(dbType?: string): boolean {
return !!dbType && TRANSACTION_SUPPORTED_TYPES.includes(dbType);
}