use crate::ffi;
use crate::{Error, Record, Result};
#[cfg(doc)]
use crate::Database;
pub struct View {
h: ffi::PMSIHANDLE,
}
impl View {
pub fn close(&self) {
unsafe {
ffi::MsiViewClose(*self.h);
}
}
pub fn execute(&self, record: Option<Record>) -> Result<()> {
unsafe {
let h = match record {
Some(r) => *r.h,
None => ffi::MSIHANDLE::null(),
};
let ret = ffi::MsiViewExecute(*self.h, h);
if ret != ffi::ERROR_SUCCESS {
return Err(
Error::from_last_error_record().unwrap_or_else(|| Error::from_error_code(ret))
);
}
Ok(())
}
}
pub fn modify(&self, mode: ModifyMode, record: &Record) -> Result<()> {
unsafe {
let ret = ffi::MsiViewModify(*self.h, mode, *record.h);
if ret != ffi::ERROR_SUCCESS {
return Err(
Error::from_last_error_record().unwrap_or_else(|| Error::from_error_code(ret))
);
}
Ok(())
}
}
pub(crate) fn from_handle(h: ffi::MSIHANDLE) -> Self {
View { h: h.to_owned() }
}
}
impl Drop for View {
fn drop(&mut self) {
self.close();
}
}
impl Iterator for View {
type Item = Record;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let mut h = ffi::MSIHANDLE::null();
ffi::MsiViewFetch(*self.h, &mut h);
if h.is_null() {
return None;
}
Some(Record::from_handle(h))
}
}
}
#[repr(u32)]
pub enum ModifyMode {
Seek = u32::MAX,
Refresh = 0,
Insert = 1,
Update = 2,
Assign = 3,
Replace = 4,
Merge = 5,
Delete = 6,
InsertTemporary = 7,
}