Search in sources :

Example 76 with Row

use of org.h2.result.Row in project h2database by h2database.

the class RowList method writeAllRows.

private void writeAllRows() {
    if (file == null) {
        Database db = session.getDatabase();
        String fileName = db.createTempFile();
        file = db.openFile(fileName, "rw", false);
        file.setCheckedWriting(false);
        file.seek(FileStore.HEADER_LENGTH);
        rowBuff = Data.create(db, Constants.DEFAULT_PAGE_SIZE);
        file.seek(FileStore.HEADER_LENGTH);
    }
    Data buff = rowBuff;
    initBuffer(buff);
    for (int i = 0, size = list.size(); i < size; i++) {
        if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) {
            flushBuffer(buff);
            initBuffer(buff);
        }
        Row r = list.get(i);
        writeRow(buff, r);
    }
    flushBuffer(buff);
    file.autoDelete();
    list.clear();
    memory = 0;
}
Also used : Database(org.h2.engine.Database) Data(org.h2.store.Data)

Example 77 with Row

use of org.h2.result.Row in project h2database by h2database.

the class RowList method writeRow.

private void writeRow(Data buff, Row r) {
    buff.checkCapacity(1 + Data.LENGTH_INT * 8);
    buff.writeByte((byte) 1);
    buff.writeInt(r.getMemory());
    int columnCount = r.getColumnCount();
    buff.writeInt(columnCount);
    buff.writeLong(r.getKey());
    buff.writeInt(r.getVersion());
    buff.writeInt(r.isDeleted() ? 1 : 0);
    buff.writeInt(r.getSessionId());
    for (int i = 0; i < columnCount; i++) {
        Value v = r.getValue(i);
        buff.checkCapacity(1);
        if (v == null) {
            buff.writeByte((byte) 0);
        } else {
            buff.writeByte((byte) 1);
            if (v.getType() == Value.CLOB || v.getType() == Value.BLOB) {
                // otherwise the temp file is deleted
                if (v.getSmall() == null && v.getTableId() == 0) {
                    if (lobs == null) {
                        lobs = New.arrayList();
                    }
                    // need to create a copy, otherwise,
                    // if stored multiple times, it may be renamed
                    // and then not found
                    v = v.copyToTemp();
                    lobs.add(v);
                }
            }
            buff.checkCapacity(buff.getValueLen(v));
            buff.writeValue(v);
        }
    }
}
Also used : Value(org.h2.value.Value)

Example 78 with Row

use of org.h2.result.Row in project h2database by h2database.

the class UpdatableRow method insertRow.

/**
 * Insert a new row into the database.
 *
 * @param row the new row
 * @throws SQLException if the row could not be inserted
 */
public void insertRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    appendTableName(buff);
    buff.append('(');
    appendColumnList(buff, false);
    buff.append(")VALUES(");
    buff.resetCount();
    for (int i = 0; i < columnCount; i++) {
        buff.appendExceptFirst(",");
        Value v = row[i];
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
        }
    }
    buff.append(')');
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    for (int i = 0, j = 0; i < columnCount; i++) {
        Value v = row[i];
        if (v != null) {
            v.set(prep, j++ + 1);
        }
    }
    int count = prep.executeUpdate();
    if (count != 1) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 79 with Row

use of org.h2.result.Row in project h2database by h2database.

the class UpdatableRow method deleteRow.

/**
 * Delete the given row in the database.
 *
 * @param current the row
 * @throws SQLException if this row has already been deleted
 */
public void deleteRow(Value[] current) throws SQLException {
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has already been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) PreparedStatement(java.sql.PreparedStatement)

Example 80 with Row

use of org.h2.result.Row in project h2database by h2database.

the class UpdatableRow method readRow.

/**
 * Re-reads a row from the database and updates the values in the array.
 *
 * @param row the values that contain the key
 * @return the row
 */
public Value[] readRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    appendColumnList(buff, false);
    buff.append(" FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, row);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        int type = result.getColumnType(i);
        newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
    }
    return newRow;
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) ResultSet(java.sql.ResultSet) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Value (org.h2.value.Value)118 Row (org.h2.result.Row)49 Column (org.h2.table.Column)48 DbException (org.h2.message.DbException)44 SearchRow (org.h2.result.SearchRow)37 SQLException (java.sql.SQLException)29 Index (org.h2.index.Index)28 IndexColumn (org.h2.table.IndexColumn)24 Cursor (org.h2.index.Cursor)21 PreparedStatement (java.sql.PreparedStatement)20 ResultSet (java.sql.ResultSet)20 StatementBuilder (org.h2.util.StatementBuilder)20 ArrayList (java.util.ArrayList)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 Expression (org.h2.expression.Expression)17 Statement (java.sql.Statement)16 Constraint (org.h2.constraint.Constraint)16 ValueString (org.h2.value.ValueString)16 Connection (java.sql.Connection)15 SimpleResultSet (org.h2.tools.SimpleResultSet)15