Файли
openidea/src/features/database/SourceDialog.tsx
T
Waiting-Night 72ae354d29 feat(database): polish data source and table workflows (#7150)
- Introspected tree folder labels (tables, views, routines, keys, …) are localized.
- Database/schema introspection pickers get Select all filtered.
- New › Data Source lists recently added data source types in a Recent section first, seeded from existing sources on first use.
- New PostgreSQL data sources default to the postgres database.
- Modify Table is resizable, its content following the window size.

Co-authored-by: wangyuewen-china <[email protected]>
2026-12-01 15:33:08 +08:00

2385 рядки
88 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* Data source properties (IDEA's Data Sources and Drivers page): name, colour and
* comment above General / Options / SSH/SSL / Schemas / Advanced tabs, Test Connection.
*/
import { Fragment, useCallback, useEffect, useRef, useState, type ReactNode } from "react";
import {
call,
commands,
IpcError,
isTauri,
nextRequestId,
type DataSource,
type DbColor,
type DbExternalTunnel,
type DbIntrospection,
type DbJdbcDriver,
type DbOptions,
type DbProperty,
type DbSnowflake,
type DbSnowflakeAuth,
type DbSsh,
type SshAuth,
type DbSslMode,
type DbTxMode,
type RequestId,
} from "@/platform/ipc";
import { executeAction } from "@/platform/actions";
import { choosePath, chooseSavePath } from "@/platform/choosePath";
import { tx, txPart, useTx, useTxTemplate } from "@/platform/i18n";
import { useProject } from "@/platform/project";
import { useRemote } from "@/platform/remote";
import { sshLabel as configLabel } from "@/platform/remoteSettings";
import { useWorkflowSettings } from "@/platform/workflowSettings";
import { Button, Checkbox, Dialog, IconButton, Input, Select, Tabs, T, type SelectOption } from "@/ui";
import {
colorVar,
connectRemedy,
effectiveSsh,
fileSourceName,
newSourceId,
parseSourceUrl,
sshConfigOf,
sshLabel,
sshTunnelReady,
} from "./model";
import { closeDialog, loadDatabases, loadSchemas, saveSource, useDatabase } from "./store";
import { DUCKDB_JDBC_PREFIX, VENDORS, blankSnowflake, sourceUrl, vendorById, vendorOf, vendorPreset } from "./vendors";
import { JdbcDriverProfiles, jdbcProfileOption, useJdbcDrivers } from "./JdbcDriverProfiles";
import styles from "./Database.module.css";
import { isImeKey } from "@/platform/ime";
import { confirm } from "@/workbench/messages";
type Tab = "general" | "options" | "ssh" | "schemas" | "advanced";
type ConnectionMethod = "direct" | "ssh" | "external";
type DuckDbMode = "file" | "memory" | "url" | "ducklake";
type DuckLake = NonNullable<DataSource["duckLake"]>;
const DUCKDB_MODES: Array<{ id: DuckDbMode; label: string }> = [
{ id: "file", label: "File" },
{ id: "memory", label: "In memory" },
{ id: "ducklake", label: "DuckLake" },
{ id: "url", label: "JDBC URL" },
];
const DUCKLAKE_CATALOGS: Array<SelectOption<DuckLake["kind"]>> = [
{ value: "file", label: "Catalog file" },
{ value: "secret", label: "DuckDB secret" },
];
const TYPES: SelectOption[] = VENDORS.map((v) => ({ value: v.id, label: v.label, icon: v.icon }));
const COLORS: Array<SelectOption<DbColor | "none">> = [
{ value: "none", label: "No color" },
{ value: "red", label: "Red" },
{ value: "orange", label: "Orange" },
{ value: "yellow", label: "Yellow" },
{ value: "green", label: "Green" },
{ value: "blue", label: "Blue" },
{ value: "violet", label: "Violet" },
];
const SSL: Array<SelectOption<DbSslMode>> = [
{ value: "disable", label: "Disable" },
{ value: "prefer", label: "Prefer" },
{ value: "require", label: "Require" },
{ value: "verifyCa", label: "Verify CA" },
{ value: "verifyFull", label: "Verify Full" },
];
const CONNECTION_METHODS: Array<SelectOption<ConnectionMethod>> = [
{ value: "direct", label: "Direct" },
{ value: "ssh", label: "SSH tunnel" },
{ value: "external", label: "External TCP tunnel" },
];
const TX: Array<SelectOption<DbTxMode>> = [
{ value: "auto", label: "Auto" },
{ value: "manual", label: "Manual" },
];
const SSH_AUTH: Array<SelectOption<SshAuth>> = [
{ value: "openSsh", label: "OpenSSH config and authentication agent" },
{ value: "password", label: "Password" },
{ value: "keyPair", label: "Key pair" },
];
const SNOWFLAKE_AUTH: Array<SelectOption<DbSnowflakeAuth>> = [
{ value: "keyPair", label: "Key pair" },
{ value: "browser", label: "Single sign-on (web browser)" },
{ value: "password", label: "Password" },
{ value: "token", label: "Programmatic access token" },
{ value: "oAuth", label: "OAuth token" },
];
/** What the data source's secret is, by Snowflake authentication method. */
export function snowflakeSecretLabel(auth: DbSnowflakeAuth | null | undefined): string {
if (auth === "password") return "Password:";
if (auth === "token" || auth === "oAuth") return "Token:";
return "Passphrase:";
}
/** Driver property names the Advanced tab suggests, and what the backend does with them. */
const PROPERTIES: Record<string, { names: string[]; hint: string }> = {
jdbc: {
names: [],
hint: "Passed to the JDBC driver as connection properties. Configure TLS and other driver-specific settings here or in the JDBC URL. Use the User and Password fields for credentials.",
},
clickhouse: {
names: ["max_execution_time", "max_threads", "connect_timeout", "http_read_timeout"],
hint: "Query settings are sent with each request. connect_timeout and http_read_timeout are in seconds. The default wait for response data is 600 seconds.",
},
postgres: {
names: [
"statement_timeout",
"lock_timeout",
"idle_in_transaction_session_timeout",
"search_path",
"work_mem",
"application_name",
"connect_timeout",
],
hint: "Sent as run-time parameters when connecting (any server setting, e.g. statement_timeout). application_name names the session; connect_timeout is in seconds.",
},
mysql: {
names: ["sql_mode", "max_execution_time", "wait_timeout", "charset", "collation", "connect_timeout"],
hint: "Set as session variables after connecting (SET SESSION name = value). charset and collation pick the connection character set; connect_timeout is in seconds.",
},
sqlite: {
names: ["foreign_keys", "busy_timeout", "cache_size", "journal_mode", "synchronous"],
hint: "Applied as pragmas when opening the file (PRAGMA name = value).",
},
sqlServer: {
names: ["connect_timeout"],
hint: "Connection settings for SQL Server.",
},
snowflake: {
names: [
"QUERY_TAG",
"STATEMENT_TIMEOUT_IN_SECONDS",
"TIMEZONE",
"WEEK_START",
"CLIENT_SESSION_KEEP_ALIVE",
"connect_timeout",
],
hint: "Session parameters set when signing in (any session parameter, e.g. QUERY_TAG). connect_timeout is in seconds.",
},
oracle: {
names: ["NLS_DATE_FORMAT", "NLS_TIMESTAMP_FORMAT", "NLS_LANGUAGE", "connect_timeout"],
hint: "Session parameters set after connecting (ALTER SESSION SET name = value). connect_timeout is in seconds.",
},
};
const blankSsh = (): DbSsh => ({
enabled: false,
host: "",
port: null,
user: "",
auth: "openSsh",
keyFile: "",
saveSecret: false,
});
const blankOptions = (): DbOptions => ({
transaction: "auto",
defaultSchema: "",
timeZone: "",
startupScript: "",
keepAliveSecs: null,
autoDisconnectSecs: null,
singleDatabase: false,
});
const blankIntrospection = (): DbIntrospection => ({
allDatabases: true,
databases: [],
allSchemas: true,
schemas: [],
showSystem: false,
showTemplates: false,
objectFilter: "",
});
const parsePort = (v: string) => {
const n = parseInt(v, 10);
return Number.isFinite(n) && n > 0 && n < 65536 ? n : null;
};
const parseSecs = (v: string) => {
const n = parseInt(v, 10);
return Number.isFinite(n) && n > 0 ? n : null;
};
const windowsAbsolutePath = /^(?:[A-Za-z]:[/\\]|[/\\]{2})/;
const jdbcUrlPlaceholder = /<[^>]+>|\{(?:host|port|database|file)\}/;
function absoluteDatabaseFile(path: string, root: string | undefined): boolean {
return root && windowsAbsolutePath.test(root)
? windowsAbsolutePath.test(path)
: path.startsWith("/") || (!root && windowsAbsolutePath.test(path));
}
function jdbcDatabaseFile(file: string | null | undefined, root: string | undefined): string | null {
if (!file) return null;
const windows = !!root && windowsAbsolutePath.test(root);
const path = windows ? file.replace(/\\/g, "/") : file;
if (absoluteDatabaseFile(path, root)) return path;
// Windows drive-relative and current-drive paths depend on the process's working directory.
if (!root || (windows && /^(?:[A-Za-z]:|\/)/.test(path))) return null;
const directory = windows ? root.replace(/\\/g, "/") : root;
return `${directory.replace(/\/+$/, "")}/${path}`;
}
function duckLakePath(path: string, root: string | undefined, projectRelative = false): string | null {
if (!path.trim() || /[\0\r\n]/.test(path) || path.startsWith("//") || path.startsWith("\\")) return null;
return absoluteDatabaseFile(path, root) || (projectRelative && root && !path.includes(":"))
? jdbcDatabaseFile(path, root)
: null;
}
function builtInTarget(s: DataSource, driver: DataSource["driver"], root: string | undefined): Partial<DataSource> {
const url = s.url ?? "";
if (driver === "sqlite") {
const path = /^jdbc:sqlite3?:(.+)$/i.exec(url)?.[1] ?? "";
const file = windowsAbsolutePath.test(root ?? "") ? path.replace(/^\/([A-Za-z]:[/\\])/, "$1") : path;
return {
file: !/[?#]/.test(file) && !jdbcUrlPlaceholder.test(file) && absoluteDatabaseFile(file, root) ? file : null,
};
}
// The native form has one host. Multi-host and driver-specific descriptors need a manual target.
const target = url.replace(/^jdbc:clickhouse:(?=https?:\/\/)/i, "");
const parsed =
jdbcUrlPlaceholder.test(target) || target.includes("#") || /[;,\s]/.test(target.split("?", 1)[0] ?? "")
? null
: parseSourceUrl(target)?.source;
if (parsed?.driver !== driver || !parsed?.host) return { host: "", port: null, database: "", ssl: "verifyFull" };
return {
host: parsed.host,
port: parsed.port,
database: parsed.database,
user: parsed.user || s.user,
ssl: parsed.ssl && parsed.ssl !== "prefer" ? parsed.ssl : "verifyFull",
sslCaFile: parsed.sslCaFile ?? "",
sslCertFile: parsed.sslCertFile ?? "",
sslKeyFile: parsed.sslKeyFile ?? "",
};
}
function blank(): DataSource {
return {
id: newSourceId(),
name: "",
driver: "postgres",
vendor: "",
host: "localhost",
port: null,
database: "",
user: "",
file: null,
ssl: "prefer",
savePassword: false,
readOnly: false,
color: null,
comment: "",
urlOnly: false,
url: "",
jdbcDriver: "",
sslCaFile: "",
sslCertFile: "",
sslKeyFile: "",
options: blankOptions(),
introspection: blankIntrospection(),
properties: [],
};
}
function initialSource(initial: DataSource | null, preset: Partial<DataSource> | undefined): DataSource {
const defaults = initial || preset ? {} : vendorPreset(vendorById("postgres")!);
return { ...blank(), ...defaults, ...preset, ...initial };
}
async function pickFile(title: string): Promise<string | null> {
return choosePath({ directory: false, title });
}
/** A path field with Browse…. */
function FileField({
id,
label,
value,
placeholder,
disabled,
invalid,
browseLabel,
directory = false,
title,
onChange,
}: {
id: string;
label: string;
value: string;
placeholder?: string;
disabled?: boolean;
invalid?: boolean;
browseLabel?: string;
directory?: boolean;
title: string;
onChange: (v: string) => void;
}) {
return (
<>
<label className={styles.formLabel} htmlFor={id}>
<T>{label}</T>
</label>
<div className={styles.formRow}>
<Input
id={id}
className={styles.fill}
disabled={disabled}
invalid={invalid}
value={value}
placeholder={placeholder}
onChange={(e) => onChange(e.target.value)}
/>
<Button
disabled={disabled}
aria-label={browseLabel && tx(browseLabel)}
onClick={() =>
void choosePath({ directory, title: tx(title) }).then((f) => {
if (f) onChange(f);
})
}
>
<T>Browse…</T>
</Button>
</div>
</>
);
}
/** Seconds after a checkbox (Run keep-alive query each N sec.). */
function SecondsOption({
label,
value,
fallback,
onChange,
}: {
label: string;
value: number | null | undefined;
fallback: number;
onChange: (v: number | null) => void;
}) {
const [draft, setDraft] = useState(String(value ?? fallback));
const on = value != null;
return (
<>
<span />
<div className={styles.formRow}>
<Checkbox checked={on} onChange={(c) => onChange(c ? (parseSecs(draft) ?? fallback) : null)} label={label} />
<Input
className={styles.secondsInput}
inputMode="numeric"
aria-label={tx`${txPart(label)} (seconds)`}
disabled={!on}
value={draft}
onChange={(e) => {
setDraft(e.target.value);
const n = parseSecs(e.target.value);
if (n) onChange(n);
}}
/>
<span>
<T>sec.</T>
</span>
</div>
</>
);
}
/** Schemas tab: "All …" or a choice of names and `*` / `?` patterns. */
function NamePicker({
allLabel,
addLabel,
all,
chosen,
known,
onAll,
onChosen,
}: {
/** "All databases" */
allLabel: string;
/** Accessible name of the pattern input. */
addLabel: string;
all: boolean;
chosen: string[];
known: string[];
onAll: (all: boolean) => void;
onChosen: (chosen: string[]) => void;
}) {
const [pattern, setPattern] = useState("");
const names = [...new Set([...known, ...chosen])];
const add = () => {
const p = pattern.trim();
if (p && !chosen.includes(p)) onChosen([...chosen, p]);
setPattern("");
};
return (
<div className={styles.namePicker}>
<Checkbox checked={all} onChange={onAll} label={allLabel} />
{!all && (
<div className={styles.nameList}>
{names.map((n) => (
<Checkbox
key={n}
checked={chosen.includes(n)}
onChange={(c) => onChosen(c ? [...chosen, n] : chosen.filter((x) => x !== n))}
label={n}
/>
))}
<div className={styles.formRow}>
<Input
className={styles.fill}
value={pattern}
placeholder="Name or pattern, e.g. app_*"
aria-label={addLabel}
onChange={(e) => setPattern(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !isImeKey(e.nativeEvent)) {
e.preventDefault();
add();
}
}}
/>
<Button disabled={!pattern.trim()} onClick={add}>
<T>Add Pattern</T>
</Button>
</div>
</div>
)}
</div>
);
}
function Section({ title }: { title: string }) {
return (
<div className={styles.formSection}>
<T>{title}</T>
</div>
);
}
function visibleArgument(value: string): string {
// Keep quotes, whitespace and invisible formatting characters visible in the approval.
return JSON.stringify(value).replace(
/[\p{Cc}\p{Cf}\p{Default_Ignorable_Code_Point}]|[^\S ]/gu,
(character) => `\\u{${character.codePointAt(0)!.toString(16)}}`,
);
}
function externalReviewKey(source: DataSource): string | null {
if (!source.externalTunnel) return null;
return JSON.stringify([
source.externalTunnel,
source.driver,
source.host?.trim(),
source.port ?? vendorOf(source).port,
source.database,
source.user,
source.urlOnly,
source.ssl,
source.sslCaFile,
source.sslCertFile,
source.sslKeyFile,
source.readOnly,
source.properties?.filter((property) => property.name?.trim()),
source.options,
source.global,
]);
}
export function SourceDialog({
initial,
preset,
presetPassword,
}: {
initial: DataSource | null;
preset?: Partial<DataSource>;
presetPassword?: string | null;
}) {
const tx = useTx();
const txTemplate = useTxTemplate();
const [{ source: s, password, reviewedTunnel }, setDraft] = useState(() => ({
source: initialSource(initial, preset),
// `undefined`: unchanged (editing keeps the stored password).
password: presetPassword ?? undefined,
reviewedTunnel: false,
}));
const setS = (update: (source: DataSource) => DataSource) =>
setDraft((draft) => {
const source = update(draft.source);
const reviewedTunnel = draft.reviewedTunnel && externalReviewKey(source) === externalReviewKey(draft.source);
return { ...draft, source, reviewedTunnel };
});
const setPassword = (password: string | undefined) =>
setDraft((draft) => ({ ...draft, password, reviewedTunnel: draft.reviewedTunnel && draft.password === password }));
// Same convention for the SSH password / passphrase.
const [sshSecret, setSshSecret] = useState<string | undefined>(undefined);
const [tab, setTab] = useState<Tab>("general");
const [test, setTest] = useState<{ ok: boolean; text: string; trust?: boolean; tunnel?: boolean } | null>(null);
const [busyKind, setBusy] = useState<"connecting" | "external" | "creatingLake" | null>(null);
const busy = busyKind !== null;
const [reviewRequest, setReviewRequest] = useState(0);
const reviewPanel = useRef<HTMLDivElement>(null);
useEffect(() => {
if (reviewRequest) reviewPanel.current?.querySelector("input")?.focus();
}, [reviewRequest]);
const externalRequest = useRef<RequestId | null>(null);
const cancelExternalTest = useCallback(() => {
const request = externalRequest.current;
if (request === null) return;
externalRequest.current = null;
void commands.cancelRequest(request);
}, []);
useEffect(() => cancelExternalTest, [cancelExternalTest]);
const [showComment, setShowComment] = useState(!!s.comment);
// URL text being edited in native default mode or DuckDB's explicit URL mode.
const [urlDraft, setUrlDraft] = useState<string | null>(s.vendor === "duckdb" && s.urlOnly ? (s.url ?? "") : null);
const [showJdbcProfiles, setShowJdbcProfiles] = useState(false);
const [nativeDraft, setNativeDraft] = useState<{
source: DataSource;
password: string | undefined;
endpoint: Pick<DataSource, "host" | "port" | "database" | "user" | "file">;
} | null>(null);
const nativeSource = nativeDraft?.source;
const [restoredNativeTarget, setRestoredNativeTarget] = useState(false);
const [connectionSecurityConfirmed, setConnectionSecurityConfirmed] = useState(false);
const jdbc = s.driver === "jdbc";
const vendor = vendorOf(s);
const genericJdbc = vendor.id === "jdbc";
const duckdb = vendor.id === "duckdb";
const jdbcOnly = vendor.driver === "jdbc";
const [duckDbMode, setDuckDbMode] = useState<DuckDbMode>(() => {
if (s.duckLake) return "ducklake";
if (s.urlOnly) return "url";
return !s.file && s.url === DUCKDB_JDBC_PREFIX ? "memory" : "file";
});
const lakeMode = duckdb && duckDbMode === "ducklake";
const lake: DuckLake = s.duckLake ?? { kind: "file", value: "", dataPath: null };
const savedJdbcWithPreset = initial?.driver === "jdbc" && (!genericJdbc || vendorOf(initial).driver !== "jdbc");
const jdbcProfiles = useJdbcDrivers();
const availableProfiles = jdbcProfiles.drivers?.filter((d) => genericJdbc || d.vendor === vendor.id);
const selectedProfileId = s.jdbcDriver || (duckdb && availableProfiles?.length === 1 ? availableProfiles[0]!.id : "");
const jdbcProfile = availableProfiles?.find((d) => d.id === selectedProfileId);
const installedJdbcProfile = jdbcProfiles.drivers?.find((d) => d.id === s.jdbcDriver);
const projectRoot = useProject((st) => st.project?.root);
const lakeValue =
lake.kind === "file"
? duckLakePath(lake.value, projectRoot, true)
: /^[A-Za-z_][A-Za-z0-9_]*$/.test(lake.value)
? lake.value
: null;
const lakeDataPath = lake.dataPath ? duckLakePath(lake.dataPath, projectRoot) : null;
const remote = useRemote((st) => st.session !== null);
const nativeUrl = nativeSource?.urlOnly ? parseSourceUrl(nativeSource.url ?? "") : null;
const nativeOptions = nativeSource?.options;
const reviewSessionSettings =
nativeOptions?.startupScript ||
nativeOptions?.defaultSchema ||
nativeOptions?.timeZone ||
nativeOptions?.transaction === "manual";
const newJdbcPassword = jdbc && nativeSource?.driver === "snowflake" && s.snowflake?.auth !== "password";
const reviewJdbcSecurity =
jdbc &&
((savedJdbcWithPreset &&
(initial.jdbcDriver !== s.jdbcDriver ||
nativeSource ||
(!duckdb && s.url && s.url === jdbcProfile?.urlTemplate && s.url !== initial.url))) ||
(nativeSource &&
(!!initial ||
nativeSource.driver === "clickhouse" ||
nativeSource.driver === "snowflake" ||
nativeSource.ssh?.enabled ||
nativeSource.externalTunnel ||
nativeSource.urlOnly ||
["require", "verifyCa", "verifyFull"].includes(nativeSource.ssl ?? "") ||
nativeSource.sslCaFile ||
nativeSource.sslCertFile ||
nativeSource.sslKeyFile ||
nativeSource.readOnly ||
reviewSessionSettings ||
(nativeSource.properties?.length ?? 0) > 0)));
const reviewBuiltinSecurity = !jdbc && (initial?.driver === "jdbc" || restoredNativeTarget);
const loadedDbs = useDatabase((st) => (initial?.id ? st.databases[initial.id] : undefined));
const loadedSchemas = useDatabase((st) => (initial?.id ? st.schemas[initial.id] : undefined));
const set = (patch: Partial<DataSource>) => setS((x) => ({ ...x, ...patch }));
const setLake = (patch: Partial<DuckLake>) => {
set({ duckLake: { ...lake, ...patch } });
setTest(null);
};
const ssh: DbSsh = { ...blankSsh(), ...s.ssh };
const sshConfigs = useWorkflowSettings((w) => w.ssh.configs);
const sshConfigId = ssh.config?.trim() ?? "";
const sshConfig = sshConfigOf(ssh, sshConfigs);
// The host / user / authentication in effect (the chosen SSH configuration's).
const sshUsed: DbSsh = effectiveSsh(ssh, sshConfigs) ?? ssh;
const setSsh = (patch: Partial<DbSsh>) => setS((x) => ({ ...x, ssh: { ...blankSsh(), ...x.ssh, ...patch } }));
const options: DbOptions = { ...blankOptions(), ...s.options };
const connectionTimers = (targetDriver: DataSource["driver"]) => ({
keepAliveSecs: s.driver === "sqlite" || targetDriver === "sqlite" ? null : options.keepAliveSecs,
autoDisconnectSecs: options.autoDisconnectSecs,
});
const setOptions = (patch: Partial<DbOptions>) =>
setS((x) => ({ ...x, options: { ...blankOptions(), ...x.options, ...patch } }));
const intro: DbIntrospection = { ...blankIntrospection(), ...s.introspection };
const setIntro = (patch: Partial<DbIntrospection>) =>
setS((x) => ({ ...x, introspection: { ...blankIntrospection(), ...x.introspection, ...patch } }));
const properties = s.properties ?? [];
const setProperty = (i: number, patch: Partial<DbProperty>) =>
set({ properties: properties.map((p, j) => (j === i ? { ...p, ...patch } : p)) });
const sqlite = s.driver === "sqlite";
const postgres = s.driver === "postgres";
const externalTunnel = lakeMode ? null : s.externalTunnel;
const setExternalTunnel = (patch: Partial<DbExternalTunnel>) =>
setS((x) => (x.externalTunnel ? { ...x, externalTunnel: { ...x.externalTunnel, ...patch } } : x));
const externalAvailable = postgres && isTauri() && !remote && !s.pluginOwner;
/** Drivers that can list every database on the host (tree level + Schemas tab). */
const serverDatabases = postgres || s.driver === "sqlServer" || s.driver === "snowflake";
const clickhouse = s.driver === "clickhouse";
const oracle = s.driver === "oracle";
const snowflake = s.driver === "snowflake";
const sf: DbSnowflake = { ...blankSnowflake(), ...s.snowflake };
const urlOnly = !sqlite && !jdbc && !!s.urlOnly;
const parsedUrl = urlOnly ? parseSourceUrl(s.url ?? "") : null;
const snowflakeAuth = parsedUrl?.source.snowflake?.auth ?? sf.auth;
const setSnowflake = (patch: Partial<DbSnowflake>) => {
if (patch.auth && patch.auth !== snowflakeAuth) setPassword("");
setS((x) => ({ ...x, snowflake: { ...blankSnowflake(), ...x.snowflake, ...patch } }));
};
const browserSignIn = snowflake && snowflakeAuth === "browser";
const urlValid = !!parsedUrl && parsedUrl.source.driver === s.driver;
const host = urlOnly ? (parsedUrl?.source.host ?? "") : (s.host ?? "");
const database = urlOnly ? (parsedUrl?.source.database ?? "") : (s.database ?? "");
const name =
s.name?.trim() ||
(genericJdbc
? (jdbcProfile?.name ?? tx("Custom JDBC"))
: duckdb
? (lakeMode && tx("DuckLake")) || (duckDbMode === "file" && fileSourceName(s.file ?? "")) || vendor.label
: vendor.driver === "sqlite"
? fileSourceName(s.file ?? "")
: `${database || s.user || "db"}@${host || "localhost"}`);
// Snowflake is reached over HTTPS, where a tunnel to another host name cannot pass certificate checks.
const sshOn = !sqlite && !snowflake && !jdbc && !externalTunnel && !!ssh.enabled;
const connectionMethod = externalTunnel ? "external" : sshOn ? "ssh" : "direct";
const setConnectionMethod = (method: ConnectionMethod) => {
setTest(null);
set({
ssh: { ...ssh, enabled: method === "ssh" },
externalTunnel: method === "external" ? (externalTunnel ?? { executable: "", arguments: [] }) : null,
});
};
const externalError = externalTunnel
? !externalAvailable
? tx("External TCP tunnels require a local desktop window and a user-managed PostgreSQL source.")
: urlOnly
? tx("External TCP tunnels use the host and port from the default connection type.")
: s.ssl === "verifyFull"
? tx("Verify Full is unavailable with an external TCP tunnel.")
: null
: null;
const template = jdbcProfile?.urlTemplate ?? "";
const templateFields = (["host", "port", "database", "file"] as const).filter((key) => template.includes(`{${key}}`));
const renderedUrl = template.replace(
/\{(host|port|database|file)\}/g,
(token, key: "host" | "port" | "database" | "file") => String(s[key] || token),
);
const fromTemplate = !duckdb && !!templateFields.length && (s.url === template || s.url === renderedUrl);
const duckDbFile = duckdb && duckDbMode === "file" ? jdbcDatabaseFile(s.file, projectRoot) : null;
const duckDbFileError =
!duckDbFile || !s.file?.trim() || /[\r\n]/.test(duckDbFile)
? tx("Enter an absolute database path or a path relative to the open project.")
: duckDbFile.includes(";")
? tx("DuckDB database paths cannot contain semicolons.")
: null;
const jdbcUrl =
duckdb && duckDbMode === "file"
? duckDbFileError
? ""
: DUCKDB_JDBC_PREFIX + duckDbFile
: duckdb
? duckDbMode === "memory" || lakeMode
? DUCKDB_JDBC_PREFIX
: (urlDraft ?? s.url ?? "")
: fromTemplate
? renderedUrl
: (s.url ?? "");
const duckDbTarget = jdbcUrl.split(";", 1)[0]!;
const duckDbMemory =
duckdb &&
(duckDbMode === "memory" ||
(duckDbMode === "url" &&
(duckDbTarget === DUCKDB_JDBC_PREFIX ||
duckDbTarget.startsWith(DUCKDB_JDBC_PREFIX + "memory:") ||
duckDbTarget.startsWith(DUCKDB_JDBC_PREFIX + ":memory:"))));
const chooseProfile = (profile?: DbJdbcDriver, type = vendor) => {
setTest(null);
setConnectionSecurityConfirmed(false);
if (duckdb) {
set({ jdbcDriver: profile?.id ?? "" });
return;
}
if (jdbc) {
const wasDefault = !s.url || (!savedJdbcWithPreset && (fromTemplate || s.url === jdbcProfile?.urlTemplate));
set({ jdbcDriver: profile?.id ?? "", url: wasDefault ? (profile?.urlTemplate ?? "") : s.url });
return;
}
const parsed = s.urlOnly ? parseSourceUrl(s.url ?? "")?.source : undefined;
const file = parsed?.file ?? s.file;
const secure = ["require", "verifyCa", "verifyFull"].includes(parsed?.ssl ?? s.ssl ?? "");
const endpoint = {
host: parsed?.host ?? s.host,
port: (parsed ? parsed.port : s.port) ?? (clickhouse && secure ? 8443 : vendor.port),
database: parsed?.database ?? s.database,
user: parsed?.user || s.user,
file: sqlite ? jdbcDatabaseFile(file, projectRoot) : file,
};
setNativeDraft({ source: s, password, endpoint });
const snowflakeOptions = { ...sf, ...parsed?.snowflake };
const needsJdbcPassword = snowflake && snowflakeOptions.auth !== "password";
if (needsJdbcPassword) setPassword(undefined);
set({
...endpoint,
driver: "jdbc",
vendor: type.id === "jdbc" ? "" : type.id,
...(snowflake ? { snowflake: snowflakeOptions } : {}),
...(needsJdbcPassword ? { savePassword: false } : {}),
urlOnly: true,
url: profile?.urlTemplate ?? "",
jdbcDriver: profile?.id ?? "",
properties: [],
options: { ...blankOptions(), ...connectionTimers("jdbc") },
ssh: null,
externalTunnel: null,
ssl: "disable",
sslCaFile: "",
sslCertFile: "",
sslKeyFile: "",
});
};
const jdbcUrlValid =
duckdb && duckDbMode === "file"
? duckDbFileError === null
: /^jdbc:[^\s:]+:[^\r\n]*$/.test(jdbcUrl) &&
!jdbcUrlPlaceholder.test(jdbcUrl) &&
(!duckdb || jdbcUrl.startsWith(DUCKDB_JDBC_PREFIX));
const lakeDriver = jdbcProfile?.className === "org.duckdb.DuckDBDriver";
const valid =
(!externalTunnel ||
(!externalError &&
!!externalTunnel.executable.trim() &&
externalTunnel.arguments.some((argument) => argument.includes("{localPort}")))) &&
(jdbc
? !!jdbcProfile &&
jdbcUrlValid &&
!(duckDbMemory && s.readOnly) &&
(!lakeMode || (lakeDriver && !!lakeValue && (!lake.dataPath || !!lakeDataPath))) &&
(!reviewJdbcSecurity || connectionSecurityConfirmed)
: (!reviewBuiltinSecurity || connectionSecurityConfirmed) &&
(sqlite
? !!s.file?.trim()
: (urlOnly ? urlValid : !!s.host?.trim()) &&
(!sshOn || sshTunnelReady(ssh, sshConfigs)) &&
(!snowflake || (!!s.user?.trim() && (sf.auth !== "keyPair" || !!sf.privateKeyFile?.trim())))));
const sslOn = (s.ssl ?? "prefer") !== "disable";
/** What is saved / tested: a preset's port when the field was left empty. */
const effective = (): DataSource => ({
...s,
name,
port: s.port ?? (s.vendor ? vendor.port : null),
...(externalTunnel ? { ssh: null } : {}),
...(jdbc
? {
jdbcDriver: jdbcProfile?.id ?? "",
url: jdbcUrl,
urlOnly: !duckdb || duckDbMode === "url",
...(duckdb && { file: duckDbFile }),
duckLake: lakeMode ? { kind: lake.kind, value: lakeValue ?? "", dataPath: lakeDataPath } : null,
...(lakeMode && { readOnly: true, user: "", savePassword: false, properties: [], externalTunnel: null }),
ssh: null,
options: {
...options,
transaction: "auto" as const,
...(lakeMode && { startupScript: "", defaultSchema: "", timeZone: "" }),
},
}
: { duckLake: null }),
});
const setType = (id: string, builtIn = false) => {
const v = vendorById(id);
if (!v || (v.id === vendor.id && (!jdbc || !builtIn))) return;
setTest(null);
setUrlDraft(null);
setConnectionSecurityConfirmed(false);
if (v.id === "duckdb") {
const sameDriver = installedJdbcProfile?.vendor === v.id;
const existingDuckUrl = jdbc && jdbcUrl.startsWith(DUCKDB_JDBC_PREFIX);
if (!jdbc) chooseProfile(undefined, v);
setDuckDbMode(existingDuckUrl ? "url" : "file");
setUrlDraft(existingDuckUrl ? jdbcUrl : null);
set({ vendor: v.id, file: null, url: "", jdbcDriver: sameDriver ? s.jdbcDriver : "" });
return;
}
if (v.id === "jdbc") {
if (duckdb) set({ vendor: "", jdbcDriver: selectedProfileId, file: duckDbFile, url: jdbcUrl });
else if (jdbc) set({ vendor: "" });
else chooseProfile(undefined, v);
return;
}
if (jdbc && genericJdbc && installedJdbcProfile?.vendor === v.id) {
set({ vendor: v.id });
return;
}
const previous =
jdbc && nativeSource
? {
...nativeSource,
name: s.name,
color: s.color,
comment: s.comment,
global: s.global,
savePassword: newJdbcPassword ? nativeSource.savePassword : s.savePassword,
readOnly: s.readOnly,
introspection: intro,
options: { ...blankOptions(), ...nativeSource.options, ...connectionTimers(v.driver) },
}
: s;
if (nativeDraft) {
setPassword(nativeDraft.password);
const changed = (["host", "port", "database", "user", "file"] as const).some(
(field) => s[field] !== nativeDraft.endpoint[field],
);
setRestoredNativeTarget(changed || (!!s.url && !fromTemplate && s.url !== template));
} else {
setRestoredNativeTarget(false);
if (jdbc && v.driver === "snowflake" && sf.auth !== "password") setPassword(undefined);
}
setNativeDraft(null);
if (jdbc && nativeSource && vendorOf(nativeSource).id === v.id) {
setS(() => previous);
return;
}
setS(() => {
const x = previous;
const old = vendorOf(x);
const port = x.port == null || x.port === old.port ? v.port : x.port;
const database =
!initial && v.id === "postgres" && !x.database
? "postgres"
: !initial && vendor.id === "postgres" && x.database === "postgres" && v.id !== "postgres"
? ""
: x.database;
return {
...x,
driver: v.driver,
vendor: v.id === v.driver ? "" : v.id,
port,
database,
...(v.driver !== "postgres" && x.externalTunnel ? { externalTunnel: null } : {}),
ssl: v.ssl ?? (v.driver === "clickhouse" ? "disable" : x.ssl),
...(v.driver === "sqlite" && x.driver !== "jdbc" ? { options: { ...x.options, keepAliveSecs: null } } : {}),
...(v.driver === "snowflake"
? { host: x.host === "localhost" ? "" : x.host, snowflake: { ...blankSnowflake(), ...x.snowflake } }
: {}),
...(v.driver === "clickhouse"
? {
user: x.user || "default",
database: database || "default",
options: { ...blankOptions(), ...x.options, transaction: "auto" as const },
}
: {}),
...(x.driver === "jdbc"
? {
jdbcDriver: "",
urlOnly: false,
url: "",
properties: [],
options: { ...blankOptions(), ...connectionTimers(v.driver) },
ssh: null,
...builtInTarget({ ...x, url: jdbcUrl }, v.driver, projectRoot),
}
: {}),
};
});
};
/** Typing a URL in default mode fills the fields it names. */
const typeUrl = (text: string) => {
setUrlDraft(text);
const p = parseSourceUrl(text);
if (!p || p.source.driver !== s.driver) return;
const { host: h, port, database: db, user, ssl, sslCaFile, sslCertFile, sslKeyFile, vendor: v } = p.source;
if (p.source.snowflake) setSnowflake(p.source.snowflake);
if (p.source.options?.defaultSchema) setOptions({ defaultSchema: p.source.options.defaultSchema });
set({
host: h,
port,
database: db,
...(user ? { user } : {}),
...(ssl ? { ssl } : {}),
...(sslCaFile ? { sslCaFile } : {}),
...(sslCertFile ? { sslCertFile } : {}),
...(sslKeyFile ? { sslKeyFile } : {}),
...(v ? { vendor: v } : {}),
});
if (p.password) setPassword(p.password);
};
const setConnectionUrl = (url: string, on: boolean) => {
if (snowflake) {
const auth = (on ? parseSourceUrl(url)?.source.snowflake?.auth : undefined) ?? sf.auth;
if (auth !== snowflakeAuth) setPassword("");
}
set({ url, urlOnly: on });
};
const setUrlOnly = (on: boolean) => setConnectionUrl(on && !s.url?.trim() ? sourceUrl(s) : (s.url ?? ""), on);
const externalReviewed = () => {
if (!externalTunnel || reviewedTunnel) return true;
setTab("ssh");
setReviewRequest((n) => n + 1);
return false;
};
/** Test Connection, or with `tunnel` the SSH/SSL tab's Test SSH Tunnel (no database login). */
const runTest = async (tunnel = false) => {
if (busy || (!tunnel && (!valid || !externalReviewed()))) return;
const source = effective();
const request = !tunnel && source.externalTunnel ? nextRequestId() : null;
if (request !== null) externalRequest.current = request;
const stillTesting = () => request === null || externalRequest.current === request;
setBusy(request === null ? "connecting" : "external");
setTest(null);
try {
const secret = sshOn ? (sshSecret ?? null) : null;
if (tunnel) {
const route = await call(commands.dbTestTunnel(source, secret));
setTest({ ok: true, text: tx`SSH tunnel works: ${route}`, tunnel });
} else {
const v = await call(
request === null
? commands.dbTest(source, lakeMode ? null : (password ?? null), secret)
: commands.dbTestExternalTunnel(request, source, password ?? null),
);
if (stillTesting()) setTest({ ok: true, text: tx`Succeeded: ${v}` });
}
} catch (e) {
if (stillTesting()) {
const text = e instanceof Error ? e.message : String(e);
const trust =
request === null &&
e instanceof IpcError &&
connectRemedy(e.kind, e.message, { ssh: sshUsed }) === "trustHostKey";
setTest({ ok: false, text, trust, tunnel });
}
} finally {
if (stillTesting()) {
externalRequest.current = null;
setBusy(null);
}
}
};
const trustAndRetry = async () => {
// A shared SSH configuration records the failed host key under its own id.
await call(sshConfigId ? commands.sshTrustHost(sshConfigId) : commands.dbTrustSshHost(s.id ?? ""));
await runTest(!!test?.tunnel);
};
const save = async () => {
if (busy || !valid || !externalReviewed()) return;
const draft = effective();
const dialog = useDatabase.getState().dialog;
setBusy("connecting");
try {
const source = { ...draft, properties: draft.properties?.filter((p) => p.name?.trim()) ?? [] };
// An older host can discard DuckLake settings when saving a regular source.
if (source.duckLake) await call(commands.dbApproveDucklakeSource(source));
else if (jdbc && !source.global) await call(commands.dbApproveJdbcSource(source));
if (source.externalTunnel) await call(commands.dbApproveExternalTunnel(source));
await saveSource(
source,
lakeMode ? null : sqlite || password === undefined ? undefined : password || null,
!sshOn || sshSecret === undefined || sshUsed.auth === "openSsh" ? undefined : sshSecret || null,
);
if (useDatabase.getState().dialog === dialog) closeDialog();
} catch (e) {
const unsupported =
draft.duckLake &&
e instanceof IpcError &&
e.kind === "protocol" &&
e.message === "command db_approve_ducklake_source is not available remotely";
const text = unsupported
? tx("Update the remote OpenIDEA server to save DuckLake data sources.")
: e instanceof Error
? e.message
: String(e);
setTest({ ok: false, text });
} finally {
setBusy(null);
}
};
const loadNames = async () => {
const id = initial?.id;
if (!id) return;
await Promise.all([serverDatabases ? loadDatabases(id) : Promise.resolve(), loadSchemas(id)]);
};
const tabs: Array<{ id: Tab; label: string }> = [
{ id: "general", label: "General" },
{ id: "options", label: "Options" },
...(sqlite || snowflake || jdbc ? [] : [{ id: "ssh" as const, label: "SSH/SSL" }]),
...(sqlite ? [] : [{ id: "schemas" as const, label: "Schemas" }]),
...(lakeMode ? [] : [{ id: "advanced" as const, label: "Advanced" }]),
];
const current = tabs.some((t) => t.id === tab) ? tab : "general";
const props = duckdb
? { names: [], hint: "Passed to the DuckDB JDBC driver as connection properties." }
: PROPERTIES[s.driver ?? "sqlite"]!;
const pickDuckDbFile = async (create: boolean) => {
const filters = [{ name: "DuckDB", extensions: ["duckdb", "db"] }];
const defaultPath = duckDbFile ?? projectRoot;
const file = create
? await chooseSavePath({
title: tx("New DuckDB Database"),
filters,
defaultPath: jdbcDatabaseFile("database.duckdb", projectRoot) ?? "database.duckdb",
})
: await choosePath({ directory: false, title: tx("Open DuckDB Database"), filters, defaultPath });
if (file) {
set({ file });
setTest(null);
}
};
const createLake = async () => {
if (busy || !lakeDriver || (reviewJdbcSecurity && !connectionSecurityConfirmed)) return;
setBusy("creatingLake");
setTest(null);
try {
const picked = await chooseSavePath({
title: tx("Choose a new DuckLake directory"),
defaultPath: jdbcDatabaseFile("ducklake", projectRoot) ?? "ducklake",
});
if (!picked) return;
const directory = duckLakePath(picked, projectRoot);
if (!directory) throw new Error(tx("Choose an absolute directory on the database host."));
const approved = await confirm({
title: "Create New DuckLake",
message: tx`Create a DuckLake in '${directory}'? The directory must not already exist.`,
ok: "Create",
});
if (!approved) return;
const path = `${directory.replace(/\/+$/, "")}/catalog.ducklake`;
const duckLake = { kind: "file" as const, value: path, dataPath: null };
await call(commands.dbCreateDucklake(selectedProfileId, directory));
set({ duckLake });
setTest({ ok: true, text: tx("DuckLake created. The data source opens it read-only.") });
} catch (e) {
setTest({ ok: false, text: e instanceof Error ? e.message : String(e) });
} finally {
setBusy(null);
}
};
const general = (
<>
<span className={styles.formLabel}>
<T>Data source type:</T>
</span>
<Select aria-label="Data source type" value={vendor.id} options={TYPES} onChange={(id) => setType(id)} />
<span className={styles.formLabel}>{tx(genericJdbc ? "JDBC driver profile:" : "Driver:")}</span>
<div className={styles.formRow}>
<Select
className={styles.fill}
aria-label={genericJdbc ? "JDBC driver profile" : "Connection driver"}
value={jdbc ? selectedProfileId : ""}
disabled={jdbcOnly && !jdbcProfiles.drivers}
onChange={(id) => {
if (!id && !jdbcOnly) setType(vendor.id, true);
else chooseProfile(availableProfiles?.find((d) => d.id === id));
}}
options={[
{
value: "",
label: jdbcOnly ? "Choose a JDBC driver profile" : "Built-in",
disabled: duckdb,
},
...(availableProfiles ?? []).map(jdbcProfileOption),
...(jdbc && s.jdbcDriver && !jdbcProfile
? [
{
value: s.jdbcDriver,
label: installedJdbcProfile?.name ?? tx`${s.jdbcDriver} (missing)`,
verbatim: true,
disabled: true,
},
]
: []),
]}
/>
<Button disabled={!jdbcProfiles.drivers} onClick={() => setShowJdbcProfiles(true)}>
{tx(jdbc && jdbcProfile && !genericJdbc ? "Go to Driver" : "Manage Drivers…")}
</Button>
</div>
{jdbcProfiles.error ? (
<div className={styles.formFull} role="alert">
{jdbcProfiles.error}{" "}
<Button onClick={jdbcProfiles.retry}>
<T>Retry</T>
</Button>
</div>
) : !jdbcProfiles.drivers && jdbc ? (
<span className={styles.formFull} role="status">
<T>Loading JDBC driver profiles…</T>
</span>
) : duckdb && !jdbcProfile && !availableProfiles?.length ? (
<span className={styles.formFull} role="status">
<T>Add the official DuckDB JDBC JAR in Manage Drivers… to connect.</T>
</span>
) : jdbc && s.jdbcDriver && !jdbcProfile && jdbcProfiles.drivers ? (
<span className={`${styles.formFull} ${styles.bad}`} role="alert">
{tx(
duckdb
? "Select a DuckDB driver profile or add the official DuckDB JDBC JAR in Manage Drivers…"
: installedJdbcProfile
? "The JDBC driver profile is assigned to a different data source type. Select a matching profile or update its type in Custom JDBC."
: "The JDBC driver profile is missing. Select or add a profile before connecting.",
)}
</span>
) : null}
{duckdb ? (
<>
<span className={styles.formLabel}>
<T>Connection type:</T>
</span>
<div className={styles.segmented} role="group" aria-label={tx("Connection type")}>
{DUCKDB_MODES.map(({ id, label }) => (
<button
type="button"
key={id}
aria-pressed={id === duckDbMode}
disabled={busy}
onClick={() => {
setDuckDbMode(id);
if (id === "url" && duckDbMode !== "url" && urlDraft === null) set({ url: jdbcUrl });
setTest(null);
}}
>
<T>{label}</T>
</button>
))}
</div>
{duckDbMode === "file" && (
<>
<label className={styles.formLabel} htmlFor="db-duckdb-file">
<T>File:</T>
</label>
<div className={styles.formRow}>
<Input
id="db-duckdb-file"
className={styles.fill}
value={s.file ?? ""}
invalid={!!s.file && !!duckDbFileError}
placeholder="path/to/database.duckdb"
onChange={(e) => {
set({ file: e.target.value });
setTest(null);
}}
/>
<Button onClick={() => void pickDuckDbFile(false)}>
<T>Browse…</T>
</Button>
<Button disabled={!!s.readOnly} onClick={() => void pickDuckDbFile(true)}>
<T>New File…</T>
</Button>
</div>
<span />
{s.file && duckDbFileError ? (
<span className={styles.bad} role="alert">
{duckDbFileError}
</span>
) : (
<span className={styles.formHint}>
{s.readOnly ? (
<T>Read-only requires an existing database file.</T>
) : (
<T>Creates the database on first connection if the file does not exist.</T>
)}
</span>
)}
</>
)}
{lakeMode ? (
<>
<span className={styles.formLabel}>
<T>Catalog type:</T>
</span>
<Select
aria-label={tx("Catalog type")}
options={DUCKLAKE_CATALOGS}
value={lake.kind}
disabled={busy}
onChange={(kind) => setLake({ kind, value: "" })}
/>
{lake.kind === "file" ? (
<FileField
id="db-ducklake-file"
label="Catalog file:"
title="Open DuckLake Catalog"
browseLabel="Browse for DuckLake catalog"
disabled={busy}
invalid={!!lake.value && !lakeValue}
value={lake.value}
onChange={(value) => setLake({ value })}
/>
) : (
<>
<label className={styles.formLabel} htmlFor="db-ducklake-secret">
<T>Secret name:</T>
</label>
<Input
id="db-ducklake-secret"
disabled={busy}
invalid={!!lake.value && !lakeValue}
value={lake.value}
onChange={(e) => setLake({ value: e.target.value })}
/>
</>
)}
<span />
<span className={styles.formHint}>
{lake.kind === "file" ? (
<T>Choose an existing catalog file on the database host.</T>
) : (
<T>Use the name of an existing DuckDB secret of type ducklake.</T>
)}
</span>
<FileField
id="db-ducklake-data"
label="Data directory:"
title="Choose DuckLake Data Directory"
browseLabel="Browse for DuckLake data directory"
value={lake.dataPath ?? ""}
disabled={busy}
invalid={!!lake.dataPath && !lakeDataPath}
directory
onChange={(dataPath) => setLake({ dataPath })}
/>
<span />
<span className={styles.formHint}>
<T>
Optional absolute directory on the database host. Leave empty to use the catalog’s stored location.
</T>
</span>
<span />
<div className={styles.formRow}>
<Button
disabled={busy || !lakeDriver || !!(reviewJdbcSecurity && !connectionSecurityConfirmed)}
onClick={() => void createLake()}
>
<T>Create New DuckLake…</T>
</Button>
</div>
{jdbcProfile && !lakeDriver && (
<span className={styles.formFull} role="alert">
<T>DuckLake requires the official DuckDB JDBC driver.</T>
</span>
)}
</>
) : (
<>
<label className={styles.formLabel} htmlFor="db-url">
<T>URL:</T>
</label>
<Input
id="db-url"
mono
value={jdbcUrl}
readOnly={duckDbMode !== "url"}
invalid={duckDbMode === "url" && !!jdbcUrl && !jdbcUrlValid}
placeholder={DUCKDB_JDBC_PREFIX}
onChange={(e) => {
setUrlDraft(e.target.value);
setTest(null);
}}
/>
</>
)}
{duckDbMemory && (
<>
<span />
<span className={styles.formHint}>
{s.readOnly ? (
<T>Turn off Read-only in Options to use an in-memory DuckDB database.</T>
) : (
<T>Data is discarded when the connection closes.</T>
)}
</span>
</>
)}
</>
) : sqlite ? (
<FileField
id="db-file"
label="File:"
value={s.file ?? ""}
placeholder="path/to/database.db (absolute or project-relative)"
title="SQLite Database File"
onChange={(file) => set({ file: file || null })}
/>
) : (
<>
{jdbc ? (
<>
{fromTemplate &&
templateFields.map((key) => (
<Fragment key={key}>
<label className={styles.formLabel} htmlFor={`jdbc-template-${key}`}>
{tx({ host: "Host:", port: "Port:", database: "Database:", file: "File:" }[key])}
</label>
<div className={styles.formRow}>
<Input
id={`jdbc-template-${key}`}
className={styles.fill}
value={s[key] ?? ""}
inputMode={key === "port" ? "numeric" : undefined}
onChange={(e) =>
set({ [key]: key === "port" ? parsePort(e.target.value) : e.target.value, url: template })
}
/>
{key === "file" && (
<Button
onClick={() =>
void pickFile("Database File").then((file) => file && set({ file, url: template }))
}
>
<T>Browse…</T>
</Button>
)}
</div>
</Fragment>
))}
{!fromTemplate && template && s.url !== template && (
<>
<span />
<Button
onClick={() => {
set({ url: template });
setTest(null);
setConnectionSecurityConfirmed(false);
}}
>
<T>Use URL template</T>
</Button>
</>
)}
</>
) : (
<>
<span className={styles.formLabel}>
<T>Connection type:</T>
</span>
<div className={styles.segmented} role="group" aria-label={tx("Connection type")}>
<button type="button" aria-pressed={!urlOnly} onClick={() => setUrlOnly(false)}>
<T>default</T>
</button>
<button
type="button"
aria-pressed={urlOnly}
disabled={!!externalTunnel}
onClick={() => setUrlOnly(true)}
>
<T>URL only</T>
</button>
</div>
{!urlOnly && (
<>
<label className={styles.formLabel} htmlFor="db-host">
{tx(snowflake ? "Account:" : "Host:")}
</label>
<div className={styles.formRow}>
<Input
id="db-host"
className={styles.fill}
value={s.host ?? ""}
placeholder={snowflake ? "myorg-myaccount or xy12345.eu-central-1" : undefined}
onChange={(e) => set({ host: e.target.value })}
/>
<label className={styles.formLabel} htmlFor="db-port">
<T>Port:</T>
</label>
<Input
id="db-port"
className={styles.portInput}
inputMode="numeric"
value={s.port ?? ""}
placeholder={String(vendor.port ?? "")}
onChange={(e) => set({ port: parsePort(e.target.value) })}
/>
</div>
</>
)}
</>
)}
{sshOn && (
<>
<span />
<span className={styles.formHint}>{tx`Reached from the SSH host ${sshLabel({ ssh: sshUsed })}.`}</span>
</>
)}
<label className={styles.formLabel} htmlFor="db-user">
<T>User:</T>
</label>
<Input
id="db-user"
value={s.user ?? ""}
placeholder={urlOnly ? "from the URL" : ""}
onChange={(e) => set({ user: e.target.value })}
/>
{snowflake && (
<>
<span className={styles.formLabel}>
<T>Authentication:</T>
</span>
<Select
aria-label="Snowflake authentication"
value={sf.auth ?? "keyPair"}
options={SNOWFLAKE_AUTH}
onChange={(auth) => setSnowflake({ auth })}
/>
{sf.auth === "keyPair" && (
<FileField
id="db-snowflake-key"
label="Private key:"
value={sf.privateKeyFile ?? ""}
placeholder="~/.ssh/rsa_key.p8"
title="Private Key File"
onChange={(privateKeyFile) => setSnowflake({ privateKeyFile })}
/>
)}
</>
)}
{browserSignIn && (
<>
<span />
<span className={styles.formHint}>
<T>Opens your web browser to sign in with your identity provider when connecting.</T>
</span>
</>
)}
<label className={styles.formLabel} htmlFor="db-password" hidden={browserSignIn}>
{tx(snowflake ? snowflakeSecretLabel(snowflakeAuth) : "Password:")}
</label>
<div className={styles.formRow} hidden={browserSignIn}>
<Input
id="db-password"
className={styles.fill}
type="password"
value={password ?? ""}
placeholder={
initial && password === undefined && !newJdbcPassword
? "<unchanged>"
: snowflake && sf.auth === "keyPair"
? "Only for an encrypted key"
: ""
}
onChange={(e) => setPassword(e.target.value)}
/>
<Checkbox
checked={!!s.savePassword}
onChange={(savePassword) => set({ savePassword })}
label="Save password"
/>
</div>
{!urlOnly && !jdbc && (
<>
<label className={styles.formLabel} htmlFor="db-database">
<T>Database:</T>
</label>
<Input
id="db-database"
value={s.database ?? ""}
placeholder={oracle ? "Service name, e.g. ORCLPDB1" : undefined}
onChange={(e) => set({ database: e.target.value })}
/>
{snowflake && (
<>
<label className={styles.formLabel} htmlFor="db-snowflake-schema">
<T>Schema:</T>
</label>
<Input
id="db-snowflake-schema"
value={options.defaultSchema ?? ""}
onChange={(e) => setOptions({ defaultSchema: e.target.value })}
/>
<label className={styles.formLabel} htmlFor="db-snowflake-warehouse">
<T>Warehouse:</T>
</label>
<Input
id="db-snowflake-warehouse"
value={sf.warehouse ?? ""}
placeholder="The user's default warehouse when empty"
onChange={(e) => setSnowflake({ warehouse: e.target.value })}
/>
<label className={styles.formLabel} htmlFor="db-snowflake-role">
<T>Role:</T>
</label>
<Input
id="db-snowflake-role"
value={sf.role ?? ""}
placeholder="The user's default role when empty"
onChange={(e) => setSnowflake({ role: e.target.value })}
/>
</>
)}
</>
)}
<label className={styles.formLabel} htmlFor="db-url">
<T>URL:</T>
</label>
<Input
id="db-url"
mono
value={jdbc ? jdbcUrl : urlOnly ? (s.url ?? "") : (urlDraft ?? sourceUrl(s))}
invalid={
jdbc
? !!s.url && !jdbcUrlValid
: urlOnly
? !urlValid
: urlDraft !== null && parseSourceUrl(urlDraft)?.source.driver !== s.driver
}
placeholder={jdbc ? jdbcProfile?.urlTemplate : sourceUrl(s)}
onChange={(e) => (jdbc || urlOnly ? setConnectionUrl(e.target.value, true) : typeUrl(e.target.value))}
onBlur={() => setUrlDraft(null)}
/>
<span />
<span className={styles.formHint}>
{jdbc
? tx(
"The URL and Advanced properties are saved with the data source. Enter passwords in the Password field.",
)
: txTemplate(
urlOnly
? urlValid || !s.url?.trim()
? "Connects with the URL alone: host, port, database, SSL and parameters come from it."
: `Not a ${vendor.label} URL`
: "Editing the URL fills in the settings above.",
)}
</span>
</>
)}
{(reviewJdbcSecurity || reviewBuiltinSecurity) && (
<div className={styles.formWide}>
<span className={styles.formHint}>
{jdbc
? tx(
"Review the JDBC URL, TLS settings and connection properties. If your native connection used an SSH tunnel, configure the equivalent route before connecting.",
)
: tx(
restoredNativeTarget
? "The built-in driver restores the previous target and credentials. Review them before connecting."
: "Review the built-in connection settings, credentials and TLS before connecting.",
)}
</span>
{jdbc && nativeSource?.readOnly && (
<span className={styles.formHint}>
<T>JDBC asks the driver for a read-only connection. Use a database account with read-only privileges.</T>
</span>
)}
{jdbc && nativeSource?.externalTunnel && (
<span className={styles.formHint}>
<T>Configure the route for the JDBC driver if the built-in connection uses an external TCP tunnel.</T>
</span>
)}
{jdbc && reviewSessionSettings && (
<span className={styles.formHint}>
<T>
JDBC uses auto-commit. Review your startup script, default schema, and time zone, and configure
equivalent settings supported by your driver or database.
</T>
</span>
)}
{newJdbcPassword && (
<span className={styles.formHint}>
<T>This source uses a separate credential for JDBC. Enter a password if your driver requires one.</T>
</span>
)}
{jdbc && !newJdbcPassword && nativeUrl?.password && (
<span className={styles.formHint}>
<T>If the connection used a password in the previous URL, enter it in the Password field.</T>
</span>
)}
<Checkbox
checked={connectionSecurityConfirmed}
onChange={setConnectionSecurityConfirmed}
label={
jdbc ? "I have configured JDBC connection security." : "I have configured built-in connection security."
}
/>
</div>
)}
</>
);
const optionsTab = (
<>
<Section title="Connection" />
<span />
<Checkbox
checked={lakeMode || !!s.readOnly}
disabled={lakeMode || (duckDbMemory && !s.readOnly)}
onChange={(readOnly) => set({ readOnly })}
label="Read-only"
/>
{duckDbMemory ? (
<span className={styles.formWide}>
<T>In-memory DuckDB databases require write access.</T>
</span>
) : jdbc && !duckdb ? (
<span className={styles.formWide}>
<T>For read-only access, use a database account with read-only privileges.</T>
</span>
) : null}
{!jdbc && (
<>
<span className={styles.formLabel}>
<T>Transaction control:</T>
</span>
<Select
aria-label="Transaction control"
className={styles.narrowSelect}
value={clickhouse ? "auto" : (options.transaction ?? "auto")}
options={clickhouse ? TX.filter((option) => option.value === "auto") : TX}
disabled={clickhouse}
onChange={(transaction) => setOptions({ transaction })}
/>
</>
)}
{postgres && (
<>
<label className={styles.formLabel} htmlFor="db-default-schema">
<T>Default schema:</T>
</label>
<Input
id="db-default-schema"
value={options.defaultSchema ?? ""}
placeholder="public (search_path, e.g. app, public)"
onChange={(e) => setOptions({ defaultSchema: e.target.value })}
/>
</>
)}
{oracle && (
<>
<label className={styles.formLabel} htmlFor="db-default-schema">
<T>Current schema:</T>
</label>
<Input
id="db-default-schema"
value={options.defaultSchema ?? ""}
placeholder="HR (ALTER SESSION SET CURRENT_SCHEMA)"
onChange={(e) => setOptions({ defaultSchema: e.target.value })}
/>
</>
)}
{!sqlite && !jdbc && (
<>
<label className={styles.formLabel} htmlFor="db-time-zone">
<T>Time zone:</T>
</label>
<Input
id="db-time-zone"
value={options.timeZone ?? ""}
placeholder={
postgres || clickhouse
? "Server default, e.g. UTC or Europe/Berlin"
: "UTC (+00:00), e.g. Europe/Berlin or +08:00"
}
onChange={(e) => setOptions({ timeZone: e.target.value })}
/>
</>
)}
{!sqlite && (
<SecondsOption
label="Run keep-alive query each"
value={options.keepAliveSecs}
fallback={60}
onChange={(keepAliveSecs) => setOptions({ keepAliveSecs })}
/>
)}
<SecondsOption
label="Auto-disconnect after"
value={options.autoDisconnectSecs}
fallback={300}
onChange={(autoDisconnectSecs) => setOptions({ autoDisconnectSecs })}
/>
{serverDatabases && (
<>
<span />
<Checkbox
checked={!!options.singleDatabase}
onChange={(singleDatabase) => setOptions({ singleDatabase })}
label="Single database mode (show only the configured database)"
/>
</>
)}
{!jdbc && (
<>
<label className={styles.formLabel} htmlFor="db-startup">
<T>Startup script:</T>
</label>
<textarea
id="db-startup"
className={styles.textArea}
spellCheck={false}
value={options.startupScript ?? ""}
placeholder={
postgres
? "SET statement_timeout = '30s';"
: clickhouse
? "SET max_execution_time = 30;"
: sqlite
? "PRAGMA foreign_keys = ON;"
: snowflake
? "ALTER SESSION SET QUERY_TAG = 'ide';"
: oracle
? "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';"
: "SET SESSION sql_mode = 'ANSI';"
}
onChange={(e) => setOptions({ startupScript: e.target.value })}
/>
<span />
<span className={styles.formHint}>
<T>Runs on every new connection, before any other statement.</T>
</span>
</>
)}
</>
);
const sshConfigOptions: SelectOption[] = [
{ value: "", label: "Custom (fields below)" },
...sshConfigs.map((c) => ({
value: c.id,
verbatim: true,
verbatimDetail: true,
label: c.name.trim() || configLabel(c) || c.id,
detail: configLabel(c),
})),
...(sshConfigId && !sshConfig ? [{ value: sshConfigId, verbatim: true, label: tx`${sshConfigId} (missing)` }] : []),
];
const targetHost = s.host?.trim() ?? "";
const targetPort = s.port ?? vendor.port ?? 5432;
const targetAddress = `${targetHost.includes(":") && !targetHost.startsWith("[") ? `[${targetHost}]` : targetHost}:${targetPort}`;
const sshFields = (
<>
<span className={styles.formLabel}>
<T>Configuration:</T>
</span>
<Select
aria-label="SSH configuration"
disabled={!sshOn}
value={sshConfigId}
options={sshConfigOptions}
onChange={(config) => setSsh({ config })}
/>
{sshConfigId && (
<>
<span />
<span className={styles.formHint}>
{sshConfig ? (
<>{txTemplate(`Connects to ${configLabel(sshConfig) || sshConfig.name}.`)} </>
) : (
<span className={styles.bad}>{txTemplate(`SSH configuration ${sshConfigId} no longer exists.`)} </span>
)}
<button
type="button"
className={styles.linkButton}
onClick={() => void executeAction("file.settings", "tools.sshConfigurations")}
>
<T>Edit SSH Configurations…</T>
</button>
</span>
</>
)}
{!sshConfigId && (
<>
<label className={styles.formLabel} htmlFor="db-ssh-host">
<T>Host:</T>
</label>
<div className={styles.formRow}>
<Input
id="db-ssh-host"
className={styles.fill}
disabled={!sshOn}
value={ssh.host ?? ""}
placeholder={ssh.auth === "openSsh" ? "host or ~/.ssh/config alias" : "bastion.example.com"}
onChange={(e) => setSsh({ host: e.target.value })}
/>
<label className={styles.formLabel} htmlFor="db-ssh-port">
<T>Port:</T>
</label>
<Input
id="db-ssh-port"
className={styles.portInput}
inputMode="numeric"
disabled={!sshOn}
value={ssh.port ?? ""}
placeholder="22"
onChange={(e) => setSsh({ port: parsePort(e.target.value) })}
/>
</div>
<label className={styles.formLabel} htmlFor="db-ssh-user">
<T>User:</T>
</label>
<Input
id="db-ssh-user"
disabled={!sshOn}
value={ssh.user ?? ""}
placeholder={ssh.auth === "openSsh" ? "from ~/.ssh/config" : ""}
onChange={(e) => setSsh({ user: e.target.value })}
/>
<span className={styles.formLabel}>
<T>Authentication:</T>
</span>
<Select
aria-label="SSH authentication type"
disabled={!sshOn}
value={ssh.auth ?? "openSsh"}
options={SSH_AUTH}
onChange={(auth) => setSsh({ auth })}
/>
{ssh.auth === "keyPair" && (
<FileField
id="db-ssh-key"
label="Private key:"
value={ssh.keyFile ?? ""}
placeholder="~/.ssh/id_ed25519"
disabled={!sshOn}
title="Private Key File"
onChange={(keyFile) => setSsh({ keyFile })}
/>
)}
</>
)}
{sshUsed.auth === "openSsh" && !sshConfigId && (
<>
<span />
<span className={styles.formHint}>
<T>
{"Uses HostName, User, Port, IdentityFile and ProxyJump from ~/.ssh/config, and keys of the SSH agent."}
</T>
</span>
</>
)}
{sshUsed.auth !== "openSsh" && (
<>
<label className={styles.formLabel} htmlFor="db-ssh-secret">
{tx(sshUsed.auth === "password" ? "Password:" : "Passphrase:")}
</label>
<div className={styles.formRow}>
<Input
id="db-ssh-secret"
className={styles.fill}
type="password"
disabled={!sshOn}
value={sshSecret ?? ""}
placeholder={initial && sshSecret === undefined ? "<unchanged>" : ""}
onChange={(e) => setSshSecret(e.target.value)}
/>
{!sshConfigId && (
<Checkbox
checked={!!ssh.saveSecret}
disabled={!sshOn}
onChange={(saveSecret) => setSsh({ saveSecret })}
label="Save password"
/>
)}
</div>
</>
)}
<span />
<div className={styles.formRow}>
<Button
size="small"
onClick={() => void runTest(true)}
disabled={busy || !sshOn || !(sshConfigId || ssh.host?.trim())}
>
<T>Test SSH Tunnel</T>
</Button>
<span className={styles.formHint}>
<T>Logs in to the SSH host and checks that it reaches the database host and port.</T>
</span>
</div>
</>
);
const sshTab = (
<>
{externalAvailable || externalTunnel ? (
<>
<span className={styles.formLabel}>
<T>Connection method:</T>
</span>
<Select
aria-label="Connection method"
className={styles.narrowSelect}
value={connectionMethod}
options={
externalAvailable
? CONNECTION_METHODS
: CONNECTION_METHODS.map((method) => ({ ...method, disabled: method.value === "external" }))
}
onChange={setConnectionMethod}
/>
{sshOn && sshFields}
{externalAvailable && externalTunnel && (
<>
<FileField
id="db-tunnel-executable"
label="Executable:"
title="External Tunnel Executable"
value={externalTunnel.executable}
onChange={(executable) => setExternalTunnel({ executable })}
/>
<span id="db-tunnel-arguments" className={`${styles.formLabel} ${styles.tunnelArgumentsLabel}`}>
<T>Arguments:</T>
</span>
<div className={styles.tunnelArguments} role="group" aria-labelledby="db-tunnel-arguments">
{externalTunnel.arguments.map((argument, i) => (
// oxlint-disable-next-line react/no-array-index-key -- the position identifies an argument
<div key={i} className={styles.formRow}>
<Input
className={`${styles.fill} ${styles.paramInput}`}
aria-label={tx`Argument ${i + 1}`}
value={argument}
spellCheck={false}
onChange={(e) =>
setExternalTunnel({
arguments: externalTunnel.arguments.map((value, j) => (j === i ? e.target.value : value)),
})
}
/>
<IconButton
icon="close"
size="small"
aria-label={tx`Remove argument ${i + 1}`}
title="Remove"
onClick={() =>
setExternalTunnel({
arguments: externalTunnel.arguments.filter((_, j) => j !== i),
})
}
/>
</div>
))}
<Button
icon="plus"
size="small"
onClick={() => setExternalTunnel({ arguments: [...externalTunnel.arguments, ""] })}
>
<T>Add Argument</T>
</Button>
<span className={styles.formHint}>
<T>
{
"Use one field per argument. Include {localPort} for the loopback port. {host} and {port} use the PostgreSQL destination."
}
</T>
</span>
<span className={styles.formHint}>
<T>The executable must listen on 127.0.0.1 and run in the foreground.</T>
</span>
</div>
</>
)}
</>
) : (
<>
<span />
<Checkbox checked={!!ssh.enabled} onChange={(enabled) => setSsh({ enabled })} label="Use SSH tunnel" />
{sshFields}
</>
)}
<Section title="SSL" />
<span className={styles.formLabel}>
<T>Mode:</T>
</span>
<Select
aria-label="SSL mode"
className={styles.narrowSelect}
value={s.ssl ?? "prefer"}
options={clickhouse ? SSL.filter((mode) => mode.value !== "prefer") : SSL}
onChange={(ssl) => {
const secure = ssl !== "disable" && ssl !== "prefer";
const oldSecure = s.ssl !== "disable" && s.ssl !== "prefer";
set({
ssl,
...(clickhouse && (s.port == null || s.port === (oldSecure ? 8443 : 8123))
? { port: secure ? 8443 : 8123 }
: {}),
});
}}
/>
<FileField
id="db-ssl-ca"
label="CA file:"
value={s.sslCaFile ?? ""}
placeholder={s.ssl === "verifyCa" || s.ssl === "verifyFull" ? "System roots when empty" : ""}
disabled={!sslOn}
title="CA Certificate File"
onChange={(sslCaFile) => set({ sslCaFile })}
/>
<FileField
id="db-ssl-cert"
label="Client certificate file:"
value={s.sslCertFile ?? ""}
disabled={!sslOn}
title="Client Certificate File"
onChange={(sslCertFile) => set({ sslCertFile })}
/>
<FileField
id="db-ssl-key"
label="Client key file:"
value={s.sslKeyFile ?? ""}
disabled={!sslOn}
title="Client Key File"
onChange={(sslKeyFile) => set({ sslKeyFile })}
/>
{valid && externalTunnel && (
<div className={styles.tunnelReview} role="group" aria-labelledby="db-tunnel-review" ref={reviewPanel}>
<div className={styles.formSection} id="db-tunnel-review">
<T>Review External Tunnel</T>
</div>
<p className={styles.formHint}>
<T>The executable runs on this computer with your user permissions when connecting.</T>
</p>
<dl className={styles.tunnelPreview}>
<dt>
<T>Executable:</T>
</dt>
<dd>
<code>{visibleArgument(externalTunnel.executable)}</code>
</dd>
<dt>
<T>Arguments:</T>
</dt>
<dd>
<ol>
{externalTunnel.arguments.map((argument, i) => (
// oxlint-disable-next-line react/no-array-index-key -- a literal argument can repeat at different positions
<li key={i}>
<code>{visibleArgument(argument)}</code>
</li>
))}
</ol>
</dd>
<dt>
<T>Destination:</T>
</dt>
<dd>
<code>{visibleArgument(targetAddress)}</code>
</dd>
{!!s.database && (
<>
<dt>
<T>Database:</T>
</dt>
<dd>
<code>{visibleArgument(s.database)}</code>
</dd>
</>
)}
</dl>
<Checkbox
checked={reviewedTunnel}
disabled={busy}
label="Allow this command for the destination shown"
onChange={(checked) => {
const key = externalReviewKey(s);
setDraft((draft) => ({
...draft,
reviewedTunnel: checked && draft.password === password && externalReviewKey(draft.source) === key,
}));
setTest(null);
}}
/>
</div>
)}
</>
);
const knownDbs = loadedDbs?.status === "ok" ? loadedDbs.value.names : [];
const knownSchemas = loadedSchemas?.status === "ok" ? loadedSchemas.value : [];
const schemasTab = (
<>
<div className={styles.formWide}>
{serverDatabases && (
<NamePicker
allLabel={tx("All databases")}
addLabel={tx("Add databases pattern")}
all={intro.allDatabases !== false}
chosen={intro.databases ?? []}
known={knownDbs}
onAll={(allDatabases) => setIntro({ allDatabases })}
onChosen={(databases) => setIntro({ databases })}
/>
)}
<NamePicker
allLabel={s.driver === "mysql" ? tx("All schemas (databases)") : tx("All schemas")}
addLabel={s.driver === "mysql" ? tx("Add schemas (databases) pattern") : tx("Add schemas pattern")}
all={intro.allSchemas !== false}
chosen={intro.schemas ?? []}
known={knownSchemas}
onAll={(allSchemas) => setIntro({ allSchemas })}
onChosen={(schemas) => setIntro({ schemas })}
/>
<div className={styles.formRow}>
<label className={styles.formLabel} htmlFor="db-object-filter">
<T>Object filter:</T>
</label>
<Input
id="db-object-filter"
mono
value={intro.objectFilter ?? ""}
placeholder="e.g. order*, customers (tables, views, routines, sequences)"
onChange={(e) => setIntro({ objectFilter: e.target.value })}
/>
</div>
<Checkbox
checked={!!intro.showSystem}
onChange={(showSystem) => setIntro({ showSystem })}
label="Show internal system schemas"
/>
{postgres && (
<Checkbox
checked={!!intro.showTemplates}
onChange={(showTemplates) => setIntro({ showTemplates })}
label="Show template databases"
/>
)}
<div className={styles.formRow}>
<Button disabled={!initial?.id || busy} onClick={() => void loadNames()}>
<T>Load Names from Server</T>
</Button>
<span className={styles.formHint}>
{initial?.id
? tx(
"The Database tree shows only the checked names and patterns (* and ?); the connected database always shows.",
)
: tx("Save the data source to pick from the server's names; patterns (* and ?) work any time.")}
</span>
</div>
</div>
</>
);
const advancedTab = (
<div className={styles.formWide}>
<div className={styles.propertyTable} role="table" aria-label={tx("Driver properties")}>
<div role="row" className={styles.propertyHead}>
<span role="columnheader">
<T>Name</T>
</span>
<span role="columnheader">
<T>Value</T>
</span>
<span />
</div>
{properties.map((p, i) => (
// oxlint-disable-next-line react/no-array-index-key -- property fields are edited in place; position is the identity
<div role="row" key={i} className={styles.propertyRow}>
<Input
aria-label="Property name"
list="db-property-names"
value={p.name ?? ""}
placeholder="<user defined>"
onChange={(e) => setProperty(i, { name: e.target.value })}
/>
<Input
aria-label="Property value"
value={p.value ?? ""}
placeholder="<value>"
onChange={(e) => setProperty(i, { value: e.target.value })}
/>
<IconButton
icon="close"
size="small"
aria-label="Remove property"
title="Remove"
onClick={() => set({ properties: properties.filter((_, j) => j !== i) })}
/>
</div>
))}
</div>
<datalist id="db-property-names">
{props.names.map((n) => (
<option key={n} value={n} />
))}
</datalist>
<div className={styles.formRow}>
<Button icon="plus" onClick={() => set({ properties: [...properties, { name: "", value: "" }] })}>
<T>Add Property</T>
</Button>
</div>
<span className={styles.formHint}>
<T>{props.hint}</T>
</span>
</div>
);
const body: Record<Tab, ReactNode> = {
general,
options: optionsTab,
ssh: sshTab,
schemas: schemasTab,
advanced: advancedTab,
};
const shownTest = externalTunnel && !reviewedTunnel ? null : test;
return (
<Dialog
open
onClose={() => {
cancelExternalTest();
closeDialog();
}}
title={initial ? tx`Data Source: ${initial.name}` : "New Data Source"}
width={760}
footerStart={
<Button onClick={() => void runTest()} disabled={busy || !valid}>
<T>Test Connection</T>
</Button>
}
buttons={[
{ label: "Cancel", role: "cancel" },
{ label: "OK", role: "default", disabled: busy || !valid, onClick: () => void save() },
]}
>
<div className={styles.sourceLayout}>
<div className={styles.form}>
<label className={styles.formLabel} htmlFor="db-name">
<T>Name:</T>
</label>
<div className={styles.formRow}>
<Input
id="db-name"
className={styles.fill}
value={s.name ?? ""}
placeholder={name}
onChange={(e) => set({ name: e.target.value })}
/>
<span className={styles.formLabel}>
<T>Color:</T>
</span>
<Select
aria-label="Color"
className={styles.narrowSelect}
value={s.color ?? "none"}
options={COLORS}
onChange={(c) => set({ color: c === "none" ? null : c })}
renderValue={(o) => (
<span className={styles.colorValue}>
{o && o.value !== "none" && (
<span className={styles.colorSwatch} style={{ background: colorVar(o.value as DbColor) }} />
)}
<T>{o?.label ?? "No color"}</T>
</span>
)}
/>
{/* IDEA's Make Global: stored with the IDE and listed in every project. */}
<span title={tx("Available in all projects")}>
<Checkbox
checked={!!s.global}
disabled={!!s.pluginOwner}
onChange={(global) => set({ global })}
label="Global"
/>
</span>
</div>
<span />
{showComment ? (
<textarea
className={styles.textArea}
aria-label={tx("Comment")}
autoFocus={!s.comment}
rows={2}
value={s.comment ?? ""}
placeholder={tx("Comment")}
onChange={(e) => set({ comment: e.target.value })}
/>
) : (
<button type="button" className={styles.linkButton} onClick={() => setShowComment(true)}>
<T>Add comment</T>
</button>
)}
</div>
<Tabs
variant="compact"
className={styles.dialogTabs}
activeId={current}
onSelect={(id) => setTab(id as Tab)}
aria-label="Data source settings"
panelIdPrefix="db-source"
items={tabs}
/>
<div className={`${styles.form} ${styles.sourcePanel}`} role="tabpanel" id={`db-source-panel-${current}`}>
{body[current]}
</div>
<div
className={`${styles.testResult} ${shownTest?.ok ? styles.ok : shownTest || externalError ? styles.bad : ""}`}
role="status"
>
{busy && !shownTest
? busyKind === "external"
? tx("Testing external TCP tunnel and PostgreSQL connection…")
: busyKind === "creatingLake"
? tx("Creating DuckLake…")
: tx("Connecting…")
: (externalError ?? shownTest?.text)}
{shownTest?.trust && (
<>
{" "}
<Button size="small" onClick={() => void trustAndRetry()} disabled={busy}>
<T>Trust Host and Retry</T>
</Button>
</>
)}
</div>
</div>
{showJdbcProfiles && jdbcProfiles.drivers && (
<JdbcDriverProfiles
drivers={jdbcProfiles.drivers}
selected={jdbc ? (jdbcProfile?.id ?? "") : ""}
sourceVendor={genericJdbc ? undefined : vendor.id}
onClose={() => setShowJdbcProfiles(false)}
onSave={(driver) => {
jdbcProfiles.setDrivers((drivers) => [...(drivers ?? []).filter((d) => d.id !== driver.id), driver]);
if (jdbc && (genericJdbc || driver.vendor === vendor.id) && (!s.jdbcDriver || s.jdbcDriver === driver.id))
chooseProfile(driver);
}}
onUse={(driver) => {
chooseProfile(driver);
setShowJdbcProfiles(false);
}}
onRemove={(id) => jdbcProfiles.setDrivers((drivers) => (drivers ?? []).filter((d) => d.id !== id))}
/>
)}
</Dialog>
);
}