From: <rv...@us...> - 2009-06-11 04:02:18
|
Revision: 19 http://treebase.svn.sourceforge.net/treebase/?rev=19&view=rev Author: rvos Date: 2009-06-11 01:37:41 +0000 (Thu, 11 Jun 2009) Log Message: ----------- Added clarifying comments on sqldump script. Modified Paths: -------------- trunk/treebase-core/src/main/perl/dump/sqldump Modified: trunk/treebase-core/src/main/perl/dump/sqldump =================================================================== --- trunk/treebase-core/src/main/perl/dump/sqldump 2009-05-29 22:52:00 UTC (rev 18) +++ trunk/treebase-core/src/main/perl/dump/sqldump 2009-06-11 01:37:41 UTC (rev 19) @@ -1,58 +1,87 @@ #!/usr/bin/perl -use CIPRES::TreeBase::DBIUtil qw(get_colnames get_coltypes); +use CIPRES::TreeBase::DBIUtil; use CIPRES::TreeBase::RecDumper; use Getopt::Std; -my %opt = ('x' => 0); +my %opt = ( 'x' => 0 ); getopts('x', \%opt) or usage(); +# database table to dump out, provided as command line argument my $table = shift || usage(); + +# output file handle, by default to STDOUT, or... my $ofh = \*STDOUT; -if (@ARGV) { + +# ...second command line argument is file name... +if ( @ARGV ) { my $ofile = shift; + + # ...try to open a write handle to it... open my($fh), ">", $ofile or die "Couldn't write output file '$ofile': $!; aborting"; + + # ...and use that... $ofh = $fh; } +# create database handle my $dbh = CIPRES::TreeBase::DBIUtil->dbh or die "Couldn't connect to database: " . DBI->errstr; -$dbh->{RaiseError} = 1; +$dbh->{'RaiseError'} = 1; { - my @names = get_colnames($dbh, $table); - my @types = get_coltypes($dbh, $table); - my $dumper = - CIPRES::TreeBase::RecDumper->new( - FIELDS => \@names, - TYPES => \@types, - TABLE => $table - ) or die "??"; + # only using these two functions once, so for clarity as to where they + # originate let's use the fully qualified names. + my @names = CIPRES::TreeBase::DBIUtil::get_colnames($dbh, $table); + my @types = CIPRES::TreeBase::DBIUtil::get_coltypes($dbh, $table); + + # instantiate a RecDumper object, which will format a row-as-array + # into an insert statement + my $dumper = CIPRES::TreeBase::RecDumper->new( + 'FIELDS' => \@names, + 'TYPES' => \@types, + 'TABLE' => $table + ) or die "Couldn't instantiate CIPRES::TreeBase::RecDumper"; + + # give dumper the output handle to write to, i.e. either STDOUT + # or a file specified on the command line $dumper->set_output($ofh); { - my $q = qq{select * from $table}; - my $sth = $dbh->prepare($q); - $sth->execute(); - - my $row; - if ($opt{x}) { - my $count = 0; - while ($row = $sth->fetchrow_arrayref) { - ++$count; - $dumper->rec(@$row); - print STDERR "\r$count" if $count % 1000 == 0; - } - } else { - $dumper->rec(@$row) while $row = $sth->fetchrow_arrayref; - } - - $sth->finish(); - print STDERR "\n" if $opt{x}; + # get all rows from $table + my $q = qq{select * from $table}; + my $sth = $dbh->prepare($q); + $sth->execute(); + + my $row; + + # if -x is provided, print out progress counter + if ( $opt{'x'} ) { + my $count = 0; + while ( $row = $sth->fetchrow_arrayref ) { + ++$count; + $dumper->rec(@$row); + print STDERR "\r$count" if $count % 1000 == 0; + } + } + + # without -x just dump the records + else { + while ( $row = $sth->fetchrow_arrayref ) { + $dumper->rec(@$row); + } + } + + # finish the statement handler + $sth->finish(); + + # add line break if we're printing a progress counter + print STDERR "\n" if $opt{'x'}; } } +# disconnect from database $dbh->disconnect; exit 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |