$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}))
{
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;
}
}
- else
- {
- print "$self->{me}: $fallbackdir: not a directory\n";
- return undef;
- }
- 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());
}
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" .
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";
{
$self->add_tag($file_id, "haspic", undef);
}
+
+ if($self->ok($audiotype))
+ {
+ $self->add_tag($file_id, "audiotype", $audiotype);
+ }
}
sub add_tag
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);
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)
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("","","","");
- }
+ return $self->{dbh}->last_insert_id("","","","");
}
sub lookup_id
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)