use log::LevelFilter;
pub fn ensure_dicts_table_exists(db: &rusqlite::Connection) -> rusqlite::Result<()> {
db.execute_batch(
"
create table if not exists _zstd_dicts (
id integer primary key autoincrement,
chooser_key text unique,
dict blob not null
);
insert or ignore into _zstd_dicts values (-1, '[nodict]', ''); -- only added so foreign key is fulfilled
",
)?;
Ok(())
}
#[doc(hidden)]
#[macro_export]
macro_rules! format_sqlite {
($x:expr_2021, $($y:expr_2021),*) => {
format!($x, $(escape_sqlite_identifier($y),)*)
};
}
pub fn ah(e: anyhow::Error) -> rusqlite::Error {
rusqlite::Error::UserFunctionError(format!("{e:?}").into())
}
pub fn escape_sqlite_identifier(identifier: &str) -> String {
format!("`{}`", identifier.replace('`', "``"))
}
pub fn init_logging(default_level: LevelFilter) {
if std::env::var("SQLITE_ZSTD_LOG").is_err() {
unsafe { std::env::set_var("SQLITE_ZSTD_LOG", format!("{default_level}")) };
}
env_logger::try_init_from_env(env_logger::Env::new().filter("SQLITE_ZSTD_LOG")).ok();
}