Search in sources :

Example 1 with SimpleRow

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

the class Recover method dumpMVStoreFile.

private void dumpMVStoreFile(PrintWriter writer, String fileName) {
    writer.println("-- MVStore");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB FOR \"" + this.getClass().getName() + ".readBlob\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB FOR \"" + this.getClass().getName() + ".readClob\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_DB FOR \"" + this.getClass().getName() + ".readBlobDb\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_DB FOR \"" + this.getClass().getName() + ".readClobDb\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_BLOB_MAP FOR \"" + this.getClass().getName() + ".readBlobMap\";");
    writer.println("CREATE ALIAS IF NOT EXISTS READ_CLOB_MAP FOR \"" + this.getClass().getName() + ".readClobMap\";");
    resetSchema();
    setDatabaseName(fileName.substring(0, fileName.length() - Constants.SUFFIX_MV_FILE.length()));
    MVStore mv = new MVStore.Builder().fileName(fileName).readOnly().open();
    dumpLobMaps(writer, mv);
    writer.println("-- Meta");
    dumpMeta(writer, mv);
    writer.println("-- Tables");
    TransactionStore store = new TransactionStore(mv);
    try {
        store.init();
    } catch (Throwable e) {
        writeError(writer, e);
    }
    try {
        for (String mapName : mv.getMapNames()) {
            if (!mapName.startsWith("table.")) {
                continue;
            }
            String tableId = mapName.substring("table.".length());
            ValueDataType keyType = new ValueDataType(null, this, null);
            ValueDataType valueType = new ValueDataType(null, this, null);
            TransactionMap<Value, Value> dataMap = store.begin().openMap(mapName, keyType, valueType);
            Iterator<Value> dataIt = dataMap.keyIterator(null);
            boolean init = false;
            while (dataIt.hasNext()) {
                Value rowId = dataIt.next();
                Value[] values = ((ValueArray) dataMap.get(rowId)).getList();
                recordLength = values.length;
                if (!init) {
                    setStorage(Integer.parseInt(tableId));
                    // init the column types
                    for (valueId = 0; valueId < recordLength; valueId++) {
                        String columnName = storageName + "." + valueId;
                        getSQL(columnName, values[valueId]);
                    }
                    createTemporaryTable(writer);
                    init = true;
                }
                StringBuilder buff = new StringBuilder();
                buff.append("INSERT INTO O_").append(tableId).append(" VALUES(");
                for (valueId = 0; valueId < recordLength; valueId++) {
                    if (valueId > 0) {
                        buff.append(", ");
                    }
                    String columnName = storageName + "." + valueId;
                    buff.append(getSQL(columnName, values[valueId]));
                }
                buff.append(");");
                writer.println(buff.toString());
                if (storageId == 0) {
                    try {
                        SimpleRow r = new SimpleRow(values);
                        MetaRecord meta = new MetaRecord(r);
                        schema.add(meta);
                        if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
                            String sql = values[3].getString();
                            String name = extractTableOrViewName(sql);
                            tableMap.put(meta.getId(), name);
                        }
                    } catch (Throwable t) {
                        writeError(writer, t);
                    }
                }
            }
        }
        writeSchema(writer);
        writer.println("DROP ALIAS READ_BLOB_MAP;");
        writer.println("DROP ALIAS READ_CLOB_MAP;");
        writer.println("DROP TABLE IF EXISTS INFORMATION_SCHEMA.LOB_BLOCKS;");
    } catch (Throwable e) {
        writeError(writer, e);
    } finally {
        mv.close();
    }
}
Also used : ValueDataType(org.h2.mvstore.db.ValueDataType) MVStore(org.h2.mvstore.MVStore) MetaRecord(org.h2.engine.MetaRecord) TransactionStore(org.h2.mvstore.db.TransactionStore) Value(org.h2.value.Value) SimpleRow(org.h2.result.SimpleRow) ValueArray(org.h2.value.ValueArray)

Example 2 with SimpleRow

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

the class Recover method writeRow.

private void writeRow(PrintWriter writer, Data s, Value[] data) {
    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ").append(storageName).append(" VALUES(");
    for (valueId = 0; valueId < recordLength; valueId++) {
        try {
            Value v = s.readValue();
            data[valueId] = v;
            if (valueId > 0) {
                sb.append(", ");
            }
            String columnName = storageName + "." + valueId;
            sb.append(getSQL(columnName, v));
        } catch (Exception e) {
            writeDataError(writer, "exception " + e, s.getBytes());
        } catch (OutOfMemoryError e) {
            writeDataError(writer, "out of memory", s.getBytes());
        }
    }
    sb.append(");");
    writer.println(sb.toString());
    if (storageId == 0) {
        try {
            SimpleRow r = new SimpleRow(data);
            MetaRecord meta = new MetaRecord(r);
            schema.add(meta);
            if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
                String sql = data[3].getString();
                String name = extractTableOrViewName(sql);
                tableMap.put(meta.getId(), name);
            }
        } catch (Throwable t) {
            writeError(writer, t);
        }
    }
}
Also used : MetaRecord(org.h2.engine.MetaRecord) Value(org.h2.value.Value) SimpleRow(org.h2.result.SimpleRow) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

MetaRecord (org.h2.engine.MetaRecord)2 SimpleRow (org.h2.result.SimpleRow)2 Value (org.h2.value.Value)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 DbException (org.h2.message.DbException)1 MVStore (org.h2.mvstore.MVStore)1 TransactionStore (org.h2.mvstore.db.TransactionStore)1 ValueDataType (org.h2.mvstore.db.ValueDataType)1 ValueArray (org.h2.value.ValueArray)1