X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FDB.pm;h=a7766531e81ee8f6104e940a8f4ca65a7802b919;hb=e4fb6406212b7adb92d5963ae1bc156f0d43e46d;hp=6b3cd9910c4c695f2255eb559cae23da50cd16c7;hpb=0cd0034a3935f56a2a91e63185da84a150b20832;p=id3fs.git diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index 6b3cd99..a776653 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -1,3 +1,19 @@ +# id3fs - a FUSE-based filesystem for browsing audio metadata +# Copyright (C) 2010 Ian Beckwith +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + package ID3FS::DB; use strict; @@ -7,7 +23,7 @@ use ID3FS::AudioFile; use Cwd; our $SCHEMA_VERSION=1; -my $dbfile=".id3fs"; +my $dbfile=".id3fs"; # default sub new { @@ -21,6 +37,7 @@ sub new my $init=shift; $self->{base}=shift; $self->{dbpath}=shift || ($self->{base} . "/" . $dbfile); + $self->{dbpath}=Cwd::abs_path($self->{dbpath}); $self->{absbase}=Cwd::abs_path($self->{base}); my $connectstr="dbi:SQLite:dbname=$self->{dbpath}"; @@ -44,6 +61,7 @@ sub new return $self; } +# search parent directories for db sub find_db { # class method @@ -147,7 +165,6 @@ sub tags_with_values my $sql=("SELECT p.name, t.name FROM tags t\n" . "INNER JOIN tags p ON t.parents_id=p.id\n" . "GROUP BY p.name, t.name\n"); -# print "SQL: $sql\n"; my $result=$self->cmd_rows($sql); my $tags={}; for my $pair (@$result) @@ -168,31 +185,23 @@ sub tag_has_values sub relativise { - my($self, $path, $name, $mountpoint)=@_; - my $id3fs_path=$self->{dbpath}; - $id3fs_path=~s/(.*)\/.*/$1/; + my($self, $path, $name, $mountpoint, $querypath)=@_; my $rpath="$self->{absbase}/$path"; - my $vpath="$mountpoint/$id3fs_path"; + my $vpath=$mountpoint . $querypath; my @path=split(/\//,$rpath); my @rel=split(/\//,$vpath); - #absolute paths have empty first element due to leading / + # drop filename from rel + pop @rel; + # absolute paths have empty first element due to leading / shift(@path) if($path[0] eq ""); shift(@rel) if($rel[0] eq ""); - if($path[0] ne $rel[0]) - { - #no path in common, return absolute - print "FAIL: NO PATHS IN COMMON\n"; - return $name; - } # f: /home/foo/bar/baz.mp3 # r: /home/ianb/music/albums while(@path && @rel && ($path[0] eq $rel[0])) { shift(@path); shift(@rel); -# print "POP "; } -# print "\n"; my $upcount=scalar(@rel); my $result="../" x $upcount; $result .= join("/",@path); @@ -265,6 +274,11 @@ sub add { $self->add_tag($file_id, "haspic", undef); } + + if($self->ok($audiotype)) + { + $self->add_tag($file_id, "audiotype", $audiotype); + } } sub add_tag @@ -329,7 +343,6 @@ sub files_in my $sql=("SELECT files.name FROM files\n" . "INNER JOIN paths ON files.paths_id=paths.id\n" . "WHERE paths.name=?\n"); -# print "files_in: SQL: $sql\n"; return($self->cmd_firstcol($sql, $dir)); } @@ -369,7 +382,6 @@ sub prune_paths return unless(@ids); my $sql=("DELETE FROM files WHERE paths_id IN (\n\t" . join(', ', map { "\"$_\""; } @ids). "\n\t)"); -# print "SQL: \n", $sql, "\n"; $self->cmd($sql); } @@ -431,6 +443,7 @@ sub ok return(defined($thing) && length($thing) && $thing =~ /\S+/); } +# actually call the database sub cmd_sth { my($self, $sql, @params)=@_; @@ -445,6 +458,7 @@ sub cmd_sth return $sth; } +# pass cmd to db, ignore response sub cmd { my ($self, @args)=@_; @@ -452,6 +466,7 @@ sub cmd $self->cmd_sth(@args); } +# return one row sub cmd_onerow { my ($self, @args)=@_; @@ -459,6 +474,7 @@ sub cmd_onerow return($sth->fetchrow_array()); } +# returns all rows sub cmd_rows { my ($self, @args)=@_; @@ -466,12 +482,14 @@ sub cmd_rows return $sth->fetchall_arrayref(); } +# returns just the first column sub cmd_firstcol { my ($self, @args)=@_; return(map { $_->[0] } @{$self->cmd_rows(@args)}); } +# runs cmd, returns id of last insert sub cmd_id { my ($self, @args)=@_; @@ -485,6 +503,7 @@ sub last_insert_id return $self->{dbh}->last_insert_id("","","",""); } +# lookup id of $name in $table, also matching on $parent if needed sub lookup_id { my($self, $table, $name, $parent)=@_;