From 3670bafb9f763ff25941de18a31d62325c7434df Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 19 Mar 2004 17:02:18 +0000 Subject: [PATCH] Fixes in Relation.java: * Make renderConstraints() do the right thing in regard to logicalOperator, extracting the code from buildQuery() * Make checkConstraints() do the right thing in regard to logicalOperator. * Make setConstraints() do the right thin in regard to logicalOperator, i.e. don't touch if logicalOperator is OR or XOR. --- src/helma/objectmodel/db/NodeManager.java | 2 +- src/helma/objectmodel/db/Relation.java | 80 +++++++++++++---------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/helma/objectmodel/db/NodeManager.java b/src/helma/objectmodel/db/NodeManager.java index 21ee8c4b..baf8ffa5 100644 --- a/src/helma/objectmodel/db/NodeManager.java +++ b/src/helma/objectmodel/db/NodeManager.java @@ -1212,7 +1212,7 @@ public final class NodeManager { dbm.addJoinConstraints(q, " AND "); if (rel.groupby != null) { - q.append(rel.renderConstraints(home, home.getNonVirtualParent())); + rel.renderConstraints(q, home, home.getNonVirtualParent(), " AND "); if (rel.order != null) { q.append(" ORDER BY "); diff --git a/src/helma/objectmodel/db/Relation.java b/src/helma/objectmodel/db/Relation.java index 8921228d..1c853e51 100644 --- a/src/helma/objectmodel/db/Relation.java +++ b/src/helma/objectmodel/db/Relation.java @@ -779,29 +779,13 @@ public final class Relation { prefix = " AND "; } - if (constraints.length > 1 && logicalOperator != AND) { - q.append(prefix); - q.append("("); - prefix = ""; - } - - for (int i = 0; i < constraints.length; i++) { - q.append(prefix); - constraints[i].addToQuery(q, home, nonvirtual); - prefix = logicalOperator; - } - - if (constraints.length > 1 && logicalOperator != AND) { - q.append(")"); - prefix = " AND "; - } - - if (filter != null) { - appendFilter(q, nonvirtual, prefix); - } + // render the constraints and filter + renderConstraints(q, home, nonvirtual, prefix); + // add joined fetch constraints ownType.addJoinConstraints(q, prefix); + // add group and order clauses if (groupby != null) { q.append(" GROUP BY " + groupby); @@ -885,30 +869,39 @@ public final class Relation { } /** + * Render contraints and filter conditions to an SQL query string buffer * - * - * @param home ... - * @param nonvirtual ... - * - * @return ... + * @param q the query string + * @param home our home node + * @param nonvirtual our non-virtual home node + * @param prefix the prefix to use to append to the existing query (e.g. " AND ") * * @throws SQLException ... */ - public String renderConstraints(INode home, INode nonvirtual) + public void renderConstraints(StringBuffer q, INode home, + INode nonvirtual, String prefix) throws SQLException { - StringBuffer q = new StringBuffer(); - String prefix = " AND "; + + if (constraints.length > 1 && logicalOperator != AND) { + q.append(prefix); + q.append("("); + prefix = ""; + } for (int i = 0; i < constraints.length; i++) { q.append(prefix); constraints[i].addToQuery(q, home, nonvirtual); + prefix = logicalOperator; + } + + if (constraints.length > 1 && logicalOperator != AND) { + q.append(")"); + prefix = " AND "; } if (filter != null) { appendFilter(q, nonvirtual, prefix); } - - return q.toString(); } /** @@ -982,6 +975,10 @@ public final class Relation { return false; } + // counter for constraints and satisfied constraints + int count = 0; + int satisfied = 0; + INode nonvirtual = parent.getNonVirtualParent(); for (int i = 0; i < constraints.length; i++) { @@ -1000,13 +997,22 @@ public final class Relation { value = home.getString(constraints[i].localName); } - if ((value != null) && !value.equals(child.getString(propname))) { - return false; + count++; + + if (value != null && value.equals(child.getString(propname))) { + satisfied++; } } } - return true; + // check if enough constraints are met depending on logical operator + if (logicalOperator == OR) { + return satisfied > 0; + } else if (logicalOperator == XOR) { + return satisfied == 1; + } else { + return satisfied == count; + } } /** @@ -1014,7 +1020,15 @@ public final class Relation { * appropriate properties */ public void setConstraints(Node parent, Node child) { + + // if logical operator is OR or XOR we just return because we + // wouldn't know what to do anyway + if (logicalOperator != AND) { + return; + } + Node home = parent.getNonVirtualParent(); + for (int i = 0; i < constraints.length; i++) { // don't set groupby constraints since we don't know if the // parent node is the base node or a group node