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 c45713c8bb3 Add GeneratedKeyColumnProvider (#31541)
c45713c8bb3 is described below

commit c45713c8bb3279efc0dd57097b71f46b1d669c9f
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jun 2 23:21:42 2024 +0800

    Add GeneratedKeyColumnProvider (#31541)
---
 .../core/keygen/GeneratedKeyColumnProvider.java    | 35 +++++++++++++++++++++
 .../keygen/MySQLGeneratedKeyColumnProvider.java    | 36 ++++++++++++++++++++++
 ...database.core.keygen.GeneratedKeyColumnProvider | 18 +++++++++++
 .../statement/ShardingSpherePreparedStatement.java |  6 ++--
 .../core/statement/ShardingSphereStatement.java    |  6 ++--
 5 files changed, 97 insertions(+), 4 deletions(-)

diff --git 
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/keygen/GeneratedKeyColumnProvider.java
 
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/keygen/GeneratedKeyColumnProvider.java
new file mode 100644
index 00000000000..4a2c3337b56
--- /dev/null
+++ 
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/keygen/GeneratedKeyColumnProvider.java
@@ -0,0 +1,35 @@
+/*
+ * 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.infra.database.core.keygen;
+
+import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+
+/**
+ * Generated key column provider.
+ */
+@SingletonSPI
+public interface GeneratedKeyColumnProvider extends DatabaseTypedSPI {
+    
+    /**
+     * Get generated key column.
+     *
+     * @return generated key column
+     */
+    String getColumnName();
+}
diff --git 
a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/keygen/MySQLGeneratedKeyColumnProvider.java
 
b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/keygen/MySQLGeneratedKeyColumnProvider.java
new file mode 100644
index 00000000000..cd8bcb6adc6
--- /dev/null
+++ 
b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/keygen/MySQLGeneratedKeyColumnProvider.java
@@ -0,0 +1,36 @@
+/*
+ * 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.infra.database.mysql.keygen;
+
+import 
org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider;
+
+/**
+ * Generated key column provider for MySQL.
+ */
+public final class MySQLGeneratedKeyColumnProvider implements 
GeneratedKeyColumnProvider {
+    
+    @Override
+    public String getColumnName() {
+        return "GENERATED_KEY";
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        return "MySQL";
+    }
+}
diff --git 
a/infra/database/type/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider
 
b/infra/database/type/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider
new file mode 100644
index 00000000000..56f8c5087e0
--- /dev/null
+++ 
b/infra/database/type/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider
@@ -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.infra.database.mysql.keygen.MySQLGeneratedKeyColumnProvider
diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 949da00bd0f..1f8d6949915 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -37,7 +37,8 @@ import 
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatem
 import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.connection.kernel.KernelProcessor;
-import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
+import 
org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider;
+import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.exception.dialect.SQLExceptionTransformEngine;
 import 
org.apache.shardingsphere.infra.exception.kernel.syntax.EmptySQLException;
@@ -421,7 +422,8 @@ public final class ShardingSpherePreparedStatement extends 
AbstractPreparedState
     }
     
     private String getGeneratedKeysColumnName(final String columnName) {
-        return metaData.getDatabase(databaseName).getProtocolType() instanceof 
MySQLDatabaseType ? "GENERATED_KEY" : columnName;
+        return 
DatabaseTypedSPILoader.findService(GeneratedKeyColumnProvider.class, 
metaData.getDatabase(databaseName).getProtocolType())
+                
.map(GeneratedKeyColumnProvider::getColumnName).orElse(columnName);
     }
     
     @Override
diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index f06e68ff51c..dd65b28fdf2 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -34,7 +34,8 @@ import 
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatem
 import 
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
-import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
+import 
org.apache.shardingsphere.infra.database.core.keygen.GeneratedKeyColumnProvider;
+import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.exception.dialect.SQLExceptionTransformEngine;
 import 
org.apache.shardingsphere.infra.exception.kernel.syntax.EmptySQLException;
@@ -426,6 +427,7 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
     }
     
     private String getGeneratedKeysColumnName(final String columnName) {
-        return metaData.getDatabase(databaseName).getProtocolType() instanceof 
MySQLDatabaseType ? "GENERATED_KEY" : columnName;
+        return 
DatabaseTypedSPILoader.findService(GeneratedKeyColumnProvider.class, 
metaData.getDatabase(databaseName).getProtocolType())
+                
.map(GeneratedKeyColumnProvider::getColumnName).orElse(columnName);
     }
 }

Reply via email to