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.io.Serializable;
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 * A SQL table.
031 */
032public interface Table extends Serializable {
033
034    enum IndexType {
035        /** Fulltext index, may be on several columns. */
036        FULLTEXT,
037        /** Non primary index but the main one for this table. */
038        MAIN_NON_PRIMARY
039    }
040
041    boolean isAlias();
042
043    Table getRealTable();
044
045    Dialect getDialect();
046
047    String getKey();
048
049    String getPhysicalName();
050
051    String getQuotedName();
052
053    String getQuotedSuffixedName(String suffix);
054
055    Column getColumn(String name);
056
057    Column getPrimaryColumn();
058
059    Collection<Column> getColumns();
060
061    /**
062     * Adds a {@link Column} to the table.
063     */
064    Column addColumn(String name, ColumnType type, String key, Model model);
065
066    /**
067     * Adds an index on one or several columns.
068     *
069     * @param columnNames the column names
070     */
071    void addIndex(String... columnNames);
072
073    /**
074     * Adds an index of the given name and type on one or several columns.
075     *
076     * @param indexName the index name
077     * @param indexType the index type
078     * @param columnNames the column names
079     */
080    void addIndex(String indexName, IndexType indexType, String... columnNames);
081
082    /**
083     * Checks if the table has some fulltext indexes.
084     *
085     * @return {@code true} if the table has some fulltext indexes
086     */
087    boolean hasFulltextIndex();
088
089    /**
090     * Computes the SQL statement to create the table.
091     *
092     * @return the SQL create string.
093     */
094    String getCreateSql();
095
096    /**
097     * Computes the SQL statement to alter a table and add a column to it.
098     *
099     * @param column the column to add
100     * @return the SQL alter table string
101     */
102    String getAddColumnSql(Column column);
103
104    /**
105     * Computes the SQL statements to finish creating the table, usually some ALTER TABLE statements to add constraints
106     * or indexes.
107     *
108     * @return the SQL strings
109     */
110    List<String> getPostCreateSqls(Model model);
111
112    /**
113     * Computes the SQL statements to finish adding a column, usually some ALTER TABLE statements to add constraints or
114     * indexes.
115     *
116     * @return the SQL strings
117     */
118    List<String> getPostAddSqls(Column column, Model model);
119
120    /**
121     * Computes the SQL statement to drop the table.
122     * <p>
123     * TODO drop constraints and indexes
124     *
125     * @return the SQL drop string.
126     */
127    String getDropSql();
128
129}