[Bio] / FigKernelPackages / DBrtns.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/DBrtns.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (view) (download) (as text)

1 : olson 1.24 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : efrank 1.1 package DBrtns;
19 :    
20 : parrello 1.18 # Inherit the DBKernel methods. We must do this BEFORE the "use strict".
21 :     use DBKernel;
22 :     @ISA = qw(DBKernel);
23 :    
24 : efrank 1.1 use strict;
25 : olson 1.5 use POSIX;
26 : efrank 1.1 use DBI;
27 :     use FIG_Config;
28 :    
29 :     use Data::Dumper;
30 : overbeek 1.2 use Carp;
31 :    
32 :     sub new {
33 : olson 1.19 my($class,$dbms,$dbname,$dbuser,$dbpass,$dbport, $dbhost) = @_;
34 : efrank 1.1
35 :     $dbms = defined($dbms) ? $dbms : $FIG_Config::dbms;
36 :     $dbname = defined($dbname) ? $dbname : $FIG_Config::db;
37 :     $dbuser = defined($dbuser) ? $dbuser : $FIG_Config::dbuser;
38 :     $dbpass = defined($dbpass) ? $dbpass : $FIG_Config::dbpass;
39 :     $dbport = defined($dbport) ? $dbport : $FIG_Config::dbport;
40 : olson 1.19 $dbhost = defined($dbhost) ? $dbhost : $FIG_Config::dbhost;
41 : efrank 1.1
42 : olson 1.19 return DBKernel::new($class, $dbms, $dbname, $dbuser, $dbpass, $dbport, $dbhost);
43 : efrank 1.1 }
44 :    
45 : olson 1.11 =head1 get_inserted_id
46 :    
47 :     Return the last ID of a row inserted into an autonumber/serial-containing table.
48 :    
49 :     =cut
50 :    
51 : parrello 1.18 sub get_inserted_id {
52 : overbeek 1.22 my($self, $table, $sth, $id_column) = @_;
53 :    
54 :     $id_column = 'id' unless defined($id_column);
55 : parrello 1.18 if ($self->{_dbms} eq "Pg") {
56 :     my $oid = $sth->{pg_oid_status};
57 : overbeek 1.22 my $ret = $self->SQL("select $id_column from $table where oid = ?", undef, $oid);
58 : parrello 1.18 return $ret->[0]->[0];
59 :     } elsif ($self->{_dbms} eq "mysql") {
60 :     my $id = $self->{_dbh}->{mysql_insertid};
61 :     # print "mysql got $id\n";
62 :     return $id;
63 : olson 1.11 }
64 : olson 1.25 else
65 :     {
66 :     confess "Attempting get_inserted_id on unsupported database $self->{_dbms}\n";
67 :     }
68 : olson 1.11 }
69 :    
70 : olson 1.5 #
71 :     # Following are database administration routines. They create an instance of a ServerAdmin class
72 :     # for the appropriate server type (in order to eliminate the if mysql / if pg / etc stuff).
73 :     #
74 :    
75 :     sub get_server_admin
76 :     {
77 :     if ($FIG_Config::dbms eq "mysql")
78 :     {
79 :     return MysqlAdmin->new();
80 :     }
81 :     elsif ($FIG_Config::dbms eq "Pg")
82 :     {
83 :     return new PostgresAdmin();
84 :     }
85 :     else
86 :     {
87 :     warn "Unknown server type $FIG_Config::dbms\n";
88 :     return undef;
89 :     }
90 :     }
91 :     package MysqlAdmin;
92 :    
93 :     use POSIX;
94 :     use DBI;
95 :    
96 :     sub new
97 :     {
98 :     my($class) = @_;
99 :    
100 :     my $self = {};
101 :    
102 :     return bless($self, $class);
103 :     }
104 :    
105 :     sub init_db
106 :     {
107 :     my($self, $db_dir) = @_;
108 :    
109 :     if (!$db_dir)
110 :     {
111 :     warn "init_db failed: db_dir must be provided\n";
112 :     return;
113 :     }
114 :    
115 :     if (-d "$db_dir/mysql")
116 :     {
117 :     warn "init_db: mysql data directory already exists\n";
118 :     return;
119 :     }
120 :    
121 :     my $exe = "$FIG_Config::ext_bin/mysql_install_db";
122 :     if (! -x $exe)
123 :     {
124 :     $exe = "mysql_install_db";
125 : parrello 1.18 }
126 : olson 1.5
127 :     my $rc = system($exe,
128 :     "--datadir=$db_dir",
129 : olson 1.15 "--basedir=$FIG_Config::common_runtime",
130 : olson 1.5 "--user=$FIG_Config::dbuser");
131 :     if ($rc != 0)
132 :     {
133 :     my $err = $?;
134 :     if (WIFEXITED($err))
135 :     {
136 :     my $exitstat = WEXITSTATUS($err);
137 :     warn "init_db failed: $exe returned result code $exitstat\n";
138 :     }
139 :     else
140 :     {
141 :     warn "init_db failed: $exe died with signal ", WTERMSIG($err), "\n";
142 :     }
143 :     return;
144 :     }
145 :    
146 :     return 1;
147 :     }
148 :    
149 :     sub create_database
150 :     {
151 :     my($self, $db_name) = @_;
152 :    
153 :     my $drh = DBI->install_driver("mysql");
154 :    
155 :     my @dbs = DBI->data_sources("mysql", { host => $FIG_Config::dbhost,
156 :     user => $FIG_Config::dbuser,
157 :     password => $FIG_Config::dbpass });
158 :     if (grep { $_ eq $db_name } @dbs)
159 :     {
160 :     warn "Database $db_name already exists\n";
161 :     return;
162 :     }
163 :    
164 :     my $rc = $drh->func('createdb', $db_name, $FIG_Config::dbhost,
165 :     $FIG_Config::dbuser, $FIG_Config::dbpass, 'admin');
166 : parrello 1.18
167 : olson 1.5
168 :     if (!$rc)
169 :     {
170 :     warn "create_database: createdb call failed: $DBI::errstr\n";
171 :     return;
172 :     }
173 : parrello 1.18
174 : olson 1.5 return 1;
175 :     }
176 :    
177 :     sub start_server
178 :     {
179 : olson 1.16 my($self, $dont_fork) = @_;
180 : olson 1.5
181 :     print "Starting mysql server\n";
182 :    
183 :     my(@opts);
184 :    
185 :     push(@opts, "--port=$FIG_Config::dbport");
186 :     #
187 :     # Don't do this; dbuser isn't the unix uid that we are using.
188 :     #
189 :     #push(@opts, "--user=$FIG_Config::dbuser");
190 :     push(@opts, "--basedir=$FIG_Config::common_runtime");
191 :     push(@opts, "--datadir=$FIG_Config::db_datadir");
192 :     push(@opts, "--ledir=$FIG_Config::common_runtime/libexec");
193 : olson 1.6 push(@opts, "--old-password");
194 :     push(@opts, "--max-allowed-packet=128M");
195 : olson 1.5 #
196 : olson 1.21 # Use InnoDB for large-table support and allegedly better performance.
197 :     #
198 :    
199 : olson 1.23 #push(@opts, "--default-table-type=innodb");
200 : olson 1.21
201 :     #
202 : olson 1.5 # Oddly, this doesn't seem to work. need to set the environment variable.
203 :     #
204 :     #push(@opts, "--port=$FIG_Config::dbport");
205 :    
206 : olson 1.15 if (@FIG_Config::db_server_startup_options)
207 :     {
208 :     push(@opts, @FIG_Config::db_server_startup_options)
209 :     }
210 :    
211 : olson 1.5 #
212 :     # We are going to assume that if mysql has shipped with this release, we'll use it. Otherwise
213 :     # try to use a system one.
214 :     #
215 :    
216 :     my $exe = "$FIG_Config::ext_bin/mysqld_safe";
217 : olson 1.21
218 :     print "Start $exe @opts\n";
219 : olson 1.5 if (! -x $exe)
220 :     {
221 :     $exe = "mysqld_safe";
222 :     }
223 :    
224 : olson 1.16 if ($dont_fork)
225 : olson 1.5 {
226 :     $ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
227 :     exec $exe, @opts;
228 :     }
229 : olson 1.16 else
230 :     {
231 :     my $pid = fork;
232 :    
233 :     if ($pid == 0)
234 :     {
235 :     POSIX::setsid();
236 : parrello 1.18
237 : olson 1.16 $ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
238 :     exec $exe, @opts;
239 :     }
240 :     print "Forked db server $pid\n";
241 :     }
242 : olson 1.5
243 :     }
244 :    
245 : parrello 1.18 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3