elefant_tools/models/
sequence.rs

1use crate::object_id::ObjectId;
2use crate::quoting::AttemptedKeywordUsage::ColumnName;
3use crate::quoting::{quote_value_string, IdentifierQuoter, Quotable};
4use crate::PostgresSchema;
5use serde::{Deserialize, Serialize};
6use std::cmp::Ordering;
7
8#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
9pub struct PostgresSequence {
10    pub name: String,
11    pub data_type: String,
12    pub start_value: i64,
13    pub increment: i64,
14    pub min_value: i64,
15    pub max_value: i64,
16    pub cache_size: i64,
17    pub cycle: bool,
18    pub last_value: Option<i64>,
19    pub comment: Option<String>,
20    pub object_id: ObjectId,
21}
22
23impl Default for PostgresSequence {
24    fn default() -> Self {
25        Self {
26            name: String::new(),
27            data_type: String::new(),
28            start_value: 1,
29            increment: 1,
30            min_value: 1,
31            max_value: 2147483647,
32            cache_size: 1,
33            cycle: false,
34            last_value: None,
35            comment: None,
36            object_id: ObjectId::default(),
37        }
38    }
39}
40
41impl PostgresSequence {
42    pub fn get_create_statement(
43        &self,
44        schema: &PostgresSchema,
45        identifier_quoter: &IdentifierQuoter,
46    ) -> String {
47        let mut sql = format!(
48            "create sequence {}.{} as {} increment by {} minvalue {} maxvalue {} start {} cache {}",
49            schema.name.quote(identifier_quoter, ColumnName),
50            self.name.quote(identifier_quoter, ColumnName),
51            self.data_type,
52            self.increment,
53            self.min_value,
54            self.max_value,
55            self.start_value,
56            self.cache_size
57        );
58
59        if self.cycle {
60            sql.push_str(" cycle");
61        }
62
63        sql.push(';');
64
65        if let Some(comment) = &self.comment {
66            sql.push_str("\ncomment on sequence ");
67            sql.push_str(&schema.name.quote(identifier_quoter, ColumnName));
68            sql.push('.');
69            sql.push_str(&self.name.quote(identifier_quoter, ColumnName));
70            sql.push_str(" is ");
71            sql.push_str(&quote_value_string(comment));
72            sql.push(';');
73        }
74
75        sql
76    }
77
78    pub fn get_set_value_statement(
79        &self,
80        schema: &PostgresSchema,
81        identifier_quoter: &IdentifierQuoter,
82    ) -> Option<String> {
83        self.last_value.map(|last_value| {
84            format!(
85                "select pg_catalog.setval('{}.{}', {}, true);",
86                schema.name.quote(identifier_quoter, ColumnName),
87                self.name.quote(identifier_quoter, ColumnName),
88                last_value
89            )
90        })
91    }
92}
93
94impl Ord for PostgresSequence {
95    fn cmp(&self, other: &Self) -> Ordering {
96        self.name.cmp(&other.name)
97    }
98}
99
100impl PartialOrd for PostgresSequence {
101    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
102        Some(self.cmp(other))
103    }
104}