Annotation For src/sql.rs
Logged in as anonymous

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

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