Update of /cvsroot/fink/scripts/buildfink/lib/FinkFDB
In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv3481/lib/FinkFDB

Modified Files:
        DBI.pm 
Log Message:
Autopopulate database schemas

Index: DBI.pm
===================================================================
RCS file: /cvsroot/fink/scripts/buildfink/lib/FinkFDB/DBI.pm,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- DBI.pm      5 Apr 2007 01:45:51 -0000       1.5
+++ DBI.pm      5 Apr 2007 02:11:16 -0000       1.6
@@ -3,6 +3,7 @@
 use warnings;
 use Carp;
 use DBI;
+use FindBin qw($Bin);
 our @ISA = qw(FinkFDB);
 
 our %dbqueries = (
@@ -42,6 +43,7 @@
 ORDER BY is_directory DESC, file_name ASC, package_name ASC
 EOF
                  get_packages => "SELECT package_name, package_id FROM 
packages ORDER BY package_name ASC",
+                 schemacheck => "SELECT * FROM packages WHERE 0=1",
                  );
 
 sub new {
@@ -68,6 +70,8 @@
   return $self;
 }
 
+sub DESTROY { shift->disconnect(); }
+
 sub connect {
   my($self) = @_;
   $self->{dbh} = DBI->connect($self->{dbstr},
@@ -75,6 +79,35 @@
                              $self->{dbpass},
                              $self->{dbattrs}) or die "unable to connect to 
$self->{dbstr}: " . DBI->errstr;
   $self->{queries} = {};
+
+  eval {
+    $self->{dbh}->{PrintError} = 0;
+    $self->{dbh}->do($dbqueries{schemacheck});
+  };
+  $self->{dbh}->{PrintError} = 1;
+  if($@) {
+    my $schemafile = "$Bin/schemas/".lc($self->{dbtype}).".sql";
+    if(not -f $schemafile) {
+      die <<EOF;
+'$schemafile' doesn't exist.
+Database doesn't appear to have the correct schema, and don't know how to
+populate a $self->{dbtype} schema.
+EOF
+    }
+
+    open(SCHEMA, "<", $schemafile) or die
+      "Couldn't open schema file '$schemafile': $!\n";
+    local $/ = undef;
+    my $schema = <SCHEMA>;
+    close(SCHEMA);
+
+    # Yeah, this is a pretty poor excuse for a .sql parser...
+    my @statements = split(/;/, $schema);
+    foreach my $statement(@statements) {
+      $self->{dbh}->do($statement);
+    }
+    $self->{dbh}->commit();
+  }
 }
 
 sub disconnect {


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Fink-commits mailing list
Fink-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fink-commits

Reply via email to