1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::fmt::Write;

use serde::Deserialize;
use sha1::{Digest, Sha1};

use crate::{
    error::{Error, Result},
    introspection::Reflection,
    query,
    sql_builder::{Bind, SqlBuilder},
    Client, Compression,
};

pub struct Watch<V = Rows> {
    client: Client,
    sql: SqlBuilder,
    limit: Option<usize>,
    _kind: V,
}

pub struct Rows;
pub struct Events;

impl<V> Watch<V> {
    pub fn bind(mut self, value: impl Bind) -> Self {
        self.sql.bind_arg(value);
        self
    }

    // TODO: `timeout()`.

    pub fn limit(mut self, limit: impl Into<Option<usize>>) -> Self {
        self.limit = limit.into();
        self
    }

    // TODO: `groups()` for `(Version, &[T])`.

    fn cursor<T: Reflection>(self, only_events: bool) -> Result<RawCursor<T>> {
        let sql = self.sql.finish()?;
        let (sql, view) = if is_table_name(&sql) {
            (None, sql)
        } else {
            let view = make_live_view_name(&sql);
            (Some(sql), view)
        };

        Ok(RawCursor::Preparing {
            client: self.client,
            sql,
            view,
            limit: self.limit,
            only_events,
        })
    }
}

impl Watch<Rows> {
    pub(crate) fn new(client: &Client, template: &str) -> Self {
        Self {
            client: client.clone(),
            sql: SqlBuilder::new(template),
            limit: None,
            _kind: Rows,
        }
    }

    #[deprecated(since = "0.4.0", note = "use `Watch::fetch()` instead")]
    pub fn rows<T: Reflection>(self) -> Result<RowCursor<T>> {
        self.fetch()
    }

    #[deprecated(since = "0.4.0", note = "use `Watch::only_events().fetch()` instead")]
    pub fn events(self) -> Result<EventCursor> {
        self.only_events().fetch()
    }

    pub fn only_events(self) -> Watch<Events> {
        Watch {
            client: self.client,
            sql: self.sql,
            limit: self.limit,
            _kind: Events,
        }
    }

    pub fn fetch<T: Reflection>(self) -> Result<RowCursor<T>> {
        Ok(RowCursor(self.cursor(false)?))
    }

    pub async fn fetch_one<T>(self) -> Result<(Version, T)>
    where
        T: Reflection + for<'b> Deserialize<'b>,
    {
        match self.limit(1).fetch()?.next().await {
            Ok(Some(row)) => Ok(row),
            Ok(None) => Err(Error::RowNotFound),
            Err(err) => Err(err),
        }
    }
}

impl Watch<Events> {
    pub fn fetch(self) -> Result<EventCursor> {
        Ok(EventCursor(self.cursor(true)?))
    }

    pub async fn fetch_one(self) -> Result<Version> {
        match self.limit(1).fetch()?.next().await {
            Ok(Some(row)) => Ok(row),
            Ok(None) => Err(Error::RowNotFound),
            Err(err) => Err(err),
        }
    }
}

pub type Version = u64; // TODO: NonZeroU64

pub struct EventCursor(RawCursor<()>);

impl EventCursor {
    pub async fn next(&mut self) -> Result<Option<Version>> {
        Ok(self.0.next().await?.map(|(_, version)| version))
    }
}

pub struct RowCursor<T>(RawCursor<T>);

impl<T> RowCursor<T> {
    pub async fn next<'a, 'b: 'a>(&'a mut self) -> Result<Option<(Version, T)>>
    where
        T: Deserialize<'b> + Reflection,
    {
        Ok(self.0.next().await?.map(|(row, version)| (version, row)))
    }
}

enum RawCursor<T> {
    Preparing {
        client: Client,
        sql: Option<String>,
        view: String,
        limit: Option<usize>,
        only_events: bool,
    },
    Fetching(query::RowCursor<(T, Version)>),
}

impl<T> RawCursor<T> {
    pub async fn next<'a, 'b: 'a>(&'a mut self) -> Result<Option<(T, Version)>>
    where
        T: Deserialize<'b> + Reflection,
    {
        if let RawCursor::Preparing {
            client,
            sql,
            view,
            limit,
            only_events,
        } = self
        {
            // It seems `WATCH` and compression are incompatible.
            if client.compression != Compression::None {
                take_mut::take(client, |client| client.with_compression(Compression::None))
            }

            if let Some(sql) = sql {
                let create_sql = format!(
                    "CREATE LIVE VIEW IF NOT EXISTS {} WITH TIMEOUT AS {}",
                    view, sql
                );

                client.query(&create_sql).execute().await?;
            }

            let events = if *only_events { " EVENTS" } else { "" };
            let watch_sql = match limit {
                Some(limit) => format!("WATCH {}{} LIMIT {}", view, events, limit),
                None => format!("WATCH {}{}", view, events),
            };

            let cursor = client.query(&watch_sql).fetch()?;
            *self = RawCursor::Fetching(cursor);
        }

        match self {
            RawCursor::Preparing { .. } => unreachable!(),
            RawCursor::Fetching(cursor) => Ok(cursor.next().await?),
        }
    }
}

fn is_table_name(sql: &str) -> bool {
    // TODO: support quoted identifiers.
    sql.split_ascii_whitespace().take(2).count() == 1
}

fn make_live_view_name(sql: &str) -> String {
    let mut hasher = Sha1::new();
    hasher.update(sql.as_bytes());
    let result = hasher.finalize();

    let mut name = String::with_capacity(40);
    for word in &result[..] {
        let _ = write!(&mut name, "{:02x}", word);
    }

    format!("lv_{}", name)
}

#[test]
fn it_makes_live_view_name() {
    let a = make_live_view_name("SELECT 1");
    let b = make_live_view_name("SELECT 2");

    assert_ne!(a, b);
    assert_eq!(a.len(), 3 + 40);
    assert_eq!(b.len(), 3 + 40);
}