Author: adamw
Date: 2008-09-03 04:13:16 -0400 (Wed, 03 Sep 2008)
New Revision: 145
Removed:
trunk/src/main/org/jboss/envers/query/impl/Parameters.java
trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java
Log:
Cleanup
Deleted: trunk/src/main/org/jboss/envers/query/impl/Parameters.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 08:12:08 UTC
(rev 144)
+++ trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 08:13:16 UTC
(rev 145)
@@ -1,227 +0,0 @@
-/*
- * 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.query.impl;
-
-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 org.jboss.envers.query.impl.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);
- }
-}
-
Deleted: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 08:12:08 UTC
(rev 144)
+++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 08:13:16 UTC
(rev 145)
@@ -1,196 +0,0 @@
-/*
- * 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.query.impl;
-
-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 org.jboss.envers.query.impl.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;
- }
-}
Deleted: trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java 2008-09-03
08:12:08 UTC (rev 144)
+++
trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java 2008-09-03
08:13:16 UTC (rev 145)
@@ -1,59 +0,0 @@
-package org.jboss.envers.test.integration.reventity;
-
-import org.jboss.envers.RevisionNumber;
-import org.jboss.envers.RevisionTimestamp;
-import org.jboss.envers.RevisionEntity;
-
-import javax.persistence.Id;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Entity;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-@RevisionEntity
-public class CustomRevEntity {
- @Id
- @GeneratedValue
- @RevisionNumber
- private int customId;
-
- @RevisionTimestamp
- private long customTimestamp;
-
- public int getCustomId() {
- return customId;
- }
-
- public void setCustomId(int customId) {
- this.customId = customId;
- }
-
- public long getCustomTimestamp() {
- return customTimestamp;
- }
-
- public void setCustomTimestamp(long customTimestamp) {
- this.customTimestamp = customTimestamp;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof CustomRevEntity)) return false;
-
- CustomRevEntity that = (CustomRevEntity) o;
-
- if (customId != that.customId) return false;
- if (customTimestamp != that.customTimestamp) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = customId;
- result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>>
32));
- return result;
- }
-}