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。

5.9 KB
Raw Blame
import { COCKROACHDB_DRIVER_PROFILE_EXTENSION } from "@/lib/database/cockroachdbProfile";
import { DOLT_DRIVER_PROFILE_EXTENSION } from "@/lib/database/doltProfile";
import type { SqlCompletionObject, SqlCompletionTable, SqlStatementKind } from "@/lib/sql/sqlCompletion";
import type { ConnectionConfig, DatabaseType, QueryTab, TableNameFilter, TreeNodeType } from "@/types/database";

export type DriverProfileObjectTreeProfile = {
  cacheKey: string;
  groupOverrides: Array<{
    nodeType: TreeNodeType;
    label?: string;
    tableNameFilter: TableNameFilter;
  }>;
};

export type DriverProfileSqlCompletionContext = {
  statementKind: SqlStatementKind;
  suggestTables: boolean;
  suggestRoutines: boolean;
  exclusiveRoutineSuggestions: boolean;
  prefix: string;
  openingParenAfterCursor: boolean;
};

export type DriverProfileCompletionTableMetadata = Pick<SqlCompletionTable, "detail" | "boost">;

/** Cursor context passed to profile-specific contextual keyword hooks. */
export type DriverProfileKeywordContext = {
  beforeCursor: string;
  beforeToken: string;
  databaseType?: DatabaseType;
};

export type DriverProfileWorkspaceTarget = {
  database: string;
  branch?: string;
};

export type DriverProfileWorkspaceScope = "database" | "connection";

export type DriverProfileDatabaseWorkspace = {
  mode: QueryTab["mode"];
  menuLabelKey: string;
  tabTitleKey: string;
  /** Determines whether tabs are reused per connection or per database. */
  tabScope: DriverProfileWorkspaceScope;
  /** Tree levels whose context menu exposes the workspace entry. */
  entryScopes: readonly DriverProfileWorkspaceScope[];
  /** Maps a tree database name to the context opened by the workspace. */
  resolveTarget?: (database: string, source: DriverProfileWorkspaceScope) => DriverProfileWorkspaceTarget;
};

export type DriverProfileExtensionDefinition = {
  id: string;
  objectTreeProfile?: (config: ConnectionConfig) => DriverProfileObjectTreeProfile | undefined;
  completionTableMetadata?: (tableName: string) => DriverProfileCompletionTableMetadata | undefined;
  completionObjects?: (context: DriverProfileSqlCompletionContext) => SqlCompletionObject[];
  completionTables?: (context: DriverProfileSqlCompletionContext) => SqlCompletionTable[];
  sqlBuiltinTerms?: () => string;
  /** 叠加到方言关键字表上,用于 profile 专有语法(例如 CockroachDB 的 FAMILY)。 */
  sqlKeywords?: () => readonly string[];
  /** 叠加到方言函数表上,用于 profile 专有函数。 */
  functionSignatures?: () => Map<string, string[]>;
  /** 上下文相关的优先关键字(例如 CRDB 的 AS OF SYSTEM TIME 续写)。 */
  completionContextKeywords?: (context: DriverProfileKeywordContext) => readonly string[];
  /** profile 专有的数据类型关键字,追加到 DATA_TYPE_KEYWORDS。 */
  completionDataTypes?: () => readonly string[];
  routineSignatures?: () => Map<string, string[]>;
  databaseWorkspace?: DriverProfileDatabaseWorkspace;
};

export const DRIVER_PROFILE_EXTENSIONS = [DOLT_DRIVER_PROFILE_EXTENSION, COCKROACHDB_DRIVER_PROFILE_EXTENSION] as const satisfies readonly DriverProfileExtensionDefinition[];

export function driverProfileExtension(driverProfile?: string): DriverProfileExtensionDefinition | undefined {
  const normalized = driverProfile?.trim().toLowerCase();
  if (!normalized) return undefined;
  return DRIVER_PROFILE_EXTENSIONS.find((extension) => extension.id.toLowerCase() === normalized);
}

export function driverProfileObjectTreeProfileForConnection(config?: ConnectionConfig): DriverProfileObjectTreeProfile | undefined {
  if (!config) return undefined;
  return driverProfileExtension(config.driver_profile)?.objectTreeProfile?.(config);
}

export function driverProfileCompletionTableMetadata(driverProfile: string | undefined, tableName: string): DriverProfileCompletionTableMetadata | undefined {
  return driverProfileExtension(driverProfile)?.completionTableMetadata?.(tableName);
}

export function driverProfileCompletionObjects(driverProfile: string | undefined, context: DriverProfileSqlCompletionContext): SqlCompletionObject[] {
  return driverProfileExtension(driverProfile)?.completionObjects?.(context) ?? [];
}

export function driverProfileCompletionTables(driverProfile: string | undefined, context: DriverProfileSqlCompletionContext): SqlCompletionTable[] {
  return driverProfileExtension(driverProfile)?.completionTables?.(context) ?? [];
}

export function driverProfileHasCompletionCandidates(driverProfile: string | undefined, context: DriverProfileSqlCompletionContext): boolean {
  const extension = driverProfileExtension(driverProfile);
  return Boolean(extension?.completionObjects?.(context).length || extension?.completionTables?.(context).length);
}

export function driverProfileSqlBuiltinTerms(driverProfile?: string): string {
  return driverProfileExtension(driverProfile)?.sqlBuiltinTerms?.() ?? "";
}

export function driverProfileSqlKeywords(driverProfile?: string): readonly string[] {
  return driverProfileExtension(driverProfile)?.sqlKeywords?.() ?? [];
}

export function driverProfileFunctionSignatures(driverProfile?: string): Map<string, string[]> {
  return driverProfileExtension(driverProfile)?.functionSignatures?.() ?? new Map();
}

export function driverProfileContextKeywords(driverProfile: string | undefined, context: DriverProfileKeywordContext): readonly string[] {
  return driverProfileExtension(driverProfile)?.completionContextKeywords?.(context) ?? [];
}

export function driverProfileDataTypes(driverProfile?: string): readonly string[] {
  return driverProfileExtension(driverProfile)?.completionDataTypes?.() ?? [];
}

export function driverProfileRoutineSignatures(driverProfile?: string): Map<string, string[]> {
  return driverProfileExtension(driverProfile)?.routineSignatures?.() ?? new Map();
}

export function driverProfileDatabaseWorkspace(driverProfile?: string): DriverProfileDatabaseWorkspace | undefined {
  return driverProfileExtension(driverProfile)?.databaseWorkspace;
}