schema_sql_generator/common/
other_sql_generator.rs1use crate::common::generator_context::GeneratorContext;
2use crate::common::sql_writer::SqlWriter;
3use schema_model::model::types::OtherSqlOrder;
4
5pub trait OtherSqlGenerator {
6 fn output_other_sql_top(&self);
7 fn output_other_sql_bottom(&self);
8 fn output_other_sql(&self, writer: &mut SqlWriter, statement_separator: &str, sql: &str);
9}
10
11pub struct DefaultOtherSqlGenerator {
12 context: GeneratorContext,
13}
14
15impl DefaultOtherSqlGenerator {
16 pub fn new(context: GeneratorContext) -> Self {
17 Self { context }
18 }
19
20 pub fn context(&self) -> &GeneratorContext {
21 &self.context
22 }
23}
24
25impl DefaultOtherSqlGenerator {
26 pub fn output_other_sql_top_via(&self, generator: &dyn OtherSqlGenerator) {
35 let database_type = self.context.settings().database_type();
36 let statement_separator = self.context.settings().statement_separator();
37 let database_model = self.context.settings().database_model();
38
39 self.context.with_writer(|writer| {
40 database_model.schemas().iter().for_each(|schema| {
41 schema
42 .other_sql()
43 .iter()
44 .filter(|sql| sql.database_type() == database_type)
45 .filter(|sql| sql.order() == OtherSqlOrder::Top)
46 .filter(|sql| !sql.sql().is_empty())
47 .for_each(|sql| {
48 generator.output_other_sql(writer, statement_separator, sql.sql());
49 })
50 });
51 });
52 }
53
54 pub fn output_other_sql_bottom_via(&self, generator: &dyn OtherSqlGenerator) {
58 let database_type = self.context.settings().database_type();
59 let statement_separator = self.context.settings().statement_separator();
60 let database_model = self.context.settings().database_model();
61
62 self.context.with_writer(|writer| {
63 database_model.schemas().iter().for_each(|schema| {
64 schema
65 .other_sql()
66 .iter()
67 .filter(|sql| sql.database_type() == database_type)
68 .filter(|sql| sql.order() == OtherSqlOrder::Bottom)
69 .filter(|sql| !sql.sql().is_empty())
70 .for_each(|sql| {
71 generator.output_other_sql(writer, statement_separator, sql.sql());
72 })
73 });
74 });
75 }
76}
77
78impl OtherSqlGenerator for DefaultOtherSqlGenerator {
79 fn output_other_sql_top(&self) {
80 self.output_other_sql_top_via(self);
81 }
82
83 fn output_other_sql_bottom(&self) {
84 self.output_other_sql_bottom_via(self);
85 }
86
87 fn output_other_sql(&self, writer: &mut SqlWriter, statement_separator: &str, sql: &str) {
88 writer.print(sql);
89 writer.println(statement_separator);
90 writer.newline();
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::common::test_support::make_context;
98 use schema_model::builder::SchemaBuilder;
99 use schema_model::model::database_model::DatabaseModel;
100 use schema_model::model::other_sql::OtherSql;
101 use schema_model::model::types::{BooleanMode, DatabaseType, ForeignKeyMode};
102
103 struct TaggingOtherSqlGenerator(DefaultOtherSqlGenerator);
104
105 impl OtherSqlGenerator for TaggingOtherSqlGenerator {
106 fn output_other_sql_top(&self) {
107 self.0.output_other_sql_top_via(self);
108 }
109
110 fn output_other_sql_bottom(&self) {
111 self.0.output_other_sql_bottom_via(self);
112 }
113
114 fn output_other_sql(&self, writer: &mut SqlWriter, statement_separator: &str, sql: &str) {
115 writer.print("-- tagged\n");
116 self.0.output_other_sql(writer, statement_separator, sql);
117 }
118 }
119
120 #[test]
121 fn output_other_sql_top_via_dispatches_through_the_passed_generator_not_self() {
122 let schema = SchemaBuilder::new(None::<&str>)
129 .add_other_sql(OtherSql::new(
130 DatabaseType::Postgresql,
131 schema_model::model::types::OtherSqlOrder::Top,
132 "select 1",
133 ))
134 .build();
135 let model =
136 DatabaseModel::new(BooleanMode::Native, ForeignKeyMode::Relations, vec![schema]);
137 let (ctx, buffer) = make_context(model, DatabaseType::Postgresql);
138
139 let generator = TaggingOtherSqlGenerator(DefaultOtherSqlGenerator::new(ctx));
140 generator.output_other_sql_top();
141
142 let output = buffer.contents();
143 assert!(output.contains("-- tagged"));
144 assert!(output.contains("select 1"));
145 }
146}