forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower.rs
70 lines (61 loc) · 1.98 KB
/
tower.rs
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 std::env;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use tower::Service;
struct SessionService {
session: Arc<scylla::Session>,
}
// A trivial service implementation for sending parameterless simple string requests to Scylla.
impl Service<scylla::query::Query> for SessionService {
type Response = scylla::QueryResult;
type Error = scylla::transport::errors::QueryError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: scylla::query::Query) -> Self::Future {
let session = self.session.clone();
Box::pin(async move { session.query(req, &[]).await })
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} ...", uri);
let mut session: SessionService = SessionService {
session: Arc::new(
scylla::SessionBuilder::new()
.known_node(uri)
.build()
.await?,
),
};
let resp = session
.call("SELECT keyspace_name, table_name FROM system_schema.tables;".into())
.await?;
let print_text = |t: &Option<scylla::frame::response::result::CqlValue>| {
t.as_ref()
.unwrap_or(&scylla::frame::response::result::CqlValue::Text(
"<null>".to_string(),
))
.as_text()
.unwrap_or(&"<null>".to_string())
.clone()
};
println!(
"Tables:\n{}",
resp.rows()?
.into_iter()
.map(|r| format!(
"\t{}.{}",
print_text(&r.columns[0]),
print_text(&r.columns[1])
))
.collect::<Vec<String>>()
.join("\n")
);
Ok(())
}