sqlite_rs/header/
schema_cookie.rs

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