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

dk.eobjects.metamodel.query.FromClause 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 dk.eobjects.metamodel.schema.Table;

/**
 * Represents the FROM clause of a query containing FromItem's.
 * 
 * @see FromItem
 */
public class FromClause extends QueryClause {

	private static final long serialVersionUID = -8227310702249122115L;

	public FromClause(Query query) {
		super(query, QueryClause.PREFIX_FROM, QueryClause.DELIM_COMMA);
	}

	/**
	 * Gets the alias of a table, if it is registered (and visible, ie. not part
	 * of a sub-query) in the FromClause
	 * 
	 * @param table
	 *            the table to get the alias for
	 * @return the alias or null if none is found
	 */
	public String getAlias(Table table) {
		if (table != null) {
			for (FromItem item : getItems()) {
				String alias = item.getAlias(table);
				if (alias != null) {
					return alias;
				}
			}
		}
		return null;
	}

	/**
	 * Retrieves a table by it's reference which may be it's alias or it's
	 * qualified table name. Typically, this method is used to resolve a
	 * SelectItem with a reference like "foo.bar", where "foo" may either be an
	 * alias or a table name
	 * 
	 * @param reference
	 * @return a FromItem which matches the provided reference string
	 */
	public FromItem getItemByReference(String reference) {
		if (reference != null) {
			for (FromItem item : getItems()) {
				String alias = item.getAlias();
				if (reference.equals(alias)) {
					return item;
				}
				Table table = item.getTable();
				if (alias == null && table != null
						&& reference.equals(table.getName())) {
					return item;
				}
			}
		}
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy