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

dk.eobjects.metamodel.query.Query 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 org.apache.commons.lang.builder.EqualsBuilder;

import dk.eobjects.metamodel.DataContext;
import dk.eobjects.metamodel.query.OrderByItem.Direction;
import dk.eobjects.metamodel.schema.Column;
import dk.eobjects.metamodel.schema.Table;

/**
 * Represents a query to retrieve data by. A query is made up of six clauses,
 * equivalent to the SQL standard:
 * 
    *
  • the SELECT clause, which define the wanted columns of the resulting * DataSet
  • *
  • the FROM clause, which define where to retrieve the data from
  • *
  • the WHERE clause, which define filters on the retrieved data
  • *
  • the GROUP BY clause, which define if the result should be grouped and * aggregated according to some columns acting as categories
  • *
  • the HAVING clause, which define filters on the grouped data
  • *
  • the ORDER BY clause, which define sorting of the resulting dataset
  • *
* * Queries are executed using the DataContext.executeQuery method or can * alternatively be used directly in JDBC by using the toString() method. * * @see DataContext */ public class Query implements Cloneable, Serializable { private static final long serialVersionUID = -5976325207498574216L; private SelectClause _selectClause; private FromClause _fromClause; private FilterClause _whereClause; private GroupByClause _groupByClause; private FilterClause _havingClause; private OrderByClause _orderByClause; private Integer _maxRows; public Query() { _selectClause = new SelectClause(this); _fromClause = new FromClause(this); _whereClause = new FilterClause(this, QueryClause.PREFIX_WHERE); _groupByClause = new GroupByClause(this); _havingClause = new FilterClause(this, QueryClause.PREFIX_HAVING); _orderByClause = new OrderByClause(this); } public Query select(Column... columns) { for (Column column : columns) { SelectItem selectItem = new SelectItem(column); selectItem.setQuery(this); _selectClause.addItem(selectItem); } return this; } public Query select(SelectItem... items) { _selectClause.addItems(items); return this; } public Query select(String expression, String alias) { return select(new SelectItem(expression, alias)); } public Query select(String expression) { return select(expression, null); } public Query selectDistinct() { _selectClause.setDistinct(true); return this; } public Query selectCount() { return select(SelectItem.getCountAllItem()); } public Query from(FromItem... items) { _fromClause.addItems(items); return this; } public Query from(Table table) { return from(new FromItem(table)); } public Query from(String expression) { return from(new FromItem(expression)); } public Query from(Table table, String alias) { return from(new FromItem(table).setAlias(alias)); } public Query groupBy(GroupByItem... items) { for (GroupByItem item : items) { SelectItem selectItem = item.getSelectItem(); if (selectItem != null && selectItem.getQuery() == null) { selectItem.setQuery(this); } } _groupByClause.addItems(items); return this; } public Query groupBy(Column... columns) { for (Column column : columns) { SelectItem selectItem = new SelectItem(column).setQuery(this); _groupByClause.addItem(new GroupByItem(selectItem)); } return this; } public Query orderBy(OrderByItem... items) { _orderByClause.addItems(items); return this; } public Query orderBy(Column column) { return orderBy(column, Direction.ASC); } /** * @deprecated use orderBy(Column, Direction) instead */ @Deprecated public Query orderBy(Column column, boolean ascending) { if (ascending) { return orderBy(column, Direction.ASC); } else { return orderBy(column, Direction.DESC); } } public Query orderBy(Column column, Direction direction) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return orderBy(new OrderByItem(selectItem, direction)); } public Query where(FilterItem... items) { _whereClause.addItems(items); return this; } public Query where(String expression) { return where(new FilterItem(expression)); } public Query where(Column column, OperatorType operatorType, Object operand) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return where(new FilterItem(selectItem, operatorType, operand)); } public Query having(FilterItem... items) { _havingClause.addItems(items); return this; } public Query having(Column column, OperatorType operatorType, Object operand) { SelectItem selectItem = _selectClause.getSelectItem(column); if (selectItem == null) { selectItem = new SelectItem(column); } return having(new FilterItem(selectItem, operatorType, operand)); } public Query having(String expression) { return having(new FilterItem(expression)); } @Override /* * A string representation of this query. This representation will be SQL 99 * compatible and can thus be used for database queries on databases that * meet SQL standards. */ public String toString() { StringBuilder sb = new StringBuilder(); sb.append(_selectClause.toString()); sb.append(_fromClause.toString()); sb.append(_whereClause.toString()); sb.append(_groupByClause.toString()); sb.append(_havingClause.toString()); sb.append(_orderByClause.toString()); return sb.toString(); } public SelectClause getSelectClause() { return _selectClause; } public FromClause getFromClause() { return _fromClause; } public FilterClause getWhereClause() { return _whereClause; } public GroupByClause getGroupByClause() { return _groupByClause; } public FilterClause getHavingClause() { return _havingClause; } public OrderByClause getOrderByClause() { return _orderByClause; } /** * Sets the maximum number of rows to be queried. If the result of the query * yields more rows they should be discarded. * * @param maxRows * the number of desired maximum rows. Can be null (default) for * no limits */ public Query setMaxRows(Integer maxRows) { _maxRows = maxRows; return this; } /** * @return the number of maximum rows to yield from executing this query or * null if no maximum/limit is set. */ public Integer getMaxRows() { return _maxRows; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Query) { Query that = (Query) obj; EqualsBuilder eb = new EqualsBuilder(); eb.append(this.getMaxRows(), that.getMaxRows()); eb.append(this.getSelectClause(), that.getSelectClause()); eb.append(this.getFromClause(), that.getFromClause()); eb.append(this.getWhereClause(), that.getWhereClause()); eb.append(this.getGroupByClause(), that.getGroupByClause()); eb.append(this.getHavingClause(), that.getHavingClause()); eb.append(this.getOrderByClause(), that.getOrderByClause()); return eb.isEquals(); } return false; } @Override public Query clone() { Query q = new Query(); q.setMaxRows(_maxRows); q.getSelectClause().setDistinct(_selectClause.isDistinct()); for (FromItem item : _fromClause.getItems()) { q.from(item.clone()); } for (SelectItem item : _selectClause.getItems()) { q.select(item.clone()); } for (FilterItem item : _whereClause.getItems()) { q.where(item.clone()); } for (GroupByItem item : _groupByClause.getItems()) { q.groupBy(item.clone()); } for (FilterItem item : _havingClause.getItems()) { q.having(item.clone()); } for (OrderByItem item : _orderByClause.getItems()) { q.orderBy(item.clone()); } return q; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy