15 my $class=ref($proto) || $proto;
19 my($dir, $init, $me)=@_;
20 $self->{dbpath}="$dir/$dbfile";
23 my $exists=-f $self->{dbpath};
24 die("$me: $self->{dbpath}: not found. use --init to create.\n") if(!$exists && !$init);
25 die("$me: --init used but $self->{dbpath} exists.\n") if($exists && $init);
27 $self->{dbh}=DBI->connect("dbi:SQLite:dbname=$self->{dbpath}","","",
29 unless(defined($self->{dbh}))
31 die("$me: DB Error: " . $DBI::errstr . "\n");
49 my @schema=split(/\n\n/,join("", <DATA>));
53 $self->{dbh}->do($cmd);
55 $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
61 my ($version)=$self->cmd_onerow("SELECT schema_version from id3fs");
62 if(!defined($version) || $version != $SCHEMA_VERSION)
64 die("$self->{me}: id3fs database version " .
65 defined($version) ? $version : '""' .
66 "not known, current version is $SCHEMA_VERSION.\n");
72 my($self, $sql, @params)=@_;
73 my $sth=$self->{dbh}->prepare($sql);
75 for my $param (@params)
77 $param="" unless(defined($param));
78 $sth->bind_param($idx++, $param);
87 my $file=ID3FS::File->new($path);
88 return unless(defined($file));
89 my $artist=$file->artist();
90 my $album=$file->album();
91 my $v1genre=$file->v1genre();
92 my $year=$file->year();
93 my $audiotype=$file->album();
94 my $tags=$file->tags();
95 my $haspic=$file->haspic();
97 my $file_id=$self->add_to_table("files", $path);
98 my $artists_id=$self->add_to_table("artists", $artist);
99 my $albums_id=$self->add_to_table("albums", $album);
100 for my $tag (keys %$tags)
102 $self->add_tag($file_id, $tag, $tags->{$tag});
107 $self->add_tag($file_id, "year", $year);
108 if($year=~/^(\d\d\d)\d$/)
110 $self->add_tag($file_id, "decade", "${1}0s");
113 if($self->ok($v1genre))
115 $self->add_tag($file_id, "v1genre", $v1genre);
120 $self->add_tag($file_id, "haspic", undef);
123 $self->add_relation("files_x_artists",
124 { "files_id" => $file_id,
125 "artists_id" => $artists_id });
127 $self->add_relation("artists_x_albums",
128 { "artists_id" => $artists_id,
129 "albums_id" => $albums_id});
134 my($self, $file_id, $tag, $val)=@_;
135 my $tag_id=$self->add_to_table("tags", $tag);
136 $self->add_relation("files_x_tags",
137 { "files_id" => $file_id,
138 "tags_id" => $tag_id });
141 my $val_id=$self->add_to_table("tagvals", $val);
142 $self->add_relation("tags_x_tagvals",
143 { "tags_id" => $tag_id,
144 "tagvals_id" => $val_id });
150 my($self, $table, $name, $extradata)=@_;
151 my $id=$self->lookup_id($table, $name);
154 my $sql="INSERT INTO $table (";
156 if(defined($extradata))
158 push(@fields, sort keys(%$extradata));
160 $sql .= join(", ", @fields);
162 $sql .= join(", ", map { "?"; } @fields);
164 $id=$self->cmd_id($sql, $name, map { $extradata->{$_} || ""; } sort keys %$extradata);
171 my ($self, $relname, $fields)=@_;
172 return if($self->relation_exists($relname, $fields));
173 my $sql="INSERT INTO $relname (";
174 $sql .= join(", ", sort keys(%$fields));
175 $sql .= ") VALUES (";
176 $sql .= join(", ", map { "?"; } sort keys(%$fields));
178 $self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
183 my($self, $table, $name)=@_;
184 my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
190 my ($self, $relname, $fields)=@_;
191 my $sql="SELECT count(1) FROM $relname WHERE ";
194 for my $field (keys %$fields)
197 push(@vals,$fields->{$field});
199 $sql .= join(' AND ', map { "$_=?"; } @exprs);
200 my ($ret)=$self->cmd_onerow($sql, @vals);
206 my($self, $thing)=@_;
207 return(defined($thing) && length($thing));
212 my ($self, @args)=@_;
213 # don't care about retcode
214 $self->cmd_sth(@args);
219 my ($self, @args)=@_;
220 my $sth=$self->cmd_sth(@args);
221 return($sth->fetchrow_array());
226 my ($self, @args)=@_;
227 $self->cmd_sth(@args);
228 return($self->last_insert_id());
234 return $self->{dbh}->last_insert_id("","","","");
244 id INTEGER PRIMARY KEY,
248 CREATE TABLE artists (
249 id INTEGER PRIMARY KEY,
253 CREATE TABLE albums (
254 id INTEGER PRIMARY KEY,
259 id INTEGER PRIMARY KEY,
263 CREATE TABLE tagvals (
264 id INTEGER PRIMARY KEY,
268 CREATE TABLE files_x_tags (
273 CREATE TABLE tags_x_tagvals (
278 CREATE TABLE files_x_artists (
283 CREATE TABLE artists_x_albums (