4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
use base qw(Jifty::Object Class::Accessor::Fast); |
use base qw(Jifty::Object Class::Accessor::Fast); |
7 |
__PACKAGE__->mk_accessors( qw(site uid) ); |
__PACKAGE__->mk_accessors( qw(instance uid) ); |
8 |
|
|
9 |
use DBI; |
use DBI; |
10 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
|
21 |
=head2 new |
=head2 new |
22 |
|
|
23 |
my $strix = Strix->new({ site => 'os-test0604-zg' }); |
my $strix = Strix->new({ instance => 'os-test0604-zg' }); |
24 |
|
|
25 |
=head2 dbh |
=head2 dbh |
26 |
|
|
27 |
my $dbh = Strix->dbh( $site_name ); |
my $dbh = Strix->dbh( $strix_instance ); |
28 |
|
|
29 |
my $dbh = $strix->dbh; |
my $dbh = $strix->dbh; |
30 |
|
|
31 |
=cut |
=cut |
32 |
|
|
33 |
our $site_dbh; |
our $instance_dbh; |
34 |
|
|
35 |
sub dbh { |
sub dbh { |
36 |
my $self = shift; |
my $self = shift; |
37 |
|
|
38 |
my $site = shift || $self->site || confess "no site"; |
my $instance = shift || ref($self) && $self->instance || confess "no instance"; |
39 |
|
|
40 |
return $site_dbh->{$site} if $site_dbh->{$site}; |
return $instance_dbh->{$instance} if $instance_dbh->{$instance}; |
41 |
|
|
42 |
my $config = Jifty->config->app('strix') or die "need strix config"; |
my $config = Jifty->config->app('strix') or die "need strix config"; |
43 |
my $database = $config->{database} or die "no strix.database in config"; |
my $database = $config->{database} or die "no strix.database in config"; |
45 |
Jifty->log->debug("using config ", dump( $database )); |
Jifty->log->debug("using config ", dump( $database )); |
46 |
|
|
47 |
my $dsn = |
my $dsn = |
48 |
'DBI:Pg:dbname=' . $site . |
'DBI:Pg:dbname=' . $instance . |
49 |
';host=' . $database->{host} . |
';host=' . $database->{host} . |
50 |
';port=' . $database->{port}; |
';port=' . $database->{port}; |
51 |
|
|
52 |
Jifty->log->info("Connect to site $site with dsn $dsn"); |
Jifty->log->info("Connect to instance $instance with dsn $dsn"); |
53 |
|
|
54 |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; |
55 |
|
|
56 |
$site_dbh->{$site} = $dbh; |
$instance_dbh->{$instance} = $dbh; |
57 |
|
|
58 |
warn "## site_dbh = ",dump( $site_dbh ) if $debug; |
warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; |
59 |
|
|
60 |
return $dbh; |
return $dbh; |
61 |
} |
} |