filter constraint ids, drop undefs
[id3fs.git] / lib / ID3FS / DB.pm
index 01b41d8..8794ca1 100644 (file)
@@ -3,7 +3,8 @@ package ID3FS::DB;
 use strict;
 use warnings;
 use DBI;
-use ID3FS::File;
+use ID3FS::AudioFile;
+use Cwd;
 
 our $SCHEMA_VERSION=1;
 my $dbfile=".id3fs";
@@ -15,19 +16,19 @@ sub new
     my $self={};
     bless($self,$class);
 
-    my($dir, $init, $me)=@_;
-    $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)=("", "");
@@ -37,22 +38,23 @@ 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();
     }
-
+    $self->enable_foreign_keys();
     return $self;
 }
 
@@ -71,18 +73,19 @@ sub create
     }
     else
     {
-       my %indexes=( "idx_files_id"  => "files(id)",
-                     "idx_fxt_both"  => "files_x_tags(files_id, tags_id)",
+       my %indexes=( "idx_files_id"  => "files (id)",
+                     "idx_fxt_both"  => "files_x_tags (files_id, tags_id)",
                      "idx_fxt_files" => "files_x_tags (files_id)",
                      "idx_fxt_tags"  => "files_x_tags (tags_id)",
                      "idx_tags_id"   => "tags (id)",
-                     "idx_tags_name" => "tags(name)" );
+                     "idx_tags_name" => "tags (name)");
        for my $index (keys %indexes)
        {
            $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
@@ -97,6 +100,26 @@ sub checkschema
     }
 }
 
+sub enable_foreign_keys
+{
+    my $self=shift;
+    $self->cmd("PRAGMA foreign_keys = ON");
+}
+
+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)=@_;
@@ -116,30 +139,29 @@ sub tags
     my($self, @constraints)=@_;
     if(!@constraints) # /
     {
+       # FIXME: add ALL?
        my $sql="SELECT DISTINCT name FROM tags;";
        my $tags=$self->cmd_rows($sql);
        return(map { $_->[0]; } @$tags);
     }
-    my @file_ids=();
-    my @tag_ids=();
+    my @ids=();
 
     my $main_sql_start=("SELECT t2.name\n" .
                        "\tFROM (SELECT files_id FROM tags t1\n" .
                        "\t\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n" .
-                       "\t\tWHERE t1.id in \n\t\t\t(");
-    my $main_sql_mid=(")\n\t\t) AS fxt1\n" .
-                     "\tINNER JOIN files_x_tags fxt2 ON fxt1.files_id=fxt2.files_id\n" .
-                     "\tINNER JOIN tags t2 ON fxt2.tags_id=t2.id\n" .
+                       "\t\tWHERE t1.id in\n\t\t\t(");
+    my $main_sql_mid=(")\n\t\t) AS subselect\n" .
+                     "\tINNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
+                     "\tINNER JOIN tags t2 ON files_x_tags.tags_id=t2.id\n" .
                      "\tWHERE t2.id NOT IN (");
     my $main_sql_end=")\n\tGROUP BY t2.name;";
     while(my $constraint=shift @constraints)
     {
-       print "CONSTRAINT: $constraint->{name}\n";
        my $cid=$constraint->{id};
-       push(@tag_ids, $cid);
+       push(@ids, $cid);
     }
-    @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
-    my $tagstr=join(", ", @tag_ids);
+    @ids = map( { "\"$_\""; } grep { defined; } @ids) unless($self->{postgres});
+    my $tagstr=join(", ", @ids);
     my $sql = ($main_sql_start . $tagstr .
               $main_sql_mid   . $tagstr .
               $main_sql_end);
