mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-10 19:24:40 +00:00

* WIP preparing 0.7.0 release * fix: re-enable examples * fix doctests in `sqlx-core` * cherry-pick CHANGELOG entry for 0.6.3 * add actions workflow for examples * fix(cli): close connection after running migrations * fix examples * fix(sqlite): fix parsing of URLs via `Any` * fix(example): don't let Postgres `listen` example run forever * fix Postgres `transaction` example
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use sqlx::{query_file, query_file_as, query_file_unchecked, FromRow, PgPool};
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
#[derive(FromRow)]
|
|
struct PostWithAuthorQuery {
|
|
pub post_id: i64,
|
|
pub title: String,
|
|
pub body: String,
|
|
pub author_id: i64,
|
|
pub author_username: String,
|
|
}
|
|
|
|
impl Display for PostWithAuthorQuery {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
write!(
|
|
f,
|
|
r#"
|
|
post_id: {},
|
|
title: {},
|
|
body: {},
|
|
author_id: {},
|
|
author_username: {}
|
|
"#,
|
|
self.post_id, self.title, self.body, self.author_id, self.author_username
|
|
)
|
|
}
|
|
}
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
async fn main() -> anyhow::Result<()> {
|
|
let pool = PgPool::connect(&dotenvy::var("DATABASE_URL")?).await?;
|
|
|
|
// we can use a tranditional wrapper around the `query!()` macro using files
|
|
query_file!("queries/insert_seed_data.sql")
|
|
.execute(&pool)
|
|
.await?;
|
|
|
|
// we can also use `query_file_as!()` similarly to `query_as!()` to map our database models
|
|
let posts_with_authors = query_file_as!(PostWithAuthorQuery, "queries/list_all_posts.sql")
|
|
.fetch_all(&pool)
|
|
.await?;
|
|
|
|
for post_with_author in posts_with_authors {
|
|
println!("{}", post_with_author);
|
|
}
|
|
|
|
Ok(())
|
|
}
|