From 769512f35774e7a60153efe5328f1e161f2e8cfd Mon Sep 17 00:00:00 2001 From: jeff Date: Wed, 30 Dec 2020 15:46:44 +0000 Subject: [PATCH] Convert character db methods to compile-checked queries --- .env | 2 +- src/db.rs | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 8269679..34b8b52 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL="./tenebrous.sqlite" +DATABASE_URL="sqlite://./tenebrous.sqlite" diff --git a/src/db.rs b/src/db.rs index b5365c0..ffd7536 100644 --- a/src/db.rs +++ b/src/db.rs @@ -69,22 +69,32 @@ impl Dao for SqlitePool { } async fn load_character_list(&self, for_user_id: i32) -> Result, Error> { - sqlx::query_as::<_, StrippedCharacter>( - "SELECT id, user_id, viewable, character_name, data_type, data_version - FROM characters WHERE user_id = ?", + sqlx::query_as!( + StrippedCharacter, + 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) .await .map_err(|e| e.into()) } async fn load_character(&self, character_id: i32) -> Result, Error> { - sqlx::query_as::<_, Character>( - "SELECT id, user_id, viewable, character_name, data_type, data_version, data - FROM characters WHERE id = ?", + sqlx::query_as!( + Character, + 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) .await .map_err(|e| e.into())