/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use lazy_static::lazy_static; use rusqlite::{self, limits::Limit, types::ToSql}; use std::iter::Map; use std::slice::Iter;
/// Returns SQLITE_LIMIT_VARIABLE_NUMBER as read from an in-memory connection and cached. /// connection and cached. That means this will return the wrong value if it's set to a lower /// value for a connection using this will return the wrong thing, but doing so is rare enough /// that we explicitly don't support it (why would you want to lower this at runtime?). /// /// If you call this and the actual value was set to a negative number or zero (nothing prevents /// this beyond a warning in the SQLite documentation), we panic. However, it's unlikely you can /// run useful queries if this happened anyway. pubfn default_max_variable_number() -> usize {
lazy_static! { staticref MAX_VARIABLE_NUMBER: usize = { let conn = rusqlite::Connection::open_in_memory()
.expect("Failed to initialize in-memory connection (out of memory?)");
let limit = conn.limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER);
assert!(
limit > 0, "Illegal value for SQLITE_LIMIT_VARIABLE_NUMBER (must be > 0) {}",
limit
);
limit as usize
};
}
*MAX_VARIABLE_NUMBER
}
/// Helper for the case where you have a `&[impl ToSql]` of arbitrary length, but need one /// of no more than the connection's `MAX_VARIABLE_NUMBER` (rather, /// `default_max_variable_number()`). This is useful when performing batched updates. /// /// The `do_chunk` callback is called with a slice of no more than `default_max_variable_number()` /// items as it's first argument, and the offset from the start as it's second. /// /// See `each_chunk_mapped` for the case where `T` doesn't implement `ToSql`, but can be /// converted to something that does. pubfn each_chunk<'a, T, E, F>(items: &'a [T], do_chunk: F) -> Result<(), E> where
T: 'a,
F: FnMut(&'a [T], usize) -> Result<(), E>,
{
each_sized_chunk(items, default_max_variable_number(), do_chunk)
}
/// A version of `each_chunk` for the case when the conversion to `to_sql` requires an custom /// intermediate step. For example, you might want to grab a property off of an array of records pubfn each_chunk_mapped<'a, T, U, E, Mapper, DoChunk>(
items: &'a [T],
to_sql: Mapper,
do_chunk: DoChunk,
) -> Result<(), E> where
T: 'a,
U: ToSql + 'a,
Mapper: Fn(&'a T) -> U,
DoChunk: FnMut(Map<Iter<'a, T>, &'_ Mapper>, usize) -> Result<(), E>,
{
each_sized_chunk_mapped(items, default_max_variable_number(), to_sql, do_chunk)
}
// Split out for testing. Separate so that we can pass an actual slice // to the callback if they don't need mapping. We could probably unify // this with each_sized_chunk_mapped with a lot of type system trickery, // but one of the benefits to each_chunk over the mapped versions is // that the declaration is simpler. pubfn each_sized_chunk<'a, T, E, F>(
items: &'a [T],
chunk_size: usize, mut do_chunk: F,
) -> Result<(), E> where
T: 'a,
F: FnMut(&'a [T], usize) -> Result<(), E>,
{ if items.is_empty() { return Ok(());
} letmut offset = 0; for chunk in items.chunks(chunk_size) {
do_chunk(chunk, offset)?;
offset += chunk.len();
}
Ok(())
}
/// Utility to help perform batched updates, inserts, queries, etc. This is the low-level version /// of this utility which is wrapped by `each_chunk` and `each_chunk_mapped`, and it allows you to /// provide both the mapping function, and the chunk size. /// /// Note: `mapped` basically just refers to the translating of `T` to some `U` where `U: ToSql` /// using the `to_sql` function. This is useful for e.g. inserting the IDs of a large list /// of records. pubfn each_sized_chunk_mapped<'a, T, U, E, Mapper, DoChunk>(
items: &'a [T],
chunk_size: usize,
to_sql: Mapper, mut do_chunk: DoChunk,
) -> Result<(), E> where
T: 'a,
U: ToSql + 'a,
Mapper: Fn(&'a T) -> U,
DoChunk: FnMut(Map<Iter<'a, T>, &'_ Mapper>, usize) -> Result<(), E>,
{ if items.is_empty() { return Ok(());
} letmut offset = 0; for chunk in items.chunks(chunk_size) { let mapped = chunk.iter().map(&to_sql);
do_chunk(mapped, offset)?;
offset += chunk.len();
}
Ok(())
}
#[cfg(test)] fn check_chunk<T, C>(items: C, expect: &[T], desc: &str) where
C: IntoIterator,
<C as IntoIterator>::Item: ToSql,
T: ToSql,
{ let items = items.into_iter().collect::<Vec<_>>();
assert_eq!(items.len(), expect.len()); // Can't quite make the borrowing work out here w/o a loop, oh well. for (idx, (got, want)) in items.iter().zip(expect.iter()).enumerate() {
assert_eq!(
got.to_sql().unwrap(),
want.to_sql().unwrap(), // ToSqlOutput::Owned(Value::Integer(*num)), "{}: Bad value at index {}",
desc,
idx
);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.