Skip to content
Merged
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
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,8 @@

public class LibSqlConstants {

public static final Pattern CONNECTION_URL_EXAMPLE = Pattern.compile("jdbc:dbeaver:libsql:<server-url>");
public static final Pattern CONNECTION_URL_PATTERN = Pattern.compile("jdbc:dbeaver:libsql:(.+)");
public static final String CONNECTION_URL_EXAMPLES = "jdbc:dbeaver:libsql:<server-url>, libsql://";
public static final Pattern CONNECTION_URL_PATTERN = 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
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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,13 +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 format: " + LibSqlConstants.CONNECTION_URL_EXAMPLE);
}
String targetUrl = matcher.group(1);
String targetUrl = LibSqlUtils.validateAndFormatUrl(url);

Map<String, Object> props = new LinkedHashMap<>();
for (Enumeration<?> pne = info.propertyNames(); pne.hasMoreElements(); ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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,24 @@ 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;
if (formattedUrl.startsWith("jdbc:dbeaver:libsql:")) {
formattedUrl = formattedUrl.replaceFirst("jdbc:dbeaver:libsql:", "");
}
if (formattedUrl.startsWith("libsql://")) {
formattedUrl = formattedUrl.replaceFirst("libsql://", "https://");
}
return formattedUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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")
);
Assert.assertThrows(
LibSqlException.class,
() -> LibSqlUtils.validateAndFormatUrl("http://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