use crate::api::{Analysis, Answers, Describe, Response};
use crate::dialect::Dialect;
use crate::errors::Errors;
use crate::host::{Host, Lazy};
pub trait Database {
const DIALECT: Dialect;
const FINGERPRINT: &'static str;
type Describe: Describe;
fn analyze(sql: &str, host: &Host<'_, Self::Describe>) -> Lazy<Analysis>;
fn errors(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<Option<Errors>> {
Some(None)
}
fn read_only(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<bool> {
Some(false)
}
fn capabilities(_analysis: &Analysis, _host: &Host<'_, Self::Describe>) -> Lazy<Option<crate::types::Capabilities>> {
Some(None)
}
fn round(sql: &str, answers: &Answers<Self::Describe>) -> Response {
let host = Host::new(answers);
match Self::analyze(sql, &host) {
Some(analysis) => with_facts::<Self>(analysis, &host),
None => Response::Need { needs: host.needs() },
}
}
fn unbundle(_answers: &mut Answers<Self::Describe>) {}
}
pub fn with_facts<D: Database + ?Sized>(mut analysis: Analysis, host: &Host<'_, D::Describe>) -> Response {
let errors = if analysis.diagnostics.is_empty() { D::errors(&analysis, host) } else { Some(None) };
match errors {
Some(errors) => {
analysis.errors = errors;
match (D::read_only(&analysis, host), D::capabilities(&analysis, host)) {
(Some(read_only), Some(capabilities)) => {
analysis.read_only = read_only;
analysis.capabilities = capabilities;
Response::Analysis { analysis }
}
_ => Response::Need { needs: host.needs() },
}
}
None => Response::Need { needs: host.needs() },
}
}