mirror of
https://github.com/isar/rusqlite.git
synced 2025-01-20 19:00:52 +08:00
Merge remote-tracking branch 'jgallagher/master' into upgrade-deps
This commit is contained in:
commit
41c74cd15e
28
src/cache.rs
28
src/cache.rs
@ -46,6 +46,7 @@ impl Connection {
|
|||||||
self.cache.set_capacity(capacity)
|
self.cache.set_capacity(capacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove/finalize all prepared statements currently in the cache.
|
||||||
pub fn flush_prepared_statement_cache(&self) {
|
pub fn flush_prepared_statement_cache(&self) {
|
||||||
self.cache.flush()
|
self.cache.flush()
|
||||||
}
|
}
|
||||||
@ -124,7 +125,7 @@ impl StatementCache {
|
|||||||
sql: &str)
|
sql: &str)
|
||||||
-> Result<CachedStatement<'conn>> {
|
-> Result<CachedStatement<'conn>> {
|
||||||
let mut cache = self.0.borrow_mut();
|
let mut cache = self.0.borrow_mut();
|
||||||
let stmt = match cache.remove(sql) {
|
let stmt = match cache.remove(sql.trim()) {
|
||||||
Some(raw_stmt) => Ok(Statement::new(conn, raw_stmt)),
|
Some(raw_stmt) => Ok(Statement::new(conn, raw_stmt)),
|
||||||
None => conn.prepare(sql),
|
None => conn.prepare(sql),
|
||||||
};
|
};
|
||||||
@ -135,7 +136,7 @@ impl StatementCache {
|
|||||||
fn cache_stmt(&self, stmt: RawStatement) {
|
fn cache_stmt(&self, stmt: RawStatement) {
|
||||||
let mut cache = self.0.borrow_mut();
|
let mut cache = self.0.borrow_mut();
|
||||||
stmt.clear_bindings();
|
stmt.clear_bindings();
|
||||||
let sql = String::from_utf8_lossy(stmt.sql().to_bytes()).to_string();
|
let sql = String::from_utf8_lossy(stmt.sql().to_bytes()).trim().to_string();
|
||||||
cache.insert(sql, stmt);
|
cache.insert(sql, stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,4 +286,27 @@ mod test {
|
|||||||
|
|
||||||
conn.close().expect("connection not closed");
|
conn.close().expect("connection not closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cache_key() {
|
||||||
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
let cache = &db.cache;
|
||||||
|
assert_eq!(0, cache.len());
|
||||||
|
|
||||||
|
//let sql = " PRAGMA schema_version; -- comment";
|
||||||
|
let sql = "PRAGMA schema_version; ";
|
||||||
|
{
|
||||||
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
|
assert_eq!(0, cache.len());
|
||||||
|
assert_eq!(0, stmt.query_row(&[], |r| r.get::<i32, i64>(0)).unwrap());
|
||||||
|
}
|
||||||
|
assert_eq!(1, cache.len());
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
|
assert_eq!(0, cache.len());
|
||||||
|
assert_eq!(0, stmt.query_row(&[], |r| r.get::<i32, i64>(0)).unwrap());
|
||||||
|
}
|
||||||
|
assert_eq!(1, cache.len());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,13 @@
|
|||||||
//! * Strings (`String` and `&str`)
|
//! * Strings (`String` and `&str`)
|
||||||
//! * Blobs (`Vec<u8>` and `&[u8]`)
|
//! * Blobs (`Vec<u8>` and `&[u8]`)
|
||||||
//!
|
//!
|
||||||
//! Additionally, because it is such a common data type, implementations are provided for
|
//! Additionally, because it is such a common data type, implementations are
|
||||||
//! `time::Timespec` that use a string for storage (using the same format string,
|
//! provided for `time::Timespec` that use the RFC 3339 date/time format,
|
||||||
//! `"%Y-%m-%d %H:%M:%S"`, as SQLite's builtin
|
//! `"%Y-%m-%dT%H:%M:%S.%fZ"`, to store time values as strings. These values
|
||||||
//! [datetime](https://www.sqlite.org/lang_datefunc.html) function. Note that this storage
|
//! can be parsed by SQLite's builtin
|
||||||
//! truncates timespecs to the nearest second. If you want different storage for timespecs, you can
|
//! [datetime](https://www.sqlite.org/lang_datefunc.html) functions. If you
|
||||||
//! use a newtype. For example, to store timespecs as `f64`s:
|
//! want different storage for timespecs, you can use a newtype. For example, to
|
||||||
|
//! store timespecs as `f64`s:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! extern crate rusqlite;
|
//! extern crate rusqlite;
|
||||||
|
@ -3,7 +3,8 @@ extern crate time;
|
|||||||
use Result;
|
use Result;
|
||||||
use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
||||||
|
|
||||||
const SQLITE_DATETIME_FMT: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
|
const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%fZ";
|
||||||
|
const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
|
||||||
|
|
||||||
impl ToSql for time::Timespec {
|
impl ToSql for time::Timespec {
|
||||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||||
@ -19,10 +20,12 @@ impl FromSql for time::Timespec {
|
|||||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||||
value
|
value
|
||||||
.as_str()
|
.as_str()
|
||||||
.and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) {
|
.and_then(|s| {
|
||||||
Ok(tm) => Ok(tm.to_timespec()),
|
time::strptime(s, SQLITE_DATETIME_FMT)
|
||||||
Err(err) => Err(FromSqlError::Other(Box::new(err))),
|
.or_else(|err| {
|
||||||
})
|
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
|
||||||
|
.or(Err(FromSqlError::Other(Box::new(err))))})})
|
||||||
|
.map(|tm| tm.to_timespec())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user