From 1760a3198797776a8ca05de645b529cb40998b14 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Sat, 9 Sep 2006 05:01:27 +0000 Subject: Bug 323239: Move CC insertion from post_bug.cgi to Bugzilla::Bug Patch By Max Kanat-Alexander r=LpSolit, r=bkor, a=myk --- Bugzilla/Object.pm | 88 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 19 deletions(-) (limited to 'Bugzilla/Object.pm') diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index f89a371f2..8de20d5f3 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -169,22 +169,19 @@ sub create { my ($class, $params) = @_; my $dbh = Bugzilla->dbh; + $class->check_required_create_fields($params); + my $field_values = $class->run_create_validators($params); + return $class->insert_create_data($field_values); +} + +sub check_required_create_fields { + my ($class, $params) = @_; + foreach my $field ($class->REQUIRED_CREATE_FIELDS) { - ThrowCodeError('param_required', + ThrowCodeError('param_required', { function => "${class}->create", param => $field }) if !exists $params->{$field}; } - - my ($field_names, $values) = $class->run_create_validators($params); - - my $qmarks = '?,' x @$values; - chop($qmarks); - my $table = $class->DB_TABLE; - $dbh->do("INSERT INTO $table (" . join(', ', @$field_names) - . ") VALUES ($qmarks)", undef, @$values); - my $id = $dbh->bz_last_key($table, $class->ID_FIELD); - - return $class->new($id); } sub run_create_validators { @@ -192,7 +189,7 @@ sub run_create_validators { my $validators = $class->VALIDATORS; - my (@field_names, @values); + my %field_values; # We do the sort just to make sure that validation always # happens in a consistent order. foreach my $field (sort keys %$params) { @@ -206,12 +203,30 @@ sub run_create_validators { } # We want people to be able to explicitly set fields to NULL, # and that means they can be set to undef. - trick_taint($value) if defined $value; + trick_taint($value) if defined $value && !ref($value); + $field_values{$field} = $value; + } + + return \%field_values; +} + +sub insert_create_data { + my ($class, $field_values) = @_; + my $dbh = Bugzilla->dbh; + + my (@field_names, @values); + while (my ($field, $value) = each %$field_values) { push(@field_names, $field); push(@values, $value); } - return (\@field_names, \@values); + my $qmarks = '?,' x @field_names; + chop($qmarks); + my $table = $class->DB_TABLE; + $dbh->do("INSERT INTO $table (" . join(', ', @field_names) + . ") VALUES ($qmarks)", undef, @values); + my $id = $dbh->bz_last_key($table, $class->ID_FIELD); + return $class->new($id); } sub get_all { @@ -382,7 +397,35 @@ Notes: In order for this function to work in your subclass, type in the database. Your subclass also must define L and L. -=item C + Subclass Implementors: This function basically just + calls L, then + L, and then finally + L. So if you have a complex system that + you need to implement, you can do it by calling these + three functions instead of C. + +=item C + +=over + +=item B + +Part of L. Throws an error if any of the L +have not been specified in C<$params> + +=item B + +=over + +=item C<$params> - The same as C<$params> from L. + +=back + +=item B (nothing) + +=back + +=item C Description: Runs the validation of input parameters for L. This subroutine exists so that it can be overridden @@ -392,9 +435,16 @@ Description: Runs the validation of input parameters for L. Params: The same as L. -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). +Returns: A hash, in a similar format as C<$params>, except that + these are the values to be inserted into the database, + not the values that were input to L. + +=item C + +Part of L. + +Takes the return value from L and inserts the +data into the database. Returns a newly created object. =item C -- cgit v1.2.3-24-g4f1b