You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/08/05 14:15:53 UTC
[shardingsphere] branch master updated: Redesign JDBCRepository and H2Repository (#19841)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 108f4559f94 Redesign JDBCRepository and H2Repository (#19841)
108f4559f94 is described below
commit 108f4559f94e4d23d8cc3a9f2c4190fa1805c69e
Author: 小马哥 <ma...@163.com>
AuthorDate: Fri Aug 5 22:15:46 2022 +0800
Redesign JDBCRepository and H2Repository (#19841)
* Redesign JDBCRepository and H2Repository
* Redesign JDBCRepository and H2Repository
* Reuse SQL execution statements
* Fix CI errors
* Test class configuration H2 changed to JDBC
* Redesign JDBCRepository and H2Repository
* Change JDBCRepository initialization
---
.../spring/standalone-application-context.xml | 2 +-
.../repository/standalone/jdbc/JDBCRepository.java | 32 ++++++---
.../standalone/jdbc/JDBCRepositoryPropertyKey.java | 6 +-
.../standalone/jdbc/JDBCRepositoryProvider.java | 78 ++++++++++++++++++++++
.../jdbc/JDBCRepositoryProviderFactory.java | 46 +++++++++++++
...pository.standalone.StandalonePersistRepository | 2 +-
.../standalone/h2/H2JDBCRepositoryProvider.java | 71 ++++++++++++++++++++
.../repository/standalone/h2/H2Repository.java | 71 --------------------
...ository.standalone.jdbc.JDBCRepositoryProvider} | 2 +-
.../repository/standalone/h2/H2RepositoryTest.java | 28 ++++----
10 files changed, 239 insertions(+), 99 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/spring/standalone-application-context.xml b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/spring/standalone-application-context.xml
index 795be6f85e5..ff2b2b541bc 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/spring/standalone-application-context.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/resources/spring/standalone-application-context.xml
@@ -35,7 +35,7 @@
<import resource="included/data-sources-context.xml" />
<import resource="included/rules-context.xml" />
- <standalone:repository id="fileRepository" type="H2" />
+ <standalone:repository id="fileRepository" type="JDBC" />
<shardingsphere:data-source id="dataSource" data-source-names="ds_0_write,ds_0_read_0,ds_0_read_1,ds_1_write,ds_1_read_0,ds_1_read_1" rule-refs="shardingRule, readWriteSplittingRule, encryptRule, sqlParseRule">
<shardingsphere:mode type="Standalone" repository-ref="fileRepository" overwrite="true" />
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/ [...]
index 720050c00b9..e6e5053b9c0 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
@@ -33,30 +33,37 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
+import java.util.Properties;
import java.util.UUID;
/**
* JDBC repository.
*/
@Slf4j
-public abstract class JDBCRepository implements StandalonePersistRepository {
+public final class JDBCRepository implements StandalonePersistRepository {
private static final String SEPARATOR = "/";
private Connection connection;
+ private JDBCRepositoryProvider jdbcRepositoryProvider;
+
@SneakyThrows
- protected void initTable(final String jdbcUrl, final String user, final String password) {
- connection = DriverManager.getConnection(jdbcUrl, user, password);
+ @Override
+ public void init(final Properties props) {
+ JDBCRepositoryProperties localRepositoryProps = new JDBCRepositoryProperties(props);
+ connection = DriverManager.getConnection(localRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL),
+ localRepositoryProps.getValue(JDBCRepositoryPropertyKey.USERNAME), localRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD));
+ jdbcRepositoryProvider = JDBCRepositoryProviderFactory.getInstance(localRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
try (Statement statement = connection.createStatement()) {
- statement.execute("DROP TABLE IF EXISTS repository");
- statement.execute("CREATE TABLE repository(id varchar(36) PRIMARY KEY, key TEXT, value TEXT, parent TEXT)");
+ statement.execute(jdbcRepositoryProvider.dropTableSQL());
+ statement.execute(jdbcRepositoryProvider.createTableSQL());
}
}
@Override
public String get(final String key) {
- try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT value FROM repository WHERE key = ?")) {
+ try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcRepositoryProvider.selectByKeySQL())) {
preparedStatement.setString(1, key);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
@@ -71,7 +78,7 @@ public abstract class JDBCRepository implements StandalonePersistRepository {
@Override
public List<String> getChildrenKeys(final String key) {
- try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT key FROM repository WHERE parent = ?")) {
+ try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcRepositoryProvider.selectByParentKeySQL())) {
preparedStatement.setString(1, key);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
List<String> resultChildren = new LinkedList<>();
@@ -122,7 +129,7 @@ public abstract class JDBCRepository implements StandalonePersistRepository {
}
private void insert(final String key, final String value, final String parent) throws SQLException {
- try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO repository VALUES(?, ?, ?, ?)")) {
+ try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcRepositoryProvider.insertSQL())) {
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setString(2, key);
preparedStatement.setString(3, value);
@@ -132,7 +139,7 @@ public abstract class JDBCRepository implements StandalonePersistRepository {
}
private void update(final String key, final String value) throws SQLException {
- try (PreparedStatement preparedStatement = connection.prepareStatement("UPDATE repository SET value = ? WHERE key = ?")) {
+ try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcRepositoryProvider.updateSQL())) {
preparedStatement.setString(1, value);
preparedStatement.setString(2, key);
preparedStatement.executeUpdate();
@@ -141,7 +148,7 @@ public abstract class JDBCRepository implements StandalonePersistRepository {
@Override
public void delete(final String key) {
- try (PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM repository WHERE key = ?")) {
+ try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcRepositoryProvider.deleteSQL())) {
preparedStatement.setString(1, key);
preparedStatement.executeUpdate();
} catch (final SQLException ex) {
@@ -159,4 +166,9 @@ public abstract class JDBCRepository implements StandalonePersistRepository {
log.error(String.format("Failed to release %s database resources.", getType()), ex);
}
}
+
+ @Override
+ public String getType() {
+ return "JDBC";
+ }
}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryPropertyKey.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode- [...]
index 700cf4a464a..9efa8469727 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryPropertyKey.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryPropertyKey.java
@@ -28,9 +28,11 @@ import org.apache.shardingsphere.infra.util.props.TypedPropertyKey;
@Getter
public enum JDBCRepositoryPropertyKey implements TypedPropertyKey {
- JDBC_URL("jdbc_url", "", String.class),
+ PROVIDER("provider", "H2", String.class),
- USER("user", "", String.class),
+ JDBC_URL("jdbc_url", "jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL", String.class),
+
+ USERNAME("username", "sa", String.class),
PASSWORD("password", "", String.class);
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryProvider.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-rep [...]
new file mode 100644
index 00000000000..8db4893694b
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryProvider.java
@@ -0,0 +1,78 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.spi.type.required.RequiredSPI;
+import org.apache.shardingsphere.spi.type.typed.TypedSPI;
+
+/**
+ * JDBC repository provider.
+ */
+@SingletonSPI
+public interface JDBCRepositoryProvider extends TypedSPI, RequiredSPI {
+
+ /**
+ * Drop table SQL.
+ *
+ * @return SQL to drop table
+ */
+ String dropTableSQL();
+
+ /**
+ * Create table SQL.
+ *
+ * @return SQL to create table
+ */
+ String createTableSQL();
+
+ /**
+ * Select by key SQL.
+ *
+ * @return SQL to select table
+ */
+ String selectByKeySQL();
+
+ /**
+ * Select by parent key SQL.
+ *
+ * @return SQL to select table
+ */
+ String selectByParentKeySQL();
+
+ /**
+ * Insert SQL.
+ *
+ * @return SQL to insert table
+ */
+ String insertSQL();
+
+ /**
+ * Update SQL.
+ *
+ * @return SQL to update table
+ */
+ String updateSQL();
+
+ /**
+ * Delete SQL.
+ *
+ * @return SQL to delete table
+ */
+ String deleteSQL();
+}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryProviderFactory.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-m [...]
new file mode 100644
index 00000000000..308e0b6de1b
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryProviderFactory.java
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.required.RequiredSPIRegistry;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
+
+/**
+ * JDBC repository provider factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class JDBCRepositoryProviderFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(JDBCRepositoryProvider.class);
+ }
+
+ /**
+ * Get instance of JDBC repository provider.
+ *
+ * @param jdbcRepositoryType JDBC repository type
+ * @return got instance
+ */
+ public static JDBCRepositoryProvider getInstance(final Object jdbcRepositoryType) {
+ return jdbcRepositoryType == null ? RequiredSPIRegistry.getRegisteredService(JDBCRepositoryProvider.class)
+ : TypedSPIRegistry.getRegisteredService(JDBCRepositoryProvider.class, jdbcRepositoryType.toString());
+ }
+}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-sta [...]
similarity index 91%
copy from shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
copy to shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
index 04072ece3b4..870db86cd16 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.mode.repository.standalone.h2.H2Repository
+org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2JDBCRepositoryProvider.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repos [...]
new file mode 100644
index 00000000000..17c8ac9f490
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2JDBCRepositoryProvider.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.h2;
+
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepositoryProvider;
+
+/**
+ * H2 JDBC repository provider.
+ */
+public final class H2JDBCRepositoryProvider implements JDBCRepositoryProvider {
+
+ @Override
+ public String getType() {
+ return "H2";
+ }
+
+ @Override
+ public String dropTableSQL() {
+ return "DROP TABLE IF EXISTS `repository`";
+ }
+
+ @Override
+ public String createTableSQL() {
+ return "CREATE TABLE `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 = ?";
+ }
+
+ @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 boolean isDefault() {
+ return true;
+ }
+}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2Repository.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardi [...]
deleted file mode 100644
index 59192f669af..00000000000
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2Repository.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * 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.h2;
-
-import com.google.common.base.Strings;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepositoryProperties;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepositoryPropertyKey;
-
-import java.util.Optional;
-import java.util.Properties;
-
-/**
- * H2 repository.
- */
-@Slf4j
-public final class H2Repository extends JDBCRepository {
-
- private static final String DEFAULT_JDBC_URL = "jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL";
-
- private static final String DEFAULT_USER = "sa";
-
- private static final String DEFAULT_PASSWORD = "";
-
- @Override
- public void init(final Properties props) {
- JDBCRepositoryProperties localRepositoryProps = new JDBCRepositoryProperties(props);
- String jdbcUrl = Optional.ofNullable(Strings.emptyToNull(localRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL))).orElse(DEFAULT_JDBC_URL);
- String user = Optional.ofNullable(Strings.emptyToNull(localRepositoryProps.getValue(JDBCRepositoryPropertyKey.USER))).orElse(DEFAULT_USER);
- String password = Optional.ofNullable(Strings.emptyToNull(localRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD))).orElse(DEFAULT_PASSWORD);
- initTable(jdbcUrl, user, password);
- }
-
- @Override
- public String get(final String key) {
- return Optional.ofNullable(Strings.emptyToNull(super.get(key))).map(each -> each.replace("\"", "'")).orElse("");
- }
-
- @Override
- public void persist(final String key, final String value) {
- // Single quotation marks are the keywords executed by H2. Replace with double quotation marks.
- String insensitiveValue = value.replace("'", "\"");
- super.persist(key, insensitiveValue);
- }
-
- @Override
- public String getType() {
- return "H2";
- }
-
- @Override
- public boolean isDefault() {
- return true;
- }
-}
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-sta [...]
similarity index 90%
rename from shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
rename to shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepositoryProvider
index 04072ece3b4..1ce78f6ecc8 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepositoryProvider
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.mode.repository.standalone.h2.H2Repository
+org.apache.shardingsphere.mode.repository.standalone.h2.H2JDBCRepositoryProvider
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/test/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2RepositoryTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/sh [...]
index a5e5245c183..62cea87e893 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/test/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2RepositoryTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-h2/src/test/java/org/apache/shardingsphere/mode/repository/standalone/h2/H2RepositoryTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.mode.repository.standalone.h2;
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -29,43 +30,44 @@ import static org.junit.Assert.assertThat;
public final class H2RepositoryTest {
- private final H2Repository h2Repository = new H2Repository();
+ private final JDBCRepository jdbcRepository = new JDBCRepository();
@Before
public void setUp() {
Properties props = new Properties();
props.setProperty("jdbc_url", "jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
- props.setProperty("user", "sa");
+ props.setProperty("username", "sa");
props.setProperty("password", "");
props.setProperty("driver_class", "org.h2.Driver");
- h2Repository.init(props);
+ props.setProperty("provider", "H2");
+ jdbcRepository.init(props);
}
@Test
public void assertPersistAndGet() {
- h2Repository.persist("/testPath/test1", "test1_content");
- assertThat(h2Repository.get("/testPath/test1"), is("test1_content"));
- h2Repository.persist("/testPath/test1", "modify_content");
- assertThat(h2Repository.get("/testPath/test1"), is("modify_content"));
+ jdbcRepository.persist("/testPath/test1", "test1_content");
+ assertThat(jdbcRepository.get("/testPath/test1"), is("test1_content"));
+ jdbcRepository.persist("/testPath/test1", "modify_content");
+ assertThat(jdbcRepository.get("/testPath/test1"), is("modify_content"));
}
@Test
public void assertPersistAndGetChildrenKeys() {
- h2Repository.persist("/testPath/test1", "test1_content");
- h2Repository.persist("/testPath/test2", "test2_content");
- List<String> childrenKeys = h2Repository.getChildrenKeys("/testPath");
+ jdbcRepository.persist("/testPath/test1", "test1_content");
+ jdbcRepository.persist("/testPath/test2", "test2_content");
+ List<String> childrenKeys = jdbcRepository.getChildrenKeys("/testPath");
assertThat(childrenKeys.get(0), is("test1"));
assertThat(childrenKeys.get(1), is("test2"));
}
@Test
public void assertDelete() {
- h2Repository.delete("/testPath");
- assertThat(h2Repository.get("/testPath"), is(""));
+ jdbcRepository.delete("/testPath");
+ assertThat(jdbcRepository.get("/testPath"), is(""));
}
@After
public void stop() {
- h2Repository.close();
+ jdbcRepository.close();
}
}