id3fs-index -l: bare tags: don't display tags with children (eg year)
[id3fs.git] / lib / ID3FS / DB.pm
index 14f55c5..5821b53 100644 (file)
@@ -1,3 +1,19 @@
+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
 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
@@ -134,9 +152,10 @@ sub last_update
 sub bare_tags
 {
     my($self)=@_;
-    my $sql=("SELECT tags.name FROM tags\n" .
-            "WHERE tags.parents_id=''\n" .
-            "GROUP BY tags.name\n");
+    my $sql=("SELECT t1.name FROM tags t1\n" .
+            "LEFT JOIN tags t2 on t1.id=t2.parents_id\n" .
+            "WHERE t1.parents_id='' AND t2.id IS NULL\n" .
+            "GROUP BY t1.name\n");
     my @names=$self->cmd_firstcol($sql);
     return (@names);
 }
@@ -147,7 +166,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 +186,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);
@@ -224,13 +234,17 @@ sub add
     my $audiotype=$file->audiotype();
     my @tags=$file->tags();
     my $haspic=$file->haspic();
+    my $channels=$file->channels();
+    my $bitrate=$file->bitrate();
+    my $samplerate=$file->samplerate();
 
     $artist=undef unless($self->ok($artist));
     print "$self->{me}: $path: no artist tag defined\n" unless(defined($artist));
     my $artist_id=$self->add_to_table("artists",  $artist);
     my $path_id=$self->add_to_table("paths", $pathpart);
     $album=undef unless($self->ok($album));
-    if($self->{verbose} && !defined($album))
+#    if($self->{verbose} && !defined($album)) FIXME
+    if(!defined($album))
     {
        print "$self->{me}: $path: no album tag defined\n";
     }
@@ -240,12 +254,20 @@ sub add
                                    { "artists_id" => $artist_id,
                                      "albums_id"  => $albums_id,
                                      "paths_id"   => $path_id });
-    for my $tag (@tags)
+    if(@tags)
     {
-       $self->add_tag($file_id, @$tag);
+       for my $tag (@tags)
+       {
+           $self->add_tag($file_id, @$tag);
+       }
+    }
+    else
+    {
+       $self->add_tag($file_id, "UNTAGGED");
     }
 
-    $year="UNKNOWN" unless($self->ok($year));
+    $year="UNKNOWN" if(!$self->ok($year) || $year =~ /^0+$/);
+    $year+=1900 if($year=~/^\d\d$/);
     $self->add_tag($file_id, "year", $year);
     if($year=~/^(\d\d\d)\d$/)
     {
@@ -270,6 +292,32 @@ sub add
     {
        $self->add_tag($file_id, "audiotype", $audiotype);
     }
+    else
+    {
+       # should never happen
+       $self->add_tag($file_id, "audiotype", "UNKNOWN");
+    }
+
+    if($self->ok($channels))
+    {
+       if    ($channels eq "2") { $channels="stereo";  }
+       elsif ($channels eq "1") { $channels="mono";    }
+       elsif ($channels eq "0") { $channels="UNKNOWN"; }
+       $self->add_tag($file_id, "channels", $channels);
+    }
+
+    if($self->ok($bitrate))
+    {
+       $bitrate="UNKNOWN" if($bitrate=~/^0+$/);
+       $self->add_tag($file_id, "bitrate", $bitrate);
+    }
+
+    if($self->ok($samplerate))
+    {
+       $samplerate="UNKNOWN" if($samplerate=~/^0+$/);
+       $self->add_tag($file_id, "samplerate", $samplerate);
+    }
+
 }
 
 sub add_tag
@@ -334,7 +382,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));
 }
 
@@ -374,7 +421,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);
 }
 
@@ -436,6 +482,7 @@ sub ok
     return(defined($thing) && length($thing) && $thing =~ /\S+/);
 }
 
+# actually call the database
 sub cmd_sth
 {
     my($self, $sql, @params)=@_;
@@ -450,6 +497,7 @@ sub cmd_sth
     return $sth;
 }
 
+# pass cmd to db, ignore response
 sub cmd
 {
     my ($self, @args)=@_;
@@ -457,6 +505,7 @@ sub cmd
     $self->cmd_sth(@args);
 }
 
+# return one row
 sub cmd_onerow
 {
     my ($self, @args)=@_;
@@ -464,6 +513,7 @@ sub cmd_onerow
     return($sth->fetchrow_array());
 }
 
+# returns all rows
 sub cmd_rows
 {
     my ($self, @args)=@_;
@@ -471,12 +521,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)=@_;
@@ -490,6 +542,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)=@_;
@@ -514,19 +567,19 @@ CREATE TABLE id3fs (
 CREATE TABLE paths (
     id INTEGER,
     name text,
-    PRIMARY KEY(id DESC)
+    PRIMARY KEY(id ASC)
 );
 
 CREATE TABLE artists (
     id INTEGER,
     name text,
-    PRIMARY KEY(id DESC)
+    PRIMARY KEY(id ASC)
 );
 
 CREATE TABLE albums (
     id INTEGER,
     name text,
-    PRIMARY KEY(id DESC)
+    PRIMARY KEY(id ASC)
 );
 
 CREATE TABLE files (
@@ -535,7 +588,7 @@ CREATE TABLE files (
     artists_id,
     albums_id,
     paths_id,
-    PRIMARY KEY(id DESC),
+    PRIMARY KEY(id ASC),
     FOREIGN KEY(artists_id) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE,
     FOREIGN KEY(albums_id)  REFERENCES albums(id)  ON DELETE CASCADE ON UPDATE CASCADE,
     FOREIGN KEY(paths_id)   REFERENCES paths(id)   ON DELETE CASCADE ON UPDATE CASCADE
@@ -545,7 +598,7 @@ CREATE TABLE tags (
     id INTEGER,
     parents_id INTEGER,
     name text,
-    PRIMARY KEY(id DESC)
+    PRIMARY KEY(id ASC)
 );
 
 CREATE TABLE files_x_tags (