$self->{base}=shift;
my $fallbackdir=shift;
- $dbpath=$self->find_db($init, $dbpath, $fallbackdir);
- return undef unless($dbpath);
+ $self->{dbpath}=$self->find_db($init, $dbpath, $fallbackdir);
+ return undef unless($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}))
{
{
$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 id
+sub bare_tags
+{
+ 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 tags_with_values
{
- my($self, $type, $val)=@_;
- my $sql="SELECT id FROM $type WHERE name=?";
- my ($id)=$self->cmd_onerow($sql, $val);
- return($id);
+ # 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 tag_has_values
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, "decade", "${1}0s");
}
+ else
+ {
+ $self->add_tag($file_id, "decade", "UNKNOWN");
+ }
if($self->ok($v1genre))
{
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);
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
{
- my($self, $table, $name)=@_;
- my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
+ my($self, $table, $name, $parent)=@_;
+ my $sql="SELECT id FROM $table where name=?";
+ my @args=($name);
+ if($parent)
+ {
+ $sql .= " AND parents_id=?";
+ push(@args, $parent);
+ }
+ my($id)=$self->cmd_onerow($sql, @args);
return $id;
}
);
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 (
FOREIGN KEY(tags_id) REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE
);
+CREATE INDEX idx_files_id ON files (id)
+
+CREATE INDEX idx_fxt_both ON files_x_tags (files_id, tags_id)
+
+CREATE INDEX idx_fxt_files ON files_x_tags (files_id)
+
+CREATE INDEX idx_fxt_tags ON files_x_tags (tags_id)
+
+CREATE INDEX idx_tags_id ON tags (id)
+
+CREATE INDEX idx_tags_name ON tags (name)
+