From 91986ae4f25eed69862b8d0b5f176e84339c6052 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Fri, 11 Aug 2006 00:53:07 +0000 Subject: Bug 347061: Create Bugzilla::Object->create and make Bugzilla::Keyword use it Patch By Max Kanat-Alexander r=bkor, a=myk --- Bugzilla/DB/Schema.pm | 2 +- Bugzilla/Install/DB.pm | 3 +++ Bugzilla/Keyword.pm | 34 ++++++++++++++++++++++++ Bugzilla/Object.pm | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) (limited to 'Bugzilla') diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index e997e4187..50785c5b7 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -346,7 +346,7 @@ use constant ABSTRACT_SCHEMA => { keyworddefs => { FIELDS => [ - id => {TYPE => 'INT2', NOTNULL => 1, + id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, name => {TYPE => 'varchar(64)', NOTNULL => 1}, description => {TYPE => 'MEDIUMTEXT'}, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index dfe7f957a..a4ab54260 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -468,6 +468,9 @@ sub update_table_definitions { $dbh->bz_alter_column('flagtypes', 'id', {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); + $dbh->bz_alter_column('keyworddefs', 'id', + {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); + ################################################################ # New --TABLE-- changes should go *** A B O V E *** this point # ################################################################ diff --git a/Bugzilla/Keyword.pm b/Bugzilla/Keyword.pm index 2e0f19f4c..fead77821 100644 --- a/Bugzilla/Keyword.pm +++ b/Bugzilla/Keyword.pm @@ -20,6 +20,9 @@ package Bugzilla::Keyword; use base qw(Bugzilla::Object); +use Bugzilla::Error; +use Bugzilla::Util; + ############################### #### Initialization #### ############################### @@ -32,6 +35,13 @@ use constant DB_COLUMNS => qw( use constant DB_TABLE => 'keyworddefs'; +use constant REQUIRED_CREATE_FIELDS => qw(name description); + +use constant VALIDATORS => { + name => \&_check_name, + description => \&_check_description, +}; + ############################### #### Accessors ###### ############################### @@ -81,6 +91,30 @@ sub get_all_with_bug_count { return $keywords; } +############################### +### Validators ### +############################### + +sub _check_name { + my ($name) = @_; + $name = trim($name); + $name eq "" && ThrowUserError("keyword_blank_name"); + if ($name =~ /[\s,]/) { + ThrowUserError("keyword_invalid_name"); + } + my $keyword = new Bugzilla::Keyword({ name => $name }); + ThrowUserError("keyword_already_exists", { name => $name }) if $keyword; + + return $name; +} + +sub _check_description { + my ($desc) = @_; + $desc = trim($desc); + $desc eq '' && ThrowUserError("keyword_blank_description"); + return $desc; +} + 1; __END__ diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index 56789f584..c250f80fd 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -108,6 +108,45 @@ sub name { return $_[0]->{'name'}; } #### Subroutines ###### ############################### +sub create { + my ($class, $params) = @_; + my $dbh = Bugzilla->dbh; + + my $required = $class->REQUIRED_CREATE_FIELDS; + my $validators = $class->VALIDATORS; + my $table = $class->DB_TABLE; + + foreach my $field ($class->REQUIRED_CREATE_FIELDS) { + ThrowCodeError('param_required', + { function => "${class}->create", param => $field }) + if !exists $params->{$field}; + } + + my (@field_names, @values); + # We do the sort just to make sure that validation always + # happens in a consistent order. + foreach my $field (sort keys %$params) { + my $value; + if (exists $validators->{$field}) { + $value = &{$validators->{$field}}($params->{$field}); + } + else { + $value = $params->{$field}; + } + trick_taint($value); + push(@field_names, $field); + push(@values, $value); + } + + my $qmarks = '?,' x @values; + chop($qmarks); + $dbh->do("INSERT INTO $table (" . join(', ', @field_names) + . ") VALUES ($qmarks)", undef, @values); + my $id = $dbh->bz_last_key($table, 'id'); + + return $class->new($id); +} + sub get_all { my $class = shift; my $dbh = Bugzilla->dbh; @@ -173,6 +212,19 @@ The order that C and C should return objects in. This should be the name of a database column. Defaults to C. +=item C + +The list of fields that B be specified when the user calls +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. + =back =head1 METHODS @@ -210,6 +262,25 @@ C. =over +=item C + +Description: Creates a new item in the database. + Throws a User Error if any of the passed-in params + are invalid. + +Params: C<$params> - hashref - A value to put in each database + field for this object. Certain values must be set (the + ones specified in L), and + the function will throw a Code Error if you don't set + them. + +Returns: The Object just created in the database. + +Notes: In order for this function to work in your subclass, + your subclass's C field must be of C + type in the database. Your subclass also must + define L and L. + =item C Description: Returns all objects in this table from the database. -- cgit v1.2.3-24-g4f1b