001/*
002 * (C) Copyright 2006-2011 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 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.query.sql.model;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class FromClause extends Clause {
028
029    private static final long serialVersionUID = 3563271484181691679L;
030
031    public static final int DOCTYPE = 0;
032
033    public static final int LOCATION = 1;
034
035    public final FromList elements;
036
037    public final int type;
038
039    public FromClause() {
040        this(new FromList());
041    }
042
043    public FromClause(FromList elements) {
044        this(DOCTYPE, elements);
045    }
046
047    public FromClause(int type, FromList elements) {
048        super("FROM");
049        this.elements = elements;
050        this.type = type;
051    }
052
053    public void add(String alias, String element) {
054        elements.add(alias, element);
055    }
056
057    public void add(String element) {
058        elements.add(element, element);
059    }
060
061    public String get(int i) {
062        return elements.get(i);
063    }
064
065    public String get(String alias) {
066        return elements.get(alias);
067    }
068
069    public String getAlias(int i) {
070        return elements.getKey(i);
071    }
072
073    public int count() {
074        return elements.size();
075    }
076
077    public int getType() {
078        return type;
079    }
080
081    @Override
082    public void accept(IVisitor visitor) {
083        visitor.visitFromClause(this);
084    }
085
086    @Override
087    public boolean equals(Object obj) {
088        if (this == obj) {
089            return true;
090        }
091        if (obj instanceof FromClause) {
092            FromClause sc = (FromClause) obj;
093            return elements.equals(sc.elements);
094        }
095        return false;
096    }
097
098    @Override
099    public int hashCode() {
100        return elements.hashCode();
101    }
102
103    @Override
104    public String toString() {
105        return elements.toString();
106    }
107
108}