summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Model/DB.pm6
blob: cbc353dcbbc0b42e106391c0d124480bfbdc920a (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
use v6.d.PREVIEW;
use DBI::Async;
use Ultramarine::Model::DBMigration;
 
class Ultramarine::Model::DB {
    has $.db-driver is required;
    has %.db-args is required;
 
    my @migrations = (
        -> $dbh {
            $dbh.query(q:to/END/).finish; 
            CREATE TABLE songs (
                path TEXT PRIMARY KEY,
                title TEXT,
                artist TEXT
            );
            END
        },
    );
 
    has $!dbh = do {
        my DBI::Async $dbh .= new($!db-driver|%!db-args);
        my Ultramarine::Model::DBMigration $migration .= new(:$dbh,:@migrations);
        $migration.ensure-schema;
        $dbh;
    };
 
    method add-song(:$path!,:$title,:$artist{
        $!dbh.query(q:to/END/,$path,$title,$artist); 
        INSERT INTO songs(path,artist,title)
        VALUES (?,?,?)
        END
    }
 
    method get-song($path) {
        $!dbh.query(q:to/END/,$path).hash; 
        SELECT *
        FROM songs
        WHERE path=?
        END
    }
}