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
b82338c9
Commit
b82338c9
authored
17 years ago
by
Patrick Meidl
Browse files
Options
Downloads
Patches
Plain Diff
added new utility methods for file and db access
parent
560aaa8d
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
modules/Bio/EnsEMBL/IdMapping/BaseObject.pm
+126
-0
126 additions, 0 deletions
modules/Bio/EnsEMBL/IdMapping/BaseObject.pm
with
126 additions
and
0 deletions
modules/Bio/EnsEMBL/IdMapping/BaseObject.pm
+
126
−
0
View file @
b82338c9
...
...
@@ -35,6 +35,7 @@ no warnings 'uninitialized';
use
Bio::EnsEMBL::Utils::
Exception
qw(throw warning)
;
use
Bio::EnsEMBL::Utils::
Argument
qw(rearrange)
;
use
Bio::EnsEMBL::Utils::
ScriptUtils
qw(path_append)
;
=head2 new
...
...
@@ -78,6 +79,131 @@ sub new {
}
sub
get_filehandle
{
my
$self
=
shift
;
my
$filename
=
shift
;
my
$path_append
=
shift
;
my
$mode
=
shift
;
throw
("
Need a filename for this filehandle.
")
unless
(
defined
(
$filename
));
my
$path
=
$self
->
conf
->
param
('
dumppath
');
$path
=
path_append
(
$path
,
$path_append
)
if
(
defined
(
$path_append
));
$mode
||=
'
>
';
open
(
my
$fh
,
$mode
,
"
$path
/
$filename
")
or
throw
("
Unable to open
$path
/
$filename
: $!
");
return
$fh
;
}
sub
file_exists
{
my
$self
=
shift
;
my
$filename
=
shift
;
my
$path_append
=
shift
;
my
$path
=
$self
->
conf
->
param
('
dumppath
');
$path
=
path_append
(
$path
,
$path_append
)
if
(
defined
(
$path_append
));
return
(
-
s "$path/$filename");
}
sub fetch_value_from_db {
my $self = shift;
my $dbh = shift;
my $sql = shift;
throw("
Need
a
db
handle
.
"
) unless (
$dbh
and
$dbh
->isa('DBI::db'));
throw(
"
Need
an
SQL
query
to
execute
.
"
) unless (
$sql
);
my
$sth
=
$dbh
->prepare(
$sql
);
$sth
->execute;
my (
$retval
) =
$sth
->fetchrow_array;
return
$retval
;
}
sub dump_table_to_file {
my
$self
= shift;
my
$dbtype
= shift;
my
$table
= shift;
my
$filename
= shift;
# argument check
unless ((
$dbtype
eq 'source') or (
$dbtype
eq 'target')) {
throw(
"
Missing
or
unknown
db
type:
$dbtype
.
"
);
}
throw(
"
Need
a
table
name
.
"
) unless (
$table
);
throw(
"
Need
a
filename
.
"
) unless (
$filename
);
my
$fh
=
$self
->get_filehandle(
$filename
, 'tables');
my
$dba
=
$self
->cache->get_DBAdaptor(
$dbtype
);
my
$dbh
=
$dba
->dbc->db_handle;
my
$sth
=
$dbh
->prepare(
"
SELECT
*
FROM
$table
"
);
$sth
->execute;
my
$i
= 0;
while (my
@row
=
$sth
->fetchrow_array) {
$i
++;
# use '
\
N' for NULL values
for (my
$j
= 0;
$j
< scalar(
@row
);
$j
++) {
$row
[
$j
] = '
\
N' unless (defined(
$row
[
$j
]));
}
print
$fh
join(
"
\
t
"
,
@row
);
print
$fh
"
\
n
"
;
}
$sth
->finish;
return
$i
;
}
sub upload_file_into_table {
my
$self
= shift;
my
$dbtype
= shift;
my
$table
= shift;
my
$filename
= shift;
# argument check
unless ((
$dbtype
eq 'source') or (
$dbtype
eq 'target')) {
throw(
"
Missing
or
unknown
db
type:
$dbtype
.
"
);
}
throw(
"
Need
a
table
name
.
"
) unless (
$table
);
throw(
"
Need
a
filename
.
"
) unless (
$filename
);
# sanity check for dry run
if (
$self
->conf->param('dry_run')) {
$self
->logger->warning(
"
dry_run
-
skipping
db
upload
for
$filename
.\
n
"
);
return;
}
my
$file
= join('/',
$self
->conf->param('dumppath'), 'tables',
$filename
);
if (-s
$file
) {
my
$dba
=
$self
->cache->get_DBAdaptor(
$dbtype
);
my
$dbh
=
$dba
->dbc->db_handle;
my
$sql
= qq(LOAD DATA LOCAL INFILE '
$file
' INTO TABLE
$table
);
my
$sth
=
$dbh
->prepare(
$sql
);
$sth
->execute;
$sth
->finish;
} else {
$self
->logger->warning(
"
No
data
found
in
file
$filename
.\
n
"
, 1);
}
}
sub logger {
my
$self
= shift;
$self
->{'_logger'} = shift if (
@_
);
...
...
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