Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / CXGN / DB / Ima.pm
blobbbabb705660a2cc453d986b8a774e61dfe2b3e36
1 package CXGN::DB::Ima;
3 =head1 NAME
5 CXGN::Ima::DBI - subclass of Ima::DBI, customizing it for use with the
6 CXGN DB infrastructure
8 =head1 SYNOPSIS
10 ## this is a complete working script, notice you don't
11 ## have to create any database connections
13 package mything;
14 use base qw/ Ima::DBI /;
16 __PACKAGE__->set_sql( get_all_foos => <<EOSQL );
17 select name from foo
18 EOSQL
20 my $sth = __PACKAGE__->sql_get_all_foos;
21 $sth->execute;
22 my $foo_names = $sth->fetchall_arrayref;
24 =head1 DESCRIPTION
26 This is a customized subclass of Ima::DBI, an off-the-shelf,
27 well-tested connection sharing solution used by the popular
28 object-relational mapping package Class::DBI.
30 The reasons to use this package is that ALL SUBCLASSES OF THIS
31 PACKAGE SHARE THE SAME 'cxgn' DATABASE HANDLE.
33 The changes are:
34 - the DB handles it manages are now CXGN::DB::Connections,
35 not regular DBI connections
36 - the DB name in set_sql now defaults to 'cxgn', whereas in
37 vanilla Ima::DBI, it had no default
39 =head1 BASE CLASS(ES)
41 L<Ima::DBI>
43 =head1 AUTHOR(S)
45 Robert Buels
47 =cut
49 use strict;
50 use base qw/ Ima::DBI /;
51 use CXGN::DB::Connection;
53 sub set_db {
54 my $class = shift;
55 my $db_name = shift or $class->_croak("Need a db name");
56 $db_name =~ s/\s/_/g;
58 $class->_remember_handle($db_name);
59 no strict 'refs';
60 *{ $class . "::db_$db_name" } =
61 $class->_make_cxgn_db_closure(@_);
63 return 1;
66 sub _make_cxgn_db_closure {
67 my ($class, @connection) = @_;
68 my $dbh;
69 return sub {
70 unless ($dbh && $dbh->FETCH('Active') && $dbh->ping) {
71 $dbh = CXGN::DB::Connection->new(@connection);
73 return $dbh;
77 #add a 'cxgn' default
78 sub set_sql {
79 my $class = shift;
80 $_[2] ||= 'cxgn';
81 $class->SUPER::set_sql(@_);
84 #make a db_Main with default connection args
85 __PACKAGE__->set_db( 'cxgn' );