Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ensembl
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ensembl-gh-mirror
ensembl
Commits
02173540
Commit
02173540
authored
12 years ago
by
Magali Ruffier
Browse files
Options
Downloads
Patches
Plain Diff
script to run a single query across several databases
can be run across several hosts at a time
parent
d641354d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
misc-scripts/multidb_sql.pl
+129
-0
129 additions, 0 deletions
misc-scripts/multidb_sql.pl
with
129 additions
and
0 deletions
misc-scripts/multidb_sql.pl
0 → 100644
+
129
−
0
View file @
02173540
#!/usr/bin/env perl
###############################################################################################################
# multidb_sql script
# given a pattern of databases to query, runs a query accross all databases
# can take a file as input, to apply sql without printing out results
# USAGE : perl multidb_sql.pl -dbpattern _core_ -expr "select count(*) from gene"
################################################################################################################
use
strict
;
use
DBI
;
use
Getopt::
Long
;
my
(
@hosts
,
$host
,
$user
,
$pass
,
$port
,
$expression
,
$dbpattern
,
$file
,
$result_only
);
$user
=
'
ensro
'
;
$pass
=
''
;
$port
=
3306
;
@hosts
=
qw (
ens
-
staging1
ens
-
staging2
)
;
GetOptions
(
"
host|dbhost=s
",
\
$host
,
"
hosts|dbhosts=s
",
\
@hosts
,
"
user|dbuser=s
",
\
$user
,
"
pass|dbpass=s
",
\
$pass
,
"
port|dbport=i
",
\
$port
,
"
expr=s
",
\
$expression
,
"
file=s
",
\
$file
,
"
dbpattern|pattern=s
",
\
$dbpattern
,
"
result_only!
",
\
$result_only
,
);
if
(
$host
)
{
@hosts
=
$host
;
}
else
{
@hosts
=
split
(
/,/
,
join
('
,
',
@hosts
))
;
}
foreach
my
$host
(
@hosts
)
{
my
$dsn
=
"
DBI:mysql:host=
$host
";
if
(
$port
)
{
$dsn
.=
"
;port=
$port
";
}
my
@expressions
;
if
(
$file
)
{
local
*FH
;
if
(
!
-
r
$file
)
{
die
(
"
File
$file
not readable
"
);
}
open
(
FH
,
"
<
$file
"
);
my
$exp
;
while
(
my
$line
=
<
FH
>
)
{
if
(
$line
=~
/;$/
)
{
$line
=~
s/;$//
;
$exp
.=
"
"
.
$line
;
push
(
@expressions
,
$exp
);
$exp
=
"";
}
else
{
$exp
.=
"
"
.
$line
;
}
}
if
(
$exp
)
{
push
(
@expressions
,
$exp
);
}
close
FH
;
}
my
$db
=
DBI
->
connect
(
$dsn
,
$user
,
$pass
);
my
@dbnames
=
map
{
$_
->
[
0
]
}
@
{
$db
->
selectall_arrayref
(
"
show databases
"
)
};
for
my
$dbname
(
@dbnames
)
{
next
if
(
$dbname
!~
/$dbpattern/
);
unless
(
$result_only
)
{
print
"
$dbname
\n
";
}
if
((
!
$expression
)
&&
(
!
$file
))
{
next
;
}
$db
->
do
(
"
use
$dbname
"
);
if
(
$file
)
{
for
my
$sql
(
@expressions
)
{
print
STDERR
"
Do
$sql
\n
";
$db
->
do
(
$sql
);
}
}
elsif
(
$expression
=~
/^\s*select/i
||
$expression
=~
/^\s*show/i
||
$expression
=~
/^\s*desc/i
)
{
my
$res
=
$db
->
selectall_arrayref
(
$expression
);
my
@results
=
map
{
join
(
"
",
@
$_
)
}
@$res
;
my
$db_name_off
=
0
;
for
my
$result
(
@results
)
{
if
(
$result_only
){
unless
(
$db_name_off
){
$db_name_off
=
1
;
print
"
==>
$dbname
:
\n
";
}
}
print
"
Result:
",
$result
,"
\n
";
}
}
else
{
$db
->
do
(
$expression
);
print
"
done.
\n
";
}
}
}
sub
usage
{
print
STDERR
<<EOF
Usage: multidb_sql options
Where options are: -host hostname
-user username
-pass password
-port port_of_server optional
-dbpattern regular expression that the database name has to match
-expr sql statement you want to execute.
if omitted, just print database names matching
if select, show or describe prints results
-file Apply sql in file to all databases. Does not print results.
-result_only only prints out databases for which results where found
EOF
;
exit
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment