use crate::types::{DynIden, IntoIden};
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct SequenceDef {
pub(crate) name: DynIden,
pub(crate) if_not_exists: bool,
pub(crate) increment: Option<i64>,
pub(crate) min_value: Option<Option<i64>>,
pub(crate) max_value: Option<Option<i64>>,
pub(crate) start: Option<i64>,
pub(crate) cache: Option<i64>,
pub(crate) cycle: Option<bool>,
pub(crate) owned_by: Option<OwnedBy>,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum OwnedBy {
Column {
table: DynIden,
column: DynIden,
},
None,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum SequenceOption {
Restart(Option<i64>),
IncrementBy(i64),
MinValue(i64),
NoMinValue,
MaxValue(i64),
NoMaxValue,
Cache(i64),
Cycle,
NoCycle,
OwnedBy(OwnedBy),
}
impl SequenceDef {
pub fn new<N: IntoIden>(name: N) -> Self {
Self {
name: name.into_iden(),
if_not_exists: false,
increment: None,
min_value: None,
max_value: None,
start: None,
cache: None,
cycle: None,
owned_by: None,
}
}
pub fn if_not_exists(mut self, if_not_exists: bool) -> Self {
self.if_not_exists = if_not_exists;
self
}
pub fn increment(mut self, increment: i64) -> Self {
self.increment = Some(increment);
self
}
pub fn min_value(mut self, min_value: Option<i64>) -> Self {
self.min_value = Some(min_value);
self
}
pub fn max_value(mut self, max_value: Option<i64>) -> Self {
self.max_value = Some(max_value);
self
}
pub fn start(mut self, start: i64) -> Self {
self.start = Some(start);
self
}
pub fn cache(mut self, cache: i64) -> Self {
self.cache = Some(cache);
self
}
pub fn cycle(mut self, cycle: bool) -> Self {
self.cycle = Some(cycle);
self
}
pub fn owned_by_column<T: IntoIden, C: IntoIden>(mut self, table: T, column: C) -> Self {
self.owned_by = Some(OwnedBy::Column {
table: table.into_iden(),
column: column.into_iden(),
});
self
}
pub fn owned_by_none(mut self) -> Self {
self.owned_by = Some(OwnedBy::None);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
fn test_sequence_def_basic() {
let seq = SequenceDef::new("my_seq");
assert_eq!(seq.name.to_string(), "my_seq");
assert!(!seq.if_not_exists);
assert!(seq.increment.is_none());
assert!(seq.min_value.is_none());
assert!(seq.max_value.is_none());
assert!(seq.start.is_none());
assert!(seq.cache.is_none());
assert!(seq.cycle.is_none());
assert!(seq.owned_by.is_none());
}
#[rstest]
fn test_sequence_def_if_not_exists() {
let seq = SequenceDef::new("my_seq").if_not_exists(true);
assert_eq!(seq.name.to_string(), "my_seq");
assert!(seq.if_not_exists);
}
#[rstest]
fn test_sequence_def_increment() {
let seq = SequenceDef::new("my_seq").increment(5);
assert_eq!(seq.increment, Some(5));
}
#[rstest]
fn test_sequence_def_min_max_values() {
let seq = SequenceDef::new("my_seq")
.min_value(Some(1))
.max_value(Some(1000));
assert_eq!(seq.min_value, Some(Some(1)));
assert_eq!(seq.max_value, Some(Some(1000)));
}
#[rstest]
fn test_sequence_def_no_min_max_values() {
let seq = SequenceDef::new("my_seq").min_value(None).max_value(None);
assert_eq!(seq.min_value, Some(None));
assert_eq!(seq.max_value, Some(None));
}
#[rstest]
fn test_sequence_def_start() {
let seq = SequenceDef::new("my_seq").start(100);
assert_eq!(seq.start, Some(100));
}
#[rstest]
fn test_sequence_def_cache() {
let seq = SequenceDef::new("my_seq").cache(20);
assert_eq!(seq.cache, Some(20));
}
#[rstest]
fn test_sequence_def_cycle() {
let seq = SequenceDef::new("my_seq").cycle(true);
assert_eq!(seq.cycle, Some(true));
}
#[rstest]
fn test_sequence_def_no_cycle() {
let seq = SequenceDef::new("my_seq").cycle(false);
assert_eq!(seq.cycle, Some(false));
}
#[rstest]
fn test_sequence_def_owned_by_column() {
let seq = SequenceDef::new("my_seq").owned_by_column("my_table", "id");
match seq.owned_by {
Some(OwnedBy::Column { table, column }) => {
assert_eq!(table.to_string(), "my_table");
assert_eq!(column.to_string(), "id");
}
_ => panic!("Expected OwnedBy::Column"),
}
}
#[rstest]
fn test_sequence_def_owned_by_none() {
let seq = SequenceDef::new("my_seq").owned_by_none();
assert!(matches!(seq.owned_by, Some(OwnedBy::None)));
}
#[rstest]
fn test_sequence_def_all_options() {
let seq = SequenceDef::new("my_seq")
.if_not_exists(true)
.increment(5)
.min_value(Some(1))
.max_value(Some(1000))
.start(100)
.cache(20)
.cycle(true)
.owned_by_column("my_table", "id");
assert_eq!(seq.name.to_string(), "my_seq");
assert!(seq.if_not_exists);
assert_eq!(seq.increment, Some(5));
assert_eq!(seq.min_value, Some(Some(1)));
assert_eq!(seq.max_value, Some(Some(1000)));
assert_eq!(seq.start, Some(100));
assert_eq!(seq.cache, Some(20));
assert_eq!(seq.cycle, Some(true));
match seq.owned_by {
Some(OwnedBy::Column { table, column }) => {
assert_eq!(table.to_string(), "my_table");
assert_eq!(column.to_string(), "id");
}
_ => panic!("Expected OwnedBy::Column"),
}
}
}