From a0345412e82f81da64ec74c325c10117eb050fbf Mon Sep 17 00:00:00 2001 From: catbref Date: Thu, 7 Jun 2018 09:15:54 +0100 Subject: [PATCH] first stab at repository interfaces and hsqldb implementations --- src/data/transaction/GenesisTransaction.java | 6 +-- src/repository/Repository.java | 9 ++++ src/repository/TransactionRepository.java | 9 ++++ .../hsqldb/HSQLDBGenesisTransaction.java | 30 +++++++++++++ src/repository/hsqldb/HSQLDBTransaction.java | 42 +++++++++++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/repository/Repository.java create mode 100644 src/repository/TransactionRepository.java create mode 100644 src/repository/hsqldb/HSQLDBGenesisTransaction.java create mode 100644 src/repository/hsqldb/HSQLDBTransaction.java diff --git a/src/data/transaction/GenesisTransaction.java b/src/data/transaction/GenesisTransaction.java index 0a6f5730..7c0f84e5 100644 --- a/src/data/transaction/GenesisTransaction.java +++ b/src/data/transaction/GenesisTransaction.java @@ -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); } diff --git a/src/repository/Repository.java b/src/repository/Repository.java new file mode 100644 index 00000000..19ea21d1 --- /dev/null +++ b/src/repository/Repository.java @@ -0,0 +1,9 @@ +package repository; + +public interface Repository { + + // public void save(); + + // public void delete(); + +} diff --git a/src/repository/TransactionRepository.java b/src/repository/TransactionRepository.java new file mode 100644 index 00000000..3fd9d61d --- /dev/null +++ b/src/repository/TransactionRepository.java @@ -0,0 +1,9 @@ +package repository; + +import data.transaction.Transaction; + +public interface TransactionRepository extends Repository { + + public Transaction fromSignature(byte[] signature); + +} diff --git a/src/repository/hsqldb/HSQLDBGenesisTransaction.java b/src/repository/hsqldb/HSQLDBGenesisTransaction.java new file mode 100644 index 00000000..5f2f1d6e --- /dev/null +++ b/src/repository/hsqldb/HSQLDBGenesisTransaction.java @@ -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; + } + } + +} diff --git a/src/repository/hsqldb/HSQLDBTransaction.java b/src/repository/hsqldb/HSQLDBTransaction.java new file mode 100644 index 00000000..fd429310 --- /dev/null +++ b/src/repository/hsqldb/HSQLDBTransaction.java @@ -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; + } + } + +}