]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/remove_mysql_tables
added use strict to all biopieces
[biopieces.git] / bp_bin / remove_mysql_tables
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Remove MySQL database tables.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::SQL;
34 use Maasha::UCSC;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $record, %table_hash, $dbh, $table );
41
42 $options = Maasha::Biopieces::parse_options(
43     [
44         { long => 'database', short => 'd', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
45         { long => 'tables',   short => 't', type => 'list',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
46         { long => 'keys',     short => 'k', type => 'list',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
47         { long => 'user',     short => 'u', type => 'string', mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
48         { long => 'password', short => 'p', type => 'string', mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
49     ]   
50 );
51
52 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
54
55 Maasha::Common::error( qq(both --tables and --keys specified) ) if     $options->{ "tables" } and     $options->{ "keys" };
56 Maasha::Common::error( qq(no --tables or --keys specified) )    if not $options->{ "tables" } and not $options->{ "keys" };
57
58 $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
59 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
60
61 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
62
63 while ( $record = Maasha::Biopieces::get_record( $in ) )
64 {
65     map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
66
67     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
68 }
69
70 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
71
72 foreach $table ( sort keys %table_hash )
73 {
74     if ( Maasha::SQL::table_exists( $dbh, $table ) )
75     {
76         print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
77         Maasha::SQL::delete_table( $dbh, $table );
78         print STDERR "done.\n" if $options->{ 'verbose' };
79     }
80     else
81     {
82         print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
83     }
84 }
85
86 Maasha::SQL::disconnect( $dbh );
87
88 Maasha::Biopieces::close_stream( $in );
89 Maasha::Biopieces::close_stream( $out );
90
91
92 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
93
94
95 BEGIN
96 {
97     Maasha::Biopieces::status_set();
98 }
99
100
101 END
102 {
103     Maasha::Biopieces::status_log();
104 }
105
106
107 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
108
109
110 __END__