From 9b7d54938365e6164f1e1ee7be52ae47b7b46929 Mon Sep 17 00:00:00 2001 From: Ian Beckwith Date: Sun, 19 Sep 2010 20:22:46 +0100 Subject: [PATCH] id3fs: create & connect to DB --- bin/id3fs | 8 +++- lib/ID3FS/DB.pm | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 lib/ID3FS/DB.pm diff --git a/bin/id3fs b/bin/id3fs index 31a480f..eb0b2bd 100755 --- a/bin/id3fs +++ b/bin/id3fs @@ -2,23 +2,29 @@ # Ian Beckwith # +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 index 0000000..6b0f844 --- /dev/null +++ b/lib/ID3FS/DB.pm @@ -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("", )); + 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 +); -- 2.11.0