+ my ($self, $dir)=@_;
+ my $sql=("SELECT files.name FROM files\n" .
+ "INNER JOIN paths ON files.paths_id=paths.id\n" .
+ "WHERE paths.name=?\n");
+ return($self->cmd_firstcol($sql, $dir));
+}
+
+sub unindex
+{
+ my($self, $path, $file)=@_;
+ my $sql=("DELETE FROM files WHERE id IN (" .
+ "\tSELECT files.id FROM files\n" .
+ "\tINNER JOIN paths ON paths.id=files.paths_id\n" .
+ "\tWHERE paths.name=? and files.name=? )\n");
+ $self->cmd_rows($sql, $path, $file);
+}
+
+
+sub prune_directories
+{
+ my($self)=@_;
+ my $sql=("SELECT name, id FROM paths\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)");
+ $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);