Made dev tools not break everything about the db

Also improved multi statements to allow out of order parameter binding in statements
Ensured that all statements are run for maybe_row and single, and that of all statements only 1 of them returns only 1 row
Made bind and column calls add useful context to errors

Co-authored-by: kay@zed.dev
This commit is contained in:
Mikayla Maki 2022-11-21 13:42:26 -08:00
parent 2dc1130902
commit 3e0f9d27a7
13 changed files with 219 additions and 110 deletions

View file

@ -5,7 +5,7 @@ use std::{
sync::Arc,
};
use anyhow::Result;
use anyhow::{Context, Result};
use crate::statement::{SqlType, Statement};
@ -19,61 +19,82 @@ pub trait Column: Sized {
impl Bind for bool {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind(self.then_some(1).unwrap_or(0), start_index)
statement
.bind(self.then_some(1).unwrap_or(0), start_index)
.with_context(|| format!("Failed to bind bool at index {start_index}"))
}
}
impl Column for bool {
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
i32::column(statement, start_index).map(|(i, next_index)| (i != 0, next_index))
i32::column(statement, start_index)
.map(|(i, next_index)| (i != 0, next_index))
.with_context(|| format!("Failed to read bool at index {start_index}"))
}
}
impl Bind for &[u8] {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_blob(start_index, self)?;
statement
.bind_blob(start_index, self)
.with_context(|| format!("Failed to bind &[u8] at index {start_index}"))?;
Ok(start_index + 1)
}
}
impl<const C: usize> Bind for &[u8; C] {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_blob(start_index, self.as_slice())?;
statement
.bind_blob(start_index, self.as_slice())
.with_context(|| format!("Failed to bind &[u8; C] at index {start_index}"))?;
Ok(start_index + 1)
}
}
impl Bind for Vec<u8> {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_blob(start_index, self)?;
statement
.bind_blob(start_index, self)
.with_context(|| format!("Failed to bind Vec<u8> at index {start_index}"))?;
Ok(start_index + 1)
}
}
impl Column for Vec<u8> {
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
let result = statement.column_blob(start_index)?;
let result = statement
.column_blob(start_index)
.with_context(|| format!("Failed to read Vec<u8> at index {start_index}"))?;
Ok((Vec::from(result), start_index + 1))
}
}
impl Bind for f64 {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_double(start_index, *self)?;
statement
.bind_double(start_index, *self)
.with_context(|| format!("Failed to bind f64 at index {start_index}"))?;
Ok(start_index + 1)
}
}
impl Column for f64 {
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
let result = statement.column_double(start_index)?;
let result = statement
.column_double(start_index)
.with_context(|| format!("Failed to parse f64 at index {start_index}"))?;
Ok((result, start_index + 1))
}
}
impl Bind for i32 {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_int(start_index, *self)?;
statement
.bind_int(start_index, *self)
.with_context(|| format!("Failed to bind i32 at index {start_index}"))?;
Ok(start_index + 1)
}
}
@ -87,7 +108,9 @@ impl Column for i32 {
impl Bind for i64 {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
statement.bind_int64(start_index, *self)?;
statement
.bind_int64(start_index, *self)
.with_context(|| format!("Failed to bind i64 at index {start_index}"))?;
Ok(start_index + 1)
}
}
@ -101,7 +124,9 @@ impl Column for i64 {
impl Bind for usize {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
(*self as i64).bind(statement, start_index)
(*self as i64)
.bind(statement, start_index)
.with_context(|| format!("Failed to bind usize at index {start_index}"))
}
}

View file

