ensembl-hive  2.7.0
multidb_sql.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 
17 
18 ###############################################################################################################
19 # multidb_sql script
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 ################################################################################################################
24 
25 
26 
27 
28 use strict;
29 use warnings;
30 
31 use DBI;
32 use Getopt::Long;
33 
34 my ( @hosts, $host, $user, $pass, $port, $expression, $dbpattern, $file, $result_only );
35 
36 $user = 'ensro' ;
37 $pass = '' ;
38 $port = 4519;
39 @hosts = qw ( mysql-ens-sta-1 ) ;
40 
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,
47  "file=s", \$file,
48  "dbpattern|pattern=s", \$dbpattern,
49  "result_only!", \$result_only,
50  );
51 
52 if ($host) {
53  @hosts = $host ;
54 }
55 else {
56  @hosts = split(/,/,join(',',@hosts)) ;
57 }
58 
59 foreach my $host ( @hosts ) {
60  my $dsn = "DBI:mysql:host=$host";
61  if( $port ) {
62  $dsn .= ";port=$port";
63  }
64 
65  my @expressions;
66 
67  if( $file ) {
68  local *FH;
69  if( ! -r $file ) {
70  die ( "File $file not readable" );
71  }
72  open( FH, "<$file" );
73  my $exp;
74  while( my $line = <FH> ) {
75  if( $line =~ /;$/ ) {
76  $line =~ s/;$//;
77  $exp .= " ".$line;
78  push( @expressions, $exp );
79  $exp = "";
80  } else {
81  $exp .= " ".$line;
82  }
83  }
84  if( $exp ) {
85  push( @expressions, $exp );
86  }
87  close FH;
88  }
89 
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";
97  }
98  if(( ! $expression ) && ( !$file )) {
99  next;
100  }
101 
102  $db->do( "use $dbname" );
103  if( $file ) {
104  for my $sql ( @expressions ) {
105  print STDERR "Do $sql\n";
106  $db->do( $sql );
107  }
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 ) {
113  if($result_only){
114  unless ($db_name_off){
115  $db_name_off =1 ;
116  print "==> $dbname" . "@" . $host . " :\n";
117  }
118  }
119  print " Result: ",$result,"\n";
120  }
121  } else {
122  $db->do( $expression );
123  print " done.\n";
124  }
125  }
126 }
127 
128 sub usage {
129  print STDERR <<EOF
130 
131  Usage: multidb_sql options
132  Where options are: -host hostname
133  -user username
134  -pass password
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
142 
143 EOF
144 ;
145  exit;
146 }
147 
map
public map()
usage
public usage()