use crate::{DdlStatement, ProcedureMutability, ProcedureRegistry, Statement};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StatementCategory {
ReadOnly,
DataModifying,
CatalogModifying,
Maintenance,
TransactionControl,
SessionControl,
}
pub(crate) fn classify(
statement: &Statement,
registry: &dyn ProcedureRegistry,
) -> StatementCategory {
match statement {
Statement::Query(_) | Statement::Composite { .. } | Statement::Chained { .. } => {
StatementCategory::ReadOnly
}
Statement::Mutate(_) => StatementCategory::DataModifying,
Statement::Ddl(statement) => classify_ddl(statement),
Statement::Call(call) => registry
.lookup(&call.name)
.map(|metadata| classify_mutability(metadata.mutability))
.unwrap_or(StatementCategory::ReadOnly),
Statement::Explain { .. } => StatementCategory::ReadOnly,
Statement::StartTransaction { .. }
| Statement::Commit { .. }
| Statement::Rollback { .. } => StatementCategory::TransactionControl,
Statement::SessionSetValue { .. }
| Statement::SessionSetTimeZone { .. }
| Statement::SessionSetGraph { .. }
| Statement::SessionReset { .. }
| Statement::SessionClose { .. } => StatementCategory::SessionControl,
}
}
const fn classify_ddl(statement: &DdlStatement) -> StatementCategory {
match statement {
DdlStatement::ShowNodeTypes(_)
| DdlStatement::ShowEdgeTypes(_)
| DdlStatement::ShowIndexes(_)
| DdlStatement::ShowProcedures(_) => StatementCategory::ReadOnly,
DdlStatement::CreateGraph { .. }
| DdlStatement::DropGraph { .. }
| DdlStatement::CreateNodeType { .. }
| DdlStatement::CreateEdgeType { .. }
| DdlStatement::DropNodeType { .. }
| DdlStatement::DropEdgeType { .. }
| DdlStatement::CreateIndex { .. }
| DdlStatement::DropIndex { .. } => StatementCategory::CatalogModifying,
DdlStatement::TruncateNodeType { .. } | DdlStatement::TruncateEdgeType { .. } => {
StatementCategory::DataModifying
}
}
}
const fn classify_mutability(mutability: ProcedureMutability) -> StatementCategory {
match mutability {
ProcedureMutability::Read => StatementCategory::ReadOnly,
ProcedureMutability::SchemaWrite => StatementCategory::CatalogModifying,
ProcedureMutability::MaintenanceWrite => StatementCategory::Maintenance,
}
}