X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=AwayMsg.pm;h=e20d6b6acc39f5b825a8fce78657bc45c79c4494;hb=42a6560ca0aa3325296f3ea72096232cdc056cb5;hp=9fdf742ed366720ed91c562abfdf0d14d10143d9;hpb=e4d5c2aa7fa9dc49cbdece5472dcb155d5833ea9;p=nj-awaymsg.git diff --git a/AwayMsg.pm b/AwayMsg.pm index 9fdf742..e20d6b6 100644 --- a/AwayMsg.pm +++ b/AwayMsg.pm @@ -11,7 +11,7 @@ BEGIN { our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); - @EXPORT = qw(&db_connect $dbh); + @EXPORT = qw(&db_connect &db_commit $dbh); %EXPORT_TAGS = ( ); @EXPORT_OK = qw(); } @@ -19,36 +19,37 @@ BEGIN { our ($dbh); sub db_connect () { - my $h = DBI->connect("dbi:SQLite:away.db", + my $dbf; + foreach my $d (@INC) { + $dbf = "$d/away.db"; + if (stat $dbf) { + chdir($d) or die $!; + last; + } + } + $dbh = DBI->connect("dbi:SQLite:away.db", { AutoCommit=>0, RaiseError=>1, ShowErrorStatement=>1 }) or die "$DBI::errstr ?"; +} - $dbh->do("BEGIN"); - - $dbh->do("CREATE TEMPORARY TABLE config (". - " emailaddr TEXT PRIMARY KEY NOT NULL,". - " username TEXT NOT NULL,". - " forwardfile TEXT NOT NULL". - ")"); - - open C, "config" or die $!; - while () { - s/^\s+//; - next if m/^\#/; - chomp or die; - s/\s+$//; - my @s = split; - die "$_ ?" unless @s==3; - $dbh->do("INSERT INTO config". - " (emailaddr, username, forwardfile)". - " VALUES (?,?,?)", - {}, @s); +sub nooutput ($) { + my ($stmt) = @_; + my $sth= $dbh->prepare($stmt); + $sth->execute(); + my $row; + if ($row= $sth->fetchrow_hashref()) { + die("REFERENTIAL INTEGRITY ERROR\n". + "\n$stmt\n". Dumper($row),"\n"); } - $dbh->do("COMMIT"); +} + - return $h; +sub db_commit () { + nooutput("SELECT * FROM addresses LEFT JOIN config USING (emailaddr)"); + nooutput("SELECT * FROM addresses LEFT JOIN texts USING (textid)"); + $dbh->do("COMMIT"); } 1;