use Cwd;
our $SCHEMA_VERSION=1;
-my $dbfile=".id3fs";
+my $dbfile=".id3fs"; # default
sub new
{
my $init=shift;
$self->{base}=shift;
$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=$self->{dbpath}";
return $self;
}
+# search parent directories for db
sub find_db
{
# class method
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)
sub relativise
{
- my($self, $path, $name, $mountpoint)=@_;
- my $id3fs_path=$self->{dbpath};
- $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";
+ # no path in common, return absolute
+ # should never happen
+ # FIXME
return $name;
}
# f: /home/foo/bar/baz.mp3
{
shift(@path);
shift(@rel);
-# print "POP ";
}
-# print "\n";
my $upcount=scalar(@rel);
my $result="../" x $upcount;
$result .= join("/",@path);
my $sql=("SELECT files.name FROM files\n" .
"INNER JOIN paths ON files.paths_id=paths.id\n" .
"WHERE paths.name=?\n");
-# print "files_in: SQL: $sql\n";
return($self->cmd_firstcol($sql, $dir));
}
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);
}
return(defined($thing) && length($thing) && $thing =~ /\S+/);
}
+# actually call the database
sub cmd_sth
{
my($self, $sql, @params)=@_;
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)=@_;
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)=@_;