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
10 changes: 10 additions & 0 deletions plugin/trino-mariadb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>trino-plugin-toolkit</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand All @@ -43,6 +48,11 @@
<artifactId>javax.inject</artifactId>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Properties;

import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.airlift.configuration.ConfigBinder.configBinder;

public class MariaDbClientModule
implements Module
Expand All @@ -40,6 +41,7 @@ public class MariaDbClientModule
public void configure(Binder binder)
{
binder.bind(JdbcClient.class).annotatedWith(ForBaseJdbc.class).to(MariaDbClient.class).in(Scopes.SINGLETON);
configBinder(binder).bindConfig(MariaDbJdbcConfig.class);
binder.install(new DecimalModule());
newSetBinder(binder, ConnectorTableFunction.class).addBinding().toProvider(Query.class).in(Scopes.SINGLETON);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.mariadb;

import io.trino.plugin.jdbc.BaseJdbcConfig;
import org.mariadb.jdbc.Configuration;
import org.mariadb.jdbc.Driver;

import javax.validation.constraints.AssertTrue;

import java.sql.SQLException;

public class MariaDbJdbcConfig
extends BaseJdbcConfig
{
@AssertTrue(message = "Invalid JDBC URL for MariaDB connector")
public boolean isUrlValid()
{
Driver driver = new Driver();
return driver.acceptsURL(getConnectionUrl());
Comment thread
ebyhr marked this conversation as resolved.
Outdated
}

@AssertTrue(message = "Database (catalog) must not be specified in JDBC URL for MariaDB connector")
public boolean isUrlWithoutDatabase()
{
try {
Configuration conf = Configuration.parse(getConnectionUrl());
if (conf == null) {
return false;
}
return conf.database() == null;
}
catch (SQLException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.mariadb;

import org.testng.annotations.Test;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class TestMariaDbJdbcConfig
Comment thread
ebyhr marked this conversation as resolved.
Outdated
{
@Test
public void testIsUrlValid()
{
assertTrue(isUrlValid("jdbc:mariadb://example.net:3306"));
assertTrue(isUrlValid("jdbc:mariadb://example.net:3306/"));
assertFalse(isUrlValid("jdbc:notmariadb://example.net:3306"));
assertFalse(isUrlValid("jdbc:notmariadb://example.net:3306/"));
}

private static boolean isUrlValid(String url)
{
MariaDbJdbcConfig config = new MariaDbJdbcConfig();
config.setConnectionUrl(url);
return config.isUrlValid();
}

@Test
public void testIsUrlWithoutDatabase()
{
assertTrue(isUrlWithoutDatabase("jdbc:mariadb://example.net:3306"));
assertTrue(isUrlWithoutDatabase("jdbc:mariadb://example.net:3306/"));
assertFalse(isUrlWithoutDatabase("jdbc:mariadb://example.net:3306/somedatabase"));
}

private static boolean isUrlWithoutDatabase(String url)
{
MariaDbJdbcConfig config = new MariaDbJdbcConfig();
config.setConnectionUrl(url);
return config.isUrlWithoutDatabase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.testng.annotations.Test;

import static com.google.common.collect.Iterables.getOnlyElement;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestMariaDbPlugin
{
Expand All @@ -29,5 +30,11 @@ public void testCreateConnector()
Plugin plugin = new MariaDbPlugin();
ConnectorFactory factory = getOnlyElement(plugin.getConnectorFactories());
factory.create("test", ImmutableMap.of("connection-url", "jdbc:mariadb://test"), new TestingConnectorContext()).shutdown();

assertThatThrownBy(() -> factory.create("test", ImmutableMap.of("connection-url", "test"), new TestingConnectorContext()))
.hasMessageContaining("Invalid JDBC URL for MariaDB connector");

assertThatThrownBy(() -> factory.create("test", ImmutableMap.of("connection-url", "jdbc:mariadb://test/abc"), new TestingConnectorContext()))
.hasMessageContaining("Database (catalog) must not be specified in JDBC URL for MariaDB connector");
}
}