Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / CXGN / Insitu / DB.pm
blobecc4c7db10b068a38ea91f17ed9a6426735d5140
3 use strict;
5 package CXGN::Insitu::DB;
7 sub new {
8 my $class = shift;
9 my $dbh = shift;
11 if (!$dbh) {
12 die "CXGN::Insitu::DB: need a database handle";
14 unless( ref($dbh) && $dbh->can('selectall_arrayref') ) {
15 die "Need a database handle as argument!\n";
17 my $self = bless {}, $class;
19 $self->set_dbh($dbh);
21 return $self;
24 =head2 get_dbh, set_dbh
26 Usage: Accessors for the dbh property
27 Desc: the database handle is set in the
28 constructor and it should never be
29 necessary to call the setter. Always use
30 the getter to obtain a database handle,
31 do not try to cache it in some cheesy variable.
32 Ret: setter: database handle (fresh!)
33 Args: getter: a valid database handle, preferably
34 in the form of a CXGN::DB::Connection object.
35 Side Effects: this database handle is used in all database
36 transactions of this object.
37 Example:
39 =cut
41 sub get_dbh {
42 my $self=shift;
43 return $self->{dbh};
47 sub set_dbh {
48 my $self=shift;
49 $self->{dbh}=shift;
52 =head2 stats
54 Synopsis: my ($exp_count, $image_count, $tag_count)
55 = $insitu ->stats();
56 Arguments: none
57 Returns: a list of three values, representing the
58 number of experiments, images and counts
59 associated with the insitu database.
60 Side effects: none
61 Description:
63 =cut
65 sub stats {
66 my $self = shift;
68 my $e_count_q = "SELECT count(*) FROM insitu.experiment";
69 my $eh = $self->get_dbh()->prepare($e_count_q);
70 $eh->execute();
71 my ($e_count) = $eh->fetchrow_array();
73 my $i_count_q = "SELECT count(*) FROM metadata.md_image join insitu.experiment_image using (image_id)";
74 my $ih = $self->get_dbh()->prepare($i_count_q);
75 $ih->execute();
76 my ($i_count) = $ih->fetchrow_array();
78 my $t_count_q = "SELECT count(*) FROM metadata.md_tag join insitu.experiment_tag using (tag_id)";
79 my $th = $self->get_dbh()->prepare($t_count_q);
80 $th->execute();
81 my ($t_count) = $th->fetchrow_array();
83 return ($e_count, $i_count, $t_count);
86 return 1;