reylo_engine_sqlite/
api.rs1use 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
11pub type Answer = reylo_engine_core::api::Answer<NoDescribe>;
13pub type Answers = reylo_engine_core::api::Answers<NoDescribe>;
14
15pub 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 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 fn errors(analysis: &Analysis, host: &Host) -> Lazy<Option<reylo_engine_core::errors::Errors>> {
38 crate::errors::errors(analysis, host)
39 }
40
41 fn read_only(analysis: &Analysis, _host: &Host) -> Lazy<bool> {
43 Some(analysis.kind == Some(reylo_engine_core::types::StatementKind::Select))
44 }
45
46 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 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}