From 03e5578dcb23b702016dc7f03db3870d49d2184d Mon Sep 17 00:00:00 2001 From: jude Date: Mon, 12 Oct 2020 23:17:47 +0100 Subject: [PATCH] made strings table name configurable --- Cargo.toml | 2 +- src/consts.rs | 2 ++ src/models.rs | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 922af84..6484622 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["jellywx "] edition = "2018" [dependencies] -serenity = { version = "0.9.0-rc.2", features = ["collector"] } +serenity = { version = "0.9.0-rc.2", features = ["collector", "rustls_backend"] } dotenv = "0.15" tokio = { version = "0.2.19", features = ["process"] } reqwest = "0.10.6" diff --git a/src/consts.rs b/src/consts.rs index 2591c76..927023f 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -45,4 +45,6 @@ lazy_static! { env::var("LOCAL_LANGUAGE").unwrap_or_else(|_| "EN".to_string()); pub static ref PYTHON_LOCATION: String = env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string()); + pub static ref STRINGS_TABLE: String = + env::var("STRINGS_TABLE").unwrap_or_else(|_| "strings".to_string()); } diff --git a/src/models.rs b/src/models.rs index 488bd4a..ac55f8b 100644 --- a/src/models.rs +++ b/src/models.rs @@ -5,12 +5,12 @@ use serenity::{ use std::env; -use sqlx::MySqlPool; +use sqlx::{Cursor, MySqlPool, Row}; use chrono::NaiveDateTime; use chrono_tz::Tz; -use crate::consts::{LOCAL_LANGUAGE, PREFIX}; +use crate::consts::{LOCAL_LANGUAGE, PREFIX, STRINGS_TABLE}; pub struct GuildData { pub id: u32, @@ -249,16 +249,27 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ? } pub async fn response(&self, pool: &MySqlPool, name: &str) -> String { - let row = sqlx::query!( + let query_str = &format!( " -SELECT value FROM strings WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ? - ", self.language, *LOCAL_LANGUAGE, name, *LOCAL_LANGUAGE) - .fetch_one(pool) - .await - .unwrap_or_else(|_| panic!("No string with that name: {}", name)); +SELECT value FROM {} WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ? + ", + *STRINGS_TABLE + ); - row.value - .unwrap_or_else(|| panic!("Null string with that name: {}", name)) + let mut query = sqlx::query(&query_str) + .bind(&self.language) + .bind(&*LOCAL_LANGUAGE) + .bind(name) + .bind(&*LOCAL_LANGUAGE) + .fetch(pool); + + let row = query + .next() + .await + .unwrap_or_else(|e| panic!("Database error: {:?}", e)) + .unwrap_or_else(|| panic!("No string with that name: {}", name)); + + row.get::("value").clone() } pub fn timezone(&self) -> Tz {