You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/05/14 12:47:32 UTC

[shardingsphere] branch master updated: Add shardingsphere-sql-translator-jooq-provider module (#17655)

This is an automated email from the ASF dual-hosted git repository.

panjuan 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 93afde76190 Add shardingsphere-sql-translator-jooq-provider module (#17655)
93afde76190 is described below

commit 93afde76190b25829a7ff66871a30172280021fe
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat May 14 20:47:22 2022 +0800

    Add shardingsphere-sql-translator-jooq-provider module (#17655)
---
 .../shardingsphere-sql-translator-provider/pom.xml |  9 +---
 .../pom.xml                                        | 17 +++---
 .../sqltranslator/jooq/JooQDialectRegistry.java    | 62 ++++++++++++++++++++++
 .../sqltranslator/jooq/JooQSQLTranslator.java      | 49 +++++++++++++++++
 ....shardingsphere.sqltranslator.spi.SQLTranslator | 18 +++++++
 5 files changed, 141 insertions(+), 14 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml
index 0ec4e9ec5db..c0e4f5f7a4a 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml
@@ -30,13 +30,6 @@
     
     <modules>
         <module>shardingsphere-sql-translator-native-provider</module>
+        <module>shardingsphere-sql-translator-jooq-provider</module>
     </modules>
-    
-    <dependencies>
-        <dependency>
-            <groupId>org.apache.shardingsphere</groupId>
-            <artifactId>shardingsphere-sql-translator-api</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-    </dependencies>
 </project>
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/pom.xml
similarity index 79%
copy from shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml
copy to shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/pom.xml
index 0ec4e9ec5db..5430b13bb90 100644
--- a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/pom.xml
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/pom.xml
@@ -21,16 +21,15 @@
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.shardingsphere</groupId>
-        <artifactId>shardingsphere-sql-translator</artifactId>
+        <artifactId>shardingsphere-sql-translator-provider</artifactId>
         <version>5.1.2-SNAPSHOT</version>
     </parent>
-    <artifactId>shardingsphere-sql-translator-provider</artifactId>
-    <packaging>pom</packaging>
+    <artifactId>shardingsphere-sql-translator-jooq-provider</artifactId>
     <name>${project.artifactId}</name>
     
-    <modules>
-        <module>shardingsphere-sql-translator-native-provider</module>
-    </modules>
+    <properties>
+        <jooq.version>3.14.15</jooq.version>
+    </properties>
     
     <dependencies>
         <dependency>
@@ -38,5 +37,11 @@
             <artifactId>shardingsphere-sql-translator-api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.jooq</groupId>
+            <artifactId>jooq</artifactId>
+            <version>${jooq.version}</version>
+        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java
new file mode 100644
index 00000000000..4569114059a
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQDialectRegistry.java
@@ -0,0 +1,62 @@
+/*
+ * 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.sqltranslator.jooq;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
+import org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
+import org.apache.shardingsphere.sqltranslator.exception.UnsupportedTranslatedDatabaseException;
+import org.jooq.SQLDialect;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * JOOQ SQL dialect registry.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class JooQDialectRegistry {
+    
+    private static final Map<DatabaseType, SQLDialect> DATABASE_DIALECT_MAP = new HashMap<>();
+    
+    static {
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("PostgreSQL"), SQLDialect.POSTGRES);
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("MySQL"), SQLDialect.MYSQL);
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("MariaDB"), SQLDialect.MARIADB);
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("openGauss"), SQLDialect.POSTGRES);
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("H2"), SQLDialect.H2);
+        DATABASE_DIALECT_MAP.put(DatabaseTypeFactory.getInstance("SQL92"), SQLDialect.DEFAULT);
+    }
+    
+    /**
+     * Get SQL dialect.
+     *
+     * @param databaseType database type
+     * @return SQL dialect
+     * @throws SQLTranslationException SQL translation exception
+     */
+    public static SQLDialect getSQLDialect(final DatabaseType databaseType) throws SQLTranslationException {
+        SQLDialect result = DATABASE_DIALECT_MAP.get(databaseType);
+        if (null == result) {
+            throw new UnsupportedTranslatedDatabaseException(databaseType);
+        }
+        return result;
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
new file mode 100644
index 00000000000..18b62ed41b6
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
@@ -0,0 +1,49 @@
+/*
+ * 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.sqltranslator.jooq;
+
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
+import org.apache.shardingsphere.sqltranslator.exception.UnsupportedTranslatedSQLException;
+import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
+import org.jooq.Query;
+import org.jooq.impl.DSL;
+
+/**
+ * JOOQ SQL translator.
+ */
+public final class JooQSQLTranslator implements SQLTranslator {
+    
+    @Override
+    public String translate(final String sql, final SQLStatement statement, final DatabaseType frontendDatabaseType, final DatabaseType backendDatabaseType) throws SQLTranslationException {
+        try {
+            Query query = DSL.using(JooQDialectRegistry.getSQLDialect(frontendDatabaseType)).parser().parseQuery(sql);
+            return DSL.using(JooQDialectRegistry.getSQLDialect(backendDatabaseType)).render(query);
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            throw new UnsupportedTranslatedSQLException(sql, ex);
+        }
+    }
+    
+    @Override
+    public String getType() {
+        return "JOOQ";
+    }
+}
diff --git a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
new file mode 100644
index 00000000000..a4a97f8e049
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/resources/META-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator
@@ -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.sqltranslator.jooq.JooQSQLTranslator