All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.query.QueryClause Maven / Gradle / Ivy

Go to download

The eobjects.dk MetaModel is a common domain model, query-engine and optimizer for different kinds of datastores.

The newest version!
/*
 * Copyright 2008 eobjects.dk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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