You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/12/30 09:29:48 UTC

[GitHub] [shardingsphere] linghengqian commented on a diff in pull request #23176: Add JDBCRepositoryProvider implementation of MYSQL

linghengqian commented on code in PR #23176:
URL: https://github.com/apache/shardingsphere/pull/23176#discussion_r1059309506


##########
mode/type/standalone/repository/provider/r2dbc-mariadb/src/main/java/org/apache/shardingsphere/mode/repository/standalone/r2dbc/mariadb/R2DBCMariaDBRepository.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.r2dbc.mariadb;
+
+import com.google.common.base.Strings;
+import io.r2dbc.pool.ConnectionPool;
+import io.r2dbc.pool.ConnectionPoolConfiguration;
+import io.r2dbc.spi.Connection;
+import io.r2dbc.spi.ConnectionFactories;
+import io.r2dbc.spi.ConnectionFactory;
+import io.r2dbc.spi.ConnectionFactoryOptions;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.UUID;
+
+/**
+ * R2DBC MariaDB repository.
+ */
+@Slf4j
+public class R2DBCMariaDBRepository implements StandalonePersistRepository {
+    
+    private static final String SEPARATOR = "/";
+    
+    private ConnectionPool connectionPool;
+    
+    @Override
+    public void init(final Properties props) {
+        ConnectionFactoryOptions connectionFactoryOptions = ConnectionFactoryOptions.builder()
+                .option(ConnectionFactoryOptions.DRIVER, "pool")
+                .option(ConnectionFactoryOptions.PROTOCOL, "mariadb")
+                .option(ConnectionFactoryOptions.HOST, props.getProperty("host"))
+                .option(ConnectionFactoryOptions.PORT, Integer.parseInt(props.getProperty("port")))
+                .option(ConnectionFactoryOptions.USER, props.getProperty("user"))
+                .option(ConnectionFactoryOptions.PASSWORD, props.getProperty("password"))
+                .option(ConnectionFactoryOptions.DATABASE, props.getProperty("database"))
+                .build();
+        ConnectionFactory connectionFactory = ConnectionFactories.get(connectionFactoryOptions);
+        ConnectionPoolConfiguration connectionPoolConfiguration = ConnectionPoolConfiguration.builder(connectionFactory).build();
+        connectionPool = new ConnectionPool(connectionPoolConfiguration);
+        Mono.usingWhen(connectionPool.create(),
+                connection -> Mono.from(connection.createBatch()
+                        .add("DROP TABLE IF EXISTS `repository`")
+                        .add("CREATE TABLE IF NOT EXISTS `repository`(id varchar(36) PRIMARY KEY, `key` TEXT, `value` TEXT, parent TEXT)")
+                        .execute()),
+                Connection::close)
+                .block();
+    }
+    
+    @Override
+    public String getDirectly(final String key) {
+        return Mono.usingWhen(connectionPool.create(),
+                connection -> Mono.from(connection.createStatement("SELECT `value` FROM `repository` WHERE `key` = ?")
+                        .bind(0, key)
+                        .execute())
+                        .flatMap(result -> Mono.from(result.map((row, rowMetadata) -> row.get("value", String.class))))
+                        .doOnError(throwable -> log.error("Get {} data by key: {} failed", getType(), key, throwable)),
+                Connection::close)
+                .block();

Review Comment:
   - I looked back at what the original issue was trying to express, and I noticed that what he actually needed was something else. So I decided to delete the R2DBC implementation in the new commit.
   
   - As for testcontainers, I am referring to the specific Docker container Url of `jdbc:tc:mysql:8.0.31:///config`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org