@ -1,6 +1,7 @@
use std::{
ffi::{CStr, CString},
marker::PhantomData,
path::Path,
};
use anyhow::{anyhow, Result};
@ -73,6 +74,11 @@ impl Connection {
}
}
pub fn backup_main_to(&self, destination: impl AsRef<Path>) -> Result<()> {
let destination = Self::open_file(destination.as_ref().to_string_lossy().as_ref());
self.backup_main(&destination)
}
pub(crate) fn last_error(&self) -> Result<()> {
unsafe {
let code = sqlite3_errcode(self.sqlite3);

View file

@ -19,8 +19,6 @@ pub struct Statement<'a> {
pub enum StepResult {
Row,
Done,
Misuse,
Other(i32),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -40,12 +38,14 @@ impl<'a> Statement<'a> {
connection,
phantom: PhantomData,
};
unsafe {
let sql = CString::new(query.as_ref())?;
let sql = CString::new(query.as_ref()).context("Error creating cstr")?;
let mut remaining_sql = sql.as_c_str();
while {
let remaining_sql_str = remaining_sql.to_str()?.trim();
let remaining_sql_str = remaining_sql
.to_str()
.context("Parsing remaining sql")?
.trim();
remaining_sql_str != ";" && !remaining_sql_str.is_empty()
} {
let mut raw_statement = 0 as *mut sqlite3_stmt;
@ -92,116 +92,136 @@ impl<'a> Statement<'a> {
}
}
fn bind_index_with(&self, index: i32, bind: impl Fn(&*mut sqlite3_stmt) -> ()) -> Result<()> {
let mut any_succeed = false;
unsafe {
for raw_statement in self.raw_statements.iter() {
if index <= sqlite3_bind_parameter_count(*raw_statement) {
bind(raw_statement);
self.connection
.last_error()
.with_context(|| format!("Failed to bind value at index {index}"))?;
any_succeed = true;
} else {
continue;
}
}
}
if any_succeed {
Ok(())
} else {
Err(anyhow!("Failed to bind parameters"))
}
}
pub fn bind_blob(&self, index: i32, blob: &[u8]) -> Result<()> {
let index = index as c_int;
let blob_pointer = blob.as_ptr() as *const _;
let len = blob.len() as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_blob(*raw_statement, index, blob_pointer, len, SQLITE_TRANSIENT());
}
}
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_blob(*raw_statement, index, blob_pointer, len, SQLITE_TRANSIENT());
})
}
pub fn column_blob<'b>(&'b mut self, index: i32) -> Result<&'b [u8]> {
let index = index as c_int;
let pointer = unsafe { sqlite3_column_blob(self.current_statement(), index) };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read blob at index {index}"))?;
if pointer.is_null() {
return Ok(&[]);
}
let len = unsafe { sqlite3_column_bytes(self.current_statement(), index) as usize };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read length of blob at index {index}"))?;
unsafe { Ok(slice::from_raw_parts(pointer as *const u8, len)) }
}
pub fn bind_double(&self, index: i32, double: f64) -> Result<()> {
let index = index as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_double(*raw_statement, index, double);
}
}
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_double(*raw_statement, index, double);
})
}
pub fn column_double(&self, index: i32) -> Result<f64> {
let index = index as c_int;
let result = unsafe { sqlite3_column_double(self.current_statement(), index) };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read double at index {index}"))?;
Ok(result)
}
pub fn bind_int(&self, index: i32, int: i32) -> Result<()> {
let index = index as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_int(*raw_statement, index, int);
}
};
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_int(*raw_statement, index, int);
})
}
pub fn column_int(&self, index: i32) -> Result<i32> {
let index = index as c_int;
let result = unsafe { sqlite3_column_int(self.current_statement(), index) };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read int at index {index}"))?;
Ok(result)
}
pub fn bind_int64(&self, index: i32, int: i64) -> Result<()> {
let index = index as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_int64(*raw_statement, index, int);
}
}
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_int64(*raw_statement, index, int);
})
}
pub fn column_int64(&self, index: i32) -> Result<i64> {
let index = index as c_int;
let result = unsafe { sqlite3_column_int64(self.current_statement(), index) };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read i64 at index {index}"))?;
Ok(result)
}
pub fn bind_null(&self, index: i32) -> Result<()> {
let index = index as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_null(*raw_statement, index);
}
}
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_null(*raw_statement, index);
})
}
pub fn bind_text(&self, index: i32, text: &str) -> Result<()> {
let index = index as c_int;
let text_pointer = text.as_ptr() as *const _;
let len = text.len() as c_int;
unsafe {
for raw_statement in self.raw_statements.iter() {
sqlite3_bind_text(*raw_statement, index, text_pointer, len, SQLITE_TRANSIENT());
}
}
self.connection.last_error()
self.bind_index_with(index, |raw_statement| unsafe {
sqlite3_bind_text(*raw_statement, index, text_pointer, len, SQLITE_TRANSIENT());
})
}
pub fn column_text<'b>(&'b mut self, index: i32) -> Result<&'b str> {
let index = index as c_int;
let pointer = unsafe { sqlite3_column_text(self.current_statement(), index) };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read text from column {index}"))?;
if pointer.is_null() {
return Ok("");
}
let len = unsafe { sqlite3_column_bytes(self.current_statement(), index) as usize };
self.connection.last_error()?;
self.connection
.last_error()
.with_context(|| format!("Failed to read text length at {index}"))?;
let slice = unsafe { slice::from_raw_parts(pointer as *const u8, len) };
Ok(str::from_utf8(slice)?)
@ -247,11 +267,11 @@ impl<'a> Statement<'a> {
self.step()
}
}
SQLITE_MISUSE => Ok(StepResult::Misuse),
other => self
.connection
.last_error()
.map(|_| StepResult::Other(other)),
SQLITE_MISUSE => Err(anyhow!("Statement step returned SQLITE_MISUSE")),
_other_error => {
self.connection.last_error()?;
unreachable!("Step returned error code and last error failed to catch it");
}
}
}
}
@ -293,11 +313,17 @@ impl<'a> Statement<'a> {
callback: impl FnOnce(&mut Statement) -> Result<R>,
) -> Result<R> {
if this.step()? != StepResult::Row {
return Err(anyhow!("single called with query that returns no rows."));
}
let result = callback(this)?;
if this.step()? != StepResult::Done {
return Err(anyhow!(
"Single(Map) called with query that returns no rows."
"single called with a query that returns more than one row."
));
}
callback(this)
Ok(result)
}
let result = logic(self, callback);
self.reset();
@ -316,10 +342,21 @@ impl<'a> Statement<'a> {
this: &mut Statement,
callback: impl FnOnce(&mut Statement) -> Result<R>,
) -> Result<Option<R>> {
if this.step()? != StepResult::Row {
if this.step().context("Failed on step call")? != StepResult::Row {
return Ok(None);
}
callback(this).map(|r| Some(r))
let result = callback(this)
.map(|r| Some(r))
.context("Failed to parse row result")?;
if this.step().context("Second step call")? != StepResult::Done {
return Err(anyhow!(
"maybe called with a query that returns more than one row."
));
}
Ok(result)
}
let result = logic(self, callback);
self.reset();
@ -350,6 +387,38 @@ mod test {
statement::{Statement, StepResult},
};
#[test]
fn binding_multiple_statements_with_parameter_gaps() {
let connection =
Connection::open_memory(Some("binding_multiple_statements_with_parameter_gaps"));
connection
.exec(indoc! {"
CREATE TABLE test (
col INTEGER
)"})
.unwrap()()
.unwrap();
let statement = Statement::prepare(
&connection,
indoc! {"
INSERT INTO test(col) VALUES (?3);
SELECT * FROM test WHERE col = ?1"},
)
.unwrap();
statement
.bind_int(1, 1)
.expect("Could not bind parameter to first index");
statement
.bind_int(2, 2)
.expect("Could not bind parameter to second index");
statement
.bind_int(3, 3)
.expect("Could not bind parameter to third index");
}
#[test]
fn blob_round_trips() {
let connection1 = Connection::open_memory(Some("blob_round_trips"));

View file

@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use crate::{
bindable::{Bind, Column},
@ -49,6 +49,12 @@ impl Connection {
query: &str,
) -> Result<impl 'a + FnMut(B) -> Result<Option<C>>> {
let mut statement = Statement::prepare(&self, query)?;
Ok(move |bindings| statement.with_bindings(bindings)?.maybe_row::<C>())
Ok(move |bindings| {
statement
.with_bindings(bindings)
.context("Bindings failed")?
.maybe_row::<C>()
.context("Maybe row failed")
})
}
}