implement id3 tag lookup
[id3fs.git] / lib / ID3FS / DB.pm
1 package ID3FS::DB;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use ID3FS::File;
7
8 our $SCHEMA_VERSION=1;
9
10 my $dbfile=".id3fs";
11
12 sub new
13 {
14     my $proto=shift;
15     my $class=ref($proto) || $proto;
16     my $self={};
17     bless($self,$class);
18
19     my($dir, $init, $me)=@_;
20     $self->{dbpath}="$dir/$dbfile";
21     $self->{me}=$me;
22
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);
26
27     $self->{dbh}=DBI->connect("dbi:SQLite:dbname=$self->{dbpath}","","",
28                               { AutoCommit=>1 } );
29     unless(defined($self->{dbh}))
30     {
31         die("$me: DB Error: " . $DBI::errstr . "\n");
32     }
33
34     if($init)
35     {
36         $self->create();
37     }
38     else
39     {
40         $self->checkschema();
41     }
42
43     return $self;
44 }
45
46 sub create
47 {
48     my($self,$name)=@_;
49     my @schema=split(/\n\n/,join("", <DATA>));
50     close(DATA);
51     for my $cmd (@schema)
52     {
53         $self->{dbh}->do($cmd);
54     }
55     $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
56 }
57
58 sub checkschema
59 {
60     my $self=shift;
61     my ($version)=$self->cmd_onerow("SELECT schema_version from id3fs");
62     if(!defined($version) || $version != $SCHEMA_VERSION)
63     {
64         die("$self->{me}: id3fs database version " .
65             defined($version) ? $version : '""' .
66             "not known, current version is $SCHEMA_VERSION.\n");
67     }
68 }
69
70 sub cmd_sth
71 {
72     my($self, $sql, @params)=@_;
73     my $sth=$self->{dbh}->prepare($sql);
74     my $idx=1;
75     for my $param (@params)
76     {
77         $param="" unless(defined($param));
78         $sth->bind_param($idx++, $param);
79     }
80     $sth->execute();
81     return $sth;
82 }
83
84 sub add
85 {
86     my($self,$path)=@_;
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 #FIXME
95 #    my $haspic=$file->haspic();
96     my $tags=$file->tags();
97     print "$path:\n";
98     for my $thing  (qw(artist album v1genre year audiotype))
99     {
100         no strict 'refs';
101         my $thingval=$file->$thing();
102         use strict 'refs';
103         print("\t$thing: ", $thingval, "\n") if(defined($thingval));
104     }
105 }
106
107 sub cmd
108 {
109     my ($self, @args)=@_;
110     # don't care about retcode
111     $self->cmd_sth(@args);
112 }
113
114 sub cmd_onerow
115 {
116     my ($self, @args)=@_;
117     my $sth=$self->cmd_sth(@args);
118     return($sth->fetchrow_array());
119 }
120
121 __DATA__
122
123 CREATE TABLE id3fs (
124     schema_version
125 );
126
127 CREATE TABLE files (
128     id INTEGER PRIMARY KEY,
129     path
130 );
131
132 CREATE TABLE tags (
133     id INTEGER PRIMARY KEY,
134     name
135 );
136
137 CREATE TABLE tagvalues (
138     id INTEGER PRIMARY KEY,
139     name
140 );
141
142 CREATE TABLE files_x_tags (
143     files_id,
144     tags_id
145 );
146
147 CREATE TABLE tags_x_tagvalues (
148     tags_id,
149     tagvalues_id
150 );
151