Skip to content

Commit ed48f30

Browse files
committed
1 parent 22094a9 commit ed48f30

4 files changed

Lines changed: 75 additions & 14 deletions

File tree

com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class LibSqlConstants {
2222

2323
public static final String CONNECTION_URL_EXAMPLES = "jdbc:dbeaver:libsql:<hostname>, libsql://<hostname>";
2424
public static final Pattern CONNECTION_URL_PATTERN =
25-
Pattern.compile("(jdbc:dbeaver:libsql:)?(libsql://)?[a-z0-9/:.-]+");
25+
Pattern.compile("(jdbc:dbeaver:libsql:|libsql://)[a-z0-9/:.-]+");
2626

2727
public static final int DRIVER_VERSION_MAJOR = 1;
2828
public static final int DRIVER_VERSION_MINOR = 0;

com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDriver.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Properties;
2525
import java.util.logging.Level;
2626
import java.util.logging.Logger;
27-
import java.util.regex.Matcher;
2827

2928
public class LibSqlDriver implements Driver {
3029

@@ -42,18 +41,7 @@ public class LibSqlDriver implements Driver {
4241

4342
@Override
4443
public Connection connect(String url, Properties info) throws SQLException {
45-
Matcher matcher = LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url);
46-
if (!matcher.matches()) {
47-
throw new LibSqlException(
48-
"Invalid connection URL: " + url +
49-
".\nExpected URL formats: " + LibSqlConstants.CONNECTION_URL_EXAMPLES);
50-
}
51-
52-
String targetUrl = url.replaceFirst("jdbc:dbeaver:libsql:", "");
53-
if (targetUrl.startsWith("libsql://")) {
54-
targetUrl = targetUrl.replaceFirst("libsql://", "https://");
55-
}
56-
44+
String targetUrl = LibSqlUtils.validateAndFormatUrl(url);
5745
Map<String, Object> props = new LinkedHashMap<>();
5846
for (Enumeration<?> pne = info.propertyNames(); pne.hasMoreElements(); ) {
5947
String propName = (String) pne.nextElement();

com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
2222
import java.sql.Statement;
23+
import java.util.regex.Matcher;
2324

2425
public class LibSqlUtils {
2526

@@ -66,4 +67,20 @@ public static ResultSet executeQuery(Connection connection, String query) throws
6667
return stat.executeQuery(query);
6768
}
6869
}
70+
71+
public static String validateAndFormatUrl(String url) throws LibSqlException {
72+
Matcher matcher = LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url);
73+
if (!matcher.matches()) {
74+
throw new LibSqlException(
75+
"Invalid connection URL: " + url +
76+
".\nExpected URL formats: " + LibSqlConstants.CONNECTION_URL_EXAMPLES
77+
);
78+
}
79+
80+
String formattedUrl = url.replaceFirst("jdbc:dbeaver:libsql:", "");
81+
if (formattedUrl.startsWith("libsql://")) {
82+
formattedUrl = formattedUrl.replaceFirst("libsql://", "https://");
83+
}
84+
return formattedUrl;
85+
}
6986
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* DBeaver - Universal Database Manager
3+
* Copyright (C) 2010-2025 DBeaver Corp
4+
*
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of DBeaver Corp and its suppliers, if any.
9+
* The intellectual and technical concepts contained
10+
* herein are proprietary to DBeaver Corp and its suppliers
11+
* and may be covered by U.S. and Foreign Patents,
12+
* patents in process, and are protected by trade secret or copyright law.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from DBeaver Corp.
16+
*/
17+
package com.dbeaver.jdbc.upd.driver.test;
18+
19+
import com.dbeaver.jdbc.driver.libsql.LibSqlException;
20+
import com.dbeaver.jdbc.driver.libsql.LibSqlUtils;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
public class LibSqlUtilsTest {
25+
26+
@Test
27+
public void testFormatUrl() throws LibSqlException {
28+
assertFormatError("localhost");
29+
assertFormatError("libsql://9M7NAcHfxSZyIm#*yxLY");
30+
31+
assertUrlFormat("jdbc:dbeaver:libsql:http://localhost", "http://localhost");
32+
assertUrlFormat("jdbc:dbeaver:libsql:https://localhost", "https://localhost");
33+
assertUrlFormat("jdbc:dbeaver:libsql:http://localhost:8080", "http://localhost:8080");
34+
assertUrlFormat("jdbc:dbeaver:libsql:libsql://localhost", "https://localhost");
35+
assertUrlFormat("libsql://turso.url.my-hostname-1", "https://turso.url.my-hostname-1");
36+
assertUrlFormat("libsql://turso.url.my-hostname-1:8080", "https://turso.url.my-hostname-1:8080");
37+
}
38+
39+
private void assertUrlFormat(String input, String expected) throws LibSqlException {
40+
Assert.assertEquals(expected, LibSqlUtils.validateAndFormatUrl(input));
41+
}
42+
43+
private void assertFormatError(String input) {
44+
String expectedMessage = "Invalid connection URL: " + input +
45+
".\nExpected URL formats: jdbc:dbeaver:libsql:<hostname>, libsql://<hostname>";
46+
try {
47+
LibSqlUtils.validateAndFormatUrl(input);
48+
Assert.fail("Expected exception: " + expectedMessage);
49+
} catch (LibSqlException e) {
50+
Assert.assertEquals(
51+
expectedMessage,
52+
e.getMessage()
53+
);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)