This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1b8d9539a32 Add JDBCRepositoryProvider implementation of MYSQL (#23176)
1b8d9539a32 is described below
commit 1b8d9539a3294d33cfc1b734510114bebdc4bd78
Author: Ling Hengqian <[email protected]>
AuthorDate: Sat Dec 31 16:35:34 2022 +0800
Add JDBCRepositoryProvider implementation of MYSQL (#23176)
* Add StandalonePersistRepository implementation of R2DBC_MariaDB
* Add JDBCRepositoryProvider implementation of MYSQL
---
.../builtin-algorithm/metadata-repository.cn.md | 12 +-
.../builtin-algorithm/metadata-repository.en.md | 12 +-
.../repository/provider/jdbc/{ => mysql}/pom.xml | 31 +++--
.../jdbc/mysql/MySQLJDBCRepositoryProvider.java | 71 +++++++++++
...standalone.jdbc.provider.JDBCRepositoryProvider | 18 +++
.../mysql/MySQLJDBCRepositoryProviderTest.java | 140 +++++++++++++++++++++
.../standalone/repository/provider/jdbc/pom.xml | 1 +
src/resources/checkstyle.xml | 2 +-
src/resources/checkstyle_ci.xml | 2 +-
9 files changed, 268 insertions(+), 21 deletions(-)
diff --git
a/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.cn.md
b/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.cn.md
index 7b9b94c6464..1dd453f33fa 100644
---
a/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.cn.md
+++
b/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.cn.md
@@ -17,12 +17,12 @@ Apache ShardingSphere 为不同的运行模式提供了不同的元数据持久
可配置属性:
-| *名称* | *数据类型* | *说明* | *默认值* |
-| ---------------------------- | --------- | ----------------- |
--------------- |
-| provider | String | 元数据存储类型 | H2 |
-| jdbc_url | String | JDBC URL |
jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
|
-| username | String | 账号 | sa
|
-| password | String | 密码 |
|
+| *名称* | *数据类型* | *说明* | *默认值*
|
+|----------|--------|-----------------------|-------------------------------------------------------------------------|
+| provider | String | 元数据存储类型,可选值为 H2,MYSQL | H2
|
+| jdbc_url | String | JDBC URL |
jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL |
+| username | String | 账号 | sa
|
+| password | String | 密码 |
|
### ZooKeeper 持久化
diff --git
a/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.en.md
b/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.en.md
index 1c8dee89b71..f79de0c9115 100644
---
a/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.en.md
+++
b/docs/document/content/user-manual/common-config/builtin-algorithm/metadata-repository.en.md
@@ -17,12 +17,12 @@ Mode: Standalone
Attributes:
-| *Name* | *Type* | *Description* |
*Default Value* |
-| ---------------------------- | ------ | --------------------------------- |
----------------------------------------------------------------------- |
-| provider | String | Type for metadata persist |
H2 |
-| jdbc_url | String | JDBC URL |
jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
|
-| username | String | username | sa
|
-| password | String | password |
|
+| *Name* | *Type* | *Description*
| *Default Value* |
+|----------|--------|------------------------------------------------------------|-------------------------------------------------------------------------|
+| provider | String | Type for metadata persist, the optional value is H2,
MYSQL | H2
|
+| jdbc_url | String | JDBC URL
| jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL |
+| username | String | username
| sa |
+| password | String | password
| |
### ZooKeeper Repository
diff --git a/mode/type/standalone/repository/provider/jdbc/pom.xml
b/mode/type/standalone/repository/provider/jdbc/mysql/pom.xml
similarity index 56%
copy from mode/type/standalone/repository/provider/jdbc/pom.xml
copy to mode/type/standalone/repository/provider/jdbc/mysql/pom.xml
index c5541a5f76c..ce004de3689 100644
--- a/mode/type/standalone/repository/provider/jdbc/pom.xml
+++ b/mode/type/standalone/repository/provider/jdbc/mysql/pom.xml
@@ -21,14 +21,31 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
-
<artifactId>shardingsphere-standalone-mode-repository-provider</artifactId>
+ <artifactId>shardingsphere-standalone-mode-repository-jdbc</artifactId>
<version>5.3.1-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-standalone-mode-repository-jdbc</artifactId>
- <packaging>pom</packaging>
- <modules>
- <module>core</module>
- <module>h2</module>
- </modules>
+
<artifactId>shardingsphere-standalone-mode-repository-jdbc-mysql</artifactId>
+
+ <properties>
+ <testcontainers.version>1.17.6</testcontainers.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-standalone-mode-repository-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-standalone-mode-repository-jdbc-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
diff --git
a/mode/type/standalone/repository/provider/jdbc/mysql/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProvider.java
b/mode/type/standalone/repository/provider/jdbc/mysql/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProvider.java
new file mode 100644
index 00000000000..347356f4c13
--- /dev/null
+++
b/mode/type/standalone/repository/provider/jdbc/mysql/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProvider.java
@@ -0,0 +1,71 @@
+/*
+ * 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.mode.repository.standalone.jdbc.mysql;
+
+import
org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider;
+
+/**
+ * MySQL JDBC repository provider.
+ */
+public final class MySQLJDBCRepositoryProvider implements
JDBCRepositoryProvider {
+
+ @Override
+ public String dropTableSQL() {
+ return "DROP TABLE IF EXISTS `repository`";
+ }
+
+ @Override
+ public String createTableSQL() {
+ return "CREATE TABLE IF NOT EXISTS `repository`(id varchar(36) PRIMARY
KEY, `key` TEXT, `value` TEXT, parent TEXT)";
+ }
+
+ @Override
+ public String selectByKeySQL() {
+ return "SELECT `value` FROM `repository` WHERE `key` = ?";
+ }
+
+ @Override
+ public String selectByParentKeySQL() {
+ return "SELECT `key` FROM `repository` WHERE parent = ? ORDER BY `key`
ASC";
+ }
+
+ @Override
+ public String insertSQL() {
+ return "INSERT INTO `repository` VALUES(?, ?, ?, ?)";
+ }
+
+ @Override
+ public String updateSQL() {
+ return "UPDATE `repository` SET `value` = ? WHERE `key` = ?";
+ }
+
+ @Override
+ public String deleteSQL() {
+ return "DELETE FROM `repository` WHERE `key` = ?";
+ }
+
+ @Override
+ public String getType() {
+ return "MYSQL";
+ }
+
+ @Override
+ public boolean isDefault() {
+ return false;
+ }
+}
diff --git
a/mode/type/standalone/repository/provider/jdbc/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider
b/mode/type/standalone/repository/provider/jdbc/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider
new file mode 100644
index 00000000000..489eb10272b
--- /dev/null
+++
b/mode/type/standalone/repository/provider/jdbc/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.mode.repository.standalone.jdbc.mysql.MySQLJDBCRepositoryProvider
diff --git
a/mode/type/standalone/repository/provider/jdbc/mysql/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProviderTest.java
b/mode/type/standalone/repository/provider/jdbc/mysql/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProviderTest.java
new file mode 100644
index 00000000000..1d513062266
--- /dev/null
+++
b/mode/type/standalone/repository/provider/jdbc/mysql/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/mysql/MySQLJDBCRepositoryProviderTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.mode.repository.standalone.jdbc.mysql;
+
+import com.mysql.jdbc.ConnectionImpl;
+import com.mysql.jdbc.JDBC42CallableStatement;
+import com.mysql.jdbc.ResultSetImpl;
+import com.mysql.jdbc.StatementImpl;
+import com.zaxxer.hikari.HikariDataSource;
+import
org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockedConstruction;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MySQLJDBCRepositoryProviderTest {
+
+ @Mock
+ private ConnectionImpl mockJdbcConnection;
+
+ @Mock
+ private StatementImpl mockStatement;
+
+ @Mock
+ private JDBC42CallableStatement mockPreparedStatement;
+
+ @Mock
+ private JDBC42CallableStatement mockPreparedStatementForPersist;
+
+ @Mock
+ private ResultSetImpl mockResultSet;
+
+ private final MySQLJDBCRepositoryProvider provider = new
MySQLJDBCRepositoryProvider();
+
+ private MockedConstruction<HikariDataSource> mockedConstruction;
+
+ private JDBCRepository repository;
+
+ @Before
+ public void setUp() throws SQLException {
+ this.mockedConstruction = mockConstruction(HikariDataSource.class,
(mock, context) -> when(mock.getConnection()).thenReturn(mockJdbcConnection));
+ when(mockJdbcConnection.createStatement()).thenReturn(mockStatement);
+ Properties props = new Properties();
+ props.setProperty("jdbc_url", "jdbc:mysql://localhost:3306/config");
+ props.setProperty("username", "sa");
+ props.setProperty("password", "");
+ props.setProperty("provider", "MYSQL");
+ repository = new JDBCRepository();
+ repository.init(props);
+ }
+
+ @After
+ public void stop() {
+ repository.close();
+ this.mockedConstruction.close();
+ }
+
+ @Test
+ public void assertPersistAndGet() throws SQLException {
+
when(mockJdbcConnection.prepareStatement(eq(provider.selectByKeySQL()))).thenReturn(mockPreparedStatement);
+
when(mockJdbcConnection.prepareStatement(eq(provider.insertSQL()))).thenReturn(mockPreparedStatementForPersist);
+
when(mockJdbcConnection.prepareStatement(eq(provider.updateSQL()))).thenReturn(mockPreparedStatementForPersist);
+ when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
+ when(mockResultSet.next())
+ .thenReturn(false)
+ .thenReturn(true)
+ .thenReturn(true);
+ when(mockResultSet.getString(eq("value")))
+ .thenReturn("test1_content")
+ .thenReturn("test1_content")
+ .thenReturn("modify_content");
+ repository.persist("/testPath/test1", "test1_content");
+ assertThat(repository.getDirectly("/testPath/test1"),
is("test1_content"));
+ repository.persist("/testPath/test1", "modify_content");
+ assertThat(repository.getDirectly("/testPath/test1"),
is("modify_content"));
+ }
+
+ @Test
+ public void assertPersistAndGetChildrenKeys() throws SQLException {
+
when(mockJdbcConnection.prepareStatement(eq(provider.selectByKeySQL()))).thenReturn(mockPreparedStatement);
+
when(mockJdbcConnection.prepareStatement(eq(provider.insertSQL()))).thenReturn(mockPreparedStatementForPersist);
+
when(mockJdbcConnection.prepareStatement(eq(provider.selectByParentKeySQL()))).thenReturn(mockPreparedStatement);
+ when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
+ when(mockResultSet.next())
+ .thenReturn(false)
+ .thenReturn(false)
+ .thenReturn(false)
+ .thenReturn(false)
+ .thenReturn(true)
+ .thenReturn(true)
+ .thenReturn(false);
+ when(mockResultSet.getString(eq("key")))
+ .thenReturn("test1")
+ .thenReturn("test2");
+ repository.persist("/testPath/test1", "test1_content");
+ repository.persist("/testPath/test2", "test2_content");
+ List<String> childrenKeys = repository.getChildrenKeys("/testPath");
+ assertThat(childrenKeys.get(0), is("test1"));
+ assertThat(childrenKeys.get(1), is("test2"));
+ }
+
+ @Test
+ public void assertDelete() throws SQLException {
+
when(mockJdbcConnection.prepareStatement(eq(provider.selectByKeySQL()))).thenReturn(mockPreparedStatement);
+
when(mockJdbcConnection.prepareStatement(eq(provider.deleteSQL()))).thenReturn(mockPreparedStatementForPersist);
+ when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
+ when(mockResultSet.next()).thenReturn(false);
+ repository.delete("/testPath");
+ assertThat(repository.getDirectly("/testPath"), is(""));
+ }
+}
diff --git a/mode/type/standalone/repository/provider/jdbc/pom.xml
b/mode/type/standalone/repository/provider/jdbc/pom.xml
index c5541a5f76c..539ccbdfa25 100644
--- a/mode/type/standalone/repository/provider/jdbc/pom.xml
+++ b/mode/type/standalone/repository/provider/jdbc/pom.xml
@@ -30,5 +30,6 @@
<modules>
<module>core</module>
<module>h2</module>
+ <module>mysql</module>
</modules>
</project>
diff --git a/src/resources/checkstyle.xml b/src/resources/checkstyle.xml
index cead530417f..85c0ae4039c 100644
--- a/src/resources/checkstyle.xml
+++ b/src/resources/checkstyle.xml
@@ -54,7 +54,7 @@
<module name="ParameterName" />
<module name="CatchParameterName" />
<module name="AbbreviationAsWordInName">
- <property name="allowedAbbreviationLength" value="6" />
+ <property name="allowedAbbreviationLength" value="8" />
</module>
<!-- Size Violations -->
diff --git a/src/resources/checkstyle_ci.xml b/src/resources/checkstyle_ci.xml
index 3921808c46f..2f3a08375a0 100644
--- a/src/resources/checkstyle_ci.xml
+++ b/src/resources/checkstyle_ci.xml
@@ -54,7 +54,7 @@
<module name="ParameterName" />
<module name="CatchParameterName" />
<module name="AbbreviationAsWordInName">
- <property name="allowedAbbreviationLength" value="6" />
+ <property name="allowedAbbreviationLength" value="8" />
</module>
<!-- Size Violations -->