From a9f1797258696b1f9e1a425a69433c2f98272dc8 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 28 May 2003 18:01:23 +0000 Subject: [PATCH] Fetch joined nodes in dedicated method, fixing a number of bugs related to reading to the resultset after ResultSet.next() has been called. --- src/helma/objectmodel/db/NodeManager.java | 118 ++++++---------------- 1 file changed, 33 insertions(+), 85 deletions(-) diff --git a/src/helma/objectmodel/db/NodeManager.java b/src/helma/objectmodel/db/NodeManager.java index 6d01a038..80f6273c 100644 --- a/src/helma/objectmodel/db/NodeManager.java +++ b/src/helma/objectmodel/db/NodeManager.java @@ -1134,27 +1134,7 @@ public final class NodeManager { } } - int resultSetOffset = columns.length; - // create joined objects - for (int i = 0; i < joins.length; i++) { - DbMapping jdbm = joins[i].otherType; - node = createNode(jdbm, rs, jdbm.getColumns(), resultSetOffset); - if (node != null) { - primKey = node.getKey(); - // register new nodes with the cache. If an up-to-date copy - // existed in the cache, use that. - synchronized (cache) { - Node oldnode = (Node) cache.put(primKey, node); - - if ((oldnode != null) && - (oldnode.getState() != INode.INVALID)) { - // found an ok version in the cache, use it. - cache.put(primKey, oldnode); - } - } - } - resultSetOffset += jdbm.getColumns().length; - } + fetchJoinedNodes(rs, joins, columns.length); } } finally { @@ -1308,27 +1288,7 @@ public final class NodeManager { } } - int resultSetOffset = columns.length; - // create joined objects - for (int i = 0; i < joins.length; i++) { - DbMapping jdbm = joins[i].otherType; - node = createNode(jdbm, rs, jdbm.getColumns(), resultSetOffset); - if (node != null) { - primKey = node.getKey(); - // register new nodes with the cache. If an up-to-date copy - // existed in the cache, use that. - synchronized (cache) { - Node oldnode = (Node) cache.put(primKey, node); - - if ((oldnode != null) && - (oldnode.getState() != INode.INVALID)) { - // found an ok version in the cache, use it. - cache.put(primKey, oldnode); - } - } - } - resultSetOffset += jdbm.getColumns().length; - } + fetchJoinedNodes(rs, joins, columns.length); } // If these are grouped nodes, build the intermediary group nodes @@ -1540,27 +1500,7 @@ public final class NodeManager { node = createNode(dbm, rs, columns, 0); - int resultSetOffset = columns.length; - // create joined objects - for (int i = 0; i < joins.length; i++) { - DbMapping jdbm = joins[i].otherType; - node = createNode(jdbm, rs, jdbm.getColumns(), resultSetOffset); - if (node != null) { - Key primKey = node.getKey(); - // register new nodes with the cache. If an up-to-date copy - // existed in the cache, use that. - synchronized (cache) { - Node oldnode = (Node) cache.put(primKey, node); - - if ((oldnode != null) && - (oldnode.getState() != INode.INVALID)) { - // found an ok version in the cache, use it. - cache.put(primKey, oldnode); - } - } - } - resultSetOffset += jdbm.getColumns().length; - } + fetchJoinedNodes(rs, joins, columns.length); if (rs.next()) { throw new RuntimeException("More than one value returned by query."); @@ -1651,6 +1591,8 @@ public final class NodeManager { node = createNode(rel.otherType, rs, columns, 0); + fetchJoinedNodes(rs, joins, columns.length); + if (rs.next()) { throw new RuntimeException("More than one value returned by query."); } @@ -1665,28 +1607,6 @@ public final class NodeManager { } } - int resultSetOffset = columns.length; - // create joined objects - for (int i = 0; i < joins.length; i++) { - DbMapping jdbm = joins[i].otherType; - node = createNode(jdbm, rs, jdbm.getColumns(), resultSetOffset); - if (node != null) { - Key primKey = node.getKey(); - // register new nodes with the cache. If an up-to-date copy - // existed in the cache, use that. - synchronized (cache) { - Node oldnode = (Node) cache.put(primKey, node); - - if ((oldnode != null) && - (oldnode.getState() != INode.INVALID)) { - // found an ok version in the cache, use it. - cache.put(primKey, oldnode); - } - } - } - resultSetOffset += jdbm.getColumns().length; - } - } finally { if (stmt != null) { try { @@ -1886,6 +1806,34 @@ public final class NodeManager { return node; } + /** + * Fetch nodes that are fetched additionally to another node via join. + */ + private void fetchJoinedNodes(ResultSet rs, Relation[] joins, int offset) + throws ClassNotFoundException, SQLException, IOException { + int resultSetOffset = offset; + // create joined objects + for (int i = 0; i < joins.length; i++) { + DbMapping jdbm = joins[i].otherType; + Node node = createNode(jdbm, rs, jdbm.getColumns(), resultSetOffset); + if (node != null) { + Key primKey = node.getKey(); + // register new nodes with the cache. If an up-to-date copy + // existed in the cache, use that. + synchronized (cache) { + Node oldnode = (Node) cache.put(primKey, node); + + if ((oldnode != null) && + (oldnode.getState() != INode.INVALID)) { + // found an ok version in the cache, use it. + cache.put(primKey, oldnode); + } + } + } + resultSetOffset += jdbm.getColumns().length; + } + } + /** * Get a DbMapping for a given prototype name. This is just a proxy