X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FDB.pm;h=273f6dcad5ac82d05887f8767f2f4a730bbfaecb;hb=a474cec8c4e0ea282f21902dc2bffdabb82667c3;hp=246cd92dc17d351edb171773f667dbef76797fa8;hpb=6c6aa71f693295bf263f86d20dd43b5d001fbfa9;p=id3fs.git diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index 246cd92..273f6dc 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 { @@ -19,12 +35,8 @@ sub new $self->{me}=shift; $self->{verbose}=shift; my $init=shift; - my $dbpath=shift; $self->{base}=shift; - my $fallbackdir=shift; - - $self->{dbpath}=$self->find_db($init, $dbpath, $fallbackdir); - return undef unless($self->{dbpath}); + $self->{dbpath}=shift || ($self->{base} . "/" . $dbfile); $self->{absbase}=Cwd::abs_path($self->{base}); my $connectstr="dbi:SQLite:dbname=$self->{dbpath}"; @@ -48,48 +60,39 @@ sub new return $self; } +# search parent directories for db sub find_db { - my($self, $init, $dbpath, $fallbackdir)=@_; - my $file=undef; + # class method + shift if(ref($_[0]) eq "ID3FS::DB"); + + my($me, $init, @dirs)=@_; my $base=undef; - if(defined($dbpath)) - { - $file=$dbpath; - } - if(defined ($self->{base})) + for my $dir (@dirs) { - $file="$self->{base}/$dbfile" unless defined($file); - $base=$self->{base}; - } - elsif(defined($fallbackdir) && -d $fallbackdir) - { - my $path=Cwd::abs_path($fallbackdir); + my $path=Cwd::abs_path($dir); do { - $file="$path/$dbfile"; $base=$path; $path=~s/(.*)\/.*/$1/; } - while(! -f $file && length($path) && -d $path); - if(! -f $file) + while(! -f "$base/$dbfile" && length($path) && -d $path); + if(-f "$base/$dbfile") { - $file="$fallbackdir/$dbfile"; - $base=$fallbackdir; + return $base; } } - else + if(!-f "$base/$dbfile") { - print "$self->{me}: $fallbackdir: not a directory\n"; - return undef; - } - if(!-f $file && !$init) - { - print "$self->{me}: db not found at $file\n"; - return undef; + unless($init) + { + print "$me: db not found at $base/$dbfile\n"; + return undef; + } + $base=$dirs[0]; + } - $self->{base}=$base; - return $file; + return $base; } sub base_dir { return shift->{base}; } @@ -161,7 +164,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) @@ -204,9 +206,7 @@ sub relativise { shift(@path); shift(@rel); -# print "POP "; } -# print "\n"; my $upcount=scalar(@rel); my $result="../" x $upcount; $result .= join("/",@path); @@ -279,6 +279,11 @@ sub add { $self->add_tag($file_id, "haspic", undef); } + + if($self->ok($audiotype)) + { + $self->add_tag($file_id, "audiotype", $audiotype); + } } sub add_tag @@ -343,7 +348,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)); } @@ -383,7 +387,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); } @@ -445,6 +448,7 @@ sub ok return(defined($thing) && length($thing) && $thing =~ /\S+/); } +# actually call the database sub cmd_sth { my($self, $sql, @params)=@_; @@ -459,6 +463,7 @@ sub cmd_sth return $sth; } +# pass cmd to db, ignore response sub cmd { my ($self, @args)=@_; @@ -466,6 +471,7 @@ sub cmd $self->cmd_sth(@args); } +# return one row sub cmd_onerow { my ($self, @args)=@_; @@ -473,6 +479,7 @@ sub cmd_onerow return($sth->fetchrow_array()); } +# returns all rows sub cmd_rows { my ($self, @args)=@_; @@ -480,12 +487,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)=@_; @@ -499,6 +508,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)=@_;