File Coverage

blib/lib/SPOPSx/Ginsu/DBI.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package SPOPSx::Ginsu::DBI;
2              
3 7     7   45 use strict;
  7         13  
  7         267  
4 7     7   38 use vars qw($VERSION $DSN $USER $PASS);
  7         15  
  7         646  
5              
6             $VERSION = sprintf "%d.%03d", q$Revision: 1.18 $ =~ /: (\d+)\.(\d+)/;
7              
8 7     7   38 use base qw( SPOPS::DBI::MySQL SPOPS::DBI );
  7         11  
  7         16718  
9             use SPOPS 0.86;
10             use SPOPS::DBI;
11             use SPOPS::DBI::MySQL;
12              
13             sub dbi_connect {
14             my $self = shift;
15             $self->set_dbi_connect_args(@_) if @_;
16             return DBI->connect( $self->DBI_DSN, $self->DBI_USER, $self->DBI_PASS, $self->DBI_OPT );
17             }
18              
19             sub dbi_disconnect {
20             my $self = shift;
21             $self->DBH->disconnect;
22             $self->set_DBH( undef );
23             }
24              
25             sub global_datasource_handle {
26             my ($self) = @_;
27             my $attempts = 1;
28             until ( $self->DBH && $self->DBH->ping ) {
29             if (my $dbh = $self->dbi_connect ) {
30             $self->set_DBH( $dbh );
31             } else {
32             $attempts++;
33             warn "DBI->connect() attempt " . $attempts . " (pid = $$): $DBI::errstr\n";
34             die "DBI->connect() failed: $DBI::errstr" if $attempts > 10;
35             sleep 1 if $attempts > 5;
36             };
37             }
38             return $self->DBH;
39             }
40              
41             sub drop_table {
42             my $class = shift;
43             # This will only work for classes whose config has been processed.
44             # I.e. it won't work for our ClubMembers type of class which is
45             # only used to create a 'links_to' table
46             # my $SQL = "DROP TABLE " . $class->CONFIG->{base_table};
47              
48             # ... so we have to do it this way instead ...
49             my $conf = eval '$' . $class . '::CONF';
50             if ($conf) {
51             my ($alias) = grep $conf->{$_}->{class} eq $class, keys %$conf;
52             my $SQL = "DROP TABLE IF EXISTS " . $conf->{$alias}->{base_table};
53             my $db = $class->global_datasource_handle;
54             $db->do($SQL);
55             }
56            
57             return 1;
58             }
59              
60             sub create_table {
61             my $class = shift;
62             my $SQL = eval '$' . $class . '::TABLE_DEF';
63             if ($SQL) {
64             my $db = $class->global_datasource_handle;
65             $db->do($SQL) or die $db->errstr;
66             }
67             }
68              
69             sub recreate_table {
70             my $class = shift;
71             $class->drop_table;
72             $class->create_table;
73             }
74              
75             1;
76             __END__