Skip to main content

cartel_sqlite/
database.rs

1use crate::{options, runtime, sys};
2use std::{mem, time};
3
4#[derive(Debug)]
5pub enum Error {
6    Catalog(crate::Error),
7    ParameterItemsZero,
8    ParameterItems {
9        actual: usize,
10    },
11    ParameterBytesZero,
12    ScanRowsZero,
13    RowBytesZero,
14    RowBytes {
15        actual: usize,
16    },
17    CollectRows {
18        actual: usize,
19        maximum: usize,
20    },
21    CollectBytes {
22        actual: usize,
23        required: usize,
24    },
25    StatementBytes {
26        actual: usize,
27        maximum: usize,
28    },
29    MainCacheBytes {
30        actual: usize,
31    },
32    MainCacheInteger {
33        actual: usize,
34    },
35    TemporaryCacheBytes {
36        actual: usize,
37    },
38    TemporaryCacheInteger {
39        actual: usize,
40    },
41    Contention {
42        operation: Operation,
43        interval: time::Duration,
44        window: time::Duration,
45    },
46    Savepoints {
47        actual: usize,
48        maximum: usize,
49    },
50    Runtime(runtime::Error),
51}
52
53#[derive(Clone, Copy, PartialEq, Eq, Debug)]
54pub enum Operation {
55    Open,
56    Call,
57}
58
59pub struct Target<'sql> {
60    location: Location,
61    options: options::OpenOptions,
62    commands: sys::Commands<'sql>,
63}
64
65const _: () = {
66    assert!(!mem::needs_drop::<Error>());
67    assert!(mem::size_of::<Error>() <= 5 * mem::size_of::<usize>());
68};
69
70pub(crate) enum Location {
71    File(sys::Filename),
72    Memory,
73}
74
75#[derive(Clone, Copy)]
76pub(crate) enum Kind {
77    File,
78    Memory,
79}
80
81impl Location {
82    pub(crate) const fn kind(&self) -> Kind {
83        match self {
84            Self::File(_) => Kind::File,
85            Self::Memory => Kind::Memory,
86        }
87    }
88}
89
90impl<'sql> Target<'sql> {
91    pub(crate) fn new(
92        _: runtime::Runtime,
93        location: Location,
94        options: options::OpenOptions,
95    ) -> Self {
96        Self {
97            location,
98            options,
99            commands: sys::Commands::EMPTY,
100        }
101    }
102
103    /// Runs parameterless, columnless commands before catalog preparation.
104    /// Each runs in autocommit mode; later failure preserves earlier changes.
105    /// Attaching validates only SQL text and does not open the database.
106    pub fn commands(mut self, sql: &'sql [&'sql str]) -> Result<Self, crate::Error> {
107        self.commands = sys::Commands::try_new(sql)?;
108        Ok(self)
109    }
110
111    pub(crate) const fn sql_bytes(&self) -> usize {
112        self.commands.max_bytes()
113    }
114
115    pub(crate) fn into_parts(self) -> (Location, options::OpenOptions, sys::Commands<'sql>) {
116        (self.location, self.options, self.commands)
117    }
118}