use super::{
optional_text, string, text, Catalog, Family, NativeRecordOwner, RelationRecord, Result,
};
use crate::catalog::{RelationIdentity, ViewRow};
impl Catalog {
pub(in crate::catalog) fn native_view_has_private_changes(
&self,
relation: &RelationIdentity,
) -> Result<bool> {
Ok(self
.read_native(|snapshot| {
let key = crate::mvcc::native::NativeRecordIdentity::new(
Family::Views,
NativeRecordOwner::Database(snapshot.database),
)?
.encode_key(
&[text(&relation.schema), text(&relation.name)],
&snapshot.control,
)?;
Ok(snapshot
.view
.private_keys(&key, None, 1, &snapshot.control)?
.iter()
.any(|entry| entry.key() == &*key))
})?
.unwrap_or(false))
}
pub(in crate::catalog) fn save_native_view(&self, view: &ViewRow) -> Result<Option<()>> {
if !self.conn.is_native_record_session() {
return Ok(None);
}
let (security_owner, acl, columns) =
super::super::role_security::encode_relation(&view.security)?;
self.save_native_relation(
RelationRecord::View,
&view.relation,
&[
text(&view.relation.schema),
text(&view.relation.name),
text("view"),
text(&view.definition_json),
(&security_owner).into(),
optional_text(acl.as_deref()),
text(&columns),
],
)
}
pub(in crate::catalog) fn load_native_views(&self) -> Result<Option<Vec<ViewRow>>> {
self.read_native(|snapshot| {
let mut views = Vec::new();
snapshot.visit_rows(
Family::Views,
Some(NativeRecordOwner::Database(snapshot.database)),
&[],
|row| {
views.push(ViewRow {
relation: RelationIdentity::new(string(row[0])?, string(row[1])?),
definition_json: string(row[3])?,
security: super::super::role_security::decode_relation_cells(
row[4], row[5], row[6],
)?,
});
Ok(())
},
)?;
let acls = snapshot.load_relation_acls()?;
for view in &mut views {
acls.apply(&view.relation, &mut view.security)?;
}
Ok(views)
})
}
}