diff --git a/core/lib.rs b/core/lib.rs index 9efe0a045..ecd8b5d74 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -49,6 +49,7 @@ use std::{ borrow::Cow, cell::{Cell, RefCell, UnsafeCell}, collections::HashMap, + fmt::Display, io::Write, num::NonZero, ops::Deref, @@ -583,6 +584,7 @@ impl Connection { } // Clearly there is something to improve here, Vec> isn't a couple of tea + /// Query the current rows/values of `pragma_name`. pub fn pragma_query(self: &Rc, pragma_name: &str) -> Result>> { let pragma = format!("PRAGMA {}", pragma_name); let mut stmt = self.prepare(pragma)?; @@ -607,6 +609,74 @@ impl Connection { Ok(results) } + + /// Set a new value to `pragma_name`. + /// + /// Some pragmas will return the updated value which cannot be retrieved + /// with this method. + pub fn pragma_update( + self: &Rc, + pragma_name: &str, + pragma_value: V, + ) -> Result>> { + let pragma = format!("PRAGMA {} = {}", pragma_name, pragma_value); + let mut stmt = self.prepare(pragma)?; + let mut results = Vec::new(); + loop { + match stmt.step()? { + vdbe::StepResult::Row => { + let row: Vec = stmt + .row() + .unwrap() + .get_values() + .map(|v| v.clone()) + .collect(); + results.push(row); + } + vdbe::StepResult::Interrupt | vdbe::StepResult::Busy => { + return Err(LimboError::Busy); + } + _ => break, + } + } + + Ok(results) + } + + /// Query the current value(s) of `pragma_name` associated to + /// `pragma_value`. + /// + /// This method can be used with query-only pragmas which need an argument + /// (e.g. `table_info('one_tbl')`) or pragmas which returns value(s) + /// (e.g. `integrity_check`). + pub fn pragma( + self: &Rc, + pragma_name: &str, + pragma_value: V, + ) -> Result>> { + let pragma = format!("PRAGMA {}({})", pragma_name, pragma_value); + let mut stmt = self.prepare(pragma)?; + let mut results = Vec::new(); + loop { + match stmt.step()? { + vdbe::StepResult::Row => { + let row: Vec = stmt + .row() + .unwrap() + .get_values() + .map(|v| v.clone()) + .collect(); + results.push(row); + } + vdbe::StepResult::Interrupt | vdbe::StepResult::Busy => { + return Err(LimboError::Busy); + } + _ => break, + } + } + + Ok(results) + } } pub struct Statement {