less intrusive conversion, due to failing test cases

Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/7f76973e
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/7f76973e
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/7f76973e

Branch: refs/heads/master
Commit: 7f76973edc8e9e72bd404484585c11f156766926
Parents: 2d156bc
Author: Jörg Unbehauen <jo...@unbehauen.net>
Authored: Thu Jul 27 18:18:12 2017 +0200
Committer: Jörg Unbehauen <jo...@unbehauen.net>
Committed: Thu Jul 27 18:18:12 2017 +0200

----------------------------------------------------------------------
 .../apache/metamodel/xml/XmlDomDataContext.java | 67 +++++++++++---------
 1 file changed, 38 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metamodel/blob/7f76973e/xml/src/main/java/org/apache/metamodel/xml/XmlDomDataContext.java
----------------------------------------------------------------------
diff --git a/xml/src/main/java/org/apache/metamodel/xml/XmlDomDataContext.java 
b/xml/src/main/java/org/apache/metamodel/xml/XmlDomDataContext.java
index 3c4d0cd..ad8181c 100644
--- a/xml/src/main/java/org/apache/metamodel/xml/XmlDomDataContext.java
+++ b/xml/src/main/java/org/apache/metamodel/xml/XmlDomDataContext.java
@@ -21,7 +21,11 @@ package org.apache.metamodel.xml;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -175,7 +179,7 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
     }
 
     @Override
