Search in sources :

Example 11 with IndexType

use of org.h2.index.IndexType in project h2database by h2database.

the class MVTable method addIndex.

@Override
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType, boolean create, String indexComment) {
    if (indexType.isPrimaryKey()) {
        for (IndexColumn c : cols) {
            Column column = c.column;
            if (column.isNullable()) {
                throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, column.getName());
            }
            column.setPrimaryKey(true);
        }
    }
    boolean isSessionTemporary = isTemporary() && !isGlobalTemporary();
    if (!isSessionTemporary) {
        database.lockMeta(session);
    }
    MVIndex index;
    int mainIndexColumn;
    mainIndexColumn = getMainIndexColumn(indexType, cols);
    if (database.isStarting()) {
        if (transactionStore.store.hasMap("index." + indexId)) {
            mainIndexColumn = -1;
        }
    } else if (primaryIndex.getRowCountMax() != 0) {
        mainIndexColumn = -1;
    }
    if (mainIndexColumn != -1) {
        primaryIndex.setMainIndexColumn(mainIndexColumn);
        index = new MVDelegateIndex(this, indexId, indexName, primaryIndex, indexType);
    } else if (indexType.isSpatial()) {
        index = new MVSpatialIndex(session.getDatabase(), this, indexId, indexName, cols, indexType);
    } else {
        index = new MVSecondaryIndex(session.getDatabase(), this, indexId, indexName, cols, indexType);
    }
    if (index.needRebuild()) {
        rebuildIndex(session, index, indexName);
    }
    index.setTemporary(isTemporary());
    if (index.getCreateSQL() != null) {
        index.setComment(indexComment);
        if (isSessionTemporary) {
            session.addLocalTempTableIndex(index);
        } else {
            database.addSchemaObject(session, index);
        }
    }
    indexes.add(index);
    setModified();
    return index;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Constraint(org.h2.constraint.Constraint) IndexColumn(org.h2.table.IndexColumn)

Aggregations

IndexType (org.h2.index.IndexType)7 Index (org.h2.index.Index)6 Constraint (org.h2.constraint.Constraint)4 IndexColumn (org.h2.table.IndexColumn)4 Column (org.h2.table.Column)3 ArrayList (java.util.ArrayList)2 Database (org.h2.engine.Database)2 LinkedIndex (org.h2.index.LinkedIndex)2 MultiVersionIndex (org.h2.index.MultiVersionIndex)2 PageBtreeIndex (org.h2.index.PageBtreeIndex)2 PageDataIndex (org.h2.index.PageDataIndex)2 PageDelegateIndex (org.h2.index.PageDelegateIndex)2 DbException (org.h2.message.DbException)2 Table (org.h2.table.Table)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1