remove self mutable dependency as pool

This commit is contained in:
yggverse 2026-01-07 22:20:57 +02:00
parent 98ec671758
commit e7e3969e00

View file

@ -1,7 +1,7 @@
use mysql::{Error, PooledConn, prelude::Queryable}; use mysql::{Error, Pool, prelude::Queryable};
pub struct Mysql { pub struct Mysql {
connection: PooledConn, pool: Pool,
} }
impl Mysql { impl Mysql {
@ -13,19 +13,14 @@ impl Mysql {
database: &str, database: &str,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
Ok(Self { Ok(Self {
connection: mysql::Pool::new( pool: mysql::Pool::new(
format!("mysql://{user}:{password}@{host}:{port}/{database}").as_str(), format!("mysql://{user}:{password}@{host}:{port}/{database}").as_str(),
)? )?,
.get_conn()?,
}) })
} }
pub fn channels_by_url( pub fn channels_by_url(&self, url: &str, limit: Option<usize>) -> Result<Vec<Channel>, Error> {
&mut self, self.pool.get_conn()?.exec_map(
url: &str,
limit: Option<usize>,
) -> Result<Vec<Channel>, Error> {
self.connection.exec_map(
format!( format!(
"SELECT `channel_id`, `url` FROM `channel` WHERE `url` = ? LIMIT {}", "SELECT `channel_id`, `url` FROM `channel` WHERE `url` = ? LIMIT {}",
limit.unwrap_or(DEFAULT_LIMIT) limit.unwrap_or(DEFAULT_LIMIT)
@ -35,15 +30,15 @@ impl Mysql {
) )
} }
pub fn insert_channel(&mut self, url: &str) -> Result<u64, Error> { pub fn insert_channel(&self, url: &str) -> Result<u64, Error> {
self.connection let mut c = self.pool.get_conn()?;
.exec_drop("INSERT INTO `channel` SET `url` = ?", (url,))?; c.exec_drop("INSERT INTO `channel` SET `url` = ?", (url,))?;
Ok(c.last_insert_id())
Ok(self.connection.last_insert_id())
} }
pub fn channel_item(&mut self, channel_item_id: u64) -> Result<Option<ChannelItem>, Error> { pub fn channel_item(&self, channel_item_id: u64) -> Result<Option<ChannelItem>, Error> {
self.connection self.pool
.get_conn()?
.exec_first( .exec_first(
"SELECT `channel_item_id`, "SELECT `channel_item_id`,
`channel_id`, `channel_id`,
@ -72,12 +67,12 @@ impl Mysql {
} }
pub fn channel_items_by_channel_id_guid( pub fn channel_items_by_channel_id_guid(
&mut self, &self,
channel_id: u64, channel_id: u64,
guid: &str, guid: &str,
limit: Option<usize>, limit: Option<usize>,
) -> Result<Vec<ChannelItem>, Error> { ) -> Result<Vec<ChannelItem>, Error> {
self.connection.exec_map( self.pool.get_conn()?.exec_map(
format!( format!(
"SELECT `channel_item_id`, "SELECT `channel_item_id`,
`channel_id`, `channel_id`,
@ -102,7 +97,7 @@ impl Mysql {
} }
pub fn insert_channel_item( pub fn insert_channel_item(
&mut self, &self,
channel_id: u64, channel_id: u64,
pub_date: i64, pub_date: i64,
guid: &str, guid: &str,
@ -110,7 +105,8 @@ impl Mysql {
title: Option<&str>, title: Option<&str>,
description: Option<&str>, description: Option<&str>,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
self.connection.exec_drop( let mut c = self.pool.get_conn()?;
c.exec_drop(
"INSERT INTO `channel_item` SET `channel_id` = ?, "INSERT INTO `channel_item` SET `channel_id` = ?,
`pub_date` = ?, `pub_date` = ?,
`guid` = ?, `guid` = ?,
@ -119,11 +115,11 @@ impl Mysql {
`description` = ?", `description` = ?",
(channel_id, pub_date, guid, link, title, description), (channel_id, pub_date, guid, link, title, description),
)?; )?;
Ok(self.connection.last_insert_id()) Ok(c.last_insert_id())
} }
pub fn contents(&mut self, limit: Option<usize>) -> Result<Vec<Content>, Error> { pub fn contents(&self, limit: Option<usize>) -> Result<Vec<Content>, Error> {
self.connection.query_map( self.pool.get_conn()?.query_map(
format!( format!(
"SELECT `content_id`, "SELECT `content_id`,
`channel_item_id`, `channel_item_id`,
@ -143,12 +139,12 @@ impl Mysql {
} }
pub fn contents_by_channel_item_id_source_id( pub fn contents_by_channel_item_id_source_id(
&mut self, &self,
channel_item_id: u64, channel_item_id: u64,
source_id: Option<u64>, source_id: Option<u64>,
limit: Option<usize>, limit: Option<usize>,
) -> Result<Vec<Content>, Error> { ) -> Result<Vec<Content>, Error> {
self.connection.exec_map( self.pool.get_conn()?.exec_map(
format!( format!(
"SELECT `content_id`, "SELECT `content_id`,
`channel_item_id`, `channel_item_id`,
@ -163,17 +159,18 @@ impl Mysql {
} }
pub fn insert_content( pub fn insert_content(
&mut self, &self,
channel_item_id: u64, channel_item_id: u64,
source_id: Option<u64>, source_id: Option<u64>,
title: String, title: String,
description: String, description: String,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
self.connection.exec_drop( let mut c = self.pool.get_conn()?;
c.exec_drop(
"INSERT INTO `content` SET `channel_item_id` = ?, `source_id` = ?, `title` = ?, `description` = ?", "INSERT INTO `content` SET `channel_item_id` = ?, `source_id` = ?, `title` = ?, `description` = ?",
(channel_item_id, source_id, title, description ), (channel_item_id, source_id, title, description ),
)?; )?;
Ok(self.connection.last_insert_id()) Ok(c.last_insert_id())
} }
} }