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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.jdbc.db;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024
025import org.nuxeo.ecm.core.storage.sql.ColumnType;
026import org.nuxeo.ecm.core.storage.sql.Model;
027import org.nuxeo.ecm.core.storage.sql.jdbc.dialect.Dialect;
028
029/**
030 * An alias for an existing table. The returned columns are wrapped.
031 */
032public class TableAlias implements Table {
033
034    private static final long serialVersionUID = 1L;
035
036    /** The table this is an alias of. */
037    protected final Table table;
038
039    /** The name (alias) used to refer to this table. */
040    protected final String alias;
041
042    protected final Dialect dialect;
043
044    /**
045     * Creates a table as an alias for another one.
046     */
047    public TableAlias(Table table, String alias) {
048        this.table = table;
049        this.alias = alias;
050        dialect = table.getDialect();
051    }
052
053    @Override
054    public boolean isAlias() {
055        return true;
056    }
057
058    @Override
059    public Table getRealTable() {
060        return table;
061    }
062
063    @Override
064    public Dialect getDialect() {
065        return dialect;
066    }
067
068    @Override
069    public String getKey() {
070        return table.getKey();
071    }
072
073    @Override
074    public String getPhysicalName() {
075        return alias;
076    }
077
078    @Override
079    public String getQuotedName() {
080        return dialect.openQuote() + alias + dialect.closeQuote();
081    }
082
083    @Override
084    public String getQuotedSuffixedName(String suffix) {
085        return dialect.openQuote() + alias + suffix + dialect.closeQuote();
086    }
087
088    @Override
089    public String toString() {
090        StringBuilder buf = new StringBuilder();
091        buf.append("Table(");
092        buf.append(table.getPhysicalName());
093        buf.append(" AS ");
094        buf.append(alias);
095        buf.append(')');
096        return buf.toString();
097    }
098
099    @Override
100    public Column getColumn(String name) {
101        return new Column(table.getColumn(name), this);
102    }
103
104    @Override
105    public Column getPrimaryColumn() {
106        return new Column(table.getPrimaryColumn(), this);
107    }
108
109    // probably never used
110    @Override
111    public Collection<Column> getColumns() {
112        Collection<Column> columns = table.getColumns();
113        List<Column> result = new ArrayList<Column>(columns.size());
114        for (Column column : columns) {
115            result.add(new Column(column, this));
116        }
117        return result;
118    }
119
120    @Override
121    public Column addColumn(String name, ColumnType type, String key, Model model) {
122        throw new UnsupportedOperationException();
123    }
124
125    @Override
126    public void addIndex(String... columnNames) {
127        throw new UnsupportedOperationException();
128    }
129
130    @Override
131    public void addIndex(String indexName, IndexType indexType, String... columnNames) {
132        throw new UnsupportedOperationException();
133    }
134
135    @Override
136    public boolean hasFulltextIndex() {
137        throw new UnsupportedOperationException();
138    }
139
140    @Override
141    public String getCreateSql() {
142        throw new UnsupportedOperationException();
143    }
144
145    @Override
146    public String getAddColumnSql(Column column) {
147        throw new UnsupportedOperationException();
148    }
149
150    @Override
151    public List<String> getPostCreateSqls(Model model) {
152        throw new UnsupportedOperationException();
153    }
154
155    @Override
156    public List<String> getPostAddSqls(Column column, Model model) {
157        throw new UnsupportedOperationException();
158    }
159
160    @Override
161    public String getDropSql() {
162        throw new UnsupportedOperationException();
163    }
164
165}