Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
public class LibSqlConstants {

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

public static final int DRIVER_VERSION_MAJOR = 1;
public static final int DRIVER_VERSION_MINOR = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;

public class LibSqlDriver implements Driver {

Expand All @@ -42,15 +41,7 @@ public class LibSqlDriver implements Driver {

@Override
public Connection connect(String url, Properties info) throws SQLException {
Matcher matcher = LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url);
if (!matcher.matches()) {
throw new LibSqlException(
"Invalid connection URL: " + url +
".\nExpected URL formats: " + LibSqlConstants.CONNECTION_URL_EXAMPLES);
}
String targetUrl = matcher.group(0)
.replaceAll(LibSqlConstants.CONNECTION_PROTOCOLS_REGEXP, "https://");

String targetUrl = LibSqlUtils.validateAndFormatUrl(url);
Map<String, Object> props = new LinkedHashMap<>();
for (Enumeration<?> pne = info.propertyNames(); pne.hasMoreElements(); ) {
String propName = (String) pne.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
*/
package com.dbeaver.jdbc.driver.libsql;

import org.jkiss.code.NotNull;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;

public class LibSqlUtils {

Expand Down Expand Up @@ -66,4 +69,21 @@ public static ResultSet executeQuery(Connection connection, String query) throws
return stat.executeQuery(query);
}
}

@NotNull
public static String validateAndFormatUrl(@NotNull String url) throws LibSqlException {
Matcher matcher = LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url);
if (!matcher.matches()) {
throw new LibSqlException(
"Invalid connection URL: " + url +
".\nExpected URL formats: " + LibSqlConstants.CONNECTION_URL_EXAMPLES
);
}

String formattedUrl = url.replaceFirst("jdbc:dbeaver:libsql:", "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will replace just the first occurrence of jdbc:dbeaver:libsql:. Did you want to trim it if it's in the beginning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please either add a regex anchor or put a startsWith check.

if (formattedUrl.startsWith("libsql://")) {
formattedUrl = formattedUrl.replaceFirst("libsql://", "https://");
}
return formattedUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2025 DBeaver Corp
*
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of DBeaver Corp and its suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to DBeaver Corp and its suppliers
* and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from DBeaver Corp.
*/
package com.dbeaver.jdbc.upd.driver.test;

import com.dbeaver.jdbc.driver.libsql.LibSqlException;
import com.dbeaver.jdbc.driver.libsql.LibSqlUtils;
import org.junit.Assert;
import org.junit.Test;

public class LibSqlUtilsTest {

@Test
public void testFormatUrl() throws LibSqlException {
Assert.assertThrows(
LibSqlException.class,
() -> LibSqlUtils.validateAndFormatUrl("localhost")
);

assertUrlFormat("jdbc:dbeaver:libsql:http://localhost", "http://localhost");
assertUrlFormat("jdbc:dbeaver:libsql:https://localhost", "https://localhost");
assertUrlFormat("jdbc:dbeaver:libsql:http://localhost:8080", "http://localhost:8080");
assertUrlFormat("jdbc:dbeaver:libsql:libsql://localhost", "https://localhost");
assertUrlFormat("libsql://turso.url.my-hostname-1", "https://turso.url.my-hostname-1");
assertUrlFormat("libsql://turso.url.my-hostname-1:8080", "https://turso.url.my-hostname-1:8080");
}

private void assertUrlFormat(String input, String expected) throws LibSqlException {
Assert.assertEquals(expected, LibSqlUtils.validateAndFormatUrl(input));
}
}
Loading