Add binding to sqlite3_expanded_sql

This commit is contained in:
gwenn 2018-08-10 20:52:11 +02:00
parent 31de0187a2
commit 4c3fa7bd53
2 changed files with 29 additions and 0 deletions

View File

@ -88,6 +88,18 @@ impl RawStatement {
pub fn readonly(&self) -> bool {
unsafe { ffi::sqlite3_stmt_readonly(self.0) != 0 }
}
#[cfg(feature = "bundled")]
pub fn expanded_sql(&self) -> Option<&CStr> {
unsafe {
let ptr = ffi::sqlite3_expanded_sql(self.0);
if ptr.is_null() {
None
} else {
Some(CStr::from_ptr(ptr))
}
}
}
}
impl Drop for RawStatement {

View File

@ -480,6 +480,14 @@ impl<'conn> Statement<'conn> {
}
Ok(())
}
/// Returns a string containing the SQL text of prepared statement with bound parameters expanded.
#[cfg(feature = "bundled")]
pub fn expanded_sql(&self) -> Option<&str> {
unsafe {
self.stmt.expanded_sql().map(|s| str::from_utf8_unchecked(s.to_bytes()))
}
}
}
impl<'conn> Into<RawStatement> for Statement<'conn> {
@ -816,4 +824,13 @@ mod test {
let y: Result<i64> = stmt.query_row(&[], |r| r.get("y"));
assert_eq!(3i64, y.unwrap());
}
#[test]
#[cfg(feature = "bundled")]
fn test_expanded_sql() {
let db = Connection::open_in_memory().unwrap();
let stmt = db.prepare("SELECT ?").unwrap();
stmt.bind_parameter(&1, 1).unwrap();
assert_eq!(Some("SELECT 1"), stmt.expanded_sql());
}
}