ensembl
SchemaConverter
Toolbar
Package variables
No package variables defined.
Included modules
Synopsis
No synopsis!
Description
No description!
Methods
big_table | No description | Code |
check_possible_transfer | No description | Code |
clear_target | No description | Code |
close_dbh | No description | Code |
column_rename | No description | Code |
column_skip | No description | Code |
custom_select | No description | Code |
do_first | No description | Code |
new | No description | Code |
read_dbs | No description | Code |
set_row_modifier | No description | Code |
source_dbh | No description | Code |
standard_table_transfer | No description | Code |
table_rename | No description | Code |
table_skip | No description | Code |
target_dbh | No description | Code |
tmp_dir | No description | Code |
transfer | No description | Code |
Methods description
None available.
Methods code
sub big_table
{ my ( $self, $table ) = @_;
$self->{targetdb}{tables}{$table}{its_a_big_un} = 1; } |
sub check_possible_transfer
{ my $self = shift;
for my $tablename ( keys %{$self->{targetdb}{tables}} ) {
my $sourcetable;
if( exists $self->{targetdb}{tables}{$tablename}{transfer} ) {
next;
}
if( exists $self->{targetdb}{tables}{$tablename}{link} ) {
$sourcetable = $self->{targetdb}{tables}{$tablename}{link};
if( $sourcetable eq "" ) {
next;
}
} else {
if( exists $self->{targetdb}{tables}{$tablename}{select} ) {
next;
} elsif( ! exists $self->{sourcedb}{tables}{$tablename} ) {
die "Couldnt find source for $tablename. Enter empty sourcetable.";
} else {
$sourcetable = $tablename;
}
}
my @newcols = @{$self->{targetdb}{tables}{$tablename}{columns}};
my @oldcols = @{$self->{sourcedb}{tables}{$sourcetable}{columns}};
my %rename;
if( exists $self->{targetdb}{tables}{$tablename}{columnrename} ) {
%rename = %{$self->{targetdb}{tables}{$tablename}{columnrename}};
} else {
%rename = ();
}
for my $colname ( @newcols ) {
my $selname;
if( exists $rename{$colname} ) {
$selname = $rename{$colname};
if( $selname eq "" ) {
$selname = "NULL";
}
} else {
my $colExists = 0;
for my $oldcol ( @oldcols ) {
if( $oldcol eq $colname ) {
$selname = $colname;
$colExists = 1;
last;
}
}
if( ! $colExists ) {
die "Couldnt fill $tablename.$colname\n";
}
}
}
} } |
sub clear_target
{ my $self = shift;
for my $tablename ( keys %{$self->{targetdb}{tables}} ) {
$self->target_dbh()->do( "delete from $tablename" );
} } |
sub close_dbh
{ my $self = shift;
$self->source_dbh()->disconnect();
$self->target_dbh()->disconnect(); } |
sub column_rename
{ my ( $self, $newtable, $oldcol, $newcol ) = @_;
$self->{targetdb}{tables}{$newtable}{columnrename}{$newcol} = $oldcol; } |
sub column_skip
{ my ( $self, $newtable, $newcol ) = @_;
$self->{targetdb}{tables}{$newtable}{columnrename}{$newcol} = ""; } |
sub custom_select
{ my ( $self, $newtable, $select ) = @_;
$self->{targetdb}{tables}{$newtable}{select} = $select; } |
sub do_first
{ my $self = shift;
my @ordered_table_list= @_;
$self->{targetdb}{dofirst} =\@ ordered_table_list;
}
1; } |
sub new
{ my ( $class, @args ) = @_;
my $self = {};
bless $self, $class;
$self->source_dbh( $args[0] );
$self->target_dbh( $args[1] );
$self->read_dbs();
return $self; } |
sub read_dbs
{ my $self = shift;
my $dbh;
for my $db_name ('targetdb', 'sourcedb' ) {
if( $db_name eq 'targetdb' ) {
$dbh = $self->target_dbh();
} else {
$dbh = $self->source_dbh();
}
my $sth = $dbh->prepare( "show tables" );
$sth->execute();
while( my $arref = $sth->fetchrow_arrayref() ) {
$self->{$db_name}{tables}{$arref->[0]} = {};
}
my @tables = keys %{$self->{$db_name}{tables}};
for my $table ( @tables ) {
$sth = $dbh->prepare( "show columns from $table" );
$sth->execute();
while( my $arref = $sth->fetchrow_arrayref () ) {
push( @{$self->{$db_name}{tables}{$table}{columns}}, $arref->[0] );
}
}
} } |
sub set_row_modifier
{ my ( $self, $newtable, $row_modifier ) = @_;
$self->{targetdb}{tables}{$newtable}{row_modify} = $row_modifier; } |
sub source_dbh
{ my ( $self, $arg ) = @_;
( defined $arg ) &&
( $self->{'source_dbh'} = $arg );
return $self->{'source_dbh'}; } |
sub standard_table_transfer
{ my ( $self, $sourcetable, $targettable, $tmpfile ) = @_;
my $sourcedb = $self->source_dbh();
my $targetdb = $self->target_dbh();
my $select = "";
if( exists $self->{targetdb}{tables}{$targettable}{select} ) {
$select = $self->{targetdb}{tables}{$targettable}{select};
} else {
my @newcols = @{$self->{targetdb}{tables}{$targettable}{columns}};
my @oldcols = @{$self->{sourcedb}{tables}{$sourcetable}{columns}};
my %rename;
if( exists $self->{targetdb}{tables}{$targettable}{columnrename} ) {
%rename = %{$self->{targetdb}{tables}{$targettable}{columnrename}};
} else {
%rename = ();
}
for my $colname ( @newcols ) {
my $selname;
if( exists $rename{$colname} ) {
$selname = $rename{$colname};
if( $selname eq "" ) {
$selname = "NULL";
}
} else {
my $colExists = 0;
for my $oldcol ( @oldcols ) {
if( $oldcol eq $colname ) {
$selname = $colname;
$colExists = 1;
last;
}
}
if( ! $colExists ) {
die "Couldnt fill $targettable.$colname\n";
}
}
$select .= " $selname,";
}
chop( $select );
$select = "SELECT $select from $sourcetable";
}
my $sth;
if (defined $self->{targetdb}{tables}{$targettable}{its_a_big_un}) {
$sth = $self->source_dbh()->prepare( $select , { mysql_use_result => 1 } );
}
else {
$sth = $self->source_dbh()->prepare( $select );
}
$sth->execute();
my $row;
if( exists $self->{targetdb}{tables}{$targettable}{row_modify} ) {
my $rowmod = $self->{targetdb}{tables}{$targettable}{row_modify};
while( my $arref = $sth->fetchrow_arrayref() ) {
$row = &{$rowmod}($arref);
print $tmpfile ( join( "\t",@{$row} ),"\n" );
}
} else {
while( my $arref = $sth->fetchrow_arrayref() ) {
$row = join( "\t", @$arref );
print $tmpfile "$row\n";
}
} } |
sub table_rename
{ my ( $self, $oldtable, $newtable ) = @_;
$self->{targetdb}{tables}{$newtable}{link} = $oldtable; } |
sub table_skip
{ my ( $self, $newtable ) = @_;
$self->{targetdb}{tables}{$newtable}{link} = ""; } |
sub target_dbh
{ my ( $self, $arg ) = @_;
( defined $arg ) &&
( $self->{'target_dbh'} = $arg );
return $self->{'target_dbh'}; } |
sub tmp_dir
{ my ( $self, $arg ) = @_;
( defined $arg ) &&
( $self->{'tmp_dir'} = $arg );
return $self->{'tmp_dir'}; } |
sub transfer
{ my $self = shift;
local *FH;
my $tmpdir;
if( ! defined $self->tmp_dir() ) {
$self->close_dbh();
die( "No tmp_dir specified" );
} else {
$tmpdir = $self->tmp_dir();
}
$self->check_possible_transfer();
my @ordered_tables = $self->{targetdb}{dofirst};
my %all_tables = %{$self->{targetdb}{tables}};
for my $tablename ( @ordered_tables ) {
delete $all_tables{$tablename};
}
for my $tablename ( keys %all_tables ) {
push( @ordered_tables, $tablename );
}
for my $tablename ( @ordered_tables ) {
my $skip = 0;
print STDERR "Transfer $tablename ";
open( FH, ">$tmpdir/$tablename.txt" ) or die "cant open dumpfile";
if( exists $self->{targetdb}{tables}{$tablename}{transfer} ) {
my $transfunc = $self->{targetdb}{tables}{$tablename}{transfer};
&$transfunc( $self->source_dbh(), $self->target_dbh(), $tablename,\* FH );
} else {
my $sourcetable;
if( exists $self->{targetdb}{tables}{$tablename}{link} ) {
$sourcetable = $self->{targetdb}{tables}{$tablename}{link};
if( $sourcetable eq "" ) {
$skip = 1;
}
} else {
if( exists $self->{targetdb}{tables}{$tablename}{select} ) {
$sourcetable = undef;
} elsif( ! exists $self->{sourcedb}{tables}{$tablename} ) {
die "Couldnt find source for $tablename. Enter empty sourcetable.";
} else {
$sourcetable = $tablename;
}
}
if( ! $skip ) {
$self->standard_table_transfer( $sourcetable, $tablename,\* FH );
}
}
close FH;
if( ! $skip ) {
$self->target_dbh->do( "load data infile '$tmpdir/$tablename.txt' into table $tablename" );
}
unlink "$tmpdir/$tablename.txt";
print STDERR " finished\n";
}
}
} |
General documentation
No general documentation available.