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