| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use super::Pool; |
| 5 |
use sqlx::FromRow; |
| 6 |
|
| 7 |
#[derive(Clone, Debug, FromRow)] |
| 8 |
pub struct Logged { |
| 9 |
pub idx: i64, |
| 10 |
pub object: String, |
| 11 |
pub kind: String, |
| 12 |
pub repo_id: Option<i64>, |
| 13 |
pub received_ms: i64, |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
pub async fn entry_of(pool: &Pool, object: &str, kind: &str) -> sqlx::Result<Option<i64>> { |
| 18 |
sqlx::query_scalar("SELECT idx FROM tlog_entries WHERE object = $1 AND kind = $2").bind(object).bind(kind).fetch_optional(pool).await |
| 19 |
} |
| 20 |
|
| 21 |
pub async fn record(pool: &Pool, idx: i64, object: &str, kind: &str, repo_id: Option<i64>, received_ms: i64) -> sqlx::Result<()> { |
| 22 |
sqlx::query("INSERT INTO tlog_entries (idx, object, kind, repo_id, received_ms) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING") |
| 23 |
.bind(idx) |
| 24 |
.bind(object) |
| 25 |
.bind(kind) |
| 26 |
.bind(repo_id) |
| 27 |
.bind(received_ms) |
| 28 |
.execute(pool) |
| 29 |
.await?; |
| 30 |
Ok(()) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
pub async fn recent(pool: &Pool, repo_id: Option<i64>, limit: i64) -> sqlx::Result<Vec<Logged>> { |
| 35 |
match repo_id { |
| 36 |
Some(r) => { |
| 37 |
sqlx::query_as("SELECT * FROM tlog_entries WHERE repo_id = $1 ORDER BY idx DESC LIMIT $2") |
| 38 |
.bind(r) |
| 39 |
.bind(limit) |
| 40 |
.fetch_all(pool) |
| 41 |
.await |
| 42 |
} |
| 43 |
None => sqlx::query_as("SELECT * FROM tlog_entries ORDER BY idx DESC LIMIT $1").bind(limit).fetch_all(pool).await, |
| 44 |
} |
| 45 |
} |