Skip to main content

schema_sql_generator/common/
table_constraint_generator.rs

1use crate::common::generator_context::GeneratorContext;
2use schema_model::model::constraint::Constraint;
3use schema_model::model::table::Table;
4
5pub trait TableConstraintGenerator {
6    fn table_check_constraints(&self, table: &Table) -> Vec<String>;
7}
8
9pub struct DefaultTableConstraintGenerator {
10    context: GeneratorContext,
11}
12
13impl DefaultTableConstraintGenerator {
14    pub fn new(context: GeneratorContext) -> Self {
15        Self {
16            context,
17        }
18    }
19
20    pub fn context(&self) -> &GeneratorContext {
21        &self.context
22    }
23
24    fn generator_constraint(&self, constraint: &Constraint) -> String {
25        format!("   constraint {} {}",
26               constraint.name(),
27               constraint.sql())
28    }
29}
30
31impl TableConstraintGenerator for DefaultTableConstraintGenerator {
32    fn table_check_constraints(&self, table: &Table) -> Vec<String> {
33        table.constraints().iter().map(|constraint| {
34            self.generator_constraint(constraint)
35        }).collect()
36    }
37}