Author: adamw
Date: 2008-09-03 04:12:08 -0400 (Wed, 03 Sep 2008)
New Revision: 144
Added:
trunk/src/main/org/jboss/envers/tools/query/
trunk/src/main/org/jboss/envers/tools/query/Parameters.java
trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java
trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java
trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java
trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java
trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java
Log:
ENVERS-47: moving query builder to tools
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -21,7 +21,7 @@
*/
package org.jboss.envers.entities.mapper.id;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Iterator;
import java.util.List;
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-03 08:10:24
UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-03 08:12:08
UTC (rev 144)
@@ -21,7 +21,7 @@
*/
package org.jboss.envers.entities.mapper.id;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
import java.util.List;
Modified:
trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import java.util.List;
import java.util.ArrayList;
Modified: trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified:
trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -23,8 +23,8 @@
import org.jboss.envers.entities.RelationDescription;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -23,8 +23,8 @@
import org.jboss.envers.entities.RelationDescription;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -24,8 +24,8 @@
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.entities.RelationDescription;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java
===================================================================
---
trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-09-03
08:10:24 UTC (rev 143)
+++
trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -24,8 +24,8 @@
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.entities.RelationDescription;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import java.util.ArrayList;
import java.util.List;
Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
/**
* @author Adam Warski (adam at warski dot org)
Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -22,8 +22,8 @@
package org.jboss.envers.query.criteria;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.query.impl.QueryBuilder;
-import org.jboss.envers.query.impl.Parameters;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import java.util.List;
import java.util.ArrayList;
Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -31,6 +31,7 @@
import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.tools.Pair;
import org.jboss.envers.tools.Triple;
+import org.jboss.envers.tools.query.QueryBuilder;
import org.hibernate.*;
import javax.persistence.NonUniqueResultException;
Modified: trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-03
08:10:24 UTC (rev 143)
+++ trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-03
08:12:08 UTC (rev 144)
@@ -24,6 +24,7 @@
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.query.criteria.VersionsCriterion;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.tools.query.QueryBuilder;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.configuration.VersionsConfiguration;
Copied: trunk/src/main/org/jboss/envers/tools/query/Parameters.java (from rev 143,
trunk/src/main/org/jboss/envers/query/impl/Parameters.java)
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/Parameters.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-03 08:12:08 UTC
(rev 144)
@@ -0,0 +1,227 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ *
+ * See the copyright.txt in the distribution for a full listing of individual
+ * contributors. This copyrighted material is made available to anyone wishing
+ * to use, modify, copy, or redistribute it subject to the terms and
+ * conditions of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT A WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.tools.query;
+
+import org.jboss.envers.tools.MutableInteger;
+import org.jboss.envers.tools.MutableBoolean;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Parameters of a query, built using {@link QueryBuilder}.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class Parameters {
+ public final static String AND = "and";
+ public final static String OR = "or";
+
+ /**
+ * Main alias of the entity.
+ */
+ private final String alias;
+ /**
+ * Connective between these parameters - "and" or "or".
+ */
+ private final String connective;
+ /**
+ * For use by the parameter generator. Must be the same in all "child" (and
parent) parameters.
+ */
+ private final MutableInteger queryParamCounter;
+
+ /**
+ * A list of sub-parameters (parameters with a different connective).
+ */
+ private final List<Parameters> subParameters;
+ /**
+ * A list of negated parameters.
+ */
+ private final List<Parameters> negatedParameters;
+ /**
+ * A list of complete where-expressions.
+ */
+ private final List<String> expressions;
+ /**
+ * Values of parameters used in expressions.
+ */
+ private final Map<String, Object> localQueryParamValues;
+
+ Parameters(String alias, String connective, MutableInteger queryParamCounter) {
+ this.alias = alias;
+ this.connective = connective;
+ this.queryParamCounter = queryParamCounter;
+
+ subParameters = new ArrayList<Parameters>();
+ negatedParameters = new ArrayList<Parameters>();
+ expressions = new ArrayList<String>();
+ localQueryParamValues = new HashMap<String, Object>();
+ }
+
+ private String generateQueryParam() {
+ return "_p" + queryParamCounter.getAndIncrease();
+ }
+
+ /**
+ * Adds sub-parameters with a new connective. That is, the parameters will be grouped
in parentheses in the
+ * generated query, e.g.: ... and (exp1 or exp2) and ..., assuming the old connective
is "and", and the
+ * new connective is "or".
+ * @param newConnective New connective of the parameters.
+ * @return Sub-parameters with the given connective.
+ */
+ public Parameters addSubParameters(String newConnective) {
+ if (connective.equals(newConnective)) {
+ return this;
+ } else {
+ Parameters newParams = new Parameters(alias, newConnective,
queryParamCounter);
+ subParameters.add(newParams);
+ return newParams;
+ }
+ }
+
+ /**
+ * Adds negated parameters, by default with the "and" connective. These
paremeters will be grouped in parentheses
+ * in the generated query and negated, e.g. ... not (exp1 and exp2) ...
+ * @return Negated sub paremters.
+ */
+ public Parameters addNegatedParameters() {
+ Parameters newParams = new Parameters(alias, AND, queryParamCounter);
+ negatedParameters.add(newParams);
+ return newParams;
+ }
+
+ public void addWhere(String left, String op, String right) {
+ addWhere(left, true, op, right, true);
+ }
+
+ public void addWhere(String left, boolean addAliasLeft, String op, String right,
boolean addAliasRight) {
+ StringBuilder expression = new StringBuilder();
+
+ if (addAliasLeft) { expression.append(alias).append("."); }
+ expression.append(left);
+
+ expression.append(" ").append(op).append(" ");
+
+ if (addAliasRight) { expression.append(alias).append("."); }
+ expression.append(right);
+
+ expressions.add(expression.toString());
+ }
+
+ public void addWhereWithParam(String left, String op, Object paramValue) {
+ addWhereWithParam(left, true, op, paramValue);
+ }
+
+ public void addWhereWithParam(String left, boolean addAlias, String op, Object
paramValue) {
+ StringBuilder expression = new StringBuilder();
+
+ if (addAlias) { expression.append(alias).append("."); }
+ expression.append(left);
+
+ expression.append(" ").append(op).append(" ");
+
+ String paramName = generateQueryParam();
+ localQueryParamValues.put(paramName, paramValue);
+ expression.append(":").append(paramName);
+
+ expressions.add(expression.toString());
+ }
+
+ public void addWhereWithParams(String left, String opStart, Object[] paramValues,
String opEnd) {
+ StringBuilder expression = new StringBuilder();
+
+ expression.append(alias).append(".").append(left).append("
").append(opStart);
+
+ for (int i=0; i<paramValues.length; i++) {
+ Object paramValue = paramValues[i];
+ String paramName = generateQueryParam();
+ localQueryParamValues.put(paramName, paramValue);
+ expression.append(":").append(paramName);
+
+ if (i != paramValues.length-1) {
+ expression.append(", ");
+ }
+ }
+
+ expression.append(opEnd);
+
+ expressions.add(expression.toString());
+ }
+
+ public void addWhere(String left, String op, QueryBuilder right) {
+ StringBuilder expression = new StringBuilder();
+
+ expression.append(alias).append(".").append(left);
+
+ expression.append(" ").append(op).append(" ");
+
+ expression.append("(");
+ right.build(expression, localQueryParamValues);
+ expression.append(")");
+
+ expressions.add(expression.toString());
+ }
+
+ private void append(StringBuilder sb, String toAppend, MutableBoolean isFirst) {
+ if (!isFirst.isSet()) {
+ sb.append(" ").append(connective).append(" ");
+ }
+
+ sb.append(toAppend);
+
+ isFirst.unset();
+ }
+
+ boolean isEmpty() {
+ return expressions.size() == 0 && subParameters.size() == 0 &&
negatedParameters.size() == 0;
+ }
+
+ void build(StringBuilder sb, Map<String, Object> queryParamValues) {
+ MutableBoolean isFirst = new MutableBoolean(true);
+
+ for (String expression : expressions) {
+ append(sb, expression, isFirst);
+ }
+
+ for (Parameters sub : subParameters) {
+ if (!subParameters.isEmpty()) {
+ append(sb, "(", isFirst);
+ sub.build(sb, queryParamValues);
+ sb.append(")");
+ }
+ }
+
+ for (Parameters negated : negatedParameters) {
+ if (!negatedParameters.isEmpty()) {
+ append(sb, "not (", isFirst);
+ negated.build(sb, queryParamValues);
+ sb.append(")");
+ }
+ }
+
+ queryParamValues.putAll(localQueryParamValues);
+ }
+}
+
Copied: trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java (from rev 143,
trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java)
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-03 08:12:08 UTC
(rev 144)
@@ -0,0 +1,196 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ *
+ * See the copyright.txt in the distribution for a full listing of individual
+ * contributors. This copyrighted material is made available to anyone wishing
+ * to use, modify, copy, or redistribute it subject to the terms and
+ * conditions of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT A WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.tools.query;
+
+import org.jboss.envers.tools.MutableInteger;
+import org.jboss.envers.tools.Pair;
+import org.jboss.envers.tools.StringTools;
+
+import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * A class for incrementaly building a HQL query.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class QueryBuilder {
+ private final String entityName;
+ private final String alias;
+
+ /**
+ * For use by alias generator (in case an alias is not provided by the user).
+ */
+ private final MutableInteger aliasCounter;
+ /**
+ * For use by parameter generator, in {@link Parameters}. This counter must be
+ * the same in all parameters and sub-queries of this query.
+ */
+ private final MutableInteger paramCounter;
+ /**
+ * Main "where" parameters for this query.
+ */
+ private final Parameters rootParameters;
+
+ /**
+ * A list of pairs (from entity name, alias name).
+ */
+ private final List<Pair<String, String>> froms;
+ /**
+ * A list of pairs (property name, order ascending?).
+ */
+ private final List<Pair<String, Boolean>> orders;
+ /**
+ * A list of complete projection definitions: either a sole property name, or a
function(property name).
+ */
+ private final List<String> projections;
+
+ /**
+ *
+ * @param entityName Main entity which should be selected.
+ * @param alias Alias of the entity
+ */
+ public QueryBuilder(String entityName, String alias) {
+ this(entityName, alias, new MutableInteger(), new MutableInteger());
+ }
+
+ private QueryBuilder(String entityName, String alias, MutableInteger aliasCounter,
MutableInteger paramCounter) {
+ this.entityName = entityName;
+ this.alias = alias;
+ this.aliasCounter = aliasCounter;
+ this.paramCounter = paramCounter;
+
+ rootParameters = new Parameters(alias, "and", paramCounter);
+
+ froms = new ArrayList<Pair<String, String>>();
+ orders = new ArrayList<Pair<String, Boolean>>();
+ projections = new ArrayList<String>();
+
+ addFrom(entityName, alias);
+ }
+
+ /**
+ * Add an entity from which to select.
+ * @param entityName Name of the entity from which to select.
+ * @param alias Alias of the entity. Should be different than all other aliases.
+ */
+ public void addFrom(String entityName, String alias) {
+ froms.add(Pair.make(entityName, alias));
+ }
+
+ private String generateAlias() {
+ return "_e" + aliasCounter.getAndIncrease();
+ }
+
+ /**
+ * @return A sub-query builder for the same entity (with an auto-generated alias).
The sub-query can
+ * be later used as a value of a parameter.
+ */
+ public QueryBuilder newSubQueryBuilder() {
+ return newSubQueryBuilder(entityName, generateAlias());
+ }
+
+ /**
+ * @param entityName Entity name, which will be the main entity for the sub-query.
+ * @param alias Alias of the entity, which can later be used in parameters.
+ * @return A sub-query builder for the given entity, with the given alias. The
sub-query can
+ * be later used as a value of a parameter.
+ */
+ public QueryBuilder newSubQueryBuilder(String entityName, String alias) {
+ return new QueryBuilder(entityName, alias, aliasCounter, paramCounter);
+ }
+
+ public Parameters getRootParameters() {
+ return rootParameters;
+ }
+
+ public void addOrder(String propertyName, boolean ascending) {
+ orders.add(Pair.make(propertyName, ascending));
+ }
+
+ public void addProjection(String function, String propertyName, boolean distinct) {
+ if (function == null) {
+ projections.add((distinct ? "distinct " : "") + alias +
"." + propertyName);
+ } else {
+ projections.add(function + "(" + (distinct ? "distinct "
: "") + alias + "." + propertyName + ")");
+ }
+ }
+
+ /**
+ * Builds the given query, appending results to the given string buffer, and adding
all query parameter values
+ * that are used to the map provided.
+ * @param sb String builder to which the query will be appended.
+ * @param queryParamValues Values of parameters used in the query.
+ */
+ public void build(StringBuilder sb, Map<String, Object> queryParamValues) {
+ sb.append("select ");
+ if (projections.size() > 0) {
+ // all projections separated with commas
+ StringTools.append(sb, projections.iterator(), ", ");
+ } else {
+ // all aliases separated with commas
+ StringTools.append(sb, getAliasList().iterator(), ", ");
+ }
+ sb.append(" from ");
+ // all from entities with aliases, separated with commas
+ StringTools.append(sb, getFromList().iterator(), ", ");
+ // where part - rootParameters
+ if (!rootParameters.isEmpty()) {
+ sb.append(" where ");
+ rootParameters.build(sb, queryParamValues);
+ }
+ // orders
+ if (orders.size() > 0) {
+ sb.append(" order by ");
+ StringTools.append(sb, getOrderList().iterator(), ", ");
+ }
+ }
+
+ private List<String> getAliasList() {
+ List<String> aliasList = new ArrayList<String>();
+ for (Pair<String, String> from : froms) {
+ aliasList.add(from.getSecond());
+ }
+
+ return aliasList;
+ }
+
+ private List<String> getFromList() {
+ List<String> fromList = new ArrayList<String>();
+ for (Pair<String, String> from : froms) {
+ fromList.add(from.getFirst() + " " + from.getSecond());
+ }
+
+ return fromList;
+ }
+
+ private List<String> getOrderList() {
+ List<String> orderList = new ArrayList<String>();
+ for (Pair<String, Boolean> order : orders) {
+ orderList.add(alias + "." + order.getFirst() + " " +
(order.getSecond() ? "asc" : "desc"));
+ }
+
+ return orderList;
+ }
+}