Skip to content

Commit

Permalink
Stop iteration upon exception
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Mar 10, 2024
1 parent df377a7 commit a73b36f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlite"
version = "0.33.0"
version = "0.34.0"
edition = "2021"
license = "Apache-2.0/MIT"
authors = [
Expand Down
27 changes: 19 additions & 8 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use crate::value::Value;
pub struct Cursor<'l, 'm> {
column_count: usize,
statement: &'m mut Statement<'l>,
poisoned: bool,
}

/// An iterator for a prepared statement with ownership.
pub struct CursorWithOwnership<'l> {
column_count: usize,
statement: Statement<'l>,
poisoned: bool,
}

/// A row.
Expand Down Expand Up @@ -66,6 +68,7 @@ macro_rules! implement(
#[allow(unused_mut)]
pub fn reset(mut self) -> Result<Self> {
self.statement.reset()?;
self.poisoned = false;
Ok(self)
}

Expand Down Expand Up @@ -95,15 +98,21 @@ macro_rules! implement(
type Item = Result<Row>;

fn next(&mut self) -> Option<Self::Item> {
let column_mapping = self.statement.column_mapping();
self.try_next()
.map(|values| {
values.map(|values| Row {
column_mapping,
if self.poisoned {
return None;
}
match self.try_next() {
Ok(value) => {
value.map(|values| Ok(Row {
column_mapping: self.statement.column_mapping(),
values,
})
})
.transpose()
}))
}
Err(error) => {
self.poisoned = true;
Some(Err(error))
}
}
}
}
}
Expand Down Expand Up @@ -203,12 +212,14 @@ pub fn new<'l, 'm>(statement: &'m mut Statement<'l>) -> Cursor<'l, 'm> {
Cursor {
column_count: statement.column_count(),
statement,
poisoned: false,
}
}

pub fn new_with_ownership(statement: Statement<'_>) -> CursorWithOwnership<'_> {
CursorWithOwnership {
column_count: statement.column_count(),
statement,
poisoned: false,
}
}
6 changes: 4 additions & 2 deletions tests/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use sqlite::{Result, Type, Value};
use sqlite::{Type, Value};

mod common;

Expand Down Expand Up @@ -97,7 +97,9 @@ fn iter_count_with_exception() {
ok!(connection
.execute("CREATE TRIGGER bar BEFORE INSERT ON foo BEGIN SELECT RAISE(FAIL, 'buz'); END"));
let mut statement = ok!(connection.prepare("INSERT INTO foo VALUES (0) RETURNING rowid;"));
assert!(statement.iter().collect::<Result<Vec<_>>>().is_err());
let results = statement.iter().collect::<Vec<_>>();
assert_eq!(results.len(), 1);
assert!(matches!(results[0], Err(_)));
}

#[test]
Expand Down

0 comments on commit a73b36f

Please sign in to comment.