2021-06-13 15:17:35 +08:00
|
|
|
//! Run-Time Limits
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2022-01-09 07:55:12 +08:00
|
|
|
use crate::{ffi, Connection};
|
2017-02-17 00:17:24 +08:00
|
|
|
use std::os::raw::c_int;
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2022-01-09 07:55:12 +08:00
|
|
|
/// Run-Time limit categories, for use with [`Connection::limit`] and
|
|
|
|
/// [`Connection::set_limit`].
|
|
|
|
///
|
|
|
|
/// See the official documentation for more information:
|
|
|
|
/// - <https://www.sqlite.org/c3ref/c_limit_attached.html>
|
|
|
|
/// - <https://www.sqlite.org/limits.html>
|
|
|
|
#[repr(i32)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
#[allow(clippy::upper_case_acronyms, non_camel_case_types)]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "limits")))]
|
|
|
|
pub enum Limit {
|
|
|
|
/// The maximum size of any string or BLOB or table row, in bytes.
|
|
|
|
SQLITE_LIMIT_LENGTH = ffi::SQLITE_LIMIT_LENGTH,
|
|
|
|
/// The maximum length of an SQL statement, in bytes.
|
|
|
|
SQLITE_LIMIT_SQL_LENGTH = ffi::SQLITE_LIMIT_SQL_LENGTH,
|
|
|
|
/// The maximum number of columns in a table definition or in the result set
|
|
|
|
/// of a SELECT or the maximum number of columns in an index or in an
|
|
|
|
/// ORDER BY or GROUP BY clause.
|
|
|
|
SQLITE_LIMIT_COLUMN = ffi::SQLITE_LIMIT_COLUMN,
|
|
|
|
/// The maximum depth of the parse tree on any expression.
|
|
|
|
SQLITE_LIMIT_EXPR_DEPTH = ffi::SQLITE_LIMIT_EXPR_DEPTH,
|
|
|
|
/// The maximum number of terms in a compound SELECT statement.
|
|
|
|
SQLITE_LIMIT_COMPOUND_SELECT = ffi::SQLITE_LIMIT_COMPOUND_SELECT,
|
|
|
|
/// The maximum number of instructions in a virtual machine program used to
|
|
|
|
/// implement an SQL statement.
|
|
|
|
SQLITE_LIMIT_VDBE_OP = ffi::SQLITE_LIMIT_VDBE_OP,
|
|
|
|
/// The maximum number of arguments on a function.
|
|
|
|
SQLITE_LIMIT_FUNCTION_ARG = ffi::SQLITE_LIMIT_FUNCTION_ARG,
|
|
|
|
/// The maximum number of attached databases.
|
|
|
|
SQLITE_LIMIT_ATTACHED = ffi::SQLITE_LIMIT_ATTACHED,
|
|
|
|
/// The maximum length of the pattern argument to the LIKE or GLOB
|
|
|
|
/// operators.
|
|
|
|
SQLITE_LIMIT_LIKE_PATTERN_LENGTH = ffi::SQLITE_LIMIT_LIKE_PATTERN_LENGTH,
|
|
|
|
/// The maximum index number of any parameter in an SQL statement.
|
|
|
|
SQLITE_LIMIT_VARIABLE_NUMBER = ffi::SQLITE_LIMIT_VARIABLE_NUMBER,
|
|
|
|
/// The maximum depth of recursion for triggers.
|
2022-09-03 02:04:18 +08:00
|
|
|
SQLITE_LIMIT_TRIGGER_DEPTH = ffi::SQLITE_LIMIT_TRIGGER_DEPTH,
|
2022-01-09 07:55:12 +08:00
|
|
|
/// The maximum number of auxiliary worker threads that a single prepared
|
|
|
|
/// statement may start.
|
2022-09-03 02:04:18 +08:00
|
|
|
SQLITE_LIMIT_WORKER_THREADS = ffi::SQLITE_LIMIT_WORKER_THREADS,
|
2022-01-09 07:55:12 +08:00
|
|
|
}
|
2017-02-04 18:01:38 +08:00
|
|
|
|
|
|
|
impl Connection {
|
2022-01-09 07:55:12 +08:00
|
|
|
/// Returns the current value of a [`Limit`].
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2022-01-09 07:55:12 +08:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "limits")))]
|
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
|
|
|
|
2022-01-09 07:55:12 +08:00
|
|
|
/// Changes the [`Limit`] to `new_val`, returning the prior
|
2020-04-06 13:15:27 +08:00
|
|
|
/// value of the limit.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2022-01-09 07:55:12 +08:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "limits")))]
|
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 {
|
2022-01-09 07:55:12 +08:00
|
|
|
use super::*;
|
2020-11-06 05:14:00 +08:00
|
|
|
use crate::{Connection, Result};
|
2017-02-04 18:01:38 +08:00
|
|
|
|
2022-01-09 07:55:12 +08:00
|
|
|
#[test]
|
|
|
|
fn test_limit_values() {
|
2022-10-09 00:45:51 +08:00
|
|
|
assert_eq!(Limit::SQLITE_LIMIT_LENGTH as i32, ffi::SQLITE_LIMIT_LENGTH,);
|
2022-01-09 07:55:12 +08:00
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_SQL_LENGTH as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_SQL_LENGTH,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
2022-10-09 00:45:51 +08:00
|
|
|
assert_eq!(Limit::SQLITE_LIMIT_COLUMN as i32, ffi::SQLITE_LIMIT_COLUMN,);
|
2022-01-09 07:55:12 +08:00
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_EXPR_DEPTH as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_EXPR_DEPTH,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_COMPOUND_SELECT as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_COMPOUND_SELECT,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_VDBE_OP as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_VDBE_OP,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_FUNCTION_ARG as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_FUNCTION_ARG,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_ATTACHED as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_ATTACHED,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_LIKE_PATTERN_LENGTH,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_VARIABLE_NUMBER as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_VARIABLE_NUMBER,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
#[cfg(feature = "bundled")]
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_TRIGGER_DEPTH as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_TRIGGER_DEPTH,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
#[cfg(feature = "bundled")]
|
|
|
|
assert_eq!(
|
|
|
|
Limit::SQLITE_LIMIT_WORKER_THREADS as i32,
|
2022-10-09 00:45:51 +08:00
|
|
|
ffi::SQLITE_LIMIT_WORKER_THREADS,
|
2022-01-09 07:55:12 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-04 18:01:38 +08:00
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_limit() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
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.
|
2019-02-02 18:37:26 +08:00
|
|
|
if crate::version_number() >= 3_006_018 {
|
2017-02-10 09:12:24 +08:00
|
|
|
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.
|
2019-02-02 18:37:26 +08:00
|
|
|
if crate::version_number() >= 3_008_007 {
|
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));
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-02-04 18:01:38 +08:00
|
|
|
}
|
|
|
|
}
|