From 29ab35c232fdff053f78b18598bfbe6a13f29826 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Thu, 19 Apr 2007 09:04:54 +0000 Subject: Bug 372531: "match" should be a generic function in Bugzilla::Object Patch By Max Kanat-Alexander r=LpSolit, a=LpSolit --- Bugzilla/Object.pm | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'Bugzilla/Object.pm') diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index ee1be2f9f..7d24c392a 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -23,6 +23,7 @@ use strict; package Bugzilla::Object; +use Bugzilla::Constants; use Bugzilla::Util; use Bugzilla::Error; @@ -134,6 +135,42 @@ sub new_from_list { return $objects; } +# Note: Future extensions to this could be: +# * Accept arrays for an IN clause +# * Add a MATCH_JOIN constant so that we can join against +# certain other tables for the WHERE criteria. +sub match { + my ($invocant, $criteria) = @_; + my $class = ref($invocant) || $invocant; + my $dbh = Bugzilla->dbh; + my $id = $class->ID_FIELD; + my $table = $class->DB_TABLE; + + return [$class->get_all] if !$criteria; + + my (@terms, @values); + foreach my $field (keys %$criteria) { + my $value = $criteria->{$field}; + if ($value eq NOT_NULL) { + push(@terms, "$field IS NOT NULL"); + } + elsif ($value eq IS_NULL) { + push(@terms, "$field IS NULL"); + } + else { + push(@terms, "$field = ?"); + push(@values, $value); + } + } + + my $where = join(' AND ', @terms); + my $ids = $dbh->selectcol_arrayref( + "SELECT $id FROM $table WHERE $where", undef, @values) + || []; + + return $class->new_from_list($ids); +} + ############################### #### Accessors ###### ############################### @@ -462,6 +499,38 @@ A fully-initialized object. Returns: A reference to an array of objects. +=item C + +=over + +=item B + +Gets a list of objects from the database based on certain criteria. + +Basically, a simple way of doing a sort of "SELECT" statement (like SQL) +to get objects. + +All criteria are joined by C, so adding more criteria will give you +a smaller set of results, not a larger set. + +=item B + +A hashref, where the keys are column names of the table, pointing to the +value that you want to match against for that column. + +There are two special values, the constants C and C, +which means "give me objects where this field is NULL or NOT NULL, +respectively." + +If you don't specify any criteria, calling this function is the same +as doing C<[$class-Eget_all]>. + +=item B + +An arrayref of objects, or an empty arrayref if there are no matches. + +=back + =back =head2 Database Manipulation -- cgit v1.2.3-24-g4f1b