last_update: store in id3fs table, update when index is updated,
[id3fs.git] / lib / ID3FS / DB.pm
index 784abb9..b6570ce 100644 (file)
@@ -16,21 +16,19 @@ sub new
     my $self={};
     bless($self,$class);
 
-    my($dir, $init, $me)=@_;
-    $self->{base}=$dir;
-    $self->{absbase}=Cwd::abs_path($dir);
-    $self->{dbpath}="$dir/$dbfile";
-    $self->{me}=$me;
+    $self->{me}=shift;
+    $self->{dbpath}=shift;
+    $self->{base}=shift;
+    $self->{fallbackdir}=shift;
 
-    my $exists=-f $self->{dbpath};
-
-    $self->{postgres}=0;
-
-    unless($self->{postgres})
+    if(!defined($self->{base}) &&
+       defined($self->{fallbackdir}) &&
+       -d $self->{fallbackdir})
     {
-       die("$me: $self->{dbpath}: not found. use --init to create.\n") if(!$exists && !$init);
-       die("$me: --init used but $self->{dbpath} exists.\n")           if($exists && $init);
+       $self->{base}=$self->{fallbackdir};
     }
+    $self->{dbpath}="$self->{base}/$dbfile" unless(defined($self->{dbpath}));
+    $self->{absbase}=Cwd::abs_path($self->{base});
 
     my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
     my ($user, $pass)=("", "");
@@ -40,20 +38,21 @@ sub new
        $user="ianb";
        $pass="foo";
     }
+    my $exists=-f $self->{dbpath};
     $self->{dbh}=DBI->connect($connectstr, $user, $pass,
                              { AutoCommit=>1 } );
     unless(defined($self->{dbh}))
     {
-       die("$me: DB Error: " . $DBI::errstr . "\n");
+       die("$self->{me}: DB Error: " . $DBI::errstr . "\n");
     }
 
-    if($init)
+    if($exists)
     {
-       $self->create();
+       $self->checkschema();
     }
     else
     {
-       $self->checkschema();
+       $self->create();
     }
 
     return $self;
@@ -85,7 +84,8 @@ sub create
            $self->{dbh}->do("CREATE INDEX $index ON " . $indexes{$index});
        }
     }
-    $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
+    $self->cmd("INSERT INTO id3fs (schema_version, last_update) VALUES (?, ?)",
+              $SCHEMA_VERSION, time());
 }
 
 sub checkschema
@@ -100,6 +100,20 @@ sub checkschema
     }
 }
 
+sub last_update
+{
+    my($self, $newval)=@_;
+    if(defined($newval))
+    {
+       $self->cmd("UPDATE id3fs SET last_update=?", $newval);
+    }
+    else
+    {
+       ($newval)=$self->cmd_onerow("SELECT last_update from id3fs");
+    }
+    return $newval;
+}
+
 sub cmd_sth
 {
     my($self, $sql, @params)=@_;
@@ -266,15 +280,15 @@ sub artist_tracks
 sub album_tracks
 {
     my($self, $album_id)=@_;
-    my $sql=("SELECT files.name FROM albums\n\t" .
-            "INNER JOIN files_x_albums ON albums.id=files_x_albums.albums_id\n\t" .
+    my $sql=("SELECT files.name FROM files\n\t" .
+            "INNER JOIN files_x_albums ON files.id=files_x_albums.files_id\n\t" .
             "INNER JOIN albums ON albums.id=files_x_albums.albums_id\n\t" .
             "WHERE albums.id=?\n\t" .
             "GROUP BY files.name\n");
-    print "ALBUM_TRACKS SQL: $sql\n";
+    print "ALBUM_TRACKS SQL($album_id): $sql\n";
     my $result=$self->cmd_rows($sql, $album_id);
     my @names=map { $_->[0]; } @$result;
-    @names = map { s/.*\///; } @names;
+    @names = map { s/.*\///; $_;} @names;
     print "TRACKS: ", join(', ', @names), "\n";
     return(@names);
 }
@@ -289,9 +303,7 @@ sub tracks
     }
     elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
     {
-       # FIXME
-       return(());
-#      return $self->album_tracks($constraints[$#constraints]->{id});
+       return $self->album_tracks($constraints[$#constraints]->{id});
     }
 
     my $main_sql_start=("SELECT files.name\n" .
@@ -351,7 +363,7 @@ sub add
 {
     my($self,$path)=@_;
     my $relpath=$path;
-    $relpath =~ s/$self->{base}\/?//;
+    $relpath =~ s/^\Q$self->{base}\E\/?//;
     my($filepart,$pathpart);
     if($path !~ /\//)
     {
@@ -551,7 +563,8 @@ sub last_insert_id
 __DATA__
 
 CREATE TABLE id3fs (
-    schema_version INTEGER
+    schema_version INTEGER,
+    last_update
 );
 
 CREATE TABLE files (