qortal/src/qora/block/BlockFactory.java
catbref 5b78268915 Convertion to thread-local Connection
Much tidier code thanks to not having to pass Connection objects around
as params. Also no need for two forms of the same method, one with Connection
param, one without.

Also corrected SQL-Transaction-related methods in DB, e.g. commit, rollback, etc.
so they use the proper underlying JDBC methods.
2018-06-06 11:39:58 +01:00

59 lines
1.2 KiB
Java

package qora.block;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import database.DB;
import database.NoDataFoundException;
public class BlockFactory {
/**
* Load Block from DB using block signature.
*
* @param signature
* @return ? extends Block, or null if not found
* @throws SQLException
*/
public static Block fromSignature(byte[] signature) throws SQLException {
Block block = Block.fromSignature(signature);
if (block == null)
return null;
// Can we promote to a GenesisBlock?
if (GenesisBlock.isGenesisBlock(block))
return GenesisBlock.getInstance();
// Standard block
return block;
}
/**
* Load Block from DB using block height
*
* @param height
* @return ? extends Block, or null if not found
* @throws SQLException
*/
public static Block fromHeight(int height) throws SQLException {
if (height == 1)
return GenesisBlock.getInstance();
PreparedStatement preparedStatement = DB.getConnection().prepareStatement("SELECT signature FROM Blocks WHERE height = ?");
preparedStatement.setInt(1, height);
try {
return new Block(DB.checkedExecute(preparedStatement));
} catch (NoDataFoundException e) {
return null;
}
}
// Navigation
// Converters
// Processing
}