Für Auth*-Models eigene Datenbankverbindung nutzen
authorMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 30 Dec 2010 13:19:00 +0000 (14:19 +0100)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 30 Dec 2010 13:19:00 +0000 (14:19 +0100)
SL/DB.pm
SL/DB/Object.pm

index 18f8696..bcbf2e1 100644 (file)
--- a/SL/DB.pm
+++ b/SL/DB.pm
@@ -17,12 +17,7 @@ sub create {
   my $domain = shift || SL::DB->default_domain;
   my $type   = shift || SL::DB->default_type;
 
-  if ($type eq 'LXOFFICE') {
-    $domain = 'LXEMPTY' unless %::myconfig && $::myconfig{dbname};
-    $type   = join $SUBSCRIPT_SEPARATOR, map { $::myconfig{$_} } qw(dbdriver dbname dbhost dbport dbuser dbpasswd) if %::myconfig;
-  }
-
-  _register_db($domain, $type);
+  my ($domain, $type) = _register_db($domain, $type);
 
   my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
 
@@ -33,22 +28,46 @@ sub _register_db {
   my $domain = shift;
   my $type   = shift;
 
-  my $idx    = "${domain}::${type}";
-  return if $_db_registered{$idx};
-
-  $_db_registered{$idx} = 1;
-
-  __PACKAGE__->register_db(domain          => $domain,
-                           type            => $type,
-                           driver          => $::myconfig{dbdriver} || 'Pg',
-                           database        => $::myconfig{dbname},
-                           host            => $::myconfig{dbhost},
-                           port            => $::myconfig{dbport} || 5432,
-                           username        => $::myconfig{dbuser},
-                           password        => $::myconfig{dbpasswd},
-                           connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
-                                              },
-                          );
+  my %connect_settings;
+
+  if (!%::myconfig) {
+    $type = 'LXOFFICE_EMPTY';
+    %connect_settings = ( driver => 'Pg' );
+
+  } elsif ($type eq 'LXOFFICE_AUTH') {
+    %connect_settings = ( driver          => $::myconfig{dbdriver} || 'Pg',
+                          database        => $::auth->{DB_config}->{db},
+                          host            => $::auth->{DB_config}->{host},
+                          port            => $::auth->{DB_config}->{port} || 5432,
+                          username        => $::auth->{DB_config}->{user},
+                          password        => $::auth->{DB_config}->{password},
+                          connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
+                                             });
+  } else {
+    %connect_settings = ( driver          => $::myconfig{dbdriver} || 'Pg',
+                          database        => $::myconfig{dbname},
+                          host            => $::myconfig{dbhost},
+                          port            => $::myconfig{dbport} || 5432,
+                          username        => $::myconfig{dbuser},
+                          password        => $::myconfig{dbpasswd},
+                          connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
+                                             });
+  }
+
+  $domain = 'LXOFFICE' if $type =~ m/^LXOFFICE/;
+  $type  .= join($SUBSCRIPT_SEPARATOR, map { $::connect_setings{$_} } sort keys %connect_settings);
+  my $idx = "${domain}::${type}";
+
+  if (!$_db_registered{$idx}) {
+    $_db_registered{$idx} = 1;
+
+    __PACKAGE__->register_db(domain => $domain,
+                             type   => $type,
+                             %connect_settings,
+                            );
+  }
+
+  return ($domain, $type);
 }
 
 1;
index f138518..52b9300 100644 (file)
@@ -25,7 +25,7 @@ sub new {
 sub init_db {
   my $class_or_self = shift;
   my $class         = ref($class_or_self) || $class_or_self;
-  my $type          = 'LXOFFICE';
+  my $type          = $class =~ m/::Auth/ ? 'LXOFFICE_AUTH' : 'LXOFFICE';
 
   return SL::DB::create(undef, $type);
 }