-    public DataSet materializeMainSchemaTable(Table table, List<Column> 
columns, int maxRows) {
+    public DataSet materializeMainSchemaTable(Table table, List<Column> 
columnList, int maxRows) {
         loadSchema();
         List<Object[]> tableData = _tableData.get(table.getName());
         if (tableData == null) {
@@ -183,8 +187,9 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
                     + _tableData.keySet());
         }
 
-        final List<SelectItem> selectItems = 
columns.stream().map(SelectItem::new).collect(Collectors.toList());
+        final List<SelectItem> selectItems = 
columnList.stream().map(SelectItem::new).collect(Collectors.toList());
         final DataSetHeader header = new CachingDataSetHeader(selectItems);
+        final Column[] columns = columnList.toArray(new 
Column[columnList.size()]);
 
         final List<Row> resultData = new ArrayList<Row>();
         for (Object[] tableDataRow : tableData) {
@@ -192,9 +197,9 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
                 break;
             }
             maxRows--;
-            Object[] dataValues = new Object[columns.size()];
-            for (int i = 0; i < columns.size(); i++) {
-                Column column = columns.get(i);
+            Object[] dataValues = new Object[columns.length];
+            for (int i = 0; i < columns.length; i++) {
+                Column column = columns[i];
                 int columnNumber = column.getColumnNumber();
                 // Some rows may not contain values for all columns
                 // (attributes)
@@ -256,7 +261,7 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
 
         // Remove tables from schema that has no data (typically root
         // node or pure XML structure)
-        List<Table> tables = _schema.getTables();
+        Table[] tables = _schema.getTables().toArray(new Table[0]);
         for (Table table : tables) {
             String tableName = table.getName();
             List<Object[]> tableRows = _tableData.get(tableName);
@@ -328,7 +333,7 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
                     table.addColumn(foreignKeyColumn);
 
                     MutableRelationship.createRelationship(parentKeyColumn ,
-                            foreignKeyColumn );
+                            foreignKeyColumn);
 
                 } else {
                     foreignKeyColumn = null;
@@ -336,9 +341,9 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
             } else {
                 idColumn = getIdColumn(table);
                 tableRows = _tableData.get(tableName);
-                List<Column> foreignKeys = table.getForeignKeys();
-                if (foreignKeys.size() == 1) {
-                    foreignKeyColumn = (MutableColumn) foreignKeys.get(0);
+                Column[] foreignKeys = table.getForeignKeys().toArray(new 
Column[0]);
+                if (foreignKeys.length == 1) {
+                    foreignKeyColumn = (MutableColumn) foreignKeys[0];
                 } else {
                     foreignKeyColumn = null;
                 }
@@ -398,9 +403,8 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
     }
 
     private Column getTextContentColumn(MutableTable table, String 
preferredColumnName) {
-        List<Column> columns = table.getColumns();
         MutableColumn column = null;
-        for (Column col : columns) {
+        for (Column col : table.getColumns()) {
             if (NATIVE_TYPE_TEXT.equals(col.getNativeType())) {
                 column = (MutableColumn) col;
                 break;
@@ -417,9 +421,8 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
     }
 
     private MutableColumn getIdColumn(MutableTable table) {
-        List<Column> columns = table.getColumns();
         MutableColumn column = null;
-        for (Column col : columns) {
+        for (Column col : table.getColumns()) {
             if (NATIVE_TYPE_PRIMARY_KEY.equals(col.getNativeType())) {
                 column = (MutableColumn) col;
                 break;
@@ -504,15 +507,18 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
         // Check that foreignTable is not primary table in other relationships
         // (if so we can't flatten as that would require id-rewriting of those
         // foreign tables as well)
-        if (!foreignTable.getPrimaryKeyRelationships().isEmpty()) {
-            Collection<Relationship> foreignPrimaryRelationships = 
foreignTable.getPrimaryKeyRelationships();
+        if (foreignTable.getPrimaryKeyRelationships().size() != 0) {
+            Relationship[] foreignPrimaryRelationships = 
foreignTable.getPrimaryKeyRelationships().toArray(new Relationship[0]);
+            String[] foreignPrimaryNames = new 
String[foreignPrimaryRelationships.length];
+            for (int i = 0; i < foreignPrimaryRelationships.length; i++) {
+                foreignPrimaryNames[i] = 
foreignPrimaryRelationships[i].getForeignTable().getName();
+            }
             throw new UnsupportedOperationException("Cannot flatten foreign 
table '" + foreignTable.getName()
-                    + "' as it acts as primary table for tables: " + 
foreignPrimaryRelationships.stream()
-                    .map(ftRel -> 
ftRel.getForeignTable().getName()).collect(Collectors.joining(", ")));
+                    + "' as it acts as primary table for tables: " + 
Arrays.toString(foreignPrimaryNames));
         }
 
-        List<Column> primaryColumns = new 
ArrayList<>(primaryTable.getColumns());
-        List<Column> foreignColumns = new 
ArrayList<>(foreignTable.getColumns());
+        List<Column> primaryColumns = new 
ArrayList<Column>(primaryTable.getColumns());
+        List<Column> foreignColumns = new 
ArrayList<Column>(foreignTable.getColumns());
 
         // Remove the surrogate id
         String primaryTableName = primaryTable.getName();
@@ -521,7 +527,8 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
         foreignColumns.remove(idColumn);
 
         // Remove the foreign keys
-        for (Column foreignKey : foreignTable.getForeignKeys()) {
+        Column[] foreignKeys = foreignTable.getForeignKeys().toArray(new 
Column[0]);
+        for (Column foreignKey : foreignKeys) {
             foreignColumns.remove(foreignKey);
         }
 
@@ -567,20 +574,22 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
      * mapped tables).
      */
     public XmlDomDataContext autoFlattenTables() {
-        for (Table table : _schema.getTables()) {
+        Table[] tables = _schema.getTables().toArray(new Table[0]);
+        for (Table table : tables) {
             // First check to see that this table still exist (ie. has not been
             // flattened in a previous loop)
             if (_tableData.containsKey(table.getName())) {
                 // Find all tables that represent inner tags
-                Collection<Relationship> foreignKeyRelationships = 
table.getForeignKeyRelationships();
-                if (foreignKeyRelationships.size() == 1 && 
table.getPrimaryKeyRelationships().size() == 0) {
-                    Relationship foreignKeyRelationship = 
foreignKeyRelationships.iterator().next();
+                Relationship[] foreignKeyRelationships = 
table.getForeignKeyRelationships().toArray(new Relationship[0]);
+                if (foreignKeyRelationships.length == 1 && 
table.getPrimaryKeyRelationships().size() == 0) {
+                    Relationship foreignKeyRelationship = 
foreignKeyRelationships[0];
 
                     // If there is exactly one inner tag then we can probably
                     // flatten the tables, but it's only relevant if the inner
                     // tag only carry a single data column
                     int nonDataColumns = 0;
-                    for (Column column : table.getColumns()) {
+                    Column[] columns = table.getColumns().toArray(new 
Column[0]);
+                    for (Column column : columns) {
                         String nativeType = column.getNativeType();
                         // Use the native column type constants to determine if
                         // the column is an artificial column
@@ -589,14 +598,14 @@ public class XmlDomDataContext extends 
QueryPostprocessDataContext {
                         }
                     }
 
-                    if (table.getColumns().size() == nonDataColumns + 1) {
+                    if (columns.length == nonDataColumns + 1) {
                         // If the foreign key is unique for all rows, we will
                         // flatten it (otherwise it means that multiple inner
                         // tags occur, which requires two tables to deal with
                         // multiplicity)
                         boolean uniqueForeignKeys = true;
 
-                        List<Column> foreignColumns = 
foreignKeyRelationship.getForeignColumns();
+                        Column[] foreignColumns = 
foreignKeyRelationship.getForeignColumns().toArray(new Column[0]);
 
                         SelectItem countAllItem = SelectItem.getCountAllItem();
                         Query q = new 
Query().select(foreignColumns).select(countAllItem).from(table).groupBy(

Reply via email to