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

Annotation of /FigKernelPackages/DBrtns.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (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.26 my($class,$dbms,$dbname,$dbuser,$dbpass,$dbport, $dbhost, $dbsock) = @_;
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 : olson 1.26 $dbsock = defined($dbsock) ? $dbsock : $FIG_Config::dbsock;
42 : efrank 1.1
43 : olson 1.26 return DBKernel::new($class, $dbms, $dbname, $dbuser, $dbpass, $dbport, $dbhost, $dbsock);
44 : efrank 1.1 }
45 :    
46 : olson 1.11 =head1 get_inserted_id
47 :    
48 :     Return the last ID of a row inserted into an autonumber/serial-containing table.
49 :    
50 :     =cut
51 :    
52 : parrello 1.18 sub get_inserted_id {
53 : overbeek 1.22 my($self, $table, $sth, $id_column) = @_;
54 :    
55 :     $id_column = 'id' unless defined($id_column);
56 : parrello 1.18 if ($self->{_dbms} eq "Pg") {
57 :     my $oid = $sth->{pg_oid_status};
58 : overbeek 1.22 my $ret = $self->SQL("select $id_column from $table where oid = ?", undef, $oid);
59 : parrello 1.18 return $ret->[0]->[0];
60 :     } elsif ($self->{_dbms} eq "mysql") {
61 :     my $id = $self->{_dbh}->{mysql_insertid};
62 :     # print "mysql got $id\n";
63 :     return $id;
64 : olson 1.11 }
65 : olson 1.25 else
66 :     {
67 :     confess "Attempting get_inserted_id on unsupported database $self->{_dbms}\n";
68 :     }
69 : olson 1.11 }
70 :    
71 : olson 1.5 #
72 :     # Following are database administration routines. They create an instance of a ServerAdmin class
73 :     # for the appropriate server type (in order to eliminate the if mysql / if pg / etc stuff).
74 :     #
75 :    
76 :     sub get_server_admin
77 :     {
78 :     if ($FIG_Config::dbms eq "mysql")
79 :     {
80 :     return MysqlAdmin->new();
81 :     }
82 :     elsif ($FIG_Config::dbms eq "Pg")
83 :     {
84 :     return new PostgresAdmin();
85 :     }
86 :     else
87 :     {
88 :     warn "Unknown server type $FIG_Config::dbms\n";
89 :     return undef;
90 :     }
91 :     }
92 :     package MysqlAdmin;
93 :    
94 :     use POSIX;
95 :     use DBI;
96 :    
97 :     sub new
98 :     {
99 :     my($class) = @_;
100 :    
101 :     my $self = {};
102 :    
103 :     return bless($self, $class);
104 :     }
105 :    
106 :     sub init_db
107 :     {
108 :     my($self, $db_dir) = @_;
109 :    
110 :     if (!$db_dir)
111 :     {
112 :     warn "init_db failed: db_dir must be provided\n";
113 :     return;
114 :     }
115 :    
116 :     if (-d "$db_dir/mysql")
117 :     {
118 :     warn "init_db: mysql data directory already exists\n";
119 :     return;
120 :     }
121 :    
122 :     my $exe = "$FIG_Config::ext_bin/mysql_install_db";
123 :     if (! -x $exe)
124 :     {
125 :     $exe = "mysql_install_db";
126 : parrello 1.18 }
127 : olson 1.5
128 : olson 1.26
129 :     my @opts;
130 :    
131 :     push(@opts, "--datadir=$db_dir");
132 :     push(@opts, "--user=$FIG_Config::dbuser");
133 :    
134 :     if (not $FIG_Config::use_system_mysql)
135 :     {
136 :     push(@opts, "--basedir=$FIG_Config::common_runtime")
137 :     }
138 :    
139 :    
140 :     my $rc = system($exe, @opts);
141 : olson 1.5 if ($rc != 0)
142 :     {
143 :     my $err = $?;
144 :     if (WIFEXITED($err))
145 :     {
146 :     my $exitstat = WEXITSTATUS($err);
147 :     warn "init_db failed: $exe returned result code $exitstat\n";
148 :     }
149 :     else
150 :     {
151 :     warn "init_db failed: $exe died with signal ", WTERMSIG($err), "\n";
152 :     }
153 :     return;
154 :     }
155 :    
156 :     return 1;
157 :     }
158 :    
159 :     sub create_database
160 :     {
161 :     my($self, $db_name) = @_;
162 :    
163 :     my $drh = DBI->install_driver("mysql");
164 :    
165 :     my @dbs = DBI->data_sources("mysql", { host => $FIG_Config::dbhost,
166 :     user => $FIG_Config::dbuser,
167 :     password => $FIG_Config::dbpass });
168 :     if (grep { $_ eq $db_name } @dbs)
169 :     {
170 :     warn "Database $db_name already exists\n";
171 :     return;
172 :     }
173 :    
174 :     my $rc = $drh->func('createdb', $db_name, $FIG_Config::dbhost,
175 :     $FIG_Config::dbuser, $FIG_Config::dbpass, 'admin');
176 : parrello 1.18
177 : olson 1.5
178 :     if (!$rc)
179 :     {
180 :     warn "create_database: createdb call failed: $DBI::errstr\n";
181 :     return;
182 :     }
183 : parrello 1.18
184 : olson 1.5 return 1;
185 :     }
186 :    
187 :     sub start_server
188 :     {
189 : olson 1.16 my($self, $dont_fork) = @_;
190 : olson 1.5
191 :     print "Starting mysql server\n";
192 :    
193 :     my(@opts);
194 :    
195 : olson 1.26 my $cnf = "$FIG_Config::fig_disk/config/my.cnf";
196 :    
197 :     if ($FIG_Config::use_system_mysql)
198 :     {
199 :     #
200 :     # This has to be first in the argument list.
201 :     #
202 :     push(@opts, "--defaults-extra-file=$cnf");
203 :     }
204 :    
205 : overbeek 1.27 #
206 :     # Put this first, so config can put --defaults-extra-file here
207 :     # and have it show up first.
208 :     #
209 :     if (@FIG_Config::db_server_startup_options)
210 :     {
211 :     push(@opts, @FIG_Config::db_server_startup_options)
212 :     }
213 :    
214 : olson 1.5 push(@opts, "--port=$FIG_Config::dbport");
215 :     #
216 :     # Don't do this; dbuser isn't the unix uid that we are using.
217 :     #
218 :     #push(@opts, "--user=$FIG_Config::dbuser");
219 : olson 1.26
220 : olson 1.5 push(@opts, "--datadir=$FIG_Config::db_datadir");
221 : olson 1.26
222 :     if ($FIG_Config::use_system_mysql)
223 :     {
224 :     push(@opts, "--err-log=$FIG_Config::temp/mysql.log");
225 :     push(@opts, "--socket=$FIG_Config::dbsock");
226 :    
227 :     #
228 :     # Feh. You can't actually override the socket that /etc/my.cnf
229 :     # sets up, so we need to set up a config/my.cnf with the socket in it.
230 :     #
231 :    
232 :     if (! -f $cnf)
233 :     {
234 :     if (open(F, ">$cnf"))
235 :     {
236 :     print F <<END;
237 :     [mysqld]
238 :     socket=$FIG_Config::dbsock
239 :     END
240 :     close(F);
241 :     }
242 :     }
243 :     }
244 :     else
245 :     {
246 :     push(@opts, "--basedir=$FIG_Config::common_runtime");
247 :     push(@opts, "--ledir=$FIG_Config::common_runtime/libexec");
248 :     }
249 :    
250 :     if (not $FIG_Config::mysql_v3)
251 :     {
252 :     push(@opts, "--old-password");
253 :     push(@opts, "--max-allowed-packet=128M");
254 :     }
255 :    
256 : olson 1.5 #
257 : olson 1.21 # Use InnoDB for large-table support and allegedly better performance.
258 :     #
259 :    
260 : olson 1.23 #push(@opts, "--default-table-type=innodb");
261 : olson 1.21
262 :     #
263 : olson 1.5 # Oddly, this doesn't seem to work. need to set the environment variable.
264 :     #
265 :     #push(@opts, "--port=$FIG_Config::dbport");
266 :    
267 :     #
268 :     # We are going to assume that if mysql has shipped with this release, we'll use it. Otherwise
269 :     # try to use a system one.
270 :     #
271 :    
272 : olson 1.26 my $exe;
273 :     if ($FIG_Config::mysql_v3)
274 :     {
275 :     $exe = "safe_mysqld";
276 :     }
277 :     else
278 :     {
279 :     $exe = "mysqld_safe";
280 :     }
281 : olson 1.21
282 : olson 1.26 if (-x "$FIG_Config::ext_bin/$exe")
283 : olson 1.5 {
284 : olson 1.26 $exe = "$FIG_Config::ext_bin/$exe";
285 : olson 1.5 }
286 :    
287 : olson 1.26 print "Start $exe @opts\n";
288 :    
289 : olson 1.16 if ($dont_fork)
290 : olson 1.5 {
291 :     $ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
292 :     exec $exe, @opts;
293 :     }
294 : olson 1.16 else
295 :     {
296 :     my $pid = fork;
297 :    
298 :     if ($pid == 0)
299 :     {
300 :     POSIX::setsid();
301 : parrello 1.18
302 : olson 1.16 $ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
303 :     exec $exe, @opts;
304 :     }
305 :     print "Forked db server $pid\n";
306 :     }
307 : olson 1.5
308 :     }
309 :    
310 : parrello 1.18 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3