Add database configuration logic; minor bugfixes and code cleanup
authortobiasly <tobiasly>
Sat, 14 Dec 2002 05:56:44 +0000 (05:56 +0000)
committertobiasly <tobiasly>
Sat, 14 Dec 2002 05:56:44 +0000 (05:56 +0000)
lib/BackupPC/Config.pm
lib/BackupPC/Config/Db.pm
lib/BackupPC/Config/Db/MySQL.pm

index 26ccead..b8ef8e8 100644 (file)
@@ -14,7 +14,6 @@ sub CheckConfigInfo
     my($attr, $val, $def, $ref);
     
     foreach $attr (sort keys %{ $self->{Conf} }) {
-        print AA "Checking $attr...";
         $val = $self->{Conf}->{$attr};
         $ref = ref $val;
         $def = $ConfigDef{$attr};
@@ -104,7 +103,8 @@ sub timeStamp
             . ($noPad ? "" : " ");
 }
 
-sub ConnectData {
+sub ConnectData
+{
     # fallback routine in case no database used
     return 1;
 
index 5343fa6..44c5882 100644 (file)
@@ -5,7 +5,32 @@ use warnings;
 use strict;
 
 use DBI;
-our $SELF;
+
+sub GetDbConnInfo
+{
+    my($self, $dbi) = @_;
+    my($ret, $mesg, $dbConfig);
+
+    $dbConfig = "$self->{TopDir}/conf/db.pl";
+    
+    our %Db;
+    
+    if ( !defined($ret = do $dbConfig) && ($! || $@) ) {
+        $mesg = "Couldn't open $dbConfig: $!" if ( $! );
+        $mesg = "Couldn't execute $dbConfig: $@" if ( $@ );
+        $mesg =~ s/[\n\r]+//;
+        
+        $self->{errstr} = $mesg;
+        return undef;
+    }
+    
+    $Db{passwd} = $ENV{BPC_DBPASSWD} if !exists $Db{passwd};
+    
+    my %parm = %Db;
+    undef %Db;
+    
+    return %parm;
+}
 
 sub BackupInfoRead
 {
@@ -214,7 +239,6 @@ our %gConfigTypeField; # will be defined by database-specific Config module
 
 sub ConfigWrite {
     my($self, $client) = @_;
-    $SELF = $self;
     my $dbh = $self->{dbh};
     
     $dbh->{RaiseError} = 0;
@@ -251,7 +275,6 @@ sub ConfigWrite {
 
 sub _ConfigWriteScalar {
     my($dbh, $def, $client, $attr, $val) = @_;
-    $SELF->Debug("SCALAR $val") if $attr eq 'BackupFilesOnly';
     return if !defined $val;
     
     my $ref = ref $val;
@@ -265,7 +288,6 @@ sub _ConfigWriteScalar {
 
 sub _ConfigWriteArray {
     my($dbh, $def, $client, $attr, $val, $key) = @_;
-    $SELF->Debug("ARRAY $val, $key") if $attr eq 'BackupFilesOnly';
     return if !defined $val;
 
     $key = '' unless defined $key;
@@ -291,7 +313,6 @@ sub _ConfigWriteArray {
 
 sub _ConfigWriteHash {
     my($dbh, $def, $client, $attr, $val, $subscript) = @_;
-    $SELF->Debug("HASH $val") if $attr eq 'BackupFilesOnly';
     return if !defined $val;
 
     $subscript = -1 unless defined $subscript;
@@ -354,7 +375,6 @@ sub _ConfigWriteArrayOfHash {
 
 sub _ConfigWriteHashOfArray {
     my($dbh, $def, $client, $attr, $val) = @_;
-    $SELF->Debug("HASHOFARRAY $val") if $attr eq 'BackupFilesOnly';
     return if !defined $val;
 
     my $ref = ref $val;
index dfb21cb..45a3b09 100644 (file)
@@ -7,6 +7,8 @@ use strict;
 use DBI;
 use DBD::mysql;
 
+our %Db;
+
 %BackupPC::Config::Db::gConfigTypeField =
     (BOOLEAN   => 'valueBit',
      INT       => 'valueInt',
@@ -15,48 +17,69 @@ use DBD::mysql;
      MEMO      => 'valueMemo',
     );
 
-sub ConnectData {
-   my($self) = @_;
-
-   my $dsn = 'DBI:mysql:database=backuppc;host=reagan';
-   $self->{dbh} = DBI->connect($dsn, 'root', undef, {RaiseError => 1,
-                               AutoCommit => 1,});
+sub ConnectData
+{
+    my($self) = @_;
+    return if $self->{dbh};
+    
+    my($mesg, %db, $parm, @missing);
+    
+    %db = $self->GetDbConnInfo;
+    return $self->{errstr} if $self->{errstr};
+
+    foreach $parm (qw(host database user passwd)) {
+        push(@missing, $parm) if !exists $db{$parm}
+    }
+    
+    if (@missing) {
+        $mesg = "Missing Db connection parameters: "
+            . join(", ", @missing);
+        return $mesg;
+    }
+        
+    my $dsn = "DBI:mysql:database=$db{database};host=$db{host}";
+
+    $self->{dbh} = DBI->connect($dsn, $db{user}, $db{passwd}, {RaiseError => 1,
+                                AutoCommit => 1,});
+    
+    return;
 }
 
-sub ConfigMTime {
-   my($self) = @_;
-return time();
-   my $cmd = "SHOW TABLE STATUS LIKE 'Config'";
-   my $sth = $self->{dbh}->prepare($cmd);
-
-   $sth->execute;
-   my $row = $sth->fetchrow_hashref || return time();
-   my $mtime;
-
-   if (defined($mtime = $row->{'Update_time'})) {
-      $mtime =~ m/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/
-        || return time();
-      return &Date_SecsSince1970($2,$3,$1,$4,$5,$6);
-   } else {
-      return time();
-   }
+
+sub ConfigMTime
+{
+    my($self) = @_;
+    my $cmd = "SHOW TABLE STATUS LIKE 'Config'";
+    my $sth = $self->{dbh}->prepare($cmd);
+    $sth->execute;
+    my $row = $sth->fetchrow_hashref || return time();
+    my $mtime;
+    if (defined($mtime = $row->{'Update_time'})) {
+       $mtime =~ m/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/
+         || return time();
+       return &Date_SecsSince1970($2,$3,$1,$4,$5,$6);
+    } else {
+       return time();
+    }
 }
 
-sub HostsMTime {
-   my($self) = @_;
-return time();
-   my $cmd = "SHOW TABLE STATUS LIKE 'Client'";
-   my $sth = $self->{dbh}->prepare($cmd);
-
-   $sth->execute;
-   my $row = $sth->fetchrow_hashref || return time();
-   my $mtime;
-
-   if (defined($mtime = $row->{'Update_time'})) {
-      return &Date_SecsSince1970($mtime);
-   } else {
-      return time();
-   }
+sub HostsMTime
+{
+    my($self) = @_;
+    my $cmd = "SHOW TABLE STATUS LIKE 'Client'";
+    my $sth = $self->{dbh}->prepare($cmd);
+    $sth->execute;
+    my $row = $sth->fetchrow_hashref || return time();
+    my $mtime;
+    if (defined($mtime = $row->{'Update_time'})) {
+       return &Date_SecsSince1970($mtime);
+    } else {
+       return time();
+    }
 }
 
 # Date subs borrowed from Date::Manip.
@@ -64,46 +87,48 @@ return time();
 # This program is free software; you can redistribute it and/or modify it
 # under the same terms as Perl itself.
 
-sub Date_SecsSince1970 {
-  my($mysqlDate) = @_;
-  $mysqlDate =~ m/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/
-    || return time();
-  my($y,$m,$d,$h,$mn,$s) = ($1,$2,$3,$4,$5,$6);
-  my($sec_now,$sec_70,$Ny,$N4,$N100,$N400,$dayofyear,$days)=();
-  my($cc,$yy)=();
-
-  $y=~ /(\d{2})(\d{2})/;
-  ($cc,$yy)=($1,$2);
-
-  $Ny=$y;
-
-  $N4=($Ny-1)/4 + 1;
-  $N4=0         if ($y==0);
-
-  $N100=$cc + 1;
-  $N100--       if ($yy==0);
-  $N100=0       if ($y==0);
-
-  $N400=($N100-1)/4 + 1;
-  $N400=0       if ($y==0);
-
-  my(@days) = ( 0, 31, 59, 90,120,151,181,212,243,273,304,334,365);
-  my($ly)=0;
-  $ly=1  if ($m>2 && &Date_LeapYear($y));
-
-  $dayofyear=$days[$m-1]+$d+$ly;
-  $days= $Ny*365 + $N4 - $N100 + $N400 + $dayofyear;
-  $sec_now=($days-1)*24*3600 + $h*3600 + $mn*60 + $s;
-  $sec_70 =62167219200;
-  return ($sec_now-$sec_70);
+sub Date_SecsSince1970
+{
+    my($mysqlDate) = @_;
+    $mysqlDate =~ m/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/
+      || return time();
+    my($y,$m,$d,$h,$mn,$s) = ($1,$2,$3,$4,$5,$6);
+    my($sec_now,$sec_70,$Ny,$N4,$N100,$N400,$dayofyear,$days)=();
+    my($cc,$yy)=();
+  
+    $y=~ /(\d{2})(\d{2})/;
+    ($cc,$yy)=($1,$2);
+  
+    $Ny=$y;
+  
+    $N4=($Ny-1)/4 + 1;
+    $N4=0         if ($y==0);
+  
+    $N100=$cc + 1;
+    $N100--       if ($yy==0);
+    $N100=0       if ($y==0);
+  
+    $N400=($N100-1)/4 + 1;
+    $N400=0       if ($y==0);
+  
+    my(@days) = ( 0, 31, 59, 90,120,151,181,212,243,273,304,334,365);
+    my($ly)=0;
+    $ly=1  if ($m>2 && &Date_LeapYear($y));
+  
+    $dayofyear=$days[$m-1]+$d+$ly;
+    $days= $Ny*365 + $N4 - $N100 + $N400 + $dayofyear;
+    $sec_now=($days-1)*24*3600 + $h*3600 + $mn*60 + $s;
+    $sec_70 =62167219200;
+    return ($sec_now-$sec_70);
 }
 
-sub Date_LeapYear {
-  my($y)=@_;
-  return 0 if $y % 4;
-  return 1 if $y % 100;
-  return 0 if $y % 400;
-  return 1;
+sub Date_LeapYear
+{
+    my($y)=@_;
+    return 0 if $y % 4;
+    return 1 if $y % 100;
+    return 0 if $y % 400;
+    return 1;
 }