2017-02-04 18:01:38 +08:00
|
|
|
//! Run-Time Limits
|
|
|
|
|
2017-02-17 00:17:24 +08:00
|
|
|
use std::os::raw::c_int;
|
2017-02-04 18:01:38 +08:00
|
|
|
|
|
|
|
use ffi;
|
|
|
|
pub use ffi::Limit;
|
|
|
|
|
|
|
|
use Connection;
|
|
|
|
|
|
|
|
impl Connection {
|
|
|
|
/// Returns the current value of a limit.
|
2017-02-07 00:43:32 +08:00
|
|
|
pub fn limit(&self, limit: Limit) -> i32 {
|
2017-02-04 18:01:38 +08:00
|
|
|
let c = self.db.borrow();
|
2017-02-07 00:43:32 +08:00
|
|
|
unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, -1) }
|
2017-02-04 18:01:38 +08:00
|
|
|
}
|
2017-02-08 08:43:54 +08:00
|
|
|
|
|
|
|
/// Changes the limit to `new_val`, returning the prior value of the limit.
|
2017-02-04 18:01:38 +08:00
|
|
|
pub fn set_limit(&self, limit: Limit, new_val: i32) -> i32 {
|
|
|
|
let c = self.db.borrow_mut();
|
2017-02-07 00:43:32 +08:00
|
|
|
unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, new_val) }
|
2017-02-04 18:01:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2017-02-08 08:43:54 +08:00
|
|
|
use ffi::Limit;
|
2017-02-04 18:01:38 +08:00
|
|
|
use Connection;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_limit() {
|
|
|
|
let db = Connection::open_in_memory().unwrap();
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_LENGTH, 1024);
|
|
|
|
assert_eq!(1024, db.limit(Limit::SQLITE_LIMIT_LENGTH));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_SQL_LENGTH, 1024);
|
2017-02-07 00:43:32 +08:00
|
|
|
assert_eq!(1024, db.limit(Limit::SQLITE_LIMIT_SQL_LENGTH));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_COLUMN, 64);
|
|
|
|
assert_eq!(64, db.limit(Limit::SQLITE_LIMIT_COLUMN));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_EXPR_DEPTH, 256);
|
|
|
|
assert_eq!(256, db.limit(Limit::SQLITE_LIMIT_EXPR_DEPTH));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_COMPOUND_SELECT, 32);
|
|
|
|
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_COMPOUND_SELECT));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_FUNCTION_ARG, 32);
|
|
|
|
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_FUNCTION_ARG));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_ATTACHED, 2);
|
|
|
|
assert_eq!(2, db.limit(Limit::SQLITE_LIMIT_ATTACHED));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-07 00:43:32 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 128);
|
|
|
|
assert_eq!(128, db.limit(Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER, 99);
|
2017-02-07 00:43:32 +08:00
|
|
|
assert_eq!(99, db.limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER));
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-10 09:12:24 +08:00
|
|
|
// SQLITE_LIMIT_TRIGGER_DEPTH was added in SQLite 3.6.18.
|
|
|
|
if ::version_number() >= 3006018 {
|
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_TRIGGER_DEPTH, 32);
|
|
|
|
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_TRIGGER_DEPTH));
|
|
|
|
}
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2017-02-08 08:43:54 +08:00
|
|
|
// SQLITE_LIMIT_WORKER_THREADS was added in SQLite 3.8.7.
|
|
|
|
if ::version_number() >= 3008007 {
|
2017-02-08 01:41:14 +08:00
|
|
|
db.set_limit(Limit::SQLITE_LIMIT_WORKER_THREADS, 2);
|
|
|
|
assert_eq!(2, db.limit(Limit::SQLITE_LIMIT_WORKER_THREADS));
|
|
|
|
}
|
2017-02-04 18:01:38 +08:00
|
|
|
}
|
|
|
|
}
|