3 # Copyright (C) 2007-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Remove MySQL database tables.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $user, $password, $in, $out, $record, %table_hash, $dbh, $table );
42 $user = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
43 $password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
45 $options = Maasha::Biopieces::parse_options(
47 { long => 'database', short => 'd', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
48 { long => 'tables', short => 't', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => $user, allowed => undef, disallowed => undef },
51 { long => 'password', short => 'p', type => 'string', mandatory => 'no', default => $password, allowed => undef, disallowed => undef },
55 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
56 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
58 Maasha::Common::error( qq(both --tables and --keys specified) ) if $options->{ "tables" } and $options->{ "keys" };
59 Maasha::Common::error( qq(no --tables or --keys specified) ) if not $options->{ "tables" } and not $options->{ "keys" };
61 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
62 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
64 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
66 while ( $record = Maasha::Biopieces::get_record( $in ) )
68 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
70 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
73 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
75 foreach $table ( sort keys %table_hash )
77 if ( Maasha::SQL::table_exists( $dbh, $table ) )
79 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
80 Maasha::SQL::delete_table( $dbh, $table );
81 print STDERR "done.\n" if $options->{ 'verbose' };
85 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
89 Maasha::SQL::disconnect( $dbh );
91 Maasha::Biopieces::close_stream( $in );
92 Maasha::Biopieces::close_stream( $out );
95 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100 Maasha::Biopieces::status_set();
106 Maasha::Biopieces::status_log();
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<