use crate::ffi;
use crate::{Error, Record, Result, View};
use std::ffi::CString;
pub struct Database {
h: ffi::PMSIHANDLE,
}
impl Database {
pub fn open_view(&self, sql: &str) -> Result<View> {
unsafe {
let mut h = ffi::MSIHANDLE::null();
let sql = CString::new(sql)?;
let ret = ffi::MsiDatabaseOpenView(*self.h, sql.as_ptr(), &mut h);
if ret != ffi::ERROR_SUCCESS {
return Err(
Error::from_last_error_record().unwrap_or_else(|| Error::from_error_code(ret))
);
}
Ok(View::from_handle(h))
}
}
pub fn primary_keys(&self, table: &str) -> Result<Record> {
unsafe {
let mut h = ffi::MSIHANDLE::null();
let table = CString::new(table)?;
let ret = ffi::MsiDatabaseGetPrimaryKeys(*self.h, table.as_ptr(), &mut h);
if ret != ffi::ERROR_SUCCESS {
return Err(Error::from_error_code(ret));
}
Ok(Record::from_handle(h))
}
}
pub(crate) fn from_handle(h: ffi::MSIHANDLE) -> Self {
Database { h: h.to_owned() }
}
}