mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 14:06:29 +03:00
webxdc update events
This commit is contained in:
20
src/sql.rs
20
src/sql.rs
@@ -7,6 +7,7 @@ use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{bail, Context as _, Result};
|
||||
use rusqlite::types::FromSql;
|
||||
use rusqlite::{config::DbConfig, Connection, OpenFlags};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -363,6 +364,25 @@ impl Sql {
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns unique values of a `column` in `table`
|
||||
pub async fn distinct<T: FromSql + Default>(
|
||||
&self,
|
||||
table: &str,
|
||||
column: &str,
|
||||
) -> Result<Vec<T>> {
|
||||
let conn = self.get_conn().await?;
|
||||
let rows: Result<Vec<T>> = tokio::task::block_in_place(move || {
|
||||
let mut stmt = conn.prepare(&format!("SELECT DISTINCT {column} FROM {table}"))?;
|
||||
let rows = stmt
|
||||
.query([])?
|
||||
.mapped(|r| r.get(0))
|
||||
.map(|a| a.unwrap_or_default())
|
||||
.collect();
|
||||
Ok(rows)
|
||||
});
|
||||
rows
|
||||
}
|
||||
|
||||
/// Prepares and executes the statement and maps a function over the resulting rows.
|
||||
/// Then executes the second function over the returned iterator and returns the
|
||||
/// result of that function.
|
||||
|
||||
Reference in New Issue
Block a user