id3fs: create & connect to DB
authorIan Beckwith <ianb@erislabs.net>
Sun, 19 Sep 2010 19:22:46 +0000 (20:22 +0100)
committerIan Beckwith <ianb@erislabs.net>
Sun, 19 Sep 2010 19:22:46 +0000 (20:22 +0100)
bin/id3fs
lib/ID3FS/DB.pm [new file with mode: 0644]

index 31a480f..eb0b2bd 100755 (executable)
--- a/bin/id3fs
+++ b/bin/id3fs
@@ -2,23 +2,29 @@
 # Ian Beckwith <ianb@erislabs.net>
 #
 
+use lib '/home/ianb/projects/id3fs/id3fs/lib';
 use strict;
 use Getopt::Long qw(Configure);
+use ID3FS::DB;
 use vars qw($me);
 $me=($0=~/(?:.*\/)?(.*)/)[0];
 
 my $verbose=0;
 my $help=0;
-
+my $init=0;
 Configure(qw(bundling no_ignore_case));
 my $optret=GetOptions(
     "verbose|v"  => \$verbose,
     "quiet|q"    => sub { $verbose=0; },
     "help|h"     => \$help,
+    "init|i"     => \$init,
     );
 
 usage() if(!@ARGV || !$optret || $help);
 
+my $path=shift;
+my $db=ID3FS::DB->new($path, $init, $me);
+
 sub usage
 {
     die("Usage: $me [-v] [-q] [-h] [--] file...\n".
diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm
new file mode 100644 (file)
index 0000000..6b0f844
--- /dev/null
@@ -0,0 +1,121 @@
+package ID3FS::DB;
+
+use strict;
+use warnings;
+use DBI;
+
+our $SCHEMA_VERSION=1;
+
+my $dbfile=".id3fs";
+
+sub new
+{
+    my $proto=shift;
+    my $class=ref($proto) || $proto;
+    my $self={};
+    bless($self,$class);
+
+    my($dir, $init, $me)=@_;
+    $self->{dbpath}="$dir/$dbfile";
+    $self->{me}=$me;
+
+    my $exists=-f $self->{dbpath};
+    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->{dbh}=DBI->connect("dbi:SQLite:dbname=$self->{dbpath}","","",
+                             { AutoCommit=>1 } );
+    unless(defined($self->{dbh}))
+    {
+       die("$me: DB Error: " . $DBI::errstr . "\n");
+    }
+
+    if($init)
+    {
+       $self->create();
+    }
+    else
+    {
+       $self->checkschema();
+    }
+
+    return $self;
+}
+
+sub create
+{
+    my($self,$name)=@_;
+    my @schema=split(/\n\n/,join("", <DATA>));
+    close(DATA);
+    for my $cmd (@schema)
+    {
+       $self->{dbh}->do($cmd);
+    }
+    $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION);
+}
+
+sub checkschema
+{
+    my $self=shift;
+    my ($version)=$self->cmd_onerow("SELECT schema_version from id3fs");
+    if(!defined($version) || $version != $SCHEMA_VERSION)
+    {
+       die("$self->{me}: id3fs database version " .
+           defined($version) ? $version : '""' .
+           "not known, current version is $SCHEMA_VERSION.\n");
+    }
+}
+
+sub cmd_sth
+{
+    my($self, $sql, @params)=@_;
+    my $sth=$self->{dbh}->prepare($sql);
+    my $idx=1;
+    for my $param (@params)
+    {
+       $param="" unless(defined($param));
+       $sth->bind_param($idx++, $param);
+    }
+    $sth->execute();
+    return $sth;
+}
+
+sub cmd
+{
+    my ($self, @args)=@_;
+    # don't care about retcode
+    $self->cmd_sth(@args);
+}
+
+sub cmd_onerow
+{
+    my ($self, @args)=@_;
+    my $sth=$self->cmd_sth(@args);
+    return($sth->fetchrow_array());
+}
+
+__DATA__
+
+CREATE TABLE id3fs (
+    schema_version
+);
+
+CREATE TABLE files (
+    id INTEGER PRIMARY KEY,
+    path
+);
+
+CREATE TABLE tags (
+    id INTEGER PRIMARY KEY,
+    name
+);
+
+CREATE TABLE tags_x_files (
+    files_id,
+    tags_id
+);
+
+CREATE TABLE v1genres (
+   id,
+   name
+);