| 1 |
use std::cell::RefCell; |
| 2 |
use std::rc::Rc; |
| 3 |
|
| 4 |
use super::row::{PrivateSqliteRow, SqliteRow}; |
| 5 |
use super::stmt::StatementUse; |
| 6 |
use crate::result::QueryResult; |
| 7 |
|
| 8 |
#[allow(missing_debug_implementations)] |
| 9 |
pub struct StatementIterator<'stmt, 'query> { |
| 10 |
inner: PrivateStatementIterator<'stmt, 'query>, |
| 11 |
column_names: Option<Rc<[Option<String>]>>, |
| 12 |
field_count: usize, |
| 13 |
} |
| 14 |
|
| 15 |
impl<'stmt, 'query> StatementIterator<'stmt, 'query> { |
| 16 |
#[cold] |
| 17 |
#[allow(unsafe_code)] // call to unsafe function |
| 18 |
fn handle_duplicated_row_case( |
| 19 |
outer_last_row: &mut Rc<RefCell<PrivateSqliteRow<'stmt, 'query>>>, |
| 20 |
column_names: &mut Option<Rc<[Option<String>]>>, |
| 21 |
field_count: usize, |
| 22 |
) -> Option<QueryResult<SqliteRow<'stmt, 'query>>> { |
| 23 |
// We don't own the statement. There is another existing reference, likely because |
| 24 |
// a user stored the row in some long time container before calling next another time |
| 25 |
// In this case we copy out the current values into a temporary store and advance |
| 26 |
// the statement iterator internally afterwards |
| 27 |
let last_row = { |
| 28 |
let mut last_row = match outer_last_row.try_borrow_mut() { |
| 29 |
Ok(o) => o, |
| 30 |
Err(_e) => { |
| 31 |
return Some(Err(crate::result::Error::DeserializationError( |
| 32 |
"Failed to reborrow row. Try to release any `SqliteField` or `SqliteValue` \ |
| 33 |
that exists at this point" |
| 34 |
.into(), |
| 35 |
))); |
| 36 |
} |
| 37 |
}; |
| 38 |
let last_row = &mut *last_row; |
| 39 |
let duplicated = last_row.duplicate(column_names); |
| 40 |
std::mem::replace(last_row, duplicated) |
| 41 |
}; |
| 42 |
if let PrivateSqliteRow::Direct(mut stmt) = last_row { |
| 43 |
let res = unsafe { |
| 44 |
// This is actually safe here as we've already |
| 45 |
// performed one step. For the first step we would have |
| 46 |
// used `PrivateStatementIterator::NotStarted` where we don't |
| 47 |
// have access to `PrivateSqliteRow` at all |
| 48 |
stmt.step(false) |
| 49 |
}; |
| 50 |
*outer_last_row = Rc::new(RefCell::new(PrivateSqliteRow::Direct(stmt))); |
| 51 |
match res { |
| 52 |
Err(e) => Some(Err(e)), |
| 53 |
Ok(false) => None, |
| 54 |
Ok(true) => Some(Ok(SqliteRow { |
| 55 |
inner: Rc::clone(outer_last_row), |
| 56 |
field_count, |
| 57 |
})), |
| 58 |
} |
| 59 |
} else { |
| 60 |
// any other state than `PrivateSqliteRow::Direct` is invalid here |
| 61 |
// and should not happen. If this ever happens this is a logic error |
| 62 |
// in the code above |
| 63 |
unreachable!( |
| 64 |
"You've reached an impossible internal state. \ |
| 65 |
If you ever see this error message please open \ |
| 66 |
an issue at https://github.com/diesel-rs/diesel \ |
| 67 |
providing example code how to trigger this error." |
| 68 |
) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
enum PrivateStatementIterator<'stmt, 'query> { |
| 74 |
NotStarted(Option<StatementUse<'stmt, 'query>>), |
| 75 |
Started(Rc<RefCell<PrivateSqliteRow<'stmt, 'query>>>), |
| 76 |
} |
| 77 |
|
| 78 |
impl<'stmt, 'query> StatementIterator<'stmt, 'query> { |
| 79 |
pub fn new(stmt: StatementUse<'stmt, 'query>) -> StatementIterator<'stmt, 'query> { |
| 80 |
Self { |
| 81 |
inner: PrivateStatementIterator::NotStarted(Some(stmt)), |
| 82 |
column_names: None, |
| 83 |
field_count: 0, |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl<'stmt, 'query> Iterator for StatementIterator<'stmt, 'query> { |
| 89 |
type Item = QueryResult<SqliteRow<'stmt, 'query>>; |
| 90 |
|
| 91 |
#[allow(unsafe_code)] // call to unsafe function |
| 92 |
fn next(&mut self) -> Option<Self::Item> { |
| 93 |
use PrivateStatementIterator::{NotStarted, Started}; |
| 94 |
match &mut self.inner { |
| 95 |
NotStarted(stmt @ Some(_)) => { |
| 96 |
let mut stmt = stmt |
| 97 |
.take() |
| 98 |
.expect("It must be there because we checked that above"); |
| 99 |
let step = unsafe { |
| 100 |
// This is safe as we pass `first_step = true` to reset the cached column names |
| 101 |
stmt.step(true) |
| 102 |
}; |
| 103 |
match step { |
| 104 |
Err(e) => Some(Err(e)), |
| 105 |
Ok(false) => None, |
| 106 |
Ok(true) => { |
| 107 |
let field_count = stmt |
| 108 |
.column_count() |
| 109 |
.try_into() |
| 110 |
.expect("Diesel expects to run at least on a 32 bit platform"); |
| 111 |
self.field_count = field_count; |
| 112 |
let inner = Rc::new(RefCell::new(PrivateSqliteRow::Direct(stmt))); |
| 113 |
self.inner = Started(inner.clone()); |
| 114 |
Some(Ok(SqliteRow { inner, field_count })) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
Started(last_row) => { |
| 119 |
// There was already at least one iteration step |
| 120 |
// We check here if the caller already released the row value or not |
| 121 |
// by checking if our Rc owns the data or not |
| 122 |
if let Some(last_row_ref) = Rc::get_mut(last_row) { |
| 123 |
// We own the statement, there is no other reference here. |
| 124 |
// This means we don't need to copy out values from the sqlite provided |
| 125 |
// datastructures for now |
| 126 |
// We don't need to use the runtime borrowing system of the RefCell here |
| 127 |
// as we have a mutable reference, so all of this below is checked at compile time |
| 128 |
if let PrivateSqliteRow::Direct(stmt) = last_row_ref.get_mut() { |
| 129 |
let step = unsafe { |
| 130 |
// This is actually safe here as we've already |
| 131 |
// performed one step. For the first step we would have |
| 132 |
// used `PrivateStatementIterator::NotStarted` where we don't |
| 133 |
// have access to `PrivateSqliteRow` at all |
| 134 |
|
| 135 |
stmt.step(false) |
| 136 |
}; |
| 137 |
match step { |
| 138 |
Err(e) => Some(Err(e)), |
| 139 |
Ok(false) => None, |
| 140 |
Ok(true) => { |
| 141 |
let field_count = self.field_count; |
| 142 |
Some(Ok(SqliteRow { |
| 143 |
inner: Rc::clone(last_row), |
| 144 |
field_count, |
| 145 |
})) |
| 146 |
} |
| 147 |
} |
| 148 |
} else { |
| 149 |
// any other state than `PrivateSqliteRow::Direct` is invalid here |
| 150 |
// and should not happen. If this ever happens this is a logic error |
| 151 |
// in the code above |
| 152 |
unreachable!( |
| 153 |
"You've reached an impossible internal state. \ |
| 154 |
If you ever see this error message please open \ |
| 155 |
an issue at https://github.com/diesel-rs/diesel \ |
| 156 |
providing example code how to trigger this error." |
| 157 |
) |
| 158 |
} |
| 159 |
} else { |
| 160 |
Self::handle_duplicated_row_case( |
| 161 |
last_row, |
| 162 |
&mut self.column_names, |
| 163 |
self.field_count, |
| 164 |
) |
| 165 |
} |
| 166 |
} |
| 167 |
NotStarted(_s) => { |
| 168 |
// we likely got an error while executing the other |
| 169 |
// `NotStarted` branch above. In this case we just want to stop |
| 170 |
// iterating here |
| 171 |
None |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |