changed to how the guild query works so that errors are more useful and it doesnt do dumb shit

This commit is contained in:
jude 2020-10-27 17:35:01 +00:00
parent 75653a3e88
commit db56118bfb
3 changed files with 34 additions and 27 deletions

2
Cargo.lock generated
View File

@ -1175,7 +1175,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.1.4" version = "1.1.5"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"async-trait", "async-trait",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.1.4" version = "1.1.5"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -41,13 +41,10 @@ SELECT prefix FROM guilds WHERE guild = ?
} }
} }
pub async fn from_guild( pub async fn from_guild(guild: Guild, pool: &MySqlPool) -> Result<Self, sqlx::Error> {
guild: Guild,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let guild_id = guild.id.as_u64().to_owned(); let guild_id = guild.id.as_u64().to_owned();
if let Ok(g) = sqlx::query_as!( match sqlx::query_as!(
Self, Self,
" "
SELECT id, name, prefix FROM guilds WHERE guild = ? SELECT id, name, prefix FROM guilds WHERE guild = ?
@ -57,30 +54,40 @@ SELECT id, name, prefix FROM guilds WHERE guild = ?
.fetch_one(pool) .fetch_one(pool)
.await .await
{ {
g.name = guild.name; Ok(mut g) => {
g.name = Some(guild.name);
Ok(g) Ok(g)
} else { }
sqlx::query!(
" Err(sqlx::Error::RowNotFound) => {
sqlx::query!(
"
INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?) INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?)
", ",
guild_id, guild_id,
guild.name, guild.name,
*DEFAULT_PREFIX *DEFAULT_PREFIX
) )
.execute(&pool.clone()) .execute(&pool.clone())
.await?; .await?;
Ok(sqlx::query_as!( Ok(sqlx::query_as!(
Self, Self,
" "
SELECT id, name, prefix FROM guilds WHERE guild = ? SELECT id, name, prefix FROM guilds WHERE guild = ?
", ",
guild_id guild_id
) )
.fetch_one(pool) .fetch_one(pool)
.await?) .await?)
}
Err(e) => {
error!("Unexpected error in guild query: {:?}", e);
Err(e)
}
} }
} }