use crate::models::dht::ChainOpRow;
use holo_hash::{ActionHash, AnyDhtHash, DhtOpHash};
use holochain_integrity_types::dht_v2::RecordValidity;
use holochain_timestamp::Timestamp;
use sqlx::{Executor, Sqlite};
pub struct InsertChainOp<'a> {
pub op_hash: &'a DhtOpHash,
pub action_hash: &'a ActionHash,
pub op_type: i64,
pub basis_hash: &'a AnyDhtHash,
pub storage_center_loc: u32,
pub validation_status: RecordValidity,
pub locally_validated: bool,
pub when_received: Timestamp,
pub when_integrated: Timestamp,
pub serialized_size: u32,
}
pub(crate) async fn insert_chain_op<'a, 'e, E>(
executor: E,
op: InsertChainOp<'a>,
) -> sqlx::Result<()>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query(
"INSERT INTO ChainOp
(hash, op_type, action_hash, basis_hash, storage_center_loc,
validation_status, locally_validated, when_received, when_integrated,
serialized_size)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
)
.bind(op.op_hash.get_raw_36())
.bind(op.op_type)
.bind(op.action_hash.get_raw_36())
.bind(op.basis_hash.get_raw_36())
.bind(op.storage_center_loc as i64)
.bind(i64::from(op.validation_status))
.bind(op.locally_validated as i64)
.bind(op.when_received.as_micros())
.bind(op.when_integrated.as_micros())
.bind(op.serialized_size as i64)
.execute(executor)
.await?;
Ok(())
}
pub(crate) async fn get_chain_op<'e, E>(
executor: E,
hash: DhtOpHash,
) -> sqlx::Result<Option<ChainOpRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as(
"SELECT hash, op_type, action_hash, basis_hash, storage_center_loc,
validation_status, locally_validated, when_received, when_integrated,
serialized_size
FROM ChainOp WHERE hash = ?",
)
.bind(hash.get_raw_36())
.fetch_optional(executor)
.await
}
pub(crate) async fn get_chain_ops_by_basis<'e, E>(
executor: E,
basis: AnyDhtHash,
) -> sqlx::Result<Vec<ChainOpRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as("SELECT * FROM ChainOp WHERE basis_hash = ? ORDER BY when_integrated")
.bind(basis.get_raw_36())
.fetch_all(executor)
.await
}
pub(crate) async fn get_chain_ops_for_action<'e, E>(
executor: E,
action_hash: ActionHash,
) -> sqlx::Result<Vec<ChainOpRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as("SELECT * FROM ChainOp WHERE action_hash = ? ORDER BY op_type")
.bind(action_hash.get_raw_36())
.fetch_all(executor)
.await
}