8 use English qw(-no_match_vars);
 
  10 use Rose::DBx::Cache::Anywhere;
 
  12 use base qw(Rose::DB);
 
  14 __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
 
  15 __PACKAGE__->use_private_registry;
 
  17 my (%_db_registered, %_initial_sql_executed);
 
  22   return SL::DBConnect->connect(@_);
 
  26   my $domain = shift || SL::DB->default_domain;
 
  27   my $type   = shift || SL::DB->default_type;
 
  29   ($domain, $type) = _register_db($domain, $type);
 
  31   my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
 
  33   _execute_initial_sql($db);
 
  38 my %_dateformats = ( 'yy-mm-dd'   => 'ISO',
 
  39                      'yyyy-mm-dd' => 'ISO',
 
  40                      'mm/dd/yy'   => 'SQL, US',
 
  41                      'dd/mm/yy'   => 'SQL, EUROPEAN',
 
  42                      'dd.mm.yy'   => 'GERMAN'
 
  53     $type = 'LXOFFICE_EMPTY';
 
  54     %connect_settings = ( driver => 'Pg' );
 
  56   } elsif ($type eq 'LXOFFICE_AUTH') {
 
  57     %connect_settings = ( driver          => $::myconfig{dbdriver} || 'Pg',
 
  58                           database        => $::auth->{DB_config}->{db},
 
  59                           host            => $::auth->{DB_config}->{host} || 'localhost',
 
  60                           port            => $::auth->{DB_config}->{port} || 5432,
 
  61                           username        => $::auth->{DB_config}->{user},
 
  62                           password        => $::auth->{DB_config}->{password},
 
  63                           connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
 
  66     my $european_dates = 0;
 
  67     if ($::myconfig{dateformat}) {
 
  68       $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} }
 
  69                           && $_dateformats{ $::myconfig{dateformat} } =~ m/european/i;
 
  72     %connect_settings = ( driver          => $::myconfig{dbdriver} || 'Pg',
 
  73                           database        => $::myconfig{dbname},
 
  74                           host            => $::myconfig{dbhost} || 'localhost',
 
  75                           port            => $::myconfig{dbport} || 5432,
 
  76                           username        => $::myconfig{dbuser},
 
  77                           password        => $::myconfig{dbpasswd},
 
  78                           connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
 
  80                           european_dates  => $european_dates);
 
  83   my %flattened_settings = _flatten_settings(%connect_settings);
 
  85   $domain = 'LXOFFICE' if $type =~ m/^LXOFFICE/;
 
  86   $type  .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort keys %flattened_settings);
 
  87   my $idx = "${domain}::${type}";
 
  89   if (!$_db_registered{$idx}) {
 
  90     $_db_registered{$idx} = 1;
 
  92     __PACKAGE__->register_db(domain => $domain,
 
  98   return ($domain, $type);
 
 101 sub _execute_initial_sql {
 
 104   return if $_initial_sql_executed{$db} || !%::myconfig || !$::myconfig{dateformat};
 
 106   $_initial_sql_executed{$db} = 1;
 
 108   # Don't rely on dboptions being set properly. Chose them from
 
 109   # dateformat instead.
 
 110   my $pg_dateformat = $_dateformats{ $::myconfig{dateformat} };
 
 111   $db->dbh->do("set DateStyle to '${pg_dateformat}'") if $pg_dateformat;
 
 114 sub _flatten_settings {
 
 118   while (my ($key, $value) = each %settings) {
 
 119     if ('HASH' eq ref $value) {
 
 120       %flattened = ( %flattened, _flatten_settings(%{ $value }) );
 
 122       $flattened{$key} = $value;
 
 129 sub with_transaction {
 
 130   my ($self, $code, @args) = @_;
 
 132   return $self->in_transaction ? $code->(@args) : $self->do_transaction(sub { $code->(@args) });
 
 144 SL::DB - Database access class for all RDB objects
 
 150 =item C<create $domain, $type>
 
 152 Registers the database information with Rose, creates a cached
 
 153 connection and executes initial SQL statements. Those can include
 
 154 setting the time & date format to the user's preferences.
 
 156 =item C<dbi_connect $dsn, $login, $password, $options>
 
 158 Forwards the call to L<SL::DBConnect/connect> which connects to the
 
 159 database. This indirection allows L<SL::DBConnect/connect> to route
 
 160 the calls through L<DBIx::Log4Perl> if this is enabled in the
 
 163 =item C<with_transaction $code_ref, @args>
 
 165 Executes C<$code_ref> within a transaction, starting one if none is
 
 166 currently active. This is just a shortcut for the following code:
 
 168   # Verbose code in caller (an RDBO instance):
 
 170     # do stuff with $self
 
 172   return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
 
 174 Now the version using C<with_transaction>:
 
 176   return $self->db->with_transaction(sub {
 
 177     # do stuff with $self
 
 188 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>