Unterstützung für andere Datenbankencodings als Unicode/UTF-8 entfernt
[kivitendo-erp.git] / SL / DB.pm
1 package SL::DB;
2
3 use strict;
4
5 use Carp;
6 use Data::Dumper;
7 use SL::DBConnect;
8 use English qw(-no_match_vars);
9 use Rose::DB;
10 use Rose::DBx::Cache::Anywhere;
11
12 use base qw(Rose::DB);
13
14 __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
15 __PACKAGE__->use_private_registry;
16
17 my (%_db_registered);
18
19 sub dbi_connect {
20   shift;
21
22   return SL::DBConnect->connect(@_);
23 }
24
25 sub create {
26   my $domain = shift || SL::DB->default_domain;
27   my $type   = shift || SL::DB->default_type;
28
29   ($domain, $type) = _register_db($domain, $type);
30
31   my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
32
33   return $db;
34 }
35
36 sub _register_db {
37   my $domain = shift;
38   my $type   = shift;
39
40   my %specific_connect_settings;
41   my %common_connect_settings = (
42     driver           => 'Pg',
43     european_dates   => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0,
44     connect_options  => {
45       pg_enable_utf8 => 1,
46     },
47   );
48
49   if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) {
50     %specific_connect_settings = (
51       database        => $::auth->{DB_config}->{db},
52       host            => $::auth->{DB_config}->{host} || 'localhost',
53       port            => $::auth->{DB_config}->{port} || 5432,
54       username        => $::auth->{DB_config}->{user},
55       password        => $::auth->{DB_config}->{password},
56     );
57
58   } elsif ($::auth && $::auth->client) {
59     my $client        = $::auth->client;
60     %specific_connect_settings = (
61       database        => $client->{dbname},
62       host            => $client->{dbhost} || 'localhost',
63       port            => $client->{dbport} || 5432,
64       username        => $client->{dbuser},
65       password        => $client->{dbpasswd},
66     );
67
68   } elsif (%::myconfig && $::myconfig{dbname}) {
69     %specific_connect_settings = (
70       database        => $::myconfig{dbname},
71       host            => $::myconfig{dbhost} || 'localhost',
72       port            => $::myconfig{dbport} || 5432,
73       username        => $::myconfig{dbuser},
74       password        => $::myconfig{dbpasswd},
75     );
76
77   } else {
78     $type = 'KIVITENDO_EMPTY';
79   }
80
81   my %connect_settings   = (%common_connect_settings, %specific_connect_settings);
82   my %flattened_settings = _flatten_settings(%connect_settings);
83
84   $domain                = 'KIVITENDO' if $type =~ m/^KIVITENDO/;
85   $type                 .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings);
86   my $idx                = "${domain}::${type}";
87
88   if (!$_db_registered{$idx}) {
89     $_db_registered{$idx} = 1;
90
91     __PACKAGE__->register_db(domain => $domain,
92                              type   => $type,
93                              %connect_settings,
94                             );
95   }
96
97   return ($domain, $type);
98 }
99
100 sub _flatten_settings {
101   my %settings  = @_;
102   my %flattened = ();
103
104   while (my ($key, $value) = each %settings) {
105     if ('HASH' eq ref $value) {
106       %flattened = ( %flattened, _flatten_settings(%{ $value }) );
107     } else {
108       $flattened{$key} = $value;
109     }
110   }
111
112   return %flattened;
113 }
114
115 sub with_transaction {
116   my ($self, $code, @args) = @_;
117
118   return $code->(@args) if $self->in_transaction;
119   if (wantarray) {
120     my @result;
121     return $self->do_transaction(sub { @result = $code->(@args) }) ? @result : ();
122
123   } else {
124     my $result;
125     return $self->do_transaction(sub { $result = $code->(@args) }) ? $result : undef;
126   }
127 }
128
129 1;
130 __END__
131
132 =pod
133
134 =encoding utf8
135
136 =head1 NAME
137
138 SL::DB - Database access class for all RDB objects
139
140 =head1 FUNCTIONS
141
142 =over 4
143
144 =item C<create $domain, $type>
145
146 Registers the database information with Rose, creates a cached
147 connection and executes initial SQL statements. Those can include
148 setting the time & date format to the user's preferences.
149
150 =item C<dbi_connect $dsn, $login, $password, $options>
151
152 Forwards the call to L<SL::DBConnect/connect> which connects to the
153 database. This indirection allows L<SL::DBConnect/connect> to route
154 the calls through L<DBIx::Log4Perl> if this is enabled in the
155 configuration.
156
157 =item C<with_transaction $code_ref, @args>
158
159 Executes C<$code_ref> with parameters C<@args> within a transaction,
160 starting one if none is currently active. Example:
161
162   return $self->db->with_transaction(sub {
163     # do stuff with $self
164   });
165
166 One big difference to L<Rose::DB/do_transaction> is the return code
167 handling. If a transaction is already active then C<with_transcation>
168 simply returns the result of calling C<$code_ref> as-is.
169
170 Otherwise the return value depends on the result of the underlying
171 transaction. If the transaction fails then C<undef> is returned in
172 scalar context and an empty list in list context. If the transaction
173 succeeds then the return value of C<$code_ref> is returned preserving
174 context.
175
176 So if you want to differentiate between "transaction failed" and
177 "succeeded" then your C<$code_ref> should never return C<undef>
178 itself.
179
180 =back
181
182 =head1 BUGS
183
184 Nothing here yet.
185
186 =head1 AUTHOR
187
188 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
189
190 =cut