Skip to main content

reylo_engine_core/
database.rs

1//! The boundary between core and a database: what a database's crate implements so that
2//! [`crate::api::call`] can serve its statements. The trait is used through static dispatch only:
3//! each artifact instantiates `call::<D>` for exactly one `D`, and a native host that links several
4//! databases picks one per request ([`crate::api::dialect_of`]).
5
6use crate::api::{Analysis, Answers, Describe, Response};
7use crate::dialect::Dialect;
8use crate::errors::Errors;
9use crate::host::{Host, Lazy};
10
11pub trait Database {
12    /// The name a request carries, and the placeholder rules the lexer applies.
13    const DIALECT: Dialect;
14    /// The sources this database's answers come from: core's and its own ([`crate::fingerprint`]).
15    const FINGERPRINT: &'static str;
16    /// What the host answers a `describe` need with.
17    type Describe: Describe;
18
19    /// The analysis of the statement as its author wrote it, from what the host has answered so
20    /// far, or `None` with the missing needs recorded on `host`.
21    fn analyze(sql: &str, host: &Host<'_, Self::Describe>) -> Lazy<Analysis>;
22
23    /// The names each error the statement may raise can carry ([`crate::errors`]): `Some(None)`
24    /// where the statement may run code the database does not define, or the database narrows none.
25    fn errors(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<Option<Errors>> {
26        Some(None)
27    }
28
29    /// Whether the engine finds nothing in the statement that could write, read once the rest of the analysis is
30    /// settled: by default it is not.
31    fn read_only(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<bool> {
32        Some(false)
33    }
34
35    /// What running the statement may do, an upper bound ([`crate::types::Capabilities`]), read
36    /// once the rest of the analysis is settled: by default nothing is said.
37    fn capabilities(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<Option<crate::types::Capabilities>> {
38        Some(None)
39    }
40
41    /// One request's round: the analysis, and then what it reads once the rest is settled. Postgres
42    /// overrides it to settle what a server-side extension offers first.
43    fn round(sql: &str, answers: &Answers<Self::Describe>) -> Response {
44        let host = Host::new(answers);
45        match Self::analyze(sql, &host) {
46            Some(analysis) => with_facts::<Self>(analysis, &host),
47            None => Response::Need { needs: host.needs() },
48        }
49    }
50
51    /// Answers that came back in a form of the database's own, put where the analysis looks for
52    /// them. By default nothing; Postgres unpacks a `reylo.answer` bundle.
53    fn unbundle(_answers: &mut Answers<Self::Describe>) {}
54}
55
56/// The analysis with the errors it may raise, whether it writes nothing and what it may do, read in
57/// one more round ([`crate::errors`]).
58pub fn with_facts<D: Database + ?Sized>(mut analysis: Analysis, host: &Host<'_, D::Describe>) -> Response {
59    let errors = if analysis.diagnostics.is_empty() { D::errors(&analysis, host) } else { Some(None) };
60    match errors {
61        Some(errors) => {
62            analysis.errors = errors;
63            match (D::read_only(&analysis, host), D::capabilities(&analysis, host)) {
64                (Some(read_only), Some(capabilities)) => {
65                    analysis.read_only = read_only;
66                    analysis.capabilities = capabilities;
67                    Response::Analysis { analysis }
68                }
69                _ => Response::Need { needs: host.needs() },
70            }
71        }
72        None => Response::Need { needs: host.needs() },
73    }
74}