Search in sources :

Example 66 with Column

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

the class PageBtreeIndex method readRow.

/**
 * Read a row from the data page at the given offset.
 *
 * @param data the data
 * @param offset the offset
 * @param onlyPosition whether only the position of the row is stored
 * @param needData whether the row data is required
 * @return the row
 */
SearchRow readRow(Data data, int offset, boolean onlyPosition, boolean needData) {
    synchronized (data) {
        data.setPos(offset);
        long key = data.readVarLong();
        if (onlyPosition) {
            if (needData) {
                return tableData.getRow(null, key);
            }
            SearchRow row = table.getTemplateSimpleRow(true);
            row.setKey(key);
            return row;
        }
        SearchRow row = table.getTemplateSimpleRow(columns.length == 1);
        row.setKey(key);
        for (Column col : columns) {
            int idx = col.getColumnId();
            row.setValue(idx, data.readValue());
        }
        return row;
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) SearchRow(org.h2.result.SearchRow)

Example 67 with Column

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

the class LinkedCursor method next.

@Override
public boolean next() {
    try {
        boolean result = rs.next();
        if (!result) {
            rs.close();
            tableLink.reusePreparedStatement(prep, sql);
            current = null;
            return false;
        }
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
    current = tableLink.getTemplateRow();
    for (int i = 0; i < current.getColumnCount(); i++) {
        Column col = tableLink.getColumn(i);
        Value v = DataType.readValue(session, rs, i + 1, col.getType());
        current.setValue(i, v);
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Column(org.h2.table.Column) Value(org.h2.value.Value)

Example 68 with Column

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

the class LinkedIndex method find.

@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("SELECT * FROM ");
    buff.append(targetTableName).append(" T");
    for (int i = 0; first != null && i < first.getColumnCount(); i++) {
        Value v = first.getValue(i);
        if (v != null) {
            buff.appendOnlyFirst(" WHERE ");
            buff.appendExceptFirst(" AND ");
            Column col = table.getColumn(i);
            buff.append(col.getSQL());
            if (v == ValueNull.INSTANCE) {
                buff.append(" IS NULL");
            } else {
                buff.append(">=");
                addParameter(buff, col);
                params.add(v);
            }
        }
    }
    for (int i = 0; last != null && i < last.getColumnCount(); i++) {
        Value v = last.getValue(i);
        if (v != null) {
            buff.appendOnlyFirst(" WHERE ");
            buff.appendExceptFirst(" AND ");
            Column col = table.getColumn(i);
            buff.append(col.getSQL());
            if (v == ValueNull.INSTANCE) {
                buff.append(" IS NULL");
            } else {
                buff.append("<=");
                addParameter(buff, col);
                params.add(v);
            }
        }
    }
    String sql = buff.toString();
    try {
        PreparedStatement prep = link.execute(sql, params, false);
        ResultSet rs = prep.getResultSet();
        return new LinkedCursor(link, rs, session, sql, prep);
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException)

Example 69 with Column

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

the class LinkedIndex method update.

/**
 * Update a row using a UPDATE statement. This method is to be called if the
 * emit updates option is enabled.
 *
 * @param oldRow the old data
 * @param newRow the new data
 */
public void update(Row oldRow, Row newRow) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(targetTableName).append(" SET ");
    for (int i = 0; i < newRow.getColumnCount(); i++) {
        buff.appendExceptFirst(", ");
        buff.append(table.getColumn(i).getSQL()).append('=');
        Value v = newRow.getValue(i);
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (int i = 0; i < oldRow.getColumnCount(); i++) {
        Column col = table.getColumn(i);
        buff.appendExceptFirst(" AND ");
        buff.append(col.getSQL());
        Value v = oldRow.getValue(i);
        if (isNull(v)) {
            buff.append(" IS NULL");
        } else {
            buff.append('=');
            params.add(v);
            addParameter(buff, col);
        }
    }
    String sql = buff.toString();
    try {
        link.execute(sql, params, true);
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 70 with Column

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

the class NonUniqueHashIndex method find.

@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
    if (first == null || last == null) {
        throw DbException.throwInternalError(first + " " + last);
    }
    if (first != last) {
        if (compareKeys(first, last) != 0) {
            throw DbException.throwInternalError();
        }
    }
    Value v = first.getValue(indexColumn);
    /*
         * Sometimes the incoming search is a similar, but not the same type
         * e.g. the search value is INT, but the index column is LONG. In which
         * case we need to convert, otherwise the ValueHashMap will not find the
         * result.
         */
    v = v.convertTo(tableData.getColumn(indexColumn).getType());
    ArrayList<Long> positions = rows.get(v);
    return new NonUniqueHashCursor(session, tableData, positions);
}
Also used : Value(org.h2.value.Value)

Aggregations

Column (org.h2.table.Column)146 Value (org.h2.value.Value)83 IndexColumn (org.h2.table.IndexColumn)79 DbException (org.h2.message.DbException)64 SQLException (java.sql.SQLException)60 Expression (org.h2.expression.Expression)55 ExpressionColumn (org.h2.expression.ExpressionColumn)51 ValueString (org.h2.value.ValueString)34 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)32 ValueExpression (org.h2.expression.ValueExpression)30 ArrayList (java.util.ArrayList)27 PreparedStatement (java.sql.PreparedStatement)26 Index (org.h2.index.Index)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)25 Table (org.h2.table.Table)25 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)22 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)21 StatementBuilder (org.h2.util.StatementBuilder)19 Constraint (org.h2.constraint.Constraint)18