generics for Database pool

This commit is contained in:
2022-01-30 15:55:53 +00:00
parent c364343fe9
commit eb5ea3167d
8 changed files with 49 additions and 69 deletions
+9 -11
View File
@@ -1,10 +1,10 @@
use std::sync::Arc;
use poise::serenity::{async_trait, model::id::GuildId};
use sqlx::mysql::MySqlPool;
use sqlx::Executor;
use tokio::sync::RwLock;
use crate::{Context, Data};
use crate::{Context, Data, Database};
#[derive(Clone)]
pub struct GuildData {
@@ -44,9 +44,7 @@ impl CtxGuildData for Data {
let x = if let Some(guild_data) = self.guild_data_cache.get(&guild_id) {
Ok(guild_data.clone())
} else {
let pool = self.database.clone();
match GuildData::from_id(guild_id, pool).await {
match GuildData::from_id(guild_id, &self.database).await {
Ok(d) => {
let lock = Arc::new(RwLock::new(d));
@@ -66,7 +64,7 @@ impl CtxGuildData for Data {
impl GuildData {
pub async fn from_id<G: Into<GuildId>>(
guild_id: G,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database> + Copy,
) -> Result<GuildData, sqlx::Error> {
let guild_id = guild_id.into();
@@ -79,7 +77,7 @@ SELECT id, prefix, volume, allow_greets, allowed_role
",
guild_id.as_u64()
)
.fetch_one(&db_pool)
.fetch_one(db_pool)
.await;
match guild_data {
@@ -93,7 +91,7 @@ SELECT id, prefix, volume, allow_greets, allowed_role
async fn create_from_guild<G: Into<GuildId>>(
guild_id: G,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<GuildData, sqlx::Error> {
let guild_id = guild_id.into();
@@ -104,7 +102,7 @@ INSERT INTO servers (id)
",
guild_id.as_u64()
)
.execute(&db_pool)
.execute(db_pool)
.await?;
Ok(GuildData {
@@ -118,7 +116,7 @@ INSERT INTO servers (id)
pub async fn commit(
&self,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!(
"
@@ -137,7 +135,7 @@ WHERE
self.allowed_role,
self.id
)
.execute(&db_pool)
.execute(db_pool)
.await?;
Ok(())
+1 -3
View File
@@ -21,8 +21,6 @@ impl JoinSoundCtx for Data {
join_sound_id.value().clone()
} else {
let join_sound_id = {
let pool = self.database.clone();
let join_id_res = sqlx::query!(
"
SELECT join_sound_id
@@ -31,7 +29,7 @@ SELECT join_sound_id
",
user_id.as_u64()
)
.fetch_one(&pool)
.fetch_one(&self.database)
.await;
if let Ok(row) = join_id_res {
+16 -16
View File
@@ -2,10 +2,10 @@ use std::{env, path::Path};
use poise::serenity::async_trait;
use songbird::input::restartable::Restartable;
use sqlx::{mysql::MySqlPool, Error};
use sqlx::{Error, Executor};
use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use crate::{consts::UPLOAD_MAX_SIZE, error::ErrorTypes, Data};
use crate::{consts::UPLOAD_MAX_SIZE, error::ErrorTypes, Data, Database};
#[derive(Clone)]
pub struct Sound {
@@ -208,7 +208,7 @@ SELECT name, id, public, server_id, uploader_id
}
impl Sound {
async fn src(&self, db_pool: MySqlPool) -> Vec<u8> {
async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec<u8> {
struct Src {
src: Vec<u8>,
}
@@ -223,7 +223,7 @@ SELECT src
",
self.id
)
.fetch_one(&db_pool)
.fetch_one(db_pool)
.await
.unwrap();
@@ -232,7 +232,7 @@ SELECT src
pub async fn store_sound_source(
&self,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp"));
@@ -250,7 +250,7 @@ SELECT src
pub async fn playable(
&self,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<Restartable, Box<dyn std::error::Error + Send + Sync>> {
let path_name = self.store_sound_source(db_pool).await?;
@@ -261,7 +261,7 @@ SELECT src
pub async fn count_user_sounds<U: Into<u64>>(
user_id: U,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<u32, sqlx::error::Error> {
let user_id = user_id.into();
@@ -273,7 +273,7 @@ SELECT COUNT(1) as count
",
user_id
)
.fetch_one(&db_pool)
.fetch_one(db_pool)
.await?
.count;
@@ -283,7 +283,7 @@ SELECT COUNT(1) as count
pub async fn count_named_user_sounds<U: Into<u64>>(
user_id: U,
name: &String,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<u32, sqlx::error::Error> {
let user_id = user_id.into();
@@ -298,7 +298,7 @@ SELECT COUNT(1) as count
user_id,
name
)
.fetch_one(&db_pool)
.fetch_one(db_pool)
.await?
.count;
@@ -307,7 +307,7 @@ SELECT COUNT(1) as count
pub async fn commit(
&self,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!(
"
@@ -320,7 +320,7 @@ WHERE
self.public,
self.id
)
.execute(&db_pool)
.execute(db_pool)
.await?;
Ok(())
@@ -328,7 +328,7 @@ WHERE
pub async fn delete(
&self,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!(
"
@@ -338,7 +338,7 @@ DELETE
",
self.id
)
.execute(&db_pool)
.execute(db_pool)
.await?;
Ok(())
@@ -349,7 +349,7 @@ DELETE
src_url: &str,
server_id: G,
user_id: U,
db_pool: MySqlPool,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + Send>> {
let server_id = server_id.into();
let user_id = user_id.into();
@@ -396,7 +396,7 @@ INSERT INTO sounds (name, server_id, uploader_id, public, src)
user_id,
data
)
.execute(&db_pool)
.execute(db_pool)
.await
{
Ok(_) => Ok(()),