From 8e808ffbf7b7b28a1cdfda3d188cc156a2e879d9 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Wed, 6 Sep 2006 02:18:26 +0000 Subject: Bug 351098: Make Bugzilla::Object able to update objects in the database, and make Bugzilla::Keyword use it Patch By Max Kanat-Alexander r=LpSolit, a=myk --- Bugzilla/Object.pm | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 122 insertions(+), 6 deletions(-) (limited to 'Bugzilla/Object.pm') diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index 67fb707e6..f89a371f2 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -12,6 +12,10 @@ # # The Original Code is the Bugzilla Bug Tracking System. # +# The Initial Developer of the Original Code is Everything Solved. +# Portions created by Everything Solved are Copyright (C) 2006 +# Everything Solved. All Rights Reserved. +# # Contributor(s): Max Kanat-Alexander use strict; @@ -115,6 +119,48 @@ sub new_from_list { sub id { return $_[0]->{'id'}; } sub name { return $_[0]->{'name'}; } +############################### +#### Methods #### +############################### + +sub set { + my ($self, $field, $value) = @_; + + # This method is protected. It's used to help implement set_ functions. + caller->isa('Bugzilla::Object') + || ThrowCodeError('protection_violation', + { caller => caller, + superclass => __PACKAGE__, + function => 'Bugzilla::Object->set' }); + + my $validators = $self->VALIDATORS; + if (exists $validators->{$field}) { + my $validator = $validators->{$field}; + $value = $self->$validator($value); + } + + $self->{$field} = $value; +} + +sub update { + my $self = shift; + + my $dbh = Bugzilla->dbh; + my $table = $self->DB_TABLE; + my $id_field = $self->ID_FIELD; + + my $columns = join(', ', map {"$_ = ?"} $self->UPDATE_COLUMNS); + my @values; + foreach my $column ($self->UPDATE_COLUMNS) { + my $value = $self->{$column}; + trick_taint($value) if defined $value; + push(@values, $value); + } + + $dbh->do("UPDATE $table SET $columns WHERE $id_field = ?", undef, + @values, $self->id); +} + ############################### #### Subroutines ###### ############################### @@ -152,7 +198,8 @@ sub run_create_validators { foreach my $field (sort keys %$params) { my $value; if (exists $validators->{$field}) { - $value = &{$validators->{$field}}($params->{$field}); + my $validator = $validators->{$field}; + $value = $class->$validator($params->{$field}); } else { $value = $params->{$field}; @@ -254,15 +301,34 @@ C. This should be an array. =item C A hashref that points to a function that will validate each param to -C. Each function in this hashref will be passed a single -argument, the value passed to C for that field. These -functions should call L if they fail. -They must return the validated value. +L. + +Validators are called both by L and L. When +they are called by L, the first argument will be the name +of the class (what we normally call C<$class>). + +When they are called by L, the first argument will be +a reference to the current object (what we normally call C<$self>). + +The second argument will be the value passed to L or +Lfor that field. + +These functions should call L if they fail. + +The validator must return the validated value. + +=item C + +A list of columns to update when L is called. +If a field can't be changed, it shouldn't be listed here. (For example, +the L usually can't be updated.) =back =head1 METHODS +=head2 Constructors + =over =item C @@ -293,7 +359,7 @@ They must return the validated value. =back -=head1 SUBROUTINES +=head2 Database Manipulation =over @@ -330,6 +396,56 @@ Returns: Two arrayrefs. The first is an array of database field names. The second is an untainted array of values that should go into those fields (in the same order). +=item C + +Saves the values currently in this object to the database. +Only the fields specified in L will be +updated. Returns nothing and takes no parameters. + +=back + +=head2 Subclass Helpers + +These functions are intended only for use by subclasses. If +you call them from anywhere else, they will throw a C. + +=over + +=item C + +=over + +=item B + +Sets a certain hash member of this class to a certain value. +Used for updating fields. Calls the validator for this field, +if it exists. Subclasses should use this function +to implement the various C mutators for their different +fields. + +See L for more information. + +=item B + +=over + +=item C<$field> - The name of the hash member to update. This should +be the same as the name of the field in L, if it exists there. + +=item C<$value> - The value that you're setting the field to. + +=back + +=item B (nothing) + +=back + +=back + +=head1 CLASS FUNCTIONS + +=over + =item C Description: Returns all objects in this table from the database. -- cgit v1.2.3-24-g4f1b