summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Model/DB.pm6
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-23 19:17:19 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-23 19:17:19 +0000
commit7a16254f039d02f8e8dc24858ae1840b547408b1 (patch)
treeaf4e9da55949689576eeb8a4c67d2d260da7fdd8 /lib/Ultramarine/Model/DB.pm6
parentscript to call my subsonic server (diff)
downloadUltramarine-7a16254f039d02f8e8dc24858ae1840b547408b1.tar.gz
Ultramarine-7a16254f039d02f8e8dc24858ae1840b547408b1.tar.bz2
Ultramarine-7a16254f039d02f8e8dc24858ae1840b547408b1.zip
restructure DB schema, go plain DBIish
Diffstat (limited to 'lib/Ultramarine/Model/DB.pm6')
-rw-r--r--lib/Ultramarine/Model/DB.pm662
1 files changed, 52 insertions, 10 deletions
diff --git a/lib/Ultramarine/Model/DB.pm6 b/lib/Ultramarine/Model/DB.pm6
index cbc353d..7a0d512 100644
--- a/lib/Ultramarine/Model/DB.pm6
+++ b/lib/Ultramarine/Model/DB.pm6
@@ -1,6 +1,7 @@
use v6.d.PREVIEW;
-use DBI::Async;
+use DBIish;
use Ultramarine::Model::DBMigration;
+use JSON::Fast;
class Ultramarine::Model::DB {
has $.db-driver is required;
@@ -8,35 +9,76 @@ class Ultramarine::Model::DB {
my @migrations = (
-> $dbh {
- $dbh.query(q:to/END/).finish;
+ $dbh.do(q:to/END/);
CREATE TABLE songs (
path TEXT PRIMARY KEY,
- title TEXT,
- artist TEXT
+ mtime INTEGER NOT NULL,
+ metadata TEXT DEFAULT '{}'
);
END
},
);
has $!dbh = do {
- my DBI::Async $dbh .= new($!db-driver, |%!db-args);
+ my $dbh = DBIish.connect($!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)
+ method set-song(:$path!,:$mtime!,:%metadata!) {
+ my %song = pack-row(%(:$path,:$mtime,:%metadata));
+ my $sth = $!dbh.prepare(q:to/END/);
+ INSERT OR REPLACE INTO songs(path,mtime,metadata)
VALUES (?,?,?)
END
+ LEAVE { .finish with $sth }
+ $sth.execute(%song<path mtime metadata>);
}
- method get-song($path) {
- $!dbh.query(q:to/END/,$path).hash;
+ sub unpack-row(%song is copy) {
+ %song<metadata> = from-json(%song<metadata>);
+ return %song;
+ }
+ sub pack-row(%song is copy) {
+ %song<metadata> = to-json(%song<metadata>);
+ return %song;
+ }
+
+ method get-song(:$path!) {
+ my $sth = $!dbh.prepare(q:to/END/);
SELECT *
FROM songs
WHERE path=?
END
+ LEAVE { .finish with $sth }
+ $sth.execute($path);
+ return unpack-row($sth.row(:hash));
+ }
+
+ method all-songs() {
+ my $sth = $!dbh.query(q:to/END/);
+ SELECT *
+ FROM songs
+ ORDER BY path ASC
+ END
+ LEAVE { .finish with $sth };
+ return gather {
+ while $sth.row(:hash) -> %song {
+ take unpack-row(%song);
+ }
+ }
+ }
+
+ method is-up-to-date(:$path!,:$mtime!) {
+ my $sth = $!dbh.prepare(q:to/END/);
+ SELECT COUNT(*)
+ FROM songs
+ WHERE path=?
+ AND mtime >= $mtime
+ END
+ LEAVE { .finish with $sth };
+ $sth.execute($path,$mtime);
+ return ($sth.row[0]//0).Bool;
}
}