aboutsummaryrefslogtreecommitdiff
path: root/lib/App/MediaControl/DB.rakumod
blob: dd26e22958c8071e3b5c073efb59c59df5339972 (plain)
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
use v6.d;
use DB::SQLite;
 
class App::MediaControl::DB {
    has DB::SQLite $.pool is required;
 
    method !db(Callable:D $code{
        sub do-it {
            my $conn = self.pool.db;
            # we need an explicit LEAVE block because on 2021.10, `will 
            # leave { .finish }` kills precomp 
            LEAVE { .finish with $conn };
            $conn.execute('PRAGMA foreign_keys=true');
            $conn.begin;
            KEEP { .commit with $conn };
            return $code($connwith $conn;
        }
 
        loop {
            CATCH {
                when DB::SQLite::Error {
                    note "DB error: {$_.gist}, retrying";
                    sleep 0.5;
                    redo;
                }
            }
 
            return do-it();
        }
    }
 
    method ensure-schema() {
        return if self!db: { .query(
            'SELECT 1 FROM sqlite_schema WHERE type=? AND tbl_name=?',
            'table''files',
        ).value.defined };
 
        self!db: {
            .query(q:to/END/); 
            CREATE TABLE files (
                id INTEGER PRIMARY KEY,
                parent_id INTEGER NULL REFERENCES files(id)
                          ON DELETE CASCADE,
                path TEXT NOT NULL,
                name TEXT NOT NULL,
                is_dir BOOLEAN NOT NULL,
                watched_time INTEGER NULL,
                seen BOOLEAN NOT NULL DEFAULT false,
                UNIQUE (path, name)
            )
            END
        }
    }
 
    method clear-seen() {
        self!db: {
            .query(q:to/END/) 
            UPDATE files SET seen=false
            END
        }
    }
 
    method remove-unseen() {
        self!db: {
            .query(q:to/END/) 
            DELETE FROM files
            WHERE seen=false
            END
        }
    }
    
    method add-entry(Str:D() :$path! is copyStr:D() :$name!Bool:D() :$is-dir!{
        $path ~~ s{<!after '/'>$} = '/';
        $path ~~ s{<!before '/'>^} = '/';
 
        self!db: {
            .query(q:to/END/:$path, :$name:is_dir($is-dir)); 
            WITH parent(id,path) AS (
                SELECT id, path || name || '/' FROM files
            ),
            newrow(path,name,is_dir) AS (
                VALUES($path, $name, $is_dir)
            )
            INSERT INTO files(parent_id,path,name,is_dir,seen)
            SELECT parent.id, newrow.path, newrow.name, newrow.is_dir, true
            FROM newrow
            LEFT JOIN parent ON parent.path=newrow.path
            WHERE true
            ON CONFLICT (path,name) DO UPDATE SET seen=true
            END
        }
    }
 
    method remove-entry(Int:D() $id{
        self!db: {
            .query(q:to/END/:$id); 
            DELETE FROM files
            WHERE id=$id
            END
        }
    }
 
    multi method get-children-of(Any:U $) { 'IS NULL' }
    multi method get-children-of(Int:D() $id{ '=?', $id }
    proto method get-children-of($) {
        my ($clause, @binds= {*};
        self!db: {
            .query(qq:to/END/,|@binds).hashes; 
            SELECT id, path, name, is_dir, watched_time
            FROM files
            WHERE parent_id $clause
            ORDER BY name ASC
            END
        }
    }
 
    method get-parents-of(Int:D() $id{
        self!db: {
            .query(q:to/END/,:$id).hashes.reverse; 
            WITH f(id, parent_id, name) AS (
              SELECT id, parent_id, name
              FROM files
              WHERE id=$id
             UNION ALL
              SELECT files.id, files.parent_id, files.name
              FROM files JOIN f ON files.id=f.parent_id
            )
            SELECT id, name
            FROM f
            END
        }
    }
 
    method get-entry(Int:D() $id{
        self!db: {
            .query(q:to/END/,:$id).hash; 
            SELECT id, path, name, is_dir
            FROM files
            WHERE id=$id
            END
        }
    }
 
    method mark-entry-watched(Int:D() $id{
        self!db: {
            .query(q:to/END/,:$id,:time(time)); 
            UPDATE files
            SET watched_time=$time
            WHERE id=$id
            END
        }
    }
 
    method get-recently-watched-folders(Int:D() $limit=20{
        self!db: {
            .query(qq:to/END/, $limit).hashes; 
            WITH recent(id,watched_time) AS (
              SELECT parent_id AS id, MAX(watched_time) AS watched_time
              FROM files
              WHERE is_dir=false
                AND watched_time IS NOT NULL
                AND parent_id IS NOT NULL
              GROUP BY parent_id
              ORDER BY watched_time DESC
              LIMIT ?
            )
            SELECT files.id, files.path, files.name, files.is_dir, recent.watched_time
            FROM files
            JOIN recent ON files.id=recent.id
            END
        }
    }
}