Projekt

Allgemein

Profil

Herunterladen (5,15 KB) Statistiken
| Zweig: | Markierung: | Revision:
82515b2d Sven Schöling
package SL::DB;

use strict;

use Carp;
use Data::Dumper;
37d7b869 Sven Schöling
use SL::DBConnect;
82515b2d Sven Schöling
use English qw(-no_match_vars);
use Rose::DB;
37d7b869 Sven Schöling
use Rose::DBx::Cache::Anywhere;
82515b2d Sven Schöling
use base qw(Rose::DB);

37d7b869 Sven Schöling
__PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
82515b2d Sven Schöling
__PACKAGE__->use_private_registry;

my (%_db_registered, %_initial_sql_executed);

54d656bd Moritz Bunkus
sub dbi_connect {
shift;

22c02125 Moritz Bunkus
return SL::DBConnect->connect(@_);
54d656bd Moritz Bunkus
}

82515b2d Sven Schöling
sub create {
my $domain = shift || SL::DB->default_domain;
my $type = shift || SL::DB->default_type;

45aeeb13 Moritz Bunkus
($domain, $type) = _register_db($domain, $type);
82515b2d Sven Schöling
my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);

ea502a0b Moritz Bunkus
_execute_initial_sql($db);

82515b2d Sven Schöling
return $db;
}

ea502a0b Moritz Bunkus
my %_dateformats = ( 'yy-mm-dd' => 'ISO',
'yyyy-mm-dd' => 'ISO',
'mm/dd/yy' => 'SQL, US',
'dd/mm/yy' => 'SQL, EUROPEAN',
'dd.mm.yy' => 'GERMAN'
);

82515b2d Sven Schöling
sub _register_db {
my $domain = shift;
my $type = shift;

a97d97a0 Moritz Bunkus
my %connect_settings;
ea502a0b Moritz Bunkus
my $initial_sql;
a97d97a0 Moritz Bunkus
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},
fe68756c Moritz Bunkus
host => $::auth->{DB_config}->{host} || 'localhost',
a97d97a0 Moritz Bunkus
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 {
ea502a0b Moritz Bunkus
my $european_dates = 0;
if ($::myconfig{dateformat}) {
5c886d92 Sven Schöling
$european_dates = 1 if $_dateformats{ $::myconfig{dateformat} }
&& $_dateformats{ $::myconfig{dateformat} } =~ m/european/i;
ea502a0b Moritz Bunkus
}

a97d97a0 Moritz Bunkus
%connect_settings = ( driver => $::myconfig{dbdriver} || 'Pg',
database => $::myconfig{dbname},
fe68756c Moritz Bunkus
host => $::myconfig{dbhost} || 'localhost',
a97d97a0 Moritz Bunkus
port => $::myconfig{dbport} || 5432,
username => $::myconfig{dbuser},
password => $::myconfig{dbpasswd},
connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
ea502a0b Moritz Bunkus
},
european_dates => $european_dates);
a97d97a0 Moritz Bunkus
}

18a9b190 Moritz Bunkus
my %flattened_settings = _flatten_settings(%connect_settings);

a97d97a0 Moritz Bunkus
$domain = 'LXOFFICE' if $type =~ m/^LXOFFICE/;
45aeeb13 Moritz Bunkus
$type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort keys %flattened_settings);
a97d97a0 Moritz Bunkus
my $idx = "${domain}::${type}";

if (!$_db_registered{$idx}) {
$_db_registered{$idx} = 1;

__PACKAGE__->register_db(domain => $domain,
type => $type,
%connect_settings,
);
}

return ($domain, $type);
82515b2d Sven Schöling
}

ea502a0b Moritz Bunkus
sub _execute_initial_sql {
my ($db) = @_;

return if $_initial_sql_executed{$db} || !%::myconfig || !$::myconfig{dateformat};

$_initial_sql_executed{$db} = 1;

# Don't rely on dboptions being set properly. Chose them from
# dateformat instead.
my $pg_dateformat = $_dateformats{ $::myconfig{dateformat} };
$db->dbh->do("set DateStyle to '${pg_dateformat}'") if $pg_dateformat;
}

18a9b190 Moritz Bunkus
sub _flatten_settings {
my %settings = @_;
my %flattened = ();

while (my ($key, $value) = each %settings) {
if ('HASH' eq ref $value) {
%flattened = ( %flattened, _flatten_settings(%{ $value }) );
} else {
$flattened{$key} = $value;
}
}

return %flattened;
}

7cfa1f2a Moritz Bunkus
sub with_transaction {
my ($self, $code, @args) = @_;

return $self->in_transaction ? $code->(@args) : $self->do_transaction(sub { $code->(@args) });
}

82515b2d Sven Schöling
1;
7cfa1f2a Moritz Bunkus
__END__

=pod

=encoding utf8

=head1 NAME

SL::DB - Database access class for all RDB objects

=head1 FUNCTIONS

=over 4

=item C<create $domain, $type>

Registers the database information with Rose, creates a cached
connection and executes initial SQL statements. Those can include
setting the time & date format to the user's preferences.

=item C<dbi_connect $dsn, $login, $password, $options>

Forwards the call to L<SL::DBConnect/connect> which connects to the
database. This indirection allows L<SL::DBConnect/connect> to route
the calls through L<DBIx::Log4Perl> if this is enabled in the
configuration.

=item C<with_transaction $code_ref, @args>

Executes C<$code_ref> within a transaction, starting one if none is
currently active. This is just a shortcut for the following code:

# Verbose code in caller (an RDBO instance):
my $worker = sub {
# do stuff with $self
};
return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);

Now the version using C<with_transaction>:

return $self->db->with_transaction(sub {
# do stuff with $self
});

=back

=head1 BUGS

Nothing here yet.

=head1 AUTHOR

Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>

=cut