|
From: <jas...@us...> - 2002-11-21 12:28:51
|
Update of /cvsroot/genex/genex-server/Genex/Reporter
In directory sc8-pr-cvs1:/tmp/cvs-serv9614/Reporter
Modified Files:
Reporter.pm
Log Message:
added insert_matrix()
Index: Reporter.pm
===================================================================
RCS file: /cvsroot/genex/genex-server/Genex/Reporter/Reporter.pm,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Reporter.pm 15 Nov 2002 21:55:17 -0000 1.6
--- Reporter.pm 21 Nov 2002 12:28:48 -0000 1.7
***************
*** 1166,1169 ****
--- 1166,1282 ----
+ =item insert_matrix($class,%args)
+
+
+ This method inserts a matrix of data into the database specified by
+ the DB handle $dbh. Class C<Bio::Genex::Reporter> has no foreign keys of
+ type 'lookup table', so C<insert_matrix> may be invoked as a ordinary
+ class method.
+
+ my $matrix = some_method_returning_data();
+ Bio::Genex::Reporter->insert_matrix(dbh=>$dbh,matrix=>$matrix);
+
+
+
+ Supported %args
+
+ =over
+
+ =item * dbh
+
+ an active database handle
+
+ =item * matrix
+
+ an array ref holding the matrix
+
+
+
+ =back
+
+ B<NOTE:> You must log into the DB with a user/password that has INSERT
+ priveleges in the DB, otherwise you will get a DBI error.
+
+
+
+ =cut
+
+ sub insert_matrix {
+ my ($class,%args) = @_;
+ my $dbh = $args{dbh};
+ assert_dbh($dbh);
+
+ my $table_name = Bio::Genex::Reporter->table_or_viewname($dbh);
+ my @error_args = ();
+ my ($pack,$file,$line,$sub) = caller(0);
+ push(@error_args, caller=>"$pack:$file:$line:$sub");
+ my $matrix = $args{matrix};
+ $dbh->error(@error_args,
+ message=>"Must supply array reference for 'matrix'",
+ no_errstr=>1,
+ )
+ unless ref($matrix) eq 'ARRAY';
+ my $header = shift @{$matrix};
+ my $sql;
+
+
+
+ # add primary key to each row
+ push(@{$header},'rep_pk');
+
+
+ # pre-fetch the next value of the sequence
+ my $seq = 'GENEX_ID_SEQ';
+ my $seq_sql = qq[SELECT nextval('"$seq"'::text)];
+ my $seq_sth = $dbh->prepare($seq_sql)
+ or $dbh->error(@error_args,
+ sql=>$seq_sql,
+ message=>"Couldn't prepare nextval for sequence $seq",
+ );
+
+ $sql = $dbh->create_insert_sql($table_name,
+ $header,
+ );
+ my $sth = $dbh->prepare($sql)
+ or $dbh->error(@error_args,
+ message=>"Couldn't prepare insert sql",
+ sql=>$sql);
+ foreach my $row (@{$matrix}) {
+ $seq_sth->execute()
+ or $dbh->error(@error_args,
+ message=>"Couldn't execute nextval from sequence $seq",
+ sth=>$seq_sth,
+ sql=>$seq_sql);
+ my $pkey = $seq_sth->fetchrow_arrayref();
+ $dbh->error(@error_args,
+ message=>"Couldn't fetch nextval from sequence $seq",
+ sth=>$seq_sth,
+ sql=>$seq_sql)
+ unless defined $pkey && $pkey->[0];
+
+ # DBI won't re-run the SQL until we finish
+ $seq_sth->finish();
+
+ $sth->execute(@{$row},$pkey->[0])
+ or $dbh->error(@error_args,
+ message=>"Couldn't execute insert sql with args: "
+ . join(',',@{$row},$pkey->[0]),
+ sth=>$sth,
+ sql=>$sql);
+
+
+ }
+ my $ga_db = Bio::Genex::GenexAdmin->new();
+ my $description = "inserted " . scalar @{$matrix} . " Bio::Genex::Reporter objects";
+ $ga_db->description($description);
+ $ga_db->insert_db($dbh);
+
+ $dbh->error(@error_args,
+ message=>"Couldn't insert GenexAdmin record")
+ if $dbh->err;
+
+ return 1;
+ }
+
# ObjectTemplate automagically creates a new() method for us
# that method invokes $self->initialize() after first setting all
|