Skip to main content

schema_sql_generator/common/
function_generator.rs

1use crate::common::generator_context::GeneratorContext;
2use crate::common::sql_writer::SqlWriter;
3use schema_model::model::function::Function;
4
5pub trait FunctionGenerator {
6    fn output_functions(&self);
7    fn output_function(
8        &self,
9        writer: &mut SqlWriter,
10        statement_separator: &str,
11        function: &Function,
12    );
13}
14
15pub struct DefaultFunctionGenerator {
16    context: GeneratorContext,
17}
18
19impl DefaultFunctionGenerator {
20    pub fn new(context: GeneratorContext) -> Self {
21        Self { context }
22    }
23
24    pub fn context(&self) -> &GeneratorContext {
25        &self.context
26    }
27
28    /// Same iteration/filtering logic as `output_functions`, but dispatches each
29    /// function through `generator` rather than `self`. Rust has no virtual dispatch on
30    /// concrete types, so a dialect wrapper (e.g. `SqlServerFunctionGenerator`) that
31    /// overrides `output_function` but delegates `output_functions` straight to this
32    /// struct would otherwise never see its own override invoked - callers that need
33    /// their override honored should pass `self` (as a `&dyn FunctionGenerator`) here
34    /// instead of calling `output_functions` directly.
35    pub fn output_functions_via(&self, generator: &dyn FunctionGenerator) {
36        let database_type = self.context.settings().database_type();
37        let statement_separator = self.context.settings().statement_separator();
38        let database_model = self.context.settings().database_model();
39
40        self.context.with_writer(|writer| {
41            database_model.schemas().iter().for_each(|schema| {
42                schema
43                    .functions()
44                    .iter()
45                    .filter(|function| function.database_type() == database_type)
46                    .for_each(|function| {
47                        generator.output_function(writer, statement_separator, function);
48                    })
49            });
50        });
51    }
52}
53
54impl FunctionGenerator for DefaultFunctionGenerator {
55    fn output_functions(&self) {
56        self.output_functions_via(self);
57    }
58
59    fn output_function(
60        &self,
61        writer: &mut SqlWriter,
62        statement_separator: &str,
63        function: &Function,
64    ) {
65        writer.print(function.sql());
66        writer.println(statement_separator);
67        writer.newline();
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use crate::common::test_support::make_context;
75    use schema_model::builder::SchemaBuilder;
76    use schema_model::model::database_model::DatabaseModel;
77    use schema_model::model::types::{BooleanMode, DatabaseType, ForeignKeyMode};
78
79    struct TaggingFunctionGenerator(DefaultFunctionGenerator);
80
81    impl FunctionGenerator for TaggingFunctionGenerator {
82        fn output_functions(&self) {
83            self.0.output_functions_via(self);
84        }
85
86        fn output_function(
87            &self,
88            writer: &mut SqlWriter,
89            statement_separator: &str,
90            function: &Function,
91        ) {
92            writer.print("-- tagged\n");
93            self.0
94                .output_function(writer, statement_separator, function);
95        }
96    }
97
98    #[test]
99    fn output_functions_via_dispatches_through_the_passed_generator_not_self() {
100        // Regression test for M10: a wrapper around `DefaultFunctionGenerator` must have
101        // its own `output_function` override honored when `output_functions` calls
102        // `output_functions_via(self)` rather than plainly delegating to
103        // `DefaultFunctionGenerator::output_functions` - the latter would call back only
104        // into `DefaultFunctionGenerator::output_function`, silently skipping the
105        // wrapper's override, exactly the trap that made `SqlServerFunctionGenerator`'s
106        // drop-guard unreachable before it was fixed.
107        let schema = SchemaBuilder::new(None::<&str>)
108            .add_functions(vec![Function::new(
109                None::<&str>,
110                "f",
111                DatabaseType::Postgresql,
112                "create function f()",
113            )])
114            .build();
115        let model =
116            DatabaseModel::new(BooleanMode::Native, ForeignKeyMode::Relations, vec![schema]);
117        let (ctx, buffer) = make_context(model, DatabaseType::Postgresql);
118
119        let generator = TaggingFunctionGenerator(DefaultFunctionGenerator::new(ctx));
120        generator.output_functions();
121
122        let output = buffer.contents();
123        assert!(output.contains("-- tagged"));
124        assert!(output.contains("create function f()"));
125    }
126}