
dk.eobjects.metamodel.query.QueryClause Maven / Gradle / Ivy
/**
* This file is part of MetaModel.
*
* MetaModel is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MetaModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MetaModel. If not, see .
*/
package dk.eobjects.metamodel.query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
* Represents an abstract clause in a query. Clauses contains IQueryItems and
* provide basic ways of adding, modifying and removing these.
*
* @param
* the type of query item this QueryClause handles
*
* @see Query
*/
public abstract class QueryClause implements Serializable {
private static final long serialVersionUID = 3987346267433022231L;
public static final String PREFIX_SELECT = "SELECT ";
public static final String PREFIX_FROM = " FROM ";
public static final String PREFIX_WHERE = " WHERE ";
public static final String PREFIX_GROUP_BY = " GROUP BY ";
public static final String PREFIX_HAVING = " HAVING ";
public static final String PREFIX_ORDER_BY = " ORDER BY ";
public static final String DELIM_COMMA = ", ";
public static final String DELIM_AND = " AND ";
private List _items = new ArrayList();
private String _prefix;
private String _delim;
private Query _query;
public QueryClause(Query query, String prefix, String delim) {
_query = query;
_prefix = prefix;
_delim = delim;
}
public QueryClause setItems(E... items) {
_items.clear();
return addItems(items);
}
public QueryClause addItems(E... items) {
for (E item : items) {
addItem(item);
}
return this;
}
public QueryClause addItems(Collection items) {
for (E item : items) {
addItem(item);
}
return this;
}
public QueryClause addItem(E item) {
if (item.getQuery() == null) {
item.setQuery(_query);
}
_items.add(item);
return this;
}
public int getItemCount() {
return _items.size();
}
public E getItem(int index) {
return _items.get(index);
}
public List getItems() {
return _items;
}
public QueryClause removeItem(int index) {
_items.remove(index);
return this;
}
public QueryClause removeItem(E item) {
_items.remove(item);
return this;
}
public QueryClause removeItems() {
_items.clear();
return this;
}
@Override
public String toString() {
if (_items.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder(_prefix);
for (int i = 0; i < _items.size(); i++) {
E item = _items.get(i);
if (i != 0) {
sb.append(_delim);
}
sb.append(item.toString());
}
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof QueryClause>) {
QueryClause> that = (QueryClause>) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(this.getItems(), that.getItems());
return eb.isEquals();
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy