001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.core.query.sql.model;
020
021import java.util.Collection;
022
023import com.google.common.collect.Iterables;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class SelectClause extends Clause {
029
030    private static final long serialVersionUID = -3786932682733679665L;
031
032    public final SelectList elements;
033
034    public final boolean distinct;
035
036    public SelectClause() {
037        this(new SelectList(), false);
038    }
039
040    public SelectClause(boolean distinct) {
041        this(new SelectList(), distinct);
042    }
043
044    public SelectClause(SelectList elements) {
045        this(elements, false);
046    }
047
048    public SelectClause(SelectList elements, boolean distinct) {
049        super("SELECT");
050        this.elements = elements;
051        this.distinct = distinct;
052    }
053
054    public void add(String alias, Operand element) {
055        elements.put(alias, element);
056    }
057
058    public void add(Operand element) {
059        elements.put(element.toString(), element);
060    }
061
062    public Operand get(String alias) {
063        return elements.get(alias);
064    }
065
066    public Reference getVariable(String alias) {
067        return (Reference) elements.get(alias);
068    }
069
070    public Literal getLiteral(String alias) {
071        return (Literal) elements.get(alias);
072    }
073
074    public Function getFunction(String alias) {
075        return (Function) elements.get(alias);
076    }
077
078    public Expression getExpression(String alias) {
079        return (Expression) elements.get(alias);
080    }
081
082    public Operand get(int i) {
083        return Iterables.get(elements.values(), i);
084    }
085
086    public String getAlias(int i) {
087        return Iterables.get(elements.keySet(), i);
088    }
089
090    public Reference getVariable(int i) {
091        return (Reference) get(i);
092    }
093
094    public Literal getLiteral(int i) {
095        return (Literal) get(i);
096    }
097
098    public Function getFunction(int i) {
099        return (Function) get(i);
100    }
101
102    public Expression getExpression(int i) {
103        return (Expression) get(i);
104    }
105
106    public boolean isDistinct() {
107        return distinct;
108    }
109
110    public SelectList getSelectList() {
111        return elements;
112    }
113
114    public int count() {
115        return elements.size();
116    }
117
118    public boolean isEmpty() {
119        return elements.isEmpty();
120    }
121
122    /**
123     * @since 9.1
124     */
125    public Collection<Operand> operands() {
126        return elements.values();
127    }
128
129    /**
130     * @since 9.1
131     */
132    public boolean containsOperand(Object operand) {
133        return elements.containsValue(operand);
134    }
135
136    @Override
137    public void accept(IVisitor visitor) {
138        visitor.visitSelectClause(this);
139    }
140
141    @Override
142    public boolean equals(Object obj) {
143        if (this == obj) {
144            return true;
145        }
146        if (obj instanceof SelectClause) {
147            SelectClause sc = (SelectClause) obj;
148            return elements.equals(sc.elements) && (distinct == sc.distinct);
149        }
150        return false;
151    }
152
153    @Override
154    public int hashCode() {
155        int result = elements.hashCode();
156        result = 31 * result + (distinct ? 1 : 0);
157        return result;
158    }
159
160    @Override
161    public String toString() {
162        return (distinct ? "DISTINCT " : "") + elements;
163    }
164
165}