You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/09/29 07:23:48 UTC

[isis] branch master updated: ISIS-2439: fix Create Schema syntax for MariaDB/MySql

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

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 57f0daa  ISIS-2439: fix Create Schema syntax for MariaDB/MySql
57f0daa is described below

commit 57f0daa115b138d3cfbef2c6e30640eac2f1b3d0
Author: Andi Huber <ah...@apache.org>
AuthorDate: Tue Sep 29 09:23:29 2020 +0200

    ISIS-2439: fix Create Schema syntax for MariaDB/MySql
---
 .../CreateSchemaObjectFromClassMetadata.java       | 68 +++++++++++++---------
 1 file changed, 40 insertions(+), 28 deletions(-)

diff --git a/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/datanucleus/CreateSchemaObjectFromClassMetadata.java b/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/datanucleus/CreateSchemaObjectFromClassMetadata.java
index 30fd9a0..821fbab 100644
--- a/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/datanucleus/CreateSchemaObjectFromClassMetadata.java
+++ b/persistence/jdo/datanucleus-5/src/main/java/org/apache/isis/persistence/jdo/datanucleus5/datanucleus/CreateSchemaObjectFromClassMetadata.java
@@ -24,6 +24,8 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Stream;
 
 import org.datanucleus.ClassLoaderResolver;
 import org.datanucleus.enhancer.EnhancementNucleusContextImpl;
@@ -33,6 +35,8 @@ import org.datanucleus.store.ConnectionEncryptionProvider;
 
 import org.apache.isis.commons.internal.base._Strings;
 
+import lombok.RequiredArgsConstructor;
+import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
 /**
@@ -122,45 +126,53 @@ public class CreateSchemaObjectFromClassMetadata implements MetaDataListener, Da
 
     protected String buildSqlToExec(final AbstractClassMetaData cmd) {
         final String schemaName = schemaNameFor(cmd);
-        return String.format("CREATE SCHEMA \"%s\"", schemaName);
+        final String schemaCreateSqlSyntax = schemaCreateSqlSyntaxFor(cmd);
+        return String.format(schemaCreateSqlSyntax, schemaName);
     }
 
     /**
      * Determine the name of the schema.
      */
     protected String schemaNameFor(final AbstractClassMetaData cmd) {
-        String schemaName = cmd.getSchema();
-
-        // DN uses different casing for identifiers.
-        //
-        // http://www.datanucleus.org/products/accessplatform_3_2/jdo/orm/datastore_identifiers.html
-        // http://www.datanucleus.org/products/accessplatform_4_0/jdo/orm/datastore_identifiers.html
-        //
-        // the following attempts to accommodate heuristically for the "out-of-the-box" behaviour for three common
-        // db vendors without requiring lots of complex configuration of DataNucleus
-        //
-
-        String url = getPropertyAsString("javax.jdo.option.ConnectionURL");
-
-        if(url.contains("postgres")) {
-            // in DN 4.0, was forcing lower case:
-            // schemaName = schemaName.toLowerCase(Locale.ROOT);
-
-            // in DN 4.1, am guessing that may be ok to leave unchaged (quoted identifiers?)
+        return cmd.getSchema();
+    }
+    
+    /**
+     * Determine the schema creation SQL syntax.
+     */
+    protected String schemaCreateSqlSyntaxFor(final AbstractClassMetaData cmd) {
+        val jdbcVariant = JdbcVariant.detect(getPropertyAsString("javax.jdo.option.ConnectionURL"));
+        switch (jdbcVariant) {
+        case MYSQL:
+            //XXX [ISIS-2439]
+            return "CREATE SCHEMA `%s`";
+        case POSTGRES:
+        case HSQLDB:
+        case SQLSERVER:
+        case OTHER:
+        default:
+            return "CREATE SCHEMA \"%s\"";
         }
-        if(url.contains("hsqldb")) {
-            // in DN 4.0, was forcing upper case:
-            // schemaName = schemaName.toUpperCase(Locale.ROOT);
+    }
 
-            // in DN 4.1, seems to be ok to leave as unchanged (is quoted identifiers what makes this work?)
+    @RequiredArgsConstructor
+    private static enum JdbcVariant {
+        POSTGRES(url->url.startsWith("jdbc:postgres:")),
+        HSQLDB(url->url.startsWith("jdbc:hsqldb:")),
+        SQLSERVER(url->url.startsWith("jdbc:sqlserver:")),
+        MYSQL(url->url.startsWith("jdbc:mysql:") 
+                || url.startsWith("jdbc:mariadb:")),
+        OTHER(url->true)
+        ;
+        final Predicate<String> matcher;
+        static JdbcVariant detect(String connectionUrl) {
+            return Stream.of(JdbcVariant.values())
+            .filter(variant->variant.matcher.test(connectionUrl))
+            .findFirst()
+            .orElse(OTHER);
         }
-        if(url.contains("sqlserver")) {
-            // unchanged
-        }
-        return schemaName;
     }
 
-
     // -- helpers: closeSafely, getConnectionPassword
     protected void closeSafely(final AutoCloseable connection) {
         if(connection != null) {