use tokio;
use tokio_postgres;
use bb8::{Pool, RunError};
use bb8_postgres::PostgresConnectionManager;
use std::str::FromStr;
#[tokio::main]
async fn main() {
let config =
tokio_postgres::config::Config::from_str("postgresql://postgres:docker@localhost:5432")
.unwrap();
let pg_mgr = PostgresConnectionManager::new(config, tokio_postgres::NoTls);
let pool = match Pool::builder().build(pg_mgr).await {
Ok(pool) => pool,
Err(e) => panic!("builder error: {:?}", e),
};
match select(&pool).await {
Ok(result) => println!("result: {}", result),
Err(error) => eprintln!("Error occurred: {}", error),
}
}
async fn select(
pool: &Pool<PostgresConnectionManager<tokio_postgres::NoTls>>,
) -> Result<i32, RunError<tokio_postgres::Error>> {
let connection = pool.get().await?;
let stmt = connection.prepare("SELECT 1").await?;
let row = connection.query_one(&stmt, &[]).await?;
Ok(row.get::<usize, i32>(0))
}