Annotation For src/sql.rs
Logged in as anonymous

Origin for each line in src/sql.rs from check-in 0340541002:

0340541002 2025-04-24    1: use std::borrow::Cow;
0340541002 2025-04-24    2: 
0340541002 2025-04-24    3: use anyhow::{
0340541002 2025-04-24    4: 	Result,
0340541002 2025-04-24    5: 	bail,
0340541002 2025-04-24    6: };
0340541002 2025-04-24    7: use chrono::{
0340541002 2025-04-24    8: 	DateTime,
0340541002 2025-04-24    9: 	FixedOffset,
0340541002 2025-04-24   10: 	Local,
0340541002 2025-04-24   11: };
0340541002 2025-04-24   12: use sqlx::{
0340541002 2025-04-24   13: 	Pool,
0340541002 2025-04-24   14: 	Postgres,
0340541002 2025-04-24   15: 	Row,
0340541002 2025-04-24   16: 	postgres::PgPoolOptions,
0340541002 2025-04-24   17: 	pool::PoolConnection,
0340541002 2025-04-24   18: };
0340541002 2025-04-24   19: 
0340541002 2025-04-24   20: #[derive(sqlx::FromRow, Debug)]
0340541002 2025-04-24   21: pub struct List {
0340541002 2025-04-24   22: 	pub source_id: i64,
0340541002 2025-04-24   23: 	pub channel: String,
0340541002 2025-04-24   24: 	pub enabled: bool,
0340541002 2025-04-24   25: 	pub url: String,
0340541002 2025-04-24   26: 	pub iv_hash: Option<String>,
0340541002 2025-04-24   27: 	pub url_re: Option<String>,
0340541002 2025-04-24   28: }
0340541002 2025-04-24   29: 
0340541002 2025-04-24   30: #[derive(sqlx::FromRow, Debug)]
0340541002 2025-04-24   31: pub struct Source {
0340541002 2025-04-24   32: 	pub channel_id: i64,
0340541002 2025-04-24   33: 	pub url: String,
0340541002 2025-04-24   34: 	pub iv_hash: Option<String>,
0340541002 2025-04-24   35: 	pub owner: i64,
0340541002 2025-04-24   36: 	pub url_re: Option<String>,
0340541002 2025-04-24   37: }
0340541002 2025-04-24   38: 
0340541002 2025-04-24   39: #[derive(sqlx::FromRow)]
0340541002 2025-04-24   40: pub struct Queue {
0340541002 2025-04-24   41: 	pub source_id: Option<i32>,
0340541002 2025-04-24   42: 	pub next_fetch: Option<DateTime<Local>>,
0340541002 2025-04-24   43: 	pub owner: Option<i64>,
0340541002 2025-04-24   44: }
0340541002 2025-04-24   45: 
0340541002 2025-04-24   46: #[derive(Clone)]
0340541002 2025-04-24   47: pub struct Db {
0340541002 2025-04-24   48: 	pool: sqlx::Pool<sqlx::Postgres>,
0340541002 2025-04-24   49: }
0340541002 2025-04-24   50: 
0340541002 2025-04-24   51: pub struct Conn{
0340541002 2025-04-24   52: 	conn: PoolConnection<Postgres>,
0340541002 2025-04-24   53: }
0340541002 2025-04-24   54: 
0340541002 2025-04-24   55: impl Db {
0340541002 2025-04-24   56: 	pub fn new (pguri: &str) -> Result<Db> {
0340541002 2025-04-24   57: 		Ok(Db{
0340541002 2025-04-24   58: 			pool: PgPoolOptions::new()
0340541002 2025-04-24   59: 				.max_connections(5)
0340541002 2025-04-24   60: 				.acquire_timeout(std::time::Duration::new(300, 0))
0340541002 2025-04-24   61: 				.idle_timeout(std::time::Duration::new(60, 0))
0340541002 2025-04-24   62: 				.connect_lazy(pguri)?,
0340541002 2025-04-24   63: 		})
0340541002 2025-04-24   64: 	}
0340541002 2025-04-24   65: 
0340541002 2025-04-24   66: 	pub async fn begin(&mut self) -> Result<Conn> {
0340541002 2025-04-24   67: 		Conn::new(&mut self.pool).await
0340541002 2025-04-24   68: 	}
0340541002 2025-04-24   69: }
0340541002 2025-04-24   70: 
0340541002 2025-04-24   71: impl Conn {
0340541002 2025-04-24   72: 	pub async fn new (pool: &mut Pool<Postgres>) -> Result<Conn> {
0340541002 2025-04-24   73: 		let conn = pool.acquire().await?;
0340541002 2025-04-24   74: 		Ok(Conn{
0340541002 2025-04-24   75: 			conn,
0340541002 2025-04-24   76: 		})
0340541002 2025-04-24   77: 	}
0340541002 2025-04-24   78: 
0340541002 2025-04-24   79: 	pub async fn add_post (&mut self, id: i32, date: &DateTime<FixedOffset>, post_url: &str) -> Result<()> {
0340541002 2025-04-24   80: 		sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);")
0340541002 2025-04-24   81: 			.bind(id)
0340541002 2025-04-24   82: 			.bind(date)
0340541002 2025-04-24   83: 			.bind(post_url)
0340541002 2025-04-24   84: 			.execute(&mut *self.conn).await?;
0340541002 2025-04-24   85: 		Ok(())
0340541002 2025-04-24   86: 	}
0340541002 2025-04-24   87: 
0340541002 2025-04-24   88: 	pub async fn clean (&mut self, source_id: i32, owner: i64) -> Result<Cow<'_, str>> {
0340541002 2025-04-24   89: 		match sqlx::query("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;")
0340541002 2025-04-24   90: 			.bind(source_id)
0340541002 2025-04-24   91: 			.bind(owner)
0340541002 2025-04-24   92: 			.execute(&mut *self.conn).await?.rows_affected() {
0340541002 2025-04-24   93: 			0 => { Ok("No data found found.".into()) },
0340541002 2025-04-24   94: 			x => { Ok(format!("{x} posts purged.").into()) },
0340541002 2025-04-24   95: 		}
0340541002 2025-04-24   96: 	}
0340541002 2025-04-24   97: 
0340541002 2025-04-24   98: 	pub async fn delete (&mut self, source_id: i32, owner: i64) -> Result<Cow<'_, str>> {
0340541002 2025-04-24   99: 		match sqlx::query("delete from rsstg_source where source_id = $1 and owner = $2;")
0340541002 2025-04-24  100: 			.bind(source_id)
0340541002 2025-04-24  101: 			.bind(owner)
0340541002 2025-04-24  102: 			.execute(&mut *self.conn).await?.rows_affected() {
0340541002 2025-04-24  103: 			0 => { Ok("No data found found.".into()) },
0340541002 2025-04-24  104: 			x => { Ok(format!("{} sources removed.", x).into()) },
0340541002 2025-04-24  105: 		}
0340541002 2025-04-24  106: 	}
0340541002 2025-04-24  107: 
0340541002 2025-04-24  108: 	pub async fn disable (&mut self, source_id: i32, owner: i64) -> Result<&str> {
0340541002 2025-04-24  109: 		match sqlx::query("update rsstg_source set enabled = false where source_id = $1 and owner = $2")
0340541002 2025-04-24  110: 			.bind(source_id)
0340541002 2025-04-24  111: 			.bind(owner)
0340541002 2025-04-24  112: 			.execute(&mut *self.conn).await?.rows_affected() {
0340541002 2025-04-24  113: 			1 => { Ok("Source disabled.") },
0340541002 2025-04-24  114: 			0 => { Ok("Source not found.") },
0340541002 2025-04-24  115: 			_ => { bail!("Database error.") },
0340541002 2025-04-24  116: 		}
0340541002 2025-04-24  117: 	}
0340541002 2025-04-24  118: 
0340541002 2025-04-24  119: 	pub async fn enable (&mut self, source_id: i32, owner: i64) -> Result<&str> {
0340541002 2025-04-24  120: 		match sqlx::query("update rsstg_source set enabled = true where source_id = $1 and owner = $2")
0340541002 2025-04-24  121: 			.bind(source_id)
0340541002 2025-04-24  122: 			.bind(owner)
0340541002 2025-04-24  123: 			.execute(&mut *self.conn).await?.rows_affected() {
0340541002 2025-04-24  124: 			1 => { Ok("Source enabled.") },
0340541002 2025-04-24  125: 			0 => { Ok("Source not found.") },
0340541002 2025-04-24  126: 			_ => { bail!("Database error.") },
0340541002 2025-04-24  127: 		}
0340541002 2025-04-24  128: 	}
0340541002 2025-04-24  129: 
0340541002 2025-04-24  130: 	pub async fn exists (&mut self, post_url: &str, id: i32) -> Result<Option<bool>> {
0340541002 2025-04-24  131: 		let row = sqlx::query("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;")
0340541002 2025-04-24  132: 			.bind(post_url)
0340541002 2025-04-24  133: 			.bind(id)
0340541002 2025-04-24  134: 			.fetch_one(&mut *self.conn).await?;
0340541002 2025-04-24  135: 		let exists: Option<bool> = row.try_get("exists")?;
0340541002 2025-04-24  136: 		Ok(exists)
0340541002 2025-04-24  137: 	}
0340541002 2025-04-24  138: 
0340541002 2025-04-24  139: 	pub async fn get_queue (&mut self) -> Result<Vec<Queue>> {
0340541002 2025-04-24  140: 		let block: Vec<Queue> = sqlx::query_as("select source_id, next_fetch, owner from rsstg_order natural left join rsstg_source where next_fetch < now() + interval '1 minute';")
0340541002 2025-04-24  141: 			.fetch_all(&mut *self.conn).await?;
0340541002 2025-04-24  142: 		Ok(block)
0340541002 2025-04-24  143: 	}
0340541002 2025-04-24  144: 
0340541002 2025-04-24  145: 	pub async fn get_list (&mut self, owner: i64) -> Result<Vec<List>> {
0340541002 2025-04-24  146: 		let source: Vec<List> = sqlx::query_as("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 order by source_id")
0340541002 2025-04-24  147: 			.bind(owner)
0340541002 2025-04-24  148: 			.fetch_all(&mut *self.conn).await?;
0340541002 2025-04-24  149: 		Ok(source)
0340541002 2025-04-24  150: 	}
0340541002 2025-04-24  151: 
0340541002 2025-04-24  152: 	pub async fn get_source (&mut self, id: i32, owner: i64) -> Result<Source> {
0340541002 2025-04-24  153: 		let source: Source = sqlx::query_as("select channel_id, url, iv_hash, owner, url_re from rsstg_source where source_id = $1 and owner = $2")
0340541002 2025-04-24  154: 			.bind(id)
0340541002 2025-04-24  155: 			.bind(owner)
0340541002 2025-04-24  156: 			.fetch_one(&mut *self.conn).await?;
0340541002 2025-04-24  157: 		Ok(source)
0340541002 2025-04-24  158: 	}
0340541002 2025-04-24  159: 
0340541002 2025-04-24  160: 	pub async fn set_scrape (&mut self, id: i32) -> Result<()> {
0340541002 2025-04-24  161: 		sqlx::query("update rsstg_source set last_scrape = now() where source_id = $1;")
0340541002 2025-04-24  162: 			.bind(id)
0340541002 2025-04-24  163: 			.execute(&mut *self.conn).await?;
0340541002 2025-04-24  164: 		Ok(())
0340541002 2025-04-24  165: 	}
0340541002 2025-04-24  166: 
0340541002 2025-04-24  167: 	pub async fn update (&mut self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: i64) -> Result<&str> {
0340541002 2025-04-24  168: 		match match update {
0340541002 2025-04-24  169: 				Some(id) => {
0340541002 2025-04-24  170: 					sqlx::query("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1")
0340541002 2025-04-24  171: 						.bind(id)
0340541002 2025-04-24  172: 				},
0340541002 2025-04-24  173: 				None => {
0340541002 2025-04-24  174: 					sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner, channel, url_re) values ($1, $2, $3, $4, $5, $6)")
0340541002 2025-04-24  175: 				},
0340541002 2025-04-24  176: 			}
0340541002 2025-04-24  177: 				.bind(channel_id)
0340541002 2025-04-24  178: 				.bind(url)
0340541002 2025-04-24  179: 				.bind(iv_hash)
0340541002 2025-04-24  180: 				.bind(owner)
0340541002 2025-04-24  181: 				.bind(channel)
0340541002 2025-04-24  182: 				.bind(url_re)
0340541002 2025-04-24  183: 				.execute(&mut *self.conn).await
0340541002 2025-04-24  184: 			{
0340541002 2025-04-24  185: 			Ok(_) => Ok(match update {
0340541002 2025-04-24  186: 				Some(_) => "Channel updated.",
0340541002 2025-04-24  187: 				None => "Channel added.",
0340541002 2025-04-24  188: 			}),
0340541002 2025-04-24  189: 			Err(sqlx::Error::Database(err)) => {
0340541002 2025-04-24  190: 				match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() {
0340541002 2025-04-24  191: 					Some("_bt_check_unique", ) => {
0340541002 2025-04-24  192: 						Ok("Duplicate key.")
0340541002 2025-04-24  193: 					},
0340541002 2025-04-24  194: 					Some(_) => {
0340541002 2025-04-24  195: 						Ok("Database error.")
0340541002 2025-04-24  196: 					},
0340541002 2025-04-24  197: 					None => {
0340541002 2025-04-24  198: 						Ok("No database error extracted.")
0340541002 2025-04-24  199: 					},
0340541002 2025-04-24  200: 				}
0340541002 2025-04-24  201: 			},
0340541002 2025-04-24  202: 			Err(err) => {
0340541002 2025-04-24  203: 				bail!("Sorry, unknown error:\n{err:#?}\n");
0340541002 2025-04-24  204: 			},
0340541002 2025-04-24  205: 		}
0340541002 2025-04-24  206: 	}
0340541002 2025-04-24  207: }