use super::super::model::Table;
use super::super::naming::versioned_schema;
use super::super::quote::{quote_ident, quote_qualified};
pub const SECURITY_INVOKER_MIN_MAJOR: u32 = 15;
pub fn create_view(schema: &str, version: &str, table: &Table, major_version: u32) -> Vec<String> {
let view = quote_qualified(&versioned_schema(schema, version), &table.name);
let mut statements = vec![
format!("DROP VIEW IF EXISTS {view}"),
format!(
"CREATE VIEW {view}{} AS SELECT {} FROM {}",
options(major_version),
projection(table),
quote_qualified(schema, &table.physical_name)
),
];
for column in table.visible_columns() {
if let Some(default) = &column.default {
statements.push(format!(
"ALTER VIEW {view} ALTER {} SET DEFAULT {default}",
quote_ident(&column.name)
));
}
}
statements
}
fn projection(table: &Table) -> String {
table
.visible_columns()
.iter()
.map(|column| {
format!(
"{} AS {}",
quote_ident(&column.physical_name),
quote_ident(&column.name)
)
})
.collect::<Vec<_>>()
.join(", ")
}
fn options(major_version: u32) -> &'static str {
if major_version >= SECURITY_INVOKER_MIN_MAJOR {
" WITH (security_invoker = true)"
} else {
""
}
}