blib/lib/MOBY/central_db_connection.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 117 CC 10 SCAR 24.8
CC
10
Cov
0%
AUTOLOAD
30.0
new
20.0
db_connect_object
6.0
CRAP 11.9
1 package MOBY::central_db_connection;
2 use strict;
3 use Carp;
4 use vars qw($AUTOLOAD @ISA);
5 use MOBY::Config;
6  
7 use vars qw /$VERSION/;
8 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
9  
10 =head1 NAME
11  
12 MOBY::central_db_connection - container object for a specific DB connection
13  
14 =head1 SYNOPSIS
15  
16 use MOBY::simple_output;
17 my $dbh = MOBY::central_db_connection->new(
18 db_connect_object => "MOBY::mysql",
19 username => "myusername"
20 password => "mypassword",
21 dbname => "dbname",
22 host => "dbhost",
23 port => "3306",
24 )
25  
26 $sth = $dbh->prepare("select * from tablename");
27  
28  
29 =cut
30  
31 =head1 DESCRIPTION
32  
33 representation of the authority table. Can write to the database
34  
35 =head1 AUTHORS
36  
37 Mark Wilkinson (mwilkinson@gene.pbi.nrc.ca)
38  
39  
40 =cut
41  
42 {
43  
44 # Encapsulated:
45 # DATA
46 #___________________________________________________________
47 #ATTRIBUTES
48 my %_attr_data = # DEFAULT ACCESSIBILITY
49 (
50 db_connect_object => [ "MOBY::mysql", 'read/write' ],
51 datasource => [ 'mobycentral', 'read/write' ],
52  
53 #username => ["mobycentral", 'read/write'],
54 #password => ["mobycentral", 'read/write'],
55 #dbname => ["mobycentral", 'read/write'],
56 #host => ["localhost", 'read/write'],
57 #port => [3306, 'read/write'],
58 dbh => [ undef, 'read/write' ],
59 );
60  
61 #_____________________________________________________________
62 # METHODS, to operate on encapsulated class data
63 # Is a specified object attribute accessible in a given mode
64 sub _accessible {
65 my ( $self, $attr, $mode ) = @_;
66 $_attr_data{$attr}[1] =~ /$mode/;
67 }
68  
69 # Classwide default value for a specified object attribute
70 sub _default_for {
71 my ( $self, $attr ) = @_;
72 $_attr_data{$attr}[0];
73 }
74  
75 # List of names of all specified object attributes
76 sub _standard_keys {
77 keys %_attr_data;
78 }
79  
80 sub db_connect_object {
81 my ( $self, $attr ) = @_;
82 $self->{db_connect_object} = $attr if defined $attr;
83 return $self->{db_connect_object};
84 }
85  
86 sub dbh {
87 my ( $self, $attr ) = @_;
88 $self->{dbh} = $attr if defined $attr;
89 return $self->{dbh};
90 }
91 }
92  
93 sub new {
94 my ( $caller, %args ) = @_;
95 my $caller_is_obj = ref($caller);
96 return $caller if $caller_is_obj;
97 my $class = $caller_is_obj || $caller;
98 my $proxy;
99 my $self = bless {}, $class;
100 foreach my $attrname ( $self->_standard_keys ) {
101 if ( exists $args{$attrname} ) {
102 $self->{$attrname} = $args{$attrname};
103 } elsif ($caller_is_obj) {
104 $self->{$attrname} = $caller->{$attrname};
105 } else {
106 $self->{$attrname} = $self->_default_for($attrname);
107 }
108 }
109 $CONFIG ||= MOBY::Config->new;
110  
111 # getting the dbh is bad bad bad!!!
112 my $dbh = $CONFIG->getDataAdaptor( datasource => 'mobycentral' )->dbh;
113 $self->dbh($dbh);
114 return $self;
115 }
116  
117 sub AUTOLOAD {
118 no strict "refs";
119 my ( $self, $newval ) = @_;
120 $AUTOLOAD =~ /.*::(\w+)/;
121 my $attr = $1;
122 if ( $self->_accessible( $attr, 'write' ) ) {
123 *{$AUTOLOAD} = sub {
124 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
125 return $_[0]->{$attr};
126 }; ### end of created subroutine
127 ### this is called first time only
128 if ( defined $newval ) {
129 $self->{$attr} = $newval;
130 }
131 return $self->{$attr};
132 } elsif ( $self->_accessible( $attr, 'read' ) ) {
133 *{$AUTOLOAD} = sub {
134 return $_[0]->{$attr};
135 }; ### end of created subroutine
136 return $self->{$attr};
137 }
138  
139 # Must have been a mistake then...
140 croak "No such method: $AUTOLOAD";
141 }
142 sub DESTROY { }
143 1;