summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Object.pm
diff options
context:
space:
mode:
authormkanat%bugzilla.org <>2007-04-19 11:04:54 +0200
committermkanat%bugzilla.org <>2007-04-19 11:04:54 +0200
commit29ab35c232fdff053f78b18598bfbe6a13f29826 (patch)
tree5763e4609f0b96bcd5afbb93fdcc10924f51dc87 /Bugzilla/Object.pm
parent0e2a60dc182c15159110e996b370fe1640af0fa4 (diff)
downloadbugzilla-29ab35c232fdff053f78b18598bfbe6a13f29826.tar.gz
bugzilla-29ab35c232fdff053f78b18598bfbe6a13f29826.tar.xz
Bug 372531: "match" should be a generic function in Bugzilla::Object
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=LpSolit
Diffstat (limited to 'Bugzilla/Object.pm')
-rw-r--r--Bugzilla/Object.pm69
1 files changed, 69 insertions, 0 deletions
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<match>
+
+=over
+
+=item B<Description>
+
+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<AND>, so adding more criteria will give you
+a smaller set of results, not a larger set.
+
+=item B<Params>
+
+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<NULL> and C<NOT_NULL>,
+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-E<gt>get_all]>.
+
+=item B<Returns>
+
+An arrayref of objects, or an empty arrayref if there are no matches.
+
+=back
+
=back
=head2 Database Manipulation