Skip to main content

coven/
member_write.rs

1//! The app's write as a member begins: the creator's first write (§10.3, E1).
2
3use crate::*;
4use coven_database::SqlContext;
5
6/// The app's SQL as a member begins, in one write with the membership. The
7/// creator's first write runs on it, inside `create_store`, after slot 1 has
8/// registered the creator and their device and made the circles the app
9/// asked for, and before the store is visible (§10.3). It offers the app's
10/// SQL only: no operation, file or administrative step exists on it, so what
11/// it commits is the member's own rows and nothing in the store's
12/// membership.
13pub struct MemberWrite<'write> {
14    sql: SqlContext<'write, 'write>,
15}
16
17impl<'write> MemberWrite<'write> {
18    pub(crate) fn new(sql: SqlContext<'write, 'write>) -> Self {
19        Self { sql }
20    }
21
22    /// Execute one app statement with parameters.
23    pub fn execute<P: Params>(&self, sql: &str, params: P) -> SqliteResult<usize> {
24        self.sql.execute(sql, params)
25    }
26
27    /// Execute app statements within this write's transaction.
28    pub fn execute_batch(&self, sql: &str) -> SqliteResult<()> {
29        self.sql.execute_batch(sql)
30    }
31
32    /// Map one app row, including changes already made by this write.
33    pub fn query_row<T, P, F>(&self, sql: &str, params: P, map: F) -> SqliteResult<T>
34    where
35        P: Params,
36        F: FnOnce(&Row<'_>) -> SqliteResult<T>,
37    {
38        self.sql.query_row(sql, params, map)
39    }
40
41    /// Map all app rows produced by a query within this write.
42    pub fn query<T, P, F>(&self, sql: &str, params: P, map: F) -> SqliteResult<Vec<T>>
43    where
44        P: Params,
45        F: FnMut(&Row<'_>) -> SqliteResult<T>,
46    {
47        self.sql.query(sql, params, map)
48    }
49}
50
51impl SqlRead for MemberWrite<'_> {
52    fn query_row<T, P, F>(&self, sql: &str, params: P, map: F) -> SqliteResult<T>
53    where
54        P: Params,
55        F: FnOnce(&Row<'_>) -> SqliteResult<T>,
56    {
57        MemberWrite::query_row(self, sql, params, map)
58    }
59
60    fn query<T, P, F>(&self, sql: &str, params: P, map: F) -> SqliteResult<Vec<T>>
61    where
62        P: Params,
63        F: FnMut(&Row<'_>) -> SqliteResult<T>,
64    {
65        MemberWrite::query(self, sql, params, map)
66    }
67}