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

org.tinygroup.jsqlparser.statement.select.PlainSelect Maven / Gradle / Ivy

There is a newer version: 3.4.9
Show newest version
/**
 *  Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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 org.tinygroup.jsqlparser.statement.select;

import org.tinygroup.jsqlparser.expression.Expression;
import org.tinygroup.jsqlparser.expression.OracleHierarchicalExpression;
import org.tinygroup.jsqlparser.schema.Table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * The core of a "SELECT" statement (no UNION, no ORDER BY)
 */
public class PlainSelect implements SelectBody {

    private Distinct distinct = null;
    private List selectItems;
    private List intoTables;
    private FromItem fromItem;
    private List joins;
    private Expression where;
    private List groupByColumnReferences;
    private List orderByElements;
    private Expression having;
    private Limit limit;
    private Offset offset;
    private Fetch fetch;
    private Top top;
    private OracleHierarchicalExpression oracleHierarchical = null;
    private boolean oracleSiblings = false;
    private boolean forUpdate = false;

    /**
     * The {@link FromItem} in this query
     *
     * @return the {@link FromItem}
     */
    public FromItem getFromItem() {
        return fromItem;
    }

    public List
getIntoTables() { return intoTables; } /** * The {@link SelectItem}s in this query (for example the A,B,C in "SELECT * A,B,C") * * @return a list of {@link SelectItem}s */ public List getSelectItems() { return selectItems; } public Expression getWhere() { return where; } public void setFromItem(FromItem item) { fromItem = item; } public void setIntoTables(List
intoTables) { this.intoTables = intoTables; } public void setSelectItems(List list) { selectItems = list; } public void addSelectItems(SelectItem... items) { if (selectItems == null) { selectItems = new ArrayList(); } Collections.addAll(selectItems, items); } public void setWhere(Expression where) { this.where = where; } /** * The list of {@link Join}s * * @return the list of {@link Join}s */ public List getJoins() { return joins; } public void setJoins(List list) { joins = list; } public void accept(SelectVisitor selectVisitor) { selectVisitor.visit(this); } public List getOrderByElements() { return orderByElements; } public void setOrderByElements(List orderByElements) { this.orderByElements = orderByElements; } public Limit getLimit() { return limit; } public void setLimit(Limit limit) { this.limit = limit; } public Offset getOffset() { return offset; } public void setOffset(Offset offset) { this.offset = offset; } public Fetch getFetch() { return fetch; } public void setFetch(Fetch fetch) { this.fetch = fetch; } public Top getTop() { return top; } public void setTop(Top top) { this.top = top; } public Distinct getDistinct() { return distinct; } public void setDistinct(Distinct distinct) { this.distinct = distinct; } public Expression getHaving() { return having; } public void setHaving(Expression expression) { having = expression; } /** * A list of {@link Expression}s of the GROUP BY clause. It is null in case * there is no GROUP BY clause * * @return a list of {@link Expression}s */ public List getGroupByColumnReferences() { return groupByColumnReferences; } public void setGroupByColumnReferences(List list) { groupByColumnReferences = list; } public OracleHierarchicalExpression getOracleHierarchical() { return oracleHierarchical; } public void setOracleHierarchical(OracleHierarchicalExpression oracleHierarchical) { this.oracleHierarchical = oracleHierarchical; } public boolean isOracleSiblings() { return oracleSiblings; } public void setOracleSiblings(boolean oracleSiblings) { this.oracleSiblings = oracleSiblings; } public boolean isForUpdate() { return forUpdate; } public void setForUpdate(boolean forUpdate) { this.forUpdate = forUpdate; } @Override public String toString() { StringBuilder sql = new StringBuilder("SELECT "); if (distinct != null) { sql.append(distinct).append(" "); } if (top != null) { sql.append(top).append(" "); } sql.append(getStringList(selectItems)); if (intoTables != null) { sql.append(" INTO "); for (Iterator
iter = intoTables.iterator(); iter.hasNext();) { sql.append(iter.next().toString()); if (iter.hasNext()) { sql.append(", "); } } } if (fromItem != null) { sql.append(" FROM ").append(fromItem); if (joins != null) { Iterator it = joins.iterator(); while (it.hasNext()) { Join join = it.next(); if (join.isSimple()) { sql.append(", ").append(join); } else { sql.append(" ").append(join); } } } // sql += getFormatedList(joins, "", false, false); if (where != null) { sql.append(" WHERE ").append(where); } if (oracleHierarchical != null) { sql.append(oracleHierarchical.toString()); } sql.append(getFormatedList(groupByColumnReferences, "GROUP BY")); if (having != null) { sql.append(" HAVING ").append(having); } sql.append(orderByToString(oracleSiblings, orderByElements)); if (limit != null) { sql.append(limit); } if (offset != null) { sql.append(offset); } if (fetch != null) { sql.append(fetch); } if (isForUpdate()) { sql.append(" FOR UPDATE"); } } return sql.toString(); } public static String orderByToString(List orderByElements) { return orderByToString(false, orderByElements); } public static String orderByToString(boolean oracleSiblings, List orderByElements) { return getFormatedList(orderByElements, oracleSiblings ? "ORDER SIBLINGS BY" : "ORDER BY"); } public static String getFormatedList(List list, String expression) { return getFormatedList(list, expression, true, false); } public static String getFormatedList(List list, String expression, boolean useComma, boolean useBrackets) { String sql = getStringList(list, useComma, useBrackets); if (sql.length() > 0) { if (expression.length() > 0) { sql = " " + expression + " " + sql; } else { sql = " " + sql; } } return sql; } /** * List the toString out put of the objects in the List comma separated. If * the List is null or empty an empty string is returned. * * The same as getStringList(list, true, false) * * @see #getStringList(List, boolean, boolean) * @param list list of objects with toString methods * @return comma separated list of the elements in the list */ public static String getStringList(List list) { return getStringList(list, true, false); } /** * List the toString out put of the objects in the List that can be comma * separated. If the List is null or empty an empty string is returned. * * @param list list of objects with toString methods * @param useComma true if the list has to be comma separated * @param useBrackets true if the list has to be enclosed in brackets * @return comma separated list of the elements in the list */ public static String getStringList(List list, boolean useComma, boolean useBrackets) { String ans = ""; String comma = ","; if (!useComma) { comma = ""; } if (list != null) { if (useBrackets) { ans += "("; } for (int i = 0; i < list.size(); i++) { ans += "" + list.get(i) + ((i < list.size() - 1) ? comma + " " : ""); } if (useBrackets) { ans += ")"; } } return ans; } }