提交 cd8b4d8d 编写于 作者: Y yanqf

Add support for MariadDB;

上级 e93dc9cc
......@@ -93,7 +93,8 @@
<mysql-connector-java.version>5.1.47</mysql-connector-java.version>
<postgresql.version>42.2.5</postgresql.version>
<mssql.version>6.1.7.jre7-preview</mssql.version>
<mariadb-java-client.version>2.4.2</mariadb-java-client.version>
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<maven-assembly-plugin.version>3.1.0</maven-assembly-plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
......@@ -291,6 +292,12 @@
<version>${mysql-connector-java.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb-java-client.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.metadata.datasource.dialect;
import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.core.metadata.datasource.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.spi.database.DataSourceMetaData;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Data source meta data for MariaDB.
*
* @author yanqiufang
*/
@Getter
public final class MariaDBDataSourceMetaData implements DataSourceMetaData {
private static final int DEFAULT_PORT = 3306;
private final String hostName;
private final int port;
private final String schemaName;
private final Pattern pattern = Pattern.compile("jdbc:(mysql|mariadb)(:replication|:failover|:sequential|:aurora)?:(\\w*:)?//([\\w\\-\\.]+):?([0-9]*)/([\\w\\-]+);?\\S*", Pattern.CASE_INSENSITIVE);
public MariaDBDataSourceMetaData(final String url) {
Matcher matcher = pattern.matcher(url);
if (!matcher.find()) {
throw new UnrecognizedDatabaseURLException(url, pattern.pattern());
}
hostName = matcher.group(4);
port = Strings.isNullOrEmpty(matcher.group(5)) ? DEFAULT_PORT : Integer.valueOf(matcher.group(5));
schemaName = matcher.group(6);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.core.spi.database;
import org.apache.shardingsphere.core.database.DatabaseTypes;
import org.apache.shardingsphere.core.metadata.datasource.dialect.MariaDBDataSourceMetaData;
import org.apache.shardingsphere.spi.database.BranchDatabaseType;
import org.apache.shardingsphere.spi.database.DataSourceMetaData;
import org.apache.shardingsphere.spi.database.DatabaseType;
import java.util.Collection;
import java.util.Collections;
/**
* Database type of Mariadb.
*
* @author yanqiufang
*/
public final class MariaDBDatabaseType implements BranchDatabaseType {
@Override
public String getName() {
return "MariaDB";
}
@Override
public Collection<String> getJdbcUrlPrefixAlias() {
return Collections.singletonList("jdbc:mariadb:");
}
@Override
public DataSourceMetaData getDataSourceMetaData(final String url) {
return new MariaDBDataSourceMetaData(url);
}
@Override
public DatabaseType getTrunkDatabaseType() {
return DatabaseTypes.getActualDatabaseType("MySQL");
}
}
......@@ -16,6 +16,7 @@
#
org.apache.shardingsphere.core.spi.database.MySQLDatabaseType
org.apache.shardingsphere.core.spi.database.MariaDBDatabaseType
org.apache.shardingsphere.core.spi.database.PostgreSQLDatabaseType
org.apache.shardingsphere.core.spi.database.OracleDatabaseType
org.apache.shardingsphere.core.spi.database.SQLServerDatabaseType
......
......@@ -75,6 +75,10 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
......
......@@ -21,6 +21,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.spi.database.DatabaseType;
import org.apache.shardingsphere.transaction.xa.jta.connection.dialect.H2XAConnectionWrapper;
import org.apache.shardingsphere.transaction.xa.jta.connection.dialect.MariaDBXAConnectionWrapper;
import org.apache.shardingsphere.transaction.xa.jta.connection.dialect.MySQLXAConnectionWrapper;
import org.apache.shardingsphere.transaction.xa.jta.connection.dialect.PostgreSQLXAConnectionWrapper;
......@@ -48,6 +49,8 @@ public final class XAConnectionFactory {
switch (databaseType.getName()) {
case "MySQL":
return new MySQLXAConnectionWrapper().wrap(xaDataSource, connection);
case "MariaDB":
return new MariaDBXAConnectionWrapper().wrap(xaDataSource, connection);
case "PostgreSQL":
return new PostgreSQLXAConnectionWrapper().wrap(xaDataSource, connection);
case "H2":
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.transaction.xa.jta.connection.dialect;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper;
import org.mariadb.jdbc.MariaDbConnection;
import org.mariadb.jdbc.MariaXaConnection;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import java.sql.Connection;
/**
* XA connection wrapper for MariaDB.
*
* @author yanqiufang
*/
@RequiredArgsConstructor
public final class MariaDBXAConnectionWrapper implements XAConnectionWrapper {
@SneakyThrows
@Override
public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) {
MariaDbConnection physicalConnection = (MariaDbConnection) connection.unwrap(Class.forName("org.mariadb.jdbc.MariaDbConnection"));
return new MariaXaConnection(physicalConnection);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.core.metadata.datasource.dialect.MariaDBDataSourceMetaData;
import org.apache.shardingsphere.transaction.xa.jta.datasource.properties.XADataSourceDefinition;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
/**
* XA data source definition for MariaDB.
*
* @author yanqiufang
*/
public final class MariaDBXADataSourceDefinition implements XADataSourceDefinition {
@Override
public String getDatabaseType() {
return "MariaDB";
}
@Override
public Collection<String> getXADriverClassName() {
return Collections.singletonList("org.mariadb.jdbc.MariaDbDataSource");
}
@Override
public Properties getXAProperties(final DatabaseAccessConfiguration databaseAccessConfiguration) {
Properties result = new Properties();
MariaDBDataSourceMetaData dataSourceMetaData = new MariaDBDataSourceMetaData(databaseAccessConfiguration.getUrl());
result.setProperty("user", databaseAccessConfiguration.getUsername());
result.setProperty("password", Optional.fromNullable(databaseAccessConfiguration.getPassword()).or(""));
result.setProperty("url", databaseAccessConfiguration.getUrl());
result.setProperty("ServerName", dataSourceMetaData.getHostName());
result.setProperty("port", String.valueOf(dataSourceMetaData.getPort()));
result.setProperty("DatabaseName", dataSourceMetaData.getSchemaName());
return result;
}
}
......@@ -16,6 +16,7 @@
#
org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect.MySQLXADataSourceDefinition
org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect.MariaDBXADataSourceDefinition
org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect.PostgreSQLXADataSourceDefinition
org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect.OracleXADataSourceDefinition
org.apache.shardingsphere.transaction.xa.jta.datasource.properties.dialect.SQLServerXADataSourceDefinition
......
......@@ -127,6 +127,8 @@ public final class DataSourceUtils {
switch (databaseType.getName()) {
case "MySQL":
return String.format("jdbc:mysql://localhost:3306/%s", databaseName);
case "MariaDB":
return String.format("jdbc:mariadb://localhost:3306/%s", databaseName);
case "PostgreSQL":
return String.format("jdbc:postgresql://localhost:5432/%s", databaseName);
case "Oracle":
......
......@@ -21,6 +21,7 @@ import org.apache.shardingsphere.core.database.DatabaseTypes;
import org.h2.jdbcx.JdbcXAConnection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mariadb.jdbc.MariaXaConnection;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.postgresql.xa.PGXAConnection;
......@@ -45,7 +46,12 @@ public final class XAConnectionFactoryTest {
public void assertCreateMySQLXAConnection() {
XAConnectionFactory.createXAConnection(DatabaseTypes.getActualDatabaseType("MySQL"), xaDataSource, connection);
}
@Test(expected = Exception.class)
public void assertCreateMariaDBXAConnection() {
assertThat(XAConnectionFactory.createXAConnection(DatabaseTypes.getActualDatabaseType("MariaDB"), xaDataSource, connection), instanceOf(MariaXaConnection.class));
}
@Test
public void assertCreatePostgreSQLXAConnection() {
assertThat(XAConnectionFactory.createXAConnection(DatabaseTypes.getActualDatabaseType("PostgreSQL"), xaDataSource, connection), instanceOf(PGXAConnection.class));
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.transaction.xa.jta.connection.dialect;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.core.database.DatabaseTypes;
import org.apache.shardingsphere.transaction.xa.fixture.DataSourceUtils;
import org.apache.shardingsphere.transaction.xa.jta.datasource.XADataSourceFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAResource;
import java.sql.Connection;
import java.sql.SQLException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class MariaDBXAConnectionWrapperTest {
private XADataSource xaDataSource;
@Mock
private Connection connection;
@Before
@SuppressWarnings("unchecked")
public void setUp() throws SQLException, ClassNotFoundException {
Connection connection = (Connection) mock(Class.forName("org.mariadb.jdbc.MariaDbConnection"));
DataSource dataSource = DataSourceUtils.build(HikariDataSource.class, DatabaseTypes.getActualDatabaseType("MariaDB"), "ds1");
xaDataSource = XADataSourceFactory.build(DatabaseTypes.getActualDatabaseType("MariaDB"), dataSource);
when(this.connection.unwrap((Class<Object>) any())).thenReturn(connection);
}
@Test
public void assertCreateMariaDBConnection() throws SQLException {
XAConnection actual = new MariaDBXAConnectionWrapper().wrap(xaDataSource, connection);
assertThat(actual.getXAResource(), instanceOf(XAResource.class));
assertThat(actual.getConnection(), instanceOf(Connection.class));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册