cooklang_sync_client/
registry.rs

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
use diesel::dsl::{max, sql};
use diesel::prelude::*;
use diesel::{insert_into, update};

use log::trace;

use crate::connection::Connection;
use crate::models::*;
use crate::schema::*;

type Result<T, E = diesel::result::Error> = std::result::Result<T, E>;

pub fn create(conn: &mut Connection, forms: &Vec<CreateForm>) -> Result<usize> {
    trace!("inserting {:?}", forms);

    insert_into(file_records::table).values(forms).execute(conn)
}

pub fn update_jid(conn: &mut Connection, record: &FileRecord, jid: i32) -> Result<usize> {
    trace!("update_jid {:?}: {:?}", jid, record);

    update(file_records::table)
        .filter(file_records::id.eq(record.id))
        .set(file_records::jid.eq(jid))
        .execute(conn)
}

pub fn delete(conn: &mut Connection, forms: &Vec<DeleteForm>) -> Result<usize> {
    trace!("marking as deleted {:?}", forms);

    insert_into(file_records::table).values(forms).execute(conn)
}

pub fn non_deleted(conn: &mut Connection, namespace_id: i32) -> Result<Vec<FileRecord>> {
    trace!("non_deleted");

    // Consider only latest record for the same path.
    let subquery = file_records::table
        .filter(file_records::namespace_id.eq(namespace_id))
        .group_by(file_records::path)
        .select(max(file_records::id))
        .into_boxed()
        .select(sql::<diesel::sql_types::Integer>("max(id)"));

    file_records::table
        .filter(file_records::deleted.eq(false))
        .filter(file_records::id.eq_any(subquery))
        .select(FileRecord::as_select())
        .order(file_records::id.asc())
        .load::<FileRecord>(conn)
}

/// Files that don't have jid
/// These should be send to remote
pub fn updated_locally(conn: &mut Connection, namespace_id: i32) -> Result<Vec<FileRecord>> {
    trace!("updated_locally");

    // Need to ignore records which come after record with jid
    // for the same path
    let subquery = file_records::table
        .filter(file_records::namespace_id.eq(namespace_id))
        .group_by(file_records::path)
        .select(max(file_records::id))
        .into_boxed()
        .select(sql::<diesel::sql_types::Integer>("max(id)"));

    let query = file_records::table
        .select(FileRecord::as_select())
        .filter(file_records::jid.is_null())
        .filter(file_records::id.eq_any(subquery))
        .order(file_records::id.asc());

    query.load::<FileRecord>(conn)
}

pub fn latest_jid(conn: &mut Connection, namespace_id: i32) -> Result<i32> {
    trace!("latest_jid");

    let r = file_records::table
        .filter(file_records::jid.is_not_null())
        .filter(file_records::namespace_id.eq(namespace_id))
        .select(FileRecord::as_select())
        .order(file_records::jid.desc())
        .first::<FileRecord>(conn);

    match r {
        Ok(r) => Ok(r.jid.unwrap_or(0)),
        Err(e) => Err(e),
    }
}