+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010 Ian Beckwith <ianb@erislabs.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
package ID3FS::DB;
use strict;
use Cwd;
our $SCHEMA_VERSION=1;
-my $dbfile=".id3fs";
+my $dbfile=".id3fs"; # default
sub new
{
$self->{me}=shift;
$self->{verbose}=shift;
my $init=shift;
- my $dbpath=shift;
$self->{base}=shift;
- my $fallbackdir=shift;
-
- $dbpath=$self->find_db($init, $dbpath, $fallbackdir);
- return undef unless($dbpath);
+ $self->{dbpath}=shift || ($self->{base} . "/" . $dbfile);
+ $self->{dbpath}=Cwd::abs_path($self->{dbpath});
$self->{absbase}=Cwd::abs_path($self->{base});
- my $connectstr="dbi:SQLite:dbname=$dbpath";
- my ($user, $pass)=("", "");
- if($self->{postgres})
- {
- $connectstr="dbi:Pg:dbname=id3fs";
- $user="ianb";
- $pass="foo";
- }
- my $exists=-f $dbpath;
- $self->{dbh}=DBI->connect($connectstr, $user, $pass,
+ my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
+ my $exists=-f $self->{dbpath};
+ $self->{dbh}=DBI->connect($connectstr, undef, undef,
{ AutoCommit=>1 } );
unless(defined($self->{dbh}))
{
return $self;
}
+# search parent directories for db
sub find_db
{
- my($self, $init, $dbpath, $fallbackdir)=@_;
- my $file=undef;
+ # class method
+ shift if(ref($_[0]) eq "ID3FS::DB");
+
+ my($me, $init, @dirs)=@_;
my $base=undef;
- if(defined($dbpath))
- {
- $file=$dbpath;
- }
- if(defined ($self->{base}))
+ for my $dir (@dirs)
{
- $file="$self->{base}/$dbfile" unless defined($file);
- $base=$self->{base};
- }
- elsif(defined($fallbackdir) && -d $fallbackdir)
- {
- my $path=Cwd::abs_path($fallbackdir);
+ my $path=Cwd::abs_path($dir);
do
{
- $file="$path/$dbfile";
$base=$path;
$path=~s/(.*)\/.*/$1/;
}
- while(! -f $file && length($path) && -d $path);
- if(! -f $file)
+ while(! -f "$base/$dbfile" && length($path) && -d $path);
+ if(-f "$base/$dbfile")
{
- $file="$fallbackdir/$dbfile";
- $base=$fallbackdir;
+ return $base;
}
}
- if(!-f $file && !$init)
+ if(!-f "$base/$dbfile")
{
- print "$self->{me}: db not found at $file\n";
- return undef;
+ unless($init)
+ {
+ print "$me: db not found at $base/$dbfile\n";
+ return undef;
+ }
+ $base=$dirs[0];
+
}
- $self->{base}=$base;
- return $file;
+ return $base;
}
sub base_dir { return shift->{base}; }
{
$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, last_update) VALUES (?, ?)",
$SCHEMA_VERSION, time());
}
return $newval;
}
-sub cmd_sth
-{
- my($self, $sql, @params)=@_;
- my $sth=$self->{dbh}->prepare($sql);
- my $idx=1;
- for my $param (@params)
- {
- $param="" unless(defined($param));
- $sth->bind_param($idx++, $param);
- }
- $sth->execute();
- return $sth;
-}
-
-sub tags
-{
- my($self, $path)=@_;
- my @constraints=@{$path->{elements}};
- if(!@constraints) # /
- {
- # FIXME: add ALL?
- my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
- my $tags=$self->cmd_rows($sql);
- return(map { $_->[0]; } @$tags);
- }
- my @ids=();
-
- my $sql=("SELECT t2.name FROM (\n" .
- $self->tags_subselect($path) .
- ") 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 @used=$path->used_tags();
- print "tags(): USED: ", join(", ", @used), "\n";
- if(@used)
- {
- $sql .= "WHERE t2.id NOT IN (";
- $sql .= join(', ', @used);
- $sql .= ")\n";
- }
- $sql .= "GROUP BY t2.name;";
- 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, $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($path) .
- ") 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($path) .
- "\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($path) .
- "\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($path) .
- "\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
+sub bare_tags
{
- 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);
+ my($self)=@_;
+ my $sql=("SELECT t1.name FROM tags t1\n" .
+ "LEFT JOIN tags t2 on t1.id=t2.parents_id\n" .
+ "WHERE t1.parents_id='' AND t2.id IS NULL\n" .
+ "GROUP BY t1.name\n");
+ my @names=$self->cmd_firstcol($sql);
+ return (@names);
}
-sub tracks
+sub tags_with_values
{
- 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($path) .
- "\t) AS subselect\n" .
- "INNER JOIN files ON files.id=subselect.files_id\n" .
- "GROUP BY files.name;");
- print "SQL: $sql\n";
+ 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");
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
-{
- my($self, $path)=@_;
- my $tree=$path->{tagtree};
- my ($sqlclause, $joinsneeded)=$tree->to_sql();
- print "SQL($joinsneeded): $sqlclause\n";
- my $sql="\tSELECT fxt1.files_id FROM files_x_tags fxt1\n";
- for(my $i=2; $i <= $joinsneeded; $i++)
- {
- $sql .= ("\tINNER JOIN files_x_tags fxt$i ON " .
- "fxt1.files_id=fxt${i}.files_id\n");
- }
- $sql .= "\tWHERE $sqlclause\n";
- $sql .= "\tGROUP BY fxt${joinsneeded}.files_id\n";
- return $sql;
-}
-
-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 tags_subselect_and
-{
- 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)
+ my $tags={};
+ for my $pair (@$result)
{
- $sql .= "\tWHERE\n\t\t";
- $sql .= join("\n\t\tAND ", @andclauses) . "\n";
+ push(@{$tags->{$pair->[0]}}, $pair->[1]);
}
- $sql .= "\tGROUP BY files_x_tags.files_id\n";
- return $sql;
+ return $tags;
}
-sub constraints_tag_list
+sub tag_has_values
{
- 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);
+ 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 relativise
{
- my($self, $path, $name, $mountpoint, $id3fs_path)=@_;
- $id3fs_path=~s/(.*)\/.*/$1/;
+ my($self, $path, $name, $mountpoint, $querypath)=@_;
my $rpath="$self->{absbase}/$path";
- my $vpath="$mountpoint/$id3fs_path";
+ my $vpath=$mountpoint . $querypath;
my @path=split(/\//,$rpath);
my @rel=split(/\//,$vpath);
- #absolute paths have empty first element due to leading /
+ # drop filename from rel
+ pop @rel;
+ # 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);
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 $relpath=$path;
- $relpath =~ s/^\Q$self->{base}\E\/?//;
+ my $relpath=Cwd::abs_path($path);
+ $relpath =~ s/^\Q$self->{absbase}\E\/?//;
my($filepart,$pathpart);
if($relpath !~ /\//)
{
my $audiotype=$file->audiotype();
my @tags=$file->tags();
my $haspic=$file->haspic();
+ my $channels=$file->channels();
+ my $bitrate=$file->bitrate();
+ my $samplerate=$file->samplerate();
$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))
+# if($self->{verbose} && !defined($album)) FIXME
+ if(!defined($album))
{
print "$self->{me}: $path: no album tag defined\n";
}
{ "artists_id" => $artist_id,
"albums_id" => $albums_id,
"paths_id" => $path_id });
- for my $tag (@tags)
+ if(@tags)
{
- $self->add_tag($file_id, @$tag);
+ for my $tag (@tags)
+ {
+ $self->add_tag($file_id, @$tag);
+ }
+ }
+ else
+ {
+ $self->add_tag($file_id, "UNTAGGED");
}
- if($self->ok($year))
+ $year="UNKNOWN" if(!$self->ok($year) || $year =~ /^0+$/);
+ $year+=1900 if($year=~/^\d\d$/);
+ $self->add_tag($file_id, "year", $year);
+ if($year=~/^(\d\d\d)\d$/)
{
- $self->add_tag($file_id, "year", $year);
- if($year=~/^(\d\d\d)\d$/)
- {
- $self->add_tag($file_id, "decade", "${1}0s");
- }
+ $self->add_tag($file_id, "decade", "${1}0s");
+ }
+ else
+ {
+ $self->add_tag($file_id, "decade", "UNKNOWN");
}
if($self->ok($v1genre))
{
$self->add_tag($file_id, "haspic", undef);
}
+
+ if($self->ok($audiotype))
+ {
+ $self->add_tag($file_id, "audiotype", $audiotype);
+ }
+ else
+ {
+ # should never happen
+ $self->add_tag($file_id, "audiotype", "UNKNOWN");
+ }
+
+ if($self->ok($channels))
+ {
+ if ($channels eq "2") { $channels="stereo"; }
+ elsif ($channels eq "1") { $channels="mono"; }
+ elsif ($channels eq "0") { $channels="UNKNOWN"; }
+ $self->add_tag($file_id, "channels", $channels);
+ }
+
+ if($self->ok($bitrate))
+ {
+ $bitrate="UNKNOWN" if($bitrate=~/^0+$/);
+ $self->add_tag($file_id, "bitrate", $bitrate);
+ }
+
+ if($self->ok($samplerate))
+ {
+ $samplerate="UNKNOWN" if($samplerate=~/^0+$/);
+ $self->add_tag($file_id, "samplerate", $samplerate);
+ }
+
}
sub add_tag
sub add_to_table
{
my($self, $table, $name, $extradata)=@_;
- my $id=$self->lookup_id($table, $name);
+ my $parent=undef;
+ if($extradata && $extradata->{parents_id})
+ {
+ $parent=$extradata->{parents_id};
+ }
+ my $id=$self->lookup_id($table, $name, $parent);
unless(defined($id))
{
my $sql="INSERT INTO $table (";
- $sql .= "id, " if($self->{postgres});
my @fields=qw(name);
if(defined($extradata))
{
}
$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);
$self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
}
-sub lookup_id
-{
- my($self, $table, $name)=@_;
- my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
- 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);
+ 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 ORDER BY name\n");
+ my $sql=("SELECT name, id FROM paths\n");
my $pathsref=$self->cmd_rows($sql);
my @ids=();
for my $pathpair (@$pathsref)
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);
}
VACUUM
EOT
- print "SQL: $sql\n";
+# print "SQL: $sql\n";
my @sql=split(/\n\n/, $sql);
$self->cmd($_) for (@sql);
}
return(defined($thing) && length($thing) && $thing =~ /\S+/);
}
+# actually call the database
+sub cmd_sth
+{
+ my($self, $sql, @params)=@_;
+ my $sth=$self->{dbh}->prepare($sql);
+ my $idx=1;
+ for my $param (@params)
+ {
+ $param="" unless(defined($param));
+ $sth->bind_param($idx++, $param);
+ }
+ $sth->execute();
+ return $sth;
+}
+
+# pass cmd to db, ignore response
sub cmd
{
my ($self, @args)=@_;
$self->cmd_sth(@args);
}
+# return one row
sub cmd_onerow
{
my ($self, @args)=@_;
return($sth->fetchrow_array());
}
+# returns all rows
sub cmd_rows
{
my ($self, @args)=@_;
return $sth->fetchall_arrayref();
}
+# returns just the first column
+sub cmd_firstcol
+{
+ my ($self, @args)=@_;
+ return(map { $_->[0] } @{$self->cmd_rows(@args)});
+}
+
+# runs cmd, returns id of last insert
sub cmd_id
{
my ($self, @args)=@_;
sub last_insert_id
{
my $self=shift;
- if($self->{postgres})
- {
- return $self->{dbh}->last_insert_id(undef, undef, undef, undef,
- { sequence => "seq" });
- }
- else
+ return $self->{dbh}->last_insert_id("","","","");
+}
+
+# lookup id of $name in $table, also matching on $parent if needed
+sub lookup_id
+{
+ my($self, $table, $name, $parent)=@_;
+ my $sql="SELECT id FROM $table where name=?";
+ my @args=($name);
+ if($parent)
{
- return $self->{dbh}->last_insert_id("","","","");
+ $sql .= " AND parents_id=?";
+ push(@args, $parent);
}
+ my($id)=$self->cmd_onerow($sql, @args);
+ return $id;
}
__DATA__
);
CREATE TABLE paths (
- id INTEGER PRIMARY KEY,
- name text
+ id INTEGER,
+ name text,
+ PRIMARY KEY(id ASC)
);
CREATE TABLE artists (
- id INTEGER PRIMARY KEY,
- name text
+ id INTEGER,
+ name text,
+ PRIMARY KEY(id ASC)
);
CREATE TABLE albums (
- id INTEGER PRIMARY KEY,
- name text
+ id INTEGER,
+ name text,
+ PRIMARY KEY(id ASC)
);
CREATE TABLE files (
- id INTEGER PRIMARY KEY,
+ id INTEGER,
name text,
artists_id,
albums_id,
paths_id,
+ PRIMARY KEY(id ASC),
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,
+ id INTEGER,
parents_id INTEGER,
- name text
+ name text,
+ PRIMARY KEY(id ASC)
);
CREATE TABLE files_x_tags (
FOREIGN KEY(tags_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE
);
+CREATE INDEX idx_fxt_both ON files_x_tags (files_id, tags_id)
+
+CREATE INDEX idx_fxt_tags ON files_x_tags (tags_id)
+
+CREATE INDEX idx_files_id_name ON files (id, name)
+
+CREATE INDEX idx_files_name_id ON files (name, id)
+
+CREATE INDEX idx_tags_id_parent_name ON tags (id, parents_id, name)
+
+CREATE INDEX idx_tags_parent_id_name ON tags (parents_id, id, name)
+
+CREATE INDEX idx_tags_name ON tags (name)