start implementing albums
[id3fs.git] / lib / ID3FS / DB.pm
index b8a2c8c..b432cea 100644 (file)
@@ -6,7 +6,6 @@ use DBI;
 use ID3FS::File;
 
 our $SCHEMA_VERSION=1;
-
 my $dbfile=".id3fs";
 
 sub new
@@ -21,10 +20,24 @@ sub new
     $self->{me}=$me;
 
     my $exists=-f $self->{dbpath};
-    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->{dbh}=DBI->connect("dbi:SQLite:dbname=$self->{dbpath}","","",
+    $self->{postgres}=0;
+
+    unless($self->{postgres})
+    {
+       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);
+    }
+
+    my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
+    my ($user, $pass)=("", "");
+    if($self->{postgres})
+    {
+       $connectstr="dbi:Pg:dbname=id3fs";
+       $user="ianb";
+       $pass="foo";
+    }
+    $self->{dbh}=DBI->connect($connectstr, $user, $pass,
                              { AutoCommit=>1 } );
     unless(defined($self->{dbh}))
     {
@@ -52,6 +65,23 @@ sub create
     {
        $self->{dbh}->do($cmd);
     }
+    if($self->{postgres})
+    {
+       $self->cmd("CREATE SEQUENCE seq");
+    }
+    else
+    {
+       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)" );
+       for my $index (keys %indexes)
+       {
+           $self->{dbh}->do("CREATE INDEX $index ON " . $indexes{$index});
+       }
+    }
     $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
 }
 
@@ -83,12 +113,221 @@ sub cmd_sth
 
 sub tags
 {
-    my($self, $path)=@_;
-    my $sql="SELECT DISTINCT name FROM tags;";
-    my $tags=$self->cmd_rows($sql);
+    my($self, @constraints)=@_;
+    if(!@constraints) # /
+    {
+       my $sql="SELECT DISTINCT name FROM tags;";
+       my $tags=$self->cmd_rows($sql);
+       return(map { $_->[0]; } @$tags);
+    }
+    my @file_ids=();
+    my @tag_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 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);
+    }
+    @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+    my $tagstr=join(", ", @tag_ids);
+    my $sql = ($main_sql_start . $tagstr .
+              $main_sql_mid   . $tagstr .
+              $main_sql_end);
+    print "SQL: $sql\n";
+    my $result=$self->cmd_rows($sql);
+    my @tagnames=map { $_->[0]; } @$result;
+    print "SUBNAMES: ", join(', ', @tagnames), "\n";
+    return(@tagnames);
+}
+
+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);
 }
 
