use holo_hash::DhtOpHash;
use sqlx::{Executor, Sqlite};
pub(crate) async fn op_exists<'e, E>(executor: E, hash: &DhtOpHash) -> sqlx::Result<bool>
where
E: Executor<'e, Database = Sqlite>,
{
let bytes = hash.get_raw_36();
let row: (i64,) = sqlx::query_as(
"SELECT EXISTS (
SELECT 1 FROM ChainOp WHERE locally_validated = 1 AND hash = ?1
UNION ALL
SELECT 1 FROM LimboChainOp WHERE hash = ?1
UNION ALL
SELECT 1 FROM WarrantOp WHERE hash = ?1
UNION ALL
SELECT 1 FROM LimboWarrantOp WHERE hash = ?1
LIMIT 1
)",
)
.bind(bytes)
.fetch_one(executor)
.await?;
Ok(row.0 != 0)
}
pub(crate) async fn op_hashes_present<'e, E>(
executor: E,
hashes: &[DhtOpHash],
) -> sqlx::Result<Vec<bool>>
where
E: Executor<'e, Database = Sqlite> + Copy,
{
let mut out = Vec::with_capacity(hashes.len());
for h in hashes {
out.push(op_exists(executor, h).await?);
}
Ok(out)
}