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