my $self={};
bless($self,$class);
- my($dir, $init, $me)=@_;
- $self->{base}=$dir;
- $self->{absbase}=Cwd::abs_path($dir);
- $self->{dbpath}="$dir/$dbfile";
- $self->{me}=$me;
+ $self->{me}=shift;
+ $self->{dbpath}=shift;
+ $self->{base}=shift;
+ $self->{fallbackdir}=shift;
- my $exists=-f $self->{dbpath};
-
- $self->{postgres}=0;
-
- unless($self->{postgres})
+ if(!defined($self->{base}) &&
+ defined($self->{fallbackdir}) &&
+ -d $self->{fallbackdir})
{
- die("$me: $self->{dbpath}: not found. use --init to create.\n") if(!$exists && !$init);
- die("$me: --init used but $self->{dbpath} exists.\n") if($exists && $init);
+ $self->{base}=$self->{fallbackdir};
}
+ $self->{dbpath}="$self->{base}/$dbfile" unless(defined($self->{dbpath}));
+ $self->{absbase}=Cwd::abs_path($self->{base});
my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
my ($user, $pass)=("", "");
$user="ianb";
$pass="foo";
}
+ my $exists=-f $self->{dbpath};
$self->{dbh}=DBI->connect($connectstr, $user, $pass,
{ AutoCommit=>1 } );
unless(defined($self->{dbh}))
{
- die("$me: DB Error: " . $DBI::errstr . "\n");
+ die("$self->{me}: DB Error: " . $DBI::errstr . "\n");
}
- if($init)
+ if($exists)
{
- $self->create();
+ $self->checkschema();
}
else
{
- $self->checkschema();
+ $self->create();
}
return $self;
$self->{dbh}->do("CREATE INDEX $index ON " . $indexes{$index});
}
}
- $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
+ $self->cmd("INSERT INTO id3fs (schema_version, last_update) VALUES (?, ?)",
+ $SCHEMA_VERSION, time());
}
sub checkschema
}
}
+sub last_update
+{
+ my($self, $newval)=@_;
+ if(defined($newval))
+ {
+ $self->cmd("UPDATE id3fs SET last_update=?", $newval);
+ }
+ else
+ {
+ ($newval)=$self->cmd_onerow("SELECT last_update from id3fs");
+ }
+ return $newval;
+}
+
sub cmd_sth
{
my($self, $sql, @params)=@_;
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);
"\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
"\t\tWHERE tags.id in\n\t\t\t(");
my $main_sql_end=(")\n\t\t) AS subselect\n" .
- "\tINNER JOIN files_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
- "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+ "\tINNER JOIN files ON subselect.files_id=files.id\n" .
+ "\tINNER JOIN artists ON files.artists_id=artists.id\n" .
"\n\tGROUP BY artists.name;");
while(my $constraint=shift @constraints)
{
"\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
"\t\tWHERE tags.id in\n\t\t\t(");
my $main_sql_end=(")\n\t\t) AS subselect\n" .
- "\tINNER JOIN files_x_albums ON subselect.files_id=files_x_albums.files_id\n" .
- "\tINNER JOIN albums ON albums.id=files_x_albums.albums_id\n" .
+ "\tINNER JOIN files ON subselect.files_id=files.id\n" .
+ "\tINNER JOIN albums ON files.albums_id=albums.id\n" .
"\n\tGROUP BY albums.name;");
while(my $constraint=shift @constraints)
{
sub artist_albums
{
my($self, $artist_id)=@_;
- my $sql=("SELECT albums.name FROM artists\n\t" .
- "INNER JOIN artists_x_albums ON artists.id=artists_x_albums.artists_id\n\t" .
- "INNER JOIN albums ON albums.id=artists_x_albums.albums_id\n\t" .
- "WHERE artists.id=?\n\t" .
+ my $sql=("SELECT albums.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.name <> ''\n\t" .
"GROUP BY albums.name\n");
print "ARTIST_ALBUMS SQL: $sql\n";
my $result=$self->cmd_rows($sql, $artist_id);
sub artist_tracks
{
my($self, $artist_id)=@_;
- my $sql=("SELECT files.name FROM artists\n\t" .
- "INNER JOIN artists_x_files ON artists.id=files_x_artists.artists_id\n\t" .
- "INNER JOIN files ON files.id=files_x_artists.files_id\n\t" .
- "WHERE artists.id=?\n\t" .
+ my $sql=("SELECT files.name FROM files\n\t" .
+ "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;
- @names = map { s/.*\///; } @names;
+ @names = map { s/.*\///; $_; } @names;
print "ARTISTTRACKS: ", join(', ', @names), "\n";
return(@names);
}
{
my($self, $album_id)=@_;
my $sql=("SELECT files.name FROM files\n\t" .
- "INNER JOIN files_x_albums ON albums.id=files_x_albums.albums_id\n\t" .
- "INNER JOIN albums ON albums.id=files_x_albums.albums_id\n\t" .
- "WHERE albums.id=?\n\t" .
+ "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
+ "WHERE albums.id=?\n\t" . #AND albums.name <> ''\n\t" .
"GROUP BY files.name\n");
print "ALBUM_TRACKS SQL($album_id): $sql\n";
my $result=$self->cmd_rows($sql, $album_id);
{
my $id=$constraints[$#constraints]->{id};
my $sql=("SELECT paths.name, files.name FROM files\n" .
- "INNER JOIN paths_x_files ON files.id=paths_x_files.files_id\n" .
- "INNER JOIN paths ON paths_x_files.paths_id=paths.id\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 $tags=$file->tags();
my $haspic=$file->haspic();
- my $file_id=$self->add_to_table("files", $filepart);
+ $artist=undef unless($self->ok($artist));
+ my $artist_id=$self->add_to_table("artists", $artist);
my $path_id=$self->add_to_table("paths", $pathpart);
- $self->add_relation("paths_x_files",
- { "paths_id" => $path_id,
- "files_id" => $file_id});
-
+ $album=undef unless($self->ok($album));
+ my $albums_id=$self->add_to_table("albums", $album);
+ my $file_id=$self->add_to_table("files", $filepart,
+ { "artists_id" => $artist_id,
+ "albums_id" => $albums_id,
+ "paths_id" => $path_id });
for my $tag (keys %$tags)
{
$self->add_tag($file_id, $tag, $tags->{$tag});
}
- my $artist_id;
- if($self->ok($artist))
- {
- $artist_id=$self->add_to_table("artists", $artist);
- $self->add_relation("files_x_artists",
- { "files_id" => $file_id,
- "artists_id" => $artist_id });
- }
-
- if($self->ok($album))
- {
- my $albums_id=$self->add_to_table("albums", $album);
- $self->add_relation("files_x_albums",
- { "files_id" => $file_id,
- "albums_id" => $albums_id});
- if($self->ok($artist))
- {
- $self->add_relation("artists_x_albums",
- { "artists_id" => $artist_id,
- "albums_id" => $albums_id});
- }
- }
-
if($self->ok($year))
{
$self->add_tag($file_id, "year", $year);
__DATA__
CREATE TABLE id3fs (
- schema_version INTEGER
+ schema_version INTEGER,
+ last_update
);
CREATE TABLE files (
id INTEGER PRIMARY KEY,
+ artists_id,
+ albums_id,
+ paths_id,
name text
);
name text
);
-CREATE TABLE paths_x_files (
- paths_id INTEGER,
- files_id INTEGER
-);
-
CREATE TABLE files_x_tags (
files_id INTEGER,
tags_id INTEGER
tags_id INTEGER,
tagvals_id INTEGER
);
-
-CREATE TABLE files_x_artists (
- files_id INTEGER,
- artists_id INTEGER
-);
-
-CREATE TABLE files_x_albums (
- files_id INTEGER,
- albums_id INTEGER
-);
-
-CREATE TABLE artists_x_albums (
- artists_id INTEGER,
- albums_id INTEGER
-);