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 print "CONSTRAINT: $constraint->{name}\n";
155 my $cid=$constraint->{id};
158 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
159 my $tagstr=join(", ", @ids);
160 my $sql = ($main_sql_start . $tagstr .
161 $main_sql_mid . $tagstr .
164 my $result=$self->cmd_rows($sql);
165 my @tagnames=map { $_->[0]; } @$result;
166 print "SUBNAMES: ", join(', ', @tagnames), "\n";
173 my $sql=("SELECT DISTINCT tagvals.name FROM tags\n" .
174 "INNER JOIN tags_x_tagvals ON tags.id=tags_x_tagvals.tags_id\n" .
175 "INNER JOIN tagvals ON tagvals.id=tags_x_tagvals.tagvals_id\n" .
176 "WHERE tags.name=?");
177 my $tags=$self->cmd_rows($sql, $tag);
178 return(map { $_->[0]; } @$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 print "CONSTRAINT: $constraint->{name}\n";
202 my $cid=$constraint->{id};
205 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
206 my $tagstr=join(", ", @ids);
207 my $sql = ($main_sql_start . $tagstr .
210 my $result=$self->cmd_rows($sql);
211 my @tagnames=map { $_->[0]; } @$result;
212 print "ARTISTS: ", join(', ', @tagnames), "\n";
218 my($self, @constraints)=@_;
220 # FIXME: rework PathElements
221 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
223 return $self->artist_albums($constraints[$#constraints]->{id});
225 my $main_sql_start=("SELECT albums.name\n" .
226 "\tFROM (SELECT files_id FROM tags\n" .
227 "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
228 "\t\tWHERE tags.id in\n\t\t\t(");
229 my $main_sql_end=(")\n\t\t) AS subselect\n" .
230 "\tINNER JOIN files ON subselect.files_id=files.id\n" .
231 "\tINNER JOIN albums ON files.albums_id=albums.id\n" .
232 "\n\tGROUP BY albums.name;");
233 while(my $constraint=shift @constraints)
235 print "CONSTRAINT: $constraint->{name}\n";
236 my $cid=$constraint->{id};
239 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
240 my $str=join(", ", @ids);
241 my $sql = ($main_sql_start . $str .
244 my $result=$self->cmd_rows($sql);
245 my @names=map { $_->[0]; } @$result;
246 print "ALBUMS: ", join(', ', @names), "\n";
252 my($self, $artist_id)=@_;
253 my $sql=("SELECT albums.name FROM files\n\t" .
254 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
255 "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
256 "WHERE artists.id=? and albums.name <> ''\n\t" .
257 "GROUP BY albums.name\n");
258 print "ARTIST_ALBUMS SQL: $sql\n";
259 my $result=$self->cmd_rows($sql, $artist_id);
260 my @albums=map { $_->[0]; } @$result;
261 print "ALBUMS: ", join(', ', @albums), "\n";
267 my($self, $artist_id)=@_;
268 my $sql=("SELECT files.name FROM files\n\t" .
269 "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
270 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
271 "WHERE artists.id=? AND albums.name=''\n\t" .
272 "GROUP BY files.name\n");
273 print "ARTIST_TRACKS SQL: $sql\n";
274 my $result=$self->cmd_rows($sql, $artist_id);
275 my @names=map { $_->[0]; } @$result;
276 @names = map { s/.*\///; $_; } @names;
277 print "ARTISTTRACKS: ", join(', ', @names), "\n";
283 my($self, $album_id)=@_;
284 my $sql=("SELECT files.name FROM files\n\t" .
285 "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
286 "WHERE albums.id=?\n\t" . #AND albums.name <> ''\n\t" .
287 "GROUP BY files.name\n");
288 print "ALBUM_TRACKS SQL($album_id): $sql\n";
289 my $result=$self->cmd_rows($sql, $album_id);
290 my @names=map { $_->[0]; } @$result;
291 @names = map { s/.*\///; $_;} @names;
292 print "TRACKS: ", join(', ', @names), "\n";
298 my($self, @constraints)=@_;
299 # FIXME: rework PathElements
300 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
302 return $self->artist_tracks($constraints[$#constraints]->{id});
304 elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
306 return $self->album_tracks($constraints[$#constraints]->{id});
309 my $main_sql_start=("SELECT files.name\n" .
310 "\tFROM (SELECT files_id FROM tags\n" .
311 "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
312 "\t\tWHERE tags.id in\n\t\t\t(");
313 my $main_sql_end=(")\n\t\t) AS subselect\n" .
314 "\tINNER JOIN files ON files.id=subselect.files_id" .
315 "\tGROUP BY files.name;");
317 while(my $constraint=shift @constraints)
319 print "CONSTRAINT: $constraint->{name}\n";
320 my $cid=$constraint->{id};
323 @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
324 my $str=join(", ", @ids);
325 my $sql = ($main_sql_start . $str .
328 my $result=$self->cmd_rows($sql);
329 my @names=map { $_->[0]; } @$result;
330 @names = map { s/.*\///; $_; } @names;
331 print "TRACKS: ", join(', ', @names), "\n";
337 my($self, @constraints)=@_;
338 if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
340 my $id=$constraints[$#constraints]->{id};
341 my $sql=("SELECT paths.name, files.name FROM files\n" .
342 "INNER JOIN paths ON files.paths_id=paths.id\n" .
343 "WHERE files.id=?\n" .
344 "GROUP BY paths.name, files.name");
345 print "FILENAME SQL: $sql\n";
346 my ($path, $name)=$self->cmd_onerow($sql, $id);
347 return($self->{absbase} . "/$path/$name");
349 die("DB::filename: unhandled case\n"); #FIXME
354 my($self, $type, $val)=@_;
355 print "ID: $type $val\n";
356 my $sql="SELECT id FROM $type WHERE name=?";
357 my ($id)=$self->cmd_onerow($sql, $val);
365 $relpath =~ s/^\Q$self->{base}\E\/?//;
366 my($filepart,$pathpart);
374 ($pathpart, $filepart) = ($relpath =~ /(.*)\/(.*)/);
376 my $file=ID3FS::File->new($path);
377 return unless(defined($file));
378 my $artist=$file->artist();
379 my $album=$file->album();
380 my $v1genre=$file->v1genre();
381 my $year=$file->year();
382 my $audiotype=$file->album();
383 my $tags=$file->tags();
384 my $haspic=$file->haspic();
386 $artist=undef unless($self->ok($artist));
387 my $artist_id=$self->add_to_table("artists", $artist);
388 my $path_id=$self->add_to_table("paths", $pathpart);
389 $album=undef unless($self->ok($album));
390 my $albums_id=$self->add_to_table("albums", $album);
391 my $file_id=$self->add_to_table("files", $filepart,
392 { "artists_id" => $artist_id,
393 "albums_id" => $albums_id,
394 "paths_id" => $path_id });
395 for my $tag (keys %$tags)
397 $self->add_tag($file_id, $tag, $tags->{$tag});
402 $self->add_tag($file_id, "year", $year);
403 if($year=~/^(\d\d\d)\d$/)
405 $self->add_tag($file_id, "decade", "${1}0s");
409 if($self->ok($v1genre))
411 $self->add_tag($file_id, "v1genre", $v1genre);
416 $self->add_tag($file_id, "haspic", undef);
422 my($self, $file_id, $tag, $val)=@_;
423 my $tag_id=$self->add_to_table("tags", $tag);
424 $self->add_relation("files_x_tags",
425 { "files_id" => $file_id,
426 "tags_id" => $tag_id });
429 my $val_id=$self->add_to_table("tagvals", $val);
430 $self->add_relation("tags_x_tagvals",
431 { "tags_id" => $tag_id,
432 "tagvals_id" => $val_id });
438 my($self, $table, $name, $extradata)=@_;
439 my $id=$self->lookup_id($table, $name);
442 my $sql="INSERT INTO $table (";
443 $sql .= "id, " if($self->{postgres});
445 if(defined($extradata))
447 push(@fields, sort keys(%$extradata));
449 $sql .= join(", ", @fields);
451 $sql .=") nextval('seq'), " if($self->{postgres});
452 $sql .= join(", ", map { "?"; } @fields);
454 $id=$self->cmd_id($sql, $name, map { $extradata->{$_} || ""; } sort keys %$extradata);
461 my ($self, $relname, $fields)=@_;
462 return if($self->relation_exists($relname, $fields));
463 my $sql="INSERT INTO $relname (";
464 $sql .= join(", ", sort keys(%$fields));
465 $sql .= ") VALUES (";
466 $sql .= join(", ", map { "?"; } sort keys(%$fields));
468 $self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
473 my($self, $table, $name)=@_;
474 my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
480 my ($self, $relname, $fields)=@_;
481 my $sql="SELECT count(1) FROM $relname WHERE ";
484 for my $field (keys %$fields)
487 push(@vals,$fields->{$field});
489 $sql .= join(' AND ', map { "$_=?"; } @exprs);
490 my ($ret)=$self->cmd_onerow($sql, @vals);
496 my($self, $thing)=@_;
497 return(defined($thing) && length($thing) && $thing =~ /\S+/);
502 my ($self, @args)=@_;
503 # don't care about retcode
504 $self->cmd_sth(@args);
509 my ($self, @args)=@_;
510 my $sth=$self->cmd_sth(@args);
511 return($sth->fetchrow_array());
516 my ($self, @args)=@_;
517 my $sth=$self->cmd_sth(@args);
518 return $sth->fetchall_arrayref();
523 my ($self, @args)=@_;
524 $self->cmd_sth(@args);
525 return($self->last_insert_id());
531 if($self->{postgres})
533 return $self->{dbh}->last_insert_id(undef, undef, undef, undef,
534 { sequence => "seq" });
538 return $self->{dbh}->last_insert_id("","","","");
545 schema_version INTEGER,
550 id INTEGER PRIMARY KEY,
558 id INTEGER PRIMARY KEY,
562 CREATE TABLE artists (
563 id INTEGER PRIMARY KEY,
567 CREATE TABLE albums (
568 id INTEGER PRIMARY KEY,
573 id INTEGER PRIMARY KEY,
577 CREATE TABLE tagvals (
578 id INTEGER PRIMARY KEY,
582 CREATE TABLE files_x_tags (
587 CREATE TABLE tags_x_tagvals (