Search in sources :

Example 11 with CreateIndex

use of org.h2.command.ddl.CreateIndex in project ignite by apache.

the class GridSqlQueryParser method parseCreateIndex.

/**
 * Parse {@code CREATE INDEX} statement.
 *
 * @param createIdx {@code CREATE INDEX} statement.
 * @see <a href="http://h2database.com/html/grammar.html#create_index">H2 {@code CREATE INDEX} spec.</a>
 */
private GridSqlCreateIndex parseCreateIndex(CreateIndex createIdx) {
    if (CREATE_INDEX_HASH.get(createIdx) || CREATE_INDEX_PRIMARY_KEY.get(createIdx) || CREATE_INDEX_UNIQUE.get(createIdx))
        throw new IgniteSQLException("Only SPATIAL modifier is supported for CREATE INDEX", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    GridSqlCreateIndex res = new GridSqlCreateIndex();
    Schema schema = SCHEMA_COMMAND_SCHEMA.get(createIdx);
    String tblName = CREATE_INDEX_TABLE_NAME.get(createIdx);
    res.schemaName(schema.getName());
    res.tableName(tblName);
    res.ifNotExists(CREATE_INDEX_IF_NOT_EXISTS.get(createIdx));
    QueryIndex idx = new QueryIndex();
    idx.setName(CREATE_INDEX_NAME.get(createIdx));
    idx.setIndexType(CREATE_INDEX_SPATIAL.get(createIdx) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
    IndexColumn[] cols = CREATE_INDEX_COLUMNS.get(createIdx);
    LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>(cols.length);
    for (IndexColumn col : CREATE_INDEX_COLUMNS.get(createIdx)) {
        int sortType = INDEX_COLUMN_SORT_TYPE.get(col);
        if ((sortType & SortOrder.NULLS_FIRST) != 0 || (sortType & SortOrder.NULLS_LAST) != 0)
            throw new IgniteSQLException("NULLS FIRST and NULLS LAST modifiers are not supported for index columns", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        flds.put(INDEX_COLUMN_NAME.get(col), (sortType & SortOrder.DESCENDING) == 0);
    }
    idx.setFields(flds);
    res.index(idx);
    return res;
}
Also used : Schema(org.h2.schema.Schema) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryIndex(org.apache.ignite.cache.QueryIndex) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) IndexColumn(org.h2.table.IndexColumn) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with CreateIndex

use of org.h2.command.ddl.CreateIndex in project h2database by h2database.

the class AlterTableAddConstraint method createIndex.

private Index createIndex(Table t, IndexColumn[] cols, boolean unique) {
    int indexId = getObjectId();
    IndexType indexType;
    if (unique) {
        // for unique constraints
        indexType = IndexType.createUnique(t.isPersistIndexes(), false);
    } else {
        // constraints
        indexType = IndexType.createNonUnique(t.isPersistIndexes());
    }
    indexType.setBelongsToConstraint(true);
    String prefix = constraintName == null ? "CONSTRAINT" : constraintName;
    String indexName = t.getSchema().getUniqueIndexName(session, t, prefix + "_INDEX_");
    try {
        Index index = t.addIndex(session, indexName, indexId, cols, indexType, true, null);
        createdIndexes.add(index);
        return index;
    } finally {
        getSchema().freeUniqueName(indexName);
    }
}
Also used : Index(org.h2.index.Index) IndexType(org.h2.index.IndexType) Constraint(org.h2.constraint.Constraint)

Aggregations

CreateIndex (org.h2.command.ddl.CreateIndex)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)3 IndexType (org.h2.index.IndexType)3 IndexColumn (org.h2.table.IndexColumn)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 ValueString (org.h2.value.ValueString)3 PreparedStatement (java.sql.PreparedStatement)2 Constraint (org.h2.constraint.Constraint)2 Database (org.h2.engine.Database)2 Index (org.h2.index.Index)2 Schema (org.h2.schema.Schema)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 QueryIndex (org.apache.ignite.cache.QueryIndex)1 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)1