001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.query.sql.model;
016
017/**
018 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
019 */
020public class SelectClause extends Clause {
021
022    private static final long serialVersionUID = -3786932682733679665L;
023
024    public final SelectList elements;
025
026    public final boolean distinct;
027
028    public SelectClause() {
029        this(new SelectList(), false);
030    }
031
032    public SelectClause(boolean distinct) {
033        this(new SelectList(), distinct);
034    }
035
036    public SelectClause(SelectList elements) {
037        this(elements, false);
038    }
039
040    public SelectClause(SelectList elements, boolean distinct) {
041        super("SELECT");
042        this.elements = elements;
043        this.distinct = distinct;
044    }
045
046    public void add(String alias, Operand element) {
047        elements.add(alias, element);
048    }
049
050    public void add(Operand element) {
051        elements.add(element.toString(), element);
052    }
053
054    public Operand get(String alias) {
055        return elements.get(alias);
056    }
057
058    public Reference getVariable(String alias) {
059        return (Reference) elements.get(alias);
060    }
061
062    public Literal getLiteral(String alias) {
063        return (Literal) elements.get(alias);
064    }
065
066    public Function getFunction(String alias) {
067        return (Function) elements.get(alias);
068    }
069
070    public Expression getExpression(String alias) {
071        return (Expression) elements.get(alias);
072    }
073
074    public Operand get(int i) {
075        return elements.get(i);
076    }
077
078    public String getAlias(int i) {
079        return elements.getKey(i);
080    }
081
082    public Reference getVariable(int i) {
083        return (Reference) elements.get(i);
084    }
085
086    public Literal getLiteral(int i) {
087        return (Literal) elements.get(i);
088    }
089
090    public Function getFunction(int i) {
091        return (Function) elements.get(i);
092    }
093
094    public Expression getExpression(int i) {
095        return (Expression) elements.get(i);
096    }
097
098    public boolean isDistinct() {
099        return distinct;
100    }
101
102    public SelectList getSelectList() {
103        return elements;
104    }
105
106    public int count() {
107        return elements.size();
108    }
109
110    public boolean isEmpty() {
111        return elements.isEmpty();
112    }
113
114    @Override
115    public void accept(IVisitor visitor) {
116        visitor.visitSelectClause(this);
117    }
118
119    @Override
120    public boolean equals(Object obj) {
121        if (this == obj) {
122            return true;
123        }
124        if (obj instanceof SelectClause) {
125            SelectClause sc = (SelectClause) obj;
126            return elements.equals(sc.elements) && (distinct == sc.distinct);
127        }
128        return false;
129    }
130
131    @Override
132    public int hashCode() {
133        int result = elements.hashCode();
134        result = 31 * result + (distinct ? 1 : 0);
135        return result;
136    }
137
138    @Override
139    public String toString() {
140        return (distinct ? "DISTINCT " : "") + elements;
141    }
142
143}