2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 ###############################################################################################################
20 # given a pattern of databases to query, runs a query accross all databases
21 # can take a file as input, to apply sql without printing out results
22 # USAGE : perl multidb_sql.pl -dbpattern _core_ -expr "select count(*) from gene"
23 ################################################################################################################
34 my ( @hosts, $host, $user, $pass, $port, $expression, $dbpattern, $file, $result_only );
39 @hosts = qw ( mysql-ens-sta-1 ) ;
41 GetOptions(
"host|dbhost=s", \$host,
42 "hosts|dbhosts=s", \@hosts,
43 "user|dbuser=s", \$user,
44 "pass|dbpass=s", \$pass,
45 "port|dbport=i", \$port,
46 "expr=s", \$expression,
48 "dbpattern|pattern=s", \$dbpattern,
49 "result_only!", \$result_only,
56 @hosts = split(/,/,join(
',',@hosts)) ;
59 foreach my $host ( @hosts ) {
60 my $dsn =
"DBI:mysql:host=$host";
62 $dsn .=
";port=$port";
70 die (
"File $file not readable" );
74 while( my $line = <FH> ) {
78 push( @expressions, $exp );
85 push( @expressions, $exp );
90 my $db = DBI->connect( $dsn, $user, $pass ) or
91 die
"Unable to connect to database(s): $dsn, $user";
92 my @dbnames =
map {$_->[0] } @{ $db->selectall_arrayref(
"show databases" ) };
93 for my $dbname ( @dbnames) {
94 next
if ( $dbname !~ /$dbpattern/ );
95 unless ($result_only) {
96 print
"$dbname" .
"@" .
"$host\n";
98 if(( ! $expression ) && ( !$file )) {
102 $db->do(
"use $dbname" );
104 for my $sql ( @expressions ) {
105 print STDERR
"Do $sql\n";
108 } elsif( $expression =~ /^\s*select/i || $expression =~ /^\s*show/i || $expression =~ /^\s*desc/i ) {
109 my $res = $db->selectall_arrayref( $expression );
110 my @results =
map { join(
" ", @$_ ) } @$res ;
111 my $db_name_off = 0 ;
112 for my $result ( @results ) {
114 unless ($db_name_off){
116 print
"==> $dbname" .
"@" . $host .
" :\n";
119 print
" Result: ",$result,
"\n";
122 $db->do( $expression );
131 Usage: multidb_sql options
132 Where options are: -host hostname
135 -port port_of_server optional
136 -dbpattern regular expression that the database name has to match
137 -expr sql statement you want to execute.
138 if omitted, just print database names matching
139 if select, show or describe prints results
140 -file Apply sql in file to all databases. Does not print results.
141 -result_only only prints out databases
for which results where found