/** * Save the ledger entry * * @param con Database connection * @throws SQLException Database error occurred */ private void save(Connection con) throws SQLException { try (PreparedStatement stmt = con.prepareStatement( "INSERT INTO account_ledger " + "(account_id, event_type, event_id, holding_type, holding_id, change, balance, " + "block_id, height, timestamp) " + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { int i = 0; stmt.setLong(++i, accountId); stmt.setByte(++i, (byte) event.getCode()); stmt.setLong(++i, eventId); if (holding != null) { stmt.setByte(++i, (byte) holding.getCode()); } else { stmt.setByte(++i, (byte) -1); } DbUtils.setLong(stmt, ++i, holdingId); stmt.setLong(++i, change); stmt.setLong(++i, balance); stmt.setLong(++i, blockId); stmt.setInt(++i, height); stmt.setInt(++i, timestamp); stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { ledgerId = rs.getLong(1); } } } }
/** * Return the hash code * * @return Hash code */ @Override public int hashCode() { return (Long.hashCode(accountId) ^ event.getCode() ^ Long.hashCode(eventId) ^ (holding != null ? holding.getCode() : 0) ^ (holdingId != null ? Long.hashCode(holdingId) : 0)); }
/** * Return the ledger entries sorted in descending insert order * * @param accountId Account identifier or zero if no account identifier * @param event Ledger event or null * @param eventId Ledger event identifier or zero if no event identifier * @param holding Ledger holding or null * @param holdingId Ledger holding identifier or zero if no holding identifier * @param firstIndex First matching entry index, inclusive * @param lastIndex Last matching entry index, inclusive * @return List of ledger entries */ public static List<LedgerEntry> getEntries( long accountId, LedgerEvent event, long eventId, LedgerHolding holding, long holdingId, int firstIndex, int lastIndex) { if (!ledgerEnabled) { return Collections.emptyList(); } List<LedgerEntry> entryList = new ArrayList<>(); // // Build the SELECT statement to search the entries StringBuilder sb = new StringBuilder(128); sb.append("SELECT * FROM account_ledger "); if (accountId != 0 || event != null || holding != null) { sb.append("WHERE "); } if (accountId != 0) { sb.append("account_id = ? "); } if (event != null) { if (accountId != 0) { sb.append("AND "); } sb.append("event_type = ? "); if (eventId != 0) sb.append("AND event_id = ? "); } if (holding != null) { if (accountId != 0 || event != null) { sb.append("AND "); } sb.append("holding_type = ? "); if (holdingId != 0) sb.append("AND holding_id = ? "); } sb.append("ORDER BY db_id DESC "); sb.append(DbUtils.limitsClause(firstIndex, lastIndex)); // // Get the ledger entries // blockchain.readLock(); try (Connection con = Db.db.getConnection(); PreparedStatement pstmt = con.prepareStatement(sb.toString())) { int i = 0; if (accountId != 0) { pstmt.setLong(++i, accountId); } if (event != null) { pstmt.setByte(++i, (byte) event.getCode()); if (eventId != 0) { pstmt.setLong(++i, eventId); } } if (holding != null) { pstmt.setByte(++i, (byte) holding.getCode()); if (holdingId != 0) { pstmt.setLong(++i, holdingId); } } DbUtils.setLimits(++i, pstmt, firstIndex, lastIndex); try (ResultSet rs = pstmt.executeQuery()) { while (rs.next()) { entryList.add(new LedgerEntry(rs)); } } } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } finally { blockchain.readUnlock(); } return entryList; }