aboutsummaryrefslogtreecommitdiff
path: root/lib/App/MediaControl/DB.rakumod
blob: 5f67bb6ac006e6dc4d7166e441c17792268ea320 (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
use v6.d;
use DB::SQLite;
 
class App::MediaControl::DB {
    has DB::SQLite $.pool is required;
 
    method !db(Callable:D $code{
        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.begin;
        $conn.execute('PRAGMA foreign_keys=true');
        KEEP { .commit with $conn };
        return $code($connwith $conn;
    }
 
    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 '/'>^} = '/';
 
        note "add-entry($path,$name)";
        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 id, newrow.path, name, 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(Str:D() :$path! is copyStr:D() :$name!{
        $path ~~ s{<!after '/'>$} = '/';
        $path ~~ s{<!before '/'>^} = '/';
 
        note "remove-entry($path,$name)";
        self!db: {
            .query(q:to/END/:$path, :$name); 
            DELETE FROM files
            WHERE name=$name
              AND path=$path
            END
        }
    }
 
    multi method get-children-of(Any:U $id{
        self!get-children('IS NULL');
    }
    multi method get-children-of(Int:D() $id{
        self!get-children('=$id',$id);
    }
    method !get-children(Str $clause*@binds{
        self!db: {
            .query(qq:to/END/,|@binds).hashes; 
            SELECT id, name, is_dir
            FROM files
            WHERE parent_id $clause
            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
        }
    }
}