use super::claim::Condition;
use super::{BuiltIn, SqlWriter};
use crate::role::Role;
use crate::spec::TableSpec;
use crate::statement::Param;
const HEAD: [Condition; 2] = [Condition::InGroup, Condition::Unfinished];
const TAKING: [Condition; 2] = [Condition::Due, Condition::Free];
impl<D> SqlWriter<'_, D>
where
D: BuiltIn + ?Sized,
{
pub(super) fn head_conditions(&mut self, spec: &TableSpec<'_>) -> &mut Self {
self.conditions(spec, &HEAD, " WHERE ")
}
pub(super) fn taking_conditions(&mut self, spec: &TableSpec<'_>) -> &mut Self {
self.conditions(spec, &TAKING, " AND ")
}
#[cfg(feature = "mysql")]
pub(crate) fn group_count(&mut self, spec: &TableSpec<'_>, lock: &str) -> &mut Self {
self.push("SELECT COUNT(*) FROM ")
.table(spec)
.head_conditions(spec)
.push(lock)
}
pub(super) fn group_free(&mut self, spec: &TableSpec<'_>) -> &mut Self {
let (Some(group), Some(expiry)) =
(spec.column(Role::Group), spec.column(Role::LockedUntil))
else {
return self;
};
self.push(" AND NOT EXISTS (SELECT 1 FROM ")
.table(spec)
.push(" AS __work WHERE __work.")
.ident(group.name())
.push(" = ")
.param(Param::Group)
.push(" AND __work.")
.ident(expiry.name())
.push(" > ")
.param(Param::LeaseNow)
.push(")")
}
}