fixed recursive_children cvterm function, and added tests for parents and children
[cxgn-corelibs.git] / lib / CXGN / Chado / Db.pm
blobc0963d52c9f62d3077d27918811f6dca021e9025
1 =head1 NAME
3 CXGN::Chado::Db - a class to handle Db terms.
5 implements an object for the Chado 'db' database table:
7 Column | Type | Modifiers
8 db_id | integer | not null default nextval('public.db_db_id_seq'::text)
9 name | character varying(255) | not null
10 description | character varying(255) |
11 urlprefix | character varying(255) |
12 url | character varying(255) |
15 =head1 SYNOPSIS
17 my $db=CXGN::Chado::Db->new($dbh, $db_id);
18 my $db_name= $db->get_db_name();
20 #do an update:
21 $db->set_description("new description for SGN db");
22 $db->store();
25 #OR store a new db :
26 my $db=CXGN::Chado::Db->new($dbh);
27 $db->set_db_name($db_name);
28 $db->set_urlprefix($prefix);
29 $db->set_url($url);
30 my $db_id=$db->store();
33 =head1 AUTHOR
35 Naama Menda <nm249@cornell.edu>
36 Lukas Mueller <lam87@cornell.edu> (added implementation of Bio::Ontology::TermI)
38 =cut
39 use strict;
41 package CXGN::Chado::Db;
43 use base qw / CXGN::DB::Object /;
46 =head2 new
48 Usage: CXGN::Chado::Db->new($dbh, $id)
49 Desc: a new Db object
50 Ret: Db object
51 Args: a database handle, a database db_id (optional)
52 Side Effects: none
53 Example:
55 =cut
57 sub new {
58 my $class = shift;
59 my $dbh = shift;
60 my $id = shift;
61 my $self= $class->SUPER::new($dbh);
63 if ($id) {
64 $self->set_db_id($id);
65 $self->fetch();
67 return $self;
70 =head2 new_with_name
72 Usage: my $db = CXGN::Chado::Db->new_with_name($dbh, "GO");
73 Desc: an alternate constructor that takes a db name as parameter
74 db name is case insensitive, but if more than one is found
75 only one db_id is returned + a warning.
76 Side Effects: accesses the database.
77 Example:
79 =cut
81 sub new_with_name {
82 my $class = shift;
83 my $dbh = shift;
84 my $name = shift;
86 my $query = "SELECT db_id FROM db WHERE name ilike ?";
87 my $sth = $dbh->prepare($query);
88 $sth->execute($name);
89 my @ids=();
90 while (my ($id) = $sth->fetchrow_array()) {
91 push @ids, $id;
93 if (scalar(@ids) > 1 ) { warn "Db.pm: new with name found more than one db with iname $name! Please check your databse."; }
94 my $db_id= $ids[0] || undef; #return only the 1st id
95 my $self = CXGN::Chado::Db->new($dbh, $db_id);
96 return $self;
102 sub fetch {
103 my $self = shift;
104 my $query = "SELECT name, description, urlprefix, url
105 FROM db
106 WHERE db_id=?";
107 my $sth = $self->get_dbh()->prepare($query);
108 $sth->execute($self->get_db_id());
109 my ($name, $description, $urlprefix, $url) =
110 $sth->fetchrow_array();
112 $self->set_db_name($name);
113 $self->set_description($description);
114 $self->set_urlprefix($urlprefix);
115 $self->set_url($url);
118 =head2 store
120 Usage: $db->store()
121 Desc: store a new db in the database (update if db_id exists)
122 Ret: a database id (db_id)
123 Args: none
124 Side Effects: accesses the database
125 Example:
127 =cut
130 sub store {
131 my $self= shift;
132 my $db_id= $self->get_db_id();
133 if (!$db_id) {
135 my $query = "INSERT INTO public.db (name, description, urlprefix,url) VALUES(?,?,?,?)";
136 my $sth= $self->get_dbh()->prepare($query);
137 $self->d( "Db.pm: storing * " . $self->get_db_name() . "\n\n");
138 $sth->execute($self->get_db_name, $self->get_description, $self->get_urlprefix, $self->get_url());
139 $db_id= $self->get_dbh()->last_insert_id("db", "public");
140 $self->set_db_id($db_id);
141 }else {
142 my $query = "UPDATE public.db SET description=?, urlprefix=? , url=? WHERE db_id=?";
143 my $sth= $self->get_dbh()->prepare($query);
144 $sth->execute($self->get_description(), $self->get_urlprefix(),$self->get_url(), $db_id);
146 return $db_id;
149 =head2 Class properties
151 The following class properties have accessors (get_db_id, set_db_id...):
153 db_id
154 db_name
155 description
156 urlprefix
159 =cut
162 sub get_db_id {
163 my $self=shift;
164 return $self->{db_id};
167 sub set_db_id {
168 my $self=shift;
169 $self->{db_id}=shift;
173 sub get_db_name {
174 my $self=shift;
175 return $self->{db_name};
179 sub set_db_name {
180 my $self=shift;
181 $self->{db_name}=shift;
185 sub get_description {
186 my $self=shift;
187 return $self->{description};
190 sub set_description {
191 my $self=shift;
192 $self->{description}=shift;
196 sub get_urlprefix {
197 my $self=shift;
198 return $self->{urlprefix};
201 sub set_urlprefix {
202 my $self=shift;
203 $self->{urlprefix}=shift;
206 sub get_url {
207 my $self=shift;
208 return $self->{url};
211 sub set_url {
212 my $self=shift;
213 $self->{url}=shift;
218 return 1;