Skip to main content

max / makenotwork

991 B · 32 lines History Blame Raw
1 use std::fmt::Write as _;
2
3 use super::Uuid;
4
5 /// Insert mention rows for a post (batch insert). Ignores duplicates.
6 #[tracing::instrument(skip_all)]
7 pub async fn insert_mentions<'e, E: sqlx::PgExecutor<'e>>(
8 executor: E,
9 post_id: Uuid,
10 user_ids: &[Uuid],
11 ) -> Result<(), sqlx::Error> {
12 if user_ids.is_empty() {
13 return Ok(());
14 }
15 // runtime-checked: dynamic SQL (cannot use compile-time macro)
16 // Build multi-row INSERT: VALUES ($1, $2), ($1, $3), ... ON CONFLICT DO NOTHING
17 let mut sql = String::from("INSERT INTO post_mentions (post_id, mentioned_user_id) VALUES ");
18 for (i, _) in user_ids.iter().enumerate() {
19 if i > 0 {
20 sql.push_str(", ");
21 }
22 let _ = write!(sql, "($1, ${})", i + 2);
23 }
24 sql.push_str(" ON CONFLICT DO NOTHING");
25 let mut query = sqlx::query(&sql).bind(post_id);
26 for user_id in user_ids {
27 query = query.bind(user_id);
28 }
29 query.execute(executor).await?;
30 Ok(())
31 }
32