add better example
This commit is contained in:
parent
864317cc20
commit
bae89ab882
@ -8,3 +8,6 @@ edition = "2021"
|
||||
[dependencies]
|
||||
inline-postgres-impl = { path = "../inline-postgres-impl" }
|
||||
inline-postgres-macros = { path = "../inline-postgres-macros" }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
50
inline-postgres/examples/atla.rs
Normal file
50
inline-postgres/examples/atla.rs
Normal 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(())
|
||||
}
|
||||
@ -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(())
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user