add better example

This commit is contained in:
Jonas Maier 2023-04-02 21:45:26 +02:00
parent 864317cc20
commit bae89ab882
3 changed files with 54 additions and 19 deletions

View File

@ -7,4 +7,7 @@ edition = "2021"
[dependencies]
inline-postgres-impl = { path = "../inline-postgres-impl" }
inline-postgres-macros = { path = "../inline-postgres-macros" }
inline-postgres-macros = { path = "../inline-postgres-macros" }
[dev-dependencies]
tokio = { version = "1", features = ["full"] }

View File

@ -0,0 +1,50 @@
use inline_postgres as pg;
use inline_postgres::prelude::*;
#[tokio::main]
async fn main() -> Result<(), pg::Error> {
let (client, connection) = pg::connect("host=localhost user=postgres", pg::NoTls).await?;
// wait for the connection
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
client.exec(stmt!{
CREATE TABLE KID (
NAME VARCHAR(10),
AGE INT4
)
}).await?;
client.exec(stmt!{
INSERT INTO KID (NAME, AGE)
VALUES
("Aang", 12),
("Sokka", 14),
("Toph", 12),
("Zuko", 16)
}).await?;
let actual_age = 112;
client.exec(stmt!{
UPDATE KID
SET AGE = {actual_age}
WHERE NAME = "Aang"
}).await?;
let ages = client.fetch(sql! {
SELECT SUM(AGE) as [sum: i64] FROM KID
}).await?;
println!("{}", ages[0].sum);
client.exec(stmt! {
DROP TABLE KID
}).await?;
Ok(())
}

View File

@ -1,18 +0,0 @@
use inline_postgres as pg;
use inline_postgres::prelude::*;
fn main() -> Result<(), pg::Error> {
let mut client = pg::Client::connect("host=localhost, user=postgres", pg::NoTls)?;
let x = 5;
let rows = client.fetch(sql! {
select 1 as [a:i32], generate_series(1, {x}) as [b: i32]
})?;
for row in rows {
println!("{row:?}");
}
Ok(())
}