From 4c3fa7bd5386fb10b23f5b7de2d803af4bd6bb04 Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 10 Aug 2018 20:52:11 +0200 Subject: [PATCH] Add binding to sqlite3_expanded_sql --- src/raw_statement.rs | 12 ++++++++++++ src/statement.rs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/raw_statement.rs b/src/raw_statement.rs index f4deda1..42a328d 100644 --- a/src/raw_statement.rs +++ b/src/raw_statement.rs @@ -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 { diff --git a/src/statement.rs b/src/statement.rs index 8d3b753..0735819 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -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 for Statement<'conn> { @@ -816,4 +824,13 @@ mod test { let y: Result = 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()); + } }