@@ -152,39 +174,233 @@ sub tags
 
 sub tag_values
 {
-    my($self, $tag)=@_;
-    my $sql=("SELECT DISTINCT tagvals.name FROM tags\n" .
-            "INNER JOIN tags_x_tagvals ON tags.id=tags_x_tagvals.tags_id\n" .
-            "INNER JOIN tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n" .
-            "WHERE tags.name=?");
-    my $tags=$self->cmd_rows($sql, $tag);
-    return(map { $_->[0]; } @$tags);
+    my($self, $tagid)=@_;
+    my $sql=("SELECT DISTINCT tagvals.name FROM tagvals\n" .
+            "INNER JOIN tags_x_tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n" .
+            "WHERE tags_x_tagvals.tags_id=?");
+    my $tags=$self->cmd_rows($sql, $tagid);
+    my @tags=map { $_->[0]; } @$tags;
+    @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
+    return @tags;
+}
+
+sub artists
+{
+    my($self, @constraints)=@_;
+    if(!@constraints) # /ALL
+    {
+       my $sql="SELECT DISTINCT name FROM artists;";
+       my $tags=$self->cmd_rows($sql);
+       return(map { $_->[0]; } @$tags);
+    }
+    my @ids=();
+    my $main_sql_start=("SELECT artists.name\n" .
+                       "\tFROM (SELECT files_id FROM tags\n" .
+                       "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+                       "\t\tWHERE tags.id in\n\t\t\t(");
+    my $main_sql_end=(")\n\t\t) AS subselect\n" .
+                     "\tINNER JOIN files ON subselect.files_id=files.id\n" .
+                     "\tINNER JOIN artists ON files.artists_id=artists.id\n" .
+                     "\n\tGROUP BY artists.name;");
+    while(my $constraint=shift @constraints)
+    {
+       my $cid=$constraint->{id};
+       push(@ids, $cid);
+    }
+    @ids = map( { "\"$_\""; } grep { defined; } @ids) unless($self->{postgres});
+    my $tagstr=join(", ", @ids);
+    my $sql = ($main_sql_start . $tagstr .
+              $main_sql_end);
+    print "SQL: $sql\n";
+    my $result=$self->cmd_rows($sql);
+    my @tagnames=map { $_->[0]; } @$result;
+    print "ARTISTS: ", join(', ', @tagnames), "\n";
+    return(@tagnames);
+}
+
+sub albums
+{
+    my($self, @constraints)=@_;
+    my @ids=();
+    # FIXME: rework PathElements
+    if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+    {
+       return $self->artist_albums($constraints[$#constraints]->{id});
+    }
+    my $main_sql_start=("SELECT albums.name\n" .
+                       "\tFROM (SELECT files_id FROM tags\n" .
+                       "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+                       "\t\tWHERE tags.id in\n\t\t\t(");
+    my $main_sql_end=(")\n\t\t) AS subselect\n" .
+                     "\tINNER JOIN files ON subselect.files_id=files.id\n" .
+                     "\tINNER JOIN albums ON files.albums_id=albums.id\n" .
+                     "\n\tGROUP BY albums.name;");
+    while(my $constraint=shift @constraints)
+    {
+       my $cid=$constraint->{id};
+       push(@ids, $cid);
+    }
+    @ids = map( { "\"$_\""; } grep { defined; } @ids) unless($self->{postgres});
+    my $str=join(", ", @ids);
+    my $sql = ($main_sql_start . $str .
+              $main_sql_end);
+    my $result=$self->cmd_rows($sql);
+    my @names=map { $_->[0]; } @$result;
+    print "ALBUMS: ", join(', ', @names), "\n";
+    return(@names);
+}
+
+sub artist_albums
+{
+    my($self, $artist_id)=@_;
+    my $sql=("SELECT albums.name FROM files\n\t" .
+            "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
+            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+            "WHERE artists.id=? and albums.name <> ''\n\t" .
+            "GROUP BY albums.name\n");
+    print "ARTIST_ALBUMS SQL: $sql\n";
+    my $result=$self->cmd_rows($sql, $artist_id);
+    my @albums=map { $_->[0]; } @$result;
+    print "ALBUMS: ", join(', ', @albums), "\n";
+    return(@albums);
 }
 
-sub tag_id
+sub artist_tracks
 {
-    my($self, $tag)=@_;
-    my $sql='SELECT id FROM tags WHERE name=?';
-    my ($id)=$self->cmd_onerow($sql, $tag);
+    my($self, $artist_id)=@_;
+    my $sql=("SELECT files.name FROM files\n\t" .
+            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+            "INNER JOIN albums  ON albums.id=files.albums_id\n\t" .
+            "WHERE artists.id=? AND albums.name=''\n\t" .
+            "GROUP BY files.name\n");
+    print "ARTIST_TRACKS SQL: $sql\n";
+    my $result=$self->cmd_rows($sql, $artist_id);
+    my @names=map { $_->[0]; } @$result;
+    @names = map { s/.*\///; $_; } @names;
+    print "ARTISTTRACKS: ", join(', ', @names), "\n";
+    return(@names);
+}
+
+sub album_tracks
+{
+    my($self, $artist_id, $album_id)=@_;
+    my $sql=("SELECT files.name FROM files\n\t" .
+            "INNER JOIN albums  ON albums.id=files.albums_id\n\t" .
+            "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+            "WHERE artists.id=? AND albums.id=?\n\t" .
+            "GROUP BY files.name\n");
+    print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n";
+    my $result=$self->cmd_rows($sql, $artist_id, $album_id);
+    my @names=map { $_->[0]; } @$result;
+    @names = map { s/.*\///; $_;} @names;
+    print "TRACKS: ", join(', ', @names), "\n";
+    return(@names);
+}
+
+sub tracks
+{
+    my($self, @constraints)=@_;
+    # FIXME: rework PathElements
+    if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+    {
+       return $self->artist_tracks($constraints[$#constraints]->{id});
+    }
+    elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
+    {
+       my $artist_id=0;
+       my $artist=$constraints[($#constraints)-1];
+       if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
+       {
+           # should always happen
+           $artist_id=$artist->{id};
+       }
+       return $self->album_tracks($artist_id, $constraints[$#constraints]->{id});
+    }
+
+    my $main_sql_start=("SELECT files.name\n" .
+                       "\tFROM (SELECT files_id FROM tags\n" .
+                       "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+                       "\t\tWHERE tags.id in\n\t\t\t(");
+    my $main_sql_end=(")\n\t\t) AS subselect\n" .
+                     "\tINNER JOIN files ON files.id=subselect.files_id" .
+                     "\tGROUP BY files.name;");
+    my @ids;
+    while(my $constraint=shift @constraints)
+    {
+       my $cid=$constraint->{id};
+       push(@ids, $cid);
+    }
+    @ids = map( { "\"$_\""; } grep { defined; } @ids) unless($self->{postgres});
+    my $str=join(", ", @ids);
+    my $sql = ($main_sql_start . $str .
+              $main_sql_end);
+    print "SQL: $sql\n";
+    my $result=$self->cmd_rows($sql);
+    my @names=map { $_->[0]; } @$result;
+    @names = map { s/.*\///; $_; } @names;
+    print "TRACKS: ", join(', ', @names), "\n";
+    return(@names);
+}
+
+sub filename
+{
+    my($self, @constraints)=@_;
+    if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
+    {
+       my $id=$constraints[$#constraints]->{id};
+       my $sql=("SELECT paths.name, files.name FROM files\n" .
+                "INNER JOIN paths ON files.paths_id=paths.id\n" .
+                "WHERE files.id=?\n" .
+                "GROUP BY paths.name, files.name");
+       print "FILENAME SQL: $sql\n";
+       my ($path, $name)=$self->cmd_onerow($sql, $id);
+       return($self->{absbase} . "/$path/$name");
+    }
+    die("DB::filename: unhandled case\n"); #FIXME
+}
+
+sub id
+{
+    my($self, $type, $val)=@_;
+    my $sql="SELECT id FROM $type WHERE name=?";
+    my ($id)=$self->cmd_onerow($sql, $val);
     return($id);
 }
 
 sub add
 {
     my($self,$path)=@_;
-    my $file=ID3FS::File->new($path);
+    my $relpath=$path;
+    $relpath =~ s/^\Q$self->{base}\E\/?//;
+    my($filepart,$pathpart);
+    if($path !~ /\//)
+    {
+       $pathpart='';
+       $filepart=$relpath;
+    }
+    else
+    {
+       ($pathpart, $filepart) = ($relpath =~ /(.*)\/(.*)/);
+    }
+    my $file=ID3FS::AudioFile->new($path);
     return unless(defined($file));
     my $artist=$file->artist();
     my $album=$file->album();
     my $v1genre=$file->v1genre();
     my $year=$file->year();
-    my $audiotype=$file->album();
+    my $audiotype=$file->audiotype();
     my $tags=$file->tags();
     my $haspic=$file->haspic();
 
-    my $file_id=$self->add_to_table("files", $path);
-    my $artists_id=$self->add_to_table("artists",  $artist);
-    my $albums_id=$self->add_to_table("albums",  $album);
+    $artist=undef unless($self->ok($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));
+    my $albums_id=$self->add_to_table("albums", $album);
+    my $file_id=$self->add_to_table("files", $filepart,
+                                   { "artists_id" => $artist_id,
+                                     "albums_id"  => $albums_id,
+                                     "paths_id"   => $path_id });
     for my $tag (keys %$tags)
     {
        $self->add_tag($file_id, $tag, $tags->{$tag});
@@ -198,6 +414,7 @@ sub add
            $self->add_tag($file_id, "decade", "${1}0s");
        }
     }
+
     if($self->ok($v1genre))
     {
        $self->add_tag($file_id, "v1genre", $v1genre);
@@ -207,14 +424,6 @@ sub add
     {
        $self->add_tag($file_id, "haspic", undef);
     }
-
-    $self->add_relation("files_x_artists",
-                       { "files_id" => $file_id,
-                         "artists_id" => $artists_id });
-
-    $self->add_relation("artists_x_albums",
-                     { "artists_id" => $artists_id,
-                       "albums_id" => $albums_id});
 }
 
 sub add_tag
@@ -275,6 +484,17 @@ sub lookup_id
     return $id;
 }
 
+sub tag_has_values
+{
+    my($self, $id)=@_;
+    my $sql=("SELECT COUNT(*) FROM tags\n\t" .
+            "INNER JOIN tags_x_tagvals ON tags.id=tags_x_tagvals.tags_id\n\t" .
+            "INNER JOIN tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n\t" .
+            "WHERE tags.id=?\n");
+    my ($rows)=$self->cmd_onerow($sql, $id);
+    return $rows;
+}
+
 sub relation_exists
 {
     my ($self, $relname, $fields)=@_;
@@ -294,7 +514,7 @@ sub relation_exists
 sub ok
 {
     my($self, $thing)=@_;
-    return(defined($thing) && length($thing));
+    return(defined($thing) && length($thing) && $thing =~ /\S+/);
 }
 
 sub cmd
@@ -342,10 +562,11 @@ sub last_insert_id
 __DATA__
 
 CREATE TABLE id3fs (
-    schema_version INTEGER
+    schema_version INTEGER,
+    last_update
 );
 
-CREATE TABLE files (
+CREATE TABLE paths (
     id INTEGER PRIMARY KEY,
     name text
 );
@@ -360,6 +581,17 @@ CREATE TABLE albums (
     name text
 );
 
+CREATE TABLE files (
+    id INTEGER PRIMARY KEY,
+    name text,
+    artists_id,
+    albums_id,
+    paths_id,
+    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
+);
+
 CREATE TABLE tags (
     id INTEGER PRIMARY KEY,
     name text
@@ -372,20 +604,14 @@ CREATE TABLE tagvals (
 
 CREATE TABLE files_x_tags (
     files_id INTEGER,
-    tags_id INTEGER
+    tags_id INTEGER,
+    FOREIGN KEY(files_id) REFERENCES files(id) ON DELETE CASCADE ON UPDATE CASCADE,
+    FOREIGN KEY(tags_id)  REFERENCES tags(id)  ON DELETE CASCADE ON UPDATE CASCADE
 );
 
 CREATE TABLE tags_x_tagvals (
     tags_id INTEGER,
-    tagvals_id INTEGER
-);
-
-CREATE TABLE files_x_artists (
-    files_id INTEGER,
-    artists_id INTEGER
-);
-
-CREATE TABLE artists_x_albums (
-    artists_id INTEGER,
-    albums_id INTEGER
+    tagvals_id INTEGER,
+    FOREIGN KEY(tags_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE,
+    FOREIGN KEY(tagvals_id) REFERENCES tagvals(id) ON DELETE CASCADE ON UPDATE CASCADE
 );