use crate::common::generator_context::GeneratorContext;
use crate::common::other_sql_generator::{DefaultOtherSqlGenerator, OtherSqlGenerator};
use crate::common::sql_writer::SqlWriter;
pub struct SqlServerOtherSqlGenerator {
other_sql_generator: DefaultOtherSqlGenerator,
}
impl SqlServerOtherSqlGenerator {
pub fn new(context: GeneratorContext) -> Self {
Self {
other_sql_generator: DefaultOtherSqlGenerator::new(context),
}
}
}
impl OtherSqlGenerator for SqlServerOtherSqlGenerator {
fn output_other_sql_top(&self) {
self.other_sql_generator.output_other_sql_top_via(self);
}
fn output_other_sql_bottom(&self) {
self.other_sql_generator.output_other_sql_bottom_via(self);
}
fn output_other_sql(&self, writer: &mut SqlWriter, statement_separator: &str, sql: &str) {
self.other_sql_generator
.output_other_sql(writer, statement_separator, sql);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::test_support::make_context;
use schema_model::builder::SchemaBuilder;
use schema_model::model::database_model::DatabaseModel;
use schema_model::model::other_sql::OtherSql;
use schema_model::model::types::{BooleanMode, DatabaseType, ForeignKeyMode, OtherSqlOrder};
#[test]
fn output_other_sql_top_renders_matching_entries_only() {
let schema = SchemaBuilder::new(None::<&str>)
.add_other_sql(OtherSql::new(
DatabaseType::SqlServer,
OtherSqlOrder::Top,
"set nocount on",
))
.add_other_sql(OtherSql::new(
DatabaseType::Postgresql,
OtherSqlOrder::Top,
"select 1",
))
.add_other_sql(OtherSql::new(
DatabaseType::SqlServer,
OtherSqlOrder::Bottom,
"update statistics",
))
.build();
let model =
DatabaseModel::new(BooleanMode::Native, ForeignKeyMode::Relations, vec![schema]);
let (ctx, buffer) = make_context(model, DatabaseType::SqlServer);
let generator = SqlServerOtherSqlGenerator::new(ctx);
generator.output_other_sql_top();
let output = buffer.contents();
assert!(output.contains("set nocount on"));
assert!(!output.contains("select 1"));
assert!(!output.contains("update statistics"));
}
#[test]
fn output_other_sql_bottom_renders_matching_entries_only() {
let schema = SchemaBuilder::new(None::<&str>)
.add_other_sql(OtherSql::new(
DatabaseType::SqlServer,
OtherSqlOrder::Bottom,
"update statistics",
))
.add_other_sql(OtherSql::new(
DatabaseType::SqlServer,
OtherSqlOrder::Top,
"set nocount on",
))
.build();
let model =
DatabaseModel::new(BooleanMode::Native, ForeignKeyMode::Relations, vec![schema]);
let (ctx, buffer) = make_context(model, DatabaseType::SqlServer);
let generator = SqlServerOtherSqlGenerator::new(ctx);
generator.output_other_sql_bottom();
let output = buffer.contents();
assert!(output.contains("update statistics"));
assert!(!output.contains("set nocount on"));
}
}