| 1 | #!/usr/bin/perl -w | ||
| 2 | package MOBY::mysql; | ||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | use vars qw($AUTOLOAD @ISA); | ||
| 6 | |||
| 7 | use vars qw /$VERSION/; | ||
| 8 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | MOBY::mysql - makes a MYSQL database 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 | ) | ||
| 24 | |||
| 25 | $sth = $dbh->prepare("select * from tablename"); | ||
| 26 | |||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | makes a mysql conneciton to the database. Should be created via | ||
| 33 | a central_db_connection object only! | ||
| 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 | |||
| 51 | #_____________________________________________________________ | ||
| 52 | # METHODS, to operate on encapsulated class data | ||
| 53 | # Is a specified object attribute accessible in a given mode | ||
| 54 | sub _accessible { | ||
| 55 | my ( $self, $attr, $mode ) = @_; | ||
| 56 | $_attr_data{$attr}[1] =~ /$mode/; | ||
| 57 | } | ||
| 58 | |||
| 59 | # Classwide default value for a specified object attribute | ||
| 60 | sub _default_for { | ||
| 61 | my ( $self, $attr ) = @_; | ||
| 62 | $_attr_data{$attr}[0]; | ||
| 63 | } | ||
| 64 | |||
| 65 | # List of names of all specified object attributes | ||
| 66 | sub _standard_keys { | ||
| 67 | keys %_attr_data; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub new { | ||
| 72 | my ( $caller, $dbname, $username, $password, $host, $port ) = @_; | ||
| 73 | my $debug = 0; | ||
| 74 | my $caller_is_obj = ref($caller); | ||
| 75 | return $caller if $caller_is_obj; | ||
| 76 | my $class = $caller_is_obj || $caller; | ||
| 77 | my $proxy; | ||
| 78 | my $self = bless {}, $class; | ||
| 79 | my ($dsn) = "DBI:mysql:$dbname:$host:$port"; | ||
| 80 | $debug | ||
| 81 | && &_LOG("connecting to db with params $dbname, $username, $password\n"); | ||
| 82 | my $dbh = DBI->connect( $dsn, $username, $password, { RaiseError => 1 } ) | ||
| 83 | or die "can't connect to database"; | ||
| 84 | $debug && &_LOG("CONNECTED!\n"); | ||
| 85 | $self->databasehandle($dbh); | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub AUTOLOAD { | ||
| 90 | no strict "refs"; | ||
| 91 | my ( $self, $newval ) = @_; | ||
| 92 | $AUTOLOAD =~ /.*::(\w+)/; | ||
| 93 | my $attr = $1; | ||
| 94 | if ( $self->_accessible( $attr, 'write' ) ) { | ||
| 95 | *{$AUTOLOAD} = sub { | ||
| 96 | if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] } | ||
| 97 | return $_[0]->{$attr}; | ||
| 98 | }; ### end of created subroutine | ||
| 99 | ### this is called first time only | ||
| 100 | if ( defined $newval ) { | ||
| 101 | $self->{$attr} = $newval; | ||
| 102 | } | ||
| 103 | return $self->{$attr}; | ||
| 104 | } elsif ( $self->_accessible( $attr, 'read' ) ) { | ||
| 105 | *{$AUTOLOAD} = sub { | ||
| 106 | return $_[0]->{$attr}; | ||
| 107 | }; ### end of created subroutine | ||
| 108 | return $self->{$attr}; | ||
| 109 | } | ||
| 110 | |||
| 111 | # Must have been a mistake then... | ||
| 112 | croak "No such method: $AUTOLOAD"; | ||
| 113 | } | ||
| 114 | sub DESTROY { } | ||
| 115 | 1; |