[LON-CAPA-cvs] cvs: loncom /metadata_database/LONCAPA lonmetadata.pm
matthew
lon-capa-cvs@mail.lon-capa.org
Mon, 12 Jan 2004 21:32:20 -0000
matthew Mon Jan 12 16:32:20 2004 EDT
Modified files:
/loncom/metadata_database/LONCAPA lonmetadata.pm
Log:
Implement store_metadata. lookup_metadata is not tested in the least.
Index: loncom/metadata_database/LONCAPA/lonmetadata.pm
diff -u loncom/metadata_database/LONCAPA/lonmetadata.pm:1.1 loncom/metadata_database/LONCAPA/lonmetadata.pm:1.2
--- loncom/metadata_database/LONCAPA/lonmetadata.pm:1.1 Mon Jan 12 10:07:08 2004
+++ loncom/metadata_database/LONCAPA/lonmetadata.pm Mon Jan 12 16:32:20 2004
@@ -1,6 +1,6 @@
# The LearningOnline Network with CAPA
#
-# $Id: lonmetadata.pm,v 1.1 2004/01/12 15:07:08 matthew Exp $
+# $Id: lonmetadata.pm,v 1.2 2004/01/12 21:32:20 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -165,8 +165,8 @@
Input: None
-Returns: An array of hash references describing the columns and rows
-of the metadata table.
+Returns: An array of hash references describing the columns and indicies
+of the metadata table(s).
=cut
@@ -250,7 +250,63 @@
######################################################################
######################################################################
+{
+ ##
+ ## WARNING: The following cleverness may cause trouble in cases where
+ ## the dbi connection is dropped and recreated - a stale statement
+ ## handler may linger around and cause trouble.
+ ##
+ ## In most scripts, this will work fine. If the dbi is going to be
+ ## dropped and (possibly) later recreated, call &clear_sth. Yes it
+ ## is annoying but $sth appearantly does not have a link back to the
+ ## $dbh, so we can't check our validity.
+ ##
+ my $sth = undef;
+
+sub create_statement_handler {
+ my $dbh = shift();
+ my $request = 'INSERT INTO metadata VALUES(';
+ foreach (@Metadata_Table_Description) {
+ $request .= '?,';
+ }
+ chop $request;
+ $request.= ')';
+ $sth = $dbh->prepare($request);
+ return;
+}
+
+sub clear_sth { $sth=undef; }
+
sub store_metadata {
+ my $dbh = shift();
+ my $errors = '';
+ if (! defined($sth)) {
+ &create_statement_handler($dbh);
+ }
+ my $successcount = 0;
+ while (my $mdata = shift()) {
+ next if (ref($mdata) ne "HASH");
+ my @MData;
+ foreach my $field (@Metadata_Table_Description) {
+ if (exists($mdata->{$field->{'name'}})) {
+ push(@MData,$mdata->{$field->{'name'}});
+ } else {
+ push(@MData,undef);
+ }
+ }
+ $sth->execute(@MData);
+ if (! $sth->err) {
+ $successcount++;
+ } else {
+ $errors = join(',',$errors,$sth->errstr);
+ }
+ }
+ if (wantarray()) {
+ return ($successcount,$errors);
+ } else {
+ return $successcount;
+ }
+}
}
@@ -264,13 +320,38 @@
Inputs: database handle ($dbh) and a hash or hash reference containing
metadata which will be used for a search.
-Returns:
+Returns: scalar with error string on failure, array reference on success.
+The array reference is the same one returned by $sth->fetchall_arrayref().
=cut
######################################################################
######################################################################
-sub lookup_metadata {}
+sub lookup_metadata {
+ my ($dbh,$condition,$fetchparameter) = @_;
+ my $error;
+ my $returnvalue=[];
+ my $request = 'SELECT * FROM metadata';
+ if (defined($condition)) {
+ $request .= ' WHERE '.$condition;
+ }
+ my $sth = $dbh->prepare($request);
+ if ($sth->err) {
+ $error = $sth->errstr;
+ }
+ if (! $error) {
+ $sth->execute();
+ if ($sth->err) {
+ $error = $sth->errstr;
+ } else {
+ $returnvalue = $sth->fetchall_arrayref($fetchparameter);
+ if ($sth->err) {
+ $error = $sth->errstr;
+ }
+ }
+ }
+ return ($error,$returnvalue);
+}
######################################################################
######################################################################