Search in sources :

Example 56 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class Recover method dumpLobMaps.

private void dumpLobMaps(PrintWriter writer, MVStore mv) {
    lobMaps = mv.hasMap("lobData");
    if (!lobMaps) {
        return;
    }
    MVMap<Long, byte[]> lobData = mv.openMap("lobData");
    StreamStore streamStore = new StreamStore(lobData);
    MVMap<Long, Object[]> lobMap = mv.openMap("lobMap");
    writer.println("-- LOB");
    writer.println("CREATE TABLE IF NOT EXISTS " + "INFORMATION_SCHEMA.LOB_BLOCKS(" + "LOB_ID BIGINT, SEQ INT, DATA BINARY, " + "PRIMARY KEY(LOB_ID, SEQ));");
    boolean hasErrors = false;
    for (Entry<Long, Object[]> e : lobMap.entrySet()) {
        long lobId = e.getKey();
        Object[] value = e.getValue();
        byte[] streamStoreId = (byte[]) value[0];
        InputStream in = streamStore.get(streamStoreId);
        int len = 8 * 1024;
        byte[] block = new byte[len];
        try {
            for (int seq = 0; ; seq++) {
                int l = IOUtils.readFully(in, block, block.length);
                String x = StringUtils.convertBytesToHex(block, l);
                if (l > 0) {
                    writer.println("INSERT INTO INFORMATION_SCHEMA.LOB_BLOCKS " + "VALUES(" + lobId + ", " + seq + ", '" + x + "');");
                }
                if (l != len) {
                    break;
                }
            }
        } catch (IOException ex) {
            writeError(writer, ex);
            hasErrors = true;
        }
    }
    writer.println("-- lobMap.size: " + lobMap.sizeAsLong());
    writer.println("-- lobData.size: " + lobData.sizeAsLong());
    if (hasErrors) {
        writer.println("-- lobMap");
        for (Long k : lobMap.keyList()) {
            Object[] value = lobMap.get(k);
            byte[] streamStoreId = (byte[]) value[0];
            writer.println("--     " + k + " " + StreamStore.toString(streamStoreId));
        }
        writer.println("-- lobData");
        for (Long k : lobData.keyList()) {
            writer.println("--     " + k + " len " + lobData.get(k).length);
        }
    }
}
Also used : StreamStore(org.h2.mvstore.StreamStore) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ValueLong(org.h2.value.ValueLong) DbObject(org.h2.engine.DbObject)

Example 57 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class Db method upgradeDb.

Db upgradeDb() {
    if (!upgradeChecked.contains(dbUpgrader.getClass())) {
        // flag as checked immediately because calls are nested.
        upgradeChecked.add(dbUpgrader.getClass());
        JQDatabase model = dbUpgrader.getClass().getAnnotation(JQDatabase.class);
        if (model.version() > 0) {
            DbVersion v = new DbVersion();
            DbVersion dbVersion = // (SCHEMA="" && TABLE="") == DATABASE
            from(v).where(v.schema).is("").and(v.table).is("").selectFirst();
            if (dbVersion == null) {
                // database has no version registration, but model specifies
                // version: insert DbVersion entry and return.
                DbVersion newDb = new DbVersion(model.version());
                insert(newDb);
            } else {
                // check to see if upgrade is required.
                if ((model.version() > dbVersion.version) && (dbUpgrader != null)) {
                    // database is an older version than the model
                    boolean success = dbUpgrader.upgradeDatabase(this, dbVersion.version, model.version());
                    if (success) {
                        dbVersion.version = model.version();
                        update(dbVersion);
                    }
                }
            }
        }
    }
    return this;
}
Also used : JQDatabase(org.h2.jaqu.Table.JQDatabase)

Example 58 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class JdbcResultSet method updateBinaryStream.

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @param length the number of characters
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("updateBinaryStream(" + quote(columnLabel) + ", x, " + length + "L);");
        }
        checkClosed();
        Value v = conn.createBlob(x, length);
        update(columnLabel, v);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 59 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class JdbcResultSet method updateBlob.

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateBlob(String columnLabel, Blob x) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("updateBlob(" + quote(columnLabel) + ", x);");
        }
        checkClosed();
        Value v;
        if (x == null) {
            v = ValueNull.INSTANCE;
        } else {
            v = conn.createBlob(x.getBinaryStream(), -1);
        }
        update(columnLabel, v);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 60 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class JdbcResultSet method updateNCharacterStream.

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @param length the number of characters
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateNCharacterStream(String columnLabel, Reader x, long length) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("updateNCharacterStream(" + quote(columnLabel) + ", x, " + length + "L);");
        }
        checkClosed();
        Value v = conn.createClob(x, length);
        update(columnLabel, v);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

Statement (java.sql.Statement)215 ResultSet (java.sql.ResultSet)205 PreparedStatement (java.sql.PreparedStatement)202 Connection (java.sql.Connection)201 JdbcConnection (org.h2.jdbc.JdbcConnection)99 SimpleResultSet (org.h2.tools.SimpleResultSet)64 SQLException (java.sql.SQLException)56 JdbcStatement (org.h2.jdbc.JdbcStatement)46 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)35 Savepoint (java.sql.Savepoint)32 Random (java.util.Random)28 Value (org.h2.value.Value)28 DbException (org.h2.message.DbException)27 Column (org.h2.table.Column)18 Task (org.h2.util.Task)17 ValueString (org.h2.value.ValueString)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 StringReader (java.io.StringReader)12 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)11