Skip to main content

fsqlite_types/
limits.rs

1/// Maximum length of a TEXT or BLOB in bytes.
2/// Also limits the size of a row in a table or index.
3pub const MAX_LENGTH: u32 = 1_000_000_000;
4
5/// Minimum allowed value for the length limit.
6pub const MIN_LENGTH: u32 = 30;
7
8/// Maximum size of any single memory allocation.
9pub const MAX_ALLOCATION_SIZE: u32 = 2_147_483_391;
10
11/// Maximum number of columns in a table, index, or view.
12/// Also the maximum number of terms in SET, result set, GROUP BY, ORDER BY, VALUES.
13pub const MAX_COLUMN: u16 = 2000;
14
15/// Maximum length of a single SQL statement in bytes.
16pub const MAX_SQL_LENGTH: u32 = 1_000_000_000;
17
18/// Maximum depth of an expression tree.
19pub const MAX_EXPR_DEPTH: u32 = 1000;
20
21/// Maximum depth of the parser stack.
22pub const MAX_PARSER_DEPTH: u32 = 2500;
23
24/// Maximum number of terms in a compound SELECT statement.
25pub const MAX_COMPOUND_SELECT: u32 = 500;
26
27/// Maximum number of opcodes in a VDBE program.
28pub const MAX_VDBE_OP: u32 = 250_000_000;
29
30/// Maximum number of arguments to an SQL function.
31pub const MAX_FUNCTION_ARG: u16 = 1000;
32
33/// Default suggested cache size (negative means limit by memory in KB).
34/// -2000 means 2048000 bytes = ~2 MB.
35pub const DEFAULT_CACHE_SIZE: i32 = -2000;
36
37/// Default number of WAL frames before auto-checkpointing.
38pub const DEFAULT_WAL_AUTOCHECKPOINT: u32 = 1000;
39
40/// Maximum number of attached databases.
41pub const MAX_ATTACHED: u8 = 10;
42
43/// Maximum value of a `?nnn` wildcard parameter number.
44pub const MAX_VARIABLE_NUMBER: u32 = 32766;
45
46/// Maximum page size in bytes (must be 65536).
47pub const MAX_PAGE_SIZE: u32 = 65536;
48
49/// Default page size in bytes.
50pub const DEFAULT_PAGE_SIZE: u32 = 4096;
51
52/// Maximum default page size (used for auto-selection).
53pub const MAX_DEFAULT_PAGE_SIZE: u32 = 8192;
54
55/// Maximum number of pages in one database file.
56pub const MAX_PAGE_COUNT: u32 = 0xFFFF_FFFE;
57
58/// Maximum length in bytes of a LIKE or GLOB pattern.
59pub const MAX_LIKE_PATTERN_LENGTH: u32 = 50000;
60
61/// Maximum depth of recursion for triggers.
62pub const MAX_TRIGGER_DEPTH: u32 = 1000;
63
64/// Maximum depth of B-tree cursor stack (max tree depth).
65/// A database with 2^31 pages and a branching factor of 2 would have depth ~31.
66/// SQLite uses 20 as a safe maximum.
67pub const BTREE_MAX_DEPTH: u8 = 20;
68
69/// Minimum number of cells on a B-tree page.
70pub const BTREE_MIN_CELLS: u32 = 2;
71
72/// Size of the database file header in bytes.
73pub const DB_HEADER_SIZE: u32 = 100;
74
75/// Size of a B-tree page header for leaf pages.
76pub const BTREE_LEAF_HEADER_SIZE: u8 = 8;
77
78/// Size of a B-tree page header for interior pages.
79pub const BTREE_INTERIOR_HEADER_SIZE: u8 = 12;
80
81/// Size of a cell pointer (u16) in the cell pointer array.
82pub const CELL_POINTER_SIZE: u8 = 2;
83
84/// WAL magic number (big-endian checksums).
85pub const WAL_MAGIC_BE: u32 = 0x377F_0682;
86
87/// WAL magic number (little-endian checksums).
88pub const WAL_MAGIC_LE: u32 = 0x377F_0683;
89
90/// Size of a WAL frame header in bytes.
91pub const WAL_FRAME_HEADER_SIZE: u32 = 24;
92
93/// Size of the WAL file header in bytes.
94pub const WAL_HEADER_SIZE: u32 = 32;
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn limit_values_match_sqlite() {
102        assert_eq!(MAX_LENGTH, 1_000_000_000);
103        assert_eq!(MAX_COLUMN, 2000);
104        assert_eq!(MAX_SQL_LENGTH, 1_000_000_000);
105        assert_eq!(MAX_EXPR_DEPTH, 1000);
106        assert_eq!(MAX_PARSER_DEPTH, 2500);
107        assert_eq!(MAX_COMPOUND_SELECT, 500);
108        assert_eq!(MAX_VDBE_OP, 250_000_000);
109        assert_eq!(MAX_FUNCTION_ARG, 1000);
110        assert_eq!(DEFAULT_CACHE_SIZE, -2000);
111        assert_eq!(DEFAULT_WAL_AUTOCHECKPOINT, 1000);
112        assert_eq!(MAX_ATTACHED, 10);
113        assert_eq!(MAX_VARIABLE_NUMBER, 32766);
114        assert_eq!(MAX_PAGE_SIZE, 65536);
115        assert_eq!(DEFAULT_PAGE_SIZE, 4096);
116        assert_eq!(MAX_DEFAULT_PAGE_SIZE, 8192);
117        assert_eq!(MAX_PAGE_COUNT, 0xFFFF_FFFE);
118        assert_eq!(MAX_LIKE_PATTERN_LENGTH, 50000);
119        assert_eq!(MAX_TRIGGER_DEPTH, 1000);
120    }
121
122    #[test]
123    fn default_page_size_is_power_of_two() {
124        assert!(DEFAULT_PAGE_SIZE.is_power_of_two());
125    }
126
127    #[test]
128    fn max_page_size_is_power_of_two() {
129        assert!(MAX_PAGE_SIZE.is_power_of_two());
130    }
131
132    #[test]
133    fn wal_constants() {
134        assert_eq!(WAL_FRAME_HEADER_SIZE, 24);
135        assert_eq!(WAL_HEADER_SIZE, 32);
136    }
137}