Fix setSize to work with Moose attributes. Update the SQL query.

This commit is contained in:
Colin Kuskie 2010-01-13 17:08:34 -08:00
parent 10ca1fd206
commit da6bbd1669
2 changed files with 11 additions and 70 deletions

View file

@ -2255,9 +2255,9 @@ sub setSize {
$sizetest .= $self->get($key);
}
my $size = length($sizetest) + $extra;
$self->session->db->write("update assetData set assetSize=".$size." where assetId=".$self->session->db->quote($self->getId)." and revisionDate=".$self->session->db->quote($self->get("revisionDate")));
$self->session->db->write("update assetData set assetSize=? where assetId=? and revisionDate=?",[$size, $self->getId, $self->revisionDate]);
$self->purgeCache;
$self->{_properties}{assetSize} = $size;
$self->assetSize($size);
}
@ -2291,82 +2291,19 @@ sub write {
my $self = shift;
$self->lastModified(time());
# if keywords were specified, then let's set them the right way
#if (exists $properties->{keywords}) {
# WebGUI::Keyword->new($self->session)->setKeywordsForAsset(
# {keywords=>$properties->{keywords}, asset=>$self});
#}
# check the definition of all properties against what was given to us
# my %setPairs = ();
# my %tableFields = ();
# foreach my $property ($self->getProperties) {
#
# # skip a property unless it was passed in to update
# next unless (exists $properties->{$property});
#
# # get the property definition
# my $propertyDefinition = $self->getProperty($property);
#
# # get a list of the fields available in this table so we don't try to insert
# # something for a field that doesn't exist
# my $table = $propertyDefinition->{tableName};
# unless (exists $tableFields{$table}) {
# my $sth = $self->session->db->read('DESCRIBE `'.$table.'`');
# while (my ($col) = $sth->array) {
# $tableFields{$table}{$col} = 1;
# }
# }
#
# # skip properties that aren't yet in the table
# if (!exists $tableFields{$table}{$property}) {
# $self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$table."'");
# next;
# }
#
# # use the update value
# my $value = $properties->{$property};
# # use the current value because the update value was undef
# unless (defined $value) {
# $value = $self->get($property);
# }
#
# # set the property
# if ($propertyDefinition->{serialize}) {
# $setPairs{$table}{$property} = JSON->new->canonical->encode($value);
# }
# else {
# $setPairs{$table}{$property} = $value;
# }
# $self->{_properties}{$property} = $value;
# }
#
# # if there's anything to update, then do so
# my $db = $self->session->db;
# foreach my $table (keys %setPairs) {
# my @values = values %{$setPairs{$table}};
# my @columnNames = map { $_.'=?' } keys %{$setPairs{$table}};
# push(@values, $self->getId, $self->get("revisionDate"));
# $db->write("update ".$table." set ".join(",",@columnNames)." where assetId=? and revisionDate=?",\@values);
# }
##Get list of classes
##Get properties for only that class
##Write them to the db.
my $db = $self->session->db;
CLASS: foreach my $meta (reverse $self->meta->get_all_class_metas()) {
my $table = $db->quoteIdentifier($meta->tableName);
my @properties = $meta->get_property_list;
my @values = map { $self->$_ } @properties;
my @columnNames = map { $db->quote_identifier($_).'=?' } @properties;
my @columnNames = map { $db->quoteIdentifier($_).'=?' } @properties;
push @values, $self->getId, $self->revisionDate;
$db->write("update ".$table." set ".join(",",@columnNames)." where assetId=? and revisionDate=?",\@values);
}
# we've changed something so we need to update our size
# update the asset's size, which also purges the cache.
$self->setSize();
# we've changed something so cache is no longer valid
$self->purgeCache;
}
#-------------------------------------------------------------------

View file

@ -20,7 +20,7 @@ use Test::More;
use Test::Deep;
use Test::Exception;
plan tests => 31;
plan tests => 34;
my $session = WebGUI::Test->session;
@ -160,9 +160,13 @@ my $session = WebGUI::Test->session;
$session->db->write("replace into assetData (assetId, revisionDate) VALUES (?,?)", [$testId, $revisionDate]);
my $testAsset = WebGUI::Asset->new($session, $testId, $revisionDate);
$testAsset->title('wg8 test title');
$testAsset->lastModified(0);
is $testAsset->assetSize, 0, 'assetSize is 0 by default';
$testAsset->write();
my $testTitle = $session->db->quickScalar('select title from assetData where assetId=? and revisionDate=?',[$testId, $revisionDate]);
is $testTitle, 'wg8 test title', 'data written correctly to db';
isnt $testAsset->lastModified, 0, 'lastModified updated on write';
isnt $testAsset->assetSize, 0, 'assetSize updated on write';
my $testData = $session->db->quickHashRef('select * from assetData where assetId=? and revisionDate=?',[$testId, $revisionDate]);
is $testData->{title}, 'wg8 test title', 'data written correctly to db';
$session->db->write("delete from asset where assetId=?", [$testId]);
$session->db->write("delete from assetData where assetId=?", [$testId]);
}