forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower.rs
74 lines (65 loc) · 2.14 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
71
72
73
74
use scylla::frame::response::result::Row;
use scylla::transport::session::Session;
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<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_unpaged(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 rows_result = session
.call("SELECT keyspace_name, table_name FROM system_schema.tables;".into())
.await?
.into_rows_result()?;
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{}",
rows_result
.rows::<Row>()?
.map(|r| r.map(|r| format!(
"\t{}.{}",
print_text(&r.columns[0]),
print_text(&r.columns[1])
)))
.collect::<Result<Vec<String>, _>>()
.unwrap()
.join("\n")
);
Ok(())
}