mithril_persistence/database/record/
storable_cardano_transaction.rs

1use sqlite::Row;
2
3use mithril_common::entities::{BlockHash, TransactionHash};
4
5use crate::sqlite::{HydrationError, Projection, SqLiteEntity};
6
7/// The record representation of a cardano transaction that can be stored in the database.
8#[derive(Debug, PartialEq, Clone)]
9pub struct StorableCardanoTransactionRecord {
10    /// Unique hash of the transaction
11    pub transaction_hash: TransactionHash,
12
13    /// Block hash of the transaction
14    pub block_hash: BlockHash,
15}
16
17impl StorableCardanoTransactionRecord {
18    /// SQLite max variables per prepared query are `32 766`, given each record needs to bind two variables and to leave some
19    /// room for other variables (i.e. in WHERE clause) we fix this limit to 10k, meaning 20 000 binds at once maximum
20    pub const MAX_PER_INSERT: usize = 10_000;
21
22    /// StorableCardanoTransactionRecord factory
23    pub fn new<T: Into<TransactionHash>, U: Into<BlockHash>>(hash: T, block_hash: U) -> Self {
24        Self {
25            transaction_hash: hash.into(),
26            block_hash: block_hash.into(),
27        }
28    }
29}
30
31impl SqLiteEntity for StorableCardanoTransactionRecord {
32    fn hydrate(row: Row) -> Result<Self, HydrationError>
33    where
34        Self: Sized,
35    {
36        let transaction_hash = row.read::<&str, _>(0);
37        let block_hash = row.read::<&str, _>(1);
38
39        Ok(Self {
40            transaction_hash: transaction_hash.to_string(),
41            block_hash: block_hash.to_string(),
42        })
43    }
44
45    fn get_projection() -> Projection {
46        Projection::from(&[
47            (
48                "transaction_hash",
49                "{:cardano_tx:}.transaction_hash",
50                "text",
51            ),
52            ("block_hash", "{:cardano_tx:}.block_hash", "text"),
53        ])
54    }
55}