first stab at repository interfaces and hsqldb implementations

This commit is contained in:
catbref 2018-06-07 09:15:54 +01:00
parent 2ea6f12b3c
commit a0345412e8
5 changed files with 93 additions and 3 deletions

View File

@ -13,14 +13,14 @@ public class GenesisTransaction extends Transaction {
// Constructors
public GenesisTransaction(String recipient, BigDecimal amount, long timestamp, byte[] signature) {
public GenesisTransaction(Account recipient, BigDecimal amount, long timestamp, byte[] signature) {
super(TransactionType.GENESIS, BigDecimal.ZERO, new GenesisAccount(), timestamp, signature);
this.recipient = new Account(recipient);
this.recipient = recipient;
this.amount = amount;
}
public GenesisTransaction(String recipient, BigDecimal amount, long timestamp) {
public GenesisTransaction(Account recipient, BigDecimal amount, long timestamp) {
this(recipient, amount, timestamp, null);
}

View File

@ -0,0 +1,9 @@
package repository;
public interface Repository {
// public void save();
// public void delete();
}

View File

@ -0,0 +1,9 @@
package repository;
import data.transaction.Transaction;
public interface TransactionRepository extends Repository {
public Transaction fromSignature(byte[] signature);
}

View File

@ -0,0 +1,30 @@
package repository.hsqldb;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import data.account.Account;
import data.account.PublicKeyAccount;
import data.transaction.GenesisTransaction;
import data.transaction.Transaction;
import database.DB;
public class HSQLDBGenesisTransaction extends HSQLDBTransaction {
protected Transaction fromSignature(byte[] signature, byte[] reference, PublicKeyAccount creator, long timestamp, BigDecimal fee) {
try {
ResultSet rs = DB.checkedExecute("SELECT recipient, amount FROM GenesisTransactions WHERE signature = ?", signature);
if (rs == null)
return null;
Account recipient = new Account(rs.getString(1));
BigDecimal amount = rs.getBigDecimal(2).setScale(8);
return new GenesisTransaction(recipient, amount, timestamp, signature);
} catch (SQLException e) {
return null;
}
}
}

View File

@ -0,0 +1,42 @@
package repository.hsqldb;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import database.DB;
import data.account.PublicKeyAccount;
import data.transaction.Transaction;
import data.transaction.Transaction.TransactionType;
import repository.TransactionRepository;
public class HSQLDBTransaction implements TransactionRepository {
private static final int REFERENCE_LENGTH = 64;
public Transaction fromSignature(byte[] signature) {
try {
ResultSet rs = DB.checkedExecute("SELECT type, reference, creator, creation, fee FROM Transactions WHERE signature = ?", signature);
if (rs == null)
return null;
TransactionType type = TransactionType.valueOf(rs.getInt(1));
byte[] reference = DB.getResultSetBytes(rs.getBinaryStream(1), REFERENCE_LENGTH);
// Note: can't use CREATOR_LENGTH in case we encounter Genesis Account's short, 8-byte public key
PublicKeyAccount creator = new PublicKeyAccount(DB.getResultSetBytes(rs.getBinaryStream(2)));
long timestamp = rs.getTimestamp(3).getTime();
BigDecimal fee = rs.getBigDecimal(4).setScale(8);
switch (type) {
case GENESIS:
return new HSQLDBGenesisTransaction().fromSignature(signature, reference, creator, timestamp, fee);
default:
return null;
}
} catch (SQLException e) {
return null;
}
}
}