Convert character db methods to compile-checked queries

This commit is contained in:
projectmoon 2020-12-30 15:46:44 +00:00
parent 5ea07b8953
commit 53501c3747
2 changed files with 19 additions and 9 deletions

2
.env
View File

@ -1 +1 @@
DATABASE_URL="./tenebrous.sqlite" DATABASE_URL="sqlite://./tenebrous.sqlite"

View File

@ -69,22 +69,32 @@ impl Dao for SqlitePool {
} }
async fn load_character_list(&self, for_user_id: i32) -> Result<Vec<StrippedCharacter>, Error> { async fn load_character_list(&self, for_user_id: i32) -> Result<Vec<StrippedCharacter>, Error> {
sqlx::query_as::<_, StrippedCharacter>( sqlx::query_as!(
"SELECT id, user_id, viewable, character_name, data_type, data_version StrippedCharacter,
FROM characters WHERE user_id = ?", r#"SELECT id as "id: _",
user_id as "user_id: _",
data_type as "data_type: _",
data_version as "data_version: _",
viewable, character_name
FROM characters WHERE user_id = ?"#,
for_user_id
) )
.bind(for_user_id)
.fetch_all(self) .fetch_all(self)
.await .await
.map_err(|e| e.into()) .map_err(|e| e.into())
} }
async fn load_character(&self, character_id: i32) -> Result<Option<Character>, Error> { async fn load_character(&self, character_id: i32) -> Result<Option<Character>, Error> {
sqlx::query_as::<_, Character>( sqlx::query_as!(
"SELECT id, user_id, viewable, character_name, data_type, data_version, data Character,
FROM characters WHERE id = ?", r#"SELECT id as "id: _",
user_id as "user_id: _",
viewable, character_name, data,
data_type as "data_type: _",
data_version as "data_version: _"
FROM characters WHERE id = ?"#,
character_id
) )
.bind(character_id)
.fetch_optional(self) .fetch_optional(self)
.await .await
.map_err(|e| e.into()) .map_err(|e| e.into())