mirror of
https://github.com/isar/rusqlite.git
synced 2025-03-29 00:32:57 +08:00
Fix Batch impl
- document that there is no parsing error recovery - and remove `Iterator` impl to prevent users from looping indefinitely on Some(Err(..))
This commit is contained in:
parent
13bbefdad3
commit
0bf85a801c
29
src/lib.rs
29
src/lib.rs
@ -54,6 +54,8 @@
|
|||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||||
|
|
||||||
|
pub use fallible_iterator;
|
||||||
|
pub use fallible_streaming_iterator;
|
||||||
pub use libsqlite3_sys as ffi;
|
pub use libsqlite3_sys as ffi;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -1095,7 +1097,13 @@ impl fmt::Debug for Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Batch iterator
|
/// Batch fallible iterator
|
||||||
|
///
|
||||||
|
/// # Warning
|
||||||
|
///
|
||||||
|
/// There is no recovery on parsing error, when a invalid statement is found in `sql`, SQLite cannot jump to the next statement.
|
||||||
|
/// So you should break the loop when an error is raised by the `next` method.
|
||||||
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use rusqlite::{Batch, Connection, Result};
|
/// use rusqlite::{Batch, Connection, Result};
|
||||||
///
|
///
|
||||||
@ -1124,12 +1132,16 @@ impl<'conn, 'sql> Batch<'conn, 'sql> {
|
|||||||
pub fn new(conn: &'conn Connection, sql: &'sql str) -> Self {
|
pub fn new(conn: &'conn Connection, sql: &'sql str) -> Self {
|
||||||
Batch { conn, sql, tail: 0 }
|
Batch { conn, sql, tail: 0 }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
impl<'conn> fallible_iterator::FallibleIterator for Batch<'conn, '_> {
|
||||||
|
type Error = Error;
|
||||||
|
type Item = Statement<'conn>;
|
||||||
|
|
||||||
/// Iterates on each batch statements.
|
/// Iterates on each batch statements.
|
||||||
///
|
///
|
||||||
/// Returns `Ok(None)` when batch is completed.
|
/// Returns `Ok(None)` when batch is completed.
|
||||||
#[expect(clippy::should_implement_trait)] // fallible iterator
|
#[expect(clippy::should_implement_trait)] // fallible iterator
|
||||||
pub fn next(&mut self) -> Result<Option<Statement<'conn>>> {
|
fn next(&mut self) -> Result<Option<Statement<'conn>>> {
|
||||||
while self.tail < self.sql.len() {
|
while self.tail < self.sql.len() {
|
||||||
let sql = &self.sql[self.tail..];
|
let sql = &self.sql[self.tail..];
|
||||||
let next = self.conn.prepare(sql)?;
|
let next = self.conn.prepare(sql)?;
|
||||||
@ -1148,14 +1160,6 @@ impl<'conn, 'sql> Batch<'conn, 'sql> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> Iterator for Batch<'conn, '_> {
|
|
||||||
type Item = Result<Statement<'conn>>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Result<Statement<'conn>>> {
|
|
||||||
self.next().transpose()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags::bitflags! {
|
bitflags::bitflags! {
|
||||||
/// Flags for opening SQLite database connections. See
|
/// Flags for opening SQLite database connections. See
|
||||||
/// [sqlite3_open_v2](https://www.sqlite.org/c3ref/open.html) for details.
|
/// [sqlite3_open_v2](https://www.sqlite.org/c3ref/open.html) for details.
|
||||||
@ -2202,9 +2206,8 @@ mod test {
|
|||||||
CREATE TABLE tbl1 (col);
|
CREATE TABLE tbl1 (col);
|
||||||
CREATE TABLE tbl2 (col);
|
CREATE TABLE tbl2 (col);
|
||||||
";
|
";
|
||||||
let batch = Batch::new(&db, sql);
|
let mut batch = Batch::new(&db, sql);
|
||||||
for stmt in batch {
|
while let Some(mut stmt) = batch.next()? {
|
||||||
let mut stmt = stmt?;
|
|
||||||
stmt.execute([])?;
|
stmt.execute([])?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -5,7 +5,7 @@ use std::convert;
|
|||||||
use super::{Error, Result, Statement};
|
use super::{Error, Result, Statement};
|
||||||
use crate::types::{FromSql, FromSqlError, ValueRef};
|
use crate::types::{FromSql, FromSqlError, ValueRef};
|
||||||
|
|
||||||
/// A handle for the resulting rows of a query.
|
/// A handle (lazy fallible streaming iterator) for the resulting rows of a query.
|
||||||
#[must_use = "Rows is lazy and will do nothing unless consumed"]
|
#[must_use = "Rows is lazy and will do nothing unless consumed"]
|
||||||
pub struct Rows<'stmt> {
|
pub struct Rows<'stmt> {
|
||||||
pub(crate) stmt: Option<&'stmt Statement<'stmt>>,
|
pub(crate) stmt: Option<&'stmt Statement<'stmt>>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user