+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 @file_ids=();
+    my @tag_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_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+                     "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+                     "\n\tGROUP BY artists.name;");
+    while(my $constraint=shift @constraints)
+    {
+       print "CONSTRAINT: $constraint->{name}\n";
+       my $cid=$constraint->{id};
+       push(@tag_ids, $cid);
+    }
+    @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+    my $tagstr=join(", ", @tag_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 @file_ids=(); # FIXME: needed? what about in artists()
+    my @tag_ids=();
+    # FIXME: rework PathElements
+    if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+    {
+       return $self->artist_albums($constraints[$#constraints]->{id});
+    }
+    return(); # FIXME
+    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_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+                     "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+                     "\n\tGROUP BY artists.name;");
+    while(my $constraint=shift @constraints)
+    {
+       print "CONSTRAINT: $constraint->{name}\n";
+       my $cid=$constraint->{id};
+       push(@tag_ids, $cid);
+    }
+    @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+    my $tagstr=join(", ", @tag_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 artist_albums
+{
+    my($self, $artist_id)=@_;
+    my $sql=("SELECT albums.name FROM artists\n\t" .
+            "INNER JOIN artists_x_albums ON artists.id=artists_x_albums.artists_id\n\t" .
+            "INNER JOIN albums ON albums.id=artists_x_albums.albums_id\n\t" .
+            "WHERE artists.id=?\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 artist_tracks
+{
+    my($self, $artist_id)=@_;
+    my $sql=("SELECT files.name FROM artists\n\t" .
+            "INNER JOIN artists_x_files ON artists.id=files_x_artists.artists_id\n\t" .
+            "INNER JOIN files ON files.id=files_x_artists.files_id\n\t" .
+            "WHERE artists.id=?\n\t" .
+            "GROUP BY files.name\n");
+    print "ARTIST_TRACKS SQL: $sql\n";
+    my $result=$self->cmd_rows($sql, $artist_id);
+    my @albums=map { $_->[0]; } @$result;
+    print "ALBUMS: ", join(', ', @albums), "\n";
+    return(@albums);
+}
+
+sub album_tracks
+{
+    # FIXME: need albums_x_files table
+    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" .
+            "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";
+    my $result=$self->cmd_rows($sql, $album_id);
+    my @tracks=map { $_->[0]; } @$result;
+    print "TRACKS: ", join(', ', @tracks), "\n";
+    return(@tracks);
+}
+
+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")
+    {
+       # FIXME
+       return(());
+#      return $self->album_tracks($constraints[$#constraints]->{id});
+    }
+    return(); # FIXME
+    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_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+                     "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+                     "\n\tGROUP BY artists.name;");
+    my @tag_ids;
+    while(my $constraint=shift @constraints)
+    {
+       print "CONSTRAINT: $constraint->{name}\n";
+       my $cid=$constraint->{id};
+       push(@tag_ids, $cid);
+    }
+    @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+    my $tagstr=join(", ", @tag_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 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)=@_;
@@ -160,6 +399,7 @@ sub add_to_table
     unless(defined($id))
     {
        my $sql="INSERT INTO $table (";
+       $sql .= "id, " if($self->{postgres});
        my @fields=qw(name);
        if(defined($extradata))
        {
@@ -167,6 +407,7 @@ sub add_to_table
        }
        $sql .= join(", ", @fields);
        $sql .=") VALUES (";
+       $sql .=") nextval('seq'), " if($self->{postgres});
        $sql .= join(", ", map { "?"; } @fields);
        $sql .= ");";
        $id=$self->cmd_id($sql, $name, map { $extradata->{$_} || ""; } sort keys %$extradata);
@@ -246,56 +487,64 @@ sub cmd_id
 sub last_insert_id
 {
     my $self=shift;
-    return $self->{dbh}->last_insert_id("","","","");
+    if($self->{postgres})
+    {
+       return $self->{dbh}->last_insert_id(undef, undef, undef, undef,
+                                           { sequence => "seq" });
+    }
+    else
+    {
+       return $self->{dbh}->last_insert_id("","","","");
+    }
 }
 
 __DATA__
 
 CREATE TABLE id3fs (
-    schema_version
+    schema_version INTEGER
 );
 
 CREATE TABLE files (
     id INTEGER PRIMARY KEY,
-    name
+    name text
 );
 
 CREATE TABLE artists (
     id INTEGER PRIMARY KEY,
-    name
+    name text
 );
 
 CREATE TABLE albums (
     id INTEGER PRIMARY KEY,
-    name
+    name text
 );
 
 CREATE TABLE tags (
     id INTEGER PRIMARY KEY,
-    name
+    name text
 );
 
 CREATE TABLE tagvals (
     id INTEGER PRIMARY KEY,
-    name
+    name text
 );
 
 CREATE TABLE files_x_tags (
-    files_id,
-    tags_id
+    files_id INTEGER,
+    tags_id INTEGER
 );
 
 CREATE TABLE tags_x_tagvals (
-    tags_id,
-    tagvals_id
+    tags_id INTEGER,
+    tagvals_id INTEGER
 );
 
 CREATE TABLE files_x_artists (
-    files_id,
-    artists_id
+    files_id INTEGER,
+    artists_id INTEGER
 );
 
 CREATE TABLE artists_x_albums (
-    artists_id,
-    albums_id
+    artists_id INTEGER,
+    albums_id INTEGER
 );