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
71
72
73
74
75
76
77
78
79
80
81
use crate::client::Config;
use anyhow::Result;
use async_trait::async_trait;
use crate::{BatchResult, ResultSet, Statement};
pub struct Client {
client: hrana_client::Client,
client_future: hrana_client::ConnFut,
stream: hrana_client::Stream,
}
impl Client {
pub async fn new(url: impl Into<String>, token: impl Into<String>) -> Result<Self> {
let token = token.into();
let url = url.into();
let (client, client_future) =
hrana_client::Client::connect(&url, if token.is_empty() { None } else { Some(token) })
.await?;
let stream = client.open_stream().await?;
Ok(Self {
client,
client_future,
stream,
})
}
pub async fn from_config(config: Config) -> Result<Self> {
Self::new(config.url, config.auth_token.unwrap_or_default()).await
}
pub async fn shutdown(self) -> Result<()> {
self.client.shutdown().await?;
self.client_future.await?;
Ok(())
}
}
#[async_trait(?Send)]
impl crate::DatabaseClient for Client {
async fn raw_batch(
&self,
stmts: impl IntoIterator<Item = impl Into<Statement>>,
) -> anyhow::Result<BatchResult> {
let mut batch = hrana_client::proto::Batch::new();
for stmt in stmts.into_iter() {
let stmt: Statement = stmt.into();
let mut hrana_stmt = hrana_client::proto::Stmt::new(stmt.q, true);
for param in stmt.params {
hrana_stmt.bind(param);
}
batch.step(None, hrana_stmt);
}
self.stream
.execute_batch(batch)
.await
.map_err(|e| anyhow::anyhow!("{}", e))
}
async fn execute(&self, stmt: impl Into<Statement>) -> Result<ResultSet> {
let stmt: Statement = stmt.into();
let mut hrana_stmt = hrana_client::proto::Stmt::new(stmt.q, true);
for param in stmt.params {
hrana_stmt.bind(param);
}
self.stream
.execute(hrana_stmt)
.await
.map_err(|e| anyhow::anyhow!("{}", e))
}
}