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。

10.0 KB
Raw Blame
import type { DatabaseType } from "@/types/database";

export interface SqlSemanticProjectionAliasVisibility {
  where: boolean;
  groupBy: boolean;
  having: boolean;
  orderBy: boolean;
}

export interface SqlSemanticDialectAdapter {
  id: string;
  identifierQuotes: Array<{ open: string; close: string }>;
  supportsAsForTableAlias: boolean;
  projectionAliasVisibility: SqlSemanticProjectionAliasVisibility;
  normalizeIdentifier(identifier: string, quoted?: boolean): string;
  quoteIdentifier(identifier: string): string;
  qualifierRole(parts: string[], context: "table" | "column" | "routine"): "catalog" | "schema" | "table" | "package" | "unknown";
}

function quoteWith(identifier: string, quote: string): string {
  return `${quote}${identifier.replaceAll(quote, quote + quote)}${quote}`;
}

function defaultNormalize(identifier: string): string {
  return identifier;
}

function lowerUnquoted(identifier: string, quoted?: boolean): string {
  return quoted ? identifier : identifier.toLowerCase();
}

function upperUnquoted(identifier: string, quoted?: boolean): string {
  return quoted ? identifier : identifier.toUpperCase();
}

const defaultProjectionAliasVisibility: SqlSemanticProjectionAliasVisibility = {
  where: false,
  groupBy: false,
  having: false,
  orderBy: true,
};

function roleForGenericQualifier(parts: string[], context: "table" | "column" | "routine"): "catalog" | "schema" | "table" | "package" | "unknown" {
  if (parts.length <= 0) return "unknown";
  if (context === "column") return parts.length >= 2 ? "table" : "table";
  if (context === "routine") return parts.length >= 2 ? "package" : "schema";
  if (parts.length >= 2) return "schema";
  return "schema";
}

function roleForMysqlLikeQualifier(parts: string[], context: "table" | "column" | "routine"): "catalog" | "schema" | "table" | "package" | "unknown" {
  if (context === "column") return "table";
  if (context === "routine") return parts.length >= 2 ? "package" : "schema";
  return parts.length >= 1 ? "schema" : "unknown";
}

export const SQL_SEMANTIC_DIALECTS: Record<string, SqlSemanticDialectAdapter> = {
  generic: {
    id: "generic",
    identifierQuotes: [{ open: '"', close: '"' }],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole: roleForGenericQualifier,
  },
  snowflake: {
    id: "snowflake",
    identifierQuotes: [{ open: '"', close: '"' }],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: upperUnquoted,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole: roleForGenericQualifier,
  },
  postgres: {
    id: "postgres",
    identifierQuotes: [{ open: '"', close: '"' }],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: lowerUnquoted,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole: roleForGenericQualifier,
  },
  mysql: {
    id: "mysql",
    identifierQuotes: [
      { open: "`", close: "`" },
      { open: '"', close: '"' },
    ],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: { where: false, groupBy: true, having: true, orderBy: true },
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, "`"),
    qualifierRole: roleForMysqlLikeQualifier,
  },
  doris: {
    id: "doris",
    identifierQuotes: [
      { open: "`", close: "`" },
      { open: '"', close: '"' },
    ],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: { where: false, groupBy: true, having: true, orderBy: true },
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, "`"),
    qualifierRole: roleForMysqlLikeQualifier,
  },
  clickhouse: {
    id: "clickhouse",
    identifierQuotes: [
      { open: "`", close: "`" },
      { open: '"', close: '"' },
    ],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: { where: false, groupBy: true, having: true, orderBy: true },
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, "`"),
    qualifierRole(parts, context) {
      if (context === "column") return parts.length >= 2 ? "table" : "table";
      if (context === "routine") return parts.length >= 2 ? "package" : "schema";
      return parts.length >= 1 ? "schema" : "unknown";
    },
  },
  sqlserver: {
    id: "sqlserver",
    identifierQuotes: [
      { open: "[", close: "]" },
      { open: '"', close: '"' },
    ],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => `[${identifier.replaceAll("]", "]]")}]`,
    qualifierRole(parts, context) {
      if (context === "column") return parts.length >= 2 ? "table" : "table";
      if (context === "routine") return parts.length >= 2 ? "package" : "schema";
      if (parts.length >= 2) return "schema";
      return "schema";
    },
  },
  sqlite: {
    id: "sqlite",
    identifierQuotes: [
      { open: '"', close: '"' },
      { open: "`", close: "`" },
      { open: "[", close: "]" },
    ],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: { where: false, groupBy: true, having: true, orderBy: true },
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole: roleForGenericQualifier,
  },
  duckdb: {
    id: "duckdb",
    identifierQuotes: [{ open: '"', close: '"' }],
    supportsAsForTableAlias: true,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: defaultNormalize,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole: roleForGenericQualifier,
  },
  oracle: {
    id: "oracle",
    identifierQuotes: [{ open: '"', close: '"' }],
    supportsAsForTableAlias: false,
    projectionAliasVisibility: defaultProjectionAliasVisibility,
    normalizeIdentifier: upperUnquoted,
    quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
    qualifierRole(parts, context) {
      if (context === "column") return parts.length >= 2 ? "table" : "table";
      if (context === "routine") return parts.length >= 2 ? "package" : "schema";
      return parts.length >= 1 ? "schema" : "unknown";
    },
  },
  // SOQL: identifiers are never quoted, field names are case-insensitive, there are
  // no table aliases or projection aliases, and a qualifier is a relationship path
  // segment (Account.Owner) that always resolves to a related sObject ("table").
  soql: {
    id: "soql",
    identifierQuotes: [],
    supportsAsForTableAlias: false,
    projectionAliasVisibility: { where: false, groupBy: false, having: false, orderBy: false },
    normalizeIdentifier: (identifier) => identifier.toLowerCase(),
    quoteIdentifier: (identifier) => identifier,
    qualifierRole(_parts, context) {
      if (context === "column") return "table";
      return "unknown";
    },
  },
};

