Skip to content

Commit 310576a

Browse files
authored
#17 Added turso urls support (#27)
* #17 Added turso urls support * #17 Remove reassign
1 parent a2c75c1 commit 310576a

File tree

4 files changed

+77
-13
lines changed

4 files changed

+77
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DBeaver - Universal Database Manager
3-
* Copyright (C) 2010-2024 DBeaver Corp and others
3+
* Copyright (C) 2010-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@
2020

2121
public class LibSqlConstants {
2222

23-
public static final Pattern CONNECTION_URL_EXAMPLE = Pattern.compile("jdbc:dbeaver:libsql:<server-url>");
24-
public static final Pattern CONNECTION_URL_PATTERN = Pattern.compile("jdbc:dbeaver:libsql:(.+)");
23+
public static final String CONNECTION_URL_EXAMPLES = "jdbc:dbeaver:libsql:<server-url>, libsql://";
24+
public static final Pattern CONNECTION_URL_PATTERN = Pattern.compile("(jdbc:dbeaver:libsql:|libsql://)(.+)");
2525

2626
public static final int DRIVER_VERSION_MAJOR = 1;
2727
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: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DBeaver - Universal Database Manager
3-
* Copyright (C) 2010-2024 DBeaver Corp and others
3+
* Copyright (C) 2010-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -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,13 +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 format: " + LibSqlConstants.CONNECTION_URL_EXAMPLE);
50-
}
51-
String targetUrl = matcher.group(1);
44+
String targetUrl = LibSqlUtils.validateAndFormatUrl(url);
5245

5346
Map<String, Object> props = new LinkedHashMap<>();
5447
for (Enumeration<?> pne = info.propertyNames(); pne.hasMoreElements(); ) {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DBeaver - Universal Database Manager
3-
* Copyright (C) 2010-2024 DBeaver Corp and others
3+
* Copyright (C) 2010-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
1616
*/
1717
package com.dbeaver.jdbc.driver.libsql;
1818

19+
import org.jkiss.code.NotNull;
20+
1921
import java.sql.Connection;
2022
import java.sql.ResultSet;
2123
import java.sql.SQLException;
2224
import java.sql.Statement;
25+
import java.util.regex.Matcher;
2326

2427
public class LibSqlUtils {
2528

@@ -66,4 +69,24 @@ public static ResultSet executeQuery(Connection connection, String query) throws
6669
return stat.executeQuery(query);
6770
}
6871
}
72+
73+
@NotNull
74+
public static String validateAndFormatUrl(@NotNull String url) throws LibSqlException {
75+
Matcher matcher = LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url);
76+
if (!matcher.matches()) {
77+
throw new LibSqlException(
78+
"Invalid connection URL: " + url +
79+
".\nExpected URL formats: " + LibSqlConstants.CONNECTION_URL_EXAMPLES
80+
);
81+
}
82+
83+
String formattedUrl = url;
84+
if (formattedUrl.startsWith("jdbc:dbeaver:libsql:")) {
85+
formattedUrl = formattedUrl.replaceFirst("jdbc:dbeaver:libsql:", "");
86+
}
87+
if (formattedUrl.startsWith("libsql://")) {
88+
formattedUrl = formattedUrl.replaceFirst("libsql://", "https://");
89+
}
90+
return formattedUrl;
91+
}
6992
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
Assert.assertThrows(
29+
LibSqlException.class,
30+
() -> LibSqlUtils.validateAndFormatUrl("localhost")
31+
);
32+
Assert.assertThrows(
33+
LibSqlException.class,
34+
() -> LibSqlUtils.validateAndFormatUrl("http://localhost")
35+
);
36+
37+
assertUrlFormat("jdbc:dbeaver:libsql:http://localhost", "http://localhost");
38+
assertUrlFormat("jdbc:dbeaver:libsql:https://localhost", "https://localhost");
39+
assertUrlFormat("jdbc:dbeaver:libsql:http://localhost:8080", "http://localhost:8080");
40+
assertUrlFormat("jdbc:dbeaver:libsql:libsql://localhost", "https://localhost");
41+
assertUrlFormat("libsql://turso.url.my-hostname-1", "https://turso.url.my-hostname-1");
42+
assertUrlFormat("libsql://turso.url.my-hostname-1:8080", "https://turso.url.my-hostname-1:8080");
43+
}
44+
45+
private void assertUrlFormat(String input, String expected) throws LibSqlException {
46+
Assert.assertEquals(expected, LibSqlUtils.validateAndFormatUrl(input));
47+
}
48+
}

0 commit comments

Comments
 (0)