Revision 576c2a14
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/DB.pm | ||
---|---|---|
14 | 14 |
__PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere'); |
15 | 15 |
__PACKAGE__->use_private_registry; |
16 | 16 |
|
17 |
my (%_db_registered, %_initial_sql_executed);
|
|
17 |
my (%_db_registered); |
|
18 | 18 |
|
19 | 19 |
sub dbi_connect { |
20 | 20 |
shift; |
... | ... | |
30 | 30 |
|
31 | 31 |
my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type); |
32 | 32 |
|
33 |
_execute_initial_sql($db); |
|
34 |
|
|
35 | 33 |
return $db; |
36 | 34 |
} |
37 | 35 |
|
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' |
|
43 |
); |
|
44 |
|
|
45 | 36 |
sub _register_db { |
46 | 37 |
my $domain = shift; |
47 | 38 |
my $type = shift; |
... | ... | |
49 | 40 |
my %specific_connect_settings; |
50 | 41 |
my %common_connect_settings = ( |
51 | 42 |
driver => 'Pg', |
43 |
european_dates => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0, |
|
52 | 44 |
connect_options => { |
53 | 45 |
pg_enable_utf8 => $::locale && $::locale->is_utf8, |
54 | 46 |
}, |
55 | 47 |
); |
56 | 48 |
|
57 |
if ($::myconfig{dateformat}) { |
|
58 |
$common_connect_settings{european_dates} = 1 if ($_dateformats{ $::myconfig{dateformat} } || '') =~ m/european/i; |
|
59 |
} |
|
60 |
|
|
61 | 49 |
if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) { |
62 | 50 |
%specific_connect_settings = ( |
63 | 51 |
database => $::auth->{DB_config}->{db}, |
... | ... | |
109 | 97 |
return ($domain, $type); |
110 | 98 |
} |
111 | 99 |
|
112 |
sub _execute_initial_sql { |
|
113 |
my ($db) = @_; |
|
114 |
|
|
115 |
return if $_initial_sql_executed{$db} || !%::myconfig || !$::myconfig{dateformat}; |
|
116 |
|
|
117 |
$_initial_sql_executed{$db} = 1; |
|
118 |
|
|
119 |
# Don't rely on dboptions being set properly. Chose them from |
|
120 |
# dateformat instead. |
|
121 |
my $pg_dateformat = $_dateformats{ $::myconfig{dateformat} }; |
|
122 |
$db->dbh->do("set DateStyle to '${pg_dateformat}'") if $pg_dateformat; |
|
123 |
} |
|
124 |
|
|
125 | 100 |
sub _flatten_settings { |
126 | 101 |
my %settings = @_; |
127 | 102 |
my %flattened = (); |
SL/DBConnect.pm | ||
---|---|---|
5 | 5 |
use DBI; |
6 | 6 |
use SL::DB; |
7 | 7 |
|
8 |
sub connect { |
|
8 |
my %dateformat_to_datestyle = ( |
|
9 |
'yy-mm-dd' => 'ISO', |
|
10 |
'yyyy-mm-dd' => 'ISO', |
|
11 |
'mm/dd/yy' => 'SQL, US', |
|
12 |
'dd/mm/yy' => 'SQL, EUROPEAN', |
|
13 |
'dd.mm.yy' => 'GERMAN' |
|
14 |
); |
|
15 |
|
|
16 |
sub _connect { |
|
9 | 17 |
my ($self, @args) = @_; |
10 | 18 |
@args = $self->get_connect_args if !@args; |
11 | 19 |
|
... | ... | |
22 | 30 |
return DBIx::Log4perl->connect(@args); |
23 | 31 |
} |
24 | 32 |
|
33 |
sub connect { |
|
34 |
my ($self, @args) = @_; |
|
35 |
|
|
36 |
my $dbh = $self->_connect(@args); |
|
37 |
return undef if !$dbh; |
|
38 |
|
|
39 |
my $initial_sql = $self->get_initial_sql; |
|
40 |
$dbh->do($initial_sql) if $initial_sql; |
|
41 |
|
|
42 |
return $dbh; |
|
43 |
} |
|
44 |
|
|
45 |
sub get_datestyle { |
|
46 |
my ($self, $dateformat) = @_; |
|
47 |
return $dateformat_to_datestyle{ $dateformat || $::myconfig{dateformat} }; |
|
48 |
} |
|
49 |
|
|
50 |
sub get_initial_sql { |
|
51 |
my ($self) = @_; |
|
52 |
|
|
53 |
return undef if !%::myconfig || !$::myconfig{dateformat}; |
|
54 |
|
|
55 |
my $datestyle = $self->get_datestyle; |
|
56 |
return $datestyle ? qq|SET DateStyle to '${datestyle}'| : ''; |
|
57 |
} |
|
58 |
|
|
25 | 59 |
sub get_connect_args { |
26 | 60 |
my ($self, @args) = @_; |
27 | 61 |
my ($domain, $type) = SL::DB::_register_db(SL::DB->default_domain, 'KIVITENDO'); |
... | ... | |
97 | 131 |
parameter to L<DBI/connect>). They're merged with default settings by |
98 | 132 |
filtering them through L/get_options>. |
99 | 133 |
|
134 |
=item C<get_datestyle [$dateformat]> |
|
135 |
|
|
136 |
Returns the appropriate value for the C<SET DateStyle to...> SQL call |
|
137 |
depending on C<$dateformat> (e.g. C<SQL, EUROPEAN> if C<$dateformat> |
|
138 |
equals C<dd.mm.yy>). If C<$dateformat> is not given then it defaults |
|
139 |
to C<$::myconfig{dateformat}>. |
|
140 |
|
|
141 |
=item C<get_initial_sql> |
|
142 |
|
|
143 |
Returns SQL commands that should be executed right after a connection |
|
144 |
has been established. This is usually the call to configure the |
|
145 |
C<DateStyle> format used by the database. |
|
146 |
|
|
100 | 147 |
=item C<get_options [%options]> |
101 | 148 |
|
102 | 149 |
Returns a hash reference of database options (fourth parameter to |
Auch abrufbar als: Unified diff
Handling vom initialen SQL (SET DateStyle ...) zentralisiert
Damit auch das Datumsformat für $::form->get_standard_dbh und
Konsorten gefixt.