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。

fix(connection): parse jdbc connection urls

t8y2b523c397164d
diff --git a/src/lib/connectionUrl.ts b/src/lib/connectionUrl.ts
index 1b1eb2c838..2dabdadd47 100644
--- a/src/lib/connectionUrl.ts
+++ b/src/lib/connectionUrl.ts
@@ -69,11 +69,50 @@ function profileForScheme(scheme: string, preferredProfile?: string): Connection
return SCHEME_PROFILES[scheme];
}
+function parseJdbcSqlServerUrl(source: string): ParsedConnectionUrl | null {
+ const match = source.match(/^jdbc:sqlserver:\/\/([^;:/]+)(?::(\d+))?(?:;(.*))?$/i);
+ if (!match) return null;
+
+ const profile = SCHEME_PROFILES.sqlserver;
+ const props = new Map<string, string>();
+ const urlParams: string[] = [];
+ for (const part of (match[3] || "").split(";")) {
+ if (!part) continue;
+ const [rawKey, ...rest] = part.split("=");
+ const key = rawKey.trim();
+ const value = rest.join("=");
+ const normalizedKey = key.toLowerCase();
+ if (normalizedKey === "databasename" || normalizedKey === "database" || normalizedKey === "user") {
+ props.set(normalizedKey, value);
+ } else if (normalizedKey === "password") {
+ props.set(normalizedKey, value);
+ } else {
+ urlParams.push(part);
+ }
+ }
+
+ return {
+ dbType: profile.type,
+ driverProfile: profile.profile,
+ driverLabel: profile.label,
+ host: match[1],
+ port: match[2] ? Number(match[2]) : profile.defaultPort,
+ username: decodeUrlPart(props.get("user") || ""),
+ password: decodeUrlPart(props.get("password") || ""),
+ database: decodeUrlPart(props.get("databasename") || props.get("database") || "") || undefined,
+ urlParams: urlParams.join(";"),
+ ssl: false,
+ };
+}
+
export function parseConnectionUrl(value: string, preferredProfile?: string): ParsedConnectionUrl {
- const source = value.trim();
- if (!source) {
+ const input = value.trim();
+ if (!input) {
throw new Error("Connection URL is empty");
}
+ const jdbcSqlServer = parseJdbcSqlServerUrl(input);
+ if (jdbcSqlServer) return jdbcSqlServer;
+ const source = input.replace(/^jdbc:/i, "");
let parsed: URL;
try {
diff --git a/tests/connectionUrl.test.ts b/tests/connectionUrl.test.ts
index f4e1981b45..d4cf13f2b9 100644
--- a/tests/connectionUrl.test.ts
+++ b/tests/connectionUrl.test.ts
@@ -30,6 +30,43 @@ test("parses mysql URLs with encoded credentials", () => {
assert.equal(parsed.urlParams, "charset=utf8mb4");
});
+test("parses JDBC URLs by using the inner database URL", () => {
+ const postgres = parseConnectionUrl("jdbc:postgresql://alice:[email protected]:5433/app?sslmode=require");
+ assert.equal(postgres.dbType, "postgres");
+ assert.equal(postgres.driverProfile, "postgres");
+ assert.equal(postgres.host, "db.example.com");
+ assert.equal(postgres.port, 5433);
+ assert.equal(postgres.username, "alice");
+ assert.equal(postgres.password, "secret");
+ assert.equal(postgres.database, "app");
+ assert.equal(postgres.urlParams, "sslmode=require");
+
+ const mysql = parseConnectionUrl("jdbc:mysql://root:p%[email protected]:3307/shop?charset=utf8mb4");
+ assert.equal(mysql.dbType, "mysql");
+ assert.equal(mysql.driverProfile, "mysql");
+ assert.equal(mysql.host, "127.0.0.1");
+ assert.equal(mysql.port, 3307);
+ assert.equal(mysql.username, "root");
+ assert.equal(mysql.password, "p@ss");
+ assert.equal(mysql.database, "shop");
+ assert.equal(mysql.urlParams, "charset=utf8mb4");
+});
+
+test("parses SQL Server JDBC URLs with semicolon properties", () => {
+ const parsed = parseConnectionUrl(
+ "jdbc:sqlserver://sql.example.com:1434;databaseName=erp;user=sa;password=s%40cret;encrypt=true",
+ );
+
+ assert.equal(parsed.dbType, "sqlserver");
+ assert.equal(parsed.driverProfile, "sqlserver");
+ assert.equal(parsed.host, "sql.example.com");
+ assert.equal(parsed.port, 1434);
+ assert.equal(parsed.username, "sa");
+ assert.equal(parsed.password, "s@cret");
+ assert.equal(parsed.database, "erp");
+ assert.equal(parsed.urlParams, "encrypt=true");
+});
+
test("keeps MongoDB URLs as connection strings", () => {
const source = "mongodb+srv://reader:[email protected]/app?retryWrites=true";
const parsed = parseConnectionUrl(source);