fixed recursive_children cvterm function, and added tests for parents and children
[cxgn-corelibs.git] / lib / MOBY / mysql.pm
blobc4a79aae70f711e925576a27eb16a161fbe67172
1 #!/usr/bin/perl -w
2 package MOBY::mysql;
3 use strict;
4 use Carp;
5 use vars qw($AUTOLOAD @ISA);
7 =head1 NAME
9 MOBY::mysql - makes a MYSQL database 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",
22 $sth = $dbh->prepare("select * from tablename");
25 =cut
27 =head1 DESCRIPTION
29 makes a mysql conneciton to the database. Should be created via
30 a central_db_connection object only!
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
46 ();
48 #_____________________________________________________________
49 # METHODS, to operate on encapsulated class data
50 # Is a specified object attribute accessible in a given mode
51 sub _accessible {
52 my ( $self, $attr, $mode ) = @_;
53 $_attr_data{$attr}[1] =~ /$mode/;
56 # Classwide default value for a specified object attribute
57 sub _default_for {
58 my ( $self, $attr ) = @_;
59 $_attr_data{$attr}[0];
62 # List of names of all specified object attributes
63 sub _standard_keys {
64 keys %_attr_data;
68 sub new {
69 my ( $caller, $dbname, $username, $password, $host, $port ) = @_;
70 my $debug = 0;
71 my $caller_is_obj = ref($caller);
72 return $caller if $caller_is_obj;
73 my $class = $caller_is_obj || $caller;
74 my $proxy;
75 my $self = bless {}, $class;
76 my ($dsn) = "DBI:mysql:$dbname:$host:$port";
77 $debug
78 && &_LOG("connecting to db with params $dbname, $username, $password\n");
79 my $dbh = DBI->connect( $dsn, $username, $password, { RaiseError => 1 } )
80 or die "can't connect to database";
81 $debug && &_LOG("CONNECTED!\n");
82 $self->databasehandle($dbh);
83 return $self;
86 sub AUTOLOAD {
87 no strict "refs";
88 my ( $self, $newval ) = @_;
89 $AUTOLOAD =~ /.*::(\w+)/;
90 my $attr = $1;
91 if ( $self->_accessible( $attr, 'write' ) ) {
92 *{$AUTOLOAD} = sub {
93 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
94 return $_[0]->{$attr};
95 }; ### end of created subroutine
96 ### this is called first time only
97 if ( defined $newval ) {
98 $self->{$attr} = $newval;
100 return $self->{$attr};
101 } elsif ( $self->_accessible( $attr, 'read' ) ) {
102 *{$AUTOLOAD} = sub {
103 return $_[0]->{$attr};
104 }; ### end of created subroutine
105 return $self->{$attr};
108 # Must have been a mistake then...
109 croak "No such method: $AUTOLOAD";
111 sub DESTROY { }