MCPcopy Create free account
hub / github.com/bedroombuilds/python2rust / main

Function main

21_postgresql/rust/pg_async/src/main.rs:38–80  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36
37#[tokio::main]
38async fn main() -> Result<(), Error> {
39 let (mut client, connection) =
40 tokio_postgres::connect("host=localhost user=htd", NoTls).await?;
41
42 // connection object performs communication with DB, spawned to run on its own
43 tokio::spawn(async move {
44 if let Err(e) = connection.await {
45 eprintln!("connection error: {}", e);
46 }
47 });
48
49 let rows = client.query(MOST_RECOVERED, &[]).await?;
50
51 let value: chrono::NaiveDate = rows[0].get(0);
52 println!("{:?}", value);
53 println!("{:?}", row_to_str_vec(&rows[0]).await);
54 println!("{:?}", row_to_str_hashmap(&rows[0]).await);
55
56 let transaction = client.transaction().await?;
57 transaction.query("DELETE FROM month_cases", &[]).await?;
58 let ins_mv = transaction
59 .prepare(
60 "INSERT INTO month_cases
61 (mon, new_cases, recovered)
62 VALUES ($1, $2, $3)",
63 )
64 .await?;
65
66 for row in transaction.query(GROUP_BY_MONTH, &[]).await? {
67 println!("{:?}", row_to_str_vec(&row).await);
68 let date: chrono::NaiveDate = row.get(0);
69 let new_cases: i64 = row.get(1);
70 let recovered: i64 = row.get(2);
71 transaction
72 .execute(
73 &ins_mv,
74 &[&date.to_string(), &(new_cases as i32), &(recovered as i32)],
75 )
76 .await?;
77 }
78 transaction.commit().await?;
79 Ok(())
80}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected