From: Ian Beckwith Date: Wed, 22 Sep 2010 22:14:03 +0000 (+0100) Subject: support postgres as well as sqlite (for testing) X-Git-Tag: debian/1.0-1~194 X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=commitdiff_plain;h=e62dd7f2ced7805305933c97f2254883a45c3b83 support postgres as well as sqlite (for testing) --- diff --git a/lib/ID3FS/DB.pm b/lib/ID3FS/DB.pm index fe660d5..f9ae1dd 100644 --- a/lib/ID3FS/DB.pm +++ b/lib/ID3FS/DB.pm @@ -6,7 +6,6 @@ use DBI; use ID3FS::File; our $SCHEMA_VERSION=1; - my $dbfile=".id3fs"; sub new @@ -21,10 +20,24 @@ sub new $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}","","", + $self->{postgres}=0; + + unless($self->{postgres}) + { + 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); + } + + my $connectstr="dbi:SQLite:dbname=$self->{dbpath}"; + my ($user, $pass)=("", ""); + if($self->{postgres}) + { + $connectstr="dbi:Pg:dbname=id3fs"; + $user="ianb"; + $pass="foo"; + } + $self->{dbh}=DBI->connect($connectstr, $user, $pass, { AutoCommit=>1 } ); unless(defined($self->{dbh})) { @@ -52,6 +65,10 @@ sub create { $self->{dbh}->do($cmd); } + if($self->{postgres}) + { + $self->cmd("CREATE SEQUENCE seq"); + } $self->cmd("INSERT INTO id3fs (schema_version) VALUES (?)", $SCHEMA_VERSION); } @@ -208,6 +225,7 @@ sub add_to_table unless(defined($id)) { my $sql="INSERT INTO $table ("; + $sql .= "id, " if($self->{postgres}); my @fields=qw(name); if(defined($extradata)) { @@ -215,6 +233,7 @@ sub add_to_table } $sql .= join(", ", @fields); $sql .=") VALUES ("; + $sql .=") nextval('seq'), " if($self->{postgres}); $sql .= join(", ", map { "?"; } @fields); $sql .= ");"; $id=$self->cmd_id($sql, $name, map { $extradata->{$_} || ""; } sort keys %$extradata); @@ -294,56 +313,64 @@ sub cmd_id sub last_insert_id { my $self=shift; - return $self->{dbh}->last_insert_id("","","",""); + if($self->{postgres}) + { + return $self->{dbh}->last_insert_id(undef, undef, undef, undef, + { sequence => "seq" }); + } + else + { + return $self->{dbh}->last_insert_id("","","",""); + } } __DATA__ CREATE TABLE id3fs ( - schema_version + schema_version INTEGER ); CREATE TABLE files ( id INTEGER PRIMARY KEY, - name + name text ); CREATE TABLE artists ( id INTEGER PRIMARY KEY, - name + name text ); CREATE TABLE albums ( id INTEGER PRIMARY KEY, - name + name text ); CREATE TABLE tags ( id INTEGER PRIMARY KEY, - name + name text ); CREATE TABLE tagvals ( id INTEGER PRIMARY KEY, - name + name text ); CREATE TABLE files_x_tags ( - files_id, - tags_id + files_id INTEGER, + tags_id INTEGER ); CREATE TABLE tags_x_tagvals ( - tags_id, - tagvals_id + tags_id INTEGER, + tagvals_id INTEGER ); CREATE TABLE files_x_artists ( - files_id, - artists_id + files_id INTEGER, + artists_id INTEGER ); CREATE TABLE artists_x_albums ( - artists_id, - albums_id + artists_id INTEGER, + albums_id INTEGER );