X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FDB.pm;h=50d904378782d71f7278a965e0f21f5d8d644c27;hb=6dabdf427ad2980410665482e096c5ab3fc24a0e;hp=f9ae1dd1954fc616fd04c1e0c8f726fca5f24650;hpb=e62dd7f2ced7805305933c97f2254883a45c3b83;p=id3fs.git diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index f9ae1dd..50d9043 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -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,21 +16,18 @@ sub new my $self={}; bless($self,$class); - my($dir, $init, $me)=@_; - $self->{dbpath}="$dir/$dbfile"; - $self->{me}=$me; + $self->{me}=shift; + $self->{verbose}=shift; + my $init=shift; + my $dbpath=shift; + $self->{base}=shift; + my $fallbackdir=shift; - my $exists=-f $self->{dbpath}; + $dbpath=$self->find_db($init, $dbpath, $fallbackdir); + return undef unless($dbpath); + $self->{absbase}=Cwd::abs_path($self->{base}); - $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 $connectstr="dbi:SQLite:dbname=$dbpath"; my ($user, $pass)=("", ""); if($self->{postgres}) { @@ -37,25 +35,67 @@ sub new $user="ianb"; $pass="foo"; } + my $exists=-f $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; } +sub find_db +{ + my($self, $init, $dbpath, $fallbackdir)=@_; + my $file=undef; + my $base=undef; + if(defined($dbpath)) + { + $file=$dbpath; + } + if(defined ($self->{base})) + { + $file="$self->{base}/$dbfile" unless defined($file); + $base=$self->{base}; + } + elsif(defined($fallbackdir) && -d $fallbackdir) + { + my $path=Cwd::abs_path($fallbackdir); + do + { + $file="$path/$dbfile"; + $base=$path; + $path=~s/(.*)\/.*/$1/; + } + while(! -f $file && length($path) && -d $path); + if(! -f $file) + { + $file="$fallbackdir/$dbfile"; + $base=$fallbackdir; + } + } + if(!-f $file && !$init) + { + print "$self->{me}: db not found at $file\n"; + return undef; + } + $self->{base}=$base; + return $file; +} + +sub base_dir { return shift->{base}; } + sub create { my($self,$name)=@_; @@ -69,7 +109,21 @@ sub create { $self->cmd("CREATE SEQUENCE seq"); } - $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION); + 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, last_update) VALUES (?, ?)", + $SCHEMA_VERSION, time()); } sub checkschema @@ -84,6 +138,32 @@ sub checkschema } } +sub analyze +{ + my $self=shift; + $self->cmd("ANALYZE"); +} + +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)=@_; @@ -100,34 +180,52 @@ sub cmd_sth sub tags { - my($self, @constraints)=@_; + my($self, $path)=@_; + my @constraints=@{$path->{elements}}; if(!@constraints) # / { - my $sql="SELECT DISTINCT name FROM tags;"; + # FIXME: add ALL? + my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';"; my $tags=$self->cmd_rows($sql); return(map { $_->[0]; } @$tags); } - my @file_ids=(); - my @tag_ids=(); - my $main_sql_start=("SELECT DISTINCT tags.name FROM files\n" . - "INNER JOIN files_x_tags ON files.id=files_x_tags.files_id\n" . - "INNER JOIN tags ON tags.id=files_x_tags.tags_id\n" . - "WHERE files.id in (" . - ("\tSELECT DISTINCT files.id FROM files\n" . - "\tINNER JOIN files_x_tags ON files.id=files_x_tags.files_id\n" . - "\tINNER JOIN tags ON tags.id=files_x_tags.tags_id\n" . - "\tWHERE tags.id in (")); - my $main_sql_mid=")\n) AND tags.id NOT IN ("; - my $main_sql_end=")\n"; - while(my $constraint=shift @constraints) - { - print "CONSTRAINT: $constraint->{name}\n"; - my $cid=$constraint->{id}; - push(@tag_ids, $cid); - } - my $sql = ($main_sql_start . join(", ", @tag_ids) . - $main_sql_mid . join(", ", @tag_ids) . - $main_sql_end); + my @ids=(); + + my $sql=("SELECT t2.name FROM (\n" . + $self->tags_subselect(@constraints) . + ") AS subselect\n" . + "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" . + "INNER JOIN tags t2 ON files_x_tags.tags_id=t2.id\n"); + my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); + my @tags=@$tags; + my @tags_vals=@$tags_vals;; + my @orclauses=(); + my @andclauses=(); + use Data::Dumper; +# print "TAGS: ", Dumper \@tags; +# print "VALS: ", Dumper \@tags_vals; + + push(@andclauses, "( t2.parents_id=" . (defined($parent) ? $parent : "''") . " )"); + if(@tags) + { + push(@orclauses, "( t2.id NOT IN ( " . join(', ', @tags) ." ) )"); + } + for my $pair (@tags_vals) + { + my($tag, $val)=@$pair; +# push(@orclauses, "( NOT ( t2.parents_id=$tag AND t2.id=$val ) )"); + push(@andclauses, "( NOT ( t2.id=$tag ) )"); + } + if(@orclauses) + { + push(@andclauses, join("\n\tOR ", @orclauses)); + } + if(@andclauses) + { + $sql .= "\tWHERE\n\t\t"; + $sql .= join("\n\tAND ", @andclauses) . "\n"; + } + $sql .= "GROUP BY t2.name;"; print "SQL: $sql\n"; my $result=$self->cmd_rows($sql); my @tagnames=map { $_->[0]; } @$result; @@ -137,42 +235,430 @@ 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 name FROM tags\n" . + "WHERE parents_id=?"); + my $tags=$self->cmd_rows($sql, $tagid); + my @tags=map { $_->[0]; } @$tags; + @tags=map { length($_) ? $_ : "NOVALUE"; } @tags; + return @tags; +} + +sub artists +{ + my($self, $path)=@_; + my @constraints=@{$path->{elements}}; + if(!@constraints) # /ALL + { + my $sql="SELECT DISTINCT name FROM artists;"; + my $tags=$self->cmd_rows($sql); + return(map { $_->[0]; } @$tags); + } + my @ids=(); + my $sql=("SELECT artists.name FROM (\n" . + $self->tags_subselect(@constraints) . + ") AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n" . + "INNER JOIN artists ON files.artists_id=artists.id\n" . + "GROUP BY artists.name;"); + 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, $path)=@_; + my @constraints=@{$path->{elements}}; + my @ids=(); + # FIXME: rework PathElements + if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist") + { + return $self->artist_albums($constraints[$#constraints]->{id}, $path); + } + my $sql=("SELECT albums.name\n" . + "\tFROM (\n" . + $self->tags_subselect(@constraints) . + "\t) AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n" . + "INNER JOIN albums ON files.albums_id=albums.id\n" . + "GROUP BY albums.name;"); + print "SQL(ALBUMS): \n$sql\n"; + 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, $path)=@_; + my @constraints=@{$path->{elements}}; + my $sql=("SELECT albums.name FROM (\n" . + $self->tags_subselect(@constraints) . + "\t) AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n" . + "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 artist_tracks +{ + my($self, $artist_id, $path)=@_; + my @constraints=@{$path->{elements}}; + my $sql=("SELECT files.name FROM (\n" . + $self->tags_subselect(@constraints) . + "\t) AS subselect\n" . + "INNER JOIN files ON subselect.files_id=files.id\n" . + "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; + 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; + print "TRACKS: ", join(', ', @names), "\n"; + return(@names); +} + +sub tracks +{ + my($self, $path)=@_; + my @constraints=@{$path->{elements}}; + # FIXME: rework PathElements + if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist") + { + return $self->artist_tracks($constraints[$#constraints]->{id}, @constraints); + } + 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 $sql=("SELECT files.name\n" . + "\tFROM (\n" . + $self->tags_subselect(@constraints) . + "\t) AS subselect\n" . + "INNER JOIN files ON files.id=subselect.files_id\n" . + "GROUP BY files.name;"); + print "SQL: $sql\n"; + my $result=$self->cmd_rows($sql); + my @names=map { $_->[0]; } @$result; + print "TRACKS: ", join(', ', @names), "\n"; + return(@names); +} + +sub filename +{ + my($self, $mountpoint, $path)=@_; + my @constraints=@{$path->{elements}}; + 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); + my $id3fs_path=join('/', map { $_->{name}; } @constraints); + return($self->relativise($path, $name, $mountpoint, $id3fs_path)); + } + die("DB::filename: unhandled case\n"); #FIXME +} + +sub tags_subselect +{ + return shift->tags_subselect_and(@_); +} + + +sub tags_subselect_and_not +{ + my($self,@constraints)=@_; + my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); + my @tags=@$tags; + my @tags_vals=@$tags_vals;; + my $cnt=1; + my @andclauses=(); + my $sql=''; + for my $tag (@tags) + { + if($cnt == 1) + { + $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n"; + push(@andclauses, "\t\tfxt${cnt}.tags_id=$tag"); + } + else + { + $sql .= ("\tLEFT JOIN files_x_tags fxt$cnt ON fxt" . + ($cnt-1) . ".files_id=fxt${cnt}.files_id\n"); + push(@andclauses, "\t\tfxt${cnt}.tags_id IS NULL"); + } + print "AND: @andclauses\n"; + $cnt++; + } + if(@andclauses) + { + $sql .= "\tWHERE\n\t\t"; + $sql .= join(" AND\n\t\t", @andclauses) . "\n"; + } + $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n"; + return $sql; } -sub tag_id + +sub tags_subselect_and { - my($self, $tag)=@_; - my $sql='SELECT id FROM tags WHERE name=?'; - my ($id)=$self->cmd_onerow($sql, $tag); + my($self,@constraints)=@_; + my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); + my @tags=@$tags; + my @tags_vals=@$tags_vals;; + my $cnt=1; + my @andclauses=(); + my $sql=''; + for my $tag (@tags) + { + if($cnt == 1) + { + $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n"; + } + else + { + $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" . + ($cnt-1) . ".files_id=fxt${cnt}.files_id\n"); + } + push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag"); + print "AND: @andclauses\n"; + $cnt++; + } + if(@andclauses) + { + $sql .= "\tWHERE\n\t\t"; + $sql .= join(" AND\n\t\t", @andclauses) . "\n"; + } + $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n"; + return $sql; +} + +sub tags_subselect_or +{ + my($self,@constraints)=@_; + my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints); + my @tags=@$tags; + my @tags_vals=@$tags_vals;; + + my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" . + "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n"); + my @orclauses=(); + my @andclauses=(); + # FIXME: and / or? + if(@tags) + { + push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )"); + push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )"); + } + for my $pair (@tags_vals) + { + my($tag, $val)=@$pair; + push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )"); + } +# push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )"); + if(@orclauses) + { + push(@andclauses, join("\n\t\tOR ", @orclauses)); + } + if(@andclauses) + { + $sql .= "\tWHERE\n\t\t"; + $sql .= join("\n\t\tAND ", @andclauses) . "\n"; + } + $sql .= "\tGROUP BY files_x_tags.files_id\n"; + return $sql; +} + +sub constraints_tag_list +{ + my($self, @constraints)=@_; + my $lasttag=undef; + my @tags=(); + my @tags_vals=(); + for my $constraint (@constraints) + { +# print ref($constraint), ": ", $constraint->{name}, "\n"; + if(ref($constraint) eq "ID3FS::PathElement::Tag") + { + if(defined($lasttag)) + { +# print "TAGVAL\n"; + push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id}); + $lasttag=undef; + } + elsif($self->tag_has_values($constraint->{id})) + { +# print "HASVALUES\n"; + $lasttag=$constraint->{id} if defined($constraint->{id}); + } + else + { +# print "NOVALUES\n"; + push(@tags, $constraint->{id}) if(defined($constraint->{id})); + } + } + } + unless($self->{postgres}) + { + @tags=map{ "\"$_\""; } @tags; + @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); + $lasttag="\"$lasttag\"" if defined($lasttag); + } + return(\@tags, \@tags_vals, $lasttag); +} + + +sub relativise +{ + my($self, $path, $name, $mountpoint, $id3fs_path)=@_; + $id3fs_path=~s/(.*)\/.*/$1/; + my $rpath="$self->{absbase}/$path"; + my $vpath="$mountpoint/$id3fs_path"; + my @path=split(/\//,$rpath); + my @rel=split(/\//,$vpath); + #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); + $result .= "/$name"; + return $result; +} + +sub bare_tags +{ + my($self)=@_; + my $sql=("SELECT tags.name FROM tags\n" . + "WHERE tags.parents_id=''\n" . + "GROUP BY tags.name\n"); + my $result=$self->cmd_rows($sql); + my @names=map { $_->[0]; } @$result; + return (@names); +} + +sub tags_with_values +{ + # FIXME: only shows one level of tag depth + my($self)=@_; + 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) + { + push(@{$tags->{$pair->[0]}}, $pair->[1]); + } + return $tags; +} + +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($relpath !~ /\//) + { + $pathpart=''; + $filepart=$relpath; + } + else + { + ($pathpart, $filepart) = ($relpath =~ /(.*)\/(.*)/); + } + my $file=ID3FS::AudioFile->new($path, $self->{me}); 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 $tags=$file->tags(); + 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); - for my $tag (keys %$tags) + $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)) + { + print "$self->{me}: $path: no album tag defined\n"; + } + + 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 (@tags) { - $self->add_tag($file_id, $tag, $tags->{$tag}); + $self->add_tag($file_id, @$tag); } if($self->ok($year)) @@ -183,6 +669,7 @@ sub add $self->add_tag($file_id, "decade", "${1}0s"); } } + if($self->ok($v1genre)) { $self->add_tag($file_id, "v1genre", $v1genre); @@ -192,29 +679,23 @@ 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 { - my($self, $file_id, $tag, $val)=@_; - my $tag_id=$self->add_to_table("tags", $tag); + my($self, $file_id, $tag, $value)=@_; + my $tag_id=$self->add_to_table("tags", $tag, + { "parents_id" => undef }); $self->add_relation("files_x_tags", { "files_id" => $file_id, "tags_id" => $tag_id }); - if(defined($val)) + if(defined($value) && length($value)) { - my $val_id=$self->add_to_table("tagvals", $val); - $self->add_relation("tags_x_tagvals", - { "tags_id" => $tag_id, - "tagvals_id" => $val_id }); + my $val_id=$self->add_to_table("tags", $value, + { "parents_id" => $tag_id }); + $self->add_relation("files_x_tags", + { "files_id" => $file_id, + "tags_id" => $val_id }); } } @@ -260,6 +741,91 @@ sub lookup_id return $id; } +sub tag_has_values +{ + my($self, $id)=@_; + my $sql=("SELECT COUNT(*) FROM tags\n\t" . + "WHERE tags.parents_id=?\n"); + my ($rows)=$self->cmd_onerow($sql, $id); + return $rows; +} + +sub files_in +{ + my ($self, $dir)=@_; + $dir=~s/^$self->{base}\/?//; + my $sql=("SELECT files.name FROM files\n" . + "INNER JOIN paths ON files.paths_id=paths.id\n" . + "WHERE paths.name=?\n"); + my $files=$self->cmd_rows($sql, $dir); + return(map { $_->[0]; } @$files); +} + +sub prune_directories +{ + my($self)=@_; + my $sql=("SELECT name, id FROM paths ORDER BY name\n"); + my $pathsref=$self->cmd_rows($sql); + my @ids=(); + for my $pathpair (@$pathsref) + { + my($path, $id)=@$pathpair; + my $fullpath="$self->{absbase}/$path"; + unless(-d $fullpath) + { + push(@ids, $id) + } + } + $self->prune_paths(@ids); + return scalar(@ids); +} + +sub prune_paths +{ + my($self, @ids)=@_; + 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); +} + +sub remove_unused +{ + my($self)=@_; + my $sql=<<'EOT'; + DELETE FROM artists WHERE id IN ( + SELECT artists.id FROM artists + LEFT JOIN files ON files.artists_id=artists.id + WHERE files.id IS NULL); + + DELETE FROM albums WHERE id IN ( + SELECT albums.id FROM albums + LEFT JOIN files ON files.albums_id=albums.id + WHERE files.id IS NULL); + + DELETE FROM paths WHERE id IN ( + SELECT paths.id FROM paths + LEFT JOIN files ON files.paths_id=paths.id + WHERE files.id IS NULL); + + DELETE FROM files_x_tags WHERE files_id IN ( + SELECT files_x_tags.files_id FROM files_x_tags + LEFT JOIN files ON files.id=files_x_tags.files_id + WHERE files.id IS NULL); + + DELETE FROM tags WHERE id IN ( + SELECT tags.id FROM tags + LEFT JOIN files_x_tags ON files_x_tags.tags_id=tags.id + WHERE files_x_tags.files_id IS NULL); + + VACUUM +EOT + print "SQL: $sql\n"; + my @sql=split(/\n\n/, $sql); + $self->cmd($_) for (@sql); +} + sub relation_exists { my ($self, $relname, $fields)=@_; @@ -279,7 +845,7 @@ sub relation_exists sub ok { my($self, $thing)=@_; - return(defined($thing) && length($thing)); + return(defined($thing) && length($thing) && $thing =~ /\S+/); } sub cmd @@ -327,10 +893,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 ); @@ -345,32 +912,27 @@ CREATE TABLE albums ( name text ); -CREATE TABLE tags ( +CREATE TABLE files ( id INTEGER PRIMARY KEY, - name text + 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 tagvals ( +CREATE TABLE tags ( id INTEGER PRIMARY KEY, + parents_id INTEGER, name text ); CREATE TABLE files_x_tags ( files_id INTEGER, - tags_id INTEGER -); - -CREATE TABLE tags_x_tagvals ( tags_id INTEGER, - tagvals_id INTEGER -); - -CREATE TABLE files_x_artists ( - files_id INTEGER, - artists_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 artists_x_albums ( - artists_id INTEGER, - albums_id INTEGER -);