toasty 0.4.0

An async ORM for Rust supporting SQL and NoSQL databases
Documentation
use crate::engine::exec::{
    DeleteByKey, Eval, ExecStatement, Filter, FindPkByIndex, GetByKey, Guard, NestedMerge, Project,
    QueryPk, ReadModifyWrite, SetVar, UpdateByKey,
};

use std::fmt;

pub(crate) enum Action {
    /// Delete a record by the primary key
    DeleteByKey(DeleteByKey),

    /// Evaluate a function in memory
    Eval(Eval),

    /// Execute a statement
    ExecStatement(Box<ExecStatement>),

    /// Filter a value stream
    Filter(Filter),

    FindPkByIndex(FindPkByIndex),

    /// Execute `Operation::GetByKey` using key input
    GetByKey(GetByKey),

    /// Conditionally pass through or suppress a data stream
    Guard(Guard),

    /// Combines parent and child data into nested structures.
    ///
    /// Loads all batch data upfront, then recursively processes each row by filtering
    /// and merging child data at all nesting levels, finally projecting each row with
    /// its nested children into the final result.
    NestedMerge(NestedMerge),

    /// Take the contents of a variable and project it one or more times to a
    /// specified variable.
    Project(Project),

    /// Query records by primary key
    QueryPk(QueryPk),

    /// Perform an atomic operation in multiple steps
    ReadModifyWrite(Box<ReadModifyWrite>),

    /// Set a variable to a const
    SetVar(SetVar),

    /// Update a record by the primary key
    UpdateByKey(UpdateByKey),
}

impl Action {
    /// Returns the action variant name for logging.
    pub(crate) fn name(&self) -> &'static str {
        match self {
            Action::DeleteByKey(_) => "delete_by_key",
            Action::Eval(_) => "eval",
            Action::ExecStatement(_) => "exec_statement",
            Action::Filter(_) => "filter",
            Action::FindPkByIndex(_) => "find_pk_by_index",
            Action::GetByKey(_) => "get_by_key",
            Action::Guard(_) => "guard",
            Action::NestedMerge(_) => "nested_merge",
            Action::Project(_) => "project",
            Action::QueryPk(_) => "query_pk",
            Action::ReadModifyWrite(_) => "read_modify_write",
            Action::SetVar(_) => "set_var",
            Action::UpdateByKey(_) => "update_by_key",
        }
    }

    /// Returns true if this action issues a database operation.
    ///
    /// Used to determine whether a plan needs to be wrapped in a transaction.
    /// In-memory actions (Filter, Project, NestedMerge, SetVar, Eval) return false.
    pub(crate) fn is_db_op(&self) -> bool {
        match self {
            Action::DeleteByKey(_)
            | Action::ExecStatement(_)
            | Action::FindPkByIndex(_)
            | Action::GetByKey(_)
            | Action::QueryPk(_)
            | Action::ReadModifyWrite(_)
            | Action::UpdateByKey(_) => true,

            Action::Eval(_)
            | Action::Filter(_)
            | Action::Guard(_)
            | Action::NestedMerge(_)
            | Action::Project(_)
            | Action::SetVar(_) => false,
        }
    }
}

impl fmt::Debug for Action {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DeleteByKey(a) => a.fmt(f),
            Self::Eval(a) => a.fmt(f),
            Self::ExecStatement(a) => a.fmt(f),
            Self::Filter(a) => a.fmt(f),
            Self::FindPkByIndex(a) => a.fmt(f),
            Self::GetByKey(a) => a.fmt(f),
            Self::Guard(a) => a.fmt(f),
            Self::NestedMerge(a) => a.fmt(f),
            Self::QueryPk(a) => a.fmt(f),
            Self::ReadModifyWrite(a) => a.fmt(f),
            Self::Project(a) => a.fmt(f),
            Self::SetVar(a) => a.fmt(f),
            Self::UpdateByKey(a) => a.fmt(f),
        }
    }
}