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。

12.3 KB
Raw Blame
const SIMPLE_LOWER_IDENTIFIER = /^[a-z_][a-z0-9_$]*$/;
const SIMPLE_MYSQL_IDENTIFIER = /^[A-Za-z_$\u0080-\uFFFF][A-Za-z0-9_$\u0080-\uFFFF]*$/u;
const SIMPLE_CLICKHOUSE_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
const SIMPLE_DAMENG_IDENTIFIER = /^[A-Z_][A-Z0-9_]*$/;
const SIMPLE_ORACLE_IDENTIFIER = /^[A-Z][A-Z0-9_$#]*$/;

// PostgreSQL pg_get_keywords() categories R and T. GaussDB's PostgreSQL-
// compatible modes use the same identifier constraints for bare names.
const POSTGRES_RESERVED_IDENTIFIER_KEYWORDS = new Set([
  "all",
  "analyse",
  "analyze",
  "and",
  "any",
  "array",
  "as",
  "asc",
  "asymmetric",
  "authorization",
  "binary",
  "both",
  "case",
  "cast",
  "check",
  "collate",
  "collation",
  "column",
  "concurrently",
  "constraint",
  "create",
  "cross",
  "current_catalog",
  "current_date",
  "current_role",
  "current_schema",
  "current_time",
  "current_timestamp",
  "current_user",
  "default",
  "deferrable",
  "desc",
  "distinct",
  "do",
  "else",
  "end",
  "except",
  "false",
  "fetch",
  "for",
  "foreign",
  "freeze",
  "from",
  "full",
  "grant",
  "group",
  "having",
  "ilike",
  "in",
  "initially",
  "inner",
  "intersect",
  "into",
  "is",
  "isnull",
  "join",
  "lateral",
  "leading",
  "left",
  "like",
  "limit",
  "localtime",
  "localtimestamp",
  "natural",
  "not",
  "notnull",
  "null",
  "offset",
  "on",
  "only",
  "or",
  "order",
  "outer",
  "overlaps",
  "placing",
  "primary",
  "references",
  "returning",
  "right",
  "select",
  "session_user",
  "similar",
  "some",
  "symmetric",
  "system_user",
  "table",
  "tablesample",
  "then",
  "to",
  "trailing",
  "true",
  "union",
  "unique",
  "user",
  "using",
  "variadic",
  "verbose",
  "when",
  "where",
  "while",
  "window",
  "with",
]);

// GaussDB MySQL-compatible drivers report a backtick. These additional words
// cover MySQL-only names that cannot safely be emitted as bare identifiers —
// the full MySQL reserved-word list (MySQL 8.4 manual, keywords marked (R))
// minus the PostgreSQL set above, so the union matches MySQL exactly.
const MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS = new Set([
  "_filename",
  "accessible",
  "add",
  "alter",
  "asensitive",
  "auto_increment",
  "before",
  "between",
  "bigint",
  "blob",
  "by",
  "call",
  "cascade",
  "change",
  "char",
  "character",
  "condition",
  "continue",
  "convert",
  "cube",
  "cume_dist",
  "cursor",
  "database",
  "databases",
  "day_hour",
  "day_microsecond",
  "day_minute",
  "day_second",
  "dec",
  "decimal",
  "declare",
  "delayed",
  "delete",
  "dense_rank",
  "describe",
  "deterministic",
  "distinctrow",
  "div",
  "double",
  "drop",
  "dual",
  "each",
  "elseif",
  "empty",
  "enclosed",
  "escaped",
  "explain",
  "exists",
  "exit",
  "first_value",
  "float",
  "float4",
  "float8",
  "force",
  "fulltext",
  "function",
  "generated",
  "get",
  "grouping",
  "groups",
  "high_priority",
  "hour_microsecond",
  "hour_minute",
  "hour_second",
  "if",
  "ignore",
  "index",
  "infile",
  "inout",
  "insensitive",
  "insert",
  "int",
  "int1",
  "int2",
  "int3",
  "int4",
  "int8",
  "integer",
  "interval",
  "io_after_gtids",
  "io_before_gtids",
  "iterate",
  "json_table",
  "key",
  "keys",
  "kill",
  "lag",
  "last_value",
  "lead",
  "leave",
  "linear",
  "lines",
  "load",
  "lock",
  "long",
  "longblob",
  "longtext",
  "loop",
  "low_priority",
  "master_ssl_verify_server_cert",
  "match",
  "maxvalue",
  "mediumblob",
  "mediumint",
  "mediumtext",
  "middleint",
  "minute_microsecond",
  "minute_second",
  "mod",
  "modifies",
  "no_write_to_binlog",
  "nth_value",
  "ntile",
  "numeric",
  "of",
  "optimize",
  "optimizer_costs",
  "option",
  "optionally",
  "out",
  "outfile",
  "over",
  "partition",
  "percent_rank",
  "precision",
  "procedure",
  "purge",
  "qualify",
  "range",
  "rank",
  "read",
  "read_write",
  "reads",
  "real",
  "recursive",
  "regexp",
  "release",
  "rename",
  "repeat",
  "replace",
  "require",
  "resignal",
  "restrict",
  "return",
  "revoke",
  "rlike",
  "row",
  "row_number",
  "rows",
  "schema",
  "schemas",
  "second_microsecond",
  "sensitive",
  "separator",
  "set",
  "show",
  "signal",
  "smallint",
  "spatial",
  "specific",
  "sql",
  "sql_big_result",
  "sql_calc_found_rows",
  "sql_small_result",
  "sqlexception",
  "sqlstate",
  "sqlwarning",
  "ssl",
  "starting",
  "stored",
  "straight_join",
  "system",
  "terminated",
  "tinyblob",
  "tinyint",
  "tinytext",
  "trigger",
  "undo",
  "unlock",
  "unsigned",
  "update",
  "usage",
  "use",
  "utc_date",
  "utc_time",
  "utc_timestamp",
  "values",
  "varbinary",
  "varchar",
  "varcharacter",
  "varying",
  "virtual",
  "write",
  "xor",
  "year_month",
  "zerofill",
]);

// ClickHouse identifiers: bare identifiers cannot contain `$` or non-ASCII characters
// and cannot match reserved keywords. Keywords outside PostgreSQL's set are listed here.
const CLICKHOUSE_ONLY_RESERVED_IDENTIFIER_KEYWORDS = new Set([
  "alias",
  "alter",
  "anti",
  "asof",
  "attach",
  "between",
  "by",
  "cluster",
  "codec",
  "database",
  "databases",
  "delete",
  "describe",
  "detach",
  "drop",
  "engine",
  "ephemeral",
  "exchange",
  "exists",
  "explain",
  "final",
  "format",
  "global",
  "if",
  "insert",
  "interval",
  "kill",
  "materialized",
  "optimize",
  "partition",
  "populate",
  "prewhere",
  "qualify",
  "rename",
  "replace",
  "sample",
  "semi",
  "set",
  "settings",
  "show",
  "system",
  "totals",
  "ttl",
  "values",
  "view",
  "watch",
]);

// Oracle 11g SQL reserved words. Ordinary unquoted Oracle identifiers are
// uppercase-folded, so only uppercase names outside this set are safe to unwrap.
const ORACLE_RESERVED_IDENTIFIER_KEYWORDS = new Set([
  "ACCESS",
  "ADD",
  "ALL",
  "ALTER",
  "AND",
  "ANY",
  "AS",
  "ASC",
  "AUDIT",
  "BETWEEN",
  "BY",
  "CHAR",
  "CHECK",
  "CLUSTER",
  "COLUMN",
  "COMMENT",
  "COMPRESS",
  "CONNECT",
  "CREATE",
  "CURRENT",
  "DATE",
  "DECIMAL",
  "DEFAULT",
  "DELETE",
  "DESC",
  "DISTINCT",
  "DROP",
  "ELSE",
  "EXCLUSIVE",
  "EXISTS",
  "FILE",
  "FLOAT",
  "FOR",
  "FROM",
  "GRANT",
  "GROUP",
  "HAVING",
  "IDENTIFIED",
  "IMMEDIATE",
  "IN",
  "INCREMENT",
  "INDEX",
  "INITIAL",
  "INSERT",
  "INTEGER",
  "INTERSECT",
  "INTO",
  "IS",
  "LEVEL",
  "LIKE",
  "LOCK",
  "LONG",
  "MAXEXTENTS",
  "MINUS",
  "MLSLABEL",
  "MODE",
  "MODIFY",
  "NOAUDIT",
  "NOCOMPRESS",
  "NOT",
  "NOWAIT",
  "NULL",
  "NUMBER",
  "OF",
  "OFFLINE",
  "ON",
  "ONLINE",
  "OPTION",
  "OR",
  "ORDER",
  "PCTFREE",
  "PRIOR",
  "PRIVILEGES",
  "PUBLIC",
  "RAW",
  "RENAME",
  "RESOURCE",
  "REVOKE",
  "ROW",
  "ROWID",
  "ROWNUM",
  "ROWS",
  "SELECT",
  "SESSION",
  "SET",
  "SHARE",
  "SIZE",
  "SMALLINT",
  "START",
  "SUCCESSFUL",
  "SYNONYM",
  "SYSDATE",
  "TABLE",
  "THEN",
  "TO",
  "TRIGGER",
  "UID",
  "UNION",
  "UNIQUE",
  "UPDATE",
  "USER",
  "VALIDATE",
  "VALUES",
  "VARCHAR",
  "VARCHAR2",
  "VIEW",
  "WHENEVER",
  "WHERE",
  "WITH",
]);

// V$RESERVED_WORDS rows marked RESERVED = Y across the supported DM8 test
// instances. DM's native identifier rules use uppercase ordinary names.
const DAMENG_RESERVED_IDENTIFIER_KEYWORDS = new Set([
  "ABSOLUTE",
  "ABSTRACT",
  "ADD",
  "ADMIN",
  "ALL",
  "ALTER",
  "AND",
  "ANY",
  "ARRAY",
  "ARRAYLEN",
  "AS",
  "ASC",
  "ASSIGN",
  "AUDIT",
  "AUTHORIZATION",
  "AUTO_INCREMENT",
  "BEGIN",
  "BETWEEN",
  "BIGDATEDIFF",
  "BINARY",
  "BOOL",
  "BOTH",
  "BREAK",
  "BSTRING",
  "BY",
  "BYTE",
  "CALL",
  "CASE",
  "CAST",
  "CATCH",
  "CHAR",
  "CHECK",
  "CLASS",
  "CLUSTER",
  "CLUSTERBTR",
  "COLLATION",
  "COLLECT",
  "COLUMN",
  "COMMENT",
  "COMMIT",
  "COMMITWORK",
  "CONNECT",
  "CONNECT_BY_ROOT",
  "CONST",
  "CONSTRAINT",
  "CONTAINS",
  "CONTEXT",
  "CONTINUE",
  "CONVERT",
  "CORRESPONDING",
  "CREATE",
  "CROSS",
  "CRYPTO",
  "CUBE",
  "CURRENT",
  "CURSOR",
  "DATEADD",
  "DATEDIFF",
  "DATEPART",
  "DECIMAL",
  "DECLARE",
  "DECODE",
  "DEFAULT",
  "DELETE",
  "DESC",
  "DISABLE",
  "DISKSPACE",
  "DISTINCT",
  "DISTRIBUTED",
  "DO",
  "DOMAIN",
  "DOUBLE",
  "DROP",
  "ELSE",
  "ELSEIF",
  "ELSIF",
  "ENABLE",
  "END",
  "EQU",
  "EXCHANGE",
  "EXEC",
  "EXECUTE",
  "EXISTS",
  "EXIT",
  "EXPLAIN",
  "EXTERN",
  "EXTRACT",
  "FETCH",
  "FINAL",
  "FINALLY",
  "FIRST",
  "FLASHBACK",
  "FLOAT",
  "FOR",
  "FOREIGN",
  "FROM",
  "FULL",
  "FULLY",
  "FUNCTION",
  "GET",
  "GOTO",
  "GRANT",
  "GROUP",
  "GROUPING",
  "HAVING",
  "IDENTITY",
  "IF",
  "IFNULL",
  "IMMEDIATE",
  "IN",
  "INDEX",
  "INLINE",
  "INNER",
  "INSERT",
  "INT",
  "INTERSECT",
  "INTERVAL",
  "INTO",
  "IS",
  "JOIN",
  "KEEP",
  "LEADING",
  "LEFT",
  "LEXER",
  "LIKE",
  "LIST",
  "LNNVL",
  "LOGIN",
  "LOOP",
  "MEMBER",
  "MINUS",
  "MULTISET",
  "NATURAL",
  "NEW",
  "NEXT",
  "NOCOPY",
  "NOCYCLE",
  "NOT",
  "NULL",
  "OBJECT",
  "OF",
  "ON",
  "OR",
  "ORDER",
  "OUT",
  "OVER",
  "OVERLAY",
  "OVERRIDE",
  "PARTITION",
  "PENDANT",
  "PERCENT",
  "PRIMARY",
  "PRINT",
  "PRIOR",
  "PRIVATE",
  "PRIVILEGES",
  "PROCEDURE",
  "PROTECTED",
  "PUBLIC",
  "RAISE",
  "RECORD",
  "REF",
  "REFERENCE",
  "REFERENCES",
  "REFERENCING",
  "RELATIVE",
  "REPEAT",
  "REPLICATE",
  "RETURN",
  "RETURNING",
  "REVERSE",
  "REVOKE",
  "RIGHT",
  "ROLLBACK",
  "ROLLUP",
  "ROW",
  "ROWNUM",
  "ROWS",
  "SAVEPOINT",
  "SBYTE",
  "SCHEMA",
  "SEALED",
  "SECTION",
  "SELECT",
  "SET",
  "SETS",
  "SHORT",
  "SIZEOF",
  "SOME",
  "STATIC",
  "STRUCT",
  "SUBPARTITION",
  "SWITCH",
  "SYNONYM",
  "TABLE",
  "THROW",
  "TIMESTAMPADD",
  "TIMESTAMPDIFF",
  "TO",
  "TOP",
  "TRAILING",
  "TRIGGER",
  "TRIM",
  "TRUNCATE",
  "TRY",
  "TYPEDEF",
  "TYPEOF",
  "UINT",
  "ULONG",
  "UNION",
  "UNIQUE",
  "UNTIL",
  "UPDATE",
  "USER",
  "USHORT",
  "USING",
  "VALUES",
  "VARRAY",
  "VERIFY",
  "VIEW",
  "VIRTUAL",
  "VOID",
  "WHEN",
  "WHENEVER",
  "WHERE",
  "WHILE",
  "WITH",
  "WITHIN",
  "XMLAGG",
  "XMLPARSE",
  "XMLTABLE",
]);

export function isExplicitlyQuotedSqlIdentifier(value: string): boolean {
  if (value.length < 2) return false;
  return (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("`") && value.endsWith("`")) || (value.startsWith("[") && value.endsWith("]"));
}

export function requiresPostgresIdentifierQuote(identifier: string, additionalKeywords?: ReadonlySet<string>): boolean {
  if (!SIMPLE_LOWER_IDENTIFIER.test(identifier)) return true;
  return POSTGRES_RESERVED_IDENTIFIER_KEYWORDS.has(identifier) || additionalKeywords?.has(identifier) === true;
}

export function requiresMysqlIdentifierQuote(identifier: string, additionalKeywords?: ReadonlySet<string>): boolean {
  if (!SIMPLE_MYSQL_IDENTIFIER.test(identifier)) return true;
  const normalized = identifier.toLowerCase();
  return POSTGRES_RESERVED_IDENTIFIER_KEYWORDS.has(normalized) || additionalKeywords?.has(normalized) === true || MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(normalized);
}

export function requiresClickhouseIdentifierQuote(identifier: string, additionalKeywords?: ReadonlySet<string>): boolean {
  if (!SIMPLE_CLICKHOUSE_IDENTIFIER.test(identifier)) return true;
  const normalized = identifier.toLowerCase();
  return POSTGRES_RESERVED_IDENTIFIER_KEYWORDS.has(normalized) || additionalKeywords?.has(normalized) === true || CLICKHOUSE_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(normalized);
}

export function requiresOracleIdentifierQuote(identifier: string): boolean {
  return !SIMPLE_ORACLE_IDENTIFIER.test(identifier) || isOracleReservedKeyword(identifier);
}

export function isOracleReservedKeyword(identifier: string): boolean {
  return ORACLE_RESERVED_IDENTIFIER_KEYWORDS.has(identifier.toUpperCase());
}

export function isDamengReservedKeyword(identifier: string): boolean {
  return DAMENG_RESERVED_IDENTIFIER_KEYWORDS.has(identifier.toUpperCase());
}

export function requiresDamengIdentifierQuote(identifier: string): boolean {
  return !SIMPLE_DAMENG_IDENTIFIER.test(identifier) || DAMENG_RESERVED_IDENTIFIER_KEYWORDS.has(identifier);
}

export function quoteGaussDbJdbcIdentifier(identifier: string, identifierQuote: string): string {
  if (isExplicitlyQuotedSqlIdentifier(identifier)) return identifier;
  const quote = identifierQuote.trim();
  if (!quote) return identifier;
  const requiresQuote = quote === "`" ? requiresPostgresIdentifierQuote(identifier) || MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(identifier.toLowerCase()) : requiresPostgresIdentifierQuote(identifier);
  if (!requiresQuote) return identifier;
  return `${quote}${identifier.replaceAll(quote, quote + quote)}${quote}`;
}