version 1.9, 2004/04/23 20:30:07
|
version 1.10, 2004/06/11 19:52:12
|
Line 285 sub create_statement_handler {
|
Line 285 sub create_statement_handler {
|
sub clear_sth { $sth=undef; $sth_table=undef;} |
sub clear_sth { $sth=undef; $sth_table=undef;} |
|
|
sub store_metadata { |
sub store_metadata { |
my $dbh = shift(); |
my ($dbh,$tablename,@Metadata)=@_; |
my $tablename = shift(); |
|
my $errors = ''; |
my $errors = ''; |
if (! defined($sth) || |
if (! defined($sth) || |
( defined($tablename) && ($sth_table ne $tablename)) || |
( defined($tablename) && ($sth_table ne $tablename)) || |
Line 294 sub store_metadata {
|
Line 293 sub store_metadata {
|
&create_statement_handler($dbh,$tablename); |
&create_statement_handler($dbh,$tablename); |
} |
} |
my $successcount = 0; |
my $successcount = 0; |
while (my $mdata = shift()) { |
foreach my $mdata (@Metadata) { |
next if (ref($mdata) ne "HASH"); |
next if (ref($mdata) ne "HASH"); |
my @MData; |
my @MData; |
foreach my $field (@Metadata_Table_Description) { |
foreach my $field (@Metadata_Table_Description) { |
if (exists($mdata->{$field->{'name'}})) { |
my $fname = $field->{'name'}; |
if ($mdata->{$field->{'name'}} eq 'nan') { |
if (exists($mdata->{$fname}) && |
|
defined($mdata->{$fname}) && |
|
$mdata->{$fname} ne '') { |
|
if ($mdata->{$fname} eq 'nan' || |
|
$mdata->{$fname} eq '') { |
push(@MData,'NULL'); |
push(@MData,'NULL'); |
} else { |
} else { |
push(@MData,$mdata->{$field->{'name'}}); |
push(@MData,$mdata->{$fname}); |
} |
} |
} else { |
} else { |
push(@MData,undef); |
push(@MData,undef); |
Line 314 sub store_metadata {
|
Line 317 sub store_metadata {
|
} else { |
} else { |
$errors = join(',',$errors,$sth->errstr); |
$errors = join(',',$errors,$sth->errstr); |
} |
} |
|
$errors =~ s/^,//; |
} |
} |
if (wantarray()) { |
if (wantarray()) { |
return ($successcount,$errors); |
return ($successcount,$errors); |
Line 342 The array reference is the same one retu
|
Line 346 The array reference is the same one retu
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub lookup_metadata { |
sub lookup_metadata { |
my ($dbh,$condition,$fetchparameter) = @_; |
my ($dbh,$condition,$fetchparameter,$tablename) = @_; |
|
$tablename = 'metadata' if (! defined($tablename)); |
my $error; |
my $error; |
my $returnvalue=[]; |
my $returnvalue=[]; |
my $request = 'SELECT * FROM metadata'; |
my $request = 'SELECT * FROM '.$tablename; |
if (defined($condition)) { |
if (defined($condition)) { |
$request .= ' WHERE '.$condition; |
$request .= ' WHERE '.$condition; |
} |
} |
Line 374 sub lookup_metadata {
|
Line 379 sub lookup_metadata {
|
|
|
=item delete_metadata() |
=item delete_metadata() |
|
|
Not implemented yet |
Removes a single metadata record, based on its url. |
|
|
|
Inputs: $dbh, the database handler. |
|
$tablename, the name of the metadata table to remove from. default: 'metadata' |
|
$url, the url of the resource to remove from the metadata database. |
|
|
|
Returns: undef on success, dbh errorstr on failure. |
|
|
=cut |
=cut |
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub delete_metadata {} |
sub delete_metadata { |
|
my ($dbh,$tablename,$url) = @_; |
|
$tablename = 'metadata' if (! defined($tablename)); |
|
my $error; |
|
my $delete_command = 'DELETE FROM '.$tablename.' WHERE url='. |
|
$dbh->quote($url); |
|
$dbh->do($delete_command); |
|
if ($dbh->err) { |
|
$error = $dbh->errstr(); |
|
} |
|
return $error; |
|
} |
|
|
|
###################################################################### |
|
###################################################################### |
|
|
|
=pod |
|
|
|
=item update_metadata |
|
|
|
Updates metadata record in mysql database. It does not matter if the record |
|
currently exists. Fields not present in the new metadata will be taken |
|
from the current record, if it exists. To delete an entry for a key, set |
|
it to "" or undef. |
|
|
|
Inputs: |
|
$dbh, database handle |
|
$newmetadata, hash reference containing the new metadata |
|
$tablename, metadata table name. Defaults to 'metadata'. |
|
|
|
Returns: |
|
$error on failure. undef on success. |
|
|
|
=cut |
|
|
|
###################################################################### |
|
###################################################################### |
|
sub update_metadata { |
|
my ($dbh,$tablename,$newmetadata)=@_; |
|
my $error; |
|
$tablename = 'metadata' if (! defined($tablename)); |
|
if (! exists($newmetadata->{'url'})) { |
|
$error = 'Unable to update: no url specified'; |
|
} |
|
return $error if (defined($error)); |
|
# |
|
# Retrieve current values |
|
my $row; |
|
($error,$row) = &lookup_metadata($dbh, |
|
' url='.$dbh->quote($newmetadata->{'url'}), |
|
undef,$tablename); |
|
return $error if ($error); |
|
my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash(@{$row->[0]}); |
|
# |
|
# Update metadata values |
|
while (my ($key,$value) = each(%$newmetadata)) { |
|
$metadata{$key} = $value; |
|
} |
|
# |
|
# Delete old data (deleting a nonexistant record does not produce an error. |
|
$error = &delete_metadata($dbh,$tablename,$newmetadata->{'url'}); |
|
return $error if (defined($error)); |
|
# |
|
# Store updated metadata |
|
my $success; |
|
($success,$error) = &store_metadata($dbh,$tablename,\%metadata); |
|
return $error; |
|
} |
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
Line 719 sub escape {
|
Line 797 sub escape {
|
return $str; |
return $str; |
} |
} |
|
|
|
|
|
|
|
|
1; |
1; |
|
|
__END__; |
__END__; |