Skip to main content

reylo_engine_sqlite/
api.rs

1//! The SQLite side of the request protocol ([`reylo_engine_core::api`]).
2
3use crate::host::Host;
4use reylo_engine_core::api::NoDescribe;
5use reylo_engine_core::database::Database;
6use reylo_engine_core::dialect::Dialect;
7use reylo_engine_core::host::Lazy;
8
9pub use reylo_engine_core::api::{Analysis, Column, DbError, Diagnostic, Need, Param, Response, Scope};
10
11/// What the host learned for one need: never a describe.
12pub type Answer = reylo_engine_core::api::Answer<NoDescribe>;
13pub type Answers = reylo_engine_core::api::Answers<NoDescribe>;
14
15/// SQLite, for [`reylo_engine_core::api::call`].
16pub struct Sqlite;
17
18impl Database for Sqlite {
19    const DIALECT: Dialect = Dialect::Sqlite;
20    const FINGERPRINT: &'static str = crate::FINGERPRINT;
21    type Describe = NoDescribe;
22
23    /// The replayed analysis: the host answers the schema and function queries
24    /// ([`crate::sqlite::analyze`]), the statement's structured parameters expanded first.
25    fn analyze(sql: &str, host: &Host) -> Lazy<Analysis> {
26        host.on_engine_stack(|host| {
27            let inner = |sql: &str, marked: &[String]| match crate::analysis::lexed(sql, Dialect::Sqlite) {
28                Ok(lexed) => crate::sqlite::analyze(sql, &lexed, host, marked),
29                Err(bare) => Some(bare),
30            };
31            crate::structured::analyze(sql, Dialect::Sqlite, host, &inner, &|diagnostics| crate::analysis::bare_of(sql, Dialect::Sqlite, diagnostics))
32        })
33    }
34
35    /// The unique keys, NOT NULL columns and STRICT tables the statement's constraint errors may
36    /// name, where it fires no trigger.
37    fn errors(analysis: &Analysis, host: &Host) -> Lazy<Option<reylo_engine_core::errors::Errors>> {
38        crate::errors::errors(analysis, host)
39    }
40
41    /// A SELECT writes nothing on SQLite, which has no writing function and no locking clause.
42    fn read_only(analysis: &Analysis, _host: &Host) -> Lazy<bool> {
43        Some(analysis.kind == Some(reylo_engine_core::types::StatementKind::Select))
44    }
45
46    /// What running the statement may do: what the statement says, and what the schema adds to its writes.
47    fn capabilities(analysis: &Analysis, host: &Host) -> Lazy<Option<reylo_engine_core::types::Capabilities>> {
48        crate::capabilities::refine(analysis, host)
49    }
50}
51
52#[cfg(test)]
53mod protocol {
54    use super::*;
55    use reylo_engine_core::api::call;
56    use serde_json::Value;
57
58    fn ask(request: &str) -> Value {
59        serde_json::from_str(&call::<Sqlite>(request)).unwrap()
60    }
61
62    #[test]
63    fn the_artifact_names_its_database_and_refuses_another_s_statement() {
64        let ping = ask(r#"{"op":"ping"}"#);
65        assert_eq!((ping["status"].as_str(), ping["database"].as_str(), ping["fingerprint"].as_str()), (Some("ok"), Some("sqlite"), Some(crate::FINGERPRINT)));
66        // Lexing and the project file are core's, for any dialect.
67        assert_eq!(ask(r#"{"op":"lex","sql":"select :a","dialect":"postgres"}"#)["lexed"]["params"], serde_json::json!(["a"]));
68        assert_eq!(ask(r#"{"op":"config","text":"url: postgres://localhost/app"}"#)["status"], "config");
69        let refused = ask(r#"{"op":"analyze","sql":"select 1","dialect":"postgres","answers":{"describe:select 1":{"describe":{"params":[],"columns":[]}}}}"#);
70        assert_eq!((refused["status"].as_str(), refused["message"].as_str()), (Some("error"), Some("this engine reads sqlite statements, not postgres ones")));
71    }
72}