summaryrefslogtreecommitdiff
path: root/src/repo
diff options
context:
space:
mode:
Diffstat (limited to 'src/repo')
-rw-r--r--src/repo/channel.rs7
-rw-r--r--src/repo/sequence.rs52
2 files changed, 43 insertions, 16 deletions
diff --git a/src/repo/channel.rs b/src/repo/channel.rs
index efc2ced..ad42710 100644
--- a/src/repo/channel.rs
+++ b/src/repo/channel.rs
@@ -82,7 +82,10 @@ impl<'c> Channels<'c> {
Ok(channel)
}
- pub async fn all(&mut self) -> Result<Vec<Channel>, sqlx::Error> {
+ pub async fn all(
+ &mut self,
+ resume_point: Option<Sequence>,
+ ) -> Result<Vec<Channel>, sqlx::Error> {
let channels = sqlx::query_as!(
Channel,
r#"
@@ -92,8 +95,10 @@ impl<'c> Channels<'c> {
created_at as "created_at: DateTime",
created_sequence as "created_sequence: Sequence"
from channel
+ where coalesce(created_sequence <= $1, true)
order by channel.name
"#,
+ resume_point,
)
.fetch_all(&mut *self.0)
.await?;
diff --git a/src/repo/sequence.rs b/src/repo/sequence.rs
index 8fe9dab..c47b41c 100644
--- a/src/repo/sequence.rs
+++ b/src/repo/sequence.rs
@@ -1,3 +1,5 @@
+use std::fmt;
+
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
pub trait Provider {
@@ -10,6 +12,37 @@ impl<'c> Provider for Transaction<'c, Sqlite> {
}
}
+pub struct Sequences<'t>(&'t mut SqliteConnection);
+
+impl<'c> Sequences<'c> {
+ pub async fn next(&mut self) -> Result<Sequence, sqlx::Error> {
+ let next = sqlx::query_scalar!(
+ r#"
+ update event_sequence
+ set last_value = last_value + 1
+ returning last_value as "next_value: Sequence"
+ "#,
+ )
+ .fetch_one(&mut *self.0)
+ .await?;
+
+ Ok(next)
+ }
+
+ pub async fn current(&mut self) -> Result<Sequence, sqlx::Error> {
+ let next = sqlx::query_scalar!(
+ r#"
+ select last_value as "last_value: Sequence"
+ from event_sequence
+ "#,
+ )
+ .fetch_one(&mut *self.0)
+ .await?;
+
+ Ok(next)
+ }
+}
+
#[derive(
Clone,
Copy,
@@ -26,20 +59,9 @@ impl<'c> Provider for Transaction<'c, Sqlite> {
#[sqlx(transparent)]
pub struct Sequence(i64);
-pub struct Sequences<'t>(&'t mut SqliteConnection);
-
-impl<'c> Sequences<'c> {
- pub async fn next(&mut self) -> Result<Sequence, sqlx::Error> {
- let next = sqlx::query_scalar!(
- r#"
- update event_sequence
- set last_value = last_value + 1
- returning last_value as "next_value: Sequence"
- "#,
- )
- .fetch_one(&mut *self.0)
- .await?;
-
- Ok(next)
+impl fmt::Display for Sequence {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let Self(value) = self;
+ value.fmt(f)
}
}