sq3_rs/file_header/
schema_cookie.rs

1use std::ops::Deref;
2
3use sq3_derive::Name;
4use sq3_parser::TypeName;
5
6use crate::{result::SqliteResult, traits::ParseBytes};
7
8/// # Schema cookie (4 Bytes)
9///
10///  The schema cookie is a 4-byte big-endian integer at offset 40 that is
11/// incremented whenever the database schema changes. A prepared statement is
12/// compiled against a specific version of the database schema. When the
13/// database schema changes, the statement must be reprepared. When a prepared
14/// statement runs, it first checks the schema cookie to ensure the value is the
15/// same as when the statement was prepared and if the schema cookie has
16/// changed, the statement either automatically reprepares and reruns or it
17/// aborts with an [SQLITE_SCHEMA](https://www.sqlite.org/rescode.html#schema)
18/// error.
19#[derive(Debug, Default, Name)]
20pub struct SchemaCookie(u32);
21impl Deref for SchemaCookie {
22    type Target = u32;
23
24    fn deref(&self) -> &Self::Target {
25        &self.0
26    }
27}
28
29impl ParseBytes for SchemaCookie {
30    const LENGTH_BYTES: usize = 4;
31
32    fn parsing_handler(bytes: &[u8]) -> SqliteResult<Self> {
33        let buf: [u8; Self::LENGTH_BYTES] = bytes.try_into()?;
34
35        let database_size = u32::from_be_bytes(buf);
36
37        Ok(Self(database_size))
38    }
39}