Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / MOBY / central_db_connection.pm
blob4e7af8715a0053c792cad0d49d2a82d1a5fbe4c6
1 package MOBY::central_db_connection;
2 use strict;
3 use Carp;
4 use vars qw($AUTOLOAD @ISA);
5 use MOBY::Config;
7 =head1 NAME
9 MOBY::central_db_connection - container object for a specific DB connection
11 =head1 SYNOPSIS
13 use MOBY::simple_output;
14 my $dbh = MOBY::central_db_connection->new(
15 db_connect_object => "MOBY::mysql",
16 username => "myusername"
17 password => "mypassword",
18 dbname => "dbname",
19 host => "dbhost",
20 port => "3306",
23 $sth = $dbh->prepare("select * from tablename");
26 =cut
28 =head1 DESCRIPTION
30 representation of the authority table. Can write to the database
32 =head1 AUTHORS
34 Mark Wilkinson (mwilkinson@gene.pbi.nrc.ca)
37 =cut
41 # Encapsulated:
42 # DATA
43 #___________________________________________________________
44 #ATTRIBUTES
45 my %_attr_data = # DEFAULT ACCESSIBILITY
47 db_connect_object => [ "MOBY::mysql", 'read/write' ],
48 datasource => [ 'mobycentral', 'read/write' ],
50 #username => ["mobycentral", 'read/write'],
51 #password => ["mobycentral", 'read/write'],
52 #dbname => ["mobycentral", 'read/write'],
53 #host => ["localhost", 'read/write'],
54 #port => [3306, 'read/write'],
55 dbh => [ undef, 'read/write' ],
58 #_____________________________________________________________
59 # METHODS, to operate on encapsulated class data
60 # Is a specified object attribute accessible in a given mode
61 sub _accessible {
62 my ( $self, $attr, $mode ) = @_;
63 $_attr_data{$attr}[1] =~ /$mode/;
66 # Classwide default value for a specified object attribute
67 sub _default_for {
68 my ( $self, $attr ) = @_;
69 $_attr_data{$attr}[0];
72 # List of names of all specified object attributes
73 sub _standard_keys {
74 keys %_attr_data;
77 sub db_connect_object {
78 my ( $self, $attr ) = @_;
79 $self->{db_connect_object} = $attr if defined $attr;
80 return $self->{db_connect_object};
83 sub dbh {
84 my ( $self, $attr ) = @_;
85 $self->{dbh} = $attr if defined $attr;
86 return $self->{dbh};
90 sub new {
91 my ( $caller, %args ) = @_;
92 my $caller_is_obj = ref($caller);
93 return $caller if $caller_is_obj;
94 my $class = $caller_is_obj || $caller;
95 my $proxy;
96 my $self = bless {}, $class;
97 foreach my $attrname ( $self->_standard_keys ) {
98 if ( exists $args{$attrname} ) {
99 $self->{$attrname} = $args{$attrname};
100 } elsif ($caller_is_obj) {
101 $self->{$attrname} = $caller->{$attrname};
102 } else {
103 $self->{$attrname} = $self->_default_for($attrname);
106 $CONFIG ||= MOBY::Config->new;
108 # getting the dbh is bad bad bad!!!
109 my $dbh = $CONFIG->getDataAdaptor( datasource => 'mobycentral' )->dbh;
110 $self->dbh($dbh);
111 return $self;
114 sub AUTOLOAD {
115 no strict "refs";
116 my ( $self, $newval ) = @_;
117 $AUTOLOAD =~ /.*::(\w+)/;
118 my $attr = $1;
119 if ( $self->_accessible( $attr, 'write' ) ) {
120 *{$AUTOLOAD} = sub {
121 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
122 return $_[0]->{$attr};
123 }; ### end of created subroutine
124 ### this is called first time only
125 if ( defined $newval ) {
126 $self->{$attr} = $newval;
128 return $self->{$attr};
129 } elsif ( $self->_accessible( $attr, 'read' ) ) {
130 *{$AUTOLOAD} = sub {
131 return $_[0]->{$attr};
132 }; ### end of created subroutine
133 return $self->{$attr};
136 # Must have been a mistake then...
137 croak "No such method: $AUTOLOAD";
139 sub DESTROY { }