дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 10:07:11 +02:00
A custom JDBC source has driver "jdbc", so completion quoted only names that aren't plain words. On Oracle (and other case-folding servers) a mixed-case table such as BAS_MeterUnit was inserted bare and folded to BAS_METERUNIT (ORA-00942). Completion now takes the dialect from the source's URL scheme (jdbc:oracle:thin:@…, jdbc:postgresql://…), using the vendor scheme table Data Source from URL already uses, and quotes table, column and JOIN candidates the way that dialect needs. Unknown schemes keep the previous behaviour. Co-authored-by: eclix-del <[email protected]>
215 рядки
7.5 KiB
TypeScript
215 рядки
7.5 KiB
TypeScript
/**
|
|
* Data source types of New ▸ Data Source: the three drivers the backend speaks,
|
|
* and products that speak their wire protocol (a preset of port, icon and URL scheme).
|
|
*/
|
|
import type { DataSource, DbDriver, DbSnowflake, DbSslMode } from "@/platform/ipc";
|
|
|
|
export interface DbVendor {
|
|
/** Stored in `DataSource.vendor`; a driver's own entry has the driver's id. */
|
|
id: string;
|
|
label: string;
|
|
driver: DbDriver;
|
|
port: number | null;
|
|
icon: string;
|
|
/** JDBC URL scheme (`jdbc:<scheme>://…`). */
|
|
scheme: string;
|
|
/** Managed services that only accept encrypted connections. */
|
|
ssl?: DbSslMode;
|
|
}
|
|
|
|
export const VENDORS: readonly DbVendor[] = [
|
|
{ id: "aurora-mysql", label: "Amazon Aurora MySQL", driver: "mysql", port: 3306, icon: "db-aurora", scheme: "mysql" },
|
|
{
|
|
id: "redshift",
|
|
label: "Amazon Redshift",
|
|
driver: "postgres",
|
|
port: 5439,
|
|
icon: "db-redshift",
|
|
scheme: "redshift",
|
|
ssl: "require",
|
|
},
|
|
{
|
|
id: "clickhouse",
|
|
label: "ClickHouse",
|
|
driver: "clickhouse",
|
|
port: 8123,
|
|
icon: "db-clickhouse",
|
|
scheme: "clickhouse",
|
|
},
|
|
{
|
|
id: "cockroachdb",
|
|
label: "CockroachDB",
|
|
driver: "postgres",
|
|
port: 26257,
|
|
icon: "db-cockroach",
|
|
scheme: "postgresql",
|
|
},
|
|
{ id: "jdbc", label: "Custom JDBC", driver: "jdbc", port: null, icon: "database", scheme: "" },
|
|
{ id: "greenplum", label: "Greenplum", driver: "postgres", port: 5432, icon: "db-greenplum", scheme: "postgresql" },
|
|
{ id: "mariadb", label: "MariaDB", driver: "mysql", port: 3306, icon: "db-mariadb", scheme: "mariadb" },
|
|
{
|
|
id: "sqlServer",
|
|
label: "Microsoft SQL Server",
|
|
driver: "sqlServer",
|
|
port: 1433,
|
|
icon: "db-sqlserver",
|
|
scheme: "sqlserver",
|
|
},
|
|
{ id: "mysql", label: "MySQL", driver: "mysql", port: 3306, icon: "db-mysql", scheme: "mysql" },
|
|
{ id: "neon", label: "Neon", driver: "postgres", port: 5432, icon: "db-neon", scheme: "postgresql", ssl: "require" },
|
|
{
|
|
id: "oceanbase",
|
|
label: "OceanBase (MySQL mode)",
|
|
driver: "mysql",
|
|
port: 2881,
|
|
icon: "db-oceanbase",
|
|
scheme: "oceanbase",
|
|
},
|
|
{ id: "oracle", label: "Oracle", driver: "oracle", port: 1521, icon: "db-oracle", scheme: "oracle" },
|
|
{ id: "postgres", label: "PostgreSQL", driver: "postgres", port: 5432, icon: "db-postgres", scheme: "postgresql" },
|
|
{
|
|
id: "singlestore",
|
|
label: "SingleStore",
|
|
driver: "mysql",
|
|
port: 3306,
|
|
icon: "db-singlestore",
|
|
scheme: "singlestore",
|
|
},
|
|
{ id: "snowflake", label: "Snowflake", driver: "snowflake", port: 443, icon: "db-snowflake", scheme: "snowflake" },
|
|
{ id: "sqlite", label: "SQLite", driver: "sqlite", port: null, icon: "db-sqlite", scheme: "sqlite" },
|
|
{
|
|
id: "starrocks",
|
|
label: "StarRocks",
|
|
driver: "mysql",
|
|
port: 9030,
|
|
// No dedicated glyph yet: StarRocks speaks the MySQL protocol, so reuse its icon.
|
|
icon: "db-mysql",
|
|
scheme: "starrocks",
|
|
},
|
|
{
|
|
id: "supabase",
|
|
label: "Supabase",
|
|
driver: "postgres",
|
|
port: 5432,
|
|
icon: "db-supabase",
|
|
scheme: "postgresql",
|
|
ssl: "require",
|
|
},
|
|
{ id: "tidb", label: "TiDB", driver: "mysql", port: 4000, icon: "db-tidb", scheme: "mysql" },
|
|
{
|
|
id: "timescaledb",
|
|
label: "TimescaleDB",
|
|
driver: "postgres",
|
|
port: 5432,
|
|
icon: "db-timescale",
|
|
scheme: "postgresql",
|
|
},
|
|
{ id: "yugabytedb", label: "YugabyteDB", driver: "postgres", port: 5433, icon: "db-yugabyte", scheme: "postgresql" },
|
|
];
|
|
|
|
const BY_ID = new Map(VENDORS.map((v) => [v.id, v]));
|
|
|
|
/** The type of a data source: its preset, else its driver's own entry. */
|
|
export function vendorOf(s: Pick<DataSource, "vendor" | "driver">): DbVendor {
|
|
const v = s.vendor ? BY_ID.get(s.vendor) : undefined;
|
|
return v && v.driver === (s.driver ?? "sqlite") ? v : BY_ID.get(s.driver ?? "sqlite")!;
|
|
}
|
|
|
|
export const vendorById = (id: string): DbVendor | undefined => BY_ID.get(id);
|
|
|
|
/** The fields a new data source of type `v` starts with. */
|
|
export function vendorPreset(v: DbVendor): Partial<DataSource> {
|
|
const preset: Partial<DataSource> = { driver: v.driver, vendor: v.id === v.driver ? "" : v.id, port: v.port };
|
|
if (v.ssl) preset.ssl = v.ssl;
|
|
if (v.driver === "clickhouse") {
|
|
preset.user = "default";
|
|
preset.database = "default";
|
|
preset.ssl = "disable";
|
|
}
|
|
if (v.driver === "snowflake") {
|
|
// The account identifier has no useful default; key pairs are what Snowflake recommends.
|
|
preset.host = "";
|
|
preset.snowflake = blankSnowflake();
|
|
}
|
|
return preset;
|
|
}
|
|
|
|
export const blankSnowflake = (): DbSnowflake => ({ warehouse: "", role: "", auth: "keyPair", privateKeyFile: "" });
|
|
|
|
/** The host of a Snowflake account identifier (`myorg-myaccount` → `myorg-myaccount.snowflakecomputing.com`). */
|
|
export function snowflakeHost(account: string): string {
|
|
const host = account
|
|
.trim()
|
|
.replace(/^https?:\/\//i, "")
|
|
.replace(/[/?#].*$/, "");
|
|
if (!host || /\.snowflakecomputing\./i.test(host)) return host;
|
|
return `${host.replace(/_/g, "-")}.snowflakecomputing.com`;
|
|
}
|
|
|
|
/** URL schemes (besides the JDBC ones) that name a type. */
|
|
const SCHEMES: Record<string, string> = {
|
|
clickhouse: "clickhouse",
|
|
http: "clickhouse",
|
|
https: "clickhouse",
|
|
postgres: "postgres",
|
|
postgresql: "postgres",
|
|
pgsql: "postgres",
|
|
redshift: "redshift",
|
|
cockroachdb: "cockroachdb",
|
|
cockroach: "cockroachdb",
|
|
yugabytedb: "yugabytedb",
|
|
timescaledb: "timescaledb",
|
|
greenplum: "greenplum",
|
|
mysql: "mysql",
|
|
mariadb: "mariadb",
|
|
tidb: "tidb",
|
|
singlestore: "singlestore",
|
|
memsql: "singlestore",
|
|
oceanbase: "oceanbase",
|
|
starrocks: "starrocks",
|
|
oracle: "oracle",
|
|
oracledb: "oracle",
|
|
sqlserver: "sqlServer",
|
|
mssql: "sqlServer",
|
|
snowflake: "snowflake",
|
|
};
|
|
|
|
export const vendorOfScheme = (scheme: string): DbVendor | undefined => BY_ID.get(SCHEMES[scheme.toLowerCase()] ?? "");
|
|
|
|
/**
|
|
* The dialect a custom JDBC source's URL speaks (`jdbc:oracle:thin:@//db:1521/ORCL` → `oracle`):
|
|
* its `driver: "jdbc"` says nothing about how the server folds identifiers.
|
|
*/
|
|
export function jdbcDialect(url: string | null | undefined): DbDriver | null {
|
|
const scheme = /^(?:jdbc:)?([a-z][a-z0-9]*):/i.exec((url ?? "").trim())?.[1] ?? "";
|
|
return vendorOfScheme(scheme)?.driver ?? null;
|
|
}
|
|
|
|
const bracketed = (host: string) => (host.includes(":") && !host.startsWith("[") ? `[${host}]` : host);
|
|
|
|
/** The JDBC URL of a data source's fields (the General tab's URL preview). */
|
|
export function sourceUrl(s: DataSource): string {
|
|
const v = vendorOf(s);
|
|
if (v.driver === "jdbc") return s.url ?? "";
|
|
if (v.driver === "sqlite") return `jdbc:sqlite:${s.file ?? ""}`;
|
|
const host = bracketed(s.host?.trim() || "localhost");
|
|
const port = s.port ?? v.port;
|
|
const db = s.database ? `/${encodeURIComponent(s.database)}` : "";
|
|
if (v.driver === "snowflake") {
|
|
const settings = s.snowflake ?? blankSnowflake();
|
|
const query = new URLSearchParams();
|
|
if (s.database) query.set("db", s.database);
|
|
if (s.options?.defaultSchema) query.set("schema", s.options.defaultSchema);
|
|
if (settings.warehouse) query.set("warehouse", settings.warehouse);
|
|
if (settings.role) query.set("role", settings.role);
|
|
const account = snowflakeHost(s.host ?? "") || "<account>.snowflakecomputing.com";
|
|
const text = query.toString();
|
|
return `jdbc:snowflake://${account}${s.port && s.port !== 443 ? `:${s.port}` : ""}/${text ? `?${text}` : ""}`;
|
|
}
|
|
if (v.driver === "clickhouse") {
|
|
const secure = s.ssl != null && s.ssl !== "disable" && s.ssl !== "prefer";
|
|
return `${secure ? "https" : "http"}://${host}:${s.port ?? (secure ? 8443 : 8123)}${db}`;
|
|
}
|
|
return `jdbc:${v.scheme}://${host}${port ? `:${port}` : ""}${db}`;
|
|
}
|