#[cfg(test)]
use crate::db::session::sql::write_policy::SqlWriteReturningBounds;
use crate::db::{
session::sql::write_policy::{
DEFAULT_PUBLIC_BOUNDED_WRITE_LIMIT, DEFAULT_PUBLIC_WRITE_RETURNING_RESPONSE_BYTES,
SqlWriteBoundedPolicyRejection, SqlWriteExecutionBounds, SqlWriteExposureClass,
SqlWritePlanCore, SqlWritePolicyBounds, SqlWriteShapePolicyRejection,
SqlWriteStatementShape,
},
sql::parser::SqlDeleteStatement,
};
#[doc(hidden)]
pub(in crate::db) const DEFAULT_PUBLIC_BOUNDED_DELETE_LIMIT: u32 =
DEFAULT_PUBLIC_BOUNDED_WRITE_LIMIT;
#[doc(hidden)]
pub(in crate::db) const DEFAULT_PUBLIC_DELETE_RETURNING_RESPONSE_BYTES: u32 =
DEFAULT_PUBLIC_WRITE_RETURNING_RESPONSE_BYTES;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) enum SqlDeleteExposurePolicy {
PublicPrimaryKeyOnly,
PublicBoundedDeterministic,
}
impl SqlDeleteExposurePolicy {
pub(super) const fn exposure_class(self) -> SqlWriteExposureClass {
match self {
Self::PublicPrimaryKeyOnly => SqlWriteExposureClass::PublicPrimaryKeyOnly,
Self::PublicBoundedDeterministic => SqlWriteExposureClass::PublicBoundedDeterministic,
}
}
}
#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub(in crate::db) struct SqlDeletePolicyContext<'a> {
pub primary_key_fields: &'a [&'a str],
pub max_public_bounded_limit: u32,
pub max_returning_rows: Option<u32>,
pub max_returning_response_bytes: Option<u32>,
}
impl<'a> SqlDeletePolicyContext<'a> {
#[cfg(test)]
#[must_use]
pub(in crate::db) const fn new(primary_key_fields: &'a [&'a str]) -> Self {
Self {
primary_key_fields,
max_public_bounded_limit: DEFAULT_PUBLIC_BOUNDED_DELETE_LIMIT,
max_returning_rows: None,
max_returning_response_bytes: None,
}
}
pub(super) const fn write_bounds(self) -> SqlWritePolicyBounds {
SqlWritePolicyBounds::new(
self.max_public_bounded_limit,
self.max_returning_rows,
self.max_returning_response_bytes,
)
}
#[must_use]
pub(in crate::db) const fn public_generated(primary_key_fields: &'a [&'a str]) -> Self {
Self {
primary_key_fields,
max_public_bounded_limit: DEFAULT_PUBLIC_BOUNDED_DELETE_LIMIT,
max_returning_rows: None,
max_returning_response_bytes: Some(DEFAULT_PUBLIC_DELETE_RETURNING_RESPONSE_BYTES),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) struct SqlDeleteStatementClassification {
pub target_entity: String,
pub write_shape: SqlWriteStatementShape,
}
pub(super) type SqlDeletePlanCore = SqlWritePlanCore<SqlDeleteStatement>;
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) enum SqlValidatedDeletePlan {
PublicPrimaryKeyOnly(SqlPublicPrimaryKeyDeletePlan),
PublicBoundedDeterministic(SqlPublicBoundedDeletePlan),
}
impl SqlValidatedDeletePlan {
#[cfg(test)]
#[must_use]
pub(in crate::db) const fn execution_bounds(&self) -> SqlWriteExecutionBounds {
match self {
Self::PublicPrimaryKeyOnly(plan) => plan.core.execution_bounds(),
Self::PublicBoundedDeterministic(plan) => plan.core.execution_bounds(),
}
}
#[cfg(test)]
#[must_use]
pub(in crate::db) const fn returning_bounds(&self) -> SqlWriteReturningBounds {
self.execution_bounds().returning
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) struct SqlPublicPrimaryKeyDeletePlan {
pub(super) core: SqlDeletePlanCore,
}
impl SqlPublicPrimaryKeyDeletePlan {
pub(in crate::db::session::sql) const fn statement(&self) -> &SqlDeleteStatement {
self.core.statement()
}
#[must_use]
pub(in crate::db) const fn execution_bounds(&self) -> SqlWriteExecutionBounds {
self.core.execution_bounds()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) struct SqlPublicBoundedDeletePlan {
pub(super) core: SqlDeletePlanCore,
}
impl SqlPublicBoundedDeletePlan {
pub(in crate::db::session::sql) const fn statement(&self) -> &SqlDeleteStatement {
self.core.statement()
}
#[must_use]
pub(in crate::db) const fn execution_bounds(&self) -> SqlWriteExecutionBounds {
self.core.execution_bounds()
}
#[cfg(test)]
pub(in crate::db) const fn set_execution_bounds_for_tests(
&mut self,
execution_bounds: SqlWriteExecutionBounds,
) {
self.core.set_execution_bounds_for_tests(execution_bounds);
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) enum SqlDeletePolicyRejection {
NotDelete,
MissingWhere,
PrimaryKeyProofFailed,
MissingCanonicalPrimaryKeyOrder,
DescendingOrder,
MissingLimit,
OffsetUnsupported,
LimitTooHigh,
}
impl SqlDeletePolicyRejection {
const fn from_bounded_write_rejection(rejection: SqlWriteBoundedPolicyRejection) -> Self {
match rejection {
SqlWriteBoundedPolicyRejection::MissingCanonicalPrimaryKeyOrder => {
Self::MissingCanonicalPrimaryKeyOrder
}
SqlWriteBoundedPolicyRejection::DescendingOrder => Self::DescendingOrder,
SqlWriteBoundedPolicyRejection::MissingLimit => Self::MissingLimit,
SqlWriteBoundedPolicyRejection::OffsetUnsupported => Self::OffsetUnsupported,
SqlWriteBoundedPolicyRejection::LimitTooHigh => Self::LimitTooHigh,
}
}
pub(super) const fn from_write_shape_rejection(
rejection: SqlWriteShapePolicyRejection,
) -> Self {
match rejection {
SqlWriteShapePolicyRejection::MissingWhere => Self::MissingWhere,
SqlWriteShapePolicyRejection::PrimaryKeyProofFailed => Self::PrimaryKeyProofFailed,
SqlWriteShapePolicyRejection::Bounded(rejection) => {
Self::from_bounded_write_rejection(rejection)
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub(in crate::db) struct SqlDeletePolicyReport {
pub classification: Option<SqlDeleteStatementClassification>,
pub plan: Option<SqlValidatedDeletePlan>,
pub rejection: Option<SqlDeletePolicyRejection>,
}
impl SqlDeletePolicyReport {
#[cfg(test)]
#[must_use]
pub(in crate::db) const fn is_admitted(&self) -> bool {
self.rejection.is_none()
}
pub(super) const fn admitted(
classification: SqlDeleteStatementClassification,
plan: SqlValidatedDeletePlan,
) -> Self {
Self {
classification: Some(classification),
plan: Some(plan),
rejection: None,
}
}
pub(super) const fn classified_rejection(
classification: SqlDeleteStatementClassification,
rejection: SqlDeletePolicyRejection,
) -> Self {
Self {
classification: Some(classification),
plan: None,
rejection: Some(rejection),
}
}
pub(super) const fn rejected(rejection: SqlDeletePolicyRejection) -> Self {
Self {
classification: None,
plan: None,
rejection: Some(rejection),
}
}
}