Convert load user queries to compile-checked

This commit is contained in:
projectmoon 2020-12-30 15:55:35 +00:00
parent fb1c21d710
commit e148b45edd
1 changed files with 14 additions and 8 deletions

View File

@ -35,17 +35,23 @@ pub(crate) trait Dao {
#[rocket::async_trait] #[rocket::async_trait]
impl Dao for SqlitePool { impl Dao for SqlitePool {
async fn load_user_by_id(&self, user_id: i32) -> sqlx::Result<Option<User>> { async fn load_user_by_id(&self, user_id: i32) -> sqlx::Result<Option<User>> {
sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?") sqlx::query_as!(
.bind(user_id) User,
.fetch_optional(self) r#"SELECT id as "id: _", username, password FROM users WHERE id = ?"#,
.await user_id
)
.fetch_optional(self)
.await
} }
async fn load_user(&self, for_username: &str) -> sqlx::Result<Option<User>> { async fn load_user(&self, for_username: &str) -> sqlx::Result<Option<User>> {
sqlx::query_as::<_, User>("SELECT * FROM users WHERE username = ?") sqlx::query_as!(
.bind(for_username) User,
.fetch_optional(self) r#"SELECT id as "id: _", username, password FROM users WHERE username = ?"#,
.await for_username
)
.fetch_optional(self)
.await
} }
async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result<User> { async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result<User> {