You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2017/06/27 14:31:07 UTC

syncope git commit: [SYNCOPE-1128] Now ordering table rows before export, also for 'internal' foreign keys

Repository: syncope
Updated Branches:
  refs/heads/1_2_X be7c689a5 -> 9805be634


[SYNCOPE-1128] Now ordering table rows before export, also for 'internal' foreign keys


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/9805be63
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/9805be63
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/9805be63

Branch: refs/heads/1_2_X
Commit: 9805be634ee307c343073a09860e3c931359fd35
Parents: be7c689
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Tue Jun 27 16:31:00 2017 +0200
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Tue Jun 27 16:31:00 2017 +0200

----------------------------------------------------------------------
 .../syncope/core/util/ContentExporter.java      | 92 +++++++++++++-------
 1 file changed, 62 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/9805be63/core/src/main/java/org/apache/syncope/core/util/ContentExporter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/ContentExporter.java b/core/src/main/java/org/apache/syncope/core/util/ContentExporter.java
index e7fd35b..0af230b 100644
--- a/core/src/main/java/org/apache/syncope/core/util/ContentExporter.java
+++ b/core/src/main/java/org/apache/syncope/core/util/ContentExporter.java
@@ -66,13 +66,13 @@ public class ContentExporter extends AbstractContentDealer {
 
     protected final static Set<String> TABLE_PREFIXES_TO_BE_EXCLUDED =
             new HashSet<String>(Arrays.asList(new String[] {
-                "QRTZ_", "LOGGING", "REPORTEXEC", "TASKEXEC",
-                "SYNCOPEUSER", "UATTR", "UATTRVALUE", "UATTRUNIQUEVALUE", "UDERATTR", "UVIRATTR",
-                "MEMBERSHIP", "MATTR", "MATTRVALUE", "MATTRUNIQUEVALUE", "MDERATTR", "MVIRATTR"
-            }));
-    
+        "QRTZ_", "LOGGING", "REPORTEXEC", "TASKEXEC",
+        "SYNCOPEUSER", "UATTR", "UATTRVALUE", "UATTRUNIQUEVALUE", "UDERATTR", "UVIRATTR",
+        "MEMBERSHIP", "MATTR", "MATTRVALUE", "MATTRUNIQUEVALUE", "MDERATTR", "MVIRATTR"
+    }));
+
     protected final static Set<String> TABLE_SUFFIXES_TO_BE_INCLUDED =
-            new HashSet<String>(Arrays.asList(new String[] {"TEMPLATE"}));
+            new HashSet<String>(Arrays.asList(new String[] { "TEMPLATE" }));
 
     protected static final Map<String, String> TABLES_TO_BE_FILTERED =
             Collections.singletonMap("TASK", "DTYPE <> 'PropagationTask'");
@@ -85,10 +85,10 @@ public class ContentExporter extends AbstractContentDealer {
         for (String prefix : TABLE_PREFIXES_TO_BE_EXCLUDED) {
             if (tableName.toUpperCase().startsWith(prefix)) {
                 for (String suffix : TABLE_SUFFIXES_TO_BE_INCLUDED) {
-                    if (!tableName.toUpperCase().endsWith(suffix)) {                       
+                    if (!tableName.toUpperCase().endsWith(suffix)) {
                         allowed = false;
                     }
-                }               
+                }
             }
         }
         return allowed;
@@ -217,7 +217,11 @@ public class ContentExporter extends AbstractContentDealer {
         return res;
     }
 
-    private void doExportTable(final TransformerHandler handler, final Connection conn, final String tableName,
+    private void doExportTable(
+            final TransformerHandler handler,
+            final String dbSchema,
+            final Connection conn,
+            final String tableName,
             final String whereClause) throws SQLException, SAXException {
 
         LOG.debug("Export table {}", tableName);
@@ -226,24 +230,58 @@ public class ContentExporter extends AbstractContentDealer {
 
         PreparedStatement stmt = null;
         ResultSet rs = null;
-        ResultSet pkeyRS = null;
         try {
-            // ------------------------------------
-            // retrieve primary keys to perform an ordered select
+            StringBuilder orderBy = new StringBuilder();
 
-            final DatabaseMetaData meta = conn.getMetaData();
-            pkeyRS = meta.getPrimaryKeys(null, null, tableName);
-
-            final StringBuilder orderBy = new StringBuilder();
+            DatabaseMetaData meta = conn.getMetaData();
 
-            while (pkeyRS.next()) {
-                final String columnName = pkeyRS.getString("COLUMN_NAME");
-                if (columnName != null) {
-                    if (orderBy.length() > 0) {
-                        orderBy.append(",");
+            // ------------------------------------
+            // retrieve foreign keys (linked to the same table) to perform an ordered select
+            ResultSet pkeyRS = null;
+            try {
+                pkeyRS = meta.getImportedKeys(conn.getCatalog(), dbSchema, tableName);
+                while (pkeyRS.next()) {
+                    if (tableName.equals(pkeyRS.getString("PKTABLE_NAME"))) {
+                        String columnName = pkeyRS.getString("FKCOLUMN_NAME");
+                        if (columnName != null) {
+                            if (orderBy.length() > 0) {
+                                orderBy.append(",");
+                            }
+
+                            orderBy.append(columnName);
+                        }
                     }
+                }
+            } finally {
+                if (pkeyRS != null) {
+                    try {
+                        pkeyRS.close();
+                    } catch (SQLException e) {
+                        LOG.error("While closing result set", e);
+                    }
+                }
+            }
 
-                    orderBy.append(columnName);
+            // retrieve primary keys to perform an ordered select
+            try {
+                pkeyRS = meta.getPrimaryKeys(null, null, tableName);
+                while (pkeyRS.next()) {
+                    String columnName = pkeyRS.getString("COLUMN_NAME");
+                    if (columnName != null) {
+                        if (orderBy.length() > 0) {
+                            orderBy.append(",");
+                        }
+
+                        orderBy.append(columnName);
+                    }
+                }
+            } finally {
+                if (pkeyRS != null) {
+                    try {
+                        pkeyRS.close();
+                    } catch (SQLException e) {
+                        LOG.error("While closing result set", e);
+                    }
                 }
             }
 
@@ -289,13 +327,6 @@ public class ContentExporter extends AbstractContentDealer {
                     LOG.error("While closing result set", e);
                 }
             }
-            if (pkeyRS != null) {
-                try {
-                    pkeyRS.close();
-                } catch (SQLException e) {
-                    LOG.error("While closing result set", e);
-                }
-            }
             if (stmt != null) {
                 try {
                     stmt.close();
@@ -347,7 +378,8 @@ public class ContentExporter extends AbstractContentDealer {
             // then sort tables based on foreign keys and dump
             for (String tableName : sortByForeignKeys(conn, tableNames)) {
                 try {
-                    doExportTable(handler, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase()));
+                    doExportTable(
+                            handler, dbSchema, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase()));
                 } catch (Exception e) {
                     LOG.error("Failure exporting table {}", tableName, e);
                 }