From 09650d2214422d3e56adbf1885dca3aa07ce1f7e Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 3 Mar 2018 14:07:18 +0000 Subject: more retrieval methods in DB --- lib/Ultramarine/Model/DB/SQLite.pm6 | 52 +++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'lib/Ultramarine/Model/DB/SQLite.pm6') diff --git a/lib/Ultramarine/Model/DB/SQLite.pm6 b/lib/Ultramarine/Model/DB/SQLite.pm6 index 8a0e017..030a32f 100644 --- a/lib/Ultramarine/Model/DB/SQLite.pm6 +++ b/lib/Ultramarine/Model/DB/SQLite.pm6 @@ -130,7 +130,7 @@ class Ultramarine::Model::DB::SQLite does Ultramarine::Model::DB { $rel-path .= parent; } }; - my $parent-id=Nil; + my $parent-id=$ROOT-DIR-ID; while my $name = @components.pop { $parent-id = self.ensure-one-directory(:$name,:$parent-id); } @@ -214,7 +214,9 @@ class Ultramarine::Model::DB::SQLite does Ultramarine::Model::DB { sub unpack-row(%song is copy) { %song = from-json(%song); - return %song; + return Ultramarine::Model::DB::File.new( + |(%song:p.Capture), + ); } method get-song(Str() :$path!) { @@ -228,6 +230,52 @@ class Ultramarine::Model::DB::SQLite does Ultramarine::Model::DB { return unpack-row($sth.row(:hash)); } + method top-folders() { + return (Ultramarine::Model::DB::Directory.new( + id => $ROOT-DIR-ID, + name => 'Music', + ),); + } + + method dir-children-of(Int() :$id = $ROOT-DIR-ID) { + my $sth = $!dbh.prepare(q:to/END/); + SELECT * + FROM directories + WHERE parent_id = ? + AND id != ? + ORDER BY name ASC + END + # the C< AND id != ? > is because the root dir is its own + # parent, and we never want to return it + $sth.execute($id,$id); + + return gather { + while $sth.row(:hash) -> %dir { + take Ultramarine::Model::DB::Directory.new( + |(%dir:p.Capture), + ); + } + .finish with $sth; + }; + } + + method songs-children-of(Int() :$id = $ROOT-DIR-ID) { + my $sth = $!dbh.prepare(q:to/END/); + SELECT * + FROM songs + WHERE directory_id = ? + ORDER BY path ASC + END + $sth.execute($id); + + return gather { + while $sth.row(:hash) -> %song { + take unpack-row(%song); + } + .finish with $sth; + }; + } + method all-songs() { my $sth = $!dbh.prepare(q:to/END/); SELECT * -- cgit v1.2.3