X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FDB.pm;h=14f55c5b723e14f830999110575adaa4b13a4c68;hb=3cfb86272c06fe583e4eea36c27a5383115ff4c3;hp=5cd78158a809ca140a1add4e585510eafc6e3577;hpb=72614ea3786a31bcc1f24b0bdc9cd888f8a81f66;p=id3fs.git diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index 5cd7815..14f55c5 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -19,24 +19,13 @@ 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->{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})) { @@ -57,41 +46,36 @@ sub new 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)) + for my $dir (@dirs) { - $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); + 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}; } @@ -105,23 +89,6 @@ 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, last_update) VALUES (?, ?)", $SCHEMA_VERSION, time()); } @@ -164,276 +131,45 @@ sub last_update 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, @constraints)=@_; - if(!@constraints) # / - { - # FIXME: add ALL? - my $sql="SELECT DISTINCT name FROM tags;"; - my $tags=$self->cmd_rows($sql); - return(map { $_->[0]; } @$tags); - } - my @ids=(); - - my $main_sql_start=("SELECT t2.name\n" . - "\tFROM (\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" . - "WHERE t2.id NOT IN ("); - my $main_sql_end=")\nGROUP BY t2.name;"; - # FIXME: generalise more? - - 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 "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, @constraints)=@_; - 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, @constraints)=@_; - my @ids=(); - # FIXME: rework PathElements - if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist") - { - return $self->artist_albums($constraints[$#constraints]->{id}, @constraints); - } - 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;"); - 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, @constraints)=@_; - 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, @constraints)=@_; - 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 +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 tags.name FROM tags\n" . + "WHERE tags.parents_id=''\n" . + "GROUP BY tags.name\n"); + my @names=$self->cmd_firstcol($sql); + return (@names); } -sub tracks +sub tags_with_values { - my($self, @constraints)=@_; - # 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($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 @names=map { $_->[0]; } @$result; - print "TRACKS: ", join(', ', @names), "\n"; - return(@names); -} - -sub filename -{ - my($self, $mountpoint, @constraints)=@_; - if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File") + my $tags={}; + for my $pair (@$result) { - 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)); + push(@{$tags->{$pair->[0]}}, $pair->[1]); } - die("DB::filename: unhandled case\n"); #FIXME + return $tags; } -sub tags_subselect +sub tag_has_values { - my($self,@constraints)=@_; - use Data::Dumper; - my @tags=(); - my @tags_vals=(); - my $lasttag=''; -# print "CONSTRAINTS: \n", Dumper \@constraints; - for my $constraint (@constraints) - { -# print ref($constraint),"\n"; - if(ref($constraint) eq "ID3FS::PathElement::Tag") - { - if($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})); - } - } - elsif(ref($constraint) eq "ID3FS::PathElement::Tagval") - { -# print "TAGVAL\n"; - push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id}); - } - } - unless($self->{postgres}) - { - @tags=map{ "\"$_\""; } @tags; - @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); - } -# print "TAGS\n", Dumper \@tags; -# print "\nVALS\n", Dumper(\@tags_vals), "\n"; - 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 @clauses=(); - if(@tags) - { - push(@clauses, "(t1.id IN ( " . join(', ', @tags) ." ) )"); - } - for my $pair (@tags_vals) - { - my($tag, $val)=@$pair; - push(@clauses, "( t1.parents_id=$tag AND t1.id=$val )"); - } - if(@clauses) - { - $sql .= "\tWHERE\n\t\t" . join("\n\t\tOR ", @clauses) . "\n"; - } - return $sql; + 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)=@_; + my($self, $path, $name, $mountpoint)=@_; + my $id3fs_path=$self->{dbpath}; $id3fs_path=~s/(.*)\/.*/$1/; my $rpath="$self->{absbase}/$path"; my $vpath="$mountpoint/$id3fs_path"; @@ -454,9 +190,9 @@ sub relativise { shift(@path); shift(@rel); - print "POP "; +# print "POP "; } - print "\n"; +# print "\n"; my $upcount=scalar(@rel); my $result="../" x $upcount; $result .= join("/",@path); @@ -464,49 +200,13 @@ sub relativise 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($path !~ /\//) + if($relpath !~ /\//) { $pathpart=''; $filepart=$relpath; @@ -545,13 +245,15 @@ sub add $self->add_tag($file_id, @$tag); } - if($self->ok($year)) + $year="UNKNOWN" unless($self->ok($year)); + $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)) @@ -563,31 +265,43 @@ sub add { $self->add_tag($file_id, "haspic", undef); } + + if($self->ok($audiotype)) + { + $self->add_tag($file_id, "audiotype", $audiotype); + } } sub add_tag { - my($self, $file_id, @tags)=@_; - my $parent_id=undef; - for my $tag (@tags) + 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($value) && length($value)) { - my $tag_id=$self->add_to_table("tags", $tag, - { "parents_id" => $parent_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" => $tag_id }); - $parent_id=$tag_id; + "tags_id" => $val_id }); } } 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)) { @@ -595,7 +309,6 @@ 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); @@ -615,37 +328,31 @@ sub add_relation $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); +# print "files_in: SQL: $sql\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 ORDER BY name\n"); + my $sql=("SELECT name, id FROM paths\n"); my $pathsref=$self->cmd_rows($sql); my @ids=(); for my $pathpair (@$pathsref) @@ -667,7 +374,7 @@ sub prune_paths return unless(@ids); my $sql=("DELETE FROM files WHERE paths_id IN (\n\t" . join(', ', map { "\"$_\""; } @ids). "\n\t)"); - print "SQL: \n", $sql, "\n"; +# print "SQL: \n", $sql, "\n"; $self->cmd($sql); } @@ -700,8 +407,9 @@ sub remove_unused 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"; +# print "SQL: $sql\n"; my @sql=split(/\n\n/, $sql); $self->cmd($_) for (@sql); } @@ -728,6 +436,20 @@ sub ok return(defined($thing) && length($thing) && $thing =~ /\S+/); } +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 cmd { my ($self, @args)=@_; @@ -749,6 +471,12 @@ sub cmd_rows return $sth->fetchall_arrayref(); } +sub cmd_firstcol +{ + my ($self, @args)=@_; + return(map { $_->[0] } @{$self->cmd_rows(@args)}); +} + sub cmd_id { my ($self, @args)=@_; @@ -759,15 +487,21 @@ sub cmd_id 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("","","",""); +} + +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__ @@ -778,35 +512,40 @@ CREATE TABLE id3fs ( ); CREATE TABLE paths ( - id INTEGER PRIMARY KEY, - name text + id INTEGER, + name text, + PRIMARY KEY(id DESC) ); CREATE TABLE artists ( - id INTEGER PRIMARY KEY, - name text + id INTEGER, + name text, + PRIMARY KEY(id DESC) ); CREATE TABLE albums ( - id INTEGER PRIMARY KEY, - name text + id INTEGER, + name text, + PRIMARY KEY(id DESC) ); CREATE TABLE files ( - id INTEGER PRIMARY KEY, + id INTEGER, name text, artists_id, albums_id, paths_id, + PRIMARY KEY(id DESC), 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 DESC) ); CREATE TABLE files_x_tags ( @@ -816,3 +555,16 @@ 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)