export function sqlReferenceAnalysisDialectFor(options: { databaseType?: DatabaseType; identifierQuote?: string; fallbackDialect: string }): string {
  if (options.databaseType === "kyuubi") return "spark";
  if (options.databaseType === "kingbase" && options.identifierQuote === "`") return "mysql";
  if (options.databaseType === "doris" || options.databaseType === "starrocks") return "doris";
  return options.fallbackDialect;
}

export function sqlSemanticDialectFor(options: { databaseType?: DatabaseType; dialect?: "mysql" | "postgres" | "sqlserver" | "clickhouse" | "doris" | "soql" }): SqlSemanticDialectAdapter {
  if (options.databaseType === "snowflake") return SQL_SEMANTIC_DIALECTS.snowflake;
  if (options.databaseType === "salesforce") return SQL_SEMANTIC_DIALECTS.soql;
  if (options.databaseType === "clickhouse") return SQL_SEMANTIC_DIALECTS.clickhouse;
  // Doris/StarRocks connections ride the editor's MySQL fallback dialect (codeMirrorSqlDialect maps
  // them to "mysql"), so the explicit-dialect branch below would otherwise mask the doris adapter
  // (LATERAL VIEW modeling, etc.) on the real editor path. Like clickhouse, they win on
  // databaseType regardless of the passed dialect.
  if (options.databaseType === "doris" || options.databaseType === "starrocks") return SQL_SEMANTIC_DIALECTS.doris;
  if (options.dialect && SQL_SEMANTIC_DIALECTS[options.dialect]) return SQL_SEMANTIC_DIALECTS[options.dialect];
  switch (options.databaseType) {
    case "postgres":
    case "redshift":
    case "opengauss":
    case "gaussdb":
    case "highgo":
    case "uxdb":
      return SQL_SEMANTIC_DIALECTS.postgres;
    case "mysql":
      return SQL_SEMANTIC_DIALECTS.mysql;
    case "sqlserver":
      return SQL_SEMANTIC_DIALECTS.sqlserver;
    case "sqlite":
    case "rqlite":
    case "turso":
    case "cloudflare-d1":
      return SQL_SEMANTIC_DIALECTS.sqlite;
    case "duckdb":
      return SQL_SEMANTIC_DIALECTS.duckdb;
    case "oracle":
    case "oceanbase-oracle":
    case "dameng":
    case "kingbase":
    case "vastbase":
    case "goldendb":
    case "yashandb":
      return SQL_SEMANTIC_DIALECTS.oracle;
    default:
      return SQL_SEMANTIC_DIALECTS.generic;
  }
}

/**
 * Resolves the dialect id ("mysql", "postgres", ...) used to keep lexical/statement-boundary
 * scanning in sync with tokenizeSqlSemantic's own dialect-aware tokenization -- most importantly,
 * whether '#' starts a line comment (MySQL) or is an operator (PostgreSQL: #, #>, #>>, #-).
 * Defaults to "mysql" when no dialect info is available (rather than sqlSemanticDialectFor's own
 * "generic" default), matching tokenizeSqlSemantic's default and preserving the behavior callers
 * had before dialect-aware scanning existed.
 */
export function resolveSqlDialectId(options: { databaseType?: DatabaseType; dialect?: "mysql" | "postgres" | "sqlserver" | "clickhouse" | "doris" | "soql" }): string {
  return options.databaseType || options.dialect ? sqlSemanticDialectFor(options).id : "mysql";
}