15 my $class=ref($proto) || $proto;
20 $self->{dbpath}=shift;
22 $self->{fallbackdir}=shift;
24 if(!defined($self->{base}) &&
25 defined($self->{fallbackdir}) &&
26 -d $self->{fallbackdir})
28 $self->{base}=$self->{fallbackdir};
30 $self->{dbpath}="$self->{base}/$dbfile" unless(defined($self->{dbpath}));
31 $self->{absbase}=Cwd::abs_path($self->{base});
33 my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
34 my ($user, $pass)=("", "");
37 $connectstr="dbi:Pg:dbname=id3fs";
41 my $exists=-f $self->{dbpath};
42 $self->{dbh}=DBI->connect($connectstr, $user, $pass,
44 unless(defined($self->{dbh}))
46 die("$self->{me}: DB Error: " . $DBI::errstr . "\n");
64 my @schema=split(/\n\n/,join("", <DATA>));
68 $self->{dbh}->do($cmd);
72 $self->cmd("CREATE SEQUENCE seq");
76 my %indexes=( "idx_files_id" => "files (id)",
77 "idx_fxt_both" => "files_x_tags (files_id, tags_id)",
78 "idx_fxt_files" => "files_x_tags (files_id)",
79 "idx_fxt_tags" => "files_x_tags (tags_id)",
80 "idx_tags_id" => "tags (id)",
81 "idx_tags_name" => "tags (name)");
82 for my $index (keys %indexes)
84 $self->{dbh}->do("CREATE INDEX $index ON " . $indexes{$index});
87 $self->cmd("INSERT INTO id3fs (schema_version, last_update) VALUES (?, ?)",
88 $SCHEMA_VERSION, time());
94 my ($version)=$self->cmd_onerow("SELECT schema_version from id3fs");
95 if(!defined($version) || $version != $SCHEMA_VERSION)
97 die("$self->{me}: id3fs database version " .
98 defined($version) ? $version : '""' .
99 "not known, current version is $SCHEMA_VERSION.\n");
105 my($self, $newval)=@_;
108 $self->cmd("UPDATE id3fs SET last_update=?", $newval);
112 ($newval)=$self->cmd_onerow("SELECT last_update from id3fs");
119 my($self, $sql, @params)=@_;
120 my $sth=$self->{dbh}->prepare($sql);
122 for my $param (@params)
124 $param="" unless(defined($param));
125 $sth->bind_param($idx++, $param);
133 my($self, @constraints)=@_;
134 if(!@constraints) # /
137 my $sql="SELECT DISTINCT name FROM tags;";
138 my $tags=$self->cmd_rows($sql);
139 return(map { $_->[0]; } @$tags);
143 my $main_sql_start=("SELECT t2.name\n" .
144 "\tFROM (SELECT files_id FROM tags t1\n" .
145 "\t\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n" .
146 "\t\tWHERE t1.id in\n\t\t\t(");
147 my $main_sql_mid=(")\n\t\t) AS subselect\n" .
148 "\tINNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
149 "\tINNER JOIN tags t2 ON files_x_tags.tags_id=t2.id\n" .
150 "\tWHERE t2.id NOT IN (");
151 my $main_sql_end=")\n\tGROUP BY t2.name;";
152 while(my $constraint=shift @constraints)
154 my $cid=$constraint->{id};
157 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
158 my $tagstr=join(", ", @ids);
159 my $sql = ($main_sql_start . $tagstr .
160 $main_sql_mid . $tagstr .
163 my $result=$self->cmd_rows($sql);
164 my @tagnames=map { $_->[0]; } @$result;
165 print "SUBNAMES: ", join(', ', @tagnames), "\n";
171 my($self, $tagid)=@_;
172 my $sql=("SELECT DISTINCT tagvals.name FROM tagvals\n" .
173 "INNER JOIN tags_x_tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n" .
174 "WHERE tags_x_tagvals.tags_id=?");
175 my $tags=$self->cmd_rows($sql, $tagid);
176 my @tags=map { $_->[0]; } @$tags;
177 @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
183 my($self, @constraints)=@_;
184 if(!@constraints) # /ALL
186 my $sql="SELECT DISTINCT name FROM artists;";
187 my $tags=$self->cmd_rows($sql);
188 return(map { $_->[0]; } @$tags);
191 my $main_sql_start=("SELECT artists.name\n" .
192 "\tFROM (SELECT files_id FROM tags\n" .
193 "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
194 "\t\tWHERE tags.id in\n\t\t\t(");
195 my $main_sql_end=(")\n\t\t) AS subselect\n" .
196 "\tINNER JOIN files ON subselect.files_id=files.id\n" .
197 "\tINNER JOIN artists ON files.artists_id=artists.id\n" .
198 "\n\tGROUP BY artists.name;");
199 while(my $constraint=shift @constraints)
201 my $cid=$constraint->{id};
204 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
205 my $tagstr=join(", ", @ids);
206 my $sql = ($main_sql_start . $tagstr .
209 my $result=$self->cmd_rows($sql);
210 my @tagnames=map { $_->[0]; } @$result;
211 print "ARTISTS: ", join(', ', @tagnames), "\n";
217 my($self, @constraints)=@_;
219 # FIXME: rework PathElements
220 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
222 return $self->artist_albums($constraints[$#constraints]->{id});
224 my $main_sql_start=("SELECT albums.name\n" .
225 "\tFROM (SELECT files_id FROM tags\n" .
226 "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
227 "\t\tWHERE tags.id in\n\t\t\t(");
228 my $main_sql_end=(")\n\t\t) AS subselect\n" .
229 "\tINNER JOIN files ON subselect.files_id=files.id\n" .
230 "\tINNER JOIN albums ON files.albums_id=albums.id\n" .
231 "\n\tGROUP BY albums.name;");
232 while(my $constraint=shift @constraints)
234 my $cid=$constraint->{id};
237 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
238 my $str=join(", ", @ids);
239 my $sql = ($main_sql_start . $str .
241 my $result=$self->cmd_rows($sql);
242 my @names=map { $_->[0]; } @$result;
243 print "ALBUMS: ", join(', ', @names), "\n";
249 my($self, $artist_id)=@_;
250 my $sql=("SELECT albums.name FROM files\n\t" .
251 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
252 "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
253 "WHERE artists.id=? and albums.name <> ''\n\t" .
254 "GROUP BY albums.name\n");
255 print "ARTIST_ALBUMS SQL: $sql\n";
256 my $result=$self->cmd_rows($sql, $artist_id);
257 my @albums=map { $_->[0]; } @$result;
258 print "ALBUMS: ", join(', ', @albums), "\n";
264 my($self, $artist_id)=@_;
265 my $sql=("SELECT files.name FROM files\n\t" .
266 "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
267 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
268 "WHERE artists.id=? AND albums.name=''\n\t" .
269 "GROUP BY files.name\n");
270 print "ARTIST_TRACKS SQL: $sql\n";
271 my $result=$self->cmd_rows($sql, $artist_id);
272 my @names=map { $_->[0]; } @$result;
273 @names = map { s/.*\///; $_; } @names;
274 print "ARTISTTRACKS: ", join(', ', @names), "\n";
280 my($self, $artist_id, $album_id)=@_;
281 my $sql=("SELECT files.name FROM files\n\t" .
282 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
283 "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
284 "WHERE artists.id=? AND albums.id=?\n\t" .
285 "GROUP BY files.name\n");
286 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n";
287 my $result=$self->cmd_rows($sql, $artist_id, $album_id);
288 my @names=map { $_->[0]; } @$result;
289 @names = map { s/.*\///; $_;} @names;
290 print "TRACKS: ", join(', ', @names), "\n";
296 my($self, @constraints)=@_;
297 # FIXME: rework PathElements
298 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
300 return $self->artist_tracks($constraints[$#constraints]->{id});
302 elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
305 my $artist=$constraints[($#constraints)-1];
306 if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
308 # should always happen
309 $artist_id=$artist->{id};
311 return $self->album_tracks($artist_id, $constraints[$#constraints]->{id});
314 my $main_sql_start=("SELECT files.name\n" .
315 "\tFROM (SELECT files_id FROM tags\n" .
316 "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
317 "\t\tWHERE tags.id in\n\t\t\t(");
318 my $main_sql_end=(")\n\t\t) AS subselect\n" .
319 "\tINNER JOIN files ON files.id=subselect.files_id" .
320 "\tGROUP BY files.name;");
322 while(my $constraint=shift @constraints)
324 my $cid=$constraint->{id};
327 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
328 my $str=join(", ", @ids);
329 my $sql = ($main_sql_start . $str .
332 my $result=$self->cmd_rows($sql);
333 my @names=map { $_->[0]; } @$result;
334 @names = map { s/.*\///; $_; } @names;
335 print "TRACKS: ", join(', ', @names), "\n";
341 my($self, @constraints)=@_;
342 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
344 my $id=$constraints[$#constraints]->{id};
345 my $sql=("SELECT paths.name, files.name FROM files\n" .
346 "INNER JOIN paths ON files.paths_id=paths.id\n" .
347 "WHERE files.id=?\n" .
348 "GROUP BY paths.name, files.name");
349 print "FILENAME SQL: $sql\n";
350 my ($path, $name)=$self->cmd_onerow($sql, $id);
351 return($self->{absbase} . "/$path/$name");
353 die("DB::filename: unhandled case\n"); #FIXME
358 my($self, $type, $val)=@_;
359 my $sql="SELECT id FROM $type WHERE name=?";
360 my ($id)=$self->cmd_onerow($sql, $val);
368 $relpath =~ s/^\Q$self->{base}\E\/?//;
369 my($filepart,$pathpart);
377 ($pathpart, $filepart) = ($relpath =~ /(.*)\/(.*)/);
379 my $file=ID3FS::AudioFile->new($path);
380 return unless(defined($file));
381 my $artist=$file->artist();
382 my $album=$file->album();
383 my $v1genre=$file->v1genre();
384 my $year=$file->year();
385 my $audiotype=$file->audiotype();
386 my $tags=$file->tags();
387 my $haspic=$file->haspic();
389 $artist=undef unless($self->ok($artist));
390 my $artist_id=$self->add_to_table("artists", $artist);
391 my $path_id=$self->add_to_table("paths", $pathpart);
392 $album=undef unless($self->ok($album));
393 my $albums_id=$self->add_to_table("albums", $album);
394 my $file_id=$self->add_to_table("files", $filepart,
395 { "artists_id" => $artist_id,
396 "albums_id" => $albums_id,
397 "paths_id" => $path_id });
398 for my $tag (keys %$tags)
400 $self->add_tag($file_id, $tag, $tags->{$tag});
405 $self->add_tag($file_id, "year", $year);
406 if($year=~/^(\d\d\d)\d$/)
408 $self->add_tag($file_id, "decade", "${1}0s");
412 if($self->ok($v1genre))
414 $self->add_tag($file_id, "v1genre", $v1genre);
419 $self->add_tag($file_id, "haspic", undef);
425 my($self, $file_id, $tag, $val)=@_;
426 my $tag_id=$self->add_to_table("tags", $tag);
427 $self->add_relation("files_x_tags",
428 { "files_id" => $file_id,
429 "tags_id" => $tag_id });
432 my $val_id=$self->add_to_table("tagvals", $val);
433 $self->add_relation("tags_x_tagvals",
434 { "tags_id" => $tag_id,
435 "tagvals_id" => $val_id });
441 my($self, $table, $name, $extradata)=@_;
442 my $id=$self->lookup_id($table, $name);
445 my $sql="INSERT INTO $table (";
446 $sql .= "id, " if($self->{postgres});
448 if(defined($extradata))
450 push(@fields, sort keys(%$extradata));
452 $sql .= join(", ", @fields);
454 $sql .=") nextval('seq'), " if($self->{postgres});
455 $sql .= join(", ", map { "?"; } @fields);
457 $id=$self->cmd_id($sql, $name, map { $extradata->{$_} || ""; } sort keys %$extradata);
464 my ($self, $relname, $fields)=@_;
465 return if($self->relation_exists($relname, $fields));
466 my $sql="INSERT INTO $relname (";
467 $sql .= join(", ", sort keys(%$fields));
468 $sql .= ") VALUES (";
469 $sql .= join(", ", map { "?"; } sort keys(%$fields));
471 $self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
476 my($self, $table, $name)=@_;
477 my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
484 my $sql=("SELECT COUNT(*) FROM tags\n\t" .
485 "INNER JOIN tags_x_tagvals ON tags.id=tags_x_tagvals.tags_id\n\t" .
486 "INNER JOIN tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n\t" .
487 "WHERE tags.id=?\n");
488 my ($rows)=$self->cmd_onerow($sql, $id);
494 my ($self, $relname, $fields)=@_;
495 my $sql="SELECT count(1) FROM $relname WHERE ";
498 for my $field (keys %$fields)
501 push(@vals,$fields->{$field});
503 $sql .= join(' AND ', map { "$_=?"; } @exprs);
504 my ($ret)=$self->cmd_onerow($sql, @vals);
510 my($self, $thing)=@_;
511 return(defined($thing) && length($thing) && $thing =~ /\S+/);
516 my ($self, @args)=@_;
517 # don't care about retcode
518 $self->cmd_sth(@args);
523 my ($self, @args)=@_;
524 my $sth=$self->cmd_sth(@args);
525 return($sth->fetchrow_array());
530 my ($self, @args)=@_;
531 my $sth=$self->cmd_sth(@args);
532 return $sth->fetchall_arrayref();
537 my ($self, @args)=@_;
538 $self->cmd_sth(@args);
539 return($self->last_insert_id());
545 if($self->{postgres})
547 return $self->{dbh}->last_insert_id(undef, undef, undef, undef,
548 { sequence => "seq" });
552 return $self->{dbh}->last_insert_id("","","","");
559 schema_version INTEGER,
564 id INTEGER PRIMARY KEY,
572 id INTEGER PRIMARY KEY,
576 CREATE TABLE artists (
577 id INTEGER PRIMARY KEY,
581 CREATE TABLE albums (
582 id INTEGER PRIMARY KEY,
587 id INTEGER PRIMARY KEY,
591 CREATE TABLE tagvals (
592 id INTEGER PRIMARY KEY,
596 CREATE TABLE files_x_tags (
601 CREATE TABLE tags_x_tagvals (