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

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

import dk.eobjects.metamodel.schema.Column;
import dk.eobjects.metamodel.schema.Table;
import dk.eobjects.metamodel.schema.Relationship;

/**
 * Represents a FROM item. FROM items can take different forms:
 * 
    *
  • table FROMs (eg. "FROM products p")
  • *
  • join FROMs with an ON clause (eg. "FROM products p INNER JOIN orders o ON * p.id = o.product_id") *
  • subquery FROMs (eg. "FROM (SELECT * FROM products) p") *
* * @see FromClause */ public class FromItem implements IQueryItem, Cloneable { private static final long serialVersionUID = -6559220014058975193L; private Table _table; private String _alias; private Query _subQuery; private JoinType _join; private FromItem _leftSide; private FromItem _rightSide; private SelectItem[] _leftOn; private SelectItem[] _rightOn; private Query _query; /** * Private constructor, used for cloning */ private FromItem() { } /** * Constructor for table FROM clauses */ public FromItem(Table table) { _table = table; } /** * Constructor for sub-query FROM clauses * * @param subQuery * the subquery to use */ public FromItem(Query subQuery) { _subQuery = subQuery; } /** * Constructor for join FROM clauses that join two tables using their * relationship. The primary table of the relationship will be the left side * of the join and the foreign table of the relationship will be the right * side of the join. * * @param join * the join type to use * @param relationship * the relationship to use for joining the tables */ public FromItem(JoinType join, Relationship relation) { _join = join; _leftSide = new FromItem(relation.getPrimaryTable()); Column[] columns = relation.getPrimaryColumns(); _leftOn = new SelectItem[columns.length]; for (int i = 0; i < columns.length; i++) { _leftOn[i] = new SelectItem(columns[i]); } _rightSide = new FromItem(relation.getForeignTable()); columns = relation.getForeignColumns(); _rightOn = new SelectItem[columns.length]; for (int i = 0; i < columns.length; i++) { _rightOn[i] = new SelectItem(columns[i]); } } /** * Constructor for advanced join types with custom relationships * * @param join * the join type to use * @param leftSide * the left side of the join * @param rightSide * the right side of the join * @param leftOn * what left-side select items to use for the ON clause * @param rightOn * what right-side select items to use for the ON clause */ public FromItem(JoinType join, FromItem leftSide, FromItem rightSide, SelectItem[] leftOn, SelectItem[] rightOn) { _join = join; _leftSide = leftSide; _rightSide = rightSide; _leftOn = leftOn; _rightOn = rightOn; } public String getAlias() { return _alias; } public String getSameQueryAlias() { if (_alias != null) { return _alias; } if (_table != null) { return _table.getQuotedName(); } return null; } public FromItem setAlias(String alias) { _alias = alias; return this; } public Table getTable() { return _table; } public Query getSubQuery() { return _subQuery; } public JoinType getJoin() { return _join; } public FromItem getLeftSide() { return _leftSide; } public FromItem getRightSide() { return _rightSide; } public SelectItem[] getLeftOn() { return _leftOn; } public SelectItem[] getRightOn() { return _rightOn; } @Override public String toString() { StringBuilder sb = new StringBuilder(toStringNoAlias()); if (_join != null && _alias != null) { sb.insert(0, '('); sb.append(')'); } if (_alias != null) { sb.append(' '); sb.append(_alias); } return sb.toString(); } public String toStringNoAlias() { StringBuilder sb = new StringBuilder(); if (_table != null) { if (_table.getSchema() != null && _table.getSchema().getName() != null) { sb.append(_table.getSchema().getName() + '.'); } sb.append(_table.getQuotedName()); } else if (_subQuery != null) { sb.append('(' + _subQuery.toString() + ')'); } else if (_join != null) { String leftSideAlias = _leftSide.getSameQueryAlias(); String rightSideAlias = _rightSide.getSameQueryAlias(); sb.append(_leftSide.toString() + ' ' + _join + " JOIN " + _rightSide.toString()); for (int i = 0; i < _leftOn.length; i++) { if (i == 0) { sb.append(" ON"); } else { sb.append(" AND "); } SelectItem primary = _leftOn[i]; SelectItem foreign = _rightOn[i]; sb.append(' '); if (leftSideAlias != null) { sb.append(leftSideAlias + '.'); } sb.append(primary.getSuperQueryAlias()); sb.append(" = "); if (rightSideAlias != null) { sb.append(rightSideAlias + '.'); } sb.append(foreign.getSuperQueryAlias()); } } return sb.toString(); } /** * Gets the alias of a table, if it is registered (and visible, ie. not part * of a sub-query) in the FromItem * * @param table * the table to get the alias for * @return the alias or null if none is found */ public String getAlias(Table table) { String result = null; if (table != null) { // Search recursively through left and right side, unless they // are sub-query FromItems if (table.equals(_table)) { result = _alias; } else if (_join != null) { result = _rightSide.getAlias(table); if (result == null) { result = _leftSide.getAlias(table); } } } return result; } public Query getQuery() { return _query; } public IQueryItem setQuery(Query query) { _query = query; return this; } @Override protected FromItem clone() { FromItem f = new FromItem(); f._alias = _alias; f._join = _join; f._table = _table; if (_subQuery != null) { f._subQuery = _subQuery.clone(); } if (_leftOn != null && _leftSide != null && _rightOn != null && _rightSide != null) { f._leftSide = _leftSide.clone(); f._leftOn = _leftOn.clone(); f._rightSide = _rightSide.clone(); f._rightOn = _rightOn.clone(); } return f; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof FromItem) { FromItem that = (FromItem) obj; EqualsBuilder eb = new EqualsBuilder(); eb.append(this.getTable(), that.getTable()); eb.append(this.getJoin(), that.getJoin()); eb.append(this.getLeftOn(), that.getLeftOn()); eb.append(this.getLeftSide(), that.getLeftSide()); eb.append(this.getRightOn(), that.getRightOn()); eb.append(this.getRightSide(), that.getRightSide()); eb.append(this.getSubQuery(), that.getSubQuery()); eb.append(this.getAlias(), that.getAlias()); return eb.isEquals(); } return false; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy