use std::borrow::Cow;
use sql_schema_describer as sql;
use sqlparser::{ast::Statement, parser::Parser};
pub(crate) struct SqliteIntrospectionFlavour;
impl super::IntrospectionFlavour for SqliteIntrospectionFlavour {
fn keep_previous_scalar_field_arity(&self, next: sql::ColumnWalker<'_>) -> bool {
next.is_in_view() && next.column_type().arity.is_nullable()
}
fn format_view_definition(&self, definition: &str) -> String {
let dialect = sqlparser::dialect::SQLiteDialect {};
let stmt = Parser::new(&dialect)
.try_with_sql(definition)
.and_then(|mut p| p.parse_statement());
let definition = match stmt {
Ok(Statement::CreateView { query, .. }) => Cow::Owned(format!("{query};")),
_ => Cow::Borrowed(definition),
};
let opts = sqlformat::FormatOptions {
uppercase: true,
..Default::default()
};
sqlformat::format(&definition, &Default::default(), opts)
}
}