use super::super::output_observation_condition;
use super::codes::{CONTRADICTION, NOT_COVERED, UNSUPPORTED};
pub(super) fn related_transaction_sql() -> String {
format!(
"(
EXISTS (SELECT 1 FROM transparent_received_outputs o
WHERE o.transaction_id = t.id_tx AND ({}))
OR EXISTS (SELECT 1 FROM transparent_received_output_spends s
JOIN transparent_received_outputs so
ON so.id = s.transparent_received_output_id
WHERE s.transaction_id = t.id_tx AND ({}))
OR EXISTS (SELECT 1 FROM transparent_spend_map m
WHERE m.spending_transaction_id = t.id_tx)
OR EXISTS (SELECT 1 FROM ironwood_enhance_routing r
WHERE r.transaction_id = t.id_tx AND r.route = 2)
)",
output_observation_condition("o"),
output_observation_condition("so"),
)
}
pub(super) fn listable_work_sql() -> String {
format!(
"t.raw IS NULL AND {}
AND NOT (:public AND (
EXISTS (SELECT 1 FROM tx_retrieval_queue q
WHERE q.txid = t.txid AND q.query_type = :enhancement)
OR EXISTS (SELECT 1 FROM ironwood_enhance_routing p
WHERE p.transaction_id = t.id_tx AND p.route = 0)
))",
related_transaction_sql()
)
}
pub(super) fn eligible_work_sql() -> String {
format!("t.mined_height IS NOT NULL AND {}", listable_work_sql())
}
pub(super) const REARMED: &str =
"(w.attempted_height IS NOT NULL AND w.attempted_height != t.mined_height)";
pub(super) fn parked_work_sql() -> String {
format!(
"NOT :public
AND ((w.last_outcome IN ({NOT_COVERED}, {CONTRADICTION})
AND (:map IS NULL OR w.last_map_sha256 IS :map))
OR (w.last_outcome = {UNSUPPORTED} AND :map IS NULL))
AND IFNULL(w.attempted_at, 0) + :backstop > :now"
)
}
pub(super) const ROUTE_TWO: &str = "EXISTS (SELECT 1 FROM ironwood_enhance_routing r
WHERE r.transaction_id = t.id_tx AND r.route = 2)";