use crate::types::{DynIden, IntoIden};
#[derive(Debug, Clone, Default)]
pub struct VacuumOption {
pub(crate) full: bool,
pub(crate) freeze: bool,
pub(crate) verbose: bool,
pub(crate) analyze: bool,
}
impl VacuumOption {
pub fn new() -> Self {
Self::default()
}
pub fn full(mut self, full: bool) -> Self {
self.full = full;
self
}
pub fn freeze(mut self, freeze: bool) -> Self {
self.freeze = freeze;
self
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn analyze(mut self, analyze: bool) -> Self {
self.analyze = analyze;
self
}
}
#[derive(Debug, Clone)]
pub struct AnalyzeTable {
#[allow(dead_code)]
pub(crate) table: DynIden,
pub(crate) columns: Vec<DynIden>,
}
impl AnalyzeTable {
pub fn new<T: IntoIden>(table: T) -> Self {
Self {
table: table.into_iden(),
columns: Vec::new(),
}
}
pub fn add_column<C: IntoIden>(mut self, column: C) -> Self {
self.columns.push(column.into_iden());
self
}
}
#[derive(Debug, Clone, Default)]
pub struct OptimizeTableOption {
pub(crate) no_write_to_binlog: bool,
pub(crate) local: bool,
}
impl OptimizeTableOption {
pub fn new() -> Self {
Self::default()
}
pub fn no_write_to_binlog(mut self, no_write_to_binlog: bool) -> Self {
self.no_write_to_binlog = no_write_to_binlog;
self
}
pub fn local(mut self, local: bool) -> Self {
self.local = local;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct RepairTableOption {
pub(crate) no_write_to_binlog: bool,
pub(crate) local: bool,
pub(crate) quick: bool,
pub(crate) extended: bool,
pub(crate) use_frm: bool,
}
impl RepairTableOption {
pub fn new() -> Self {
Self::default()
}
pub fn no_write_to_binlog(mut self, no_write_to_binlog: bool) -> Self {
self.no_write_to_binlog = no_write_to_binlog;
self
}
pub fn local(mut self, local: bool) -> Self {
self.local = local;
self
}
pub fn quick(mut self, quick: bool) -> Self {
self.quick = quick;
self
}
pub fn extended(mut self, extended: bool) -> Self {
self.extended = extended;
self
}
pub fn use_frm(mut self, use_frm: bool) -> Self {
self.use_frm = use_frm;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CheckTableOption {
ForUpgrade,
Quick,
Fast,
#[default]
Medium,
Extended,
Changed,
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
fn test_vacuum_option_default() {
let opt = VacuumOption::new();
assert!(!opt.full);
assert!(!opt.freeze);
assert!(!opt.verbose);
assert!(!opt.analyze);
}
#[rstest]
fn test_vacuum_option_full() {
let opt = VacuumOption::new().full(true);
assert!(opt.full);
assert!(!opt.freeze);
assert!(!opt.verbose);
assert!(!opt.analyze);
}
#[rstest]
fn test_vacuum_option_freeze() {
let opt = VacuumOption::new().freeze(true);
assert!(!opt.full);
assert!(opt.freeze);
assert!(!opt.verbose);
assert!(!opt.analyze);
}
#[rstest]
fn test_vacuum_option_verbose() {
let opt = VacuumOption::new().verbose(true);
assert!(!opt.full);
assert!(!opt.freeze);
assert!(opt.verbose);
assert!(!opt.analyze);
}
#[rstest]
fn test_vacuum_option_analyze() {
let opt = VacuumOption::new().analyze(true);
assert!(!opt.full);
assert!(!opt.freeze);
assert!(!opt.verbose);
assert!(opt.analyze);
}
#[rstest]
fn test_vacuum_option_combined() {
let opt = VacuumOption::new()
.full(true)
.freeze(true)
.verbose(true)
.analyze(true);
assert!(opt.full);
assert!(opt.freeze);
assert!(opt.verbose);
assert!(opt.analyze);
}
#[rstest]
fn test_analyze_table_basic() {
let tbl = AnalyzeTable::new("users");
assert_eq!(tbl.table.to_string(), "users");
assert!(tbl.columns.is_empty());
}
#[rstest]
fn test_analyze_table_with_column() {
let tbl = AnalyzeTable::new("users").add_column("email");
assert_eq!(tbl.table.to_string(), "users");
assert_eq!(tbl.columns.len(), 1);
assert_eq!(tbl.columns[0].to_string(), "email");
}
#[rstest]
fn test_analyze_table_with_multiple_columns() {
let tbl = AnalyzeTable::new("users")
.add_column("email")
.add_column("name")
.add_column("age");
assert_eq!(tbl.table.to_string(), "users");
assert_eq!(tbl.columns.len(), 3);
assert_eq!(tbl.columns[0].to_string(), "email");
assert_eq!(tbl.columns[1].to_string(), "name");
assert_eq!(tbl.columns[2].to_string(), "age");
}
#[rstest]
fn test_optimize_table_option_default() {
let opt = OptimizeTableOption::new();
assert!(!opt.no_write_to_binlog);
assert!(!opt.local);
}
#[rstest]
fn test_optimize_table_option_no_write_to_binlog() {
let opt = OptimizeTableOption::new().no_write_to_binlog(true);
assert!(opt.no_write_to_binlog);
assert!(!opt.local);
}
#[rstest]
fn test_optimize_table_option_local() {
let opt = OptimizeTableOption::new().local(true);
assert!(!opt.no_write_to_binlog);
assert!(opt.local);
}
#[rstest]
fn test_repair_table_option_default() {
let opt = RepairTableOption::new();
assert!(!opt.no_write_to_binlog);
assert!(!opt.local);
assert!(!opt.quick);
assert!(!opt.extended);
assert!(!opt.use_frm);
}
#[rstest]
fn test_repair_table_option_quick() {
let opt = RepairTableOption::new().quick(true);
assert!(opt.quick);
assert!(!opt.extended);
}
#[rstest]
fn test_repair_table_option_extended() {
let opt = RepairTableOption::new().extended(true);
assert!(!opt.quick);
assert!(opt.extended);
}
#[rstest]
fn test_repair_table_option_use_frm() {
let opt = RepairTableOption::new().use_frm(true);
assert!(opt.use_frm);
}
#[rstest]
fn test_check_table_option_default() {
let opt = CheckTableOption::default();
assert!(matches!(opt, CheckTableOption::Medium));
}
#[rstest]
fn test_check_table_option_variants() {
assert!(matches!(
CheckTableOption::ForUpgrade,
CheckTableOption::ForUpgrade
));
assert!(matches!(CheckTableOption::Quick, CheckTableOption::Quick));
assert!(matches!(CheckTableOption::Fast, CheckTableOption::Fast));
assert!(matches!(CheckTableOption::Medium, CheckTableOption::Medium));
assert!(matches!(
CheckTableOption::Extended,
CheckTableOption::Extended
));
assert!(matches!(
CheckTableOption::Changed,
CheckTableOption::Changed
));
}
}