/******************************************************************************
* Copyright © 2013-2016 The Nxt Core Developers. *
* *
* See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Nxt software, including this file, may be copied, modified, propagated, *
* or distributed except according to the terms contained in the LICENSE.txt *
* file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
package nxt.db;
import nxt.Db;
import nxt.Nxt;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public abstract class DerivedDbTable {
protected static final TransactionalDb db = Db.db;
protected final String table;
protected DerivedDbTable(String table) {
this.table = table;
Nxt.getBlockchainProcessor().registerDerivedTable(this);
}
public void rollback(int height) {
if (!db.isInTransaction()) {
throw new IllegalStateException("Not in transaction");
}
try (Connection con = db.getConnection();
PreparedStatement pstmtDelete = con.prepareStatement("DELETE FROM " + table + " WHERE height > ?")) {
pstmtDelete.setInt(1, height);
pstmtDelete.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e.toString(), e);
}
}
public void truncate() {
if (!db.isInTransaction()) {
throw new IllegalStateException("Not in transaction");
}
try (Connection con = db.getConnection();
Statement stmt = con.createStatement()) {
stmt.executeUpdate("TRUNCATE TABLE " + table);
} catch (SQLException e) {
throw new RuntimeException(e.toString(), e);
}
}
public void trim(int height) {
//nothing to trim
}
public void createSearchIndex(Connection con) throws SQLException {
//implemented in EntityDbTable only
}
public boolean isPersistent() {
return false;
}
@Override
public final String toString() {
return table;
}
}