use crate::{
database::{executor::Executor, schema_manager::Commands},
misc::{DEController, Lease},
};
use alloc::string::String;
#[cfg(feature = "std")]
use std::{fs::read_to_string, path::Path};
impl<E> Commands<E>
where
E: Executor,
{
#[inline]
pub async fn seed<I, S>(
&mut self,
buffer_cmd: &mut String,
seeds: I,
) -> Result<(), <E::Database as DEController>::Error>
where
I: Iterator<Item = S>,
S: Lease<str>,
{
for elem in seeds {
buffer_cmd.push_str(elem.lease());
}
self
.executor
.transaction(|this| async {
this.execute(buffer_cmd.as_str(), |_| Ok(())).await?;
Ok(((), this))
})
.await?;
buffer_cmd.clear();
Ok(())
}
#[cfg(feature = "std")]
#[inline]
pub async fn seed_from_dir(
&mut self,
buffer_cmd: &mut String,
dir: &Path,
) -> Result<(), <E::Database as DEController>::Error> {
let iter = crate::database::schema_manager::misc::files(dir)?.filter_map(|el_rslt| {
let el = el_rslt.ok()?;
read_to_string(el.path()).ok()
});
self.seed(buffer_cmd, iter).await
}
}