mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
This fixes `cargo check --workspace` and rust-analyzer. Also see <https://github.com/launchbadge/sqlx/discussions/1956>.
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]
|
|
async fn main() -> anyhow::Result<()> {
|
|
let pool = PgPool::connect(&dotenv::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(())
|
|
}
|