diff options
2 files changed, 57 insertions, 8 deletions
diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java index 484d0c54c3..49ed64a922 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java @@ -57,6 +57,7 @@ public class MappingWrapper { // TUSCANY-2288 private List insertOrder; private List deleteOrder; + private Set rootTableNames; // -- @@ -608,6 +609,45 @@ public class MappingWrapper { return results; } + public Set getRootTableNames() { + if (this.logger.isDebugEnabled()) { + this.logger.debug("Getting root tables"); + } + if (rootTableNames == null) { + rootTableNames = new HashSet(); + if (config == null) return rootTableNames; + // parse all relationships + Set allParents = new HashSet(); + Set allChildren = new HashSet(); + Iterator i = getConfig().getRelationship().iterator(); + while (i.hasNext()) { + Relationship r = (Relationship) i.next(); + String parent = r.getPrimaryKeyTable(); + String child = r.getForeignKeyTable(); + if (parent.equals(child)) { + // self-relationship + // do not add to all children list to allow root detection + allParents.add(parent); + } else { + allParents.add(parent); + allChildren.add(child); + } + } + // find roots (depth 0) + // roots are tables that are present in the parents set, but not in the children set + for (Iterator itParents = allParents.iterator(); itParents.hasNext(); ) { + String parent = (String) itParents.next(); + if (!allChildren.contains(parent)) { + rootTableNames.add(parent); + } + } + } + if (this.logger.isDebugEnabled()) { + this.logger.debug(rootTableNames); + } + return rootTableNames; + } + // TUSCANY-2288 public List getInsertOrder() { if (this.logger.isDebugEnabled()) { diff --git a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java index 431af18d62..03a77c000f 100644 --- a/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java +++ b/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java @@ -21,6 +21,7 @@ package org.apache.tuscany.das.rdb.graphbuilder.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; +import java.util.Set; import org.apache.log4j.Logger; @@ -91,14 +92,16 @@ public class ResultSetProcessor { if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) { while (rs.next() && start < end) { rsr.processRow(rs); - addRowToGraph(rsr, rsMetadata); - ++start; + int rootRowsCreated = addRowToGraph(rsr, rsMetadata); + start += rootRowsCreated; } } else { - while (rs.absolute(start) && start < end) { + int position = start; + while (rs.absolute(position) && start < end) { rsr.processRow(rs); - addRowToGraph(rsr, rsMetadata); - ++start; + int rootRowsCreated = addRowToGraph(rsr, rsMetadata); + start += rootRowsCreated; + ++position; } } } @@ -106,8 +109,11 @@ public class ResultSetProcessor { /** * @param row * @param resultMetadata + * @return the number of root rows created */ - private void addRowToGraph(ResultSetRow row, ResultMetadata resultMetadata) { + private int addRowToGraph(ResultSetRow row, ResultMetadata resultMetadata) throws SQLException { + int rootRowsCreated = 0; + Set rootTableNames = metadata.getConfigWrapper().getRootTableNames(); tableObjects.clear(); Iterator tables = row.getAllTableData().iterator(); while (tables.hasNext()) { @@ -124,12 +130,13 @@ public class ResultSetProcessor { String tableName = rawDataFromRow.getTableName(); DataObject tableObject = registry.get(tableName, rawDataFromRow.getPrimaryKeyValues()); boolean newlyCreated = (tableObject == null); + // check whether row is a new root row + if (newlyCreated && rootTableNames.contains(tableName)) rootRowsCreated++; if (newlyCreated && !rawDataFromRow.hasNullPrimaryKey()) {//2nd check for null data in PK, //as TableData.addData() - hasValidPrimaryKey=false is commented for a reason //with this, DataObjs with null PK will not be added to registry and tableObjects - tableObject = doMaker.createAndAddDataObject(rawDataFromRow, resultMetadata); - + tableObject = doMaker.createAndAddDataObject(rawDataFromRow, resultMetadata); if (this.logger.isDebugEnabled()) { this.logger.debug("Putting table " + tableName + " with PK " + rawDataFromRow.getPrimaryKeyValues() + " into registry"); @@ -152,6 +159,8 @@ public class ResultSetProcessor { } tableObjects.processRelationships(); + + return rootRowsCreated; } |