Search in sources :

Example 46 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class ConstraintReferential method appendUpdate.

private void appendUpdate(StatementBuilder buff) {
    buff.append("UPDATE ").append(table.getSQL()).append(" SET ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" , ");
        buff.append(Parser.quoteIdentifier(c.column.getName())).append("=?");
    }
}
Also used : IndexColumn(org.h2.table.IndexColumn)

Example 47 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class ConstraintUnique method getCreateSQLForCopy.

private String getCreateSQLForCopy(Table forTable, String quotedName, boolean internalIndex) {
    StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
    buff.append(forTable.getSQL()).append(" ADD CONSTRAINT ");
    if (forTable.isHidden()) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    buff.append(' ').append(getConstraintType().getSqlName()).append('(');
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(Parser.quoteIdentifier(c.column.getName()));
    }
    buff.append(')');
    if (internalIndex && indexOwner && forTable == this.table) {
        buff.append(" INDEX ").append(index.getSQL());
    }
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 48 with IndexColumn

use of org.h2.table.IndexColumn 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)

Example 49 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class AlterTableAddConstraint method canUseUniqueIndex.

// all cols must be in the index key, the order doesn't matter and there
// must be no other fields in the index key
private static boolean canUseUniqueIndex(Index idx, Table table, IndexColumn[] cols) {
    if (idx.getTable() != table || !idx.getIndexType().isUnique()) {
        return false;
    }
    Column[] indexCols = idx.getColumns();
    HashSet<Column> indexColsSet = new HashSet<>();
    Collections.addAll(indexColsSet, indexCols);
    HashSet<Column> colsSet = new HashSet<>();
    for (IndexColumn c : cols) {
        colsSet.add(c.column);
    }
    return colsSet.equals(indexColsSet);
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) HashSet(java.util.HashSet) IndexColumn(org.h2.table.IndexColumn)

Example 50 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class ViewIndex method getQuery.

private Query getQuery(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder) {
    Query q = prepareSubQuery(querySQL, session, masks, filters, filter, sortOrder);
    if (masks == null) {
        return q;
    }
    if (!q.allowGlobalConditions()) {
        return q;
    }
    int firstIndexParam = view.getParameterOffset(originalParameters);
    // the column index of each parameter
    // (for example: paramColumnIndex {0, 0} mean
    // param[0] is column 0, and param[1] is also column 0)
    IntArray paramColumnIndex = new IntArray();
    int indexColumnCount = 0;
    for (int i = 0; i < masks.length; i++) {
        int mask = masks[i];
        if (mask == 0) {
            continue;
        }
        indexColumnCount++;
        // the number of parameters depends on the mask;
        // for range queries it is 2: >= x AND <= y
        // but bitMask could also be 7 (=, and <=, and >=)
        int bitCount = Integer.bitCount(mask);
        for (int j = 0; j < bitCount; j++) {
            paramColumnIndex.add(i);
        }
    }
    int len = paramColumnIndex.size();
    ArrayList<Column> columnList = New.arrayList();
    for (int i = 0; i < len; ) {
        int idx = paramColumnIndex.get(i);
        columnList.add(table.getColumn(idx));
        int mask = masks[idx];
        if ((mask & IndexCondition.EQUALITY) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.EQUAL_NULL_SAFE);
            i++;
        }
        if ((mask & IndexCondition.START) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.BIGGER_EQUAL);
            i++;
        }
        if ((mask & IndexCondition.END) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.SMALLER_EQUAL);
            i++;
        }
        if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.SPATIAL_INTERSECTS);
            i++;
        }
    }
    columns = columnList.toArray(new Column[0]);
    // reconstruct the index columns from the masks
    this.indexColumns = new IndexColumn[indexColumnCount];
    this.columnIds = new int[indexColumnCount];
    for (int type = 0, indexColumnId = 0; type < 2; type++) {
        for (int i = 0; i < masks.length; i++) {
            int mask = masks[i];
            if (mask == 0) {
                continue;
            }
            if (type == 0) {
                if ((mask & IndexCondition.EQUALITY) == 0) {
                    // the first columns need to be equality conditions
                    continue;
                }
            } else {
                if ((mask & IndexCondition.EQUALITY) != 0) {
                    // after that only range conditions
                    continue;
                }
            }
            IndexColumn c = new IndexColumn();
            c.column = table.getColumn(i);
            indexColumns[indexColumnId] = c;
            columnIds[indexColumnId] = c.column.getColumnId();
            indexColumnId++;
        }
    }
    String sql = q.getPlanSQL();
    q = prepareSubQuery(sql, session, masks, filters, filter, sortOrder);
    return q;
}
Also used : Query(org.h2.command.dml.Query) IntArray(org.h2.util.IntArray) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Parameter(org.h2.expression.Parameter) IndexColumn(org.h2.table.IndexColumn)

Aggregations

IndexColumn (org.h2.table.IndexColumn)53 Column (org.h2.table.Column)23 ArrayList (java.util.ArrayList)14 Index (org.h2.index.Index)12 Value (org.h2.value.Value)12 IndexType (org.h2.index.IndexType)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 Constraint (org.h2.constraint.Constraint)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 LinkedHashMap (java.util.LinkedHashMap)5 CacheException (javax.cache.CacheException)5 TableFilter (org.h2.table.TableFilter)5 StatementBuilder (org.h2.util.StatementBuilder)5 ValueString (org.h2.value.ValueString)5 HashMap (java.util.HashMap)4 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)4 H2TreeIndexBase (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 Row (org.h2.result.Row)4 ResultSet (java.sql.ResultSet)3