1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use {
crate::{
ast::Statement,
executor::{execute, Payload},
parse_sql::parse,
plan::plan,
result::Result,
store::{GStore, GStoreMut},
translate::translate,
},
futures::executor::block_on,
std::{fmt::Debug, marker::PhantomData},
};
pub struct Glue<T: Debug, U: GStore<T> + GStoreMut<T>> {
_marker: PhantomData<T>,
pub storage: Option<U>,
}
impl<T: Debug, U: GStore<T> + GStoreMut<T>> Glue<T, U> {
pub fn new(storage: U) -> Self {
let storage = Some(storage);
Self {
_marker: PhantomData,
storage,
}
}
pub async fn plan(&self, sql: &str) -> Result<Statement> {
let parsed = parse(sql)?;
let statement = translate(&parsed[0])?;
let storage = self.storage.as_ref().unwrap();
plan(storage, statement).await
}
pub fn execute_stmt(&mut self, statement: Statement) -> Result<Payload> {
block_on(self.execute_stmt_async(statement))
}
pub fn execute(&mut self, sql: &str) -> Result<Payload> {
let statement = block_on(self.plan(sql))?;
self.execute_stmt(statement)
}
pub async fn execute_stmt_async(&mut self, statement: Statement) -> Result<Payload> {
let storage = self.storage.take().unwrap();
match execute(storage, &statement).await {
Ok((storage, payload)) => {
self.storage = Some(storage);
Ok(payload)
}
Err((storage, error)) => {
self.storage = Some(storage);
Err(error)
}
}
}
pub async fn execute_async(&mut self, sql: &str) -> Result<Payload> {
let statement = self.plan(sql).await?;
self.execute_stmt_async(statement).await
}
}